/src/solidity/test/tools/ossfuzz/SolidityCustomMutatorInterface.cpp
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #include <test/tools/ossfuzz/SolidityCustomMutatorInterface.h> |
20 | | #include <test/tools/ossfuzz/SolidityGenerator.h> |
21 | | |
22 | | #include <liblangutil/Exceptions.h> |
23 | | |
24 | | using namespace solidity::test::fuzzer::mutator; |
25 | | |
26 | | // Prototype as we can't use the FuzzerInterface.h header. |
27 | | extern "C" size_t LLVMFuzzerMutate(uint8_t* _data, size_t _size, size_t _maxSize); |
28 | | extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* _data, size_t size, size_t _maxSize, unsigned int seed); |
29 | | namespace |
30 | | { |
31 | | /// Define Solidity's custom mutator by implementing libFuzzer's |
32 | | /// custom mutator external interface. |
33 | | extern "C" size_t LLVMFuzzerCustomMutator( |
34 | | uint8_t* _data, |
35 | | size_t _size, |
36 | | size_t _maxSize, |
37 | | unsigned int _seed |
38 | | ) |
39 | 0 | { |
40 | 0 | solAssert(_data, "libFuzzerInterface: libFuzzer supplied bad buffer"); |
41 | 0 | if (_maxSize <= _size || _size == 0) |
42 | 0 | return LLVMFuzzerMutate(_data, _size, _maxSize); |
43 | 0 | return SolidityCustomMutatorInterface{_data, _size, _maxSize, _seed}.generate(); |
44 | 0 | } |
45 | | } |
46 | | |
47 | | SolidityCustomMutatorInterface::SolidityCustomMutatorInterface( |
48 | | uint8_t* _data, |
49 | | size_t _size, |
50 | | size_t _maxSize, |
51 | | unsigned int _seed |
52 | | ): |
53 | 0 | data(_data), |
54 | 0 | size(_size), |
55 | 0 | maxMutantSize(_maxSize), |
56 | 0 | generator(std::make_shared<SolidityGenerator>(_seed)) |
57 | 0 | {} |
58 | | |
59 | | size_t SolidityCustomMutatorInterface::generate() |
60 | 0 | { |
61 | 0 | std::string testCase = generator->generateTestProgram(); |
62 | | solAssert( |
63 | 0 | !testCase.empty() && data, |
64 | 0 | "Solc custom mutator: Invalid mutant or memory pointer" |
65 | 0 | ); |
66 | 0 | size_t mutantSize = std::min(testCase.size(), maxMutantSize - 1); |
67 | 0 | mempcpy(data, testCase.data(), mutantSize); |
68 | 0 | return mutantSize; |
69 | 0 | } |