@@ -2235,7 +2235,7 @@ static int zend_jit_hybrid_profile_jit_stub(dasm_State **Dst)
22352235#else
22362236# error "Unknown ZEND_MAP_PTR_KIND"
22372237#endif
2238- | inc aword [r2 + zend_jit_profile_counter_rid * sizeof(void)]
2238+ | inc aword [r2 + zend_jit_profile_counter_rid * sizeof(void* )]
22392239 | // handler = (const void*)ZEND_FUNC_INFO(op_array);
22402240 | mov r0, aword [r0 + offsetof(zend_op_array, reserved[zend_func_info_rid])]
22412241 | // return ((zend_vm_opcode_handler_t)handler)();
@@ -2433,6 +2433,12 @@ static const zend_jit_stub zend_jit_stubs[] = {
24332433 JIT_STUB(double_one),
24342434};
24352435
2436+ #if ZTS && defined(ZEND_WIN32)
2437+ extern uint32_t _tls_index;
2438+ extern char *_tls_start;
2439+ extern char *_tls_end;
2440+ #endif
2441+
24362442static int zend_jit_setup(void)
24372443{
24382444 |.if SSE
@@ -2449,15 +2455,53 @@ static int zend_jit_setup(void)
24492455
24502456#if ZTS
24512457# ifdef _WIN64
2452- // TODO: ???
24532458 tsrm_tls_index = _tls_index * sizeof(void*);
2454- // TODO: how to get this magic value?
2455- tsrm_tls_offset = 0x110;
2459+
2460+ /* To find offset of "_tsrm_ls_cache" in TLS segment we perform a linear scan of local TLS memory */
2461+ /* Probably, it might be better solution */
2462+ do {
2463+ void ***tls_mem = ((void***)__readgsqword(0x58))[_tls_index];
2464+ void *val = _tsrm_ls_cache;
2465+ size_t offset = 0;
2466+ size_t size = (char*)&_tls_end - (char*)&_tls_start;
2467+
2468+ while (offset < size) {
2469+ if (*tls_mem == val) {
2470+ tsrm_tls_offset = offset;
2471+ break;
2472+ }
2473+ tls_mem++;
2474+ offset += sizeof(void*);
2475+ }
2476+ if (offset >= size) {
2477+ // TODO: error message ???
2478+ return FAILURE;
2479+ }
2480+ } while(0);
24562481# elif ZEND_WIN32
2457- // TODO: ???
24582482 tsrm_tls_index = _tls_index * sizeof(void*);
2459- // TODO: how to get this magic value?
2460- tsrm_tls_offset = 0x110;
2483+
2484+ /* To find offset of "_tsrm_ls_cache" in TLS segment we perform a linear scan of local TLS memory */
2485+ /* Probably, it might be better solution */
2486+ do {
2487+ void ***tls_mem = ((void***)__readfsdword(0x2c))[_tls_index];
2488+ void *val = _tsrm_ls_cache;
2489+ size_t offset = 0;
2490+ size_t size = (char*)&_tls_end - (char*)&_tls_start;
2491+
2492+ while (offset < size) {
2493+ if (*tls_mem == val) {
2494+ tsrm_tls_offset = offset;
2495+ break;
2496+ }
2497+ tls_mem++;
2498+ offset += sizeof(void*);
2499+ }
2500+ if (offset >= size) {
2501+ // TODO: error message ???
2502+ return FAILURE;
2503+ }
2504+ } while(0);
24612505# elif defined(__GNUC__) && defined(__x86_64__)
24622506 tsrm_ls_cache_tcb_offset = tsrm_get_ls_cache_tcb_offset();
24632507 if (tsrm_ls_cache_tcb_offset == 0) {
0 commit comments