Skip to content

Commit 5103bc4

Browse files
committed
fix windows jit
1 parent 6afa98f commit 5103bc4

4 files changed

Lines changed: 24 additions & 22 deletions

File tree

TSRM/TSRM.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id)
534534
/* In case that extensions don't use the pointer passed from the dtor, but incorrectly
535535
* use the global pointer, we need to setup the global pointer temporarily here. */
536536
set_thread_local_storage_resource_to(thread_resources);
537-
/* Dead thread, recycled id: already freed, so just zero it. */
537+
/* Free up the old resource from the old thread instance */
538538
thread_resources->thread_id = 0;
539539
ts_free_resources(thread_resources);
540540
free(thread_resources);

TSRM/TSRM.h

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,24 @@ TSRM_API bool tsrm_is_managed_thread(void);
178178
#define TSRMG_FAST_STATIC(offset, type, element) (TSRMG_FAST_BULK_STATIC(offset, type)->element)
179179
#define TSRMG_FAST_BULK_STATIC(offset, type) ((type) (((char*) TSRMLS_CACHE)+(offset)))
180180
struct _zend_tsrm_ls_cache;
181+
#if defined(ZEND_WIN32) && !defined(LIBZEND_EXPORTS)
182+
/* Windows can't dllexport the TLS struct, so outside Zend each module
183+
* keeps a per-module `void *` pointer and reaches EG/CG via the resource-id indirection. */
184+
# define ZEND_TSRMLS_CACHE_T void *
185+
# define TSRMLS_MAIN_CACHE_DEFINE() TSRM_TLS void *_tsrm_ls_cache TSRM_TLS_MODEL_ATTR = NULL;
186+
# define TSRMLS_CACHE_DEFINE() TSRM_TLS void *_tsrm_ls_cache = NULL;
187+
#else
188+
# define ZEND_TSRMLS_CACHE_T struct _zend_tsrm_ls_cache
189+
# define TSRMLS_MAIN_CACHE_DEFINE()
190+
# define TSRMLS_CACHE_DEFINE()
191+
#endif
181192
#ifdef __cplusplus
182-
#define TSRMLS_MAIN_CACHE_EXTERN() extern "C" { extern TSRM_TLS struct _zend_tsrm_ls_cache _tsrm_ls_cache TSRM_TLS_MODEL_ATTR; }
183-
#define TSRMLS_CACHE_EXTERN() extern "C" { extern TSRM_TLS struct _zend_tsrm_ls_cache _tsrm_ls_cache; }
193+
#define TSRMLS_MAIN_CACHE_EXTERN() extern "C" { extern TSRM_TLS ZEND_TSRMLS_CACHE_T _tsrm_ls_cache TSRM_TLS_MODEL_ATTR; }
194+
#define TSRMLS_CACHE_EXTERN() extern "C" { extern TSRM_TLS ZEND_TSRMLS_CACHE_T _tsrm_ls_cache; }
184195
#else
185-
#define TSRMLS_MAIN_CACHE_EXTERN() extern TSRM_TLS struct _zend_tsrm_ls_cache _tsrm_ls_cache TSRM_TLS_MODEL_ATTR;
186-
#define TSRMLS_CACHE_EXTERN() extern TSRM_TLS struct _zend_tsrm_ls_cache _tsrm_ls_cache;
196+
#define TSRMLS_MAIN_CACHE_EXTERN() extern TSRM_TLS ZEND_TSRMLS_CACHE_T _tsrm_ls_cache TSRM_TLS_MODEL_ATTR;
197+
#define TSRMLS_CACHE_EXTERN() extern TSRM_TLS ZEND_TSRMLS_CACHE_T _tsrm_ls_cache;
187198
#endif
188-
#define TSRMLS_MAIN_CACHE_DEFINE()
189-
#define TSRMLS_CACHE_DEFINE()
190199
#define TSRMLS_CACHE_UPDATE() TSRMLS_CACHE = tsrm_get_ls_cache()
191200
#define TSRMLS_CACHE (*(void **) &_tsrm_ls_cache)
192201

Zend/zend_globals_macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ BEGIN_EXTERN_C()
4040

4141
#ifdef ZTS
4242
typedef struct _zend_tsrm_ls_cache zend_tsrm_ls_cache;
43+
# ifdef ZEND_TLS_DIRECT
4344
extern ZEND_TLS_API TSRM_TLS TSRM_TLS_MODEL_ATTR zend_tsrm_ls_cache _tsrm_ls_cache;
45+
# endif
4446
#endif
4547

4648
/* Compiler */

ext/opcache/jit/tls/zend_jit_tls_win.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,21 @@ zend_result zend_jit_resolve_tsrm_ls_cache_offsets(
3333
size_t *module_index,
3434
size_t *module_offset
3535
) {
36-
/* To find offset of "_tsrm_ls_cache" in TLS segment we perform a linear scan of local TLS memory */
37-
/* Probably, it might be better solution */
36+
/* Offset of _tsrm_ls_cache within this module's TLS block. */
3837
#ifdef _WIN64
3938
void ***tls_mem = ((void****)__readgsqword(0x58))[_tls_index];
4039
#else
4140
void ***tls_mem = ((void****)__readfsdword(0x2c))[_tls_index];
4241
#endif
43-
void *val = _tsrm_ls_cache;
44-
size_t offset = 0;
4542
size_t size = (char*)&_tls_end - (char*)&_tls_start;
46-
47-
while (offset < size) {
48-
if (*tls_mem == val) {
49-
*module_index = _tls_index * sizeof(void*);
50-
*module_offset = offset;
51-
return SUCCESS;
52-
}
53-
tls_mem++;
54-
offset += sizeof(void*);
55-
}
43+
size_t offset = (size_t)((char*)&_tsrm_ls_cache - (char*)tls_mem);
5644

5745
if (offset >= size) {
5846
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Could not enable JIT: offset >= size");
47+
return FAILURE;
5948
}
6049

61-
return FAILURE;
50+
*module_index = _tls_index * sizeof(void*);
51+
*module_offset = offset;
52+
return SUCCESS;
6253
}

0 commit comments

Comments
 (0)