Bumping manifests a=b2g-bump
[gecko.git] / mfbt / Poison.h
blob75e0f081cd9f3af05fd2bd8a64c33741fa2613e6
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>
20 MOZ_BEGIN_EXTERN_C
22 extern MFBT_DATA uintptr_t gMozillaPoisonValue;
24 /**
25 * @return the poison value.
27 inline uintptr_t mozPoisonValue()
29 return gMozillaPoisonValue;
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.
38 inline void mozWritePoison(void* aPtr, size_t aSize)
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;
50 /**
51 * Initialize the poison value.
52 * This should only be called once.
54 extern MFBT_API void mozPoisonValueInit();
56 /* Values annotated by CrashReporter */
57 extern MFBT_DATA uintptr_t gMozillaPoisonBase;
58 extern MFBT_DATA uintptr_t gMozillaPoisonSize;
60 MOZ_END_EXTERN_C
62 #endif /* mozilla_Poison_h */