libc_builtin_wrapper.c 31 KB

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