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 |
43 // Make sure mmap doesn't get #define'd away by <sys/mman.h> | 49 // Make sure mmap doesn't get #define'd away by <sys/mman.h> |
44 #undef mmap | 50 #undef mmap |
45 | 51 |
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 | |
58 extern "C" { | 52 extern "C" { |
59 void* mmap(void *start, size_t length,int prot, int flags, | 53 void* mmap(void *start, size_t length,int prot, int flags, |
60 int fd, off_t offset) __THROW | 54 int fd, off_t offset) __THROW |
61 ATTRIBUTE_SECTION(malloc_hook); | 55 ATTRIBUTE_SECTION(malloc_hook); |
62 int munmap(void* start, size_t length) __THROW | 56 int munmap(void* start, size_t length) __THROW |
63 ATTRIBUTE_SECTION(malloc_hook); | 57 ATTRIBUTE_SECTION(malloc_hook); |
64 void* sbrk(intptr_t increment) __THROW | 58 void* sbrk(intptr_t increment) __THROW |
65 ATTRIBUTE_SECTION(malloc_hook); | 59 ATTRIBUTE_SECTION(malloc_hook); |
66 } | 60 } |
67 | 61 |
68 static inline void* do_mmap(void *start, size_t length, | 62 static inline void* do_sbrk(intptr_t increment) { |
69 int prot, int flags, | 63 void* curbrk; |
70 int fd, off_t offset) __THROW { | |
71 return (void *)MALLOC_HOOK_SYSCALL(SYS_mmap, | |
72 start, length, prot, flags, fd, offset); | |
73 } | |
74 | 64 |
75 static inline void* do_sbrk(intptr_t increment) { | |
76 void* curbrk = 0; | |
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 | |
94 __asm__ __volatile__( | 65 __asm__ __volatile__( |
95 "movl .curbrk, %%eax;" | 66 "movl .curbrk, %%eax;" |
96 "movl %%eax, %0;" | 67 "movl %%eax, %0" |
97 : "=r" (curbrk) | 68 : "=r" (curbrk) |
98 :: "%eax"); | 69 :: "%eax"); |
99 #endif | |
100 | |
101 if (increment == 0) { | |
102 return curbrk; | |
103 } | |
104 | 70 |
105 char* prevbrk = static_cast<char*>(curbrk); | 71 char* prevbrk = static_cast<char*>(curbrk); |
106 void* newbrk = prevbrk + increment; | 72 void* newbrk = prevbrk + increment; |
107 | 73 |
108 if (brk(newbrk) == -1) { | 74 if (brk(newbrk) == -1) { |
| 75 assert(0); |
109 return reinterpret_cast<void*>(static_cast<intptr_t>(-1)); | 76 return reinterpret_cast<void*>(static_cast<intptr_t>(-1)); |
110 } | 77 } |
111 | 78 |
112 return prevbrk; | 79 return prevbrk; |
113 } | 80 } |
114 | 81 |
115 | 82 |
116 extern "C" void* mmap(void *start, size_t length, int prot, int flags, | 83 extern "C" void* mmap(void *start, size_t length, int prot, int flags, |
117 int fd, off_t offset) __THROW { | 84 int fd, off_t offset) __THROW { |
118 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); | 85 MallocHook::InvokePreMmapHook(start, length, prot, flags, fd, offset); |
119 void *result; | 86 void *result = do_mmap(start, length, prot, flags, fd, |
120 if (!MallocHook::InvokeMmapReplacement( | 87 static_cast<size_t>(offset)); // avoid sign extension |
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 } | |
125 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); | 88 MallocHook::InvokeMmapHook(result, start, length, prot, flags, fd, offset); |
126 return result; | 89 return result; |
127 } | 90 } |
128 | 91 |
129 extern "C" int munmap(void* start, size_t length) __THROW { | 92 extern "C" int munmap(void* start, size_t length) __THROW { |
130 MallocHook::InvokeMunmapHook(start, length); | 93 MallocHook::InvokeMunmapHook(start, length); |
131 int result; | 94 return syscall(SYS_munmap, start, length); |
132 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { | |
133 result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length); | |
134 } | |
135 | |
136 return result; | |
137 } | 95 } |
138 | 96 |
139 extern "C" void* sbrk(intptr_t increment) __THROW { | 97 extern "C" void* sbrk(intptr_t increment) __THROW { |
140 MallocHook::InvokePreSbrkHook(increment); | 98 MallocHook::InvokePreSbrkHook(increment); |
141 void *result = do_sbrk(increment); | 99 void *result = do_sbrk(increment); |
142 MallocHook::InvokeSbrkHook(result, increment); | 100 MallocHook::InvokeSbrkHook(result, increment); |
143 return result; | 101 return result; |
144 } | 102 } |
145 | 103 |
146 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, | 104 /*static*/void* MallocHook::UnhookedMMap(void *start, size_t length, int prot, |
147 int flags, int fd, off_t offset) { | 105 int flags, int fd, off_t offset) { |
148 void* result; | 106 return mmap(start, length, prot, flags, fd, offset); |
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; | |
155 } | 107 } |
156 | 108 |
157 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { | 109 /*static*/int MallocHook::UnhookedMUnmap(void *start, size_t length) { |
158 int result; | 110 return munmap(start, length); |
159 if (!MallocHook::InvokeMunmapReplacement(start, length, &result)) { | |
160 result = MALLOC_HOOK_SYSCALL(SYS_munmap, start, length); | |
161 } | |
162 return result; | |
163 } | 111 } |
164 | |
165 #undef MALLOC_HOOK_SYSCALL | |
OLD | NEW |