CheckForCaller.h (1261B)
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 https://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_freestanding_CheckForCaller_h 8 #define mozilla_freestanding_CheckForCaller_h 9 10 namespace mozilla { 11 12 #if defined(_MSC_VER) 13 # include <intrin.h> 14 # pragma intrinsic(_ReturnAddress) 15 # define RETURN_ADDRESS() _ReturnAddress() 16 #elif defined(__GNUC__) || defined(__clang__) 17 # define RETURN_ADDRESS() \ 18 __builtin_extract_return_addr(__builtin_return_address(0)) 19 #endif 20 21 template <int N> 22 bool CheckForAddress(void* aReturnAddress, const wchar_t (&aName)[N]) { 23 HMODULE callingModule; 24 if (!::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | 25 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, 26 reinterpret_cast<LPCWSTR>(aReturnAddress), 27 &callingModule)) { 28 return false; 29 } 30 31 return callingModule && callingModule == ::GetModuleHandleW(aName); 32 } 33 34 } // namespace mozilla 35 36 #endif // mozilla_freestanding_CheckForCaller_h