[fuzzer] Fix threaded stack printing

Reviewers: kcc

Subscribers: llvm-commits

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

llvm-svn: 317071
This commit is contained in:
Vitaly Buka 2017-11-01 03:02:59 +00:00
parent 050b53b311
commit 7dbc1d8433
3 changed files with 63 additions and 0 deletions

View File

@ -19,6 +19,7 @@
#include <algorithm>
#include <cstring>
#include <memory>
#include <mutex>
#include <set>
#if defined(__has_include)
@ -73,11 +74,14 @@ struct MallocFreeTracer {
static MallocFreeTracer AllocTracer;
static std::mutex MallocFreeStackMutex;
ATTRIBUTE_NO_SANITIZE_MEMORY
void MallocHook(const volatile void *ptr, size_t size) {
size_t N = AllocTracer.Mallocs++;
F->HandleMalloc(size);
if (int TraceLevel = AllocTracer.TraceLevel) {
std::lock_guard<std::mutex> Lock(MallocFreeStackMutex);
Printf("MALLOC[%zd] %p %zd\n", N, ptr, size);
if (TraceLevel >= 2 && EF)
EF->__sanitizer_print_stack_trace();
@ -88,6 +92,7 @@ ATTRIBUTE_NO_SANITIZE_MEMORY
void FreeHook(const volatile void *ptr) {
size_t N = AllocTracer.Frees++;
if (int TraceLevel = AllocTracer.TraceLevel) {
std::lock_guard<std::mutex> Lock(MallocFreeStackMutex);
Printf("FREE[%zd] %p\n", N, ptr);
if (TraceLevel >= 2 && EF)
EF->__sanitizer_print_stack_trace();

View File

@ -0,0 +1,22 @@
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
// Check that allocation tracing from different threads does not cause
// interleaving of stack traces.
#include <assert.h>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <thread>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
auto C = [&] {
volatile void *a = malloc(5639);
free((void *)a);
};
std::thread T[] = {std::thread(C), std::thread(C), std::thread(C),
std::thread(C), std::thread(C), std::thread(C)};
for (auto &X : T)
X.join();
return 0;
}

View File

@ -0,0 +1,36 @@
// FIXME: This test infinite loops on darwin because it crashes
// printing a stack trace repeatedly
UNSUPPORTED: darwin
RUN: %cpp_compiler %S/TraceMallocThreadedTest.cpp -o %t-TraceMallocThreadedTest
RUN: %t-TraceMallocThreadedTest -trace_malloc=2 -runs=1 2>&1 | FileCheck %s
CHECK: {{MALLOC\[[0-9]+] +0x[0-9]+ 5639}}
CHECK-NEXT: {{ +\#0 +}}
CHECK-NEXT: {{ +\#1 +}}
CHECK-NEXT: {{ +\#2 +}}
CHECK: {{MALLOC\[[0-9]+] +0x[0-9]+ 5639}}
CHECK-NEXT: {{ +\#0 +}}
CHECK-NEXT: {{ +\#1 +}}
CHECK-NEXT: {{ +\#2 +}}
CHECK: {{MALLOC\[[0-9]+] +0x[0-9]+ 5639}}
CHECK-NEXT: {{ +\#0 +}}
CHECK-NEXT: {{ +\#1 +}}
CHECK-NEXT: {{ +\#2 +}}
CHECK: {{MALLOC\[[0-9]+] +0x[0-9]+ 5639}}
CHECK-NEXT: {{ +\#0 +}}
CHECK-NEXT: {{ +\#1 +}}
CHECK-NEXT: {{ +\#2 +}}
CHECK: {{MALLOC\[[0-9]+] +0x[0-9]+ 5639}}
CHECK-NEXT: {{ +\#0 +}}
CHECK-NEXT: {{ +\#1 +}}
CHECK-NEXT: {{ +\#2 +}}
CHECK: {{MALLOC\[[0-9]+] +0x[0-9]+ 5639}}
CHECK-NEXT: {{ +\#0 +}}
CHECK-NEXT: {{ +\#1 +}}
CHECK-NEXT: {{ +\#2 +}}