Skip to content

Commit 9e6ac9c

Browse files
committed
Allow overriding threadSelfRestrict* funcs with a macro
https://bugs.webkit.org/show_bug.cgi?id=276648 rdar://131809314 Reviewed by Justin Michaud. The new OS_THREAD_SELF_RESTRICT and OS_THREAD_SELF_RESTRICT_SUPPORTED macros can be used to locally override the default functionality, regardless of runtime options or target, by just defining the appropriate macros upstream of FastJITPermissions.h. * Source/JavaScriptCore/assembler/FastJITPermissions.h: (threadSelfRestrictSupported): (threadSelfRestrict): (threadSelfRestrictRWXToRW): Deleted. (threadSelfRestrictRWXToRX): Deleted. * Source/JavaScriptCore/assembler/LinkBuffer.cpp: (JSC::LinkBuffer::copyCompactAndLinkCode): * Source/JavaScriptCore/assembler/SecureARM64EHashPins.cpp: (JSC::WriteToJITRegionScope::WriteToJITRegionScope): (JSC::WriteToJITRegionScope::~WriteToJITRegionScope): * Source/JavaScriptCore/jit/ExecutableAllocator.cpp: (JSC::initializeJITPageReservation): * Source/JavaScriptCore/jit/ExecutableAllocator.h: (JSC::performJITMemcpy): Canonical link: https://commits.webkit.org/281033@main
1 parent ac31fae commit 9e6ac9c

5 files changed

Lines changed: 69 additions & 49 deletions

File tree

‎Source/JavaScriptCore/assembler/FastJITPermissions.h‎

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@
2828
#include <stdint.h>
2929
#include <wtf/Platform.h>
3030

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)
3154
#if OS(DARWIN) && CPU(ARM64)
3255

3356
#include "JSCConfig.h"
@@ -37,102 +60,99 @@
3760
#if USE(INLINE_JIT_PERMISSIONS_API)
3861
#include <BrowserEngineCore/BEMemory.h>
3962

63+
template <MemoryRestriction>
4064
static ALWAYS_INLINE bool threadSelfRestrictSupported()
4165
{
4266
return (&be_memory_inline_jit_restrict_with_witness_supported != nullptr
4367
&& !!be_memory_inline_jit_restrict_with_witness_supported());
4468
}
4569

46-
static ALWAYS_INLINE void threadSelfRestrictRWXToRW()
70+
template <MemoryRestriction restriction>
71+
static ALWAYS_INLINE void threadSelfRestrict()
4772
{
4873
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();
5680
}
5781

5882
#elif USE(PTHREAD_JIT_PERMISSIONS_API)
5983
#include <pthread.h>
6084

85+
template <MemoryRestriction>
6186
static ALWAYS_INLINE bool threadSelfRestrictSupported()
6287
{
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();
7089
}
7190

72-
static ALWAYS_INLINE void threadSelfRestrictRWXToRX()
91+
template <MemoryRestriction restriction>
92+
static ALWAYS_INLINE void threadSelfRestrict()
7393
{
7494
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();
76101
}
77102

78103
#elif USE(APPLE_INTERNAL_SDK)
79104
#include <os/thread_self_restrict.h>
80105

106+
template <MemoryRestriction>
81107
SUPPRESS_ASAN static ALWAYS_INLINE bool threadSelfRestrictSupported()
82108
{
83109
return !!os_thread_self_restrict_rwx_is_supported();
84110
}
85111

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()
93114
{
94115
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();
96122
}
97123

98124
#else
99-
125+
template <MemoryRestriction>
100126
static ALWAYS_INLINE bool threadSelfRestrictSupported()
101127
{
102128
return false;
103129
}
104130

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()
113133
{
114134
bool tautologyToIgnoreWarning = true;
115135
if (tautologyToIgnoreWarning)
116136
RELEASE_ASSERT_NOT_REACHED();
117137
}
118138

119139
#endif
120-
121140
#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
122144

145+
template <MemoryRestriction>
123146
static ALWAYS_INLINE bool threadSelfRestrictSupported()
124147
{
125148
return false;
126149
}
127150

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()
134153
{
135154
CRASH();
136155
}
137156

138157
#endif // OS(DARWIN) && CPU(ARM64)
158+
#endif // defined(OS_THREAD_SELF_RESTRICT) && defined(OS_THREAD_SELF_RESTRICT_SUPPORTED)

‎Source/JavaScriptCore/assembler/LinkBuffer.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void LinkBuffer::copyCompactAndLinkCode(MacroAssembler& macroAssembler, JITCompi
328328
};
329329

330330
if (g_jscConfig.useFastJITPermissions)
331-
threadSelfRestrictRWXToRW();
331+
threadSelfRestrict<MemoryRestriction::kRwxToRw>();
332332
#if ENABLE(MPROTECT_RX_TO_RWX)
333333
ExecutableAllocator::singleton().startWriting(outData, initialSize);
334334
#endif
@@ -429,7 +429,7 @@ void LinkBuffer::copyCompactAndLinkCode(MacroAssembler& macroAssembler, JITCompi
429429
Assembler::fillNops<MachineCodeCopyMode::JITMemcpy>(outData + compactSize, nopSizeInBytes);
430430
}
431431
if (g_jscConfig.useFastJITPermissions)
432-
threadSelfRestrictRWXToRX();
432+
threadSelfRestrict<MemoryRestriction::kRwxToRx>();
433433

434434
if (m_executableMemory) {
435435
m_size = compactSize;

‎Source/JavaScriptCore/assembler/SecureARM64EHashPins.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ namespace JSC {
3939
struct WriteToJITRegionScope {
4040
ALWAYS_INLINE WriteToJITRegionScope()
4141
{
42-
threadSelfRestrictRWXToRW();
42+
threadSelfRestrict<MemoryRestriction::kRwxToRw>();
4343
}
4444

4545
ALWAYS_INLINE ~WriteToJITRegionScope()
4646
{
47-
threadSelfRestrictRWXToRX();
47+
threadSelfRestrict<MemoryRestriction::kRwxToRx>();
4848
}
4949
};
5050

‎Source/JavaScriptCore/jit/ExecutableAllocator.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,10 @@ static ALWAYS_INLINE JITReservation initializeJITPageReservation()
423423
if (reservation.pageReservation) {
424424
ASSERT(reservation.pageReservation.size() == reservation.size);
425425
reservation.base = reservation.pageReservation.base();
426-
g_jscConfig.useFastJITPermissions = threadSelfRestrictSupported();
426+
g_jscConfig.useFastJITPermissions = threadSelfRestrictSupported<MemoryRestriction::kRwxToRw>();
427427

428428
if (g_jscConfig.useFastJITPermissions)
429-
threadSelfRestrictRWXToRX();
429+
threadSelfRestrict<MemoryRestriction::kRwxToRx>();
430430

431431
#if ENABLE(SEPARATED_WX_HEAP)
432432
if (!g_jscConfig.useFastJITPermissions) {

‎Source/JavaScriptCore/jit/ExecutableAllocator.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ static ALWAYS_INLINE void* performJITMemcpy(void *dst, const void *src, size_t n
137137
#endif
138138

139139
if (g_jscConfig.useFastJITPermissions) {
140-
threadSelfRestrictRWXToRW();
140+
threadSelfRestrict<MemoryRestriction::kRwxToRw>();
141141
memcpy(dst, src, n);
142-
threadSelfRestrictRWXToRX();
142+
threadSelfRestrict<MemoryRestriction::kRwxToRx>();
143143
return dst;
144144
}
145145

0 commit comments

Comments
 (0)