michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A poison value that can be used to fill a memory space with michael@0: * an address that leads to a safe crash when dereferenced. michael@0: */ michael@0: michael@0: #ifndef mozilla_Poison_h michael@0: #define mozilla_Poison_h michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Types.h" michael@0: michael@0: #include michael@0: michael@0: MOZ_BEGIN_EXTERN_C michael@0: michael@0: extern MFBT_DATA uintptr_t gMozillaPoisonValue; michael@0: michael@0: /** michael@0: * @return the poison value. michael@0: */ michael@0: inline uintptr_t mozPoisonValue() michael@0: { michael@0: return gMozillaPoisonValue; michael@0: } michael@0: michael@0: /** michael@0: * Overwrite the memory block of aSize bytes at aPtr with the poison value. michael@0: * aPtr MUST be aligned at a sizeof(uintptr_t) boundary. michael@0: * Only an even number of sizeof(uintptr_t) bytes are overwritten, the last michael@0: * few bytes (if any) is not overwritten. michael@0: */ michael@0: inline void mozWritePoison(void* aPtr, size_t aSize) michael@0: { michael@0: const uintptr_t POISON = mozPoisonValue(); michael@0: char* p = (char*)aPtr; michael@0: char* limit = p + aSize; michael@0: MOZ_ASSERT((uintptr_t)aPtr % sizeof(uintptr_t) == 0, "bad alignment"); michael@0: MOZ_ASSERT(aSize >= sizeof(uintptr_t), "poisoning this object has no effect"); michael@0: for (; p < limit; p += sizeof(uintptr_t)) { michael@0: *((uintptr_t*)p) = POISON; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Initialize the poison value. michael@0: * This should only be called once. michael@0: */ michael@0: extern MFBT_API void mozPoisonValueInit(); michael@0: michael@0: /* Values annotated by CrashReporter */ michael@0: extern MFBT_DATA uintptr_t gMozillaPoisonBase; michael@0: extern MFBT_DATA uintptr_t gMozillaPoisonSize; michael@0: michael@0: MOZ_END_EXTERN_C michael@0: michael@0: #endif /* mozilla_Poison_h */