1919
2020using namespace LIBC_NAMESPACE ;
2121
22+ // Record of an outstanding allocation.
2223struct Alloc {
2324 void *ptr;
2425 size_t size;
2526 size_t alignment;
26- uint8_t canary;
27+ uint8_t canary; // Byte written to the allocation
2728};
2829
30+ // A simple vector that tracks allocations using the heap.
2931class AllocVec {
3032public:
3133 AllocVec (FreeListHeap &heap) : heap(&heap), size_(0 ), capacity(0 ) {
@@ -77,6 +79,7 @@ cpp::optional<T> choose(const uint8_t *&data, size_t &remainder) {
7779 return out;
7880}
7981
82+ // The type of allocation to perform
8083enum class AllocType : uint8_t {
8184 MALLOC,
8285 ALIGNED_ALLOC,
@@ -98,7 +101,7 @@ cpp::optional<AllocType> choose<AllocType>(const uint8_t *&data,
98101constexpr size_t heap_size = 64 * 1024 ;
99102
100103cpp::optional<size_t > choose_size (const uint8_t *&data, size_t &remainder) {
101- auto raw = choose<uint8_t >(data, remainder);
104+ auto raw = choose<size_t >(data, remainder);
102105 if (!raw)
103106 return cpp::nullopt ;
104107 return *raw % heap_size;
@@ -180,12 +183,15 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t remainder) {
180183 }
181184
182185 if (ptr) {
186+ // aligned_allocate should automatically apply a minimum alignment.
183187 if (alignment < alignof (max_align_t ))
184188 alignment = alignof (max_align_t );
185189 // Check alignment.
186190 if (reinterpret_cast <uintptr_t >(ptr) % alignment)
187191 __builtin_trap ();
188192
193+ // Reallocation is treated specially above, since we would otherwise
194+ // lose the original size.
189195 if (alloc_type != AllocType::REALLOC) {
190196 // Fill the object with a canary byte.
191197 inline_memset (ptr, canary, alloc_size);
0 commit comments