1 /*
2 ** C type conversions.
3 ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
4 */
5 
6 #include "lj_obj.h"
7 
8 #if LJ_HASFFI
9 
10 #include "lj_err.h"
11 #include "lj_buf.h"
12 #include "lj_tab.h"
13 #include "lj_ctype.h"
14 #include "lj_cdata.h"
15 #include "lj_cconv.h"
16 #include "lj_ccallback.h"
17 
18 /* -- Conversion errors --------------------------------------------------- */
19 
20 /* Bad conversion. */
cconv_err_conv(CTState * cts,CType * d,CType * s,CTInfo flags)21 LJ_NORET static void cconv_err_conv(CTState *cts, CType *d, CType *s,
22 				    CTInfo flags)
23 {
24   const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
25   const char *src;
26   if ((flags & CCF_FROMTV))
27     src = lj_obj_typename[1+(ctype_isnum(s->info) ? LUA_TNUMBER :
28 			     ctype_isarray(s->info) ? LUA_TSTRING : LUA_TNIL)];
29   else
30     src = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, s), NULL));
31   if (CCF_GETARG(flags))
32     lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);
33   else
34     lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);
35 }
36 
37 /* Bad conversion from TValue. */
cconv_err_convtv(CTState * cts,CType * d,TValue * o,CTInfo flags)38 LJ_NORET static void cconv_err_convtv(CTState *cts, CType *d, TValue *o,
39 				      CTInfo flags)
40 {
41   const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
42   const char *src = lj_typename(o);
43   if (CCF_GETARG(flags))
44     lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst);
45   else
46     lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst);
47 }
48 
49 /* Initializer overflow. */
cconv_err_initov(CTState * cts,CType * d)50 LJ_NORET static void cconv_err_initov(CTState *cts, CType *d)
51 {
52   const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL));
53   lj_err_callerv(cts->L, LJ_ERR_FFI_INITOV, dst);
54 }
55 
56 /* -- C type compatibility checks ----------------------------------------- */
57 
58 /* Get raw type and qualifiers for a child type. Resolves enums, too. */
cconv_childqual(CTState * cts,CType * ct,CTInfo * qual)59 static CType *cconv_childqual(CTState *cts, CType *ct, CTInfo *qual)
60 {
61   ct = ctype_child(cts, ct);
62   for (;;) {
63     if (ctype_isattrib(ct->info)) {
64       if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;
65     } else if (!ctype_isenum(ct->info)) {
66       break;
67     }
68     ct = ctype_child(cts, ct);
69   }
70   *qual |= (ct->info & CTF_QUAL);
71   return ct;
72 }
73 
74 /* Check for compatible types when converting to a pointer.
75 ** Note: these checks are more relaxed than what C99 mandates.
76 */
lj_cconv_compatptr(CTState * cts,CType * d,CType * s,CTInfo flags)77 int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags)
78 {
79   if (!((flags & CCF_CAST) || d == s)) {
80     CTInfo dqual = 0, squal = 0;
81     d = cconv_childqual(cts, d, &dqual);
82     if (!ctype_isstruct(s->info))
83       s = cconv_childqual(cts, s, &squal);
84     if ((flags & CCF_SAME)) {
85       if (dqual != squal)
86 	return 0;  /* Different qualifiers. */
87     } else if (!(flags & CCF_IGNQUAL)) {
88       if ((dqual & squal) != squal)
89 	return 0;  /* Discarded qualifiers. */
90       if (ctype_isvoid(d->info) || ctype_isvoid(s->info))
91 	return 1;  /* Converting to/from void * is always ok. */
92     }
93     if (ctype_type(d->info) != ctype_type(s->info) ||
94 	d->size != s->size)
95       return 0;  /* Different type or different size. */
96     if (ctype_isnum(d->info)) {
97       if (((d->info ^ s->info) & (CTF_BOOL|CTF_FP)))
98 	return 0;  /* Different numeric types. */
99     } else if (ctype_ispointer(d->info)) {
100       /* Check child types for compatibility. */
101       return lj_cconv_compatptr(cts, d, s, flags|CCF_SAME);
102     } else if (ctype_isstruct(d->info)) {
103       if (d != s)
104 	return 0;  /* Must be exact same type for struct/union. */
105     } else if (ctype_isfunc(d->info)) {
106       /* NYI: structural equality of functions. */
107     }
108   }
109   return 1;  /* Types are compatible. */
110 }
111 
112 /* -- C type to C type conversion ----------------------------------------- */
113 
114 /* Convert C type to C type. Caveat: expects to get the raw CType!
115 **
116 ** Note: This is only used by the interpreter and not optimized at all.
117 ** The JIT compiler will do a much better job specializing for each case.
118 */
lj_cconv_ct_ct(CTState * cts,CType * d,CType * s,uint8_t * dp,uint8_t * sp,CTInfo flags)119 void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s,
120 		    uint8_t *dp, uint8_t *sp, CTInfo flags)
121 {
122   CTSize dsize = d->size, ssize = s->size;
123   CTInfo dinfo = d->info, sinfo = s->info;
124   void *tmpptr;
125 
126   lj_assertCTS(!ctype_isenum(dinfo) && !ctype_isenum(sinfo),
127 	       "unresolved enum");
128   lj_assertCTS(!ctype_isattrib(dinfo) && !ctype_isattrib(sinfo),
129 	       "unstripped attribute");
130 
131   if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
132     goto err_conv;
133 
134   /* Some basic sanity checks. */
135   lj_assertCTS(!ctype_isnum(dinfo) || dsize > 0, "bad size for number type");
136   lj_assertCTS(!ctype_isnum(sinfo) || ssize > 0, "bad size for number type");
137   lj_assertCTS(!ctype_isbool(dinfo) || dsize == 1 || dsize == 4,
138 	       "bad size for bool type");
139   lj_assertCTS(!ctype_isbool(sinfo) || ssize == 1 || ssize == 4,
140 	       "bad size for bool type");
141   lj_assertCTS(!ctype_isinteger(dinfo) || (1u<<lj_fls(dsize)) == dsize,
142 	       "bad size for integer type");
143   lj_assertCTS(!ctype_isinteger(sinfo) || (1u<<lj_fls(ssize)) == ssize,
144 	       "bad size for integer type");
145 
146   switch (cconv_idx2(dinfo, sinfo)) {
147   /* Destination is a bool. */
148   case CCX(B, B):
149     /* Source operand is already normalized. */
150     if (dsize == 1) *dp = *sp; else *(int *)dp = *sp;
151     break;
152   case CCX(B, I): {
153     MSize i;
154     uint8_t b = 0;
155     for (i = 0; i < ssize; i++) b |= sp[i];
156     b = (b != 0);
157     if (dsize == 1) *dp = b; else *(int *)dp = b;
158     break;
159     }
160   case CCX(B, F): {
161     uint8_t b;
162     if (ssize == sizeof(double)) b = (*(double *)sp != 0);
163     else if (ssize == sizeof(float)) b = (*(float *)sp != 0);
164     else goto err_conv;  /* NYI: long double. */
165     if (dsize == 1) *dp = b; else *(int *)dp = b;
166     break;
167     }
168 
169   /* Destination is an integer. */
170   case CCX(I, B):
171   case CCX(I, I):
172   conv_I_I:
173     if (dsize > ssize) {  /* Zero-extend or sign-extend LSB. */
174 #if LJ_LE
175       uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[ssize-1]&0x80)) ? 0xff : 0;
176       memcpy(dp, sp, ssize);
177       memset(dp + ssize, fill, dsize-ssize);
178 #else
179       uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[0]&0x80)) ? 0xff : 0;
180       memset(dp, fill, dsize-ssize);
181       memcpy(dp + (dsize-ssize), sp, ssize);
182 #endif
183     } else {  /* Copy LSB. */
184 #if LJ_LE
185       memcpy(dp, sp, dsize);
186 #else
187       memcpy(dp, sp + (ssize-dsize), dsize);
188 #endif
189     }
190     break;
191   case CCX(I, F): {
192     double n;  /* Always convert via double. */
193   conv_I_F:
194     /* Convert source to double. */
195     if (ssize == sizeof(double)) n = *(double *)sp;
196     else if (ssize == sizeof(float)) n = (double)*(float *)sp;
197     else goto err_conv;  /* NYI: long double. */
198     /* Then convert double to integer. */
199     /* The conversion must exactly match the semantics of JIT-compiled code! */
200     if (dsize < 4 || (dsize == 4 && !(dinfo & CTF_UNSIGNED))) {
201       int32_t i = (int32_t)n;
202       if (dsize == 4) *(int32_t *)dp = i;
203       else if (dsize == 2) *(int16_t *)dp = (int16_t)i;
204       else *(int8_t *)dp = (int8_t)i;
205     } else if (dsize == 4) {
206       *(uint32_t *)dp = (uint32_t)n;
207     } else if (dsize == 8) {
208       if (!(dinfo & CTF_UNSIGNED))
209 	*(int64_t *)dp = (int64_t)n;
210       else
211 	*(uint64_t *)dp = lj_num2u64(n);
212     } else {
213       goto err_conv;  /* NYI: conversion to >64 bit integers. */
214     }
215     break;
216     }
217   case CCX(I, C):
218     s = ctype_child(cts, s);
219     sinfo = s->info;
220     ssize = s->size;
221     goto conv_I_F;  /* Just convert re. */
222   case CCX(I, P):
223     if (!(flags & CCF_CAST)) goto err_conv;
224     sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
225     goto conv_I_I;
226   case CCX(I, A):
227     if (!(flags & CCF_CAST)) goto err_conv;
228     sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
229     ssize = CTSIZE_PTR;
230     tmpptr = sp;
231     sp = (uint8_t *)&tmpptr;
232     goto conv_I_I;
233 
234   /* Destination is a floating-point number. */
235   case CCX(F, B):
236   case CCX(F, I): {
237     double n;  /* Always convert via double. */
238   conv_F_I:
239     /* First convert source to double. */
240     /* The conversion must exactly match the semantics of JIT-compiled code! */
241     if (ssize < 4 || (ssize == 4 && !(sinfo & CTF_UNSIGNED))) {
242       int32_t i;
243       if (ssize == 4) {
244 	i = *(int32_t *)sp;
245       } else if (!(sinfo & CTF_UNSIGNED)) {
246 	if (ssize == 2) i = *(int16_t *)sp;
247 	else i = *(int8_t *)sp;
248       } else {
249 	if (ssize == 2) i = *(uint16_t *)sp;
250 	else i = *(uint8_t *)sp;
251       }
252       n = (double)i;
253     } else if (ssize == 4) {
254       n = (double)*(uint32_t *)sp;
255     } else if (ssize == 8) {
256       if (!(sinfo & CTF_UNSIGNED)) n = (double)*(int64_t *)sp;
257       else n = (double)*(uint64_t *)sp;
258     } else {
259       goto err_conv;  /* NYI: conversion from >64 bit integers. */
260     }
261     /* Convert double to destination. */
262     if (dsize == sizeof(double)) *(double *)dp = n;
263     else if (dsize == sizeof(float)) *(float *)dp = (float)n;
264     else goto err_conv;  /* NYI: long double. */
265     break;
266     }
267   case CCX(F, F): {
268     double n;  /* Always convert via double. */
269   conv_F_F:
270     if (ssize == dsize) goto copyval;
271     /* Convert source to double. */
272     if (ssize == sizeof(double)) n = *(double *)sp;
273     else if (ssize == sizeof(float)) n = (double)*(float *)sp;
274     else goto err_conv;  /* NYI: long double. */
275     /* Convert double to destination. */
276     if (dsize == sizeof(double)) *(double *)dp = n;
277     else if (dsize == sizeof(float)) *(float *)dp = (float)n;
278     else goto err_conv;  /* NYI: long double. */
279     break;
280     }
281   case CCX(F, C):
282     s = ctype_child(cts, s);
283     sinfo = s->info;
284     ssize = s->size;
285     goto conv_F_F;  /* Ignore im, and convert from re. */
286 
287   /* Destination is a complex number. */
288   case CCX(C, I):
289     d = ctype_child(cts, d);
290     dinfo = d->info;
291     dsize = d->size;
292     memset(dp + dsize, 0, dsize);  /* Clear im. */
293     goto conv_F_I;  /* Convert to re. */
294   case CCX(C, F):
295     d = ctype_child(cts, d);
296     dinfo = d->info;
297     dsize = d->size;
298     memset(dp + dsize, 0, dsize);  /* Clear im. */
299     goto conv_F_F;  /* Convert to re. */
300 
301   case CCX(C, C):
302     if (dsize != ssize) {  /* Different types: convert re/im separately. */
303       CType *dc = ctype_child(cts, d);
304       CType *sc = ctype_child(cts, s);
305       lj_cconv_ct_ct(cts, dc, sc, dp, sp, flags);
306       lj_cconv_ct_ct(cts, dc, sc, dp + dc->size, sp + sc->size, flags);
307       return;
308     }
309     goto copyval;  /* Otherwise this is easy. */
310 
311   /* Destination is a vector. */
312   case CCX(V, I):
313   case CCX(V, F):
314   case CCX(V, C): {
315     CType *dc = ctype_child(cts, d);
316     CTSize esize;
317     /* First convert the scalar to the first element. */
318     lj_cconv_ct_ct(cts, dc, s, dp, sp, flags);
319     /* Then replicate it to the other elements (splat). */
320     for (sp = dp, esize = dc->size; dsize > esize; dsize -= esize) {
321       dp += esize;
322       memcpy(dp, sp, esize);
323     }
324     break;
325     }
326 
327   case CCX(V, V):
328     /* Copy same-sized vectors, even for different lengths/element-types. */
329     if (dsize != ssize) goto err_conv;
330     goto copyval;
331 
332   /* Destination is a pointer. */
333   case CCX(P, I):
334     if (!(flags & CCF_CAST)) goto err_conv;
335     dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
336     goto conv_I_I;
337 
338   case CCX(P, F):
339     if (!(flags & CCF_CAST) || !(flags & CCF_FROMTV)) goto err_conv;
340     /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
341     dinfo = CTINFO(CT_NUM, (LJ_64 && dsize == 8) ? 0 : CTF_UNSIGNED);
342     goto conv_I_F;
343 
344   case CCX(P, P):
345     if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;
346     cdata_setptr(dp, dsize, cdata_getptr(sp, ssize));
347     break;
348 
349   case CCX(P, A):
350   case CCX(P, S):
351     if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv;
352     cdata_setptr(dp, dsize, sp);
353     break;
354 
355   /* Destination is an array. */
356   case CCX(A, A):
357     if ((flags & CCF_CAST) || (d->info & CTF_VLA) || dsize != ssize ||
358 	d->size == CTSIZE_INVALID || !lj_cconv_compatptr(cts, d, s, flags))
359       goto err_conv;
360     goto copyval;
361 
362   /* Destination is a struct/union. */
363   case CCX(S, S):
364     if ((flags & CCF_CAST) || (d->info & CTF_VLA) || d != s)
365       goto err_conv;  /* Must be exact same type. */
366 copyval:  /* Copy value. */
367     lj_assertCTS(dsize == ssize, "value copy with different sizes");
368     memcpy(dp, sp, dsize);
369     break;
370 
371   default:
372   err_conv:
373     cconv_err_conv(cts, d, s, flags);
374   }
375 }
376 
377 /* -- C type to TValue conversion ----------------------------------------- */
378 
379 /* Convert C type to TValue. Caveat: expects to get the raw CType! */
lj_cconv_tv_ct(CTState * cts,CType * s,CTypeID sid,TValue * o,uint8_t * sp)380 int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid,
381 		   TValue *o, uint8_t *sp)
382 {
383   CTInfo sinfo = s->info;
384   if (ctype_isnum(sinfo)) {
385     if (!ctype_isbool(sinfo)) {
386       if (ctype_isinteger(sinfo) && s->size > 4) goto copyval;
387       if (LJ_DUALNUM && ctype_isinteger(sinfo)) {
388 	int32_t i;
389 	lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT32), s,
390 		       (uint8_t *)&i, sp, 0);
391 	if ((sinfo & CTF_UNSIGNED) && i < 0)
392 	  setnumV(o, (lua_Number)(uint32_t)i);
393 	else
394 	  setintV(o, i);
395       } else {
396 	lj_cconv_ct_ct(cts, ctype_get(cts, CTID_DOUBLE), s,
397 		       (uint8_t *)&o->n, sp, 0);
398 	/* Numbers are NOT canonicalized here! Beware of uninitialized data. */
399 	lj_assertCTS(tvisnum(o), "non-canonical NaN passed");
400       }
401     } else {
402       uint32_t b = s->size == 1 ? (*sp != 0) : (*(int *)sp != 0);
403       setboolV(o, b);
404       setboolV(&cts->g->tmptv2, b);  /* Remember for trace recorder. */
405     }
406     return 0;
407   } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
408     /* Create reference. */
409     setcdataV(cts->L, o, lj_cdata_newref(cts, sp, sid));
410     return 1;  /* Need GC step. */
411   } else {
412     GCcdata *cd;
413     CTSize sz;
414   copyval:  /* Copy value. */
415     sz = s->size;
416     lj_assertCTS(sz != CTSIZE_INVALID, "value copy with invalid size");
417     /* Attributes are stripped, qualifiers are kept (but mostly ignored). */
418     cd = lj_cdata_new(cts, ctype_typeid(cts, s), sz);
419     setcdataV(cts->L, o, cd);
420     memcpy(cdataptr(cd), sp, sz);
421     return 1;  /* Need GC step. */
422   }
423 }
424 
425 /* Convert bitfield to TValue. */
lj_cconv_tv_bf(CTState * cts,CType * s,TValue * o,uint8_t * sp)426 int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp)
427 {
428   CTInfo info = s->info;
429   CTSize pos, bsz;
430   uint32_t val;
431   lj_assertCTS(ctype_isbitfield(info), "bitfield expected");
432   /* NYI: packed bitfields may cause misaligned reads. */
433   switch (ctype_bitcsz(info)) {
434   case 4: val = *(uint32_t *)sp; break;
435   case 2: val = *(uint16_t *)sp; break;
436   case 1: val = *(uint8_t *)sp; break;
437   default:
438     lj_assertCTS(0, "bad bitfield container size %d", ctype_bitcsz(info));
439     val = 0;
440     break;
441   }
442   /* Check if a packed bitfield crosses a container boundary. */
443   pos = ctype_bitpos(info);
444   bsz = ctype_bitbsz(info);
445   lj_assertCTS(pos < 8*ctype_bitcsz(info), "bad bitfield position");
446   lj_assertCTS(bsz > 0 && bsz <= 8*ctype_bitcsz(info), "bad bitfield size");
447   if (pos + bsz > 8*ctype_bitcsz(info))
448     lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);
449   if (!(info & CTF_BOOL)) {
450     CTSize shift = 32 - bsz;
451     if (!(info & CTF_UNSIGNED)) {
452       setintV(o, (int32_t)(val << (shift-pos)) >> shift);
453     } else {
454       val = (val << (shift-pos)) >> shift;
455       if (!LJ_DUALNUM || (int32_t)val < 0)
456 	setnumV(o, (lua_Number)(uint32_t)val);
457       else
458 	setintV(o, (int32_t)val);
459     }
460   } else {
461     uint32_t b = (val >> pos) & 1;
462     lj_assertCTS(bsz == 1, "bad bool bitfield size");
463     setboolV(o, b);
464     setboolV(&cts->g->tmptv2, b);  /* Remember for trace recorder. */
465   }
466   return 0;  /* No GC step needed. */
467 }
468 
469 /* -- TValue to C type conversion ----------------------------------------- */
470 
471 /* Convert table to array. */
cconv_array_tab(CTState * cts,CType * d,uint8_t * dp,GCtab * t,CTInfo flags)472 static void cconv_array_tab(CTState *cts, CType *d,
473 			    uint8_t *dp, GCtab *t, CTInfo flags)
474 {
475   int32_t i;
476   CType *dc = ctype_rawchild(cts, d);  /* Array element type. */
477   CTSize size = d->size, esize = dc->size, ofs = 0;
478   for (i = 0; ; i++) {
479     TValue *tv = (TValue *)lj_tab_getint(t, i);
480     if (!tv || tvisnil(tv)) {
481       if (i == 0) continue;  /* Try again for 1-based tables. */
482       break;  /* Stop at first nil. */
483     }
484     if (ofs >= size)
485       cconv_err_initov(cts, d);
486     lj_cconv_ct_tv(cts, dc, dp + ofs, tv, flags);
487     ofs += esize;
488   }
489   if (size != CTSIZE_INVALID) {  /* Only fill up arrays with known size. */
490     if (ofs == esize) {  /* Replicate a single element. */
491       for (; ofs < size; ofs += esize) memcpy(dp + ofs, dp, esize);
492     } else {  /* Otherwise fill the remainder with zero. */
493       memset(dp + ofs, 0, size - ofs);
494     }
495   }
496 }
497 
498 /* Convert table to sub-struct/union. */
cconv_substruct_tab(CTState * cts,CType * d,uint8_t * dp,GCtab * t,int32_t * ip,CTInfo flags)499 static void cconv_substruct_tab(CTState *cts, CType *d, uint8_t *dp,
500 				GCtab *t, int32_t *ip, CTInfo flags)
501 {
502   CTypeID id = d->sib;
503   while (id) {
504     CType *df = ctype_get(cts, id);
505     id = df->sib;
506     if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {
507       TValue *tv;
508       int32_t i = *ip, iz = i;
509       if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */
510       if (i >= 0) {
511       retry:
512 	tv = (TValue *)lj_tab_getint(t, i);
513 	if (!tv || tvisnil(tv)) {
514 	  if (i == 0) { i = 1; goto retry; }  /* 1-based tables. */
515 	  if (iz == 0) { *ip = i = -1; goto tryname; }  /* Init named fields. */
516 	  break;  /* Stop at first nil. */
517 	}
518 	*ip = i + 1;
519       } else {
520       tryname:
521 	tv = (TValue *)lj_tab_getstr(t, gco2str(gcref(df->name)));
522 	if (!tv || tvisnil(tv)) continue;
523       }
524       if (ctype_isfield(df->info))
525 	lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, tv, flags);
526       else
527 	lj_cconv_bf_tv(cts, df, dp+df->size, tv);
528       if ((d->info & CTF_UNION)) break;
529     } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {
530       cconv_substruct_tab(cts, ctype_rawchild(cts, df),
531 			  dp+df->size, t, ip, flags);
532     }  /* Ignore all other entries in the chain. */
533   }
534 }
535 
536 /* Convert table to struct/union. */
cconv_struct_tab(CTState * cts,CType * d,uint8_t * dp,GCtab * t,CTInfo flags)537 static void cconv_struct_tab(CTState *cts, CType *d,
538 			     uint8_t *dp, GCtab *t, CTInfo flags)
539 {
540   int32_t i = 0;
541   memset(dp, 0, d->size);  /* Much simpler to clear the struct first. */
542   cconv_substruct_tab(cts, d, dp, t, &i, flags);
543 }
544 
545 /* Convert TValue to C type. Caveat: expects to get the raw CType! */
lj_cconv_ct_tv(CTState * cts,CType * d,uint8_t * dp,TValue * o,CTInfo flags)546 void lj_cconv_ct_tv(CTState *cts, CType *d,
547 		    uint8_t *dp, TValue *o, CTInfo flags)
548 {
549   CTypeID sid = CTID_P_VOID;
550   CType *s;
551   void *tmpptr;
552   uint8_t tmpbool, *sp = (uint8_t *)&tmpptr;
553   if (LJ_LIKELY(tvisint(o))) {
554     sp = (uint8_t *)&o->i;
555     sid = CTID_INT32;
556     flags |= CCF_FROMTV;
557   } else if (LJ_LIKELY(tvisnum(o))) {
558     sp = (uint8_t *)&o->n;
559     sid = CTID_DOUBLE;
560     flags |= CCF_FROMTV;
561   } else if (tviscdata(o)) {
562     sp = cdataptr(cdataV(o));
563     sid = cdataV(o)->ctypeid;
564     s = ctype_get(cts, sid);
565     if (ctype_isref(s->info)) {  /* Resolve reference for value. */
566       lj_assertCTS(s->size == CTSIZE_PTR, "ref is not pointer-sized");
567       sp = *(void **)sp;
568       sid = ctype_cid(s->info);
569     }
570     s = ctype_raw(cts, sid);
571     if (ctype_isfunc(s->info)) {
572       CTypeID did = ctype_typeid(cts, d);
573       sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);
574       d = ctype_get(cts, did);  /* cts->tab may have been reallocated. */
575     } else {
576       if (ctype_isenum(s->info)) s = ctype_child(cts, s);
577       goto doconv;
578     }
579   } else if (tvisstr(o)) {
580     GCstr *str = strV(o);
581     if (ctype_isenum(d->info)) {  /* Match string against enum constant. */
582       CTSize ofs;
583       CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
584       if (!cct || !ctype_isconstval(cct->info))
585 	goto err_conv;
586       lj_assertCTS(d->size == 4, "only 32 bit enum supported");  /* NYI */
587       sp = (uint8_t *)&cct->size;
588       sid = ctype_cid(cct->info);
589     } else if (ctype_isrefarray(d->info)) {  /* Copy string to array. */
590       CType *dc = ctype_rawchild(cts, d);
591       CTSize sz = str->len+1;
592       if (!ctype_isinteger(dc->info) || dc->size != 1)
593 	goto err_conv;
594       if (d->size != 0 && d->size < sz)
595 	sz = d->size;
596       memcpy(dp, strdata(str), sz);
597       return;
598     } else {  /* Otherwise pass it as a const char[]. */
599       sp = (uint8_t *)strdata(str);
600       sid = CTID_A_CCHAR;
601       flags |= CCF_FROMTV;
602     }
603   } else if (tvistab(o)) {
604     if (ctype_isarray(d->info)) {
605       cconv_array_tab(cts, d, dp, tabV(o), flags);
606       return;
607     } else if (ctype_isstruct(d->info)) {
608       cconv_struct_tab(cts, d, dp, tabV(o), flags);
609       return;
610     } else {
611       goto err_conv;
612     }
613   } else if (tvisbool(o)) {
614     tmpbool = boolV(o);
615     sp = &tmpbool;
616     sid = CTID_BOOL;
617   } else if (tvisnil(o)) {
618     tmpptr = (void *)0;
619     flags |= CCF_FROMTV;
620   } else if (tvisudata(o)) {
621     GCudata *ud = udataV(o);
622     tmpptr = uddata(ud);
623     if (ud->udtype == UDTYPE_IO_FILE)
624       tmpptr = *(void **)tmpptr;
625     else if (ud->udtype == UDTYPE_BUFFER)
626       tmpptr = ((SBufExt *)tmpptr)->r;
627   } else if (tvislightud(o)) {
628     tmpptr = lightudV(cts->g, o);
629   } else if (tvisfunc(o)) {
630     void *p = lj_ccallback_new(cts, d, funcV(o));
631     if (p) {
632       *(void **)dp = p;
633       return;
634     }
635     goto err_conv;
636   } else {
637   err_conv:
638     cconv_err_convtv(cts, d, o, flags);
639   }
640   s = ctype_get(cts, sid);
641 doconv:
642   if (ctype_isenum(d->info)) d = ctype_child(cts, d);
643   lj_cconv_ct_ct(cts, d, s, dp, sp, flags);
644 }
645 
646 /* Convert TValue to bitfield. */
lj_cconv_bf_tv(CTState * cts,CType * d,uint8_t * dp,TValue * o)647 void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o)
648 {
649   CTInfo info = d->info;
650   CTSize pos, bsz;
651   uint32_t val, mask;
652   lj_assertCTS(ctype_isbitfield(info), "bitfield expected");
653   if ((info & CTF_BOOL)) {
654     uint8_t tmpbool;
655     lj_assertCTS(ctype_bitbsz(info) == 1, "bad bool bitfield size");
656     lj_cconv_ct_tv(cts, ctype_get(cts, CTID_BOOL), &tmpbool, o, 0);
657     val = tmpbool;
658   } else {
659     CTypeID did = (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32;
660     lj_cconv_ct_tv(cts, ctype_get(cts, did), (uint8_t *)&val, o, 0);
661   }
662   pos = ctype_bitpos(info);
663   bsz = ctype_bitbsz(info);
664   lj_assertCTS(pos < 8*ctype_bitcsz(info), "bad bitfield position");
665   lj_assertCTS(bsz > 0 && bsz <= 8*ctype_bitcsz(info), "bad bitfield size");
666   /* Check if a packed bitfield crosses a container boundary. */
667   if (pos + bsz > 8*ctype_bitcsz(info))
668     lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT);
669   mask = ((1u << bsz) - 1u) << pos;
670   val = (val << pos) & mask;
671   /* NYI: packed bitfields may cause misaligned reads/writes. */
672   switch (ctype_bitcsz(info)) {
673   case 4: *(uint32_t *)dp = (*(uint32_t *)dp & ~mask) | (uint32_t)val; break;
674   case 2: *(uint16_t *)dp = (*(uint16_t *)dp & ~mask) | (uint16_t)val; break;
675   case 1: *(uint8_t *)dp = (*(uint8_t *)dp & ~mask) | (uint8_t)val; break;
676   default:
677     lj_assertCTS(0, "bad bitfield container size %d", ctype_bitcsz(info));
678     break;
679   }
680 }
681 
682 /* -- Initialize C type with TValues -------------------------------------- */
683 
684 /* Initialize an array with TValues. */
cconv_array_init(CTState * cts,CType * d,CTSize sz,uint8_t * dp,TValue * o,MSize len)685 static void cconv_array_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
686 			     TValue *o, MSize len)
687 {
688   CType *dc = ctype_rawchild(cts, d);  /* Array element type. */
689   CTSize ofs, esize = dc->size;
690   MSize i;
691   if (len*esize > sz)
692     cconv_err_initov(cts, d);
693   for (i = 0, ofs = 0; i < len; i++, ofs += esize)
694     lj_cconv_ct_tv(cts, dc, dp + ofs, o + i, 0);
695   if (ofs == esize) {  /* Replicate a single element. */
696     for (; ofs < sz; ofs += esize) memcpy(dp + ofs, dp, esize);
697   } else {  /* Otherwise fill the remainder with zero. */
698     memset(dp + ofs, 0, sz - ofs);
699   }
700 }
701 
702 /* Initialize a sub-struct/union with TValues. */
cconv_substruct_init(CTState * cts,CType * d,uint8_t * dp,TValue * o,MSize len,MSize * ip)703 static void cconv_substruct_init(CTState *cts, CType *d, uint8_t *dp,
704 				 TValue *o, MSize len, MSize *ip)
705 {
706   CTypeID id = d->sib;
707   while (id) {
708     CType *df = ctype_get(cts, id);
709     id = df->sib;
710     if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) {
711       MSize i = *ip;
712       if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */
713       if (i >= len) break;
714       *ip = i + 1;
715       if (ctype_isfield(df->info))
716 	lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, o + i, 0);
717       else
718 	lj_cconv_bf_tv(cts, df, dp+df->size, o + i);
719       if ((d->info & CTF_UNION)) break;
720     } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) {
721       cconv_substruct_init(cts, ctype_rawchild(cts, df),
722 			   dp+df->size, o, len, ip);
723       if ((d->info & CTF_UNION)) break;
724     }  /* Ignore all other entries in the chain. */
725   }
726 }
727 
728 /* Initialize a struct/union with TValues. */
cconv_struct_init(CTState * cts,CType * d,CTSize sz,uint8_t * dp,TValue * o,MSize len)729 static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp,
730 			      TValue *o, MSize len)
731 {
732   MSize i = 0;
733   memset(dp, 0, sz);  /* Much simpler to clear the struct first. */
734   cconv_substruct_init(cts, d, dp, o, len, &i);
735   if (i < len)
736     cconv_err_initov(cts, d);
737 }
738 
739 /* Check whether to use a multi-value initializer.
740 ** This is true if an aggregate is to be initialized with a value.
741 ** Valarrays are treated as values here so ct_tv handles (V|C, I|F).
742 */
lj_cconv_multi_init(CTState * cts,CType * d,TValue * o)743 int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o)
744 {
745   if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info)))
746     return 0;  /* Destination is not an aggregate. */
747   if (tvistab(o) || (tvisstr(o) && !ctype_isstruct(d->info)))
748     return 0;  /* Initializer is not a value. */
749   if (tviscdata(o) && lj_ctype_rawref(cts, cdataV(o)->ctypeid) == d)
750     return 0;  /* Source and destination are identical aggregates. */
751   return 1;  /* Otherwise the initializer is a value. */
752 }
753 
754 /* Initialize C type with TValues. Caveat: expects to get the raw CType! */
lj_cconv_ct_init(CTState * cts,CType * d,CTSize sz,uint8_t * dp,TValue * o,MSize len)755 void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz,
756 		      uint8_t *dp, TValue *o, MSize len)
757 {
758   if (len == 0)
759     memset(dp, 0, sz);
760   else if (len == 1 && !lj_cconv_multi_init(cts, d, o))
761     lj_cconv_ct_tv(cts, d, dp, o, 0);
762   else if (ctype_isarray(d->info))  /* Also handles valarray init with len>1. */
763     cconv_array_init(cts, d, sz, dp, o, len);
764   else if (ctype_isstruct(d->info))
765     cconv_struct_init(cts, d, sz, dp, o, len);
766   else
767     cconv_err_initov(cts, d);
768 }
769 
770 #endif
771