25
25
#ifndef TCG_APPLE_JIT_H
26
26
#define TCG_APPLE_JIT_H
27
27
28
- #if defined(__APPLE__ ) && defined(HAVE_PTHREAD_JIT_PROTECT ) && defined(HAVE_SPRR ) && (defined(__arm__ ) || defined(__aarch64__ ))
28
+ #include "assert.h"
29
+ #include "stdint.h"
30
+ #include "stdlib.h"
31
+ #include "stdbool.h"
29
32
30
- /* write protect enable = write disable */
31
- static inline void jit_write_protect (int enabled )
32
- {
33
- return pthread_jit_write_protect_np (enabled );
34
- }
33
+ #if defined(__APPLE__ ) && defined(HAVE_SPRR ) && (defined(__arm__ ) || defined(__aarch64__ ))
35
34
36
35
// Returns the S3_6_c15_c1_5 register's value
37
36
// Taken from
38
37
// https://stackoverflow.com/questions/70019553/lldb-how-to-read-the-permissions-of-a-memory-region-for-a-thread
39
38
// https://blog.svenpeter.dev/posts/m1_sprr_gxf/
39
+ // On Github Action (Virtualized environment), this shall always returns 0
40
40
static inline uint64_t read_sprr_perm (void )
41
41
{
42
42
uint64_t v ;
@@ -50,7 +50,11 @@ __attribute__((unused)) static inline uint8_t thread_mask()
50
50
{
51
51
uint64_t v = read_sprr_perm ();
52
52
53
- return (v >> 20 ) & 3 ;
53
+ if (v == 0 ) {
54
+ return 0 ;
55
+ } else {
56
+ return (v >> 20 ) & 3 ;
57
+ }
54
58
}
55
59
56
60
__attribute__((unused )) static inline bool thread_writeable ()
@@ -63,25 +67,69 @@ __attribute__((unused)) static inline bool thread_executable()
63
67
return thread_mask () == 1 ;
64
68
}
65
69
70
+ static inline void assert_executable (bool executable ) {
71
+ uint64_t v = read_sprr_perm ();
72
+
73
+ if (!v ) {
74
+ assert (executable == thread_executable ());
75
+ }
76
+ }
77
+
78
+ #else
79
+
80
+ // Returns the S3_6_c15_c1_5 register's value
81
+ // Taken from
82
+ // https://stackoverflow.com/questions/70019553/lldb-how-to-read-the-permissions-of-a-memory-region-for-a-thread
83
+ // https://blog.svenpeter.dev/posts/m1_sprr_gxf/
84
+ static inline uint64_t read_sprr_perm (void )
85
+ {
86
+ return 0 ;
87
+ }
88
+
89
+ __attribute__((unused )) static inline uint8_t thread_mask ()
90
+ {
91
+ return 0 ;
92
+ }
93
+
94
+ __attribute__((unused )) static inline bool thread_writeable ()
95
+ {
96
+ return false;
97
+ }
98
+
99
+ __attribute__((unused )) static inline bool thread_executable ()
100
+ {
101
+ return false;
102
+ }
103
+
104
+ static inline void assert_executable (bool executable ) {
105
+ }
106
+
107
+ #endif
108
+
109
+
110
+ #if defined(__APPLE__ ) && defined(HAVE_PTHREAD_JIT_PROTECT ) && defined(HAVE_SPRR ) && (defined(__arm__ ) || defined(__aarch64__ ))
111
+
112
+ /* write protect enable = write disable */
113
+ static inline void jit_write_protect (int enabled )
114
+ {
115
+ return pthread_jit_write_protect_np (enabled );
116
+ }
117
+
66
118
#define JIT_CALLBACK_GUARD (x ) \
67
119
{ \
68
120
bool executable = uc->current_executable; \
69
- assert (executable == thread_executable()); \
121
+ assert_executable (executable); \
70
122
x; \
71
- if (executable != thread_executable()) { \
72
- jit_write_protect(executable); \
73
- } \
123
+ jit_write_protect(executable); \
74
124
} \
75
125
76
126
77
127
#define JIT_CALLBACK_GUARD_VAR (var , x ) \
78
128
{ \
79
129
bool executable = uc->current_executable; \
80
- assert (executable == thread_executable()); \
130
+ assert_executable (executable); \
81
131
var = x; \
82
- if (executable != thread_executable()) { \
83
- jit_write_protect(executable); \
84
- } \
132
+ jit_write_protect(executable); \
85
133
} \
86
134
87
135
0 commit comments