forked from OSchip/llvm-project
				
			[Sanitizers] Add more standard compliant posix_memalign implementation for LSan.
Summary: Add more standard compliant posix_memalign implementation for LSan and use corresponding sanitizer's posix_memalign implenetations in allocation wrappers on Mac. Reviewers: eugenis, fjricci Subscribers: kubamracek, delcypher, #sanitizers, llvm-commits Differential Revision: https://reviews.llvm.org/D44335 llvm-svn: 327338
This commit is contained in:
		
							parent
							
								
									c2e54761c9
								
							
						
					
					
						commit
						79a7c4fe73
					
				| 
						 | 
					@ -38,6 +38,9 @@ using namespace __asan;
 | 
				
			||||||
#define COMMON_MALLOC_CALLOC(count, size) \
 | 
					#define COMMON_MALLOC_CALLOC(count, size) \
 | 
				
			||||||
  GET_STACK_TRACE_MALLOC; \
 | 
					  GET_STACK_TRACE_MALLOC; \
 | 
				
			||||||
  void *p = asan_calloc(count, size, &stack);
 | 
					  void *p = asan_calloc(count, size, &stack);
 | 
				
			||||||
 | 
					#define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
 | 
				
			||||||
 | 
					  GET_STACK_TRACE_MALLOC; \
 | 
				
			||||||
 | 
					  int res = asan_posix_memalign(memptr, alignment, size, &stack);
 | 
				
			||||||
