libc_emcc_wrapper.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (C) 2020 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_common.h"
  6. #include "bh_log.h"
  7. #include "wasm_export.h"
  8. #include "../interpreter/wasm.h"
  9. #if !defined(_DEFAULT_SOURCE) && !defined(BH_PLATFORM_LINUX_SGX)
  10. #include "sys/syscall.h"
  11. #endif
  12. /* clang-format off */
  13. #define get_module_inst(exec_env) \
  14. wasm_runtime_get_module_inst(exec_env)
  15. #define validate_app_addr(offset, size) \
  16. wasm_runtime_validate_app_addr(module_inst, offset, size)
  17. #define validate_app_str_addr(offset) \
  18. wasm_runtime_validate_app_str_addr(module_inst, offset)
  19. #define validate_native_addr(addr, size) \
  20. wasm_runtime_validate_native_addr(module_inst, addr, size)
  21. #define addr_app_to_native(offset) \
  22. wasm_runtime_addr_app_to_native(module_inst, offset)
  23. #define addr_native_to_app(ptr) \
  24. wasm_runtime_addr_native_to_app(module_inst, ptr)
  25. #define module_malloc(size, p_native_addr) \
  26. wasm_runtime_module_malloc(module_inst, size, p_native_addr)
  27. #define module_free(offset) \
  28. wasm_runtime_module_free(module_inst, offset)
  29. /* clang-format on */
  30. static void
  31. invoke_viiii_wrapper(wasm_exec_env_t exec_env, uint32 elem_idx, int arg0,
  32. int arg1, int arg2, int arg3)
  33. {
  34. uint32 argv[4];
  35. bool ret;
  36. argv[0] = arg0;
  37. argv[1] = arg1;
  38. argv[2] = arg2;
  39. argv[3] = arg3;
  40. ret = wasm_runtime_call_indirect(exec_env, elem_idx, 4, argv);
  41. (void)ret;
  42. }
  43. static void
  44. invoke_viii_wrapper(wasm_exec_env_t exec_env, uint32 elem_idx, int arg0,
  45. int arg1, int arg2)
  46. {
  47. uint32 argv[4];
  48. bool ret;
  49. argv[0] = arg0;
  50. argv[1] = arg1;
  51. argv[2] = arg2;
  52. ret = wasm_runtime_call_indirect(exec_env, elem_idx, 3, argv);
  53. (void)ret;
  54. }
  55. static void
  56. invoke_vii_wrapper(wasm_exec_env_t exec_env, uint32 elem_idx, int arg0,
  57. int arg1)
  58. {
  59. uint32 argv[4];
  60. bool ret;
  61. argv[0] = arg0;
  62. argv[1] = arg1;
  63. ret = wasm_runtime_call_indirect(exec_env, elem_idx, 2, argv);
  64. (void)ret;
  65. }
  66. static void
  67. invoke_vi_wrapper(wasm_exec_env_t exec_env, uint32 elem_idx, int arg0)
  68. {
  69. uint32 argv[4];
  70. bool ret;
  71. argv[0] = arg0;
  72. ret = wasm_runtime_call_indirect(exec_env, elem_idx, 1, argv);
  73. (void)ret;
  74. }
  75. static int
  76. invoke_iii_wrapper(wasm_exec_env_t exec_env, uint32 elem_idx, int arg0,
  77. int arg1)
  78. {
  79. uint32 argv[4];
  80. bool ret;
  81. argv[0] = arg0;
  82. argv[1] = arg1;
  83. ret = wasm_runtime_call_indirect(exec_env, elem_idx, 2, argv);
  84. return ret ? argv[0] : 0;
  85. }
  86. static int
  87. invoke_ii_wrapper(wasm_exec_env_t exec_env, uint32 elem_idx, int arg0)
  88. {
  89. uint32 argv[4];
  90. bool ret;
  91. argv[0] = arg0;
  92. ret = wasm_runtime_call_indirect(exec_env, elem_idx, 1, argv);
  93. return ret ? argv[0] : 0;
  94. }
  95. struct timespec_emcc {
  96. int tv_sec;
  97. int tv_nsec;
  98. };
  99. struct stat_emcc {
  100. unsigned st_dev;
  101. int __st_dev_padding;
  102. unsigned __st_ino_truncated;
  103. unsigned st_mode;
  104. unsigned st_nlink;
  105. unsigned st_uid;
  106. unsigned st_gid;
  107. unsigned st_rdev;
  108. int __st_rdev_padding;
  109. int64 st_size;
  110. int st_blksize;
  111. int st_blocks;
  112. struct timespec_emcc st_atim;
  113. struct timespec_emcc st_mtim;
  114. struct timespec_emcc st_ctim;
  115. int64 st_ino;
  116. };
  117. static int
  118. open_wrapper(wasm_exec_env_t exec_env, const char *pathname, int flags,
  119. int mode)
  120. {
  121. if (pathname == NULL)
  122. return -1;
  123. return open(pathname, flags, mode);
  124. }
  125. static int
  126. __sys_read_wrapper(wasm_exec_env_t exec_env, int fd, void *buf, uint32 count)
  127. {
  128. return read(fd, buf, count);
  129. }
  130. static void
  131. statbuf_native2app(const struct stat *statbuf_native,
  132. struct stat_emcc *statbuf_app)
  133. {
  134. statbuf_app->st_dev = (unsigned)statbuf_native->st_dev;
  135. statbuf_app->__st_ino_truncated = (unsigned)statbuf_native->st_ino;
  136. statbuf_app->st_mode = (unsigned)statbuf_native->st_mode;
  137. statbuf_app->st_nlink = (unsigned)statbuf_native->st_nlink;
  138. statbuf_app->st_uid = (unsigned)statbuf_native->st_uid;
  139. statbuf_app->st_gid = (unsigned)statbuf_native->st_gid;
  140. statbuf_app->st_rdev = (unsigned)statbuf_native->st_rdev;
  141. statbuf_app->st_size = (int64)statbuf_native->st_size;
  142. statbuf_app->st_blksize = (unsigned)statbuf_native->st_blksize;
  143. statbuf_app->st_blocks = (unsigned)statbuf_native->st_blocks;
  144. statbuf_app->st_ino = (int64)statbuf_native->st_ino;
  145. statbuf_app->st_atim.tv_sec = (int)statbuf_native->st_atim.tv_sec;
  146. statbuf_app->st_atim.tv_nsec = (int)statbuf_native->st_atim.tv_nsec;
  147. statbuf_app->st_mtim.tv_sec = (int)statbuf_native->st_mtim.tv_sec;
  148. statbuf_app->st_mtim.tv_nsec = (int)statbuf_native->st_mtim.tv_nsec;
  149. statbuf_app->st_ctim.tv_sec = (int)statbuf_native->st_ctim.tv_sec;
  150. statbuf_app->st_ctim.tv_nsec = (int)statbuf_native->st_ctim.tv_nsec;
  151. }
  152. static int
  153. __sys_stat64_wrapper(wasm_exec_env_t exec_env, const char *pathname,
  154. struct stat_emcc *statbuf_app)
  155. {
  156. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  157. int ret;
  158. struct stat statbuf;
  159. if (!validate_native_addr((void *)statbuf_app, sizeof(struct stat_emcc)))
  160. return -1;
  161. if (pathname == NULL)
  162. return -1;
  163. ret = stat(pathname, &statbuf);
  164. if (ret == 0)
  165. statbuf_native2app(&statbuf, statbuf_app);
  166. return ret;
  167. }
  168. static int
  169. __sys_fstat64_wrapper(wasm_exec_env_t exec_env, int fd,
  170. struct stat_emcc *statbuf_app)
  171. {
  172. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  173. int ret;
  174. struct stat statbuf;
  175. if (!validate_native_addr((void *)statbuf_app, sizeof(struct stat_emcc)))
  176. return -1;
  177. if (fd <= 0)
  178. return -1;
  179. ret = fstat(fd, &statbuf);
  180. if (ret == 0)
  181. statbuf_native2app(&statbuf, statbuf_app);
  182. return ret;
  183. }
  184. static int
  185. mmap_wrapper(wasm_exec_env_t exec_env, void *addr, int length, int prot,
  186. int flags, int fd, int64 offset)
  187. {
  188. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  189. uint32 buf_offset;
  190. char *buf;
  191. int size_read;
  192. buf_offset = module_malloc(length, (void **)&buf);
  193. if (buf_offset == 0)
  194. return -1;
  195. if (fd <= 0)
  196. return -1;
  197. if (lseek(fd, offset, SEEK_SET) == -1)
  198. return -1;
  199. size_read = read(fd, buf, length);
  200. (void)size_read;
  201. return buf_offset;
  202. }
  203. static int
  204. munmap_wrapper(wasm_exec_env_t exec_env, uint32 buf_offset, int length)
  205. {
  206. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  207. module_free(buf_offset);
  208. return 0;
  209. }
  210. static int
  211. __munmap_wrapper(wasm_exec_env_t exec_env, uint32 buf_offset, int length)
  212. {
  213. return munmap_wrapper(exec_env, buf_offset, length);
  214. }
  215. static int
  216. getentropy_wrapper(wasm_exec_env_t exec_env, void *buffer, uint32 length)
  217. {
  218. if (buffer == NULL)
  219. return -1;
  220. #if defined(_DEFAULT_SOURCE) || defined(BH_PLATFORM_LINUX_SGX)
  221. return getentropy(buffer, length);
  222. #else
  223. return syscall(SYS_getrandom, buffer, length, 0);
  224. #endif
  225. }
  226. static int
  227. setjmp_wrapper(wasm_exec_env_t exec_env, void *jmp_buf)
  228. {
  229. os_printf("setjmp() called\n");
  230. return 0;
  231. }
  232. static void
  233. longjmp_wrapper(wasm_exec_env_t exec_env, void *jmp_buf, int val)
  234. {
  235. os_printf("longjmp() called\n");
  236. }
  237. #if !defined(BH_PLATFORM_LINUX_SGX)
  238. static FILE *file_list[32] = { 0 };
  239. static int
  240. get_free_file_slot()
  241. {
  242. unsigned int i;
  243. for (i = 0; i < sizeof(file_list) / sizeof(FILE *); i++) {
  244. if (file_list[i] == NULL)
  245. return (int)i;
  246. }
  247. return -1;
  248. }
  249. static int
  250. fopen_wrapper(wasm_exec_env_t exec_env, const char *pathname, const char *mode)
  251. {
  252. FILE *file;
  253. int file_id;
  254. if (pathname == NULL || mode == NULL)
  255. return 0;
  256. if ((file_id = get_free_file_slot()) == -1)
  257. return 0;
  258. file = fopen(pathname, mode);
  259. if (!file)
  260. return 0;
  261. file_list[file_id] = file;
  262. return file_id + 1;
  263. }
  264. static uint32
  265. fread_wrapper(wasm_exec_env_t exec_env, void *ptr, uint32 size, uint32 nmemb,
  266. int file_id)
  267. {
  268. FILE *file;
  269. file_id = file_id - 1;
  270. if ((unsigned)file_id >= sizeof(file_list) / sizeof(FILE *)) {
  271. return 0;
  272. }
  273. if ((file = file_list[file_id]) == NULL) {
  274. return 0;
  275. }
  276. return (uint32)fread(ptr, size, nmemb, file);
  277. }
  278. static int
  279. fseeko_wrapper(wasm_exec_env_t exec_env, int file_id, int64 offset, int whence)
  280. {
  281. FILE *file;
  282. file_id = file_id - 1;
  283. if ((unsigned)file_id >= sizeof(file_list) / sizeof(FILE *)) {
  284. return -1;
  285. }
  286. if ((file = file_list[file_id]) == NULL) {
  287. return -1;
  288. }
  289. return (uint32)fseek(file, offset, whence);
  290. }
  291. static uint32
  292. emcc_fwrite_wrapper(wasm_exec_env_t exec_env, const void *ptr, uint32 size,
  293. uint32 nmemb, int file_id)
  294. {
  295. FILE *file;
  296. file_id = file_id - 1;
  297. if ((unsigned)file_id >= sizeof(file_list) / sizeof(FILE *)) {
  298. return 0;
  299. }
  300. if ((file = file_list[file_id]) == NULL) {
  301. return 0;
  302. }
  303. return (uint32)fwrite(ptr, size, nmemb, file);
  304. }
  305. static int
  306. feof_wrapper(wasm_exec_env_t exec_env, int file_id)
  307. {
  308. FILE *file;
  309. file_id = file_id - 1;
  310. if ((unsigned)file_id >= sizeof(file_list) / sizeof(FILE *))
  311. return 1;
  312. if ((file = file_list[file_id]) == NULL)
  313. return 1;
  314. return feof(file);
  315. }
  316. static int
  317. fclose_wrapper(wasm_exec_env_t exec_env, int file_id)
  318. {
  319. FILE *file;
  320. file_id = file_id - 1;
  321. if ((unsigned)file_id >= sizeof(file_list) / sizeof(FILE *))
  322. return -1;
  323. if ((file = file_list[file_id]) == NULL)
  324. return -1;
  325. file_list[file_id] = NULL;
  326. return fclose(file);
  327. }
  328. static int
  329. __sys_mkdir_wrapper(wasm_exec_env_t exec_env, const char *pathname, int mode)
  330. {
  331. if (!pathname)
  332. return -1;
  333. return mkdir(pathname, mode);
  334. }
  335. static int
  336. __sys_rmdir_wrapper(wasm_exec_env_t exec_env, const char *pathname)
  337. {
  338. if (!pathname)
  339. return -1;
  340. return rmdir(pathname);
  341. }
  342. static int
  343. __sys_unlink_wrapper(wasm_exec_env_t exec_env, const char *pathname)
  344. {
  345. if (!pathname)
  346. return -1;
  347. return unlink(pathname);
  348. }
  349. static uint32
  350. __sys_getcwd_wrapper(wasm_exec_env_t exec_env, char *buf, uint32 size)
  351. {
  352. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  353. char *ret;
  354. if (!buf)
  355. return -1;
  356. ret = getcwd(buf, size);
  357. return ret ? addr_native_to_app(ret) : 0;
  358. }
  359. #include <sys/utsname.h>
  360. struct utsname_app {
  361. char sysname[64];
  362. char nodename[64];
  363. char release[64];
  364. char version[64];
  365. char machine[64];
  366. char domainname[64];
  367. };
  368. static int
  369. __sys_uname_wrapper(wasm_exec_env_t exec_env, struct utsname_app *uname_app)
  370. {
  371. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  372. struct utsname uname_native = { 0 };
  373. uint32 length;
  374. if (!validate_native_addr(uname_app, sizeof(struct utsname_app)))
  375. return -1;
  376. if (uname(&uname_native) != 0) {
  377. return -1;
  378. }
  379. memset(uname_app, 0, sizeof(struct utsname_app));
  380. length = strlen(uname_native.sysname);
  381. if (length > sizeof(uname_app->sysname) - 1)
  382. length = sizeof(uname_app->sysname) - 1;
  383. bh_memcpy_s(uname_app->sysname, sizeof(uname_app->sysname),
  384. uname_native.sysname, length);
  385. length = strlen(uname_native.nodename);
  386. if (length > sizeof(uname_app->nodename) - 1)
  387. length = sizeof(uname_app->nodename) - 1;
  388. bh_memcpy_s(uname_app->nodename, sizeof(uname_app->nodename),
  389. uname_native.nodename, length);
  390. length = strlen(uname_native.release);
  391. if (length > sizeof(uname_app->release) - 1)
  392. length = sizeof(uname_app->release) - 1;
  393. bh_memcpy_s(uname_app->release, sizeof(uname_app->release),
  394. uname_native.release, length);
  395. length = strlen(uname_native.version);
  396. if (length > sizeof(uname_app->version) - 1)
  397. length = sizeof(uname_app->version) - 1;
  398. bh_memcpy_s(uname_app->version, sizeof(uname_app->version),
  399. uname_native.version, length);
  400. #ifdef _GNU_SOURCE
  401. length = strlen(uname_native.domainname);
  402. if (length > sizeof(uname_app->domainname) - 1)
  403. length = sizeof(uname_app->domainname) - 1;
  404. bh_memcpy_s(uname_app->domainname, sizeof(uname_app->domainname),
  405. uname_native.domainname, length);
  406. #endif
  407. return 0;
  408. }
  409. static void
  410. emscripten_notify_memory_growth_wrapper(wasm_exec_env_t exec_env, int i)
  411. {
  412. (void)i;
  413. }
  414. static void
  415. emscripten_thread_sleep_wrapper(wasm_exec_env_t exec_env, double timeout_ms)
  416. {
  417. uint64 ms = (uint64)timeout_ms;
  418. uint64 sec = ms / 1000, us = (ms % 1000) * 1000;
  419. if (sec > 0)
  420. sleep(sec);
  421. if (us > 0)
  422. usleep(us);
  423. }
  424. #endif /* end of BH_PLATFORM_LINUX_SGX */
  425. /* clang-format off */
  426. #define REG_NATIVE_FUNC(func_name, signature) \
  427. { #func_name, func_name##_wrapper, signature, NULL }
  428. /* clang-format off */
  429. static NativeSymbol native_symbols_libc_emcc[] = {
  430. REG_NATIVE_FUNC(invoke_viiii, "(iiiii)"),
  431. REG_NATIVE_FUNC(invoke_viii, "(iiii)"),
  432. REG_NATIVE_FUNC(invoke_vii, "(iii)"),
  433. REG_NATIVE_FUNC(invoke_vi, "(ii)"),
  434. REG_NATIVE_FUNC(invoke_iii, "(iii)i"),
  435. REG_NATIVE_FUNC(invoke_ii, "(ii)i"),
  436. REG_NATIVE_FUNC(open, "($ii)i"),
  437. REG_NATIVE_FUNC(__sys_read, "(i*~)i"),
  438. REG_NATIVE_FUNC(__sys_stat64, "($*)i"),
  439. REG_NATIVE_FUNC(__sys_fstat64, "(i*)i"),
  440. REG_NATIVE_FUNC(mmap, "(*iiiiI)i"),
  441. REG_NATIVE_FUNC(munmap, "(ii)i"),
  442. REG_NATIVE_FUNC(__munmap, "(ii)i"),
  443. REG_NATIVE_FUNC(getentropy, "(*~)i"),
  444. REG_NATIVE_FUNC(setjmp, "(*)i"),
  445. REG_NATIVE_FUNC(longjmp, "(*i)"),
  446. #if !defined(BH_PLATFORM_LINUX_SGX)
  447. REG_NATIVE_FUNC(fopen, "($$)i"),
  448. REG_NATIVE_FUNC(fread, "(*iii)i"),
  449. REG_NATIVE_FUNC(fseeko, "(iIi)i"),
  450. REG_NATIVE_FUNC(emcc_fwrite, "(*iii)i"),
  451. REG_NATIVE_FUNC(feof, "(i)i"),
  452. REG_NATIVE_FUNC(fclose, "(i)i"),
  453. REG_NATIVE_FUNC(__sys_mkdir, "($i)i"),
  454. REG_NATIVE_FUNC(__sys_rmdir, "($)i"),
  455. REG_NATIVE_FUNC(__sys_unlink, "($)i"),
  456. REG_NATIVE_FUNC(__sys_getcwd, "(*~)i"),
  457. REG_NATIVE_FUNC(__sys_uname, "(*)i"),
  458. REG_NATIVE_FUNC(emscripten_notify_memory_growth, "(i)"),
  459. REG_NATIVE_FUNC(emscripten_thread_sleep, "(F)"),
  460. #endif /* end of BH_PLATFORM_LINUX_SGX */
  461. };
  462. uint32
  463. get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis)
  464. {
  465. *p_libc_emcc_apis = native_symbols_libc_emcc;
  466. return sizeof(native_symbols_libc_emcc) / sizeof(NativeSymbol);
  467. }