Skip to content

Commit 79f910e

Browse files
authored
Fix read_sprr_perm for Apple real CPUs and GitHub Actions; enable Apple ARM64 wheel builds on PyPI. Fixes #2033. (#2227)
* Fix `read_sprr_perm` for Apple real CPUs and GitHub Actions; enable Apple ARM64 wheel builds on PyPI. Fixes #2033. * Guard `assert_executable` with `pthread_jit_write_protect_supported_np`
1 parent 1cb2c67 commit 79f910e

3 files changed

Lines changed: 10 additions & 45 deletions

File tree

.github/workflows/build-wheels-publish.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ jobs:
158158
python2 -m pip install capstone==4.0.2 wheelhouse/*py2*.whl
159159
python2 -m unittest discover tests/regress "*.py"
160160
161-
# https://github.com/unicorn-engine/unicorn/issues/2033
162-
# Skip macos arm64 wheels during release stage
163161
- uses: actions/upload-artifact@v4
164-
if: ${{!( startsWith(github.ref, 'refs/tags') && !startsWith(github.ref, 'refs/tags/v') && contains(matrix.os, 'macos') && contains(matrix.arch, 'arm64') )}}
165162
with:
166163
name: ${{ env.ARTIFACT_NAME }}
167164
path: ./wheelhouse/*.whl
@@ -281,7 +278,6 @@ jobs:
281278
output-dir: wheelhouse
282279

283280
- uses: actions/upload-artifact@v4
284-
if: ${{!( startsWith(github.ref, 'refs/tags') && !startsWith(github.ref, 'refs/tags/v') && contains(matrix.os, 'macos') && contains(matrix.arch, 'arm64') )}}
285281
with:
286282
name: ${{ env.ARTIFACT_NAME }}
287283
path: ./wheelhouse/*.whl

qemu/configure

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,39 +2152,13 @@ fi
21522152
if [ "$darwin" = "yes" ] ; then
21532153
cat > $TMPC << EOF
21542154
#include <pthread.h>
2155-
int main() { pthread_jit_write_protect_np(0); return 0;}
2155+
int main() { pthread_jit_write_protect_supported_np(); return 0;}
21562156
EOF
21572157
if ! compile_prog ""; then
21582158
have_pthread_jit_protect='no'
21592159
else
21602160
have_pthread_jit_protect='yes'
21612161
fi
2162-
2163-
if test "$have_pthread_jit_protect" = "yes" ; then
2164-
cat > $TMPC << EOF
2165-
#include "stdint.h"
2166-
int main() {
2167-
uint64_t v;
2168-
2169-
__asm__ __volatile__("isb sy\n"
2170-
"mrs %0, S3_6_c15_c1_5\n"
2171-
: "=r"(v)::"memory");
2172-
// In Apple Hypervisor virtualized environment (EL1), this value is not accessbile
2173-
// but pthread_jit_write_protect_np essentially is a no-op.
2174-
return 0;
2175-
}
2176-
EOF
2177-
if ! compile_prog ""; then
2178-
have_sprr_mrs='no'
2179-
else
2180-
$TMPE
2181-
if [ $? -eq 0 ]; then
2182-
have_sprr_mrs='yes'
2183-
else
2184-
have_sprr_mrs='no'
2185-
fi
2186-
fi
2187-
fi
21882162
fi
21892163

21902164
##########################################
@@ -2565,10 +2539,6 @@ if test "$have_pthread_jit_protect" = "yes" ; then
25652539
echo "HAVE_PTHREAD_JIT_PROTECT=y" >> $config_host_mak
25662540
fi
25672541

2568-
if test "$have_sprr_mrs" = "yes" ; then
2569-
echo "HAVE_SPRR_MRS=y" >> $config_host_mak
2570-
fi
2571-
25722542
# Hold two types of flag:
25732543
# CONFIG_THREAD_SETNAME_BYTHREAD - we've got a way of setting the name on
25742544
# a thread we have a handle to

qemu/include/tcg/tcg-apple-jit.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
#include "stdbool.h"
3131
#include "qemu/compiler.h"
3232

33+
#if defined(__APPLE__) && defined(HAVE_PTHREAD_JIT_PROTECT) && (defined(__arm__) || defined(__aarch64__))
34+
3335
// Returns the S3_6_c15_c1_5 register's value
34-
// Taken from
36+
// Taken from
3537
// https://stackoverflow.com/questions/70019553/lldb-how-to-read-the-permissions-of-a-memory-region-for-a-thread
3638
// https://blog.svenpeter.dev/posts/m1_sprr_gxf/
3739
// On Github Action (Virtualized environment), this shall always returns 0
38-
#if defined(HAVE_SPRR_MRS)
3940
static inline uint64_t read_sprr_perm(void)
4041
{
4142
uint64_t v;
@@ -44,17 +45,12 @@ static inline uint64_t read_sprr_perm(void)
4445
: "=r"(v)::"memory");
4546
return v;
4647
}
47-
#else
48-
static inline uint64_t read_sprr_perm(void)
49-
{
50-
return 0;
51-
}
52-
#endif
53-
54-
#if defined(__APPLE__) && defined(HAVE_SPRR_MRS) && defined(HAVE_PTHREAD_JIT_PROTECT) && (defined(__arm__) || defined(__aarch64__))
5548

5649
QEMU_UNUSED_FUNC static inline uint8_t thread_mask()
5750
{
51+
if (!pthread_jit_write_protect_supported_np()) {
52+
return 0;
53+
}
5854
uint64_t v = read_sprr_perm();
5955

6056
if (v == 0) {
@@ -75,6 +71,9 @@ QEMU_UNUSED_FUNC static inline bool thread_executable()
7571
}
7672

7773
static inline void assert_executable(bool executable) {
74+
if (!pthread_jit_write_protect_supported_np()) {
75+
return;
76+
}
7877
uint64_t v = read_sprr_perm();
7978

8079
if (!v) {

0 commit comments

Comments
 (0)