Making Minimalist Web Server in Assembly on Linux (x64)
Vložit
- čas přidán 16. 02. 2024
- In this video I will demonstrate how you can make a simple web server using x64 Assembly and Linux system calls.
I am planning to continue the POC from the video in the following Github project: github.com/nir9/fastws
Documentation I used in the video: flatassembler.net/docs.php?ar... - Věda a technologie
This is straight up sorcery. The code didn’t use a single include, extern, or struct definition. Excellent work!
That the whole point of OS's they build all these sys calls and ABI for the programmers to use it.
@@yestechguy-fj9tm Right, but the point of frameworks and standard libraries is to abstract the implementation of the ABI away from the programmer so we get a unique interface that works the same regardless of the underlying architechture. So the actual syscall number and how the arguments are passed is handled by the library. Though when you know your target, of course you can directly issue the syscall. I haven't done any low level programming on modern systems. I've done a lot during the Win95/98/XP times :)
@@yestechguy-fj9tm It's even more fun when you design your own ISA & ABI...
it's pretty hard to find hard core content like yours. I can learn a lot from your videos, keep going! Greetings from Brazil.
Thanks!
This looks like sorcery, but sorcery is actually all the levels of abstractions uppon which we are used to develop. Very nice content
next: gas compatable
Flip the registers and insert the appropriate tokens. Conversion to and from GAS is pretty simple.
also mormat elf is unnecessary i think@@NOPerative
@@ItsCOMMANDer_ Yes. Format ELF is FAsm exclusive. The format directive is unnecessary with GAS and NAsm.
I love how he shows what documentation to reference, and people still call it sorcery 😂
Legendary. Keep up with the videos. A few of your videos taught me far more than two year's worth of Computer Science courses.
Thanks! I appreciate the compliment :)
Really cool and creative how you derived addresses and call codes from C and injected that into assembly. Makes it more readable and easier to understand very impressive
Thanks :)
Incredible work! I mainly work with JavaScript and when I used to hear people say C is high level it always left me bewildered.
Now I understand.
Great video man, always so cool to see how things happen under the hood. Ty!
fasm is really a great and small assembler. I used it about 18 years ago to write a tiny keyboard hook dll which was just 2.5k in size. At that time Win98 / XP didn't had the low level hooks we have nowadays and keyboard / mouse hooks required a separate DLL which the OS injected into every application to process the hook in the active process space. Though afaik this dll doesn't work anymore. However I think the main reason is that i think it was an x86 (32bit) dll. Back in the day I used to write software with delphi and most applications where single file builds which is generally great for being portable. The keyboard hook dll was simply included as a resource and I unpacked it when it didn't exist yet.
The dll was still quite large but this was mainly because of the import and export tables which require a certain memory alignment by windows.
thx for all the heavy lifting of the pre-knowledge for this video to be made
Thank you for your service sir.
Bro......THIS is what I'm talking about! Thank you for this content!!!!
very nice as always. thanks for the repo
outstanding explantation.
Always creative
Hey! I really love your work! It would be great, if you put the links to the docs you use in the description box, so one doesn't need to retype them.
Thanks! added :)
I love it!
Amazing 🤩
While this is pure wizardry, your explanation is crystal clear and makes everything easy to follow. Outstanding work!
Really nice. Especial how you combine constants from C with assembly. Could you make a video how you work with vim(as I understand you use wsl with vim). But how did you manage to run server on wsl and open page on windows? I mean does windows automatically forwards requests inside wsl?
Thanks! I have a playlist "Vim Tips" with videos about how I work with Vim, when I do Windows dev I work with Vim for Windows and when doing Linux dev I mostly work with Vim running on WSL.
Yes, the request was automatically forwarded to WSL, I didn't configure anything special
@@nirlichtmanThanks for full response. Will check you "Vim Tips"
Love! ❤
Amazed how you can do what I do with 2 huge monitors in a tiny little split up terminal window. 👍
Yah I used to have a dual monitor setup as well but decided a couple of years ago to move back to single monitor due to neck strain
I find that even with an UWQHD monitor, it's hard to find a good way to partition the screen so that it is better than dual screens. I really need a SUWQHD I think... or just keep using dual screens. At least the footprint of a single UWQHD is an improvement.
legend
Very interesting and great job! One question: if you couldn't use the C/Python header files, where would you find the values and methodology?
Another option instead of doing the trick I did with the GCC preprocessor and header files is to manually copy over the values from the header files to the Assembly, but notice that syscall numbers change between different processor architectures so make sure you are copying the correct numbers for your architecture. Another way is to locate these values from the Linux kernel source, for example here is sockaddr_in in the kernel source elixir.bootlin.com/linux/latest/source/include/uapi/linux/in.h#L256
This was so cool, btw shouldn't you have used close() to close the opened file descriptor ? and to get the syscalls values you can check a header in /usr/include/asm/unistd_64.h (as well as unistd_32.h)
Thanks! Yah I skipped doing cleanup/error checking to keep the video short (also have a disclaimer about this on the vid), but I added close in the code on Github. You can indeed get the syscall values directly from the header files as well (on my machine it has a different path), the idea of the preprocessor trick I did in the video is to automate this
I LOVE THIS. Also wanna know how you navigate vim
Thanks! I have a playlist "Vim Tips" with information about that :)
Now lets create full scaleable enterprise app 😂
please make a video about creating a color theme for Vim 🙏
Would it have been possible to create register aliases for the system call arguments?
arg1 equ rdi
arg2 equ rsi
arg3 equ rdx
arg4 equ r10
arg5 equ r8
arg6 equ r9
I am so used to using nasm that this looks foren to me... love the video though!
I am also used to NASM but recently I started using FASM and I like it a lot more :)
Great channel!
Love this stuff bro, but please do xor rdx, rdx instead of mov rdx, 0
🔥🔥🔥🔥 wanna more and more
if i want to run this Minimalist web server in raspberry pi pico w for a simple iot on/off leds and some simple controls
is that will be more challenging ?
Based on your experience, in which scenario would be appropriate to use. NASM/FASM/MASM/etc? Subscribed by the way!
Thanks! You mean which scenarios are appropriate to use Assembly in general? There are all kinds, for example early boot code for bootloaders, using in conjunction with C/C++ code since there are cases where using Assembly for certain parts is required to call certain specific instructions, or optimize a specific part, and of course in extreme cases you can even write full programs with Assembly - like Roller Coaster Tycoon :)
@@nirlichtman Yea the famous game Roller Coaster Tycoon! I'm starting to re-learn C and NASM. Do you also have experience with microcontrollers?
@@biohazardouszxellium Not a lot just playing a little with Arduino, but I definitely plan on learning some Arduino AVR assembly when I have more time, interesting subject!
17:55 Can this segmentation fault be prevented by adding a jmp instruction to a label above accept instruction? I think this might create infinite loop that waits for new incoming connections
Yah, the segfault occurs since I did not exit properly by invoking the exit system call so the execution slided after finishing. I added that jump to the accept instruction in the Github code, that would make it wait for more connections
Okay, thank you!
I like it !
>linux assembly tutorial
>uses windows
video request: make video on how to run a fasm macro in parallel with threading or with
a separate process.
You mean a video explaining how to call assembly code from C ?
no like using thread and epoll using fasm
@@nirlichtman
🤯
I'd love to see the benchmarks. Is it faster than NGINX?
nope, nginx utilizes multiprocessing, this one does not
Just use another app to route the incoming requests to different ports and have each core run a process of this server listening on each of those ports.
Even that can be done via round robin in a fresh assembly project.
As the code is so simple and has no security checks (which is ok when you don't serve client data), it should be blazing fast.
You can even add a counter to save visits by IP in case you are DDoSed, you will still get most basic attacks off your back with a few more lines.
@@TheUnkow so a reverse proxy? 😃
@@interrrp_with_three_rs this is almost certainly faster than nginx for what it does - return a single file. Just the code in nginx to handle processing the request content, parse the url, map to a local file and related security checks/etc would be orders of magnitude more instructions than this entire program.
not really a web server, but a starting point on how to handle sockets.
Next video: writing minimalist web server in x86 machine code
6502
assembly is literally a fancy machine code in english
Next episode: making html templates
In before RollerCoaster Tycoon 3D video in assembly....
Why don't we have the same man page for " socket ", and there is no manual entry for "syscall" , I'm on Linux 6.7.4-arch
If you man -k socket
You see that it's in manual 7.
So man 7 socket
LET'S FUCKLIGN GOOO!!!!! YES YES YES YES YES YES YES YES YES YES YES YES YES YES!!! YES!!
fasm😍
What about for windows ?
That would really be interesting to see the differences in this low level!
Winsock is also planned :)
The lack of fclose is disturbing
Skipped cleanup/error handling in the video to keep it short (also added a disclaimer in the video around the middle). In the Github repo, I added calls to close
Your videos are always so good but you are talking very fast and its difficult to keep track
Thanks for the feedback, will take that into consideration 👍
you can change the speed of the vid at will
Is this web assembly
lmao
On LInux? I see a Windows task bar at the bottom.
He's accessing to a Linux system through Windows
he is using wsl 2
someone make primeagen watch this 😂 dang
If youre just using a C Libraries through syscalls anyway, why write it in asm at all?
I had this same thought, but the bottom line is, it's for fun. If he wasn't using the syscalls or c libs, he'd have to write his own OS to do it.
syscalls are 'plugs' in the kernel. The simplest way to discover the syscall 'names' is to backtrace c 'wrappers' and voila
He's not really using c libraries. The c code is a trick to get constants out of the header files.
you are insaine
That's too high-level. Consider using op-codes and binary-encoded machine instructions that work without an OS, i.e. Web Server in the BIOS.
> he uses windows
My biggest critism is tht NONE of the code you are laying down is commented. There is NO such thing as "self documenting code" especially not assembler which is what I write a significant amount of my Linux code in.
"Code never lies, comments sometimes do"
Comments are fairly pointless unless they help *you* remember something for yourself. If you can’t read the code then umm…yeah. 😬
@@Crux161Comments are not only there for you, they are there for the poor shmuck that has to deep dive into your code maybe even years later.
I have built my entire career around having to fix or maintain code written decades ago that has been hacked on repeatedly making the entire thing an unmaintainable cluster bleep (think of "a patchy web server" becoming the "apache web server" for example).
Comments explain what you are thinking while you code, the logic behind the inner workings. I have also always said that if you have thought about your code long enough to explain it to someone else then you have thought about it long enough to code it otherwise you have not.
NO code is self documenting, specially when you have if/and/but loops nested to the umpteenth level. Every block is an inlined, anonymous function. If people broke blocks out into sub functions it would move towards self documening but it NEVER truly gets there.