5 ms·
Show HN: Tiny C runtime Linux (rt0), HelloWorld 0.6k (i386), sbrk example added
- oso2k 11y agoOriginal HN announcement/discussion [https://news.ycombinator.com/item?id=8974024 https://news.ycombinator.com/item?id=8974024]. CFLAGS additions produce smaller binaries. HelloWorld in 608 bytes (i386) and 792 bytes (x86_64). An example of sbrk/brk provided in the test folder.
- duaneb 11y agoWhat is sbrk even used for anymore? Surely most heap allocators use mmap/mprotect directly these days. There's no downside. EDIT: Not trying to rain on anyone's parade, I think the syscall mentioned is beside the point of the post. :)
- oso2k 11y agoI agree that sbrk/brk are nearly deprecated. But not all allocators I studied use mprotect or mmap. Most reference material (old OS books & blogs) still refer to sbrk/brk and/or malloc when discussing reference counters or garbage collectors. And Linux has a nice gotcha (see the Linux notes here[1]) in its implementation of the sys_brk syscall. It matches neither of the interfaces described for sbrk or brk exactly that most people are familiar with. For me, however, I have a personal goal to show how to implement the basic newlib porting requirements [2]. [1] http://linux.die.net/man/2/brk http://linux.die.net/man/2/brk [2] http://wiki.osdev.org/Porting_Newlib http://wiki.osdev.org/Porting_Newlib
- duaneb 11y agoYea, sbrk makes sense in a flat address space. I don't think it makes as much sense with virtual memory.
- gull 11y agoA flat address space is the future. You don't need virtual memory when a flat address space can hold all memory addresses for all processes as it does now with 64-bit processors.
- duaneb 11y agoVirtual memory isn't just for giving each process enough space. It's for security reasons, too.
- deleted 11y ago[deleted]
- oso2k 11y agoCould you explain how virtual memory makes a process more secure? My intuition is that it would be another source of contention to manage (like CPU, RAM, IO, etc). Maybe you're considering non-sequential address space an improvement for security. That I could see. ASLR would be another, which, you can see the effect of by multiple runs of t/_end.exe in rt0.
- koenigdavidmj 11y agosbrk competes with mmap, not mprotect. And yes, I would expect mmap to be used in most modern systems. OS X, for instance, just emulates sbrk with a large static buffer, and it has no actual effect on the page tables.
- oso2k 11y ago4MB buffer to be exact [0]. [0] https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA950&lpg=PA950&dq=osx+srbk+4mb&source=bl&ots=OLljV-UpVu&sig=n0NaVzZMEBKMk6FPZAsoOPF6JLo&hl=en&sa=X&ved=0CCgQ6AEwAWoVChMIxJOc7Yy-yAIVzlqICh0v-gov#v=onepage&q=osx%20srbk%204mb&f=false https://books.google.com/books?id=K8vUkpOXhN4C&pg=PA950&lpg=...
- duaneb 11y agoUpdated, sorry, they almost seem like the same API to me—I've never used one without the other. I have no idea whether that's actually the case.