Skip to content

Commit

Permalink
lib Fuzzer FreeBSD support
Browse files Browse the repository at this point in the history
Summary: Patch by David CARLIER

Reviewers: vitalybuka, kcc, dim, emaste, davide, morehouse, george.karpenkov

Reviewed By: morehouse

Subscribers: george.karpenkov, kubamracek, srhines, mgorny, emaste, krytarowski

Differential Revision: https://reviews.llvm.org/D41642

llvm-svn: 322380
  • Loading branch information
krytarowski committed Jan 12, 2018
1 parent 93483e7 commit e81e944
Show file tree
Hide file tree
Showing 28 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion compiler-rt/cmake/config-ix.cmake
Expand Up @@ -604,7 +604,7 @@ else()
endif()

if (COMPILER_RT_HAS_SANITIZER_COMMON AND FUZZER_SUPPORTED_ARCH AND
OS_NAME MATCHES "Android|Darwin|Linux|NetBSD")
OS_NAME MATCHES "Android|Darwin|Linux|NetBSD|FreeBSD")
set(COMPILER_RT_HAS_FUZZER TRUE)
else()
set(COMPILER_RT_HAS_FUZZER FALSE)
Expand Down
14 changes: 13 additions & 1 deletion compiler-rt/lib/fuzzer/FuzzerDefs.h
Expand Up @@ -27,30 +27,42 @@
#define LIBFUZZER_FUCHSIA 0
#define LIBFUZZER_LINUX 1
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 0
#define LIBFUZZER_WINDOWS 0
#elif __APPLE__
#define LIBFUZZER_APPLE 1
#define LIBFUZZER_FUCHSIA 0
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 0
#define LIBFUZZER_WINDOWS 0
#elif __NetBSD__
#define LIBFUZZER_APPLE 0
#define LIBFUZZER_FUCHSIA 0
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_NETBSD 1
#define LIBFUZZER_FREEBSD 0
#define LIBFUZZER_WINDOWS 0
#elif __FreeBSD__
#define LIBFUZZER_APPLE 0
#define LIBFUZZER_FUCHSIA 0
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 1
#define LIBFUZZER_WINDOWS 0
#elif _WIN32
#define LIBFUZZER_APPLE 0
#define LIBFUZZER_FUCHSIA 0
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 0
#define LIBFUZZER_WINDOWS 1
#elif __Fuchsia__
#define LIBFUZZER_APPLE 0
#define LIBFUZZER_FUCHSIA 1
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 0
#define LIBFUZZER_WINDOWS 0
#else
#error "Support for your platform has not been implemented"
Expand All @@ -60,7 +72,7 @@
# define __has_attribute(x) 0
#endif

#define LIBFUZZER_POSIX (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD)
#define LIBFUZZER_POSIX (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD)

#ifdef __x86_64
# if __has_attribute(target)
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/fuzzer/FuzzerExtFunctionsWeak.cpp
Expand Up @@ -13,7 +13,7 @@
// to clients right now.
//===----------------------------------------------------------------------===//
#include "FuzzerDefs.h"
#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FUCHSIA
#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FUCHSIA || LIBFUZZER_FREEBSD

#include "FuzzerExtFunctions.h"
#include "FuzzerIO.h"
Expand Down Expand Up @@ -51,4 +51,4 @@ ExternalFunctions::ExternalFunctions() {

} // namespace fuzzer

#endif // LIBFUZZER_LINUX || LIBFUZZER_NETBSD
#endif // LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FUSCHIA || LIBFUZZER_FREEBSD
2 changes: 1 addition & 1 deletion compiler-rt/lib/fuzzer/FuzzerExtraCounters.cpp
Expand Up @@ -11,7 +11,7 @@

#include "FuzzerDefs.h"

#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD
#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD
__attribute__((weak)) extern uint8_t __start___libfuzzer_extra_counters;
__attribute__((weak)) extern uint8_t __stop___libfuzzer_extra_counters;

Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp
Expand Up @@ -9,7 +9,7 @@
// Misc utils for Linux.
//===----------------------------------------------------------------------===//
#include "FuzzerDefs.h"
#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD
#if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD
#include "FuzzerCommand.h"

#include <stdlib.h>
Expand All @@ -23,4 +23,4 @@ int ExecuteCommand(const Command &Cmd) {

} // namespace fuzzer

