Inside ptmalloc2. Peng Xu Sep 14, 2013

Similar documents
2

Debugging: Love It, Hate It Or Reverse It?

Introduction to Operating Systems (Part III)

CSE / / 60567: Computer Security. Software Security 4

Memory management. Johan Montelius KTH

Secure Software Programming and Vulnerability Analysis

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Intrusion Detection and Malware Analysis

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Optimizing Dynamic Memory Management

DEBUGGING: TESTING WS 2017/2018. Martina Seidl Institute for Formal Models and Verification

CSE 333 Lecture 6 - system calls, intro to file I/O

CSE 333 Lecture 8 - system calls, intro to file I/O

C provides some basic facilities C libraries help make those primitive facilities useful

==Phrack Inc.== Volume 0x0b, Issue 0x39, Phile #0x08 of 0x12. --=[ Disclaimer ]= //

CHIRP - Bug # Baofeng 997-S - CHIRP - No Response Issue Description. I have reviewed redmine ticket 1957 and the rejected ticket 2471

Operating System Labs. Yuanbin Wu

Memory Corruption 2. Advanced Internet Security

CSE 509: Computer Security

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Operating systems. Lecture 9

Linux Kernel Compilation & Working of malloc() in Linux Operating Systems

API 퍼징을통한취약점탐지 카이스트 차상길

Exploring System Calls with Strace by Mike Hamrick

CS Computer Systems. Lecture 8: Free Memory Management

Virtual Memory: Systems

CMPSC 311 Exam 2. March 27, 2015

Memory management. Single process. Multiple processes. How to: All memory assigned to the process Addresses defined at compile time

Dynamic Memory Allocation. Zhaoguo Wang

Low-Level I/O, C++ Preview

Lecture 07 Heap control data. Stephen Checkoway University of Illinois at Chicago

mmalloc The GNU memory-mapped malloc package Fred Fish Cygnus Support Mike Haertel Free Software Foundation

Intro to File I/O, System Calls

Virtual Memory. Alan L. Cox Some slides adapted from CMU slides

Lecture 8 Dynamic Memory Allocation

Deterministic Memory Allocation for Mission-Critical Linux

Heap Off by 1 Overflow Illustrated. Eric Conrad October 2007

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Class Information ANNOUCEMENTS

Understanding Pointers

ECE 650 Systems Programming & Engineering. Spring 2018

PROJECT 2 - MEMORY ALLOCATOR Computer Systems Principles. October 1, 2010

Topics: Virtual Memory (SGG, Chapter 09) CS 3733 Operating Systems

Project 2 Overview: Part A: User space memory allocation

CSCI 4061: Virtual Memory

Dynamic Memory: Alignment and Fragmentation

Applications of. Virtual Memory in. OS Design

