| 1 |
//===-- dictionary.c - Generate fuzzing dictionary for clang --------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// This binary emits a fuzzing dictionary describing strings that are |
| 10 |
// significant to the clang parser: keywords and other tokens. |
| 11 |
// |
| 12 |
// The dictionary can be used by a fuzzer to reach interesting parser states |
| 13 |
// much more quickly. |
| 14 |
// |
| 15 |
// The output is a single-file dictionary supported by libFuzzer and AFL: |
| 16 |
// https://llvm.org/docs/LibFuzzer.html#dictionaries |
| 17 |
// |
| 18 |
//===----------------------------------------------------------------------===// |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
|
| 22 |
static void emit(const char *Name, const char *Spelling) { |
| 23 |
static char Hex[] = "0123456789abcdef"; |
| 24 |
|
| 25 |
printf("%s=\"", Name); |
| 26 |
unsigned char C; |
| 27 |
while ((C = *Spelling++)) { |
| 28 |
if (C < 32 || C == '"' || C == '\\') |
| 29 |
printf("\\x%c%c", Hex[C>>4], Hex[C%16]); |
| 30 |
else |
| 31 |
printf("%c", C); |
| 32 |
} |
| 33 |
printf("\"\n"); |
| 34 |
} |
| 35 |
|
| 36 |
int main(int argc, char **argv) { |
| 37 |
#define PUNCTUATOR(Name, Spelling) emit(#Name, Spelling); |
| 38 |
#define KEYWORD(Name, Criteria) emit(#Name, #Name); |
| 39 |
#define PPKEYWORD(Name) emit(#Name, #Name); |
| 40 |
#define CXX_KEYWORD_OPERATOR(Name, Equivalent) emit(#Name, #Name); |
| 41 |
#define OBJC_AT_KEYWORD(Name) emit(#Name, #Name); |
| 42 |
#define ALIAS(Spelling, Equivalent, Criteria) emit(Spelling, Spelling); |
| 43 |
#include "clang/Basic/TokenKinds.def" |
| 44 |
// Some other sub-token chunks significant to the lexer. |
| 45 |
emit("ucn16", "\\u0000"); |
| 46 |
emit("ucn32", "\\U00000000"); |
| 47 |
emit("rawstart", "R\"("); |
| 48 |
emit("rawend", ")\""); |
| 49 |
emit("quote", "\""); |
| 50 |
emit("squote", "'"); |
| 51 |
emit("u8quote", "u8\""); |
| 52 |
emit("u16quote", "u\""); |
| 53 |
emit("u32quote", "U\""); |
| 54 |
emit("esc_nl", "\\\n"); |
| 55 |
emit("hex", "0x"); |
| 56 |
} |
| 57 |
|