Skip to content

Commit 54d793d

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: Fix JIT TLS on MacOS
2 parents f84605b + 3abebf3 commit 54d793d

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ PHP NEWS
2020
- Opcache:
2121
. Fixed bug GH-20081 (access to uninitialized vars in preload_load()).
2222
(Arnaud)
23+
. Fixed bug GH-20121 (JIT broken in ZTS builds on MacOS 15).
24+
(Arnaud, Shivam Mathur)
2325

2426
- Phar:
2527
. Fix memory leak of argument in webPhar. (nielsdos)

ext/opcache/jit/zend_jit_ir.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "jit/ir/ir.h"
2020
#include "jit/ir/ir_builder.h"
2121

22+
#if defined(__APPLE__) && defined(__x86_64__)
23+
# include <mach-o/dyld.h>
24+
#endif
25+
2226
#if defined(IR_TARGET_X86)
2327
# define IR_REG_SP 4 /* IR_REG_RSP */
2428
# define IR_REG_FP 5 /* IR_REG_RBP */
@@ -3329,6 +3333,24 @@ static void zend_jit_setup_unwinder(void)
33293333
}
33303334
#endif
33313335

3336+
#if defined(__APPLE__) && defined(__x86_64__)
3337+
/* Thunk format used since dydl 1284 (approx. MacOS 15)
3338+
* https://github.com/apple-oss-distributions/dyld/blob/9307719dd8dc9b385daa412b03cfceb897b2b398/libdyld/ThreadLocalVariables.h#L146 */
3339+
struct TLV_Thunkv2
3340+
{
3341+
void* func;
3342+
uint32_t key;
3343+
uint32_t offset;
3344+
};
3345+
3346+
/* Thunk format used in earlier versions */
3347+
struct TLV_Thunkv1
3348+
{
3349+
void* func;
3350+
size_t key;
3351+
size_t offset;
3352+
};
3353+
#endif
33323354

33333355
static void zend_jit_setup(bool reattached)
33343356
{
@@ -3436,12 +3458,25 @@ static void zend_jit_setup(bool reattached)
34363458
# elif defined(__APPLE__) && defined(__x86_64__)
34373459
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
34383460
if (tsrm_ls_cache_tcb_offset == 0) {
3439-
size_t *ti;
3461+
struct TLV_Thunkv2 *thunk;
34403462
__asm__(
34413463
"leaq __tsrm_ls_cache(%%rip),%0"
3442-
: "=r" (ti));
3443-
tsrm_tls_offset = ti[2];
3444-
tsrm_tls_index = ti[1] * 8;
3464+
: "=r" (thunk));
3465+
3466+
/* Detect dyld 1284: With dyld 1284, thunk->func will be _tlv_get_addr.
3467+
* Unfortunately this symbol is private, but we can find it
3468+
* as _tlv_bootstrap+8: https://github.com/apple-oss-distributions/dyld/blob/9307719dd8dc9b385daa412b03cfceb897b2b398/libdyld/threadLocalHelpers.s#L54
3469+
* In earlier versions, thunk->func will be tlv_get_addr, which is not
3470+
* _tlv_bootstrap+8.
3471+
*/
3472+
if (thunk->func == (void*)((char*)_tlv_bootstrap + 8)) {
3473+
tsrm_tls_offset = thunk->offset;
3474+
tsrm_tls_index = (size_t)thunk->key * 8;
3475+
} else {
3476+
struct TLV_Thunkv1 *thunkv1 = (struct TLV_Thunkv1*) thunk;
3477+
tsrm_tls_offset = thunkv1->offset;
3478+
tsrm_tls_index = thunkv1->key * 8;
3479+
}
34453480
}
34463481
# elif defined(__GNUC__) && defined(__x86_64__)
34473482
tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();

0 commit comments

Comments
 (0)