Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

linux pwn - glibc heap - 深入理解ptmalloc2 - 基础操作, 原文

free

后向合并,合并物理相邻低地址空闲 chunk。
前向合并,合并物理相邻高地址空闲 chunk(除了 top chunk)。

源码:
"
/* consolidate backward */
if (!prev_inuse(p)) {
...
unlink(av, p, bck, fwd);
}

    if (nextchunk != av->top) {
        /* get and clear inuse bit */
        nextinuse = inuse_bit_at_offset(nextchunk, nextsize);

        /* consolidate forward */
        if (!nextinuse) {
unlink(av, nextchunk, bck, fwd);

"
我认为顺序应该是先合并高地址chunk,再检查低地址chunk是否top chunk,如果不是top chunk再合并低地址chunk

You must be logged in to vote

Replies: 1 comment

Comment options

这里后向合并的 p 是用 chunk_at_offset(p, -((long) prevsize)) 计算得到的,所以首先合并低地址的。

/* consolidate backward */
if (!prev_inuse(p)) {
prevsize = prev_size (p);
size += prevsize;
p = chunk_at_offset(p, -((long) prevsize));
if (__glibc_unlikely (chunksize(p) != prevsize))
malloc_printerr ("corrupted size vs. prev_size while consolidating");
unlink_chunk (av, p);
}
 
if (nextchunk != av->top) {
/* get and clear inuse bit */
nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
 
/* consolidate forward */
if (!nextinuse) {
unlink_chunk (av, nextchunk);
size += nextsize;
} else
clear_inuse_bit_at_offset(nextchunk, 0);

然后

前向合并,合并物理相邻高地址空闲 chunk(除了 top chunk)。

这里除了是为了说明 top chunk 是个例外。

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.