libc_builtin_wrapper.c 31 KB

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