#define COMMON_MALLOC_VALLOC(size) \
 | 
					#define COMMON_MALLOC_VALLOC(size) \
 | 
				
			||||||
  GET_STACK_TRACE_MALLOC; \
 | 
					  GET_STACK_TRACE_MALLOC; \
 | 
				
			||||||
  void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
 | 
					  void *p = asan_memalign(GetPageSizeCached(), size, &stack, FROM_MALLOC);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -128,6 +128,21 @@ uptr GetMallocUsableSize(const void *p) {
 | 
				
			||||||
  return m->requested_size;
 | 
					  return m->requested_size;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
 | 
				
			||||||
 | 
					                        const StackTrace &stack) {
 | 
				
			||||||
 | 
					  if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
 | 
				
			||||||
 | 
					    ReturnNullOrDieOnFailure::OnBadRequest();
 | 
				
			||||||
 | 
					    return errno_EINVAL;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  void *ptr = Allocate(stack, size, alignment, kAlwaysClearMemory);
 | 
				
			||||||
 | 
					  if (UNLIKELY(!ptr))
 | 
				
			||||||
 | 
					    // OOM error is already taken care of by Allocate.
 | 
				
			||||||
 | 
					    return errno_ENOMEM;
 | 
				
			||||||
 | 
					  CHECK(IsAligned((uptr)ptr, alignment));
 | 
				
			||||||
 | 
					  *memptr = ptr;
 | 
				
			||||||
 | 
					  return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack) {
 | 
					void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack) {
 | 
				
			||||||
  if (UNLIKELY(!IsPowerOfTwo(alignment))) {
 | 
					  if (UNLIKELY(!IsPowerOfTwo(alignment))) {
 | 
				
			||||||
    errno = errno_EINVAL;
 | 
					    errno = errno_EINVAL;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -90,6 +90,8 @@ typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
AllocatorCache *GetAllocatorCache();
 | 
					AllocatorCache *GetAllocatorCache();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
 | 
				
			||||||
 | 
					                        const StackTrace &stack);
 | 
				
			||||||
void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
 | 
					void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
 | 
				
			||||||
void *lsan_malloc(uptr size, const StackTrace &stack);
 | 
					void *lsan_malloc(uptr size, const StackTrace &stack);
 | 
				
			||||||
void lsan_free(void *p);
 | 
					void lsan_free(void *p);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -86,9 +86,7 @@ INTERCEPTOR(void*, realloc, void *q, uptr size) {
 | 
				
			||||||
INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
 | 
					INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
 | 
				
			||||||
  ENSURE_LSAN_INITED;
 | 
					  ENSURE_LSAN_INITED;
 | 
				
			||||||
  GET_STACK_TRACE_MALLOC;
 | 
					  GET_STACK_TRACE_MALLOC;
 | 
				
			||||||
  *memptr = lsan_memalign(alignment, size, stack);
 | 
					  return lsan_posix_memalign(memptr, alignment, size, stack);
 | 
				
			||||||
  // FIXME: Return ENOMEM if user requested more than max alloc size.
 | 
					 | 
				
			||||||
  return 0;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
INTERCEPTOR(void*, valloc, uptr size) {
 | 
					INTERCEPTOR(void*, valloc, uptr size) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,6 +37,9 @@ using namespace __lsan;
 | 
				
			||||||
#define COMMON_MALLOC_CALLOC(count, size) \
 | 
					#define COMMON_MALLOC_CALLOC(count, size) \
 | 
				
			||||||
  GET_STACK_TRACE_MALLOC; \
 | 
					  GET_STACK_TRACE_MALLOC; \
 | 
				
			||||||
  void *p = lsan_calloc(count, size, stack)
 | 
					  void *p = lsan_calloc(count, size, stack)
 | 
				
			||||||
 | 
					#define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size) \
 | 
				
			||||||
 | 
					  GET_STACK_TRACE_MALLOC; \
 | 
				
			||||||
 | 
					  int res = lsan_posix_memalign(memptr, alignment, size, stack)
 | 
				
			||||||
#define COMMON_MALLOC_VALLOC(size) \
 | 
					#define COMMON_MALLOC_VALLOC(size) \
 | 
				
			||||||
  GET_STACK_TRACE_MALLOC; \
 | 
					  GET_STACK_TRACE_MALLOC; \
 | 
				
			||||||
  void *p = lsan_valloc(size, stack)
 | 
					  void *p = lsan_valloc(size, stack)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -170,12 +170,8 @@ INTERCEPTOR(size_t, malloc_good_size, size_t size) {
 | 
				
			||||||
INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
 | 
					INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
 | 
				
			||||||
  COMMON_MALLOC_ENTER();
 | 
					  COMMON_MALLOC_ENTER();
 | 
				
			||||||
  CHECK(memptr);
 | 
					  CHECK(memptr);
 | 
				
			||||||
  COMMON_MALLOC_MEMALIGN(alignment, size);
 | 
					  COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size);
 | 
				
			||||||
  if (p) {
 | 
					  return res;
 | 
				
			||||||
    *memptr = p;
 | 
					 | 
				
			||||||
    return 0;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  return -1;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace {
 | 
					namespace {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,6 +15,7 @@
 | 
				
			||||||
#include "sanitizer_common/sanitizer_platform.h"
 | 
					#include "sanitizer_common/sanitizer_platform.h"
 | 
				
			||||||
#if SANITIZER_MAC
 | 
					#if SANITIZER_MAC
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "sanitizer_common/sanitizer_errno.h"
 | 
				
			||||||
#include "tsan_interceptors.h"
 | 
					#include "tsan_interceptors.h"
 | 
				
			||||||
#include "tsan_stack_trace.h"
 | 
					#include "tsan_stack_trace.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,6 +40,15 @@ using namespace __tsan;
 | 
				
			||||||
  if (cur_thread()->in_symbolizer) return InternalCalloc(count, size); \
 | 
					  if (cur_thread()->in_symbolizer) return InternalCalloc(count, size); \
 | 
				
			||||||
  SCOPED_INTERCEPTOR_RAW(calloc, size, count);                         \
 | 
					  SCOPED_INTERCEPTOR_RAW(calloc, size, count);                         \
 | 
				
			||||||
  void *p = user_calloc(thr, pc, size, count)
 | 
					  void *p = user_calloc(thr, pc, size, count)
 | 
				
			||||||
 | 
					#define COMMON_MALLOC_POSIX_MEMALIGN(memptr, alignment, size)      \
 | 
				
			||||||
 | 
					  if (cur_thread()->in_symbolizer) {                               \
 | 
				
			||||||
 | 
					    void *p = InternalAlloc(size, nullptr, alignment);             \
 | 
				
			||||||
 | 
					    if (!p) return errno_ENOMEM;                                   \
 | 
				
			||||||
 | 
					    *memptr = p;                                                   \
 | 
				
			||||||
 | 
					    return 0;                                                      \
 | 
				
			||||||
 | 
					  }                                                                \
 | 
				
			||||||
 | 
					  SCOPED_INTERCEPTOR_RAW(posix_memalign, memptr, alignment, size); \
 | 
				
			||||||
 | 
					  int res = user_posix_memalign(thr, pc, memptr, alignment, size);
 | 
				
			||||||
#define COMMON_MALLOC_VALLOC(size)                            \
 | 
					#define COMMON_MALLOC_VALLOC(size)                            \
 | 
				
			||||||
  if (cur_thread()->in_symbolizer)                            \
 | 
					  if (cur_thread()->in_symbolizer)                            \
 | 
				
			||||||
    return InternalAlloc(size, nullptr, GetPageSizeCached()); \
 | 
					    return InternalAlloc(size, nullptr, GetPageSizeCached()); \
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue