[compiler-rt] r317071 - [fuzzer] Fix threaded stack printing
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 31 20:03:00 PDT 2017
Author: vitalybuka
Date: Tue Oct 31 20:02:59 2017
New Revision: 317071
URL: http://llvm.org/viewvc/llvm-project?rev=317071&view=rev
Log:
[fuzzer] Fix threaded stack printing
Reviewers: kcc
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D39397
Added:
compiler-rt/trunk/test/fuzzer/TraceMallocThreadedTest.cpp
compiler-rt/trunk/test/fuzzer/trace-malloc-threaded.test
Modified:
compiler-rt/trunk/lib/fuzzer/FuzzerLoop.cpp
Modified: compiler-rt/trunk/lib/fuzzer/FuzzerLoop.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/fuzzer/FuzzerLoop.cpp?rev=317071&r1=317070&r2=317071&view=diff
==============================================================================
--- compiler-rt/trunk/lib/fuzzer/FuzzerLoop.cpp (original)
+++ compiler-rt/trunk/lib/fuzzer/FuzzerLoop.cpp Tue Oct 31 20:02:59 2017
@@ -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();
Added: compiler-rt/trunk/test/fuzzer/TraceMallocThreadedTest.cpp
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/fuzzer/TraceMallocThreadedTest.cpp?rev=317071&view=auto
==============================================================================
--- compiler-rt/trunk/test/fuzzer/TraceMallocThreadedTest.cpp (added)
+++ compiler-rt/trunk/test/fuzzer/TraceMallocThreadedTest.cpp Tue Oct 31 20:02:59 2017
@@ -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;
+}
Added: compiler-rt/trunk/test/fuzzer/trace-malloc-threaded.test
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/fuzzer/trace-malloc-threaded.test?rev=317071&view=auto
==============================================================================
--- compiler-rt/trunk/test/fuzzer/trace-malloc-threaded.test (added)
+++ compiler-rt/trunk/test/fuzzer/trace-malloc-threaded.test Tue Oct 31 20:02:59 2017
@@ -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 +}}
More information about the llvm-commits
mailing list