Improve assertions.

This commit is contained in:
Mike Pall
2020-06-13 00:52:54 +02:00
parent 8b55054473
commit 8ae5170cdc
71 changed files with 1363 additions and 927 deletions

View File

@@ -107,9 +107,9 @@ MSize lj_ccallback_ptr2slot(CTState *cts, void *p)
/* Initialize machine code for callback function pointers. */
#if LJ_OS_NOJIT
/* Disabled callback support. */
#define callback_mcode_init(g, p) UNUSED(p)
#define callback_mcode_init(g, p) (p)
#elif LJ_TARGET_X86ORX64
static void callback_mcode_init(global_State *g, uint8_t *page)
static void *callback_mcode_init(global_State *g, uint8_t *page)
{
uint8_t *p = page;
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)
*p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2);
}
}
lua_assert(p - page <= CALLBACK_MCODE_SIZE);
return p;
}
#elif LJ_TARGET_ARM
static void callback_mcode_init(global_State *g, uint32_t *page)
static void *callback_mcode_init(global_State *g, uint32_t *page)
{
uint32_t *p = page;
void *target = (void *)lj_vm_ffi_callback;
@@ -165,10 +165,10 @@ static void callback_mcode_init(global_State *g, uint32_t *page)
*p = ARMI_B | ((page-p-2) & 0x00ffffffu);
p++;
}
lua_assert(p - page <= CALLBACK_MCODE_SIZE);
return p;
}
#elif LJ_TARGET_ARM64
static void callback_mcode_init(global_State *g, uint32_t *page)
static void *callback_mcode_init(global_State *g, uint32_t *page)
{
uint32_t *p = page;
void *target = (void *)lj_vm_ffi_callback;
@@ -185,10 +185,10 @@ static void callback_mcode_init(global_State *g, uint32_t *page)
*p = A64I_LE(A64I_B | A64F_S26((page-p) & 0x03ffffffu));
p++;
}
lua_assert(p - page <= CALLBACK_MCODE_SIZE);
return p;
}
#elif LJ_TARGET_PPC
static void callback_mcode_init(global_State *g, uint32_t *page)
static void *callback_mcode_init(global_State *g, uint32_t *page)
{
uint32_t *p = page;
void *target = (void *)lj_vm_ffi_callback;
@@ -204,10 +204,10 @@ static void callback_mcode_init(global_State *g, uint32_t *page)
*p = PPCI_B | (((page-p) & 0x00ffffffu) << 2);
p++;
}
lua_assert(p - page <= CALLBACK_MCODE_SIZE);
return p;
}
#elif LJ_TARGET_MIPS
static void callback_mcode_init(global_State *g, uint32_t *page)
static void *callback_mcode_init(global_State *g, uint32_t *page)
{
uint32_t *p = page;
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)
p++;
*p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot;
}
lua_assert(p - page <= CALLBACK_MCODE_SIZE);
return p;
}
#else
/* Missing support for this architecture. */
#define callback_mcode_init(g, p) UNUSED(p)
#define callback_mcode_init(g, p) (p)
#endif
/* -- Machine code management --------------------------------------------- */
@@ -263,7 +263,7 @@ static void callback_mcode_init(global_State *g, uint32_t *page)
static void callback_mcode_new(CTState *cts)
{
size_t sz = (size_t)CALLBACK_MCODE_SIZE;
void *p;
void *p, *pe;
if (CALLBACK_MAX_SLOT == 0)
lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV);
#if LJ_TARGET_WINDOWS
@@ -280,7 +280,10 @@ static void callback_mcode_new(CTState *cts)
p = lj_mem_new(cts->L, sz);
#endif
cts->cb.mcode = p;
callback_mcode_init(cts->g, p);
pe = callback_mcode_init(cts->g, p);
UNUSED(pe);
lj_assertCTS((size_t)((char *)pe - (char *)p) <= sz,
"miscalculated CALLBACK_MAX_SLOT");
lj_mcode_sync(p, (char *)p + sz);
#if LJ_TARGET_WINDOWS
{
@@ -421,8 +424,9 @@ void lj_ccallback_mcode_free(CTState *cts)
#define CALLBACK_HANDLE_GPR \
if (n > 1) { \
lua_assert(((LJ_ABI_SOFTFP && ctype_isnum(cta->info)) || /* double. */ \
ctype_isinteger(cta->info)) && n == 2); /* int64_t. */ \
lj_assertCTS(((LJ_ABI_SOFTFP && ctype_isnum(cta->info)) || /* double. */ \
ctype_isinteger(cta->info)) && n == 2, /* int64_t. */ \
"bad GPR type"); \
ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \
} \
if (ngpr + n <= maxgpr) { \
@@ -579,7 +583,7 @@ static void callback_conv_args(CTState *cts, lua_State *L)
CTSize sz;
int isfp;
MSize n;
lua_assert(ctype_isfield(ctf->info));
lj_assertCTS(ctype_isfield(ctf->info), "field expected");
cta = ctype_rawchild(cts, ctf);
isfp = ctype_isfp(cta->info);
sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1);
@@ -671,7 +675,7 @@ lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf)
{
lua_State *L = cts->L;
global_State *g = cts->g;
lua_assert(L != NULL);
lj_assertG(L != NULL, "uninitialized cts->L in callback");
if (tvref(g->jit_base)) {
setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK));
if (g->panic) g->panic(L);
@@ -756,7 +760,7 @@ static CType *callback_checkfunc(CTState *cts, CType *ct)
CType *ctf = ctype_get(cts, fid);
if (!ctype_isattrib(ctf->info)) {
CType *cta;
lua_assert(ctype_isfield(ctf->info));
lj_assertCTS(ctype_isfield(ctf->info), "field expected");
cta = ctype_rawchild(cts, ctf);
if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) ||
(ctype_isnum(cta->info) && cta->size <= 8)) ||