Skip to content

Memory Management

Razvan Deaconescu edited this page Apr 17, 2019 · 5 revisions

Memory Management

Code and instructions are stored in the main memory where they are read from, written to or executed by the CPU in the form of processes. Processes allocate and deallocate memory by invoking the operating system. Operating systems make sure the memory of each process is isolated to prevent information leaks and memory corruption. Each process is provided with a virtual address space.

In this chapter we show the high-level and low-level interface for allocating and deallocating memory. We talk about memory areas in the virtual process address space and how allocation occurs in each of them. We discuss valid and invalid memory accesses and best practices in developing memory safe programs.

You will get improved understanding of the common memory operations in a program: allocating, deallocating and accessing. As memory is commonly used as the process virtual address space, you will know the role and specifics of each memory area in the process virtual address space: text, data, bss, stack, heap, libraries. You will get a better grasp of memory access issues and methods of mitigating them.

Contents

instructions (code) and data need to be stored physical memory split in pages virtual memory, virtual address space for each process memory zones: executable, dynamic, access permissions allocating / deallocating memory memory accesses, out of bounds accesses, buffer overflows memory mapping, file mapping

demos:

Each process has a virtual address space with memory areas. Some created at load time, others modified at runtime. 05-memory/exec-vars.

Show new size of data and bss sections by altering program. 05-memory/data-size.

Show virtual address space difference between statically linked and dynamically linked process. Use ../02-development-environment/hello as test case.

Update stack and heap memory area. Show updates to process virtual address space. 05-memory/dynamic-memory-size

Update memory area used when using malloc(). Show malloc() internals, call to brk() or call to mmap(). 05-memory/allocation

Use of mmap() for file mapping. Implement cp using file mapping. 05-memory/copy

Use mprotect() to make area with shellcode executable and run shellcode. 05-memory/mprotect

Use mlock() to lock page to main memory. Show new resident set size.

Show buffer overflow not causing segmentation fault.

Tasks

When does malloc() end execution? Allocate using malloc() on 32 bit system. Allocate using mmap(). Look at process virtual address space using ps and pmap.

Self modifying code: replace function with another function.

Duration of malloc() vs calloc().

Clone this wiki locally