Memory Layout of a Program in C
web.eecs.utk.edu
web.eecs.utk.edu
strace ls
execve("/usr/bin/ls", ["ls"], 0x7ffd86646d90 /* 61 vars */) = 0
brk(NULL) = 0x5581b6542000
I see brk(), that's glibc.Source: run strace on almost any useful Linux command (e.g. ls, sort, which, ...), you'll see it makes calls to both brk() and mmap().
https://stackoverflow.com/questions/30542428/does-malloc-use...
there is a mallopt named M_MMAP_THRESHOLD, in general:
If requested memory is less than it, brk() will be used;
If requested memory is larger than or equals to it, mmap() will be used;On OpenBSD, DragonflyBSD, NetBSD and OSX:
> The brk and sbrk functions are historical curiosities left over from earlier days before the advent of virtual memory management.
On FreeBSD:
> The brk() and sbrk() functions are legacy interfaces from before the advent of modern virtual memory management. They are deprecated and not present on the arm64 or riscv architectures.
It's also somewhat discouraged on Solaris:
> The behavior of brk() and sbrk() is unspecified if an application also uses any other memory functions (such as malloc(3C), mmap(2), free(3C)).
Also on 64 bit isn't the last memory page like the first unmappable (I think at last on linux it is).
Lastly isn't there a region of unmapable virtual address space on 64 bit in the middle of the virtual address space due to the chips which are doing mmap not handling full 64bit of virtual address space?? I sadly can't find any info about this but I remember having read about it before? Maybe I mixed something up.
Yes. ARM64 currently has a 48b address space split in two ranges (0 to 00007FFFFFFFFFFF and FFFF800000000000 to FFFFFFFFFFFFFFFF), ARMv8 requires 48b and allows 52 but apparently the sizes of the userspace and kernel regions are configurable (though limited by the chip aka you could reduce the size of the kernel space down from 48b but can't increase it).
I'm not sure how true this is outside of a particular platform/compiler. As far as I'm aware, C doesn't actually define how pointers are represented, only that they are a reference to memory (although null is a special case). Pointers in C are very abstract which allows for much more aggressive optimisations.
And all this is before we get into how memory actually works in practice, such as CPU cache lines.
[1]: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2263.htm#q3...
For another example, the LLVM webassembly backend doesn't put the call stack in the same address space as the heap at all.
That's rather fortunate, instead. Programming (esp. in C) under an operating system/hardware architecture that does not provide this protection is a real pain. Memory protection is a feature that is meant primarily to help developers (to say nothing about security).
As it turns out, the first 8 pages on our hydra machines are void. This means that trying to read to or write from any address from 0 to 0xffff will result in a segmentation violation.
I have similar issue. I was following "A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux"[0]. And decided to put the start of .text section at virtual address 0x0:
; tiny.asm
BITS 32
org 0x0
;
; (the same as the one in the teensy elf tutorial)
;
It results in segmentation fault when ran as normal user. But fine when ran as super user. Changing the code to use address 0x10000 fix the problem.My question: Is my issue because I create an elf that has .text section inside that void region? Is this void region documented somewhere? What purpose does it serve?
[0]: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.ht...
https://wiki.debian.org/mmap_min_addr
It's a security feature meant to protect the kernel from null pointer attacks.
Thanks