Browse Source

LibC: Adjust malloc size classes to ensure 8-byte aligned pointers

The pointers returned by malloc should always be 8-byte aligned on x86.
We were not consistent about this, as some ChunkedBlock size classes
were not divisible by 8.

This fixes some OOB reads found by running GCC in UE.
Andreas Kling 4 years ago
parent
commit
3a2727844c
1 changed files with 2 additions and 2 deletions
  1. 2 2
      Libraries/LibC/malloc.cpp

+ 2 - 2
Libraries/LibC/malloc.cpp

@@ -75,7 +75,7 @@ static bool s_log_malloc = false;
 static bool s_scrub_malloc = true;
 static bool s_scrub_free = true;
 static bool s_profiling = false;
-static unsigned short size_classes[] = { 8, 16, 32, 64, 128, 252, 508, 1016, 2036, 4090, 8188, 16376, 32756, 0 };
+static unsigned short size_classes[] = { 8, 16, 32, 64, 128, 256, 500, 1016, 2032, 4088, 8184, 16376, 32752, 0 };
 static constexpr size_t num_size_classes = sizeof(size_classes) / sizeof(unsigned short);
 
 struct MallocStats {
@@ -144,7 +144,7 @@ struct ChunkedBlock
     ChunkedBlock* m_prev { nullptr };
     ChunkedBlock* m_next { nullptr };
     FreelistEntry* m_freelist { nullptr };
-    unsigned short m_free_chunks { 0 };
+    size_t m_free_chunks { 0 };
     [[gnu::aligned(8)]] unsigned char m_slot[0];
 
     void* chunk(size_t index)