|
28 | 28 | #include <stdint.h> |
29 | 29 | #include <wtf/Platform.h> |
30 | 30 |
|
| 31 | +enum class MemoryRestriction { |
| 32 | + kRwxToRw, |
| 33 | + kRwxToRx, |
| 34 | +}; |
| 35 | + |
| 36 | +#if defined(OS_THREAD_SELF_RESTRICT) != defined(OS_THREAD_SELF_RESTRICT_SUPPORTED) |
| 37 | +#error Must override both or neither of OS_THREAD_SELF_RESTRICT and OS_THREAD_SELF_RESTRICT_SUPPORTED |
| 38 | +#endif |
| 39 | + |
| 40 | +#if defined(OS_THREAD_SELF_RESTRICT) && defined(OS_THREAD_SELF_RESTRICT_SUPPORTED) |
| 41 | +template <MemoryRestriction restriction> |
| 42 | +static ALWAYS_INLINE bool threadSelfRestrictSupported() |
| 43 | +{ |
| 44 | + return OS_THREAD_SELF_RESTRICT_SUPPORTED(restriction); |
| 45 | +} |
| 46 | + |
| 47 | +template <MemoryRestriction restriction> |
| 48 | +static ALWAYS_INLINE void threadSelfRestrict() |
| 49 | +{ |
| 50 | + OS_THREAD_SELF_RESTRICT(restriction); |
| 51 | +} |
| 52 | + |
| 53 | +#else // Not defined(OS_THREAD_SELF_RESTRICT) && defined(OS_THREAD_SELF_RESTRICT_SUPPORTED) |
31 | 54 | #if OS(DARWIN) && CPU(ARM64) |
32 | 55 |
|
33 | 56 | #include "JSCConfig.h" |
|
37 | 60 | #if USE(INLINE_JIT_PERMISSIONS_API) |
38 | 61 | #include <BrowserEngineCore/BEMemory.h> |
39 | 62 |
|
| 63 | +template <MemoryRestriction> |
40 | 64 | static ALWAYS_INLINE bool threadSelfRestrictSupported() |
41 | 65 | { |
42 | 66 | return (&be_memory_inline_jit_restrict_with_witness_supported != nullptr |
43 | 67 | && !!be_memory_inline_jit_restrict_with_witness_supported()); |
44 | 68 | } |
45 | 69 |
|
46 | | -static ALWAYS_INLINE void threadSelfRestrictRWXToRW() |
| 70 | +template <MemoryRestriction restriction> |
| 71 | +static ALWAYS_INLINE void threadSelfRestrict() |
47 | 72 | { |
48 | 73 | ASSERT(g_jscConfig.useFastJITPermissions); |
49 | | - be_memory_inline_jit_restrict_rwx_to_rw_with_witness(); |
50 | | -} |
51 | | - |
52 | | -static ALWAYS_INLINE void threadSelfRestrictRWXToRX() |
53 | | -{ |
54 | | - ASSERT(g_jscConfig.useFastJITPermissions); |
55 | | - be_memory_inline_jit_restrict_rwx_to_rx_with_witness(); |
| 74 | + if constexpr (restriction == MemoryRestriction::kRwxToRw) |
| 75 | + be_memory_inline_jit_restrict_rwx_to_rw_with_witness(); |
| 76 | + else if constexpr (restriction == MemoryRestriction::kRwxToRx) |
| 77 | + be_memory_inline_jit_restrict_rwx_to_rx_with_witness(); |
| 78 | + else |
| 79 | + RELEASE_ASSERT_NOT_REACHED(); |
56 | 80 | } |
57 | 81 |
|
58 | 82 | #elif USE(PTHREAD_JIT_PERMISSIONS_API) |
59 | 83 | #include <pthread.h> |
60 | 84 |
|
| 85 | +template <MemoryRestriction> |
61 | 86 | static ALWAYS_INLINE bool threadSelfRestrictSupported() |
62 | 87 | { |
63 | | - return !!pthread_jit_write_protect_supported_np();; |
64 | | -} |
65 | | - |
66 | | -static ALWAYS_INLINE void threadSelfRestrictRWXToRW() |
67 | | -{ |
68 | | - ASSERT(g_jscConfig.useFastJITPermissions); |
69 | | - pthread_jit_write_protect_np(false); |
| 88 | + return !!pthread_jit_write_protect_supported_np(); |
70 | 89 | } |
71 | 90 |
|
72 | | -static ALWAYS_INLINE void threadSelfRestrictRWXToRX() |
| 91 | +template <MemoryRestriction restriction> |
| 92 | +static ALWAYS_INLINE void threadSelfRestrict() |
73 | 93 | { |
74 | 94 | ASSERT(g_jscConfig.useFastJITPermissions); |
75 | | - pthread_jit_write_protect_np(true); |
| 95 | + if constexpr (restriction == MemoryRestriction::kRwxToRw) |
| 96 | + pthread_jit_write_protect_np(false); |
| 97 | + else if constexpr (restriction == MemoryRestriction::kRwxToRx) |
| 98 | + pthread_jit_write_protect_np(true); |
| 99 | + else |
| 100 | + RELEASE_ASSERT_NOT_REACHED(); |
76 | 101 | } |
77 | 102 |
|
78 | 103 | #elif USE(APPLE_INTERNAL_SDK) |
79 | 104 | #include <os/thread_self_restrict.h> |
80 | 105 |
|
| 106 | +template <MemoryRestriction> |
81 | 107 | SUPPRESS_ASAN static ALWAYS_INLINE bool threadSelfRestrictSupported() |
82 | 108 | { |
83 | 109 | return !!os_thread_self_restrict_rwx_is_supported(); |
84 | 110 | } |
85 | 111 |
|
86 | | -SUPPRESS_ASAN static ALWAYS_INLINE void threadSelfRestrictRWXToRW() |
87 | | -{ |
88 | | - ASSERT(g_jscConfig.useFastJITPermissions); |
89 | | - os_thread_self_restrict_rwx_to_rw(); |
90 | | -} |
91 | | - |
92 | | -SUPPRESS_ASAN static ALWAYS_INLINE void threadSelfRestrictRWXToRX() |
| 112 | +template <MemoryRestriction restriction> |
| 113 | +static ALWAYS_INLINE void threadSelfRestrict() |
93 | 114 | { |
94 | 115 | ASSERT(g_jscConfig.useFastJITPermissions); |
95 | | - os_thread_self_restrict_rwx_to_rx(); |
| 116 | + if constexpr (restriction == MemoryRestriction::kRwxToRw) |
| 117 | + os_thread_self_restrict_rwx_to_rw(); |
| 118 | + else if constexpr (restriction == MemoryRestriction::kRwxToRx) |
| 119 | + os_thread_self_restrict_rwx_to_rx(); |
| 120 | + else |
| 121 | + RELEASE_ASSERT_NOT_REACHED(); |
96 | 122 | } |
97 | 123 |
|
98 | 124 | #else |
99 | | - |
| 125 | +template <MemoryRestriction> |
100 | 126 | static ALWAYS_INLINE bool threadSelfRestrictSupported() |
101 | 127 | { |
102 | 128 | return false; |
103 | 129 | } |
104 | 130 |
|
105 | | -static ALWAYS_INLINE void threadSelfRestrictRWXToRW() |
106 | | -{ |
107 | | - bool tautologyToIgnoreWarning = true; |
108 | | - if (tautologyToIgnoreWarning) |
109 | | - RELEASE_ASSERT_NOT_REACHED(); |
110 | | -} |
111 | | - |
112 | | -static ALWAYS_INLINE void threadSelfRestrictRWXToRX() |
| 131 | +template <MemoryRestriction> |
| 132 | +static ALWAYS_INLINE void threadSelfRestrict() |
113 | 133 | { |
114 | 134 | bool tautologyToIgnoreWarning = true; |
115 | 135 | if (tautologyToIgnoreWarning) |
116 | 136 | RELEASE_ASSERT_NOT_REACHED(); |
117 | 137 | } |
118 | 138 |
|
119 | 139 | #endif |
120 | | - |
121 | 140 | #else // Not OS(DARWIN) && CPU(ARM64) |
| 141 | +#if defined(OS_THREAD_SELF_RESTRICT) || defined(OS_THREAD_SELF_RESTRICT_SUPPORTED) |
| 142 | +#error OS_THREAD_SELF_RESTRICT and OS_THREAD_SELF_RESTRICT_SUPPORTED are only used on ARM64+Darwin-only systems |
| 143 | +#endif |
122 | 144 |
|
| 145 | +template <MemoryRestriction> |
123 | 146 | static ALWAYS_INLINE bool threadSelfRestrictSupported() |
124 | 147 | { |
125 | 148 | return false; |
126 | 149 | } |
127 | 150 |
|
128 | | -NO_RETURN_DUE_TO_CRASH ALWAYS_INLINE void threadSelfRestrictRWXToRW() |
129 | | -{ |
130 | | - CRASH(); |
131 | | -} |
132 | | - |
133 | | -NO_RETURN_DUE_TO_CRASH ALWAYS_INLINE void threadSelfRestrictRWXToRX() |
| 151 | +template <MemoryRestriction> |
| 152 | +NO_RETURN_DUE_TO_CRASH ALWAYS_INLINE void threadSelfRestrict() |
134 | 153 | { |
135 | 154 | CRASH(); |
136 | 155 | } |
137 | 156 |
|
138 | 157 | #endif // OS(DARWIN) && CPU(ARM64) |
| 158 | +#endif // defined(OS_THREAD_SELF_RESTRICT) && defined(OS_THREAD_SELF_RESTRICT_SUPPORTED) |
0 commit comments