Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fuzz_compiler_lexer.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_Lexer.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{2048});
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);
61 options.keep_comments = (flags & 1) != 0;
62 options.allow_block_comments = (flags & 2) != 0;
63 Aleph::Compiler_Lexer lexer(sources, file_id, &diagnostics, options);
64
65 size_t previous_end = 0;
66 for (size_t step = 0; step <= source.size(); ++step)
67 {
68 const auto before = lexer.current_offset();
69 const auto looked_at = lexer.peek();
70 const auto token = lexer.next();
71 if (looked_at.kind != token.kind or looked_at.lexeme != token.lexeme
72 or looked_at.span.file_id != token.span.file_id
73 or looked_at.span.begin != token.span.begin
74 or looked_at.span.end != token.span.end)
75 std::abort();
76
77 if (not token.span.is_valid() or token.span.file_id != file_id
78 or token.span.begin < previous_end or token.span.end > source.size())
79 std::abort();
80 if (token.is_eof())
81 {
82 if (token.span.begin != source.size()
83 or token.span.end != source.size())
84 std::abort();
85 return 0;
86 }
87
88 if (token.span.empty() or lexer.current_offset() <= before
89 or token.lexeme != source.substr(token.span.begin, token.span.size()))
90 std::abort();
91 previous_end = token.span.end;
92 }
93
94 std::abort();
95}
Lexical analyzer (Lexer) based on Source_Manager.
Token generator for a single source file.
Source_Offset current_offset() const noexcept
Returns the current byte offset in the file.
Compiler_Token next()
Consumes and returns the next token from the stream.
const Compiler_Token & peek()
Peeks at the next token without advancing the cursor.
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)
Tokenize arbitrary source text and validate the lexer contract.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static struct argp_option options[]
Definition ntreepic.C:1890
Configuration options for lexer behavior.
bool keep_comments
If true, comments are returned as tokens.