no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / mfbt / Poison.h
blob5b1fae1fd1f2dbc1df763c60fc960687f0496e45
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/. */
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.
12 #ifndef mozilla_Poison_h
13 #define mozilla_Poison_h
15 #include "mozilla/Assertions.h"
16 #include "mozilla/Types.h"
18 #include <stdint.h>
19 #include <string.h>
21 MOZ_BEGIN_EXTERN_C
23 extern MFBT_DATA uintptr_t gMozillaPoisonValue;
25 /**
26 * @return the poison value.
28 inline uintptr_t mozPoisonValue() { return gMozillaPoisonValue; }
30 /**
31 * Overwrite the memory block of aSize bytes at aPtr with the poison value.
32 * Only a multiple of sizeof(uintptr_t) bytes are overwritten, the last
33 * few bytes (if any) are not overwritten.
35 inline void mozWritePoison(void* aPtr, size_t aSize) {
36 const uintptr_t POISON = mozPoisonValue();
37 char* p = (char*)aPtr;
38 char* limit = p + (aSize & ~(sizeof(uintptr_t) - 1));
39 MOZ_ASSERT(aSize >= sizeof(uintptr_t), "poisoning this object has no effect");
40 for (; p < limit; p += sizeof(uintptr_t)) {
41 memcpy(p, &POISON, sizeof(POISON));
45 /* Values annotated by CrashReporter */
46 extern MFBT_DATA uintptr_t gMozillaPoisonBase;
47 extern MFBT_DATA uintptr_t gMozillaPoisonSize;
49 MOZ_END_EXTERN_C
51 #if defined(__cplusplus)
53 namespace mozilla {
55 /**
56 * A version of CorruptionCanary that is suitable as a member of objects that
57 * are statically allocated.
59 class CorruptionCanaryForStatics {
60 public:
61 constexpr CorruptionCanaryForStatics() : mValue(kCanarySet) {}
63 // This is required to avoid static constructor bloat.
64 ~CorruptionCanaryForStatics() = default;
66 void Check() const {
67 if (mValue != kCanarySet) {
68 MOZ_CRASH("Canary check failed, check lifetime");
72 protected:
73 uintptr_t mValue;
75 private:
76 static const uintptr_t kCanarySet = 0x0f0b0f0b;
79 /**
80 * This class is designed to cause crashes when various kinds of memory
81 * corruption are observed. For instance, let's say we have a class C where we
82 * suspect out-of-bounds writes to some members. We can insert a member of type
83 * Poison near the members we suspect are being corrupted by out-of-bounds
84 * writes. Or perhaps we have a class K we suspect is subject to use-after-free
85 * violations, in which case it doesn't particularly matter where in the class
86 * we add the member of type Poison.
88 * In either case, we then insert calls to Check() throughout the code. Doing
89 * so enables us to narrow down the location where the corruption is occurring.
90 * A pleasant side-effect of these additional Check() calls is that crash
91 * signatures may become more regular, as crashes will ideally occur
92 * consolidated at the point of a Check(), rather than scattered about at
93 * various uses of the corrupted memory.
95 class CorruptionCanary : public CorruptionCanaryForStatics {
96 public:
97 constexpr CorruptionCanary() = default;
99 ~CorruptionCanary() {
100 Check();
101 mValue = mozPoisonValue();
105 } // namespace mozilla
107 #endif
109 #endif /* mozilla_Poison_h */