1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * A poison value that can be used to fill a memory space with
9 * an address that leads to a safe crash when dereferenced.
12 #include "mozilla/Poison.h"
14 #include "mozilla/Assertions.h"
17 #elif !defined(__OS2__)
19 # include <sys/mman.h>
22 # define MAP_ANON MAP_ANONYMOUS
24 # error "Don't know how to get anonymous memory"
30 uintptr_t gMozillaPoisonValue
;
31 uintptr_t gMozillaPoisonBase
;
32 uintptr_t gMozillaPoisonSize
;
35 // Freed memory is filled with a poison value, which we arrange to
36 // form a pointer either to an always-unmapped region of the address
37 // space, or to a page that has been reserved and rendered
38 // inaccessible via OS primitives. See tests/TestPoisonArea.cpp for
39 // extensive discussion of the requirements for this page. The code
40 // from here to 'class FreeList' needs to be kept in sync with that
45 ReserveRegion(uintptr_t aRegion
, uintptr_t aSize
)
47 return VirtualAlloc((void*)aRegion
, aSize
, MEM_RESERVE
, PAGE_NOACCESS
);
51 ReleaseRegion(void* aRegion
, uintptr_t aSize
)
53 VirtualFree(aRegion
, aSize
, MEM_RELEASE
);
57 ProbeRegion(uintptr_t aRegion
, uintptr_t aSize
)
60 GetSystemInfo(&sinfo
);
61 if (aRegion
>= (uintptr_t)sinfo
.lpMaximumApplicationAddress
&&
62 aRegion
+ aSize
>= (uintptr_t)sinfo
.lpMaximumApplicationAddress
) {
70 GetDesiredRegionSize()
73 GetSystemInfo(&sinfo
);
74 return sinfo
.dwAllocationGranularity
;
77 #define RESERVE_FAILED 0
79 #elif defined(__OS2__)
81 ReserveRegion(uintptr_t aRegion
, uintptr_t aSize
)
83 // OS/2 doesn't support allocation at an arbitrary address,
84 // so return an address that is known to be invalid.
85 return (void*)0xFFFD0000;
89 ReleaseRegion(void* aRegion
, uintptr_t aSize
)
95 ProbeRegion(uintptr_t aRegion
, uintptr_t aSize
)
97 // There's no reliable way to probe an address in the system
98 // arena other than by touching it and seeing if a trap occurs.
103 GetDesiredRegionSize()
105 // Page size is fixed at 4k.
109 #define RESERVE_FAILED 0
113 #include "mozilla/TaggedAnonymousMemory.h"
116 ReserveRegion(uintptr_t aRegion
, uintptr_t aSize
)
118 return MozTaggedAnonymousMmap(reinterpret_cast<void*>(aRegion
), aSize
,
119 PROT_NONE
, MAP_PRIVATE
|MAP_ANON
, -1, 0,
124 ReleaseRegion(void* aRegion
, uintptr_t aSize
)
126 munmap(aRegion
, aSize
);
130 ProbeRegion(uintptr_t aRegion
, uintptr_t aSize
)
132 if (madvise(reinterpret_cast<void*>(aRegion
), aSize
, MADV_NORMAL
)) {
140 GetDesiredRegionSize()
142 return sysconf(_SC_PAGESIZE
);
145 #define RESERVE_FAILED MAP_FAILED
147 #endif // system dependencies
149 static_assert(sizeof(uintptr_t) == 4 || sizeof(uintptr_t) == 8, "");
150 static_assert(sizeof(uintptr_t) == sizeof(void*), "");
153 ReservePoisonArea(uintptr_t rgnsize
)
155 if (sizeof(uintptr_t) == 8) {
156 // Use the hardware-inaccessible region.
157 // We have to avoid 64-bit constants and shifts by 32 bits, since this
158 // code is compiled in 32-bit mode, although it is never executed there.
160 (((uintptr_t(0x7FFFFFFFu
) << 31) << 1 | uintptr_t(0xF0DEAFFFu
))
164 // First see if we can allocate the preferred poison address from the OS.
165 uintptr_t candidate
= (0xF0DEAFFF & ~(rgnsize
-1));
166 void* result
= ReserveRegion(candidate
, rgnsize
);
167 if (result
== (void*)candidate
) {
168 // success - inaccessible page allocated
172 // That didn't work, so see if the preferred address is within a range
173 // of permanently inacessible memory.
174 if (ProbeRegion(candidate
, rgnsize
)) {
175 // success - selected page cannot be usable memory
176 if (result
!= RESERVE_FAILED
) {
177 ReleaseRegion(result
, rgnsize
);
182 // The preferred address is already in use. Did the OS give us a
183 // consolation prize?
184 if (result
!= RESERVE_FAILED
) {
185 return uintptr_t(result
);
188 // It didn't, so try to allocate again, without any constraint on
190 result
= ReserveRegion(0, rgnsize
);
191 if (result
!= RESERVE_FAILED
) {
192 return uintptr_t(result
);
195 // no usable poison region identified
203 gMozillaPoisonSize
= GetDesiredRegionSize();
204 gMozillaPoisonBase
= ReservePoisonArea(gMozillaPoisonSize
);
206 if (gMozillaPoisonSize
== 0) { // can't happen
209 gMozillaPoisonValue
= gMozillaPoisonBase
+ gMozillaPoisonSize
/ 2 - 1;