当前位置:网站首页>PWN entry (3) heap
PWN entry (3) heap
2022-07-23 16:58:00 【Day-3】
1 The heap manager
1.1 Heap overview
What is a heap ?
Is a continuous linear region of the virtual address space
Provide dynamically allocated memory , Allow programs to request memory of unknown size
Between the user and the operating system , As the middleman of dynamic memory management
Respond to the user's request for memory , Request memory from the operating system , And then return it to the user program
Manage the memory released by users , optimum Return to the operating system

Various heap managers
The heap manager is not implemented by the operating system , But by the libc.so.6 Link library implementation . Encapsulates some system calls , While providing users with a convenient dynamic memory allocation interface , Strive to efficiently manage the memory requested by the system call .dlmalloc – General purpose allocator
ptmalloc2 – glibc ( On the blackboard )
jemalloc – FreeBSD and Firefox
tcmalloc – Google libumem – Solaris

1.2 arena

1.3 chunk
The unit of memory requested by the user , It is also the basic unit of memory managed by the heap manager malloc() The returned pointer points to a chunk The data area of .
chunk The concrete realization of
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 The classification of
By status
- malloced
- free
By size - fast
- small
- targe
- tcache
By specific function - top chunk
- last remainder chunk
malloced chunk
Have been assigned and filled in the corresponding data chunk
free chunk
Released malloced chunk Become free chunk
top chunk
arena Memory area that has never been used in 
last remainder chunk
malloc Segmented original chunk And the rest of it 
Chunk Microstructure of 
1.4 bin
management arena Free time chunk Structure , Exist as an array , The array elements are of corresponding size chunk The head of a linked list , Exist in arena Of malloc_state in
- unsorted bin
- fast bins
- small bins
- large bins
- (tcache)
prev_size
If the previous one is physically adjacent chunk yes free chunk, Indicates its size . Otherwise, it is used to store the previous chunk The data of 
size
Occupy a word long low 3bits Later address , Used to indicate the present chunk Size ( Whole chunk Size , Include chunk head )

A flag
NON_MAIN_ARENA, Record the current chunk Whether it does not belong to the main thread ,1 Does not belong to ,0 Indicates belonging to .
M flag
IS_MAPPED, Record the current chunk Is it by mmap The distribution of .
P flag
PREV_INUSE, Record the previous chunk Whether the block is allocated . Generally speaking , Of the first allocated memory block in the heap size Field P Bits will be set to 1, In order to prevent access to the previous illegal memory . When one chunk Of size Of P Position as 0 when , We can get through prev_size Field to get the previous chunk Size and address . It's also convenient for idle chunk A merger between .
fd pointer
stay bin Point to the next ( Non physical adjacency ) Idle chunk.
bk pointer
stay bin Middle points to the previous ( Non physical adjacency ) Idle chunk

fd_nextsize
stay large bin The middle finger moves forward with the current chunk The first free block with different sizes , It doesn't contain bin The head pointer of

bk_nextsize
stay large bin The middle finger is backward and current chunk The first free block with different sizes , It doesn't contain bin The head pointer of

Generally free large chunk stay fd In the traversal order of , Arrange in order from big to small . This can avoid looking for the right chunk Go through it one by one
main arena Of malloc_state Not at all heap segment Part of , It's a global variable , Stored in libc.so Data section of
struct malloc_state {
/* Serialize access. */
__libc_lock_define(, mutex); /* Flags (formerly in max_fast). */
int flags; /* Fastbins */
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 ]; /* Bitmap of bins, help to speed up the process of determinating if a given bin is definitely empty.*/
unsigned int binmap[ BINMAPSIZE ]; /* Linked list, points to the next arena */
struct malloc_state *next; /* Linked list for free arenas. Access to this field is serialized by free_list_lock in arena.c. */
struct malloc_state *next_free; /* Number of threads attached to this arena. 0 if the arena is on the free list. Access to this field is serialized by free_list_lock in arena.c. */
INTERNAL_SIZE_T attached_threads; /* Memory allocated from the system in this arena. */
INTERNAL_SIZE_T system_mem;
INTERNAL_SIZE_T max_system_mem;
};








2 Heap allocation policy
malloc
- It depends on the size of the memory block requested by the user and the corresponding size chunk The usual frequency of use (fastbin chunk, small chunk, large chunk), Different allocation methods are implemented in turn .
- It checks different from small to large bin Is there a corresponding free block in the memory that can meet the user's request .
- When all free chunk Can't be satisfied , It will consider top chunk.
- When top chunk Can't be satisfied , The heap allocator will apply for memory blocks .
free
- It will not be used by users for the time being chunk Recycle to the heap manager , It will be returned to the operating system when appropriate .
- It is based on chunk Size comes first try to put free chunk Chain in tcache Or is it fast bin. If you are not satisfied, you will be linked usorted bin in .
- When conditions are met free Ergodic function usorted bin And physically adjacent free chunk Merge , Put the corresponding size chunk Sort into small bin or large bin in .
- except tcache chunk And fast bin chunk, Other chunk stay free Time will be physically adjacent to it free chunk Merge
边栏推荐
- TS encapsulates the localstorage class to store information
- 微机原理与技术接口随堂练习
- Compose Canvas饼图效果绘制
- 英特尔nuc能代替主机吗_终于圆满了!最新款的Intel NUC迷你主机上线
- Taishan Office Technology Lecture: layout drawing analysis of paragraph borders
- 拼多多APP商品详情接口获取activity_id值(拼多多activity_id接口)
- Eureka notes
- iphone 无法打开openv**文件的解决方案
- Object.defineProperty方法、数据代理
- Numpy 数据分析基础知识第一阶段(NumPy基础)
猜你喜欢

Pinduoduo app product details interface to obtain activity_ ID value (pinduoduo activity_id interface)

国内生产总值(GDP)数据可视化

Visualization of gross domestic product (GDP) data

Tips and tricks for Neural Networks 深度学习训练神经网络的技巧总结(不定期更新)

MongoDB数据库+图形化工具下载安装及使用

Direct exchange

SSD: Single Shot MultiBox Detector

层次分析法(MATLAB)

FIO performance testing tool

NodeJs实现token登录注册(KOA2)
随机推荐
Priyanka Sharma, general manager of CNCF Foundation: read CNCF operation mechanism
pairwise的使用
Case analysis of building campus information management system with low code
小米集团副总裁崔宝秋:开源是人类技术进步的最佳平台和模式
Tan Zhangxi, director of risc-v Foundation: risc-v has gradually expanded from the edge to the center
泰山OFFICE技术讲座:段落边框的布局绘制分析
Leetcode-168.excel table column name
Object.defineproperty method, data agent
实时疫情数据可视化分析
NodeJs实现token登录注册(KOA2)
2022-7-22 面经复习+简单题目整理
通用分页功能
YOLOV7
Convolutional neural network model -- googlenet network structure and code implementation
检测器:用递归特征金字塔和可切换的阿托洛斯卷积检测物体
pwn入门(3)堆
精确的目标检测中定位置信度的获取
sprintf和cv::putText
【31. 走迷宫(BFS)】
僧多粥少?程序员要怎样接私活才能有效提高收入?