staticvoidmalloc_consolidate(mstate av) { mfastbinptr* fb; /* current fastbin being consolidated */ mfastbinptr* maxfb; /* last fastbin (for loop control) */ mchunkptr p; /* current chunk being consolidated */ mchunkptr nextp; /* next chunk to consolidate */ mchunkptr unsorted_bin; /* bin header */ mchunkptr first_unsorted; /* chunk to link to */
/* These have same use as in free() */ mchunkptr nextchunk; INTERNAL_SIZE_T size; INTERNAL_SIZE_T nextsize; INTERNAL_SIZE_T prevsize; int nextinuse; mchunkptr bck; mchunkptr fwd;
/* If max_fast is 0, we know that av hasn't yet been initialized, in which case do so below */
if (get_max_fast () != 0) { clear_fastchunks(av);
unsorted_bin = unsorted_chunks(av);
/* Remove each chunk from fast bin and consolidate it, placing it then in unsorted bin. Among other reasons for doing this, placing in unsorted bin avoids needing to calculate actual bins until malloc is sure that chunks aren't immediately going to be reused anyway. */
maxfb = &fastbin (av, NFASTBINS - 1); fb = &fastbin (av, 0); do { p = atomic_exchange_acq (fb, 0); if (p != 0) { do { check_inuse_chunk(av, p); nextp = p->fd;//从最大的fastbin和最小的fastbin中遍历
/* Slightly streamlined version of consolidation code in free() */ size = p->size & ~(PREV_INUSE|NON_MAIN_ARENA);//如果前一个chunk为free状态,P位归零(位于fastbin的chunk的P位永远为1) nextchunk = chunk_at_offset(p, size);//通过偏移获取下一个chunk及其size nextsize = chunksize(nextchunk);
if (!prev_inuse(p)) { prevsize = p->prev_size;//向上合并 size += prevsize; p = chunk_at_offset(p, -((long) prevsize)); unlink(av, p, bck, fwd); }
if (nextchunk != av->top) {//判断下一个是不是top chunk nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
from pwn import* elf = ELF('./mutepig') #libc = ELF('') context(arch=elf.arch, os=elf.os,log_level='debug') local = 1 if local: io = process([elf.path]) else: io = remote() DEBUG = 0 if DEBUG and local: gdb.attach(io, ''' b 0x400bce ''')
p = lambda : pause() ru = lambda x : io.recvuntil(x) rl = lambda : io.recvline() r = lambda x : io.recv(x) s = lambda x : io.send(x) sl = lambda x : io.sendline(x) ia = lambda : io.interactive() sla = lambda a, b : io.sendlineafter(a, b) sa = lambda a, b : io.sendafter(a, b) uu32 = lambda x : u32(r(x).ljust(4,b'\x00')) uu64 = lambda x : u64(r(x).ljust(8,b'\x00')) lg = lambda x,y : log.success(str(x) + ' -> ' + hex(y))