lj_cdata.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. ** C data management.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #include "lj_obj.h"
  6. #if LJ_HASFFI
  7. #include "lj_gc.h"
  8. #include "lj_err.h"
  9. #include "lj_str.h"
  10. #include "lj_tab.h"
  11. #include "lj_ctype.h"
  12. #include "lj_cconv.h"
  13. #include "lj_cdata.h"
  14. /* -- C data allocation --------------------------------------------------- */
  15. /* Allocate a new C data object holding a reference to another object. */
  16. GCcdata *lj_cdata_newref(CTState *cts, const void *p, CTypeID id)
  17. {
  18. CTypeID refid = lj_ctype_intern(cts, CTINFO_REF(id), CTSIZE_PTR);
  19. GCcdata *cd = lj_cdata_new(cts, refid, CTSIZE_PTR);
  20. *(const void **)cdataptr(cd) = p;
  21. return cd;
  22. }
  23. /* Allocate variable-sized or specially aligned C data object. */
  24. GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz, CTSize align)
  25. {
  26. global_State *g;
  27. MSize extra = sizeof(GCcdataVar) + sizeof(GCcdata) +
  28. (align > CT_MEMALIGN ? (1u<<align) - (1u<<CT_MEMALIGN) : 0);
  29. char *p = lj_mem_newt(cts->L, extra + sz, char);
  30. uintptr_t adata = (uintptr_t)p + sizeof(GCcdataVar) + sizeof(GCcdata);
  31. uintptr_t almask = (1u << align) - 1u;
  32. GCcdata *cd = (GCcdata *)(((adata + almask) & ~almask) - sizeof(GCcdata));
  33. lua_assert((char *)cd - p < 65536);
  34. cdatav(cd)->offset = (uint16_t)((char *)cd - p);
  35. cdatav(cd)->extra = extra;
  36. cdatav(cd)->len = sz;
  37. g = cts->g;
  38. setgcrefr(cd->nextgc, g->gc.root);
  39. setgcref(g->gc.root, obj2gco(cd));
  40. newwhite(g, obj2gco(cd));
  41. cd->marked |= 0x80;
  42. cd->gct = ~LJ_TCDATA;
  43. cd->ctypeid = id;
  44. return cd;
  45. }
  46. /* Free a C data object. */
  47. void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd)
  48. {
  49. if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) {
  50. GCobj *root;
  51. makewhite(g, obj2gco(cd));
  52. markfinalized(obj2gco(cd));
  53. if ((root = gcref(g->gc.mmudata)) != NULL) {
  54. setgcrefr(cd->nextgc, root->gch.nextgc);
  55. setgcref(root->gch.nextgc, obj2gco(cd));
  56. setgcref(g->gc.mmudata, obj2gco(cd));
  57. } else {
  58. setgcref(cd->nextgc, obj2gco(cd));
  59. setgcref(g->gc.mmudata, obj2gco(cd));
  60. }
  61. } else if (LJ_LIKELY(!cdataisv(cd))) {
  62. CType *ct = ctype_raw(ctype_ctsG(g), cd->ctypeid);
  63. CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR;
  64. lua_assert(ctype_hassize(ct->info) || ctype_isfunc(ct->info) ||
  65. ctype_isextern(ct->info));
  66. lj_mem_free(g, cd, sizeof(GCcdata) + sz);
  67. } else {
  68. lj_mem_free(g, memcdatav(cd), sizecdatav(cd));
  69. }
  70. }
  71. TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd)
  72. {
  73. global_State *g = G(L);
  74. GCtab *t = ctype_ctsG(g)->finalizer;
  75. if (gcref(t->metatable)) {
  76. /* Add cdata to finalizer table, if still enabled. */
  77. TValue *tv, tmp;
  78. setcdataV(L, &tmp, cd);
  79. lj_gc_anybarriert(L, t);
  80. tv = lj_tab_set(L, t, &tmp);
  81. cd->marked |= LJ_GC_CDATA_FIN;
  82. return tv;
  83. } else {
  84. /* Otherwise return dummy TValue. */
  85. return &g->tmptv;
  86. }
  87. }
  88. /* -- C data indexing ----------------------------------------------------- */
  89. /* Index C data by a TValue. Return CType and pointer. */
  90. CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp,
  91. CTInfo *qual)
  92. {
  93. uint8_t *p = (uint8_t *)cdataptr(cd);
  94. CType *ct = ctype_get(cts, cd->ctypeid);
  95. ptrdiff_t idx;
  96. /* Resolve reference for cdata object. */
  97. if (ctype_isref(ct->info)) {
  98. lua_assert(ct->size == CTSIZE_PTR);
  99. p = *(uint8_t **)p;
  100. ct = ctype_child(cts, ct);
  101. }
  102. collect_attrib:
  103. /* Skip attributes and collect qualifiers. */
  104. while (ctype_isattrib(ct->info)) {
  105. if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;
  106. ct = ctype_child(cts, ct);
  107. }
  108. lua_assert(!ctype_isref(ct->info)); /* Interning rejects refs to refs. */
  109. if (tvisint(key)) {
  110. idx = (ptrdiff_t)intV(key);
  111. goto integer_key;
  112. } else if (tvisnum(key)) { /* Numeric key. */
  113. idx = LJ_64 ? (ptrdiff_t)numV(key) : (ptrdiff_t)lj_num2int(numV(key));
  114. integer_key:
  115. if (ctype_ispointer(ct->info)) {
  116. CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */
  117. if (sz != CTSIZE_INVALID) {
  118. if (ctype_isptr(ct->info)) {
  119. p = (uint8_t *)cdata_getptr(p, ct->size);
  120. } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) {
  121. if ((ct->info & CTF_COMPLEX)) idx &= 1;
  122. *qual |= CTF_CONST; /* Valarray elements are constant. */
  123. }
  124. *pp = p + idx*(int32_t)sz;
  125. return ct;
  126. }
  127. }
  128. } else if (tviscdata(key)) { /* Integer cdata key. */
  129. GCcdata *cdk = cdataV(key);
  130. CType *ctk = ctype_raw(cts, cdk->ctypeid);
  131. if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk);
  132. if (ctype_isinteger(ctk->info)) {
  133. lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk,
  134. (uint8_t *)&idx, cdataptr(cdk), 0);
  135. goto integer_key;
  136. }
  137. } else if (tvisstr(key)) { /* String key. */
  138. GCstr *name = strV(key);
  139. if (ctype_isstruct(ct->info)) {
  140. CTSize ofs;
  141. CType *fct = lj_ctype_getfieldq(cts, ct, name, &ofs, qual);
  142. if (fct) {
  143. *pp = p + ofs;
  144. return fct;
  145. }
  146. } else if (ctype_iscomplex(ct->info)) {
  147. if (name->len == 2) {
  148. *qual |= CTF_CONST; /* Complex fields are constant. */
  149. if (strdata(name)[0] == 'r' && strdata(name)[1] == 'e') {
  150. *pp = p;
  151. return ct;
  152. } else if (strdata(name)[0] == 'i' && strdata(name)[1] == 'm') {
  153. *pp = p + (ct->size >> 1);
  154. return ct;
  155. }
  156. }
  157. } else if (cd->ctypeid == CTID_CTYPEID) {
  158. /* Allow indexing a (pointer to) struct constructor to get constants. */
  159. CType *sct = ctype_raw(cts, *(CTypeID *)p);
  160. if (ctype_isptr(sct->info))
  161. sct = ctype_rawchild(cts, sct);
  162. if (ctype_isstruct(sct->info)) {
  163. CTSize ofs;
  164. CType *fct = lj_ctype_getfield(cts, sct, name, &ofs);
  165. if (fct && ctype_isconstval(fct->info))
  166. return fct;
  167. }
  168. ct = sct; /* Allow resolving metamethods for constructors, too. */
  169. }
  170. }
  171. if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */
  172. if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) {
  173. p = (uint8_t *)cdata_getptr(p, ct->size);
  174. ct = ctype_child(cts, ct);
  175. goto collect_attrib;
  176. }
  177. }
  178. *qual |= 1; /* Lookup failed. */
  179. return ct; /* But return the resolved raw type. */
  180. }
  181. /* -- C data getters ------------------------------------------------------ */
  182. /* Get constant value and convert to TValue. */
  183. static void cdata_getconst(CTState *cts, TValue *o, CType *ct)
  184. {
  185. CType *ctt = ctype_child(cts, ct);
  186. lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);
  187. /* Constants are already zero-extended/sign-extended to 32 bits. */
  188. if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)
  189. setnumV(o, (lua_Number)(uint32_t)ct->size);
  190. else
  191. setintV(o, (int32_t)ct->size);
  192. }
  193. /* Get C data value and convert to TValue. */
  194. int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp)
  195. {
  196. CTypeID sid;
  197. if (ctype_isconstval(s->info)) {
  198. cdata_getconst(cts, o, s);
  199. return 0; /* No GC step needed. */
  200. } else if (ctype_isbitfield(s->info)) {
  201. return lj_cconv_tv_bf(cts, s, o, sp);
  202. }
  203. /* Get child type of pointer/array/field. */
  204. lua_assert(ctype_ispointer(s->info) || ctype_isfield(s->info));
  205. sid = ctype_cid(s->info);
  206. s = ctype_get(cts, sid);
  207. /* Resolve reference for field. */
  208. if (ctype_isref(s->info)) {
  209. lua_assert(s->size == CTSIZE_PTR);
  210. sp = *(uint8_t **)sp;
  211. sid = ctype_cid(s->info);
  212. s = ctype_get(cts, sid);
  213. }
  214. /* Skip attributes. */
  215. while (ctype_isattrib(s->info))
  216. s = ctype_child(cts, s);
  217. return lj_cconv_tv_ct(cts, s, sid, o, sp);
  218. }
  219. /* -- C data setters ------------------------------------------------------ */
  220. /* Convert TValue and set C data value. */
  221. void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual)
  222. {
  223. if (ctype_isconstval(d->info)) {
  224. goto err_const;
  225. } else if (ctype_isbitfield(d->info)) {
  226. if (((d->info|qual) & CTF_CONST)) goto err_const;
  227. lj_cconv_bf_tv(cts, d, dp, o);
  228. return;
  229. }
  230. /* Get child type of pointer/array/field. */
  231. lua_assert(ctype_ispointer(d->info) || ctype_isfield(d->info));
  232. d = ctype_child(cts, d);
  233. /* Resolve reference for field. */
  234. if (ctype_isref(d->info)) {
  235. lua_assert(d->size == CTSIZE_PTR);
  236. dp = *(uint8_t **)dp;
  237. d = ctype_child(cts, d);
  238. }
  239. /* Skip attributes and collect qualifiers. */
  240. for (;;) {
  241. if (ctype_isattrib(d->info)) {
  242. if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;
  243. } else {
  244. break;
  245. }
  246. d = ctype_child(cts, d);
  247. }
  248. lua_assert(ctype_hassize(d->info) && !ctype_isvoid(d->info));
  249. if (((d->info|qual) & CTF_CONST)) {
  250. err_const:
  251. lj_err_caller(cts->L, LJ_ERR_FFI_WRCONST);
  252. }
  253. lj_cconv_ct_tv(cts, d, dp, o, 0);
  254. }
  255. #endif