19
19
20
20
using namespace LIBC_NAMESPACE ;
21
21
22
+ // Record of an outstanding allocation.
22
23
struct Alloc {
23
24
void *ptr;
24
25
size_t size;
25
26
size_t alignment;
26
- uint8_t canary;
27
+ uint8_t canary; // Byte written to the allocation
27
28
};
28
29
30
+ // A simple vector that tracks allocations using the heap.
29
31
class AllocVec {
30
32
public:
31
33
AllocVec (FreeListHeap &heap) : heap(&heap), size_(0 ), capacity(0 ) {
@@ -77,6 +79,7 @@ cpp::optional<T> choose(const uint8_t *&data, size_t &remainder) {
77
79
return out;
78
80
}
79
81
82
+ // The type of allocation to perform
80
83
enum class AllocType : uint8_t {
81
84
MALLOC,
82
85
ALIGNED_ALLOC,
@@ -98,7 +101,7 @@ cpp::optional<AllocType> choose<AllocType>(const uint8_t *&data,
98
101
constexpr size_t heap_size = 64 * 1024 ;
99
102
100
103
cpp::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);
102
105
if (!raw)
103
106
return cpp::nullopt;
104
107
return *raw % heap_size;
@@ -180,12 +183,15 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t remainder) {
180
183
}
181
184
182
185
if (ptr) {
186
+ // aligned_allocate should automatically apply a minimum alignment.
183
187
if (alignment < alignof (max_align_t ))
184
188
alignment = alignof (max_align_t );
185
189
// Check alignment.
186
190
if (reinterpret_cast <uintptr_t >(ptr) % alignment)
187
191
__builtin_trap ();
188
192
193
+ // Reallocation is treated specially above, since we would otherwise
194
+ // lose the original size.
189
195
if (alloc_type != AllocType::REALLOC) {
190
196
// Fill the object with a canary byte.
191
197
inline_memset (ptr, canary, alloc_size);
0 commit comments