Skip to content

Commit ddb3201

Browse files
authored
Merge pull request #689 from derwolfe/san-2
Sanitizer build for fuzzers
2 parents c07452a + dc51f64 commit ddb3201

File tree

7 files changed

+193
-8
lines changed

7 files changed

+193
-8
lines changed

.travis.yml

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ install:
1010
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then ./cmake-3.7.1-Linux-x86_64.sh --prefix=${BASE}/usr --skip-license; fi
1111
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then export PATH="${BASE}/usr/bin:$PATH"; fi
1212
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then export LD_LIBRARY_PATH="${BASE}/usr/lib:$LD_LIBRARY_PATH"; fi
13-
- if [ "$CXX" = "g++" ]; then export CXX="g++-6" CC="gcc-6"; fi
14-
- if [ "$CXX" = "clang++" ] && [ "$TRAVIS_OS_NAME" == "linux" ]; then export CXX="clang++-3.9" CC="clang-3.9"; fi
15-
- if [ "$CXX" = "g++" ] && [ "$TRAVIS_OS_NAME" == "linux" ]; then cd /usr/include/c++/$(g++ -v 2>&1 | grep version | awk '{print $3}')/bits; ln -s ../ext/atomicity.h .; cd ${BASE}/usr; fi
13+
- if [ "$CXX" == "g++" ]; then export CXX="g++-6" CC="gcc-6"; fi
14+
- if [ "$CXX" == "clang++" ] && [ "$TRAVIS_OS_NAME" == "linux" ] && [ "$MSGPACK_FUZZ_REGRESSION" != "ON" ]; then export CXX="clang++-3.9" CC="clang-3.9"; fi
15+
- if [ "$CXX" == "clang++" ] && [ "$TRAVIS_OS_NAME" == "linux" ] && [ "$MSGPACK_FUZZ_REGRESSION" == "ON" ]; then export CXX="clang++-5.0" CC="clang-5.0"; fi
16+
- if [ "$CXX" == "g++" ] && [ "$TRAVIS_OS_NAME" == "linux" ]; then cd /usr/include/c++/$(g++ -v 2>&1 | grep version | awk '{print $3}')/bits; ln -s ../ext/atomicity.h .; cd ${BASE}/usr; fi
1617
#gtest
1718
- wget https://github.com/google/googletest/archive/release-1.7.0.zip -O googletest-release-1.7.0.zip
1819
- unzip -q googletest-release-1.7.0.zip
@@ -26,10 +27,11 @@ install:
2627
- mkdir -p ${BASE}/usr/lib
2728
- mv *.a ${BASE}/usr/lib
2829
- cd ..
29-
# valgrind
30-
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then wget http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2 && tar xjf valgrind-3.12.0.tar.bz2 && cd valgrind-3.12.0 && ./configure --prefix=${BASE}/usr > /dev/null && make -j3 > /dev/null && make install > /dev/null && cd ..; fi
31-
# boost
32-
- if [ "$BOOST" == "ON" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.66.0/boost_1_66_0.zip && unzip -q boost_1_66_0.zip && cd boost_1_66_0 && ./bootstrap.sh && ./b2 -j3 --prefix=${BASE}/usr --with-chrono --with-context --with-system --with-timer address-model=${ARCH} install > /dev/null && cd ..; fi
30+
# valgrind
31+
- if [ "$TRAVIS_OS_NAME" == "linux" ] && [ "$MSGPACK_FUZZ_REGRESSION" != "ON" ]; then wget http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2 && tar xjf valgrind-3.12.0.tar.bz2 && cd valgrind-3.12.0 && ./configure --prefix=${BASE}/usr > /dev/null && make -j3 > /dev/null && make install > /dev/null && cd ..; fi
32+
# boost
33+
- if [ "$BOOST" == "ON" ] && [ "MSGPACK_FUZZ_REGRESSION" != "ON" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.66.0/boost_1_66_0.zip && unzip -q boost_1_66_0.zip && cd boost_1_66_0 && ./bootstrap.sh && ./b2 -j3 --prefix=${BASE}/usr --with-chrono --with-context --with-system --with-timer address-model=${ARCH} install > /dev/null && cd ..; fi
34+
- if [ "$MSGPACK_FUZZ_REGRESSION" == "ON" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.66.0/boost_1_66_0.zip && unzip -q boost_1_66_0.zip && cd boost_1_66_0 && ./bootstrap.sh --with-toolset=clang && ./b2 clean && ./b2 -j$(nproc) --prefix=${BASE}/usr --with-system --with-filesystem address-model=${ARCH} install > /dev/null && cd ..; fi
3335
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s /usr/include/x86_64-linux-gnu/zconf.h /usr/include; fi
3436

3537
matrix:
@@ -209,6 +211,35 @@ matrix:
209211
- zlib1g-dev
210212
- bzip2
211213
- libc6-dbg
214+
- os: linux
215+
compiler: clang
216+
env: ACTION="ci/build_regression.sh" ARCH="64" SAN="UBSAN" MSGPACK_FUZZ_REGRESSION="ON"
217+
addons:
218+
apt:
219+
sources:
220+
- ubuntu-toolchain-r-test
221+
- llvm-toolchain-precise-3.9
222+
- llvm-toolchain-precise
223+
packages:
224+
- bzip2
225+
- clang-5.0
226+
script:
227+
- CMAKE_CXX_COMPILER="${CXX}" CMAKE_C_COMPILER="${CC}" CMAKE_LIBRARY_PATH="${BASE}" GTEST_ROOT="${BASE}/gtest" BOOST_ROOT="${BASE}/boost" CTEST_OUTPUT_ON_FAILURE=1 MSGPACK_SAN="${SAN}" ci/build_regression.sh
228+
- os: linux
229+
compiler: clang
230+
env: ACTION="ci/build_regression.sh" ARCH="64" SAN="ASAN" MSGPACK_FUZZ_REGRESSION="ON"
231+
addons:
232+
apt:
233+
sources:
234+
- ubuntu-toolchain-r-test
235+
- llvm-toolchain-precise-3.9
236+
- llvm-toolchain-precise
237+
packages:
238+
- bzip2
239+
- clang-5.0
240+
script:
241+
- CMAKE_CXX_COMPILER="${CXX}" CMAKE_C_COMPILER="${CC}" CMAKE_LIBRARY_PATH="${BASE}" GTEST_ROOT="${BASE}/gtest" BOOST_ROOT="${BASE}/boost" CTEST_OUTPUT_ON_FAILURE=1 MSGPACK_SAN="${SAN}" ci/build_regression.sh
242+
212243

213244
script:
214245
- CMAKE_CXX_COMPILER="${CXX}" CMAKE_C_COMPILER="${CC}" CMAKE_LIBRARY_PATH="${BASE}/usr/lib:${BASE}/build" GTEST_ROOT="${BASE}/usr" BOOST_ROOT="${BASE}/usr" CFLAGS="-Werror -g" CXXFLAGS="-Werror -g" ${ACTION}

CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ ENDFOREACH ()
150150
FIND_PACKAGE (GTest)
151151
FIND_PACKAGE (ZLIB)
152152
FIND_PACKAGE (Threads)
153-
IF (GTEST_FOUND AND ZLIB_FOUND AND THREADS_FOUND)
153+
IF (GTEST_FOUND AND ZLIB_FOUND AND THREADS_FOUND AND NOT "${MSGPACK_FUZZ_REGRESSION}" STREQUAL "ON")
154154
OPTION (MSGPACK_BUILD_TESTS "Build msgpack tests." ON)
155155
ENDIF ()
156156

@@ -226,6 +226,18 @@ IF (MSGPACK_ENABLE_SHARED)
226226
SET_TARGET_PROPERTIES (msgpackc PROPERTIES SOVERSION 2 VERSION 2.0.0)
227227
ENDIF ()
228228

229+
# enable regression testing
230+
IF ("${MSGPACK_FUZZ_REGRESSION}" STREQUAL "ON" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
231+
SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_BOOST ${CMAKE_CXX_FLAGS}")
232+
SET (Boost_USE_MULTITHREADED ON)
233+
SET (Boost_USE_STATIC_RUNTIME OFF)
234+
235+
enable_testing ()
236+
ADD_SUBDIRECTORY (fuzz)
237+
SET (MSGPACK_BUILD_EXAMPLES OFF)
238+
ENDIF ()
239+
240+
229241
IF (MSGPACK_BUILD_TESTS)
230242
ENABLE_TESTING ()
231243
# MEMORYCHECK_COMMAND_OPTIONS needs to place prior to CTEST_MEMORYCHECK_COMMAND

ci/build_regression.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
mkdir -p build
4+
5+
ret=$?
6+
if [ $ret -ne 0 ]
7+
then
8+
exit $ret
9+
fi
10+
11+
cd build
12+
13+
ret=$?
14+
if [ $ret -ne 0 ]
15+
then
16+
exit $ret
17+
fi
18+
19+
if [ "${ARCH}" == "32" ]
20+
then
21+
echo "64 bit support required for regressions"
22+
exit 1
23+
fi
24+
25+
cmake -DMSGPACK_FUZZ_REGRESSION="ON" -DMSGPACK_CXX11="ON" -DMSGPACK_BOOST=${BOOST} -DMSGPACK_SAN=${MSGPACK_SAN} -v ..
26+
27+
ret=$?
28+
if [ $ret -ne 0 ]
29+
then
30+
exit $ret
31+
fi
32+
33+
make
34+
35+
ret=$?
36+
if [ $ret -ne 0 ]
37+
then
38+
exit $ret
39+
fi
40+
41+
make test
42+
43+
ret=$?
44+
if [ $ret -ne 0 ]
45+
then
46+
exit $ret
47+
fi
48+
49+
exit 0

fuzz/CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FIND_PACKAGE (GTest REQUIRED)
2+
FIND_PACKAGE (ZLIB REQUIRED)
3+
FIND_PACKAGE (Boost REQUIRED COMPONENTS system filesystem)
4+
5+
INCLUDE_DIRECTORIES (
6+
${GTEST_INCLUDE_DIRS}
7+
${ZLIB_INCLUDE_DIRS}
8+
${Boost_INCLUDE_DIRS}
9+
)
10+
11+
LIST (APPEND check_PROGRAMS
12+
regression_runner.cpp
13+
)
14+
15+
LINK_DIRECTORIES (
16+
${Boost_LIBRARY_DIRS}
17+
)
18+
19+
FOREACH (source_file ${check_PROGRAMS})
20+
GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE)
21+
ADD_EXECUTABLE (
22+
${source_file_we}
23+
${source_file}
24+
)
25+
TARGET_LINK_LIBRARIES (${source_file_we}
26+
msgpackc
27+
${GTEST_BOTH_LIBRARIES}
28+
${ZLIB_LIBRARIES}
29+
${CMAKE_THREAD_LIBS_INIT}
30+
${Boost_LIBRARIES}
31+
)
32+
ADD_TEST (${source_file_we} ${source_file_we})
33+
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
34+
SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Wno-mismatched-tags -g")
35+
IF ("${MSGPACK_SAN}" STREQUAL "ASAN")
36+
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
37+
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
38+
ELSEIF ("${MSGPACK_SAN}" STREQUAL "UBSAN")
39+
SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
40+
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
41+
ENDIF()
42+
ENDIF()
43+
ENDFOREACH ()

fuzz/regression_runner.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <boost/filesystem.hpp>
2+
#include <gtest/gtest.h>
3+
4+
#include <fstream>
5+
#include <iostream>
6+
#include <vector>
7+
8+
#include "unpack_pack_fuzzer.cpp"
9+
10+
using ::testing::TestWithParam;
11+
using ::testing::ValuesIn;
12+
13+
14+
std::vector<std::string> ListDirectory(const std::string& path) {
15+
std::vector<std::string> v;
16+
17+
boost::filesystem::path p(path);
18+
boost::filesystem::directory_iterator f{p};
19+
20+
if(boost::filesystem::is_directory(p)) {
21+
while (f != boost::filesystem::directory_iterator{}) {
22+
v.push_back((*f++).path().string());
23+
}
24+
}
25+
return v;
26+
}
27+
28+
class UnpackPackFuzzerRegressionTest : public ::testing::TestWithParam<std::string> {
29+
public:
30+
};
31+
32+
TEST_P(UnpackPackFuzzerRegressionTest, Returns0) {
33+
auto fpath = GetParam();
34+
std::ifstream in(fpath, std::ifstream::binary);
35+
if (!in) {
36+
FAIL() << fpath << " not found";
37+
}
38+
in.seekg(0, in.end);
39+
size_t length = in.tellg();
40+
in.seekg(0, in.beg);
41+
std::vector<char> bytes(length);
42+
in.read(bytes.data(), bytes.size());
43+
assert(in);
44+
EXPECT_EQ(0, LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t *>(bytes.data()),
45+
bytes.size()));
46+
}
47+
48+
INSTANTIATE_TEST_CASE_P(UnpackPackFuzzerRegressions,
49+
UnpackPackFuzzerRegressionTest,
50+
::testing::ValuesIn(ListDirectory("../../fuzz/unpack_pack_fuzzer_regressions")));

0 commit comments

Comments
 (0)