|
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/. */ |
|
6 |
|
7 /* |
|
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. |
|
10 */ |
|
11 |
|
12 #ifndef mozilla_Poison_h |
|
13 #define mozilla_Poison_h |
|
14 |
|
15 #include "mozilla/Assertions.h" |
|
16 #include "mozilla/Types.h" |
|
17 |
|
18 #include <stdint.h> |
|
19 |
|
20 MOZ_BEGIN_EXTERN_C |
|
21 |
|
22 extern MFBT_DATA uintptr_t gMozillaPoisonValue; |
|
23 |
|
24 /** |
|
25 * @return the poison value. |
|
26 */ |
|
27 inline uintptr_t mozPoisonValue() |
|
28 { |
|
29 return gMozillaPoisonValue; |
|
30 } |
|
31 |
|
32 /** |
|
33 * Overwrite the memory block of aSize bytes at aPtr with the poison value. |
|
34 * aPtr MUST be aligned at a sizeof(uintptr_t) boundary. |
|
35 * Only an even number of sizeof(uintptr_t) bytes are overwritten, the last |
|
36 * few bytes (if any) is not overwritten. |
|
37 */ |
|
38 inline void mozWritePoison(void* aPtr, size_t aSize) |
|
39 { |
|
40 const uintptr_t POISON = mozPoisonValue(); |
|
41 char* p = (char*)aPtr; |
|
42 char* limit = p + aSize; |
|
43 MOZ_ASSERT((uintptr_t)aPtr % sizeof(uintptr_t) == 0, "bad alignment"); |
|
44 MOZ_ASSERT(aSize >= sizeof(uintptr_t), "poisoning this object has no effect"); |
|
45 for (; p < limit; p += sizeof(uintptr_t)) { |
|
46 *((uintptr_t*)p) = POISON; |
|
47 } |
|
48 } |
|
49 |
|
50 /** |
|
51 * Initialize the poison value. |
|
52 * This should only be called once. |
|
53 */ |
|
54 extern MFBT_API void mozPoisonValueInit(); |
|
55 |
|
56 /* Values annotated by CrashReporter */ |
|
57 extern MFBT_DATA uintptr_t gMozillaPoisonBase; |
|
58 extern MFBT_DATA uintptr_t gMozillaPoisonSize; |
|
59 |
|
60 MOZ_END_EXTERN_C |
|
61 |
|
62 #endif /* mozilla_Poison_h */ |