Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fuzz_compiler_parser.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
35#include <algorithm>
36#include <cstddef>
37#include <cstdint>
38#include <cstdlib>
39#include <string>
40
41#include <Compiler_Parser.H>
42
50extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
51{
52 const uint8_t flags = size == 0 ? 0 : data[0];
53 const size_t length = size == 0 ? 0 : std::min(size - 1, size_t{512});
54 const char *begin = size == 0 ? "" : reinterpret_cast<const char *>(data + 1);
55 const std::string source(begin, length);
56
58 const auto file_id = sources.add_virtual_file("fuzz.aw", source);
59 Aleph::Diagnostic_Engine diagnostics(sources);
60 Aleph::Compiler_Ast_Context ast(1 << 15);
62 options.allow_top_level_statements = (flags & 1) != 0;
63 Aleph::Compiler_Parser parser(ast, sources, file_id, &diagnostics, options);
64 const auto *module = parser.parse_module();
65
66 if (module == nullptr or not module->span.is_valid()
67 or module->span.file_id != file_id
68 or module->span.end > source.size())
69 std::abort();
70
71 return 0;
72}
Recursive-descent parser for the compiler-support MVP grammar.
Arena-backed ownership context for AST nodes.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
Diagnostic accumulator and renderer.
Stores source files and resolves offsets into human-readable data.
Definition ah-source.H:184
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Definition ah-source.H:258
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Parse arbitrary bounded source text and validate the module span.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static struct argp_option options[]
Definition ntreepic.C:1890
Parser configuration knobs for the MVP grammar.
bool allow_top_level_statements
If false, only top-level declarations are accepted.