开发者

issue in putting custom fields in struct malloc_chunk

i am trying to put a head-tag and a foot tag inside struct malloc_chunk, like this:

  struct malloc_shunk {
       INTERNAL_SIZE prev_size;
       INTERNAL_SIZE size;
       }

Here is what i did:

1.

  struct malloc_shunk {
       INTERNAL_SIZE foot_tag;
       INTERNAL_SIZE prev_size;
       INTERNAL_SIZE size;
       }

before putting the head_tag inside malloc_chunk, i just added foot_tag only and compiled glibc. I made a small test program that mallocs 60 bytes from the system and then frees it. Thou i can see that the malloc returned properly, free complained, saying "invalid pointer". The pointer malloc returned was 0x9313010. That makes the pointer to the start of malloc_chunk to be 0x9313004. So when the free is passed 0x9313010, it converts it to 0x9313004 through (mem2chunk) and the check it for alignment. Since my wordsize is 4, alignment check with 0x9313010 is where i am getting problems. Can you please tell me if the Mem pointer (returned by malloc) needs to be absolutely double-word aligned. (as here it may not satisfy that criterion, as difference betwwen the pointer returned by malloc and start of chunk will be 12 by开发者_如何转开发tes here and not 8).

  1. To ever come 1. issue , i just added 1 head-tag to the structure so that it becomes

    struct malloc_shunk { INTERNAL_SIZE foot_tag; INTERNAL_SIZE prev_size; INTERNAL_SIZE size; INTERNAL_SIZE head_tag; } Now the difference betwwen the pointer returned by malloc and start of chunk will be 16 which will always be double word aligned. But Here i am facing a bigger problem as the time when first malloc is called the arena is setup and bins are initallised. The size of the victim here is not coming out to be zero as it should be in normal cases. The problem is that the victim->size is actually comes out to be the place where 'top' is stored rather than 'last_remainder'. i'd like to ask for you opinion if there is any other way/workaround/solution, so that i can over come this initialization of arena issue i am currently facing.

Thanks and Regards, Kapil


From what ever i have learnt, try not to modify the structure malloc_chunk. Even if you have to then make sure that the top pointer is initially 0 otherwise heap will never be formed. Its the sYSMALLOC() function in _int_malloc() that first MMAPs a bigger chunk from kernel. It will be invoked only if the top is 0 initially.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