Line data Source code
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 106219 : inline uintptr_t mozPoisonValue()
28 : {
29 106219 : 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 6867 : inline void mozWritePoison(void* aPtr, size_t aSize)
39 : {
40 6867 : const uintptr_t POISON = mozPoisonValue();
41 6867 : char* p = (char*)aPtr;
42 6867 : char* limit = p + aSize;
43 6867 : MOZ_ASSERT((uintptr_t)aPtr % sizeof(uintptr_t) == 0, "bad alignment");
44 6867 : MOZ_ASSERT(aSize >= sizeof(uintptr_t), "poisoning this object has no effect");
45 228419 : for (; p < limit; p += sizeof(uintptr_t)) {
46 110776 : *((uintptr_t*)p) = POISON;
47 : }
48 6867 : }
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 : #if defined(__cplusplus)
63 :
64 : namespace mozilla {
65 :
66 : /**
67 : * This class is designed to cause crashes when various kinds of memory
68 : * corruption are observed. For instance, let's say we have a class C where we
69 : * suspect out-of-bounds writes to some members. We can insert a member of type
70 : * Poison near the members we suspect are being corrupted by out-of-bounds
71 : * writes. Or perhaps we have a class K we suspect is subject to use-after-free
72 : * violations, in which case it doesn't particularly matter where in the class
73 : * we add the member of type Poison.
74 : *
75 : * In either case, we then insert calls to Check() throughout the code. Doing
76 : * so enables us to narrow down the location where the corruption is occurring.
77 : * A pleasant side-effect of these additional Check() calls is that crash
78 : * signatures may become more regular, as crashes will ideally occur
79 : * consolidated at the point of a Check(), rather than scattered about at
80 : * various uses of the corrupted memory.
81 : */
82 : class CorruptionCanary {
83 : public:
84 1189 : CorruptionCanary() {
85 1189 : mValue = kCanarySet;
86 1189 : }
87 :
88 2302 : ~CorruptionCanary() {
89 1151 : Check();
90 1151 : mValue = mozPoisonValue();
91 1151 : }
92 :
93 7038 : void Check() const {
94 7038 : if (mValue != kCanarySet) {
95 0 : MOZ_CRASH("Canary check failed, check lifetime");
96 : }
97 7038 : }
98 :
99 : private:
100 : static const uintptr_t kCanarySet = 0x0f0b0f0b;
101 : uintptr_t mValue;
102 : };
103 :
104 : } // mozilla
105 :
106 : #endif
107 :
108 : #endif /* mozilla_Poison_h */
|