diff options
Diffstat (limited to 'src/lj_ccallback.c')
-rw-r--r-- | src/lj_ccallback.c | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/lj_ccallback.c b/src/lj_ccallback.c index 4edd8a35..49775d2b 100644 --- a/src/lj_ccallback.c +++ b/src/lj_ccallback.c | |||
@@ -107,9 +107,9 @@ MSize lj_ccallback_ptr2slot(CTState *cts, void *p) | |||
107 | /* Initialize machine code for callback function pointers. */ | 107 | /* Initialize machine code for callback function pointers. */ |
108 | #if LJ_OS_NOJIT | 108 | #if LJ_OS_NOJIT |
109 | /* Disabled callback support. */ | 109 | /* Disabled callback support. */ |
110 | #define callback_mcode_init(g, p) UNUSED(p) | 110 | #define callback_mcode_init(g, p) (p) |
111 | #elif LJ_TARGET_X86ORX64 | 111 | #elif LJ_TARGET_X86ORX64 |
112 | static void callback_mcode_init(global_State *g, uint8_t *page) | 112 | static void *callback_mcode_init(global_State *g, uint8_t *page) |
113 | { | 113 | { |
114 | uint8_t *p = page; | 114 | uint8_t *p = page; |
115 | uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback; | 115 | uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback; |
@@ -143,10 +143,10 @@ static void callback_mcode_init(global_State *g, uint8_t *page) | |||
143 | *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2); | 143 | *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2); |
144 | } | 144 | } |
145 | } | 145 | } |
146 | lua_assert(p - page <= CALLBACK_MCODE_SIZE); | 146 | return p; |
147 | } | 147 | } |
148 | #elif LJ_TARGET_ARM | 148 | #elif LJ_TARGET_ARM |
149 | static void callback_mcode_init(global_State *g, uint32_t *page) | 149 | static void *callback_mcode_init(global_State *g, uint32_t *page) |
150 | { | 150 | { |
151 | uint32_t *p = page; | 151 | uint32_t *p = page; |
152 | void *target = (void *)lj_vm_ffi_callback; | 152 | void *target = (void *)lj_vm_ffi_callback; |
@@ -165,10 +165,10 @@ static void callback_mcode_init(global_State *g, uint32_t *page) | |||
165 | *p = ARMI_B | ((page-p-2) & 0x00ffffffu); | 165 | *p = ARMI_B | ((page-p-2) & 0x00ffffffu); |
166 | p++; | 166 | p++; |
167 | } | 167 | } |
168 | lua_assert(p - page <= CALLBACK_MCODE_SIZE); | 168 | return p; |
169 | } | 169 | } |
170 | #elif LJ_TARGET_ARM64 | 170 | #elif LJ_TARGET_ARM64 |
171 | static void callback_mcode_init(global_State *g, uint32_t *page) | 171 | static void *callback_mcode_init(global_State *g, uint32_t *page) |
172 | { | 172 | { |
173 | uint32_t *p = page; | 173 | uint32_t *p = page; |
174 | void *target = (void *)lj_vm_ffi_callback; | 174 | void *target = (void *)lj_vm_ffi_callback; |
@@ -185,10 +185,10 @@ static void callback_mcode_init(global_State *g, uint32_t *page) | |||
185 | *p = A64I_LE(A64I_B | A64F_S26((page-p) & 0x03ffffffu)); | 185 | *p = A64I_LE(A64I_B | A64F_S26((page-p) & 0x03ffffffu)); |
186 | p++; | 186 | p++; |
187 | } | 187 | } |
188 | lua_assert(p - page <= CALLBACK_MCODE_SIZE); | 188 | return p; |
189 | } | 189 | } |
190 | #elif LJ_TARGET_PPC | 190 | #elif LJ_TARGET_PPC |
191 | static void callback_mcode_init(global_State *g, uint32_t *page) | 191 | static void *callback_mcode_init(global_State *g, uint32_t *page) |
192 | { | 192 | { |
193 | uint32_t *p = page; | 193 | uint32_t *p = page; |
194 | void *target = (void *)lj_vm_ffi_callback; | 194 | void *target = (void *)lj_vm_ffi_callback; |
@@ -204,10 +204,10 @@ static void callback_mcode_init(global_State *g, uint32_t *page) | |||
204 | *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2); | 204 | *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2); |
205 | p++; | 205 | p++; |
206 | } | 206 | } |
207 | lua_assert(p - page <= CALLBACK_MCODE_SIZE); | 207 | return p; |
208 | } | 208 | } |
209 | #elif LJ_TARGET_MIPS | 209 | #elif LJ_TARGET_MIPS |
210 | static void callback_mcode_init(global_State *g, uint32_t *page) | 210 | static void *callback_mcode_init(global_State *g, uint32_t *page) |
211 | { | 211 | { |
212 | uint32_t *p = page; | 212 | uint32_t *p = page; |
213 | uintptr_t target = (uintptr_t)(void *)lj_vm_ffi_callback; | 213 | uintptr_t target = (uintptr_t)(void *)lj_vm_ffi_callback; |
@@ -236,11 +236,11 @@ static void callback_mcode_init(global_State *g, uint32_t *page) | |||
236 | p++; | 236 | p++; |
237 | *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot; | 237 | *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot; |
238 | } | 238 | } |
239 | lua_assert(p - page <= CALLBACK_MCODE_SIZE); | 239 | return p; |
240 | } | 240 | } |
241 | #else | 241 | #else |
242 | /* Missing support for this architecture. */ | 242 | /* Missing support for this architecture. */ |
243 | #define callback_mcode_init(g, p) UNUSED(p) | 243 | #define callback_mcode_init(g, p) (p) |
244 | #endif | 244 | #endif |
245 | 245 | ||
246 | /* -- Machine code management --------------------------------------------- */ | 246 | /* -- Machine code management --------------------------------------------- */ |
@@ -263,7 +263,7 @@ static void callback_mcode_init(global_State *g, uint32_t *page) | |||
263 | static void callback_mcode_new(CTState *cts) | 263 | static void callback_mcode_new(CTState *cts) |
264 | { | 264 | { |
265 | size_t sz = (size_t)CALLBACK_MCODE_SIZE; | 265 | size_t sz = (size_t)CALLBACK_MCODE_SIZE; |
266 | void *p; | 266 | void *p, *pe; |
267 | if (CALLBACK_MAX_SLOT == 0) | 267 | if (CALLBACK_MAX_SLOT == 0) |
268 | lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); | 268 | lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); |
269 | #if LJ_TARGET_WINDOWS | 269 | #if LJ_TARGET_WINDOWS |
@@ -280,7 +280,10 @@ static void callback_mcode_new(CTState *cts) | |||
280 | p = lj_mem_new(cts->L, sz); | 280 | p = lj_mem_new(cts->L, sz); |
281 | #endif | 281 | #endif |
282 | cts->cb.mcode = p; | 282 | cts->cb.mcode = p; |
283 | callback_mcode_init(cts->g, p); | 283 | pe = callback_mcode_init(cts->g, p); |
284 | UNUSED(pe); | ||
285 | lj_assertCTS((size_t)((char *)pe - (char *)p) <= sz, | ||
286 | "miscalculated CALLBACK_MAX_SLOT"); | ||
284 | lj_mcode_sync(p, (char *)p + sz); | 287 | lj_mcode_sync(p, (char *)p + sz); |
285 | #if LJ_TARGET_WINDOWS | 288 | #if LJ_TARGET_WINDOWS |
286 | { | 289 | { |
@@ -421,8 +424,9 @@ void lj_ccallback_mcode_free(CTState *cts) | |||
421 | 424 | ||
422 | #define CALLBACK_HANDLE_GPR \ | 425 | #define CALLBACK_HANDLE_GPR \ |
423 | if (n > 1) { \ | 426 | if (n > 1) { \ |
424 | lua_assert(((LJ_ABI_SOFTFP && ctype_isnum(cta->info)) || /* double. */ \ | 427 | lj_assertCTS(((LJ_ABI_SOFTFP && ctype_isnum(cta->info)) || /* double. */ \ |
425 | ctype_isinteger(cta->info)) && n == 2); /* int64_t. */ \ | 428 | ctype_isinteger(cta->info)) && n == 2, /* int64_t. */ \ |
429 | "bad GPR type"); \ | ||
426 | ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ | 430 | ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ |
427 | } \ | 431 | } \ |
428 | if (ngpr + n <= maxgpr) { \ | 432 | if (ngpr + n <= maxgpr) { \ |
@@ -579,7 +583,7 @@ static void callback_conv_args(CTState *cts, lua_State *L) | |||
579 | CTSize sz; | 583 | CTSize sz; |
580 | int isfp; | 584 | int isfp; |
581 | MSize n; | 585 | MSize n; |
582 | lua_assert(ctype_isfield(ctf->info)); | 586 | lj_assertCTS(ctype_isfield(ctf->info), "field expected"); |
583 | cta = ctype_rawchild(cts, ctf); | 587 | cta = ctype_rawchild(cts, ctf); |
584 | isfp = ctype_isfp(cta->info); | 588 | isfp = ctype_isfp(cta->info); |
585 | sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); | 589 | sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); |
@@ -671,7 +675,7 @@ lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf) | |||
671 | { | 675 | { |
672 | lua_State *L = cts->L; | 676 | lua_State *L = cts->L; |
673 | global_State *g = cts->g; | 677 | global_State *g = cts->g; |
674 | lua_assert(L != NULL); | 678 | lj_assertG(L != NULL, "uninitialized cts->L in callback"); |
675 | if (tvref(g->jit_base)) { | 679 | if (tvref(g->jit_base)) { |
676 | setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK)); | 680 | setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK)); |
677 | if (g->panic) g->panic(L); | 681 | if (g->panic) g->panic(L); |
@@ -756,7 +760,7 @@ static CType *callback_checkfunc(CTState *cts, CType *ct) | |||
756 | CType *ctf = ctype_get(cts, fid); | 760 | CType *ctf = ctype_get(cts, fid); |
757 | if (!ctype_isattrib(ctf->info)) { | 761 | if (!ctype_isattrib(ctf->info)) { |
758 | CType *cta; | 762 | CType *cta; |
759 | lua_assert(ctype_isfield(ctf->info)); | 763 | lj_assertCTS(ctype_isfield(ctf->info), "field expected"); |
760 | cta = ctype_rawchild(cts, ctf); | 764 | cta = ctype_rawchild(cts, ctf); |
761 | if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) || | 765 | if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) || |
762 | (ctype_isnum(cta->info) && cta->size <= 8)) || | 766 | (ctype_isnum(cta->info) && cta->size <= 8)) || |