1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MmapFaultHandler_h_
7 #define MmapFaultHandler_h_
12 # ifdef HAVE_SEH_EXCEPTIONS
13 # define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) __try {
14 # define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) __try {
15 # define MMAP_FAULT_HANDLER_CATCH(retval) \
17 __except (GetExceptionCode() == EXCEPTION_IN_PAGE_ERROR \
18 ? EXCEPTION_EXECUTE_HANDLER \
19 : EXCEPTION_CONTINUE_SEARCH) { \
20 NS_WARNING("unexpected EXCEPTION_IN_PAGE_ERROR"); \
24 # define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) {
25 # define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) {
26 # define MMAP_FAULT_HANDLER_CATCH(retval) }
29 #elif defined(XP_DARWIN)
32 # define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) {
33 # define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) {
34 # define MMAP_FAULT_HANDLER_CATCH(retval) }
36 #elif defined(__wasi__)
38 # define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) {
39 # define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) {
40 # define MMAP_FAULT_HANDLER_CATCH(retval) }
45 # include "mozilla/Attributes.h"
46 # include "mozilla/Types.h"
50 class MOZ_RAII MmapAccessScope
{
52 MFBT_API
MmapAccessScope(void* aBuf
, uint32_t aBufLen
,
53 const char* aFilename
= nullptr);
54 MFBT_API
~MmapAccessScope();
56 MmapAccessScope(const MmapAccessScope
&) = delete;
57 MmapAccessScope
& operator=(const MmapAccessScope
&) = delete;
59 void SetThreadLocalScope();
60 bool IsInsideBuffer(void* aPtr
);
61 void CrashWithInfo(void* aPtr
);
63 // sigsetjmp cannot be called from a method that returns before calling
64 // siglongjmp, so the macro must call sigsetjmp directly and mJmpBuf must be
70 const char* mFilename
;
72 MmapAccessScope
* mPreviousScope
;
75 // Gets around warnings for null-checking in a macro.
77 inline bool ValidFD(T fd
) {
81 # define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) \
83 void* mmapScopeBuf = nullptr; \
84 nsCString mmapScopeFilename; \
85 uint32_t mmapScopeBufLen = 0; \
86 if (ValidFD(fd) && fd->mMap) { \
87 mmapScopeBuf = (void*)fd->mFileStart; \
88 mmapScopeBufLen = fd->mTotalLen; \
90 if (ValidFD(fd) && fd->mFile) { \
91 nsCOMPtr<nsIFile> file = fd->mFile.GetBaseFile(); \
92 file->GetNativeLeafName(mmapScopeFilename); \
94 MmapAccessScope mmapScope(mmapScopeBuf, mmapScopeBufLen, \
95 mmapScopeFilename.get()); \
96 if (sigsetjmp(mmapScope.mJmpBuf, 0) == 0) {
97 # define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) \
99 MmapAccessScope mmapScope((void*)(buf), (bufLen)); \
100 if (sigsetjmp(mmapScope.mJmpBuf, 0) == 0) {
101 # define MMAP_FAULT_HANDLER_CATCH(retval) \
104 NS_WARNING("SIGBUS received when accessing mmapped file"); \