Live data from Hacker News

Memory Layout of a Program in C

web.eecs.utk.edu

21–30 of 38 posts

Re: Memory Layout of a Program in C

#21
> Unfortunately, you cannot access all elements of memory

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).

Re: Memory Layout of a Program in C

#22
A noob question here.

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...

Re: Memory Layout of a Program in C

#23
post #11

Earlier quoted context omitted.

brk() is undefined behavior if mmap is ever called.

Can you post a definite source supporting this?

There isn't one, because it's wrong.

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().

Re: Memory Layout of a Program in C

#24
post #22

A noob question here. 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…

You may be running afoul of mmap_min_addr:

https://wiki.debian.org/mmap_min_addr

It's a security feature meant to protect the kernel from null pointer attacks.

Re: Memory Layout of a Program in C

#25
post #14

Earlier quoted context omitted.

Yes you can implement C in other ways (I've worked on a C JIT that abstracts from this flat memory model, for example) but come on we all know this is how C works on most machines most of the time and they shouldn't need to add a lot of disclaimers that it could theoretically be done a different way when they're just trying to raise awareness of how things work in practice.

The issue is that C does not work that way on modern machines. Not that old Alpha machines had doubleword aligned pointer and no byte or word load instructions. So indexes into the array had to be multiples of 4. More important, aliasing rules preclude treating memory like one big array: https://gist.github.com/shafik/848ae25ee209f698763cffee272a5... . C99 and newer go to some lengths to permit the optimizer for trea…

Nitpick: The same rules were also present in C89.

Re: Memory Layout of a Program in C

#26
post #11
post #4

Earlier quoted context omitted.

strace ls execve("/usr/bin/ls", ["ls"], 0x7ffd86646d90 /* 61 vars */) = 0 brk(NULL) = 0x5581b6542000 I see brk(), that's glibc.

brk() is undefined behavior if mmap is ever called.

I was wrong, it's only if you use MAP_FIXED to map an overlapping area.

Re: Memory Layout of a Program in C

#27

Earlier quoted context omitted.

Can you post a definite source supporting this?

There isn't one, because it's wrong. 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().

The OP admitted it was wrong, but just because everyone does it doesn't mean it's not undefined behavior.

Re: Memory Layout of a Program in C

#28
post #4
post #2

So this is a whole lot more complicated these days. There's not one stack but many for the different threads, regions have guard pages typically, and all these regions are setup with mmap (so there's no sbrk syscall anymore) just for starters.

strace ls execve("/usr/bin/ls", ["ls"], 0x7ffd86646d90 /* 61 vars */) = 0 brk(NULL) = 0x5581b6542000 I see brk(), that's glibc.

Depends on system. In FreeBSD brk() syscall doesn’t even exist on newer platforms, such as RISC-V or aarch64.

Re: Memory Layout of a Program in C

#29
post #4

Earlier quoted context omitted.

strace ls execve("/usr/bin/ls", ["ls"], 0x7ffd86646d90 /* 61 vars */) = 0 brk(NULL) = 0x5581b6542000 I see brk(), that's glibc.

Does malloc() use brk() or 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;

This is linux-specific though. Non-glibc mallocs have different tuning and many have completely foresworn brk/sbrk (openbsd and dragonflybsd mallocs haven't used brk in more than a decade[0]). In fact brk/sbrk is deprecated in every BSD:

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)).

[0] https://bugs.dragonflybsd.org/issues/84

Re: Memory Layout of a Program in C

#30
post #28
post #4

Earlier quoted context omitted.

strace ls execve("/usr/bin/ls", ["ls"], 0x7ffd86646d90 /* 61 vars */) = 0 brk(NULL) = 0x5581b6542000 I see brk(), that's glibc.

Depends on system. In FreeBSD brk() syscall doesn’t even exist on newer platforms, such as RISC-V or aarch64.

In OpenBSD, brk/sbrk still exist but they removed its use from malloc something like 15 years ago.
Post reply on HN