Sysprog 15

1,073 views
1,029 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,073
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Sysprog 15

  1. 1. C/C++ Linux System Programming <ul><ul><li>Session 15 </li></ul></ul><ul><ul><li>User-space System Programming </li></ul></ul><ul><ul><li> – session 5 </li></ul></ul>
  2. 2. Outline <ul><li>Virtual memory / Address space concepts </li></ul><ul><li>Memory Allocation </li></ul><ul><li>Memory Mapping </li></ul><ul><li>Shared Object Loading </li></ul>
  3. 3. Paging <ul><li>System memory is partitioned into pages </li></ul><ul><li>Pages stored in a multi-level hardware table </li></ul><ul><li>Access to a page that does not have an entry causes exception </li></ul><ul><ul><li>Control relinquished to software </li></ul></ul>
  4. 4. Process Address Space <ul><li>Segments: </li></ul><ul><ul><li>Text – code, read and execute </li></ul></ul><ul><ul><li>Data – heap </li></ul></ul><ul><ul><li>Bss – uninitialized data </li></ul></ul><ul><ul><li>Stack – grows downward </li></ul></ul><ul><li>Breakpoint </li></ul><ul><li>Just a representation (used to be accurate) </li></ul>
  5. 5. VM: Paging / address space interaction <ul><li>Each address space has its tables </li></ul><ul><li>On context switch, kernel switches out the active table </li></ul><ul><li>Each segment has its pages (no breakpoint) </li></ul><ul><li>Page fault: </li></ul><ul><ul><li>Read/Exec: </li></ul></ul><ul><ul><ul><li>Demand paging </li></ul></ul></ul><ul><ul><ul><li>Virtual memory: swap </li></ul></ul></ul><ul><ul><li>Writeable: Copy on Write </li></ul></ul>
  6. 6. Heap Memory Allocation <ul><li>void *calloc(size_t nmemb, size_t size); </li></ul><ul><li>void *malloc(size_t size); </li></ul><ul><li>void free(void *ptr); </li></ul><ul><li>void *realloc(void *ptr, size_t size); </li></ul><ul><li>Process -> glibc -> kernel </li></ul><ul><li>int posix_memalign(void **memptr, size_t alignment, size_t size); </li></ul><ul><li>Memory fragmentation </li></ul>
  7. 7. Off the Stack <ul><li>void *alloca(size_t size); </li></ul><ul><li>Fast </li></ul><ul><li>Limited in size </li></ul><ul><ul><li>int getrlimit(int resource, struct rlimit *rlim); </li></ul></ul><ul><ul><li>int setrlimit(int resource, const struct rlimit *rlim); </li></ul></ul><ul><ul><li>RLIMIT_STACK (SIGSEGV when exceeded) </li></ul></ul><ul><li>No fragmentation </li></ul><ul><li>Don't do it in a function call </li></ul>
  8. 8. Kernel <ul><li>System calls </li></ul><ul><ul><li>int brk(void *end_data_segment); </li></ul></ul><ul><ul><li>void *sbrk(intptr_t increment); </li></ul></ul><ul><li>Buddy allocator </li></ul><ul><ul><li>Buffers, size in powers of 2 </li></ul></ul><ul><ul><li>Go for smallest fit </li></ul></ul><ul><ul><li>Two empty blocks can be used as a single of next spot </li></ul></ul>
  9. 9. Glibc <ul><li>On smaller sizes, Object Stack allocator </li></ul><ul><ul><li>Grow and shrink (like a stack) </li></ul></ul><ul><ul><li>When whole stack is freed, it can be released </li></ul></ul><ul><li>On large allocations, anonymous memory maps </li></ul><ul><ul><li>No fragmentation / good for resize </li></ul></ul><ul><ul><li>Page size granularity / always going to kernel </li></ul></ul>
  10. 10. More Kernel Tweaks <ul><li>Huge pages </li></ul><ul><ul><li>On Intel, regular page 4k, huge page 4M </li></ul></ul><ul><ul><li>Kernel: CONFIG_HUGETLB_FS=y, (CONFIG_HUGETLB_PAGE=y) </li></ul></ul><ul><ul><li>/proc/sys/vm/nr_hugepages (# of pages) </li></ul></ul><ul><ul><li>mount -t hugetlbfs none /somedir </li></ul></ul><ul><ul><li>Open /somedir/somfile and mmap it ANONYMOUS </li></ul></ul><ul><li>Overcommitment </li></ul><ul><ul><li>/proc/sys/vm/{overcommit_memory,overcommit_ratio} </li></ul></ul><ul><ul><li>OOM killer </li></ul></ul>
  11. 11. Locking Memory <ul><li>Preventing swapping on a page </li></ul><ul><ul><li>int mlock(const void *addr, size_t len); </li></ul></ul><ul><ul><li>int munlock(const void *addr, size_t len); </li></ul></ul><ul><ul><li>int mlockall(int flags); </li></ul></ul><ul><ul><li>int munlockall(void); </li></ul></ul><ul><li>Why? </li></ul><ul><ul><li>Speed </li></ul></ul><ul><ul><li>Security </li></ul></ul>
  12. 12. Malloc Manipulation <ul><li>int mallopt (int param, int value) </li></ul><ul><ul><li>M_TRIM_THRESHOLD </li></ul></ul><ul><ul><li>M_TOP_PAD </li></ul></ul><ul><ul><li>M_MMAP_THRESHOLD </li></ul></ul><ul><ul><li>M_MMAP_MAX </li></ul></ul><ul><li>struct mallinfo mallinfo (void) </li></ul><ul><ul><li>Stats on memory usage </li></ul></ul>
  13. 13. Malloc Tracing and Checks <ul><li>void mtrace () / void muntrace () </li></ul><ul><li>Checking </li></ul><ul><ul><li>int mcheck (void (*abortfn) (enum mcheck status status)) </li></ul></ul><ul><ul><li>enum mcheck_status mprobe (void *pointer) </li></ul></ul><ul><ul><ul><li>MCHECK_OK / HEAD / TAIL / FREE </li></ul></ul></ul><ul><li>Hooks </li></ul><ul><ul><li>__malloc_hook / realloc_hook / __free_hook </li></ul></ul><ul><ul><li>Not on SUID/SGID programs </li></ul></ul>
  14. 14. Glibc Object Stack Manipulation <ul><li>int obstack_init (struct obstack *obstack_ptr) </li></ul><ul><li>void * obstack_alloc (struct obstack *obstack_ptr, int size) </li></ul><ul><li>void * obstack_copy (struct obstack *obstack_ptr, void *address, int size) </li></ul><ul><li>void obstack_free (struct obstack *obstack_ptr, void *object) </li></ul><ul><ul><li>Frees all the ones above it in stack </li></ul></ul><ul><li>void obstack_grow (struct obstack *obstack_ptr, void *data, int size) </li></ul><ul><li>void * obstack_finish (struct obstack *obstack_ptr) </li></ul>
  15. 15. More mmaps - Shared Object Loading <ul><li>ELF Format </li></ul><ul><ul><li>Interpreter ld </li></ul></ul><ul><ul><li>Symbols </li></ul></ul><ul><ul><li>GOT / PLT </li></ul></ul><ul><li>Mmap </li></ul><ul><ul><li>Copy on write (PRIVATE) </li></ul></ul><ul><li>Ld </li></ul><ul><ul><li>Map the loaded object </li></ul></ul><ul><ul><li>Write out reloc – based adjustments to address space </li></ul></ul>

×