Skip to content

Commit 4ad88ec

Browse files
bors[bot]alex
andauthored
Merge #39
39: Update libFuzzer to be from the 'release_90' branch r=nagisa a=alex Co-authored-by: Alex Gaynor <[email protected]>
2 parents 2797852 + 272e484 commit 4ad88ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3230
-1904
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: rust
22
sudo: false
33
cache: cargo
4-
dist: trusty
4+
dist: bionic
55
rust:
66
- nightly
77
os:
@@ -12,8 +12,8 @@ notifications:
1212
email: false
1313
script:
1414
- cd example
15-
- cargo rustc --release -- -Cpasses='sancov' -Cllvm-args=-sanitizer-coverage-level=4 -Cllvm-args=-sanitizer-coverage-trace-compares -Cpanic=abort -Cllvm-args=-sanitizer-coverage-trace-divs -Cllvm-args=-sanitizer-coverage-trace-geps -Cllvm-args=-sanitizer-coverage-prune-blocks=0 -Zsanitizer=address
15+
- cargo rustc --release -- -Cpasses='sancov' -Cllvm-args=-sanitizer-coverage-level=4 -Cllvm-args=-sanitizer-coverage-trace-compares -Cllvm-args=-sanitizer-coverage-inline-8bit-counters -Cllvm-args=-sanitizer-coverage-stack-depth -Cllvm-args=-sanitizer-coverage-trace-geps -Cllvm-args=-sanitizer-coverage-prune-blocks=0 -Zsanitizer=address
1616
- (! ./target/release/example -runs=100000)
1717
- cd ../example_arbitrary
18-
- cargo rustc --release -- -Cpasses='sancov' -Cllvm-args=-sanitizer-coverage-level=4 -Cllvm-args=-sanitizer-coverage-trace-compares -Cpanic=abort -Cllvm-args=-sanitizer-coverage-trace-divs -Cllvm-args=-sanitizer-coverage-trace-geps -Cllvm-args=-sanitizer-coverage-prune-blocks=0 -Zsanitizer=address
18+
- cargo rustc --release -- -Cpasses='sancov' -Cllvm-args=-sanitizer-coverage-level=4 -Cllvm-args=-sanitizer-coverage-trace-compares -Cllvm-args=-sanitizer-coverage-inline-8bit-counters -Cllvm-args=-sanitizer-coverage-stack-depth -Cllvm-args=-sanitizer-coverage-trace-geps -Cllvm-args=-sanitizer-coverage-prune-blocks=0 -Zsanitizer=address
1919
- (! ./target/release/example -runs=10000000)

