Heap analysis/glibc 2.23
(glibc 2.23) do_check_inuse_chunk
lok2h4rd
2022. 3. 1. 20:07
전체 코드는 아래 더보기
더보기
/*
Properties of inuse chunks
*/
static void
do_check_inuse_chunk (mstate av, mchunkptr p)
{
mchunkptr next;
do_check_chunk (av, p);
if (chunk_is_mmapped (p))
return; /* mmapped chunks have no next/prev */
/* Check whether it claims to be in use ... */
assert (inuse (p));
next = next_chunk (p);
/* ... and is surrounded by OK chunks.
Since more things can be checked with free chunks than inuse ones,
if an inuse chunk borders them and debug is on, it's worth doing them.
*/
if (!prev_inuse (p))
{
/* Note that we cannot even look at prev unless it is not inuse */
mchunkptr prv = prev_chunk (p);
assert (next_chunk (prv) == p);
do_check_free_chunk (av, prv);
}
if (next == av->top)
{
assert (prev_inuse (next));
assert (chunksize (next) >= MINSIZE);
}
else if (!inuse (next))
do_check_free_chunk (av, next);
}
p와 p의 이전과 이후 청크를 검사하는 함수로 주요 검사는 아래와 같다
- do_check_chunk 호출 (청크의 위치를 검사한다)
- p가 사용 중인지 검사 ( 다음 청크의 prev_inuse 검사 )
- 이전 청크 size 검사 및 do_check_free_chunk호출 ( 이전 청크가 free인 경우 )
- 다음 청크가 top 청크가 아닐 경우 do_check_free_chunk 호출
인자 및 변수
static void
do_check_inuse_chunk (mstate av, mchunkptr p)
{
mchunkptr next;
- av : 현재 아레나
- p : 검사할 청크 p
- next : p의 다음 청크
check 1
do_check_chunk (av, p);
if (chunk_is_mmapped (p))
return; /* mmapped chunks have no next/prev */
/* Check whether it claims to be in use ... */
assert (inuse (p));
next = next_chunk (p);
- do_check_chunk 호출
- p가 mmap으로 할당되어있으면 함수 종료
- 다음 청크에 prev_inuse비트가 설정되어있는지 검사
- next에 p의 다음 청크 저장
not prev_inuse
/* ... and is surrounded by OK chunks.
Since more things can be checked with free chunks than inuse ones,
if an inuse chunk borders them and debug is on, it's worth doing them.
*/
if (!prev_inuse (p))
{
/* Note that we cannot even look at prev unless it is not inuse */
mchunkptr prv = prev_chunk (p);
assert (next_chunk (prv) == p);
do_check_free_chunk (av, prv);
}
청크 p에 PREV_INUSE 비트가 설정되어 잇지 않으면 조건문이 수행된다 ( p의 이전 청크
- prv에 p의 이전 청크 저장
- prv의 다음 청크가 p인지 검사
- prv를 인자로 do_check_free_chunk 호출
next top
if (next == av->top)
{
assert (prev_inuse (next));
assert (chunksize (next) >= MINSIZE);
}
else if (!inuse (next))
do_check_free_chunk (av, next);
만약 next (p의 다음 chunk)가 top chunk라면 이전 청크가 사용 중인지 그리고 청크 사이즈를 검사한다
next 청크가 사용중 아니라면 do_check_free_chunk를 호출하여 next청크가 free된지 확인한다
사용된 함수 및 매크로
- do_check_chunk 참고 글
- do_check_free_chunk 참고 글