OLD | NEW |
1 // Copyright (c) 2011, Google Inc. | 1 // Copyright (c) 2011, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 #ifndef __FreeBSD__ | 34 #ifndef __FreeBSD__ |
35 # error Should only be including malloc_hook_mmap_freebsd.h on FreeBSD systems. | 35 # error Should only be including malloc_hook_mmap_freebsd.h on FreeBSD systems. |
36 #endif | 36 #endif |
37 | 37 |
38 #include <unistd.h> | 38 #include <unistd.h> |
39 #include <sys/syscall.h> | 39 #include <sys/syscall.h> |
40 #include <sys/mman.h> | 40 #include <sys/mman.h> |
41 #include <errno.h> | 41 #include <errno.h> |
42 | 42 |
43 static inline void* do_mmap(void *start, size_t length, | |
44 int prot, int flags, | |
45 int fd, off_t offset) __THROW { | |
46 return (void *)syscall(SYS_mmap, start, length, prot, flags, fd, offset); | |
47 } | |
48 | |
49 // Make sure mmap doesn't get #define'd away by <sys/mman.h> | 43 // Make sure mmap doesn't get #define'd away by <sys/mman.h> |
50 #undef mmap | 44 #undef mmap |
51 | 45 |
| 46 // According to the FreeBSD documentation, use syscall if you do not |
| 47 // need 64-bit alignment otherwise use __syscall. Indeed, syscall |
| 48 // doesn't work correctly in most situations on 64-bit. It's return |
| 49 // type is 'int' so for things like SYS_mmap, it actually truncates |
| 50 // the returned address to 32-bits. |
| 51 #if defined(__amd64__) || defined(__x86_64__) |
| 52 # define MALLOC_HOOK_SYSCALL __syscall |
| 53 #else |
| 54 # define MALLOC_HOOK_SYSCALL syscall |
| 55 #endif |
| 56 |
| 57 |
52 extern "C" { | 58 extern "C" { |
53 void* mmap(void *start, size_t length,int prot, int flags, | 59 void* mmap(void *start, size_t length,int prot, int flags, |
54 int fd, off_t offset) __THROW | 60 int fd, off_t offset) __THROW |
55 ATTRIBUTE_SECTION(malloc_hook); | 61 ATTRIBUTE_SECTION(malloc_hook); |
56 int munmap(void* start, size_t length) __THROW | 62 int munmap(void* start, size_t length) __THROW |
57 ATTRIBUTE_SECTION(malloc_hook); | 63 ATTRIBUTE_SECTION(malloc_hook); |
58 void* sbrk(intptr_t increment) __THROW | 64 void* sbrk(intptr_t increment) __THROW |
59 ATTRIBUTE_SECTION(malloc_hook); | 65 ATTRIBUTE_SECTION(malloc_hook); |
60 } | 66 } |
61 | 67 |
| 68 static inline void* do_mmap(void *start, size_t length, |
| 69 int prot, int flags, |
| 70 int fd, off_t offset) __THROW { |
| 71 return (void *)MALLOC_HOOK_SYSCALL(SYS_mmap, |
| 72 start, length, prot, flags, fd, offset); |
| 73 } |
| 74 |
62 static inline void* do_sbrk(intptr_t increment) { | 75 static inline void* do_sbrk(intptr_t increment) { |
63 void* curbrk; | 76 void* curbrk = 0; |
64 | 77 |
| 78 #if defined(__x86_64__) || defined(__amd64__) |
| 79 # ifdef PIC |
| 80 __asm__ __volatile__( |
| 81 "movq .curbrk@GOTPCREL(%%rip), %%rdx;" |
| 82 "movq (%%rdx), %%rax;" |
| 83 "movq %%rax, %0;" |
| 84 : "=r" (curbrk) |
| 85 :: "%rdx", "%rax"); |
| 86 # else |
| 87 __asm__ __volatile__( |
| 88 "movq .curbrk(%%rip), %%rax;" |
| 89 "movq %%rax, %0;" |
| 90 : "=r" (curbrk) |
| 91 :: "%rax"); |
| 92 # endif |
| 93 #else |
65 __asm__ __volatile__( | 94 __asm__ __volatile__( |
66 "movl .curbrk, %%eax;" | 95 "movl .curbrk, %%eax;" |
67 "movl %%eax, %0" | 96 "movl %%eax, %0;" |
68 : "=r" (curbrk) | 97 : "=r" (curbrk) |
69 :: "%eax"); | 98 :: "%eax"); |
| 99 #endif |
| 100 |
| 101 if (increment == 0) { |
| 102 return curbrk; |
| 103 } |
70 | 104 |
71 char* prevbrk = static_cast<char*>(curbrk); | 105 char* prevbrk = static_cast<char*>(curbrk); |
72 void* newbrk = prevbrk + increment; | 106 void* newbrk = prevbrk + increment; |
73 | 107 |
74 if (brk(newbrk) == -1) { | 108 if (brk(newbrk) == -1) { |
75 assert(0); | |
76 return reinterpret_cast<void*>(static_cast<intptr_t>(-1)); | 109 return reinterpret_cast<void*>(static_cast<intptr_t>(-1)); |
77 } | 110 } |
78 | 111 |
79 return prevbrk; | 112 return prevbrk; |
80 } | 113 } |
81 | 114 |
82 | 115 |
83 extern "C" void* mmap(void *start, size_t length, int prot, int flags, | 116 extern "C" void* mmap(void *start, size_t length, int prot, int flags, |
84 int fd, off_t offset) __THROW { | 117 int fd, off_t offset) __THROW { |
85 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); | 118 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
86 void *result = do_mmap(start, length, prot, flags, fd, | 119 void *result; |
87 static_cast<size_t>(offset)); // avoid sign extension | 120 if (!MallocHook::InvokeMmapReplacement( |
| 121 start, length, prot, flags, fd, offset, &result)) { |
| 122 result = do_mmap(start, length, prot, flags, fd, |
| 123 static_cast<size_t>(offset)); // avoid sign extension |
| 124 } |
88 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); | 125 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
89 return result; | 126 return result; |
90 } | 127 } |
91 | 128 |
92 extern "C" int munmap(void* start, size_t length) __THROW { | 129 extern "C" int munmap(void* start, size_t length) __THROW { |
93 MallocHook::InvokeMunmapHook(start, length); | 130 MallocHook::InvokeMunmapHook(start, length); |
94 return syscall(SYS_munmap, start, length); | 131 int result; |
| 132 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
| 133 result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length); |
| 134 } |
| 135 |
| 136 return result; |
95 } | 137 } |
96 | 138 |
97 extern "C" void* sbrk(intptr_t increment) __THROW { | 139 extern "C" void* sbrk(intptr_t increment) __THROW { |
98 MallocHook::InvokePreSbrkHook(increment); | 140 MallocHook::InvokePreSbrkHook(increment); |
99 void *result = do_sbrk(increment); | 141 void *result = do_sbrk(increment); |
100 MallocHook::InvokeSbrkHook(result, increment); | 142 MallocHook::InvokeSbrkHook(result, increment); |
101 return result; | 143 return result; |
102 } | 144 } |
103 | 145 |
104 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, | 146 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
105 int flags, int fd, off_t offset) { | 147 int flags, int fd, off_t offset) { |
106 return mmap(start, length, prot, flags, fd, offset); | 148 void* result; |
| 149 if (!MallocHook::InvokeMmapReplacement( |
| 150 » start, length, prot, flags, fd, offset, &result)) { |
| 151 result = do_mmap(start, length, prot, flags, fd, offset); |
| 152 } |
| 153 |
| 154 return result; |
107 } | 155 } |
108 | 156 |
109 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { | 157 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
110 return munmap(start, length); | 158 int result; |
| 159 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { |
| 160 result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length); |
| 161 } |
| 162 return result; |
111 } | 163 } |
| 164 |
| 165 #undef MALLOC_HOOK_SYSCALL |
OLD | NEW |