[llvm-commits] [compiler-rt] r158142 - in /compiler-rt/trunk/lib/sanitizer_common: sanitizer_allocator.cc sanitizer_common.h sanitizer_symbolizer.cc

Alexey Samsonov samsonov at google.com
Thu Jun 7 01:52:56 PDT 2012


Author: samsonov
Date: Thu Jun  7 03:52:56 2012
New Revision: 158142

URL: http://llvm.org/viewvc/llvm-project?rev=158142&view=rev
Log:
[Sanitizer] Allocator for internal runtime purposes. Currently it calls libcmalloc, but we might have to make it more low-level in future

Added:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc

Added: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc?rev=158142&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc (added)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator.cc Thu Jun  7 03:52:56 2012
@@ -0,0 +1,40 @@
+//===-- sanitizer_allocator.cc --------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is shared between AddressSanitizer and ThreadSanitizer
+// run-time libraries.
+// This allocator that is used inside run-times.
+//===----------------------------------------------------------------------===//
+#include "sanitizer_common.h"
+
+// Provisional implementation.
+// FIXME: We should probably use more low-level allocator that would
+// mmap some pages and split them into chunks to fulfill requests.
+extern "C" void *__libc_malloc(__sanitizer::uptr size);
+extern "C" void __libc_free(void *ptr);
+
+namespace __sanitizer {
+
+static const u64 kInternalAllocBlockMagic = 0x7A6CB03ABCEBC042ull;
+
+void *InternalAlloc(uptr size) {
+  void *p = __libc_malloc(size + sizeof(u64));
+  ((u64*)p)[0] = kInternalAllocBlockMagic;
+  return (char*)p + sizeof(u64);
+}
+
+void InternalFree(void *addr) {
+  if (!addr) return;
+  addr = (char*)addr - sizeof(u64);
+  CHECK_EQ(((u64*)addr)[0], kInternalAllocBlockMagic);
+  ((u64*)addr)[0] = 0;
+  __libc_free(addr);
+}
+
+}  // namespace __sanitizer

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=158142&r1=158141&r2=158142&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Thu Jun  7 03:52:56 2012
@@ -34,6 +34,8 @@
 // Memory management
 void *MmapOrDie(uptr size, const char *mem_type);
 void UnmapOrDie(void *addr, uptr size);
+void *InternalAlloc(uptr size);
+void InternalFree(void *addr);
 
 void RawWrite(const char *buffer);
 void Printf(const char *format, ...);

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc?rev=158142&r1=158141&r2=158142&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.cc Thu Jun  7 03:52:56 2012
@@ -12,18 +12,15 @@
 // run-time libraries. See sanitizer.h for details.
 //===----------------------------------------------------------------------===//
 
-// WARNING: Avoid using library functions - see comments in symbolizer.h.
+#include "sanitizer_common.h"
 #include "sanitizer_symbolizer.h"
-// FIXME: replace library malloc/free with internal_malloc/internal_free
-// that would be provided by ASan/TSan run-time libraries.
-#include <stdlib.h>
 
 namespace __sanitizer {
 
 void AddressInfo::Clear() {
-  free(module);
-  free(function);
-  free(file);
+  InternalFree(module);
+  InternalFree(function);
+  InternalFree(file);
 }
 
 void AddressInfoList::Clear() {
@@ -31,13 +28,14 @@
   while (cur) {
     cur->info.Clear();
     AddressInfoList *nxt = cur->next;
-    free(cur);
+    InternalFree(cur);
     cur = nxt;
   }
 }
 
 AddressInfoList* SymbolizeCode(uptr address) {
-  AddressInfoList *list = (AddressInfoList*)malloc(sizeof(AddressInfoList));
+  AddressInfoList *list = (AddressInfoList*)InternalAlloc(
+      sizeof(AddressInfoList));
   list->next = 0;
   list->info.address = address;
   list->info.module = 0;





More information about the llvm-commits mailing list