Memory Management

Reference 1

Layout of FreeBSD process in memory and on disk:

![figure-3-3. Layout of FreeBSD process in memory and on disk]

To begin execution of a binary file, kernel:

  • Text portion of the binary is mapped into the low part of the process’s address space. (The first page of the address space is marked as invalid, so that attempts to read or write through a null pointer will fault)
  • Initilized data portion of the file is mapped into the address space following the text.
  • Uninitialized region is mapped with zero-filled memory after the initialized data region.
  • The stack is also created from zero-filled memory. (zero-fill stack to avoid misuse previous old process’s content)

Demanding paging is used to avoid copying into memory the entire text and initialized data portion of a large program:

  • Program is loaded in small pieces(pages) as it is needed rather than all at once before it begins execution.
  • For each page, the kernel records the offset into the executable file of the corresponding data.
  • The first access on each page cause a page-fault trap in the kernel. The page-fault handler reads the correct page of the executable file into the memory

sbrk used to extend uninitialized data area with zero-filled pages. Grows from the initialized data segment. Called Heap.

Above the user stack are areas of memory that created by the system when the process is started.

  • number of arguments (argc)
  • pointer of argument vector (argv)
  • pointer of processor env vector (envp)
  • argument and environment strings
  • signal code, used when system delivers signals to the process.
  • finally the ps_strings structure, used by ps the locate the argv of the process.

Shared libraries:

  • When the executable is run, a set of shared libraries containing the routines that is needs to use are mapped into its address space as part of its startup.
    • convention is to map then just below the lower limit of the stack, since stack is not allowd to grow below the limit. But if the stack needs grow during execution, process must be restarted by itself (exec) with a larger stack and replace shared library.
    • alternatively, place shared lib just above the heap limit. But this means heap cannot increase its limit during normal execution. Therefore, FreeBSD chooses to put shared lib below stack limit.
  • The first time it calls a routine, that routine is located in the shared library and a dynamic linkage is created to it.

  1. FreeBSD Book. ↩
Created Jun 19, 2020 // Last Updated Jun 19, 2020

If you could revise
the fundmental principles of
computer system design
to improve security...

... what would you change?