4040#include <haproxy/http-hdr.h>
4141#include <haproxy/global.h>
4242#include <haproxy/buf.h>
43+ #include <haproxy/chunk.h>
4344
4445#include <stdint.h>
4546#include <string.h>
4950#define HTX_BUF_SIZE 65536
5051#define TRASH_BUF_SIZE 65536
5152
52- /* trash is a global scratch buffer used in h1_parse_msg_hdrs (via
53- * b_slow_realign_ofs). Normal startup initialises it, but the fuzzer
54- * bypasses the full init sequence. */
55- extern THREAD_LOCAL struct buffer trash ;
56-
5753static int fuzz_initialized = 0 ;
5854static char * htx_area = NULL ;
59- static char * trash_area = NULL ;
6055
6156int LLVMFuzzerTestOneInput (const uint8_t * data , size_t size ) {
6257 struct h1m h1m ;
@@ -71,14 +66,25 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
7166 if (size < 2 )
7267 return 0 ;
7368
74- /* One-time init */
69+ /* One-time init: replicate the trash buffer setup that haproxy normally
70+ * performs at startup (alloc_early_trash + alloc_trash_buffers_per_thread).
71+ * init_trash_buffers() is the public entry point that creates the pool and
72+ * allocates the two alternating trash buffers (trash_buf1/trash_buf2) plus
73+ * the scratch buffer (trash) used throughout haproxy. The first call with
74+ * first=1 mirrors alloc_early_trash(); the second with first=0 also creates
75+ * the large and small trash pools and their per-thread buffers. */
7576 if (!fuzz_initialized ) {
7677 htx_area = malloc (HTX_BUF_SIZE );
77- trash_area = malloc (TRASH_BUF_SIZE );
78- if (!htx_area || !trash_area )
78+ if (!htx_area )
7979 return 0 ;
80- chunk_init (& trash , trash_area , TRASH_BUF_SIZE );
80+ global .tune .bufsize = TRASH_BUF_SIZE ;
81+ global .tune .bufsize_large = TRASH_BUF_SIZE * 2 ;
82+ global .tune .bufsize_small = 1024 ;
8183 global .tune .max_http_hdr = MAX_HDR_NUM ;
84+ if (!init_trash_buffers (1 ))
85+ return 0 ;
86+ if (!init_trash_buffers (0 ))
87+ return 0 ;
8288 fuzz_initialized = 1 ;
8389 }
8490
0 commit comments