Skip to content

Commit 6f40a7f

Browse files
committed
Fix issues found by fuzzer
These were found almost immediately, and some of them were quite tricky. Hooray for fuzzing!
1 parent bea1b95 commit 6f40a7f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

libc/fuzzing/__support/freelist_heap_fuzz.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
using namespace LIBC_NAMESPACE;
2121

22+
// Record of an outstanding allocation.
2223
struct 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.
2931
class AllocVec {
3032
public:
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
8083
enum class AllocType : uint8_t {
8184
MALLOC,
8285
ALIGNED_ALLOC,
@@ -98,7 +101,7 @@ cpp::optional<AllocType> choose<AllocType>(const uint8_t *&data,
98101
constexpr size_t heap_size = 64 * 1024;
99102

100103
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);
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

Comments
 (0)