#endif // LIBFUZZER_LINUX || LIBFUZZER_NETBSD
#endif // LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD
2 changes: 1 addition & 1 deletion compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
Expand Up @@ -118,7 +118,7 @@ size_t GetPeakRSSMb() {
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage))
return 0;
if (LIBFUZZER_LINUX) {
if (LIBFUZZER_LINUX || LIBFUZZER_FREEBSD || LIBFUZZER_NETBSD) {
// ru_maxrss is in KiB
return usage.ru_maxrss >> 10;
} else if (LIBFUZZER_APPLE) {
Expand Down
10 changes: 9 additions & 1 deletion compiler-rt/lib/fuzzer/afl/afl_driver.cpp
Expand Up @@ -69,14 +69,22 @@ statistics from the file. If that fails then the process will quit.
#define LIBFUZZER_LINUX 1
#define LIBFUZZER_APPLE 0
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 0
#elif __APPLE__
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_APPLE 1
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 0
#elif __NetBSD__
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_APPLE 0
#define LIBFUZZER_NETBSD 1
#define LIBFUZZER_FREEBSD 0
#elif __FreeBSD__
#define LIBFUZZER_LINUX 0
#define LIBFUZZER_APPLE 0
#define LIBFUZZER_NETBSD 0
#define LIBFUZZER_FREEBSD 1
#else
#error "Support for your platform has not been implemented"
#endif
Expand Down Expand Up @@ -125,7 +133,7 @@ size_t GetPeakRSSMb() {
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage))
return 0;
if (LIBFUZZER_LINUX || LIBFUZZER_NETBSD) {
if (LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD) {
// ru_maxrss is in KiB
return usage.ru_maxrss >> 10;
} else if (LIBFUZZER_APPLE) {
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/fuzzer/build.sh
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
LIBFUZZER_SRC_DIR=$(dirname $0)
CXX="${CXX:-clang}"
for f in $LIBFUZZER_SRC_DIR/*.cpp; do
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/fuzzer/tests/CMakeLists.txt
Expand Up @@ -12,8 +12,8 @@ set_target_properties(FuzzerUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
set(LIBFUZZER_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS})
list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS --driver-mode=g++)

if(APPLE)
list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS -lc++)
if(APPLE OR CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS -lc++ -lpthread)
else()
list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS -lstdc++ -lpthread)
endif()
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
Expand Up @@ -36,7 +36,7 @@
#endif

// TLS is handled differently on different platforms
#if SANITIZER_LINUX || SANITIZER_NETBSD
#if SANITIZER_LINUX || SANITIZER_NETBSD || SANITIZER_FREEBSD
# define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
__attribute__((tls_model("initial-exec"))) thread_local
#else
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/TraceMallocThreadedTest.cpp
Expand Up @@ -7,6 +7,7 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <thread>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/afl-driver-stderr.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %no_fuzzer_cpp_compiler -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters,trace-pc-guard %S/AFLDriverTest.cpp %libfuzzer_src/afl/afl_driver.cpp -o %t-AFLDriverTest

; Test that not specifying a stderr file isn't broken.
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/caller-callee.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/CallerCalleeTest.cpp -o %t-CallerCalleeTest
CHECK: BINGO
RUN: not %t-CallerCalleeTest -use_value_profile=1 -cross_over=0 -seed=1 -runs=10000000 2>&1 | FileCheck %s
2 changes: 1 addition & 1 deletion compiler-rt/test/fuzzer/cxxstring.test
@@ -1,4 +1,4 @@
UNSUPPORTED: windows
UNSUPPORTED: windows,freebsd

RUN: %cpp_compiler %S/CxxStringEqTest.cpp -o %t-CxxStringEqTest

Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/dump_coverage.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler -fsanitize-coverage=0 -fsanitize-coverage=trace-pc-guard %S/DSO1.cpp -fPIC -shared -o %t-DSO1.so
RUN: %cpp_compiler -fsanitize-coverage=0 -fsanitize-coverage=trace-pc-guard %S/DSO2.cpp -fPIC -shared -o %t-DSO2.so
RUN: %cpp_compiler -fsanitize-coverage=0 -fsanitize-coverage=trace-pc-guard %S/DSOTestMain.cpp %S/DSOTestExtra.cpp -L. %t-DSO1.so %t-DSO2.so -o %t-DSOTest
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/equivalence-signals.test
@@ -1,6 +1,7 @@
# Run EquivalenceATest against itself with a small timeout
# to stress the signal handling and ensure that shmem doesn't mind
# the signals.
UNSUPPORTED: freebsd

RUN: %cpp_compiler %S/EquivalenceATest.cpp -o %t-EquivalenceATest
RUN: %t-EquivalenceATest -timeout=1 -run_equivalence_server=EQUIV_SIG_TEST & export APID=$!
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/equivalence.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/EquivalenceATest.cpp -o %t-EquivalenceATest
RUN: %cpp_compiler %S/EquivalenceBTest.cpp -o %t-EquivalenceBTest

Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/test/fuzzer/lit.cfg
Expand Up @@ -25,7 +25,7 @@ else:
config.test_format = lit.formats.ShTest(execute_external)

# LeakSanitizer is not supported on OSX right now.
if sys.platform.startswith('darwin'):
if sys.platform.startswith('darwin') or sys.platform.startswith('freebsd'):
lit_config.note('lsan feature unavailable')
else:
lit_config.note('lsan feature available')
Expand All @@ -51,7 +51,7 @@ config.substitutions.append(('%libfuzzer_src', libfuzzer_src_root))

def generate_compiler_cmd(is_cpp=True, fuzzer_enabled=True):
compiler_cmd = config.c_compiler
link_cmd = '-lc++' if 'darwin' in config.target_triple else '-lstdc++'
link_cmd = '-lc++' if 'darwin' or 'freebsd' in config.target_triple else '-lstdc++'
std_cmd = '-std=c++11' if is_cpp else ''
sanitizers = ['address']
if fuzzer_enabled:
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/memcmp.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/MemcmpTest.cpp -o %t-MemcmpTest
RUN: not %t-MemcmpTest -seed=1 -runs=10000000 2>&1 | FileCheck %s
CHECK: BINGO
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/memcmp64.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/Memcmp64BytesTest.cpp -o %t-Memcmp64BytesTest
RUN: not %t-Memcmp64BytesTest -seed=1 -runs=1000000 2>&1 | FileCheck %s
CHECK: BINGO
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/minimize_two_crashes.test
@@ -1,4 +1,5 @@
# Test that the minimizer stops when it sees a differe bug.
UNSUPPORTED: freebsd

RUN: %cpp_compiler %S/TwoDifferentBugsTest.cpp -o %t-TwoDifferentBugsTest

Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/recommended-dictionary.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/RepeatedMemcmp.cpp -o %t-RepeatedMemcmp
RUN: %t-RepeatedMemcmp -seed=11 -runs=100000 -max_len=20 2>&1 | FileCheck %s --check-prefix=RECOMMENDED_DICT
RECOMMENDED_DICT:###### Recommended dictionary. ######
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/strcmp.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/StrcmpTest.cpp -o %t-StrcmpTest
RUN: not %t-StrcmpTest -seed=1 -runs=2000000 2>&1 | FileCheck %s
CHECK: BINGO
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/strncmp.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/StrncmpTest.cpp -o %t-StrncmpTest
RUN: not %t-StrncmpTest -seed=2 -runs=10000000 2>&1 | FileCheck %s
CHECK: BINGO
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/strstr.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
RUN: %cpp_compiler %S/StrstrTest.cpp -o %t-StrstrTest
RUN: not %t-StrstrTest -seed=1 -runs=2000000 2>&1 | FileCheck %s
CHECK: BINGO
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/value-profile-mem.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
CHECK: BINGO
RUN: %cpp_compiler %S/SingleMemcmpTest.cpp -o %t-SingleMemcmpTest
RUN: not %t-SingleMemcmpTest -seed=1 -use_cmp=0 -use_value_profile=1 -runs=10000000 2>&1 | FileCheck %s
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/value-profile-strcmp.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
CHECK: BINGO
RUN: %cpp_compiler %S/SingleStrcmpTest.cpp -o %t-SingleStrcmpTest
RUN: not %t-SingleStrcmpTest -seed=1 -use_cmp=0 -use_value_profile=1 -runs=10000000 2>&1 | FileCheck %s
1 change: 1 addition & 0 deletions compiler-rt/test/fuzzer/value-profile-strncmp.test
@@ -1,3 +1,4 @@
UNSUPPORTED: freebsd
CHECK: BINGO
RUN: %cpp_compiler %S/SingleStrncmpTest.cpp -o %t-SingleStrncmpTest
RUN: not %t-SingleStrncmpTest -seed=1 -use_cmp=0 -use_value_profile=1 -runs=100000000 2>&1 | FileCheck %s

0 comments on commit e81e944

Please sign in to comment.