infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
fuzz_helpers.h
Go to the documentation of this file.
1#pragma once
27#include <infix/infix.h>
28#include <stdbool.h>
29#include <stddef.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35// Configuration Constants
36// These values control the complexity and depth of the generated types to
37// prevent excessively long or deep recursion, which could slow down fuzzing.
38#define MAX_RECURSION_DEPTH 32
39#define MAX_MEMBERS 16
40#define MAX_ARRAY_ELEMENTS 128
41#define MAX_TYPES_IN_POOL 16
42#define MAX_ARGS_IN_SIGNATURE 16
43#define MAX_TOTAL_FUZZ_FIELDS 256 // Prevents DoS in the generator itself.
44
45// Fuzzer Input Management
46
51typedef struct {
52 const uint8_t * data;
53 size_t size;
55
65static inline const uint8_t * consume_bytes(fuzzer_input * in, size_t n) {
66 if (in->size < n)
67 return NULL;
68 const uint8_t * ptr = in->data;
69 in->data += n;
70 in->size -= n;
71 return ptr;
72}
73
80#define DEFINE_CONSUME_T(type) \
81 static inline bool consume_##type(fuzzer_input * in, type * out) { \
82 const uint8_t * bytes = consume_bytes(in, sizeof(type)); \
83 if (!bytes) \
84 return false; \
85 memcpy(out, bytes, sizeof(type)); \
86 return true; \
87 }
88
89// Define consumer functions for the primitive types needed by the harnesses.
90DEFINE_CONSUME_T(uint8_t)
91DEFINE_CONSUME_T(size_t)
92
93
103infix_type * generate_random_type(infix_arena_t * arena, fuzzer_input * in, int depth, size_t * total_fields);
infix_arena_t * arena
Definition 005_layouts.c:57
#define DEFINE_CONSUME_T(type)
A macro to create a type-safe consumer for any given Plain Old Data (POD) type.
Definition fuzz_helpers.h:80
infix_type * generate_random_type(infix_arena_t *arena, fuzzer_input *in, int depth, size_t *total_fields)
Recursively generates a randomized infix_type from the fuzzer's input data, allocating all objects fr...
Definition fuzz_helpers.c:29
static const uint8_t * consume_bytes(fuzzer_input *in, size_t n)
Safely consume 'n' bytes from the input buffer.
Definition fuzz_helpers.h:65
Declarations for internal-only functions, types, and constants.
A helper structure to safely consume bytes from the fuzzer's input buffer.
Definition fuzz_helpers.h:51
size_t size
Definition fuzz_helpers.h:53
const uint8_t * data
Definition fuzz_helpers.h:52
Definition infix_internals.h:130
The central structure for describing any data type in the FFI system.
Definition infix.h:161