libfuzzer/CMakeLists.txt

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ set(LIBFUZZER_SOURCES
33
FuzzerDataFlowTrace.cpp
44
FuzzerDriver.cpp
55
FuzzerExtFunctionsDlsym.cpp
6-
FuzzerExtFunctionsDlsymWin.cpp
76
FuzzerExtFunctionsWeak.cpp
7+
FuzzerExtFunctionsWindows.cpp
88
FuzzerExtraCounters.cpp
9+
FuzzerFork.cpp
910
FuzzerIO.cpp
1011
FuzzerIOPosix.cpp
1112
FuzzerIOWindows.cpp
1213
FuzzerLoop.cpp
1314
FuzzerMerge.cpp
1415
FuzzerMutate.cpp
1516
FuzzerSHA1.cpp
16-
FuzzerShmemFuchsia.cpp
17-
FuzzerShmemPosix.cpp
18-
FuzzerShmemWindows.cpp
1917
FuzzerTracePC.cpp
2018
FuzzerUtil.cpp
2119
FuzzerUtilDarwin.cpp
@@ -25,6 +23,8 @@ set(LIBFUZZER_SOURCES
2523
FuzzerUtilWindows.cpp)
2624

2725
set(LIBFUZZER_HEADERS
26+
FuzzerBuiltins.h
27+
FuzzerBuiltinsMsvc.h
2828
FuzzerCommand.h
2929
FuzzerCorpus.h
3030
FuzzerDataFlowTrace.h
@@ -33,6 +33,7 @@ set(LIBFUZZER_HEADERS
3333
FuzzerExtFunctions.def
3434
FuzzerExtFunctions.h
3535
FuzzerFlags.def
36+
FuzzerFork.h
3637
FuzzerIO.h
3738
FuzzerInterface.h
3839
FuzzerInternal.h
@@ -41,7 +42,6 @@ set(LIBFUZZER_HEADERS
4142
FuzzerOptions.h
4243
FuzzerRandom.h
4344
FuzzerSHA1.h
44-
FuzzerShmem.h
4545
FuzzerTracePC.h
4646
FuzzerUtil.h
4747
FuzzerValueBitMap.h)
@@ -55,7 +55,9 @@ CHECK_CXX_SOURCE_COMPILES("
5555

5656
set(LIBFUZZER_CFLAGS ${SANITIZER_COMMON_CFLAGS})
5757

58-
if(OS_NAME MATCHES "Linux|Fuchsia" AND COMPILER_RT_LIBCXX_PATH)
58+
if(OS_NAME MATCHES "Linux|Fuchsia" AND
59+
COMPILER_RT_LIBCXX_PATH AND
60+
COMPILER_RT_LIBCXXABI_PATH)
5961
list(APPEND LIBFUZZER_CFLAGS -nostdinc++ -D_LIBCPP_ABI_VERSION=Fuzzer)
6062
# Remove -stdlib= which is unused when passing -nostdinc++.
6163
string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
@@ -69,12 +71,21 @@ if (CMAKE_CXX_FLAGS MATCHES "fsanitize-coverage")
6971
list(APPEND LIBFUZZER_CFLAGS -fno-sanitize-coverage=trace-pc-guard,edge,trace-cmp,indirect-calls,8bit-counters)
7072
endif()
7173

72-
if(NOT HAS_THREAD_LOCAL)
73-
list(APPEND LIBFUZZER_CFLAGS -Dthread_local=__thread)
74+
if(MSVC)
75+
# Silence warnings by turning off exceptions in MSVC headers and avoid an
76+
# error by unecessarily defining thread_local when it isn't even used on
77+
# Windows.
78+
list(APPEND LIBFUZZER_CFLAGS -D_HAS_EXCEPTIONS=0)
79+
else()
80+
if(NOT HAS_THREAD_LOCAL)
81+
list(APPEND LIBFUZZER_CFLAGS -Dthread_local=__thread)
82+
endif()
7483
endif()
7584

7685
set(FUZZER_SUPPORTED_OS ${SANITIZER_COMMON_SUPPORTED_OS})
7786

87+
add_compiler_rt_component(fuzzer)
88+
7889
add_compiler_rt_object_libraries(RTfuzzer
7990
OS ${FUZZER_SUPPORTED_OS}
8091
ARCHS ${FUZZER_SUPPORTED_ARCH}
@@ -106,12 +117,19 @@ add_compiler_rt_runtime(clang_rt.fuzzer_no_main
106117
CFLAGS ${LIBFUZZER_CFLAGS}
107118
PARENT_TARGET fuzzer)
108119

109-
if(OS_NAME MATCHES "Linux|Fuchsia" AND COMPILER_RT_LIBCXX_PATH)
120+
if(OS_NAME MATCHES "Linux|Fuchsia" AND
121+
COMPILER_RT_LIBCXX_PATH AND
122+
COMPILER_RT_LIBCXXABI_PATH)
110123
macro(partially_link_libcxx name dir arch)
124+
if(${arch} MATCHES "i386")
125+
set(EMULATION_ARGUMENT "-m" "elf_i386")
126+
else()
127+
set(EMULATION_ARGUMENT "")
128+
endif()
111129
set(cxx_${arch}_merge_dir "${CMAKE_CURRENT_BINARY_DIR}/cxx_${arch}_merge.dir")
112130
file(MAKE_DIRECTORY ${cxx_${arch}_merge_dir})
113131
add_custom_command(TARGET clang_rt.${name}-${arch} POST_BUILD
114-
COMMAND ${CMAKE_LINKER} --whole-archive "$<TARGET_LINKER_FILE:clang_rt.${name}-${arch}>" --no-whole-archive ${dir}/lib/libc++.a -r -o ${name}.o
132+
COMMAND ${CMAKE_LINKER} ${EMULATION_ARGUMENT} --whole-archive "$<TARGET_LINKER_FILE:clang_rt.${name}-${arch}>" --no-whole-archive ${dir}/lib/libc++.a -r -o ${name}.o
115133
COMMAND ${CMAKE_OBJCOPY} --localize-hidden ${name}.o
116134
COMMAND ${CMAKE_COMMAND} -E remove "$<TARGET_LINKER_FILE:clang_rt.${name}-${arch}>"
117135
COMMAND ${CMAKE_AR} qcs "$<TARGET_LINKER_FILE:clang_rt.${name}-${arch}>" ${name}.o
@@ -124,13 +142,8 @@ if(OS_NAME MATCHES "Linux|Fuchsia" AND COMPILER_RT_LIBCXX_PATH)
124142
set(LIBCXX_${arch}_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libcxx_fuzzer_${arch})
125143
add_custom_libcxx(libcxx_fuzzer_${arch} ${LIBCXX_${arch}_PREFIX}
126144
CFLAGS ${TARGET_CFLAGS}
127-
-D_LIBCPP_ABI_VERSION=Fuzzer
128-
-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=1
129-
-fvisibility=hidden
130145
CMAKE_ARGS -DCMAKE_CXX_COMPILER_WORKS=ON
131-
-DLIBCXX_ENABLE_EXCEPTIONS=OFF
132-
-DLIBCXX_ENABLE_SHARED=OFF
133-
-DLIBCXX_CXX_ABI=none)
146+
-DLIBCXX_ABI_NAMESPACE=Fuzzer)
134147
target_compile_options(RTfuzzer.${arch} PRIVATE -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
135148
add_dependencies(RTfuzzer.${arch} libcxx_fuzzer_${arch}-build)
136149
target_compile_options(RTfuzzer_main.${arch} PRIVATE -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)

libfuzzer/FuzzerBuiltins.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- FuzzerBuiltins.h - Internal header for builtins ----------*- C++ -* ===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Wrapper functions and marcos around builtin functions.
9+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef LLVM_FUZZER_BUILTINS_H
12+
#define LLVM_FUZZER_BUILTINS_H
13+
14+
#include "FuzzerDefs.h"
15+
16+
#if !LIBFUZZER_MSVC
17+
#include <cstdint>
18+
19+
#define GET_CALLER_PC() __builtin_return_address(0)
20+
21+
namespace fuzzer {
22+
23+
inline uint8_t Bswap(uint8_t x) { return x; }
24+
inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
25+
inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
26+
inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
27+
28+
inline uint32_t Clzll(unsigned long long X) { return __builtin_clzll(X); }
29+
inline uint32_t Clz(unsigned long long X) { return __builtin_clz(X); }
30+
inline int Popcountll(unsigned long long X) { return __builtin_popcountll(X); }
31+
32+
} // namespace fuzzer
33+
34+
#endif // !LIBFUZZER_MSVC
35+
#endif // LLVM_FUZZER_BUILTINS_H

libfuzzer/FuzzerBuiltinsMsvc.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===- FuzzerBuiltinsMSVC.h - Internal header for builtins ------*- C++ -* ===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Wrapper functions and marcos that use intrinsics instead of builtin functions
9+
// which cannot be compiled by MSVC.
10+
//===----------------------------------------------------------------------===//
11+
12+
#ifndef LLVM_FUZZER_BUILTINS_MSVC_H
13+
#define LLVM_FUZZER_BUILTINS_MSVC_H
14+
15+
#include "FuzzerDefs.h"
16+
17+
#if LIBFUZZER_MSVC
18+
#if !defined(_M_ARM) && !defined(_M_X64)
19+
#error "_BitScanReverse64 unavailable on this platform so MSVC is unsupported."
20+
#endif
21+
#include <intrin.h>
22+
#include <cstdint>
23+
#include <cstdlib>
24+
25+
// __builtin_return_address() cannot be compiled with MSVC. Use the equivalent
26+
// from <intrin.h>
27+
#define GET_CALLER_PC() _ReturnAddress()
28+
29+
namespace fuzzer {
30+
31+
inline uint8_t Bswap(uint8_t x) { return x; }
32+
// Use alternatives to __builtin functions from <stdlib.h> and <intrin.h> on
33+
// Windows since the builtins are not supported by MSVC.
34+
inline uint16_t Bswap(uint16_t x) { return _byteswap_ushort(x); }
35+
inline uint32_t Bswap(uint32_t x) { return _byteswap_ulong(x); }
36+
inline uint64_t Bswap(uint64_t x) { return _byteswap_uint64(x); }
37+
38+
// The functions below were mostly copied from
39+
// compiler-rt/lib/builtins/int_lib.h which defines the __builtin functions used
40+
// outside of Windows.
41+
inline uint32_t Clzll(uint64_t X) {
42+
unsigned long LeadZeroIdx = 0;
43+
if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
44+
return 64;
45+
}
46+
47+
inline uint32_t Clz(uint32_t X) {
48+
unsigned long LeadZeroIdx = 0;
49+
if (_BitScanReverse(&LeadZeroIdx, X)) return 31 - LeadZeroIdx;
50+
return 32;
51+
}
52+
53+
inline int Popcountll(unsigned long long X) { return __popcnt64(X); }
54+
55+
} // namespace fuzzer
56+
57+
#endif // LIBFUZER_MSVC
58+
#endif // LLVM_FUZZER_BUILTINS_MSVC_H

libfuzzer/FuzzerCommand.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//===- FuzzerCommand.h - Interface representing a process -------*- C++ -* ===//
22
//
3-
// The LLVM Compiler Infrastructure
4-
//
5-
// This file is distributed under the University of Illinois Open Source
6-
// License. See LICENSE.TXT for details.
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
87
//===----------------------------------------------------------------------===//
98
// FuzzerCommand represents a command to run in a subprocess. It allows callers
@@ -81,7 +80,7 @@ class Command final {
8180
}
8281

8382
// Like hasArgument, but checks for "-[Flag]=...".
84-
bool hasFlag(const std::string &Flag) {
83+
bool hasFlag(const std::string &Flag) const {
8584
std::string Arg("-" + Flag + "=");
8685
auto IsMatch = [&](const std::string &Other) {
8786
return Arg.compare(0, std::string::npos, Other, 0, Arg.length()) == 0;
@@ -92,7 +91,7 @@ class Command final {
9291
// Returns the value of the first instance of a given flag, or an empty string
9392
// if the flag isn't present. Ignores any occurrences after
9493
// "-ignore_remaining_args=1", if present.
95-
std::string getFlagValue(const std::string &Flag) {
94+
std::string getFlagValue(const std::string &Flag) const {
9695
std::string Arg("-" + Flag + "=");
9796
auto IsMatch = [&](const std::string &Other) {
9897
return Arg.compare(0, std::string::npos, Other, 0, Arg.length()) == 0;

libfuzzer/FuzzerCorpus.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//===- FuzzerCorpus.h - Internal header for the Fuzzer ----------*- C++ -* ===//
22
//
3-
// The LLVM Compiler Infrastructure
4-
//
5-
// This file is distributed under the University of Illinois Open Source
6-
// License. See LICENSE.TXT for details.
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
87
//===----------------------------------------------------------------------===//
98
// fuzzer::InputCorpus
@@ -86,9 +85,10 @@ class InputCorpus {
8685

8786
bool empty() const { return Inputs.empty(); }
8887
const Unit &operator[] (size_t Idx) const { return Inputs[Idx]->U; }
89-
void AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile,
90-
bool HasFocusFunction, const Vector<uint32_t> &FeatureSet,
91-
const DataFlowTrace &DFT, const InputInfo *BaseII) {
88+
InputInfo *AddToCorpus(const Unit &U, size_t NumFeatures, bool MayDeleteFile,
89+
bool HasFocusFunction,
90+
const Vector<uint32_t> &FeatureSet,
91+
const DataFlowTrace &DFT, const InputInfo *BaseII) {
9292
assert(!U.empty());
9393
if (FeatureDebug)
9494
Printf("ADD_TO_CORPUS %zd NF %zd\n", Inputs.size(), NumFeatures);
@@ -114,6 +114,7 @@ class InputCorpus {
114114
UpdateCorpusDistribution();
115115
PrintCorpus();
116116
// ValidateFeatureSet();
117+
return &II;
117118
}
118119

119120
// Debug-only
@@ -170,7 +171,7 @@ class InputCorpus {
170171
InputInfo &II = *Inputs[ChooseUnitIdxToMutate(Rand)];
171172
assert(!II.U.empty());
172173
return II;
173-
};
174+
}
174175

175176
// Returns an index of random unit from the corpus to mutate.
176177
size_t ChooseUnitIdxToMutate(Random &Rand) {
@@ -238,12 +239,6 @@ class InputCorpus {
238239
return false;
239240
}
240241

241-
bool IsFeatureNew(size_t Idx, uint32_t NewSize, bool Shrink) {
242-
assert(NewSize);
243-
uint32_t OldSize = GetFeature(Idx % kFeatureSetSize);
244-
return OldSize == 0 || (Shrink && OldSize > NewSize);
245-
}
246-
247242
size_t NumFeatures() const { return NumAddedFeatures; }
248243
size_t NumFeatureUpdates() const { return NumUpdatedFeatures; }
249244

libfuzzer/FuzzerCrossOver.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//===- FuzzerCrossOver.cpp - Cross over two test inputs -------------------===//
22
//
3-
// The LLVM Compiler Infrastructure
4-
//
5-
// This file is distributed under the University of Illinois Open Source
6-
// License. See LICENSE.TXT for details.
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
76
//
87
//===----------------------------------------------------------------------===//
98
// Cross over test inputs.

0 commit comments

Comments
 (0)