[compiler-rt] r224821 - [sanitizer] Fix off-by-8x in direct coverage.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Wed Dec 24 05:57:11 PST 2014


Author: eugenis
Date: Wed Dec 24 07:57:11 2014
New Revision: 224821

URL: http://llvm.org/viewvc/llvm-project?rev=224821&view=rev
Log:
[sanitizer] Fix off-by-8x in direct coverage.

File mapping offset was calculated by offsetting (uptr *) instead of (char *).

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
    compiler-rt/trunk/test/asan/TestCases/Linux/coverage-direct-large.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc?rev=224821&r1=224820&r2=224821&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc Wed Dec 24 07:57:11 2014
@@ -215,6 +215,7 @@ void CoverageData::Extend(uptr npcs) {
   if (size > pc_array_mapped_size) {
     uptr new_mapped_size = pc_array_mapped_size;
     while (size > new_mapped_size) new_mapped_size += kPcArrayMmapSize;
+    CHECK_LE(new_mapped_size, sizeof(uptr) * kPcArrayMaxSize);
 
     // Extend the file and map the new space at the end of pc_array.
     uptr res = internal_ftruncate(pc_fd, new_mapped_size);
@@ -223,10 +224,12 @@ void CoverageData::Extend(uptr npcs) {
       Printf("failed to extend raw coverage file: %d\n", err);
       Die();
     }
-    void *p = MapWritableFileToMemory(pc_array + pc_array_mapped_size,
+
+    uptr next_map_base = ((uptr)pc_array) + pc_array_mapped_size;
+    void *p = MapWritableFileToMemory((void *)next_map_base,
                                       new_mapped_size - pc_array_mapped_size,
                                       pc_fd, pc_array_mapped_size);
-    CHECK_EQ(p, pc_array + pc_array_mapped_size);
+    CHECK_EQ((uptr)p, next_map_base);
     pc_array_mapped_size = new_mapped_size;
   }
 

Modified: compiler-rt/trunk/test/asan/TestCases/Linux/coverage-direct-large.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Linux/coverage-direct-large.cc?rev=224821&r1=224820&r2=224821&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Linux/coverage-direct-large.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/Linux/coverage-direct-large.cc Wed Dec 24 07:57:11 2014
@@ -1,7 +1,9 @@
 // Test for direct coverage writing with lots of data.
 // Current implementation maps output file in chunks of 64K. This test overflows
 // 1 chunk.
-// RUN: %clangxx_asan -fsanitize-coverage=1 -O0 %s -o %t
+
+// RUN: %clangxx_asan -fsanitize-coverage=1 -O0 -DSHARED %s -shared -o %T/libcoverage_direct_large_test_1.so -fPIC
+// RUN: %clangxx_asan -fsanitize-coverage=1 -O0 -DSO_DIR=\"%T\" %s %libdl -o %t
 
 // RUN: rm -rf %T/coverage-direct-large
 
@@ -34,12 +36,30 @@
   F3(Q, x##0) F3(Q, x##1) F3(Q, x##2) F3(Q, x##3) F3(Q, x##4) F3(Q, x##5) \
       F3(Q, x##6) F3(Q, x##7) F3(Q, x##8) F3(Q, x##9)
 
-#define DECL(x) __attribute__((noinline)) void x() {}
+#define DECL(x) __attribute__((noinline)) static void x() {}
 #define CALL(x) x();
 
 F4(DECL, f)
 
+#ifdef SHARED
+extern "C" void so_entry() {
+  F4(CALL, f)
+}  
+#else
+
+#include <assert.h>
+#include <dlfcn.h>
 int main(void) {
   F4(CALL, f)
+
+  void *handle1 =
+      dlopen(SO_DIR "/libcoverage_direct_large_test_1.so", RTLD_LAZY);
+  assert(handle1);
+  void (*so_entry)() = (void (*)())dlsym(handle1, "so_entry");
+  assert(so_entry);
+  so_entry();
+
   return 0;
 }
+
+#endif // SHARED





More information about the llvm-commits mailing list