Skip to content

Commit b9c2c71

Browse files
committed
Build a lib/Fuzzer version for llvm-as.
Summary: This CL is associated with a fuzzing effort to find bugs in LLVM. The first step is to fuzz llvm-as to find potential issues in generating IR. Both afl-fuzz and LLVM's lib/Fuzzer are being used. This CL introduces the executable that implements the in-process fuzzer using LLVM's lib/Fuzzer. The motivation for using lib/Fuzzer is based on time comparisons between afl-fuzz and lib/Fuzzer. Early results show that per-process, the lib/Fuzzer implemenation of llvm-as (i.e. this CL) generates over 30 times the number of mutations found by afl-fuzz, per hour runtime. The speedup is due to the removal of overhead of forking a process, and loading the executable into memory. I placed this under the tools directory, since it is an executable. It is also only conditionally built if (using cmake) the flag LLVM_USEE_SANITIZE_COVERAGE is used, so that it isn't built by default. Reviewers: kcc, filcab Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D12438 llvm-svn: 246458
1 parent 86dbd92 commit b9c2c71

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if( LLVM_USE_SANITIZE_COVERAGE )
2+
set(LLVM_LINK_COMPONENTS
3+
AsmParser
4+
BitWriter
5+
Core
6+
Support
7+
)
8+
add_llvm_tool(llvm-as-fuzzer
9+
llvm-as-fuzzer.cpp)
10+
target_link_libraries(llvm-as-fuzzer
11+
LLVMFuzzer
12+
)
13+
endif()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===--- fuzz-llvm-as.cpp - Fuzzer for llvm-as using lib/Fuzzer -----------===//
2+
//
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.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// Build tool to fuzz the LLVM assembler (llvm-as) using
11+
// lib/Fuzzer. The main reason for using this tool is that it is much
12+
// faster than using afl-fuzz, since it is run in-process.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/AsmParser/Parser.h"
18+
#include "llvm/IR/LLVMContext.h"
19+
#include "llvm/IR/Module.h"
20+
#include "llvm/IR/Verifier.h"
21+
#include "llvm/Support/ErrorHandling.h"
22+
#include "llvm/Support/MemoryBuffer.h"
23+
#include "llvm/Support/raw_ostream.h"
24+
#include "llvm/Support/SourceMgr.h"
25+
26+
#include <csetjmp>
27+
28+
using namespace llvm;
29+
30+
static jmp_buf JmpBuf;
31+
32+
namespace {
33+
34+
void MyFatalErrorHandler(void *user_data, const std::string& reason,
35+
bool gen_crash_diag) {
36+
// Don't bother printing reason, just return to the test function,
37+
// since a fatal error represents a successful parse (i.e. it correctly
38+
// terminated with an error message to the user).
39+
longjmp(JmpBuf, 1);
40+
}
41+
42+
static bool InstalledHandler = false;
43+
44+
} // end of anonymous namespace
45+
46+
extern "C" void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
47+
48+
// Allocate space for locals before setjmp so that memory can be collected
49+
// if parse exits prematurely (via longjmp).
50+
StringRef Input((const char *)Data, Size);
51+
// Note: We need to create a buffer to add a null terminator to the
52+
// end of the input string. The parser assumes that the string
53+
// parsed is always null terminated.
54+
std::unique_ptr<MemoryBuffer> MemBuf = MemoryBuffer::getMemBufferCopy(Input);
55+
SMDiagnostic Err;
56+
LLVMContext &Context = getGlobalContext();
57+
std::unique_ptr<Module> M;
58+
59+
if (setjmp(JmpBuf))
60+
// If reached, we have returned with non-zero status, so exit.
61+
return;
62+
63+
// TODO(kschimpf) Write a main to do this initialization.
64+
if (!InstalledHandler) {
65+
llvm::install_fatal_error_handler(::MyFatalErrorHandler, nullptr);
66+
InstalledHandler = true;
67+
}
68+
69+
M = parseAssembly(MemBuf->getMemBufferRef(), Err, Context);
70+
71+
if (!M.get())
72+
return;
73+
74+
verifyModule(*M.get());
75+
}

0 commit comments

Comments
 (0)