Backed out changeset 8f976ed899d7 (bug 1847231) for causing bc failures on browser_se...
[gecko.git] / mozglue / misc / MmapFaultHandler.h
blob8ca43e85d3dc82bc115f7c70a9ba7ff673cae364
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_
9 #if defined(XP_WIN)
10 // Windows
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) \
16 } \
17 __except (GetExceptionCode() == EXCEPTION_IN_PAGE_ERROR \
18 ? EXCEPTION_EXECUTE_HANDLER \
19 : EXCEPTION_CONTINUE_SEARCH) { \
20 NS_WARNING("unexpected EXCEPTION_IN_PAGE_ERROR"); \
21 return retval; \
23 # else
24 # define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) {
25 # define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) {
26 # define MMAP_FAULT_HANDLER_CATCH(retval) }
27 # endif
29 #elif defined(XP_DARWIN)
30 // MacOS
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) }
42 #else
43 // Linux
45 # include "mozilla/Attributes.h"
46 # include "mozilla/Types.h"
47 # include <stdint.h>
48 # include <setjmp.h>
50 class MOZ_RAII MmapAccessScope {
51 public:
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
65 // public.
66 sigjmp_buf mJmpBuf;
68 private:
69 void* mBuf;
70 const char* mFilename;
71 uint32_t mBufLen;
72 MmapAccessScope* mPreviousScope;
75 // Gets around warnings for null-checking in a macro.
76 template <typename T>
77 inline bool ValidFD(T fd) {
78 return !!fd;
81 # define MMAP_FAULT_HANDLER_BEGIN_HANDLE(fd) \
82 { \
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; \
89 } \
90 if (ValidFD(fd) && fd->mFile) { \
91 nsCOMPtr<nsIFile> file = fd->mFile.GetBaseFile(); \
92 file->GetNativeLeafName(mmapScopeFilename); \
93 } \
94 MmapAccessScope mmapScope(mmapScopeBuf, mmapScopeBufLen, \
95 mmapScopeFilename.get()); \
96 if (sigsetjmp(mmapScope.mJmpBuf, 0) == 0) {
97 # define MMAP_FAULT_HANDLER_BEGIN_BUFFER(buf, bufLen) \
98 { \
99 MmapAccessScope mmapScope((void*)(buf), (bufLen)); \
100 if (sigsetjmp(mmapScope.mJmpBuf, 0) == 0) {
101 # define MMAP_FAULT_HANDLER_CATCH(retval) \
103 else { \
104 NS_WARNING("SIGBUS received when accessing mmapped file"); \
105 return retval; \
109 #endif
111 #endif