blob: df94513a6a58981770e8cc4e3ae1f7e1d0136045 [file] [log] [blame]
Alexey Samsonov485d3dc2012-06-04 13:50:10 +00001//===-- asan_malloc_linux.cc ----------------------------------------------===//
Kostya Serebryany019b76f2011-11-30 01:07:02 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Linux-specific malloc interception.
13// We simply define functions like malloc, free, realloc, etc.
14// They will replace the corresponding libc functions automagically.
15//===----------------------------------------------------------------------===//
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +000016#ifdef __linux__
Kostya Serebryany019b76f2011-11-30 01:07:02 +000017
18#include "asan_allocator.h"
19#include "asan_interceptors.h"
20#include "asan_internal.h"
21#include "asan_stack.h"
Kostya Serebryany9da3dd02012-12-20 11:54:21 +000022#include "asan_thread_registry.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000023
Evgeniy Stepanovbe013982012-08-27 11:15:55 +000024#if ASAN_ANDROID
Alexey Samsonova81d2682012-09-12 09:42:23 +000025DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, uptr size)
26DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
27DECLARE_REAL_AND_INTERCEPTOR(void*, calloc, uptr nmemb, uptr size)
28DECLARE_REAL_AND_INTERCEPTOR(void*, realloc, void *ptr, uptr size)
29DECLARE_REAL_AND_INTERCEPTOR(void*, memalign, uptr boundary, uptr size)
Alexey Samsonov941a6ec2012-08-06 15:13:22 +000030
Kostya Serebryany019b76f2011-11-30 01:07:02 +000031struct MallocDebug {
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000032 void* (*malloc)(uptr bytes);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000033 void (*free)(void* mem);
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000034 void* (*calloc)(uptr n_elements, uptr elem_size);
35 void* (*realloc)(void* oldMem, uptr bytes);
36 void* (*memalign)(uptr alignment, uptr bytes);
Kostya Serebryany019b76f2011-11-30 01:07:02 +000037};
38
Alexey Samsonov23e3b902012-02-03 08:37:19 +000039const MallocDebug asan_malloc_dispatch ALIGNED(32) = {
Alexey Samsonov941a6ec2012-08-06 15:13:22 +000040 WRAP(malloc), WRAP(free), WRAP(calloc), WRAP(realloc), WRAP(memalign)
Kostya Serebryany019b76f2011-11-30 01:07:02 +000041};
42
43extern "C" const MallocDebug* __libc_malloc_dispatch;
44
45namespace __asan {
46void ReplaceSystemMalloc() {
47 __libc_malloc_dispatch = &asan_malloc_dispatch;
48}
49} // namespace __asan
50
51#else // ANDROID
52
53namespace __asan {
54void ReplaceSystemMalloc() {
55}
56} // namespace __asan
57#endif // ANDROID
58
59// ---------------------- Replacement functions ---------------- {{{1
60using namespace __asan; // NOLINT
61
Alexey Samsonov15965f92012-02-02 10:39:40 +000062INTERCEPTOR(void, free, void *ptr) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000063 GET_STACK_TRACE_FREE;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000064 asan_free(ptr, &stack);
65}
66
Alexey Samsonov15965f92012-02-02 10:39:40 +000067INTERCEPTOR(void, cfree, void *ptr) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000068 GET_STACK_TRACE_FREE;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000069 asan_free(ptr, &stack);
70}
71
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000072INTERCEPTOR(void*, malloc, uptr size) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000073 GET_STACK_TRACE_MALLOC;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000074 return asan_malloc(size, &stack);
75}
76
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000077INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +000078 if (!asan_inited) {
Alexey Samsonove7254782012-02-08 13:45:31 +000079 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000080 const uptr kCallocPoolSize = 1024;
Kostya Serebryany8d032042012-05-31 14:35:53 +000081 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000082 static uptr allocated;
83 uptr size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000084 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
85 allocated += size_in_words;
86 CHECK(allocated < kCallocPoolSize);
87 return mem;
88 }
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000089 GET_STACK_TRACE_MALLOC;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000090 return asan_calloc(nmemb, size, &stack);
91}
92
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000093INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000094 GET_STACK_TRACE_MALLOC;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000095 return asan_realloc(ptr, size, &stack);
96}
97
Kostya Serebryany0e0832b2012-06-25 09:58:29 +000098INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000099 GET_STACK_TRACE_MALLOC;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000100 return asan_memalign(boundary, size, &stack);
101}
102
Kostya Serebryany0e0832b2012-06-25 09:58:29 +0000103INTERCEPTOR(void*, __libc_memalign, uptr align, uptr s)
Alexey Samsonov23e3b902012-02-03 08:37:19 +0000104 ALIAS("memalign");
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000105
Kostya Serebryany0e0832b2012-06-25 09:58:29 +0000106INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000107 GET_STACK_TRACE_MALLOC;
Alexey Samsonov209c5142012-01-17 06:39:10 +0000108 return asan_malloc_usable_size(ptr, &stack);
109}
110
Kostya Serebryany0e0832b2012-06-25 09:58:29 +0000111// We avoid including malloc.h for portability reasons.
112// man mallinfo says the fields are "long", but the implementation uses int.
113// It doesn't matter much -- we just need to make sure that the libc's mallinfo
114// is not called.
115struct fake_mallinfo {
116 int x[10];
117};
118
119INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
120 struct fake_mallinfo res;
Alexey Samsonove7254782012-02-08 13:45:31 +0000121 REAL(memset)(&res, 0, sizeof(res));
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000122 return res;
123}
124
Alexey Samsonov15965f92012-02-02 10:39:40 +0000125INTERCEPTOR(int, mallopt, int cmd, int value) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000126 return -1;
127}
128
Kostya Serebryany0e0832b2012-06-25 09:58:29 +0000129INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000130 GET_STACK_TRACE_MALLOC;
Evgeniy Stepanov823085a2012-03-21 11:32:46 +0000131 // Printf("posix_memalign: %zx %zu\n", alignment, size);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000132 return asan_posix_memalign(memptr, alignment, size, &stack);
133}
134
Kostya Serebryany0e0832b2012-06-25 09:58:29 +0000135INTERCEPTOR(void*, valloc, uptr size) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000136 GET_STACK_TRACE_MALLOC;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000137 return asan_valloc(size, &stack);
138}
139
Kostya Serebryany0e0832b2012-06-25 09:58:29 +0000140INTERCEPTOR(void*, pvalloc, uptr size) {
Kostya Serebryanybaf583c2012-12-13 09:34:23 +0000141 GET_STACK_TRACE_MALLOC;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000142 return asan_pvalloc(size, &stack);
143}
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000144
Kostya Serebryany9da3dd02012-12-20 11:54:21 +0000145INTERCEPTOR(void, malloc_stats, void) {
146 Printf("AddressSanitizer malloc_stats()\n");
147 Printf(" total mmapped: %zdM\n",
148 asanThreadRegistry().GetHeapSize() >> 20);
149}
150
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000151#endif // __linux__