malloc() is often used to allocate chunk of memory dynamically from the heap region. Each chunk contains a header and free space (the buffer in which

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Explicit Stabilisation. Verifying malloc. Natural specifications. Natural specifications. Including malloc s internal state. The crux of the proof

System-Level I/O. William J. Taffe Plymouth State University. Using the Slides of Randall E. Bryant Carnegie Mellon University

Foundations of Computer Systems

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: November 16, 11:30 PM

Programmation Système Cours 5 Memory Mapping

Memory Management. Outline. Memory. Virtual Memory. Instructor: Dr. Tongping Liu

ECE 598 Advanced Operating Systems Lecture 10

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

CSE 127 Computer Security

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Introduction to Linux, for Embedded Engineers Tutorial on Virtual Memory. Feb. 22, 2007 Tetsuyuki Kobayashi Aplix Corporation. [translated by ikoma]

My malloc: mylloc and mhysa. Johan Montelius HT2016

ECE 598 Advanced Operating Systems Lecture 12

Outline. 1 Details of paging. 2 The user-level perspective. 3 Case study: 4.4 BSD 1 / 19

Dynamic Memory Management

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

o Code, executable, and process o Main memory vs. virtual memory

CS Week 8 Lab Assignment 3. Teaching Assistant Henrique Potter

Dynamic Memory Allocation I Nov 5, 2002

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

Dnmaloc: a more secure memory allocator

Memory Management. CS449 Fall 2017

ANITA S SUPER AWESOME RECITATION SLIDES

Memory Management. CSC215 Lecture

Dynamic Memory Allocation

Dynamic Memory Allocation I

CS201: Lab #4 Writing a Dynamic Storage Allocator

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Dynamic memory. EECS 211 Winter 2019

Intel P The course that gives CMU its Zip! P6/Linux Memory System November 1, P6 memory system. Review of abbreviations

CS61, Fall 2012 Section 2 Notes

ECEN 449 Microprocessor System Design. Review of C Programming

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

File Systems: Consistency Issues

DYNAMIC MEMORY ALLOCATION ON REAL-TIME LINUX

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CS C Primer. Tyler Szepesi. January 16, 2013

Foundations of Computer Systems

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

Dynamic Memory Management! Goals of this Lecture!

CSE506: Operating Systems CSE 506: Operating Systems

10.1. CS356 Unit 10. Memory Allocation & Heap Management

More Vulnerabilities (buffer overreads, format string, integer overflow, heap overflows)

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Process Layout, Function Calls, and the Heap

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

The course that gives CMU its Zip! I/O Nov 15, 2001

Memory System Case Studies Oct. 13, 2008

Intel P6 (Bob Colwell s Chip, CMU Alumni) The course that gives CMU its Zip! Memory System Case Studies November 7, 2007.

Illustration of the problem

Transcription:

Inside ptmalloc2 Peng Xu peng.p.xu@ericsson.com Sep 14, 2013

Part I basic concept and data structure

Memory Translation

process memory layout kernel space command line and environment variables stack heap user space read from program file by exec.bss data code

System call for memory management system call list 1. brk 2. sbrk is a wrapper of brk 3. mmap why it s not a good idea to call these system call in app directly 1. System call is very expensive. 2. Glibc can balance the time and space complexity very well. test.c 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 int main(void) { 5 char* orig_heap_end = (char*)sbrk(0); 6 char* cur_heap_end = (char*)sbrk(20); 7 sprintf(orig_heap_end,"%s","hello"); 8 return 0; 9 }

heap layout after first malloc 1 #include <stdlib.h> 2 int main(void) { 3 char* ptr = (char*)malloc(1); 4 free(ptr); 5 return 0; 6 } 0 7 15 23 31 top chunk heap malloc state used malloc chunk heap info

strace output execve("./test", ["./test"], [/* 47 vars */]) = 0 brk(0) = 0x9dd9000 mmap2(null, 4096, PROT_READ PROT_WRITE, MAP_PRIVATE MAP_ANONYMOUS, -1, 0) = 0xb7714000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open("/etc/ld.so.cache", O_RDONLY O_CLOEXEC) = 3 fstat64(3, {st_mode=s_ifreg 0644, st_size=132397,...}) = 0 mmap2(null, 132397, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb76f3000 close(3) = 0 open("/usr/lib/libc.so.6", O_RDONLY O_CLOEXEC) = 3 read(3, "\177ELF\1\1\1\3\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0000\233\1\0004\0\0\0"..., 512) = 512 fstat64(3, {st_mode=s_ifreg 0755, st_size=9127208,...}) = 0 mmap2(null, 1763972, PROT_READ PROT_EXEC, MAP_PRIVATE MAP_DENYWRITE, 3, 0) = 0xb7544000 mprotect(0xb76ec000, 4096, PROT_NONE) = 0 mmap2(0xb76ed000, 12288, PROT_READ PROT_WRITE, MAP_PRIVATE MAP_FIXED MAP_DENYWRITE, 3, 0x1a8000) = 0xb76ed mmap2(0xb76f0000, 10884, PROT_READ PROT_WRITE, MAP_PRIVATE MAP_FIXED MAP_ANONYMOUS, -1, 0) = 0xb76f0000 close(3) = 0 mmap2(null, 4096, PROT_READ PROT_WRITE, MAP_PRIVATE MAP_ANONYMOUS, -1, 0) = 0xb7543000 set_thread_area({entry_number:-1 -> 6, base_addr:0xb7543700, limit:1048575, seg_32bit:1, contents:0, read_ mprotect(0xb76ed000, 8192, PROT_READ) = 0 mprotect(0xb7736000, 4096, PROT_READ) = 0 munmap(0xb76f3000, 132397) = 0 brk(0) = 0x9dd9000 brk(0x9dfa000) = 0x9dfa000 exit_group(0) =? +++ exited with 0 +++

data structure of malloc state struct malloc_state { mutex_t mutex; int flags; mfastbinptr fastbinsy[nfastbins]; /* Base of the topmost chunk -- not otherwise kept in a bin */ mchunkptr top; /* The remainder from the most recent split of a small request */ mchunkptr last_remainder; /* Normal bins packed as described above */ mchunkptr bins[nbins * 2-2]; unsigned int binmap[binmapsize]; struct malloc_state *next; /* Memory allocated from the system in this arena. */ INTERNAL_SIZE_T system_mem; INTERNAL_SIZE_T max_system_mem; }; mutex flags fastbinsy top normal bins malloc state* next mutex flags fastbinsy top normal bins malloc state* next mutex flags fastbinsy top normal bins malloc state* next mutex flags fastbinsy top normal bins malloc state* next

data structure of heap info typedef struct _heap_info { mstate ar_ptr; /* Arena for this heap. */ struct _heap_info *prev; /* Previous heap. */ size_t size; /* Current size in bytes. */ size_t mprotect_size; /* Size in bytes that has been mprotected PROT_READ PROT_WRITE. */ /* Make sure the following data is properly aligned, particularly that sizeof (heap_info) + 2 * SIZE_SZ is a multiple of MALLOC_ALIGNMENT. */ char pad[-6 * SIZE_SZ & MALLOC_ALIGN_MASK]; } heap_info; Question why are there prev in the data structure?

data structure of malloc chunk struct malloc_chunk { INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */ INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */ struct malloc_chunk* fd; /* double links -- used only if free. */ struct malloc_chunk* bk; /* Only used for large blocks: pointer to next larger size. */ struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */ struct malloc_chunk* bk_nextsize; };

chunk structure of allocated one 0 7 15 23 31 prev size if freed size user data A M P current chunk next chunk prev size If the previous chunk is freed, the value of prev size field states the previous chunk size in byte. Otherwise, the value in previous field is just one part of user data in the previous chunk.

chunk structure of freed one 0 7 15 23 31 prev size if freed size forward pointer backward pointer A M P meta data free space

TLB and Cache

Organization of the freed chunk The freed chunk will be cached. The pointer will be saved in suitable bins. There are three types of bins. fast bin < 64 bytes small bin < 512 bytes large bin > 512 bytes Small bin and large bin are also called normal bins. fast bins normal bins singly linked list circular double linked list

Fastbins Fastbin is a singly linked list. 1 2 3 4 5 6 7 16 24 32 40 48 56 64 16 24 32 40 48 56 64 16 24 32 40 48 56 64 16 24 32 40 48 56 64 16 24 32 40 48 56 64 16 24 32 40 48 56 64....... 16 24 32 40 48 56 64

Summary of heap layout top chunk dirty part management data

Part II Algorithm

algorithm for malloc Version is glibc-2.18 basic steps for malloc 1. fetch an arena one area is free create a new one if non-area is in idle 2. allocate requested size in the fetched area return the requested memory return NULL if non-memory available

malloc function libc_malloc(size_t bytes) { mstate ar_ptr; void *victim; void *(*hook) (size_t, const void *) = force_reg ( malloc_hook); if ( builtin_expect (hook!= NULL, 0)) return (*hook)(bytes, RETURN_ADDRESS (0)); arena_lookup(ar_ptr); arena_lock(ar_ptr, bytes); if(!ar_ptr) return 0; victim = _int_malloc(ar_ptr, bytes); if(!victim) { ar_ptr = arena_get_retry(ar_ptr, bytes); if ( builtin_expect(ar_ptr!= NULL, 1)) { victim = _int_malloc(ar_ptr, bytes); (void)mutex_unlock(&ar_ptr->mutex); } } else (void)mutex_unlock(&ar_ptr->mutex); assert(!victim chunk_is_mmapped(mem2chunk(victim)) ar_ptr == arena_for_chunk(mem2chunk(victim))); return victim; }

malloc procedure in one specific area exact match fastbins and smallbins closest match largebins fetch freed chunk in the matched bins if the request size falls into the fastbins or smallbins largebin do malloc consolidate, consolidated memory is put into unsorted bins do alloation if both of above steps failed, try to apply a new memory block from system

malloc flow chart

algorithm for free memory 1. free to system directory if the chunk is allocated using mmap 2. free to fastbins directory and return 3. free to normal bins and do malloc consolidate 4. if the top chunk is big enough, do heap trim or heap shrink

memory free flow chart

function list libc malloc int malloc int free malloc consolidate sysmalloc

Part III common memory error

why memory heap corruption The basic reason is that the meta data is corrupted. Overflow will be the main reason. Memory restriction condition is not matched.

how to detect and solve memory leak 1. purify 2. valgrind 3. tcmalloc