For this project, we expect you to look at this concept:
- Hack the Virtual Memory: malloc, the heap & the program break
- Everything you need to know to write your own malloc
sbrkbrkmalloc
At the end of this project, you are expected to be able to explain to anyone, without the help of Google:
- What is a program break
- How to play with a program break in order to allocate memory dynamically
- How the glibc
mallocandfreefunctions work - What is ASLR
- What is memory alignment
- What is a memory page
- How to encapsulate the memory management in order to hide it from the user
- Allowed editors:
vi,vim,emacs - All your files will be compiled on Ubuntu 14.04 LTS
- Your C programs and functions will be compiled with
gcc 4.8.4using the flags-Wall-Werror-Wextraand-pedantic - All your files should end with a new line
- A
README.mdfile, at the root of the folder of the project, is mandatory - Your code should use the
Bettystyle. It will be checked using betty-style.pl and betty-doc.pl - You are not allowed to have more than 5 functions per file
- The prototypes of all your functions should be included in your header file called
malloc.h - Don’t forget to push your header files
- All your header files should be include guarded
- You are allowed to use
globalvariables - You are allowed to use
staticvariables
- All the C source files in your directory and subdirectories must be Betty-compliant
- Unless specified otherwise, you are allowed to use the C standard library
- Of course, you’re not allowed to use the
mallocfamily from the C library…
- It is strongly advised to test your functions against real programs, like a shell, or your old projects for example.
- To do so, you can name your functions
malloc,free,reallocandcalloc(just like they're named in the glibc), and compile them into a shared library that you would load when executing a program usingLD_LIBRARY_PATHandLD_PRELOAD - Here’s a tutorial on how to do it
It is not required that your _malloc, free, calloc and realloc behave exactly like the glibc malloc, free, calloc and realloc:
- You are free to use any data structure that suits you as long as their purpose is well defined
- You are free to handle the heap as it suits you, as long as the returned pointers (for the functions that return a pointer) are aligned as required and that enough space is available
- You are free to extend the program break as it suits you, as long as it is extended by a multiple of the virtual memory page size, and as long as it is reduced when needed
- You decide of your implementation. During the correction, we will mainly focus on the strength and reliability of your functions, so make sure to handle big allocations :)
Read carefully the concept page for this project
Build the naive malloc that is presented in the concept page.
- Prototype:
void *naive_malloc(size_t size); - Where
sizeis the size needed to be allocated for the user - Your function must return a pointer to the allocated memory that is suitably aligned for any kind of variable
- You naive malloc should be able to:
- Allocate enough memory to store
- A
size_tas the chunk header - The size requested as parameter
- A
- Allocate memory pages only
- Allocate enough memory to store
- GitHub repository:
atlas-malloc - File:
naive_malloc.c,malloc.h
Write you own malloc function that allocates space in the heap
- Prototype:
void *_malloc(size_t size); - Where
sizeis the size needed to be allocated for the user - Your function must return a pointer to the allocated memory that is suitably aligned for any kind of variable
- GitHub repository:
atlas-malloc - File:
malloc.c,malloc.h
Write you own free function that frees a memory space
- Prototype:
void _free(void *ptr); - Where
ptris a pointer to the memory space to be freed
- GitHub repository:
atlas-malloc - File:
free.c,malloc.c,malloc.h
Write you own malloc function that allocates space in the heap
- Prototype:
void *_calloc(size_t nmemb, size_t size); - Where
nmembis the number of elements in the array, andsizeis the size of each element - Your function must return a pointer to the allocated memory that is suitably aligned for any kind of variable
- GitHub repository:
atlas-malloc - File:
calloc.c,free.c,malloc.c,malloc.h
Write you own malloc function that allocates space in the heap
- Prototype:
void *_realloc(void *ptr, size_t size); - Where
ptris a pointer to the memory space to resize, andsizeis the new size needed to be allocated for the user - Your function must return a pointer to the new allocated memory that is suitably aligned for any kind of variable
- GitHub repository:
atlas-malloc - File:
realloc.c,free.c,malloc.c,malloc.h
Handle multithreading !
Your functions _malloc, _free, _realloc, and _calloc must be thread safe
Your functions will be compiled and linked with the flag -pthread
Resources:
- GitHub repository:
atlas-malloc - File:
malloc.c,free.c,realloc.c,calloc.c,malloc.h
| File | Description |
|---|---|
| malloc.h | Header file for Programs |
| naive_malloc.c | Simple memory allocation |
| malloc.c | Recreating malloc function |
| free.c | Freeing allocated memory |
| calloc.c | Recreating the calloc function |
| realloc.c | Recreating the realloc function |
