FFI: Fix dangling CType references.
Reported by Sergey Kaplun. Collateral of #1360
This commit is contained in:
@@ -623,7 +623,9 @@ noth: /* Not a homogeneous float/double aggregate. */
|
|||||||
|
|
||||||
/* -- Common C call handling ---------------------------------------------- */
|
/* -- Common C call handling ---------------------------------------------- */
|
||||||
|
|
||||||
/* Infer the destination CTypeID for a vararg argument. */
|
/* Infer the destination CTypeID for a vararg argument.
|
||||||
|
** Note: may reallocate cts->tab and invalidate CType pointers.
|
||||||
|
*/
|
||||||
CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o)
|
CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o)
|
||||||
{
|
{
|
||||||
if (tvisnumber(o)) {
|
if (tvisnumber(o)) {
|
||||||
@@ -651,13 +653,16 @@ CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Setup arguments for C call. */
|
/* Setup arguments for C call.
|
||||||
|
** Note: may reallocate cts->tab and invalidate CType pointers.
|
||||||
|
*/
|
||||||
static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
|
static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
|
||||||
CCallState *cc)
|
CCallState *cc)
|
||||||
{
|
{
|
||||||
int gcsteps = 0;
|
int gcsteps = 0;
|
||||||
TValue *o, *top = L->top;
|
TValue *o, *top = L->top;
|
||||||
CTypeID fid;
|
CTypeID fid;
|
||||||
|
CTInfo info = ct->info; /* lj_ccall_ctid_vararg may invalidate ct pointer. */
|
||||||
CType *ctr;
|
CType *ctr;
|
||||||
MSize maxgpr, ngpr = 0, nsp = 0, narg;
|
MSize maxgpr, ngpr = 0, nsp = 0, narg;
|
||||||
#if CCALL_NARG_FPR
|
#if CCALL_NARG_FPR
|
||||||
@@ -676,7 +681,7 @@ static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
|
|||||||
#if LJ_TARGET_X86
|
#if LJ_TARGET_X86
|
||||||
/* x86 has several different calling conventions. */
|
/* x86 has several different calling conventions. */
|
||||||
cc->resx87 = 0;
|
cc->resx87 = 0;
|
||||||
switch (ctype_cconv(ct->info)) {
|
switch (ctype_cconv(info)) {
|
||||||
case CTCC_FASTCALL: maxgpr = 2; break;
|
case CTCC_FASTCALL: maxgpr = 2; break;
|
||||||
case CTCC_THISCALL: maxgpr = 1; break;
|
case CTCC_THISCALL: maxgpr = 1; break;
|
||||||
default: maxgpr = 0; break;
|
default: maxgpr = 0; break;
|
||||||
@@ -693,7 +698,7 @@ static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
|
|||||||
} else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) {
|
} else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) {
|
||||||
/* Preallocate cdata object and anchor it after arguments. */
|
/* Preallocate cdata object and anchor it after arguments. */
|
||||||
CTSize sz = ctr->size;
|
CTSize sz = ctr->size;
|
||||||
GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz);
|
GCcdata *cd = lj_cdata_new(cts, ctype_cid(info), sz);
|
||||||
void *dp = cdataptr(cd);
|
void *dp = cdataptr(cd);
|
||||||
setcdataV(L, L->top++, cd);
|
setcdataV(L, L->top++, cd);
|
||||||
if (ctype_isstruct(ctr->info)) {
|
if (ctype_isstruct(ctr->info)) {
|
||||||
@@ -729,7 +734,7 @@ static int ccall_set_args(lua_State *L, CTState *cts, CType *ct,
|
|||||||
lua_assert(ctype_isfield(ctf->info));
|
lua_assert(ctype_isfield(ctf->info));
|
||||||
did = ctype_cid(ctf->info);
|
did = ctype_cid(ctf->info);
|
||||||
} else {
|
} else {
|
||||||
if (!(ct->info & CTF_VARARG))
|
if (!(info & CTF_VARARG))
|
||||||
lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too many arguments. */
|
lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too many arguments. */
|
||||||
did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */
|
did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */
|
||||||
isva = 1;
|
isva = 1;
|
||||||
@@ -869,11 +874,11 @@ int lj_ccall_func(lua_State *L, GCcdata *cd)
|
|||||||
ct = ctype_rawchild(cts, ct);
|
ct = ctype_rawchild(cts, ct);
|
||||||
}
|
}
|
||||||
if (ctype_isfunc(ct->info)) {
|
if (ctype_isfunc(ct->info)) {
|
||||||
|
CTypeID id = ctype_typeid(cts, ct);
|
||||||
CCallState cc;
|
CCallState cc;
|
||||||
int gcsteps, ret;
|
int gcsteps, ret;
|
||||||
cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz);
|
cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz);
|
||||||
gcsteps = ccall_set_args(L, cts, ct, &cc);
|
gcsteps = ccall_set_args(L, cts, ct, &cc);
|
||||||
ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab);
|
|
||||||
cts->cb.slot = ~0u;
|
cts->cb.slot = ~0u;
|
||||||
lj_vm_ffi_call(&cc);
|
lj_vm_ffi_call(&cc);
|
||||||
if (cts->cb.slot != ~0u) { /* Blacklist function that called a callback. */
|
if (cts->cb.slot != ~0u) { /* Blacklist function that called a callback. */
|
||||||
@@ -881,7 +886,7 @@ int lj_ccall_func(lua_State *L, GCcdata *cd)
|
|||||||
setlightudV(&tv, (void *)cc.func);
|
setlightudV(&tv, (void *)cc.func);
|
||||||
setboolV(lj_tab_set(L, cts->miscmap, &tv), 1);
|
setboolV(lj_tab_set(L, cts->miscmap, &tv), 1);
|
||||||
}
|
}
|
||||||
ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab); /* May be reallocated. */
|
ct = ctype_get(cts, id); /* Table may have been reallocated. */
|
||||||
gcsteps += ccall_get_results(L, cts, ct, &cc, &ret);
|
gcsteps += ccall_get_results(L, cts, ct, &cc, &ret);
|
||||||
#if LJ_TARGET_X86 && LJ_ABI_WIN
|
#if LJ_TARGET_X86 && LJ_ABI_WIN
|
||||||
/* Automatically detect __stdcall and fix up C function declaration. */
|
/* Automatically detect __stdcall and fix up C function declaration. */
|
||||||
|
|||||||
@@ -988,12 +988,15 @@ static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Record argument conversions. */
|
/* Record argument conversions.
|
||||||
|
** Note: may reallocate cts->tab and invalidate CType pointers.
|
||||||
|
*/
|
||||||
static TRef crec_call_args(jit_State *J, RecordFFData *rd,
|
static TRef crec_call_args(jit_State *J, RecordFFData *rd,
|
||||||
CTState *cts, CType *ct)
|
CTState *cts, CType *ct)
|
||||||
{
|
{
|
||||||
TRef args[CCI_NARGS_MAX];
|
TRef args[CCI_NARGS_MAX];
|
||||||
CTypeID fid;
|
CTypeID fid;
|
||||||
|
CTInfo info = ct->info; /* lj_ccall_ctid_vararg may invalidate ct pointer. */
|
||||||
MSize i, n;
|
MSize i, n;
|
||||||
TRef tr, *base;
|
TRef tr, *base;
|
||||||
cTValue *o;
|
cTValue *o;
|
||||||
@@ -1002,9 +1005,9 @@ static TRef crec_call_args(jit_State *J, RecordFFData *rd,
|
|||||||
TRef *arg0 = NULL, *arg1 = NULL;
|
TRef *arg0 = NULL, *arg1 = NULL;
|
||||||
#endif
|
#endif
|
||||||
int ngpr = 0;
|
int ngpr = 0;
|
||||||
if (ctype_cconv(ct->info) == CTCC_THISCALL)
|
if (ctype_cconv(info) == CTCC_THISCALL)
|
||||||
ngpr = 1;
|
ngpr = 1;
|
||||||
else if (ctype_cconv(ct->info) == CTCC_FASTCALL)
|
else if (ctype_cconv(info) == CTCC_FASTCALL)
|
||||||
ngpr = 2;
|
ngpr = 2;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -1029,7 +1032,7 @@ static TRef crec_call_args(jit_State *J, RecordFFData *rd,
|
|||||||
lua_assert(ctype_isfield(ctf->info));
|
lua_assert(ctype_isfield(ctf->info));
|
||||||
did = ctype_cid(ctf->info);
|
did = ctype_cid(ctf->info);
|
||||||
} else {
|
} else {
|
||||||
if (!(ct->info & CTF_VARARG))
|
if (!(info & CTF_VARARG))
|
||||||
lj_trace_err(J, LJ_TRERR_NYICALL); /* Too many arguments. */
|
lj_trace_err(J, LJ_TRERR_NYICALL); /* Too many arguments. */
|
||||||
did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */
|
did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */
|
||||||
}
|
}
|
||||||
@@ -1112,12 +1115,14 @@ static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd)
|
|||||||
{
|
{
|
||||||
CTState *cts = ctype_ctsG(J2G(J));
|
CTState *cts = ctype_ctsG(J2G(J));
|
||||||
CType *ct = ctype_raw(cts, cd->ctypeid);
|
CType *ct = ctype_raw(cts, cd->ctypeid);
|
||||||
|
CTInfo info;
|
||||||
IRType tp = IRT_PTR;
|
IRType tp = IRT_PTR;
|
||||||
if (ctype_isptr(ct->info)) {
|
if (ctype_isptr(ct->info)) {
|
||||||
tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
|
tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
|
||||||
ct = ctype_rawchild(cts, ct);
|
ct = ctype_rawchild(cts, ct);
|
||||||
}
|
}
|
||||||
if (ctype_isfunc(ct->info)) {
|
info = ct->info; /* crec_call_args may invalidate ct pointer. */
|
||||||
|
if (ctype_isfunc(info)) {
|
||||||
TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR);
|
TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR);
|
||||||
CType *ctr = ctype_rawchild(cts, ct);
|
CType *ctr = ctype_rawchild(cts, ct);
|
||||||
IRType t = crec_ct2irt(cts, ctr);
|
IRType t = crec_ct2irt(cts, ctr);
|
||||||
@@ -1135,9 +1140,9 @@ static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd)
|
|||||||
ctype_isenum(ctr->info)) || t == IRT_CDATA) {
|
ctype_isenum(ctr->info)) || t == IRT_CDATA) {
|
||||||
lj_trace_err(J, LJ_TRERR_NYICALL);
|
lj_trace_err(J, LJ_TRERR_NYICALL);
|
||||||
}
|
}
|
||||||
if ((ct->info & CTF_VARARG)
|
if ((info & CTF_VARARG)
|
||||||
#if LJ_TARGET_X86
|
#if LJ_TARGET_X86
|
||||||
|| ctype_cconv(ct->info) != CTCC_CDECL
|
|| ctype_cconv(info) != CTCC_CDECL
|
||||||
#endif
|
#endif
|
||||||
)
|
)
|
||||||
func = emitir(IRT(IR_CARG, IRT_NIL), func,
|
func = emitir(IRT(IR_CARG, IRT_NIL), func,
|
||||||
@@ -1160,7 +1165,7 @@ static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd)
|
|||||||
}
|
}
|
||||||
} else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) ||
|
} else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) ||
|
||||||
t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) {
|
t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) {
|
||||||
TRef trid = lj_ir_kint(J, ctype_cid(ct->info));
|
TRef trid = lj_ir_kint(J, ctype_cid(info));
|
||||||
tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr);
|
tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr);
|
||||||
if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
|
if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
|
||||||
} else if (t == IRT_FLOAT || t == IRT_U32) {
|
} else if (t == IRT_FLOAT || t == IRT_U32) {
|
||||||
|
|||||||
Reference in New Issue
Block a user