본문 바로가기

Heap analysis/glibc 2.29

(glibc 2.29) malloc

glibc 2.26버전 분석 글과 동일하게 glibc 2.26 버전과 비교하면서 다른 점만 정리할 것이다 

단일 스레드일 경우 main_arena로 재할당하는 코드가 추가 되었다

 

전체 코드는 아래 더보기

더보기
void *
__libc_malloc (size_t bytes)
{
  mstate ar_ptr;
  void *victim;

  void *(*hook) (size_t, const void *)
    = atomic_forced_read (__malloc_hook);
  if (__builtin_expect (hook != NULL, 0))
    return (*hook)(bytes, RETURN_ADDRESS (0));
#if USE_TCACHE
  /* int_free also calls request2size, be careful to not pad twice.  */
  size_t tbytes;
  checked_request2size (bytes, tbytes);
  size_t tc_idx = csize2tidx (tbytes);

  MAYBE_INIT_TCACHE ();

  DIAG_PUSH_NEEDS_COMMENT;
  if (tc_idx < mp_.tcache_bins
      /*&& tc_idx < TCACHE_MAX_BINS*/ /* to appease gcc */
      && tcache
      && tcache->entries[tc_idx] != NULL)
    {
      return tcache_get (tc_idx);
    }
  DIAG_POP_NEEDS_COMMENT;
#endif

  if (SINGLE_THREAD_P)
    {
      victim = _int_malloc (&main_arena, bytes);
      assert (!victim || chunk_is_mmapped (mem2chunk (victim)) ||
	      &main_arena == arena_for_chunk (mem2chunk (victim)));
      return victim;
    }

  arena_get (ar_ptr, bytes);

  victim = _int_malloc (ar_ptr, bytes);
  /* Retry with another arena only if we were able to find a usable arena
     before.  */
  if (!victim && ar_ptr != NULL)
    {
      LIBC_PROBE (memory_malloc_retry, 1, bytes);
      ar_ptr = arena_get_retry (ar_ptr, bytes);
      victim = _int_malloc (ar_ptr, bytes);
    }

  if (ar_ptr != NULL)
    __libc_lock_unlock (ar_ptr->mutex);

  assert (!victim || chunk_is_mmapped (mem2chunk (victim)) ||
          ar_ptr == arena_for_chunk (mem2chunk (victim)));
  return victim;
}
libc_hidden_def (__libc_malloc)

 

 

  if (SINGLE_THREAD_P)
    {
      victim = _int_malloc (&main_arena, bytes);
      assert (!victim || chunk_is_mmapped (mem2chunk (victim)) ||
	      &main_arena == arena_for_chunk (mem2chunk (victim)));
      return victim;
    }

tcache르 재할당해주는 코드 뒤에 추가된 코드로 단일 스레드일 경우에 get_area로직을 수행하지 않고 바로 재할당해주는 코드가 추가되었다

  • victim에 재할당할 주소 저장
  • victim이 0이 아닌지, mmap으로 할당되었는지, vicitm이 메인 아레나있는지 검사

 

 

이후 코드부터는 2.26버전과 동일하다

'Heap analysis > glibc 2.29' 카테고리의 다른 글

(glibc 2.29) tcache_get  (0) 2022.05.19
(glibc 2.29) tcache_init  (0) 2022.05.19
(glibc 2.29) _int_free  (0) 2022.05.15
(glibc 2.29) free  (0) 2022.05.15
(glibc 2.29) _int_malloc  (0) 2022.05.14