Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4067bab

Browse files
bshastryLeonardo
andcommitted
Add corpus based multi source fuzzer
Co-authored-by: Leonardo <[email protected]>
1 parent eab999f commit 4067bab

File tree

7 files changed

+54
-9
lines changed

7 files changed

+54
-9
lines changed

test/TestCaseReader.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ TestCaseReader::TestCaseReader(string const& _filename):
3838
m_unreadSettings = m_settings;
3939
}
4040

41+
TestCaseReader::TestCaseReader(istringstream const& _str)
42+
{
43+
tie(m_sources, m_lineNumber) = parseSourcesAndSettingsWithLineNumber(
44+
static_cast<istream&>(const_cast<istringstream&>(_str))
45+
);
46+
}
47+
4148
string const& TestCaseReader::source() const
4249
{
4350
if (m_sources.sources.size() != 1)

test/TestCaseReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TestCaseReader
4242
public:
4343
TestCaseReader() = default;
4444
explicit TestCaseReader(std::string const& _filename);
45+
explicit TestCaseReader(std::istringstream const& _testCode);
4546

4647
SourceMap const& sources() const { return m_sources; }
4748
std::string const& source() const;

test/tools/fuzzer_common.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ void FuzzerUtil::testCompilerJsonInterface(string const& _input, bool _optimize,
7272
runCompiler(jsonCompactPrint(config), _quiet);
7373
}
7474

75-
void FuzzerUtil::testCompiler(string const& _input, bool _optimize)
75+
void FuzzerUtil::testCompiler(StringMap const& _input, bool _optimize, unsigned _rand)
7676
{
7777
frontend::CompilerStack compiler;
78-
EVMVersion evmVersion = s_evmVersions[_input.size() % s_evmVersions.size()];
78+
EVMVersion evmVersion = s_evmVersions[_rand % s_evmVersions.size()];
7979
frontend::OptimiserSettings optimiserSettings;
8080
if (_optimize)
8181
optimiserSettings = frontend::OptimiserSettings::standard();
8282
else
8383
optimiserSettings = frontend::OptimiserSettings::minimal();
84-
compiler.setSources({{"", _input}});
84+
compiler.setSources(_input);
8585
compiler.setEVMVersion(evmVersion);
8686
compiler.setOptimiserSettings(optimiserSettings);
8787
try

test/tools/fuzzer_common.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
// SPDX-License-Identifier: GPL-3.0
1818

19+
#include <libsolutil/Common.h>
20+
21+
#include <map>
1922
#include <string>
2023

2124
/**
@@ -28,5 +31,9 @@ struct FuzzerUtil
2831
static void testCompilerJsonInterface(std::string const& _input, bool _optimize, bool _quiet);
2932
static void testConstantOptimizer(std::string const& _input, bool _quiet);
3033
static void testStandardCompiler(std::string const& _input, bool _quiet);
31-
static void testCompiler(std::string const& _input, bool _optimize);
34+
/// Compiles @param _input which is a map of input file name to source code
35+
/// string with optimisation turned on if @param _optimize is true
36+
/// (off otherwise) and a pseudo-random @param _rand that selects the EVM
37+
/// version to be compiled for.
38+
static void testCompiler(solidity::StringMap const& _input, bool _optimize, unsigned _rand);
3239
};

test/tools/ossfuzz/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ if (OSSFUZZ)
2323
endif()
2424

2525
if (OSSFUZZ)
26-
add_executable(solc_opt_ossfuzz solc_opt_ossfuzz.cpp ../fuzzer_common.cpp)
26+
add_executable(solc_opt_ossfuzz solc_opt_ossfuzz.cpp ../fuzzer_common.cpp ../../TestCaseReader.cpp)
2727
target_link_libraries(solc_opt_ossfuzz PRIVATE libsolc evmasm)
2828
set_target_properties(solc_opt_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE})
2929

30-
add_executable(solc_noopt_ossfuzz solc_noopt_ossfuzz.cpp ../fuzzer_common.cpp)
30+
add_executable(solc_noopt_ossfuzz solc_noopt_ossfuzz.cpp ../fuzzer_common.cpp ../../TestCaseReader.cpp)
3131
target_link_libraries(solc_noopt_ossfuzz PRIVATE libsolc evmasm)
3232
set_target_properties(solc_noopt_ossfuzz PROPERTIES LINK_FLAGS ${LIB_FUZZING_ENGINE})
3333

test/tools/ossfuzz/solc_noopt_ossfuzz.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,29 @@
1818

1919
#include <test/tools/fuzzer_common.h>
2020

21+
#include <test/TestCaseReader.h>
22+
23+
#include <sstream>
24+
25+
using namespace solidity::frontend::test;
2126
using namespace std;
2227

2328
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* _data, size_t _size)
2429
{
2530
if (_size <= 600)
2631
{
2732
string input(reinterpret_cast<char const*>(_data), _size);
28-
FuzzerUtil::testCompiler(input, /*optimize=*/false);
33+
map<string, string> sourceCode;
34+
try
35+
{
36+
TestCaseReader t = TestCaseReader(std::istringstream(input));
37+
sourceCode = t.sources().sources;
38+
}
39+
catch (runtime_error const&)
40+
{
41+
return 0;
42+
}
43+
FuzzerUtil::testCompiler(sourceCode, /*optimize=*/false, /*_rand=*/_size);
2944
}
3045
return 0;
3146
}

test/tools/ossfuzz/solc_opt_ossfuzz.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,29 @@
1818

1919
#include <test/tools/fuzzer_common.h>
2020

21+
#include <test/TestCaseReader.h>
22+
23+
#include <sstream>
24+
25+
using namespace solidity::frontend::test;
2126
using namespace std;
2227

2328
extern "C" int LLVMFuzzerTestOneInput(uint8_t const* _data, size_t _size)
2429
{
2530
if (_size <= 600)
2631
{
27-
string input(reinterpret_cast<char const *>(_data), _size);
28-
FuzzerUtil::testCompiler(input, /*optimize=*/true);
32+
string input(reinterpret_cast<char const*>(_data), _size);
33+
map<string, string> sourceCode;
34+
try
35+
{
36+
TestCaseReader t = TestCaseReader(std::istringstream(input));
37+
sourceCode = t.sources().sources;
38+
}
39+
catch (runtime_error const&)
40+
{
41+
return 0;
42+
}
43+
FuzzerUtil::testCompiler(sourceCode, /*optimize=*/true, /*rand=*/_size);
2944
}
3045
return 0;
3146
}

0 commit comments

Comments
 (0)