lj_clib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. ** FFI C library loader.
  3. ** Copyright (C) 2005-2013 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_tab.h"
  10. #include "lj_str.h"
  11. #include "lj_udata.h"
  12. #include "lj_ctype.h"
  13. #include "lj_cconv.h"
  14. #include "lj_cdata.h"
  15. #include "lj_clib.h"
  16. /* -- OS-specific functions ----------------------------------------------- */
  17. #if LJ_TARGET_DLOPEN
  18. #include <dlfcn.h>
  19. #include <stdio.h>
  20. #if defined(RTLD_DEFAULT)
  21. #define CLIB_DEFHANDLE RTLD_DEFAULT
  22. #elif LJ_TARGET_OSX || LJ_TARGET_BSD
  23. #define CLIB_DEFHANDLE ((void *)(intptr_t)-2)
  24. #else
  25. #define CLIB_DEFHANDLE NULL
  26. #endif
  27. LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L)
  28. {
  29. lj_err_callermsg(L, dlerror());
  30. }
  31. #define clib_error(L, fmt, name) clib_error_(L)
  32. #if defined(__CYGWIN__)
  33. #define CLIB_SOPREFIX "cyg"
  34. #else
  35. #define CLIB_SOPREFIX "lib"
  36. #endif
  37. #if LJ_TARGET_OSX
  38. #define CLIB_SOEXT "%s.dylib"
  39. #elif defined(__CYGWIN__)
  40. #define CLIB_SOEXT "%s.dll"
  41. #else
  42. #define CLIB_SOEXT "%s.so"
  43. #endif
  44. static const char *clib_extname(lua_State *L, const char *name)
  45. {
  46. if (!strchr(name, '/')
  47. #ifdef __CYGWIN__
  48. && !strchr(name, '\\')
  49. #endif
  50. ) {
  51. if (!strchr(name, '.')) {
  52. name = lj_str_pushf(L, CLIB_SOEXT, name);
  53. L->top--;
  54. #ifdef __CYGWIN__
  55. } else {
  56. return name;
  57. #endif
  58. }
  59. if (!(name[0] == CLIB_SOPREFIX[0] && name[1] == CLIB_SOPREFIX[1] &&
  60. name[2] == CLIB_SOPREFIX[2])) {
  61. name = lj_str_pushf(L, CLIB_SOPREFIX "%s", name);
  62. L->top--;
  63. }
  64. }
  65. return name;
  66. }
  67. /* Check for a recognized ld script line. */
  68. static const char *clib_check_lds(lua_State *L, const char *buf)
  69. {
  70. char *p, *e;
  71. if ((!strncmp(buf, "GROUP", 5) || !strncmp(buf, "INPUT", 5)) &&
  72. (p = strchr(buf, '('))) {
  73. while (*++p == ' ') ;
  74. for (e = p; *e && *e != ' ' && *e != ')'; e++) ;
  75. return strdata(lj_str_new(L, p, e-p));
  76. }
  77. return NULL;
  78. }
  79. /* Quick and dirty solution to resolve shared library name from ld script. */
  80. static const char *clib_resolve_lds(lua_State *L, const char *name)
  81. {
  82. FILE *fp = fopen(name, "r");
  83. const char *p = NULL;
  84. if (fp) {
  85. char buf[256];
  86. if (fgets(buf, sizeof(buf), fp)) {
  87. if (!strncmp(buf, "/* GNU ld script", 16)) { /* ld script magic? */
  88. while (fgets(buf, sizeof(buf), fp)) { /* Check all lines. */
  89. p = clib_check_lds(L, buf);
  90. if (p) break;
  91. }
  92. } else { /* Otherwise check only the first line. */
  93. p = clib_check_lds(L, buf);
  94. }
  95. }
  96. fclose(fp);
  97. }
  98. return p;
  99. }
  100. static void *clib_loadlib(lua_State *L, const char *name, int global)
  101. {
  102. void *h = dlopen(clib_extname(L, name),
  103. RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL));
  104. if (!h) {
  105. const char *e, *err = dlerror();
  106. if (*err == '/' && (e = strchr(err, ':')) &&
  107. (name = clib_resolve_lds(L, strdata(lj_str_new(L, err, e-err))))) {
  108. h = dlopen(name, RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL));
  109. if (h) return h;
  110. err = dlerror();
  111. }
  112. lj_err_callermsg(L, err);
  113. }
  114. return h;
  115. }
  116. static void clib_unloadlib(CLibrary *cl)
  117. {
  118. if (cl->handle && cl->handle != CLIB_DEFHANDLE)
  119. dlclose(cl->handle);
  120. }
  121. static void *clib_getsym(CLibrary *cl, const char *name)
  122. {
  123. void *p = dlsym(cl->handle, name);
  124. return p;
  125. }
  126. #elif LJ_TARGET_WINDOWS
  127. #define WIN32_LEAN_AND_MEAN
  128. #ifndef WINVER
  129. #define WINVER 0x0500
  130. #endif
  131. #include <windows.h>
  132. #ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  133. #define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
  134. #define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
  135. BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*);
  136. #endif
  137. #define CLIB_DEFHANDLE ((void *)-1)
  138. /* Default libraries. */
  139. enum {
  140. CLIB_HANDLE_EXE,
  141. CLIB_HANDLE_DLL,
  142. CLIB_HANDLE_CRT,
  143. CLIB_HANDLE_KERNEL32,
  144. CLIB_HANDLE_USER32,
  145. CLIB_HANDLE_GDI32,
  146. CLIB_HANDLE_MAX
  147. };
  148. static void *clib_def_handle[CLIB_HANDLE_MAX];
  149. LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,
  150. const char *name)
  151. {
  152. DWORD err = GetLastError();
  153. char buf[128];
  154. if (!FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,
  155. NULL, err, 0, buf, sizeof(buf), NULL))
  156. buf[0] = '\0';
  157. lj_err_callermsg(L, lj_str_pushf(L, fmt, name, buf));
  158. }
  159. static int clib_needext(const char *s)
  160. {
  161. while (*s) {
  162. if (*s == '/' || *s == '\\' || *s == '.') return 0;
  163. s++;
  164. }
  165. return 1;
  166. }
  167. static const char *clib_extname(lua_State *L, const char *name)
  168. {
  169. if (clib_needext(name)) {
  170. name = lj_str_pushf(L, "%s.dll", name);
  171. L->top--;
  172. }
  173. return name;
  174. }
  175. static void *clib_loadlib(lua_State *L, const char *name, int global)
  176. {
  177. DWORD oldwerr = GetLastError();
  178. void *h = (void *)LoadLibraryA(clib_extname(L, name));
  179. if (!h) clib_error(L, "cannot load module " LUA_QS ": %s", name);
  180. SetLastError(oldwerr);
  181. UNUSED(global);
  182. return h;
  183. }
  184. static void clib_unloadlib(CLibrary *cl)
  185. {
  186. if (cl->handle == CLIB_DEFHANDLE) {
  187. MSize i;
  188. for (i = CLIB_HANDLE_KERNEL32; i < CLIB_HANDLE_MAX; i++) {
  189. void *h = clib_def_handle[i];
  190. if (h) {
  191. clib_def_handle[i] = NULL;
  192. FreeLibrary((HINSTANCE)h);
  193. }
  194. }
  195. } else if (!cl->handle) {
  196. FreeLibrary((HINSTANCE)cl->handle);
  197. }
  198. }
  199. static void *clib_getsym(CLibrary *cl, const char *name)
  200. {
  201. void *p = NULL;
  202. if (cl->handle == CLIB_DEFHANDLE) { /* Search default libraries. */
  203. MSize i;
  204. for (i = 0; i < CLIB_HANDLE_MAX; i++) {
  205. HINSTANCE h = (HINSTANCE)clib_def_handle[i];
  206. if (!(void *)h) { /* Resolve default library handles (once). */
  207. switch (i) {
  208. case CLIB_HANDLE_EXE: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &h); break;
  209. case CLIB_HANDLE_DLL:
  210. GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  211. (const char *)clib_def_handle, &h);
  212. break;
  213. case CLIB_HANDLE_CRT:
  214. GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  215. (const char *)&_fmode, &h);
  216. break;
  217. case CLIB_HANDLE_KERNEL32: h = LoadLibraryA("kernel32.dll"); break;
  218. case CLIB_HANDLE_USER32: h = LoadLibraryA("user32.dll"); break;
  219. case CLIB_HANDLE_GDI32: h = LoadLibraryA("gdi32.dll"); break;
  220. }
  221. if (!h) continue;
  222. clib_def_handle[i] = (void *)h;
  223. }
  224. p = (void *)GetProcAddress(h, name);
  225. if (p) break;
  226. }
  227. } else {
  228. p = (void *)GetProcAddress((HINSTANCE)cl->handle, name);
  229. }
  230. return p;
  231. }
  232. #else
  233. #define CLIB_DEFHANDLE NULL
  234. LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,
  235. const char *name)
  236. {
  237. lj_err_callermsg(L, lj_str_pushf(L, fmt, name, "no support for this OS"));
  238. }
  239. static void *clib_loadlib(lua_State *L, const char *name, int global)
  240. {
  241. lj_err_callermsg(L, "no support for loading dynamic libraries for this OS");
  242. UNUSED(name); UNUSED(global);
  243. return NULL;
  244. }
  245. static void clib_unloadlib(CLibrary *cl)
  246. {
  247. UNUSED(cl);
  248. }
  249. static void *clib_getsym(CLibrary *cl, const char *name)
  250. {
  251. UNUSED(cl); UNUSED(name);
  252. return NULL;
  253. }
  254. #endif
  255. /* -- C library indexing -------------------------------------------------- */
  256. #if LJ_TARGET_X86 && LJ_ABI_WIN
  257. /* Compute argument size for fastcall/stdcall functions. */
  258. static CTSize clib_func_argsize(CTState *cts, CType *ct)
  259. {
  260. CTSize n = 0;
  261. while (ct->sib) {
  262. CType *d;
  263. ct = ctype_get(cts, ct->sib);
  264. if (ctype_isfield(ct->info)) {
  265. d = ctype_rawchild(cts, ct);
  266. n += ((d->size + 3) & ~3);
  267. }
  268. }
  269. return n;
  270. }
  271. #endif
  272. /* Get redirected or mangled external symbol. */
  273. static const char *clib_extsym(CTState *cts, CType *ct, GCstr *name)
  274. {
  275. if (ct->sib) {
  276. CType *ctf = ctype_get(cts, ct->sib);
  277. if (ctype_isxattrib(ctf->info, CTA_REDIR))
  278. return strdata(gco2str(gcref(ctf->name)));
  279. }
  280. return strdata(name);
  281. }
  282. /* Index a C library by name. */
  283. TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name)
  284. {
  285. TValue *tv = lj_tab_setstr(L, cl->cache, name);
  286. if (LJ_UNLIKELY(tvisnil(tv))) {
  287. CTState *cts = ctype_cts(L);
  288. CType *ct;
  289. CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX);
  290. if (!id)
  291. lj_err_callerv(L, LJ_ERR_FFI_NODECL, strdata(name));
  292. if (ctype_isconstval(ct->info)) {
  293. CType *ctt = ctype_child(cts, ct);
  294. lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);
  295. if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)
  296. setnumV(tv, (lua_Number)(uint32_t)ct->size);
  297. else
  298. setintV(tv, (int32_t)ct->size);
  299. } else {
  300. const char *sym = clib_extsym(cts, ct, name);
  301. #if LJ_TARGET_WINDOWS
  302. DWORD oldwerr = GetLastError();
  303. #endif
  304. void *p = clib_getsym(cl, sym);
  305. GCcdata *cd;
  306. lua_assert(ctype_isfunc(ct->info) || ctype_isextern(ct->info));
  307. #if LJ_TARGET_X86 && LJ_ABI_WIN
  308. /* Retry with decorated name for fastcall/stdcall functions. */
  309. if (!p && ctype_isfunc(ct->info)) {
  310. CTInfo cconv = ctype_cconv(ct->info);
  311. if (cconv == CTCC_FASTCALL || cconv == CTCC_STDCALL) {
  312. CTSize sz = clib_func_argsize(cts, ct);
  313. const char *symd = lj_str_pushf(L,
  314. cconv == CTCC_FASTCALL ? "@%s@%d" : "_%s@%d",
  315. sym, sz);
  316. L->top--;
  317. p = clib_getsym(cl, symd);
  318. }
  319. }
  320. #endif
  321. if (!p)
  322. clib_error(L, "cannot resolve symbol " LUA_QS ": %s", sym);
  323. #if LJ_TARGET_WINDOWS
  324. SetLastError(oldwerr);
  325. #endif
  326. cd = lj_cdata_new(cts, id, CTSIZE_PTR);
  327. *(void **)cdataptr(cd) = p;
  328. setcdataV(L, tv, cd);
  329. }
  330. }
  331. return tv;
  332. }
  333. /* -- C library management ------------------------------------------------ */
  334. /* Create a new CLibrary object and push it on the stack. */
  335. static CLibrary *clib_new(lua_State *L, GCtab *mt)
  336. {
  337. GCtab *t = lj_tab_new(L, 0, 0);
  338. GCudata *ud = lj_udata_new(L, sizeof(CLibrary), t);
  339. CLibrary *cl = (CLibrary *)uddata(ud);
  340. cl->cache = t;
  341. ud->udtype = UDTYPE_FFI_CLIB;
  342. /* NOBARRIER: The GCudata is new (marked white). */
  343. setgcref(ud->metatable, obj2gco(mt));
  344. setudataV(L, L->top++, ud);
  345. return cl;
  346. }
  347. /* Load a C library. */
  348. void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global)
  349. {
  350. void *handle = clib_loadlib(L, strdata(name), global);
  351. CLibrary *cl = clib_new(L, mt);
  352. cl->handle = handle;
  353. }
  354. /* Unload a C library. */
  355. void lj_clib_unload(CLibrary *cl)
  356. {
  357. clib_unloadlib(cl);
  358. cl->handle = NULL;
  359. }
  360. /* Create the default C library object. */
  361. void lj_clib_default(lua_State *L, GCtab *mt)
  362. {
  363. CLibrary *cl = clib_new(L, mt);
  364. cl->handle = CLIB_DEFHANDLE;
  365. }
  366. #endif