| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <stddef.h> |
| 15 |
#include <stdint.h> |
| 16 |
|
| 17 |
#include <isc/buffer.h> |
| 18 |
#include <isc/lex.h> |
| 19 |
#include <isc/mem.h> |
| 20 |
#include <isc/string.h> |
| 21 |
#include <isc/util.h> |
| 22 |
|
| 23 |
#include "fuzz.h" |
| 24 |
|
| 25 |
bool debug = false; |
| 26 |
|
| 27 |
int |
| 28 |
LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED); |
| 29 |
|
| 30 |
int |
| 31 |
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
| 32 |
|
| 33 |
static isc_mem_t *mctx = NULL; |
| 34 |
static isc_lex_t *lex = NULL; |
| 35 |
|
| 36 |
int |
| 37 |
LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED) { |
| 38 |
isc_mem_create(&mctx); |
| 39 |
isc_lex_create(mctx, 1024, &lex); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int |
| 45 |
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 46 |
isc_buffer_t buf; |
| 47 |
isc_result_t result; |
| 48 |
isc_token_t token; |
| 49 |
isc_tokentype_t expect; |
| 50 |
bool eol; |
| 51 |
|
| 52 |
if (size < sizeof(expect) + sizeof(eol)) { |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
(void)memmove(&expect, data, sizeof(expect)); |
| 57 |
data += sizeof(expect); |
| 58 |
size -= sizeof(expect); |
| 59 |
|
| 60 |
eol = *data != 0; |
| 61 |
data += 1; |
| 62 |
size -= 1; |
| 63 |
|
| 64 |
isc_buffer_constinit(&buf, data, size); |
| 65 |
isc_buffer_add(&buf, size); |
| 66 |
isc_buffer_setactive(&buf, size); |
| 67 |
|
| 68 |
CHECK(isc_lex_openbuffer(lex, &buf)); |
| 69 |
|
| 70 |
do { |
| 71 |
result = isc_lex_getmastertoken(lex, &token, expect, eol); |
| 72 |
} while (result == ISC_R_SUCCESS && token.type != isc_tokentype_eof); |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |