libc_builtin_wrapper.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * Copyright (C) 2019 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(_WIN32) || defined(_WIN32_)
  10. #define strncasecmp _strnicmp
  11. #define strcasecmp _stricmp
  12. #endif
  13. void
  14. wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
  15. uint32
  16. wasm_runtime_get_temp_ret(wasm_module_inst_t module);
  17. void
  18. wasm_runtime_set_temp_ret(wasm_module_inst_t module, uint32 temp_ret);
  19. uint32
  20. wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
  21. void
  22. wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
  23. uint32
  24. wasm_runtime_module_realloc(wasm_module_inst_t module, uint32 ptr, uint32 size,
  25. void **p_native_addr);
  26. /* clang-format off */
  27. #define get_module_inst(exec_env) \
  28. wasm_runtime_get_module_inst(exec_env)
  29. #define validate_app_addr(offset, size) \
  30. wasm_runtime_validate_app_addr(module_inst, offset, size)
  31. #define validate_app_str_addr(offset) \
  32. wasm_runtime_validate_app_str_addr(module_inst, offset)
  33. #define validate_native_addr(addr, size) \
  34. wasm_runtime_validate_native_addr(module_inst, addr, size)
  35. #define addr_app_to_native(offset) \
  36. wasm_runtime_addr_app_to_native(module_inst, offset)
  37. #define addr_native_to_app(ptr) \
  38. wasm_runtime_addr_native_to_app(module_inst, ptr)
  39. #define module_malloc(size, p_native_addr) \
  40. wasm_runtime_module_malloc(module_inst, size, p_native_addr)
  41. #define module_free(offset) \
  42. wasm_runtime_module_free(module_inst, offset)
  43. /* clang-format on */
  44. typedef int (*out_func_t)(int c, void *ctx);
  45. enum pad_type {
  46. PAD_NONE,
  47. PAD_ZERO_BEFORE,
  48. PAD_SPACE_BEFORE,
  49. PAD_SPACE_AFTER,
  50. };
  51. typedef char *_va_list;
  52. #define _INTSIZEOF(n) (((uint32)sizeof(n) + 3) & (uint32)~3)
  53. #define _va_arg(ap, t) (*(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))
  54. #define CHECK_VA_ARG(ap, t) \
  55. do { \
  56. if ((uint8 *)ap + _INTSIZEOF(t) > native_end_addr) \
  57. goto fail; \
  58. } while (0)
  59. /**
  60. * @brief Output an unsigned int in hex format
  61. *
  62. * Output an unsigned int on output installed by platform at init time. Should
  63. * be able to handle an unsigned int of any size, 32 or 64 bit.
  64. * @param num Number to output
  65. *
  66. * @return N/A
  67. */
  68. static void
  69. _printf_hex_uint(out_func_t out, void *ctx, const uint64 num, bool is_u64,
  70. enum pad_type padding, int min_width)
  71. {
  72. int shift = sizeof(num) * 8;
  73. int found_largest_digit = 0;
  74. int remaining = 16; /* 16 digits max */
  75. int digits = 0;
  76. char nibble;
  77. while (shift >= 4) {
  78. shift -= 4;
  79. nibble = (num >> shift) & 0xf;
  80. if (nibble || found_largest_digit || shift == 0) {
  81. found_largest_digit = 1;
  82. nibble = (char)(nibble + (nibble > 9 ? 87 : 48));
  83. out((int)nibble, ctx);
  84. digits++;
  85. continue;
  86. }
  87. if (remaining-- <= min_width) {
  88. if (padding == PAD_ZERO_BEFORE) {
  89. out('0', ctx);
  90. }
  91. else if (padding == PAD_SPACE_BEFORE) {
  92. out(' ', ctx);
  93. }
  94. }
  95. }
  96. if (padding == PAD_SPACE_AFTER) {
  97. remaining = min_width * 2 - digits;
  98. while (remaining-- > 0) {
  99. out(' ', ctx);
  100. }
  101. }
  102. }
  103. /**
  104. * @brief Output an unsigned int in decimal format
  105. *
  106. * Output an unsigned int on output installed by platform at init time. Only
  107. * works with 32-bit values.
  108. * @param num Number to output
  109. *
  110. * @return N/A
  111. */
  112. static void
  113. _printf_dec_uint(out_func_t out, void *ctx, const uint32 num,
  114. enum pad_type padding, int min_width)
  115. {
  116. uint32 pos = 999999999;
  117. uint32 remainder = num;
  118. int found_largest_digit = 0;
  119. int remaining = 10; /* 10 digits max */
  120. int digits = 1;
  121. /* make sure we don't skip if value is zero */
  122. if (min_width <= 0) {
  123. min_width = 1;
  124. }
  125. while (pos >= 9) {
  126. if (found_largest_digit || remainder > pos) {
  127. found_largest_digit = 1;
  128. out((int)((remainder / (pos + 1)) + 48), ctx);
  129. digits++;
  130. }
  131. else if (remaining <= min_width && padding < PAD_SPACE_AFTER) {
  132. out((int)(padding == PAD_ZERO_BEFORE ? '0' : ' '), ctx);
  133. digits++;
  134. }
  135. remaining--;
  136. remainder %= (pos + 1);
  137. pos /= 10;
  138. }
  139. out((int)(remainder + 48), ctx);
  140. if (padding == PAD_SPACE_AFTER) {
  141. remaining = min_width - digits;
  142. while (remaining-- > 0) {
  143. out(' ', ctx);
  144. }
  145. }
  146. }
  147. static void
  148. print_err(out_func_t out, void *ctx)
  149. {
  150. out('E', ctx);
  151. out('R', ctx);
  152. out('R', ctx);
  153. }
  154. static bool
  155. _vprintf_wa(out_func_t out, void *ctx, const char *fmt, _va_list ap,
  156. wasm_module_inst_t module_inst)
  157. {
  158. int might_format = 0; /* 1 if encountered a '%' */
  159. enum pad_type padding = PAD_NONE;
  160. int min_width = -1;
  161. int long_ctr = 0;
  162. uint8 *native_end_addr;
  163. if (!wasm_runtime_get_native_addr_range(module_inst, (uint8 *)ap, NULL,
  164. &native_end_addr))
  165. goto fail;
  166. /* fmt has already been adjusted if needed */
  167. while (*fmt) {
  168. if (!might_format) {
  169. if (*fmt != '%') {
  170. out((int)*fmt, ctx);
  171. }
  172. else {
  173. might_format = 1;
  174. min_width = -1;
  175. padding = PAD_NONE;
  176. long_ctr = 0;
  177. }
  178. }
  179. else {
  180. switch (*fmt) {
  181. case '-':
  182. padding = PAD_SPACE_AFTER;
  183. goto still_might_format;
  184. case '0':
  185. if (min_width < 0 && padding == PAD_NONE) {
  186. padding = PAD_ZERO_BEFORE;
  187. goto still_might_format;
  188. }
  189. goto handle_1_to_9;
  190. case '1':
  191. case '2':
  192. case '3':
  193. case '4':
  194. case '5':
  195. case '6':
  196. case '7':
  197. case '8':
  198. case '9':
  199. handle_1_to_9:
  200. if (min_width < 0) {
  201. min_width = *fmt - '0';
  202. }
  203. else {
  204. min_width = 10 * min_width + *fmt - '0';
  205. }
  206. if (padding == PAD_NONE) {
  207. padding = PAD_SPACE_BEFORE;
  208. }
  209. goto still_might_format;
  210. case 'l':
  211. long_ctr++;
  212. /* Fall through */
  213. case 'z':
  214. case 'h':
  215. /* FIXME: do nothing for these modifiers */
  216. goto still_might_format;
  217. case 'd':
  218. case 'i':
  219. {
  220. int32 d;
  221. if (long_ctr < 2) {
  222. CHECK_VA_ARG(ap, int32);
  223. d = _va_arg(ap, int32);
  224. }
  225. else {
  226. int64 lld;
  227. CHECK_VA_ARG(ap, int64);
  228. lld = _va_arg(ap, int64);
  229. if (lld > INT32_MAX || lld < INT32_MIN) {
  230. print_err(out, ctx);
  231. break;
  232. }
  233. d = (int32)lld;
  234. }
  235. if (d < 0) {
  236. out((int)'-', ctx);
  237. d = -d;
  238. min_width--;
  239. }
  240. _printf_dec_uint(out, ctx, (uint32)d, padding, min_width);
  241. break;
  242. }
  243. case 'u':
  244. {
  245. uint32 u;
  246. if (long_ctr < 2) {
  247. CHECK_VA_ARG(ap, uint32);
  248. u = _va_arg(ap, uint32);
  249. }
  250. else {
  251. uint64 llu;
  252. CHECK_VA_ARG(ap, uint64);
  253. llu = _va_arg(ap, uint64);
  254. if (llu > INT32_MAX) {
  255. print_err(out, ctx);
  256. break;
  257. }
  258. u = (uint32)llu;
  259. }
  260. _printf_dec_uint(out, ctx, u, padding, min_width);
  261. break;
  262. }
  263. case 'p':
  264. out('0', ctx);
  265. out('x', ctx);
  266. /* left-pad pointers with zeros */
  267. padding = PAD_ZERO_BEFORE;
  268. min_width = 8;
  269. /* Fall through */
  270. case 'x':
  271. case 'X':
  272. {
  273. uint64 x;
  274. bool is_ptr = (*fmt == 'p') ? true : false;
  275. if (long_ctr < 2) {
  276. CHECK_VA_ARG(ap, uint32);
  277. x = _va_arg(ap, uint32);
  278. }
  279. else {
  280. CHECK_VA_ARG(ap, uint64);
  281. x = _va_arg(ap, uint64);
  282. }
  283. _printf_hex_uint(out, ctx, x, !is_ptr, padding, min_width);
  284. break;
  285. }
  286. case 's':
  287. {
  288. char *s;
  289. char *start;
  290. uint32 s_offset;
  291. CHECK_VA_ARG(ap, int32);
  292. s_offset = _va_arg(ap, uint32);
  293. if (!validate_app_str_addr(s_offset)) {
  294. return false;
  295. }
  296. s = start = addr_app_to_native(s_offset);
  297. while (*s)
  298. out((int)(*s++), ctx);
  299. if (padding == PAD_SPACE_AFTER) {
  300. int remaining = min_width - (int32)(s - start);
  301. while (remaining-- > 0) {
  302. out(' ', ctx);
  303. }
  304. }
  305. break;
  306. }
  307. case 'c':
  308. {
  309. int c;
  310. CHECK_VA_ARG(ap, int);
  311. c = _va_arg(ap, int);
  312. out(c, ctx);
  313. break;
  314. }
  315. case '%':
  316. {
  317. out((int)'%', ctx);
  318. break;
  319. }
  320. case 'f':
  321. {
  322. float64 f64;
  323. char buf[16], *s;
  324. /* Make 8-byte aligned */
  325. ap = (_va_list)(((uintptr_t)ap + 7) & ~(uintptr_t)7);
  326. CHECK_VA_ARG(ap, float64);
  327. f64 = _va_arg(ap, float64);
  328. snprintf(buf, sizeof(buf), "%f", f64);
  329. s = buf;
  330. while (*s)
  331. out((int)(*s++), ctx);
  332. break;
  333. }
  334. default:
  335. out((int)'%', ctx);
  336. out((int)*fmt, ctx);
  337. break;
  338. }
  339. might_format = 0;
  340. }
  341. still_might_format:
  342. ++fmt;
  343. }
  344. return true;
  345. fail:
  346. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  347. return false;
  348. }
  349. struct str_context {
  350. char *str;
  351. uint32 max;
  352. uint32 count;
  353. };
  354. static int
  355. sprintf_out(int c, struct str_context *ctx)
  356. {
  357. if (!ctx->str || ctx->count >= ctx->max) {
  358. ctx->count++;
  359. return c;
  360. }
  361. if (ctx->count == ctx->max - 1) {
  362. ctx->str[ctx->count++] = '\0';
  363. }
  364. else {
  365. ctx->str[ctx->count++] = (char)c;
  366. }
  367. return c;
  368. }
  369. #ifdef BH_PLATFORM_OPENRTOS
  370. PRIVILEGED_DATA static char print_buf[128] = { 0 };
  371. PRIVILEGED_DATA static int print_buf_size = 0;
  372. static int
  373. printf_out(int c, struct str_context *ctx)
  374. {
  375. if (c == '\n') {
  376. print_buf[print_buf_size] = '\0';
  377. os_printf("%s\n", print_buf);
  378. print_buf_size = 0;
  379. }
  380. else if (print_buf_size >= sizeof(print_buf) - 2) {
  381. print_buf[print_buf_size++] = (char)c;
  382. print_buf[print_buf_size] = '\0';
  383. os_printf("%s\n", print_buf);
  384. print_buf_size = 0;
  385. }
  386. else {
  387. print_buf[print_buf_size++] = (char)c;
  388. }
  389. ctx->count++;
  390. return c;
  391. }
  392. #else
  393. static int
  394. printf_out(int c, struct str_context *ctx)
  395. {
  396. os_printf("%c", c);
  397. ctx->count++;
  398. return c;
  399. }
  400. #endif
  401. static int
  402. printf_wrapper(wasm_exec_env_t exec_env, const char *format, _va_list va_args)
  403. {
  404. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  405. struct str_context ctx = { NULL, 0, 0 };
  406. /* format has been checked by runtime */
  407. if (!validate_native_addr(va_args, sizeof(int32)))
  408. return 0;
  409. if (!_vprintf_wa((out_func_t)printf_out, &ctx, format, va_args,
  410. module_inst))
  411. return 0;
  412. return (int)ctx.count;
  413. }
  414. static int
  415. sprintf_wrapper(wasm_exec_env_t exec_env, char *str, const char *format,
  416. _va_list va_args)
  417. {
  418. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  419. uint8 *native_end_offset;
  420. struct str_context ctx;
  421. /* str and format have been checked by runtime */
  422. if (!validate_native_addr(va_args, sizeof(uint32)))
  423. return 0;
  424. if (!wasm_runtime_get_native_addr_range(module_inst, (uint8 *)str, NULL,
  425. &native_end_offset)) {
  426. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  427. return false;
  428. }
  429. ctx.str = str;
  430. ctx.max = (uint32)(native_end_offset - (uint8 *)str);
  431. ctx.count = 0;
  432. if (!_vprintf_wa((out_func_t)sprintf_out, &ctx, format, va_args,
  433. module_inst))
  434. return 0;
  435. if (ctx.count < ctx.max) {
  436. str[ctx.count] = '\0';
  437. }
  438. return (int)ctx.count;
  439. }
  440. static int
  441. snprintf_wrapper(wasm_exec_env_t exec_env, char *str, uint32 size,
  442. const char *format, _va_list va_args)
  443. {
  444. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  445. struct str_context ctx;
  446. /* str and format have been checked by runtime */
  447. if (!validate_native_addr(va_args, sizeof(uint32)))
  448. return 0;
  449. ctx.str = str;
  450. ctx.max = size;
  451. ctx.count = 0;
  452. if (!_vprintf_wa((out_func_t)sprintf_out, &ctx, format, va_args,
  453. module_inst))
  454. return 0;
  455. if (ctx.count < ctx.max) {
  456. str[ctx.count] = '\0';
  457. }
  458. return (int)ctx.count;
  459. }
  460. static int
  461. puts_wrapper(wasm_exec_env_t exec_env, const char *str)
  462. {
  463. return os_printf("%s\n", str);
  464. }
  465. static int
  466. putchar_wrapper(wasm_exec_env_t exec_env, int c)
  467. {
  468. os_printf("%c", c);
  469. return 1;
  470. }
  471. static uint32
  472. strdup_wrapper(wasm_exec_env_t exec_env, const char *str)
  473. {
  474. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  475. char *str_ret;
  476. uint32 len;
  477. uint32 str_ret_offset = 0;
  478. /* str has been checked by runtime */
  479. if (str) {
  480. len = (uint32)strlen(str) + 1;
  481. str_ret_offset = module_malloc(len, (void **)&str_ret);
  482. if (str_ret_offset) {
  483. bh_memcpy_s(str_ret, len, str, len);
  484. }
  485. }
  486. return str_ret_offset;
  487. }
  488. static uint32
  489. _strdup_wrapper(wasm_exec_env_t exec_env, const char *str)
  490. {
  491. return strdup_wrapper(exec_env, str);
  492. }
  493. static int32
  494. memcmp_wrapper(wasm_exec_env_t exec_env, const void *s1, const void *s2,
  495. uint32 size)
  496. {
  497. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  498. /* s2 has been checked by runtime */
  499. if (!validate_native_addr((void *)s1, size))
  500. return 0;
  501. return memcmp(s1, s2, size);
  502. }
  503. static uint32
  504. memcpy_wrapper(wasm_exec_env_t exec_env, void *dst, const void *src,
  505. uint32 size)
  506. {
  507. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  508. uint32 dst_offset = addr_native_to_app(dst);
  509. if (size == 0)
  510. return dst_offset;
  511. /* src has been checked by runtime */
  512. if (!validate_native_addr(dst, size))
  513. return dst_offset;
  514. bh_memcpy_s(dst, size, src, size);
  515. return dst_offset;
  516. }
  517. static uint32
  518. memmove_wrapper(wasm_exec_env_t exec_env, void *dst, void *src, uint32 size)
  519. {
  520. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  521. uint32 dst_offset = addr_native_to_app(dst);
  522. if (size == 0)
  523. return dst_offset;
  524. /* src has been checked by runtime */
  525. if (!validate_native_addr(dst, size))
  526. return dst_offset;
  527. memmove(dst, src, size);
  528. return dst_offset;
  529. }
  530. static uint32
  531. memset_wrapper(wasm_exec_env_t exec_env, void *s, int32 c, uint32 size)
  532. {
  533. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  534. uint32 s_offset = addr_native_to_app(s);
  535. if (!validate_native_addr(s, size))
  536. return s_offset;
  537. memset(s, c, size);
  538. return s_offset;
  539. }
  540. static uint32
  541. strchr_wrapper(wasm_exec_env_t exec_env, const char *s, int32 c)
  542. {
  543. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  544. char *ret;
  545. /* s has been checked by runtime */
  546. ret = strchr(s, c);
  547. return ret ? addr_native_to_app(ret) : 0;
  548. }
  549. static int32
  550. strcmp_wrapper(wasm_exec_env_t exec_env, const char *s1, const char *s2)
  551. {
  552. /* s1 and s2 have been checked by runtime */
  553. return strcmp(s1, s2);
  554. }
  555. static int32
  556. strncmp_wrapper(wasm_exec_env_t exec_env, const char *s1, const char *s2,
  557. uint32 size)
  558. {
  559. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  560. /* s2 has been checked by runtime */
  561. if (!validate_native_addr((void *)s1, size))
  562. return 0;
  563. return strncmp(s1, s2, size);
  564. }
  565. static uint32
  566. strcpy_wrapper(wasm_exec_env_t exec_env, char *dst, const char *src)
  567. {
  568. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  569. uint32 len = (uint32)strlen(src) + 1;
  570. /* src has been checked by runtime */
  571. if (!validate_native_addr(dst, len))
  572. return 0;
  573. #ifndef BH_PLATFORM_WINDOWS
  574. strncpy(dst, src, len);
  575. #else
  576. strncpy_s(dst, len, src, len);
  577. #endif
  578. return addr_native_to_app(dst);
  579. }
  580. static uint32
  581. strncpy_wrapper(wasm_exec_env_t exec_env, char *dst, const char *src,
  582. uint32 size)
  583. {
  584. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  585. /* src has been checked by runtime */
  586. if (!validate_native_addr(dst, size))
  587. return 0;
  588. #ifndef BH_PLATFORM_WINDOWS
  589. strncpy(dst, src, size);
  590. #else
  591. strncpy_s(dst, size, src, size);
  592. #endif
  593. return addr_native_to_app(dst);
  594. }
  595. static uint32
  596. strlen_wrapper(wasm_exec_env_t exec_env, const char *s)
  597. {
  598. /* s has been checked by runtime */
  599. return (uint32)strlen(s);
  600. }
  601. static uint32
  602. malloc_wrapper(wasm_exec_env_t exec_env, uint32 size)
  603. {
  604. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  605. return module_malloc(size, NULL);
  606. }
  607. static uint32
  608. calloc_wrapper(wasm_exec_env_t exec_env, uint32 nmemb, uint32 size)
  609. {
  610. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  611. uint64 total_size = (uint64)nmemb * (uint64)size;
  612. uint32 ret_offset = 0;
  613. uint8 *ret_ptr;
  614. if (total_size >= UINT32_MAX)
  615. return 0;
  616. ret_offset = module_malloc((uint32)total_size, (void **)&ret_ptr);
  617. if (ret_offset) {
  618. memset(ret_ptr, 0, (uint32)total_size);
  619. }
  620. return ret_offset;
  621. }
  622. static uint32
  623. realloc_wrapper(wasm_exec_env_t exec_env, uint32 ptr, uint32 new_size)
  624. {
  625. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  626. return wasm_runtime_module_realloc(module_inst, ptr, new_size, NULL);
  627. }
  628. static void
  629. free_wrapper(wasm_exec_env_t exec_env, void *ptr)
  630. {
  631. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  632. if (!validate_native_addr(ptr, sizeof(uint32)))
  633. return;
  634. module_free(addr_native_to_app(ptr));
  635. }
  636. static int32
  637. atoi_wrapper(wasm_exec_env_t exec_env, const char *s)
  638. {
  639. /* s has been checked by runtime */
  640. return atoi(s);
  641. }
  642. static void
  643. exit_wrapper(wasm_exec_env_t exec_env, int32 status)
  644. {
  645. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  646. char buf[32];
  647. snprintf(buf, sizeof(buf), "env.exit(%i)", status);
  648. wasm_runtime_set_exception(module_inst, buf);
  649. }
  650. static int32
  651. strtol_wrapper(wasm_exec_env_t exec_env, const char *nptr, char **endptr,
  652. int32 base)
  653. {
  654. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  655. int32 num = 0;
  656. /* nptr has been checked by runtime */
  657. if (!validate_native_addr(endptr, sizeof(uint32)))
  658. return 0;
  659. num = (int32)strtol(nptr, endptr, base);
  660. *(uint32 *)endptr = addr_native_to_app(*endptr);
  661. return num;
  662. }
  663. static uint32
  664. strtoul_wrapper(wasm_exec_env_t exec_env, const char *nptr, char **endptr,
  665. int32 base)
  666. {
  667. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  668. uint32 num = 0;
  669. /* nptr has been checked by runtime */
  670. if (!validate_native_addr(endptr, sizeof(uint32)))
  671. return 0;
  672. num = (uint32)strtoul(nptr, endptr, base);
  673. *(uint32 *)endptr = addr_native_to_app(*endptr);
  674. return num;
  675. }
  676. static uint32
  677. memchr_wrapper(wasm_exec_env_t exec_env, const void *s, int32 c, uint32 n)
  678. {
  679. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  680. void *res;
  681. if (!validate_native_addr((void *)s, n))
  682. return 0;
  683. res = memchr(s, c, n);
  684. return addr_native_to_app(res);
  685. }
  686. static int32
  687. strncasecmp_wrapper(wasm_exec_env_t exec_env, const char *s1, const char *s2,
  688. uint32 n)
  689. {
  690. /* s1 and s2 have been checked by runtime */
  691. return strncasecmp(s1, s2, n);
  692. }
  693. static uint32
  694. strspn_wrapper(wasm_exec_env_t exec_env, const char *s, const char *accept)
  695. {
  696. /* s and accept have been checked by runtime */
  697. return (uint32)strspn(s, accept);
  698. }
  699. static uint32
  700. strcspn_wrapper(wasm_exec_env_t exec_env, const char *s, const char *reject)
  701. {
  702. /* s and reject have been checked by runtime */
  703. return (uint32)strcspn(s, reject);
  704. }
  705. static uint32
  706. strstr_wrapper(wasm_exec_env_t exec_env, const char *s, const char *find)
  707. {
  708. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  709. /* s and find have been checked by runtime */
  710. char *res = strstr(s, find);
  711. return addr_native_to_app(res);
  712. }
  713. static int32
  714. isupper_wrapper(wasm_exec_env_t exec_env, int32 c)
  715. {
  716. return isupper(c);
  717. }
  718. static int32
  719. isalpha_wrapper(wasm_exec_env_t exec_env, int32 c)
  720. {
  721. return isalpha(c);
  722. }
  723. static int32
  724. isspace_wrapper(wasm_exec_env_t exec_env, int32 c)
  725. {
  726. return isspace(c);
  727. }
  728. static int32
  729. isgraph_wrapper(wasm_exec_env_t exec_env, int32 c)
  730. {
  731. return isgraph(c);
  732. }
  733. static int32
  734. isprint_wrapper(wasm_exec_env_t exec_env, int32 c)
  735. {
  736. return isprint(c);
  737. }
  738. static int32
  739. isdigit_wrapper(wasm_exec_env_t exec_env, int32 c)
  740. {
  741. return isdigit(c);
  742. }
  743. static int32
  744. isxdigit_wrapper(wasm_exec_env_t exec_env, int32 c)
  745. {
  746. return isxdigit(c);
  747. }
  748. static int32
  749. tolower_wrapper(wasm_exec_env_t exec_env, int32 c)
  750. {
  751. return tolower(c);
  752. }
  753. static int32
  754. toupper_wrapper(wasm_exec_env_t exec_env, int32 c)
  755. {
  756. return toupper(c);
  757. }
  758. static int32
  759. isalnum_wrapper(wasm_exec_env_t exec_env, int32 c)
  760. {
  761. return isalnum(c);
  762. }
  763. static void
  764. setTempRet0_wrapper(wasm_exec_env_t exec_env, uint32 temp_ret)
  765. {
  766. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  767. wasm_runtime_set_temp_ret(module_inst, temp_ret);
  768. }
  769. static uint32
  770. getTempRet0_wrapper(wasm_exec_env_t exec_env)
  771. {
  772. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  773. return wasm_runtime_get_temp_ret(module_inst);
  774. }
  775. static uint32
  776. llvm_bswap_i16_wrapper(wasm_exec_env_t exec_env, uint32 data)
  777. {
  778. return (data & 0xFFFF0000) | ((data & 0xFF) << 8) | ((data & 0xFF00) >> 8);
  779. }
  780. static uint32
  781. llvm_bswap_i32_wrapper(wasm_exec_env_t exec_env, uint32 data)
  782. {
  783. return ((data & 0xFF) << 24) | ((data & 0xFF00) << 8)
  784. | ((data & 0xFF0000) >> 8) | ((data & 0xFF000000) >> 24);
  785. }
  786. static uint32
  787. bitshift64Lshr_wrapper(wasm_exec_env_t exec_env, uint32 uint64_part0,
  788. uint32 uint64_part1, uint32 bits)
  789. {
  790. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  791. union {
  792. uint64 value;
  793. uint32 parts[2];
  794. } u;
  795. u.parts[0] = uint64_part0;
  796. u.parts[1] = uint64_part1;
  797. u.value >>= bits;
  798. /* return low 32bit and save high 32bit to temp ret */
  799. wasm_runtime_set_temp_ret(module_inst, (uint32)(u.value >> 32));
  800. return (uint32)u.value;
  801. }
  802. static uint32
  803. bitshift64Shl_wrapper(wasm_exec_env_t exec_env, uint32 int64_part0,
  804. uint32 int64_part1, uint32 bits)
  805. {
  806. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  807. union {
  808. int64 value;
  809. uint32 parts[2];
  810. } u;
  811. u.parts[0] = int64_part0;
  812. u.parts[1] = int64_part1;
  813. u.value <<= bits;
  814. /* return low 32bit and save high 32bit to temp ret */
  815. wasm_runtime_set_temp_ret(module_inst, (uint32)(u.value >> 32));
  816. return (uint32)u.value;
  817. }
  818. static void
  819. llvm_stackrestore_wrapper(wasm_exec_env_t exec_env, uint32 llvm_stack)
  820. {
  821. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  822. os_printf("_llvm_stackrestore called!\n");
  823. wasm_runtime_set_llvm_stack(module_inst, llvm_stack);
  824. }
  825. static uint32
  826. llvm_stacksave_wrapper(wasm_exec_env_t exec_env)
  827. {
  828. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  829. os_printf("_llvm_stacksave called!\n");
  830. return wasm_runtime_get_llvm_stack(module_inst);
  831. }
  832. static uint32
  833. emscripten_memcpy_big_wrapper(wasm_exec_env_t exec_env, void *dst,
  834. const void *src, uint32 size)
  835. {
  836. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  837. uint32 dst_offset = addr_native_to_app(dst);
  838. /* src has been checked by runtime */
  839. if (!validate_native_addr(dst, size))
  840. return dst_offset;
  841. bh_memcpy_s(dst, size, src, size);
  842. return dst_offset;
  843. }
  844. static void
  845. abort_wrapper(wasm_exec_env_t exec_env, int32 code)
  846. {
  847. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  848. char buf[32];
  849. snprintf(buf, sizeof(buf), "env.abort(%i)", code);
  850. wasm_runtime_set_exception(module_inst, buf);
  851. }
  852. static void
  853. abortStackOverflow_wrapper(wasm_exec_env_t exec_env, int32 code)
  854. {
  855. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  856. char buf[32];
  857. snprintf(buf, sizeof(buf), "env.abortStackOverflow(%i)", code);
  858. wasm_runtime_set_exception(module_inst, buf);
  859. }
  860. static void
  861. nullFunc_X_wrapper(wasm_exec_env_t exec_env, int32 code)
  862. {
  863. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  864. char buf[32];
  865. snprintf(buf, sizeof(buf), "env.nullFunc_X(%i)", code);
  866. wasm_runtime_set_exception(module_inst, buf);
  867. }
  868. static uint32
  869. __cxa_allocate_exception_wrapper(wasm_exec_env_t exec_env, uint32 thrown_size)
  870. {
  871. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  872. uint32 exception = module_malloc(thrown_size, NULL);
  873. if (!exception)
  874. return 0;
  875. return exception;
  876. }
  877. static void
  878. __cxa_begin_catch_wrapper(wasm_exec_env_t exec_env, void *exception_object)
  879. {}
  880. static void
  881. __cxa_throw_wrapper(wasm_exec_env_t exec_env, void *thrown_exception,
  882. void *tinfo, uint32 table_elem_idx)
  883. {
  884. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  885. char buf[32];
  886. snprintf(buf, sizeof(buf), "%s", "exception thrown by stdc++");
  887. wasm_runtime_set_exception(module_inst, buf);
  888. }
  889. struct timespec_app {
  890. int64 tv_sec;
  891. int32 tv_nsec;
  892. };
  893. static uint32
  894. clock_gettime_wrapper(wasm_exec_env_t exec_env, uint32 clk_id,
  895. struct timespec_app *ts_app)
  896. {
  897. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  898. uint64 time;
  899. if (!validate_native_addr(ts_app, sizeof(struct timespec_app)))
  900. return (uint32)-1;
  901. time = os_time_get_boot_microsecond();
  902. ts_app->tv_sec = time / 1000000;
  903. ts_app->tv_nsec = (time % 1000000) * 1000;
  904. return (uint32)0;
  905. }
  906. static uint64
  907. clock_wrapper(wasm_exec_env_t exec_env)
  908. {
  909. /* Convert to nano seconds as CLOCKS_PER_SEC in wasi-sdk */
  910. return os_time_get_boot_microsecond() * 1000;
  911. }
  912. #if WASM_ENABLE_SPEC_TEST != 0
  913. static void
  914. print_wrapper(wasm_exec_env_t exec_env)
  915. {
  916. os_printf("in specttest.print()\n");
  917. }
  918. static void
  919. print_i32_wrapper(wasm_exec_env_t exec_env, int32 i32)
  920. {
  921. os_printf("in specttest.print_i32(%d)\n", i32);
  922. }
  923. static void
  924. print_i32_f32_wrapper(wasm_exec_env_t exec_env, int32 i32, float f32)
  925. {
  926. os_printf("in specttest.print_i32_f32(%d, %f)\n", i32, f32);
  927. }
  928. static void
  929. print_f64_f64_wrapper(wasm_exec_env_t exec_env, double f64_1, double f64_2)
  930. {
  931. os_printf("in specttest.print_f64_f64(%f, %f)\n", f64_1, f64_2);
  932. }
  933. static void
  934. print_f32_wrapper(wasm_exec_env_t exec_env, float f32)
  935. {
  936. os_printf("in specttest.print_f32(%f)\n", f32);
  937. }
  938. static void
  939. print_f64_wrapper(wasm_exec_env_t exec_env, double f64)
  940. {
  941. os_printf("in specttest.print_f64(%f)\n", f64);
  942. }
  943. #endif /* WASM_ENABLE_SPEC_TEST */
  944. #define REG_NATIVE_FUNC(func_name, signature) \
  945. { \
  946. #func_name, func_name##_wrapper, signature, NULL \
  947. }
  948. static NativeSymbol native_symbols_libc_builtin[] = {
  949. REG_NATIVE_FUNC(printf, "($*)i"),
  950. REG_NATIVE_FUNC(sprintf, "($$*)i"),
  951. REG_NATIVE_FUNC(snprintf, "(*~$*)i"),
  952. REG_NATIVE_FUNC(puts, "($)i"),
  953. REG_NATIVE_FUNC(putchar, "(i)i"),
  954. REG_NATIVE_FUNC(memcmp, "(**~)i"),
  955. REG_NATIVE_FUNC(memcpy, "(**~)i"),
  956. REG_NATIVE_FUNC(memmove, "(**~)i"),
  957. REG_NATIVE_FUNC(memset, "(*ii)i"),
  958. REG_NATIVE_FUNC(strchr, "($i)i"),
  959. REG_NATIVE_FUNC(strcmp, "($$)i"),
  960. REG_NATIVE_FUNC(strcpy, "(*$)i"),
  961. REG_NATIVE_FUNC(strlen, "($)i"),
  962. REG_NATIVE_FUNC(strncmp, "(**~)i"),
  963. REG_NATIVE_FUNC(strncpy, "(**~)i"),
  964. REG_NATIVE_FUNC(malloc, "(i)i"),
  965. REG_NATIVE_FUNC(realloc, "(ii)i"),
  966. REG_NATIVE_FUNC(calloc, "(ii)i"),
  967. REG_NATIVE_FUNC(strdup, "($)i"),
  968. /* clang may introduce __strdup */
  969. REG_NATIVE_FUNC(_strdup, "($)i"),
  970. REG_NATIVE_FUNC(free, "(*)"),
  971. REG_NATIVE_FUNC(atoi, "($)i"),
  972. REG_NATIVE_FUNC(exit, "(i)"),
  973. REG_NATIVE_FUNC(strtol, "($*i)i"),
  974. REG_NATIVE_FUNC(strtoul, "($*i)i"),
  975. REG_NATIVE_FUNC(memchr, "(*ii)i"),
  976. REG_NATIVE_FUNC(strncasecmp, "($$i)"),
  977. REG_NATIVE_FUNC(strspn, "($$)i"),
  978. REG_NATIVE_FUNC(strcspn, "($$)i"),
  979. REG_NATIVE_FUNC(strstr, "($$)i"),
  980. REG_NATIVE_FUNC(isupper, "(i)i"),
  981. REG_NATIVE_FUNC(isalpha, "(i)i"),
  982. REG_NATIVE_FUNC(isspace, "(i)i"),
  983. REG_NATIVE_FUNC(isgraph, "(i)i"),
  984. REG_NATIVE_FUNC(isprint, "(i)i"),
  985. REG_NATIVE_FUNC(isdigit, "(i)i"),
  986. REG_NATIVE_FUNC(isxdigit, "(i)i"),
  987. REG_NATIVE_FUNC(tolower, "(i)i"),
  988. REG_NATIVE_FUNC(toupper, "(i)i"),
  989. REG_NATIVE_FUNC(isalnum, "(i)i"),
  990. REG_NATIVE_FUNC(setTempRet0, "(i)"),
  991. REG_NATIVE_FUNC(getTempRet0, "()i"),
  992. REG_NATIVE_FUNC(llvm_bswap_i16, "(i)i"),
  993. REG_NATIVE_FUNC(llvm_bswap_i32, "(i)i"),
  994. REG_NATIVE_FUNC(bitshift64Lshr, "(iii)i"),
  995. REG_NATIVE_FUNC(bitshift64Shl, "(iii)i"),
  996. REG_NATIVE_FUNC(llvm_stackrestore, "(i)"),
  997. REG_NATIVE_FUNC(llvm_stacksave, "()i"),
  998. REG_NATIVE_FUNC(emscripten_memcpy_big, "(**~)i"),
  999. REG_NATIVE_FUNC(abort, "(i)"),
  1000. REG_NATIVE_FUNC(abortStackOverflow, "(i)"),
  1001. REG_NATIVE_FUNC(nullFunc_X, "(i)"),
  1002. REG_NATIVE_FUNC(__cxa_allocate_exception, "(i)i"),
  1003. REG_NATIVE_FUNC(__cxa_begin_catch, "(*)"),
  1004. REG_NATIVE_FUNC(__cxa_throw, "(**i)"),
  1005. REG_NATIVE_FUNC(clock_gettime, "(i*)i"),
  1006. REG_NATIVE_FUNC(clock, "()I"),
  1007. };
  1008. #if WASM_ENABLE_SPEC_TEST != 0
  1009. static NativeSymbol native_symbols_spectest[] = {
  1010. REG_NATIVE_FUNC(print, "()"),
  1011. REG_NATIVE_FUNC(print_i32, "(i)"),
  1012. REG_NATIVE_FUNC(print_i32_f32, "(if)"),
  1013. REG_NATIVE_FUNC(print_f64_f64, "(FF)"),
  1014. REG_NATIVE_FUNC(print_f32, "(f)"),
  1015. REG_NATIVE_FUNC(print_f64, "(F)")
  1016. };
  1017. #endif
  1018. uint32
  1019. get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis)
  1020. {
  1021. *p_libc_builtin_apis = native_symbols_libc_builtin;
  1022. return sizeof(native_symbols_libc_builtin) / sizeof(NativeSymbol);
  1023. }
  1024. #if WASM_ENABLE_SPEC_TEST != 0
  1025. uint32
  1026. get_spectest_export_apis(NativeSymbol **p_libc_builtin_apis)
  1027. {
  1028. *p_libc_builtin_apis = native_symbols_spectest;
  1029. return sizeof(native_symbols_spectest) / sizeof(NativeSymbol);
  1030. }
  1031. #endif
  1032. /*************************************
  1033. * Global Variables *
  1034. *************************************/
  1035. typedef struct WASMNativeGlobalDef {
  1036. const char *module_name;
  1037. const char *global_name;
  1038. uint8 type;
  1039. bool is_mutable;
  1040. WASMValue value;
  1041. } WASMNativeGlobalDef;
  1042. static WASMNativeGlobalDef native_global_defs[] = {
  1043. #if WASM_ENABLE_SPEC_TEST != 0
  1044. { "spectest", "global_i32", VALUE_TYPE_I32, false, .value.i32 = 666 },
  1045. { "spectest", "global_i64", VALUE_TYPE_I64, false, .value.i64 = 666 },
  1046. { "spectest", "global_f32", VALUE_TYPE_F32, false, .value.f32 = 666.6 },
  1047. { "spectest", "global_f64", VALUE_TYPE_F64, false, .value.f64 = 666.6 },
  1048. { "test", "global-i32", VALUE_TYPE_I32, false, .value.i32 = 0 },
  1049. { "test", "global-f32", VALUE_TYPE_F32, false, .value.f32 = 0 },
  1050. { "test", "global-mut-i32", VALUE_TYPE_I32, true, .value.i32 = 0 },
  1051. { "test", "global-mut-i64", VALUE_TYPE_I64, true, .value.i64 = 0 },
  1052. #endif
  1053. { "global", "NaN", VALUE_TYPE_F64, .value.u64 = 0x7FF8000000000000LL },
  1054. { "global", "Infinity", VALUE_TYPE_F64, .value.u64 = 0x7FF0000000000000LL }
  1055. };
  1056. bool
  1057. wasm_native_lookup_libc_builtin_global(const char *module_name,
  1058. const char *global_name,
  1059. WASMGlobalImport *global)
  1060. {
  1061. uint32 size = sizeof(native_global_defs) / sizeof(WASMNativeGlobalDef);
  1062. WASMNativeGlobalDef *global_def = native_global_defs;
  1063. WASMNativeGlobalDef *global_def_end = global_def + size;
  1064. if (!module_name || !global_name || !global)
  1065. return false;
  1066. /* Lookup constant globals which can be defined by table */
  1067. while (global_def < global_def_end) {
  1068. if (!strcmp(global_def->module_name, module_name)
  1069. && !strcmp(global_def->global_name, global_name)) {
  1070. global->type = global_def->type;
  1071. global->is_mutable = global_def->is_mutable;
  1072. global->global_data_linked = global_def->value;
  1073. return true;
  1074. }
  1075. global_def++;
  1076. }
  1077. return false;
  1078. }