libc_wrapper.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "wasm_native.h"
  17. #include "wasm_export.h"
  18. #include "wasm_log.h"
  19. #include "wasm_platform_log.h"
  20. void
  21. wasm_runtime_set_exception(wasm_module_inst_t module, const char *exception);
  22. uint32
  23. wasm_runtime_get_temp_ret(wasm_module_inst_t module);
  24. void
  25. wasm_runtime_set_temp_ret(wasm_module_inst_t module, uint32 temp_ret);
  26. uint32
  27. wasm_runtime_get_llvm_stack(wasm_module_inst_t module);
  28. void
  29. wasm_runtime_set_llvm_stack(wasm_module_inst_t module, uint32 llvm_stack);
  30. #define validate_app_addr(offset, size) \
  31. wasm_runtime_validate_app_addr(module_inst, offset, size)
  32. #define validate_app_str_addr(offset) \
  33. wasm_runtime_validate_app_str_addr(module_inst, offset)
  34. #define addr_app_to_native(offset) \
  35. wasm_runtime_addr_app_to_native(module_inst, offset)
  36. #define addr_native_to_app(ptr) \
  37. wasm_runtime_addr_native_to_app(module_inst, ptr)
  38. #define module_malloc(size) \
  39. wasm_runtime_module_malloc(module_inst, size)
  40. #define module_free(offset) \
  41. wasm_runtime_module_free(module_inst, offset)
  42. typedef int (*out_func_t)(int c, void *ctx);
  43. enum pad_type {
  44. PAD_NONE,
  45. PAD_ZERO_BEFORE,
  46. PAD_SPACE_BEFORE,
  47. PAD_SPACE_AFTER,
  48. };
  49. typedef char *_va_list;
  50. #define _INTSIZEOF(n) \
  51. ((sizeof(n) + 3) & ~3)
  52. #define _va_arg(ap, t) \
  53. (*(t*)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)))
  54. #define CHECK_VA_ARG(ap, t) do { \
  55. if ((uint8*)ap + _INTSIZEOF(t) > native_end_addr) \
  56. goto fail; \
  57. } while (0)
  58. /**
  59. * @brief Output an unsigned int in hex format
  60. *
  61. * Output an unsigned int on output installed by platform at init time. Should
  62. * be able to handle an unsigned int of any size, 32 or 64 bit.
  63. * @param num Number to output
  64. *
  65. * @return N/A
  66. */
  67. static void
  68. _printf_hex_uint(out_func_t out, void *ctx,
  69. const uint64 num, bool is_u64,
  70. enum pad_type padding,
  71. int min_width)
  72. {
  73. int size = sizeof(num) * (is_u64 ? 2 : 1);
  74. int found_largest_digit = 0;
  75. int remaining = 8; /* 8 digits max */
  76. int digits = 0;
  77. for (; size; size--) {
  78. char nibble = (num >> ((size - 1) << 2) & 0xf);
  79. if (nibble || found_largest_digit || size == 1) {
  80. found_largest_digit = 1;
  81. nibble += nibble > 9 ? 87 : 48;
  82. out((int) nibble, ctx);
  83. digits++;
  84. continue;
  85. }
  86. if (remaining-- <= min_width) {
  87. if (padding == PAD_ZERO_BEFORE) {
  88. out('0', ctx);
  89. } else if (padding == PAD_SPACE_BEFORE) {
  90. out(' ', ctx);
  91. }
  92. }
  93. }
  94. if (padding == PAD_SPACE_AFTER) {
  95. remaining = min_width * 2 - digits;
  96. while (remaining-- > 0) {
  97. out(' ', ctx);
  98. }
  99. }
  100. }
  101. /**
  102. * @brief Output an unsigned int in decimal format
  103. *
  104. * Output an unsigned int on output installed by platform at init time. Only
  105. * works with 32-bit values.
  106. * @param num Number to output
  107. *
  108. * @return N/A
  109. */
  110. static void
  111. _printf_dec_uint(out_func_t out, void *ctx,
  112. const uint32 num,
  113. enum pad_type padding,
  114. 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. } else if (remaining <= min_width && padding < PAD_SPACE_AFTER) {
  131. out((int) (padding == PAD_ZERO_BEFORE ? '0' : ' '), ctx);
  132. digits++;
  133. }
  134. remaining--;
  135. remainder %= (pos + 1);
  136. pos /= 10;
  137. }
  138. out((int) (remainder + 48), ctx);
  139. if (padding == PAD_SPACE_AFTER) {
  140. remaining = min_width - digits;
  141. while (remaining-- > 0) {
  142. out(' ', ctx);
  143. }
  144. }
  145. }
  146. static void
  147. print_err(out_func_t out, void *ctx)
  148. {
  149. out('E', ctx);
  150. out('R', ctx);
  151. out('R', ctx);
  152. }
  153. static bool
  154. _vprintf_wa(out_func_t out, void *ctx, const char *fmt, _va_list ap,
  155. wasm_module_inst_t module_inst)
  156. {
  157. int might_format = 0; /* 1 if encountered a '%' */
  158. enum pad_type padding = PAD_NONE;
  159. int min_width = -1;
  160. int long_ctr = 0;
  161. uint8 *native_end_addr;
  162. if (!wasm_runtime_get_native_addr_range(module_inst, (uint8*)ap,
  163. NULL, &native_end_addr))
  164. goto fail;
  165. /* fmt has already been adjusted if needed */
  166. while (*fmt) {
  167. if (!might_format) {
  168. if (*fmt != '%') {
  169. out((int) *fmt, ctx);
  170. }
  171. else {
  172. might_format = 1;
  173. min_width = -1;
  174. padding = PAD_NONE;
  175. long_ctr = 0;
  176. }
  177. }
  178. else {
  179. switch (*fmt) {
  180. case '-':
  181. padding = PAD_SPACE_AFTER;
  182. goto still_might_format;
  183. case '0':
  184. if (min_width < 0 && padding == PAD_NONE) {
  185. padding = PAD_ZERO_BEFORE;
  186. goto still_might_format;
  187. }
  188. /* Fall through */
  189. case '1' ... '9':
  190. if (min_width < 0) {
  191. min_width = *fmt - '0';
  192. } else {
  193. min_width = 10 * min_width + *fmt - '0';
  194. }
  195. if (padding == PAD_NONE) {
  196. padding = PAD_SPACE_BEFORE;
  197. }
  198. goto still_might_format;
  199. case 'l':
  200. long_ctr++;
  201. /* Fall through */
  202. case 'z':
  203. case 'h':
  204. /* FIXME: do nothing for these modifiers */
  205. goto still_might_format;
  206. case 'd':
  207. case 'i': {
  208. int32 d;
  209. if (long_ctr < 2) {
  210. CHECK_VA_ARG(ap, int32);
  211. d = _va_arg(ap, int32);
  212. }
  213. else {
  214. int64 lld;
  215. CHECK_VA_ARG(ap, int64);
  216. lld = _va_arg(ap, int64);
  217. if (lld > INT32_MAX || lld < INT32_MIN) {
  218. print_err(out, ctx);
  219. break;
  220. }
  221. d = (int32)lld;
  222. }
  223. if (d < 0) {
  224. out((int)'-', ctx);
  225. d = -d;
  226. min_width--;
  227. }
  228. _printf_dec_uint(out, ctx, d, padding, min_width);
  229. break;
  230. }
  231. case 'u': {
  232. uint32 u;
  233. if (long_ctr < 2) {
  234. CHECK_VA_ARG(ap, uint32);
  235. u = _va_arg(ap, uint32);
  236. }
  237. else {
  238. uint64 llu;
  239. CHECK_VA_ARG(ap, uint64);
  240. llu = _va_arg(ap, uint64);
  241. if (llu > INT32_MAX) {
  242. print_err(out, ctx);
  243. break;
  244. }
  245. u = (uint32)llu;
  246. }
  247. _printf_dec_uint(out, ctx, u, padding, min_width);
  248. break;
  249. }
  250. case 'p':
  251. out('0', ctx);
  252. out('x', ctx);
  253. /* left-pad pointers with zeros */
  254. padding = PAD_ZERO_BEFORE;
  255. min_width = 8;
  256. /* Fall through */
  257. case 'x':
  258. case 'X': {
  259. uint64 x;
  260. bool is_ptr = (*fmt == 'p') ? true : false;
  261. if (long_ctr < 2) {
  262. CHECK_VA_ARG(ap, uint32);
  263. x = _va_arg(ap, uint32);
  264. } else {
  265. CHECK_VA_ARG(ap, uint64);
  266. x = _va_arg(ap, uint64);
  267. }
  268. _printf_hex_uint(out, ctx, x, !is_ptr, padding, min_width);
  269. break;
  270. }
  271. case 's': {
  272. char *s;
  273. char *start;
  274. int32 s_offset;
  275. CHECK_VA_ARG(ap, uint32);
  276. s_offset = _va_arg(ap, uint32);
  277. if (!validate_app_str_addr(s_offset)) {
  278. return false;
  279. }
  280. s = start = addr_app_to_native(s_offset);
  281. while (*s)
  282. out((int) (*s++), ctx);
  283. if (padding == PAD_SPACE_AFTER) {
  284. int remaining = min_width - (s - start);
  285. while (remaining-- > 0) {
  286. out(' ', ctx);
  287. }
  288. }
  289. break;
  290. }
  291. case 'c': {
  292. int c;
  293. CHECK_VA_ARG(ap, int);
  294. c = _va_arg(ap, int);
  295. out(c, ctx);
  296. break;
  297. }
  298. case '%': {
  299. out((int) '%', ctx);
  300. break;
  301. }
  302. default:
  303. out((int) '%', ctx);
  304. out((int) *fmt, ctx);
  305. break;
  306. }
  307. might_format = 0;
  308. }
  309. still_might_format:
  310. ++fmt;
  311. }
  312. return true;
  313. fail:
  314. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  315. return false;
  316. }
  317. struct str_context {
  318. char *str;
  319. int max;
  320. int count;
  321. };
  322. static int
  323. sprintf_out(int c, struct str_context *ctx)
  324. {
  325. if (!ctx->str || ctx->count >= ctx->max) {
  326. ctx->count++;
  327. return c;
  328. }
  329. if (ctx->count == ctx->max - 1) {
  330. ctx->str[ctx->count++] = '\0';
  331. } else {
  332. ctx->str[ctx->count++] = c;
  333. }
  334. return c;
  335. }
  336. static int
  337. printf_out(int c, struct str_context *ctx)
  338. {
  339. bh_printf("%c", c);
  340. ctx->count++;
  341. return c;
  342. }
  343. static inline _va_list
  344. get_va_list(uint32 *args)
  345. {
  346. union {
  347. uint32 u;
  348. _va_list v;
  349. } u;
  350. u.u = args[0];
  351. return u.v;
  352. }
  353. static bool
  354. parse_printf_args(wasm_module_inst_t module_inst, int32 fmt_offset,
  355. int32 va_list_offset, const char **p_fmt,
  356. _va_list *p_va_args)
  357. {
  358. const char *fmt;
  359. union {
  360. uintptr_t u;
  361. _va_list v;
  362. } u;
  363. if (!validate_app_str_addr(fmt_offset)
  364. || !validate_app_addr(va_list_offset, sizeof(int32)))
  365. return false;
  366. fmt = (const char*) addr_app_to_native(fmt_offset);
  367. u.u = (uintptr_t) addr_app_to_native(va_list_offset);
  368. *p_fmt = fmt;
  369. *p_va_args = u.v;
  370. return true;
  371. }
  372. static int
  373. _printf_wrapper(wasm_module_inst_t module_inst,
  374. int32 fmt_offset, int32 va_list_offset)
  375. {
  376. struct str_context ctx = { NULL, 0, 0 };
  377. const char *fmt;
  378. _va_list va_args;
  379. if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
  380. return 0;
  381. if (!_vprintf_wa((out_func_t)printf_out, &ctx, fmt, va_args, module_inst))
  382. return 0;
  383. return ctx.count;
  384. }
  385. static int
  386. _sprintf_wrapper(wasm_module_inst_t module_inst,
  387. int32 str_offset, int32 fmt_offset, int32 va_list_offset)
  388. {
  389. int32 app_end_offset;
  390. struct str_context ctx;
  391. char *str;
  392. const char *fmt;
  393. _va_list va_args;
  394. if (!wasm_runtime_get_app_addr_range(module_inst, str_offset,
  395. NULL, &app_end_offset)) {
  396. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  397. return false;
  398. }
  399. str = addr_app_to_native(str_offset);
  400. if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
  401. return 0;
  402. ctx.str = str;
  403. ctx.max = app_end_offset - str_offset;
  404. ctx.count = 0;
  405. if (!_vprintf_wa((out_func_t)sprintf_out, &ctx, fmt, va_args, module_inst))
  406. return 0;
  407. if (ctx.count < ctx.max) {
  408. str[ctx.count] = '\0';
  409. }
  410. return ctx.count;
  411. }
  412. static int
  413. _snprintf_wrapper(wasm_module_inst_t module_inst,
  414. int32 str_offset, int32 size, int32 fmt_offset,
  415. int32 va_list_offset)
  416. {
  417. struct str_context ctx;
  418. char *str;
  419. const char *fmt;
  420. _va_list va_args;
  421. if (!validate_app_addr(str_offset, size))
  422. return 0;
  423. str = addr_app_to_native(str_offset);
  424. if (!parse_printf_args(module_inst, fmt_offset, va_list_offset, &fmt, &va_args))
  425. return 0;
  426. ctx.str = str;
  427. ctx.max = size;
  428. ctx.count = 0;
  429. if (!_vprintf_wa((out_func_t)sprintf_out, &ctx, fmt, va_args, module_inst))
  430. return 0;
  431. if (ctx.count < ctx.max) {
  432. str[ctx.count] = '\0';
  433. }
  434. return ctx.count;
  435. }
  436. static int
  437. _puts_wrapper(wasm_module_inst_t module_inst,
  438. int32 str_offset)
  439. {
  440. const char *str;
  441. if (!validate_app_str_addr(str_offset))
  442. return 0;
  443. str = addr_app_to_native(str_offset);
  444. return bh_printf("%s\n", str);
  445. }
  446. static int
  447. _putchar_wrapper(wasm_module_inst_t module_inst, int c)
  448. {
  449. bh_printf("%c", c);
  450. return 1;
  451. }
  452. static int32
  453. _strdup_wrapper(wasm_module_inst_t module_inst,
  454. int32 str_offset)
  455. {
  456. char *str, *str_ret;
  457. uint32 len;
  458. int32 str_ret_offset = 0;
  459. if (!validate_app_str_addr(str_offset))
  460. return 0;
  461. str = addr_app_to_native(str_offset);
  462. if (str) {
  463. len = strlen(str) + 1;
  464. str_ret_offset = module_malloc(len);
  465. if (str_ret_offset) {
  466. str_ret = addr_app_to_native(str_ret_offset);
  467. memcpy(str_ret, str, len);
  468. }
  469. }
  470. return str_ret_offset;
  471. }
  472. static int32
  473. _memcmp_wrapper(wasm_module_inst_t module_inst,
  474. int32 s1_offset, int32 s2_offset, int32 size)
  475. {
  476. void *s1, *s2;
  477. if (!validate_app_addr(s1_offset, size)
  478. || !validate_app_addr(s2_offset, size))
  479. return 0;
  480. s1 = addr_app_to_native(s1_offset);
  481. s2 = addr_app_to_native(s2_offset);
  482. return memcmp(s1, s2, size);
  483. }
  484. static int32
  485. _memcpy_wrapper(wasm_module_inst_t module_inst,
  486. int32 dst_offset, int32 src_offset, int32 size)
  487. {
  488. void *dst, *src;
  489. if (size == 0)
  490. return dst_offset;
  491. if (!validate_app_addr(dst_offset, size)
  492. || !validate_app_addr(src_offset, size))
  493. return dst_offset;
  494. dst = addr_app_to_native(dst_offset);
  495. src = addr_app_to_native(src_offset);
  496. memcpy(dst, src, size);
  497. return dst_offset;
  498. }
  499. static int32
  500. _memmove_wrapper(wasm_module_inst_t module_inst,
  501. int32 dst_offset, int32 src_offset, int32 size)
  502. {
  503. void *dst, *src;
  504. if (!validate_app_addr(dst_offset, size)
  505. || !validate_app_addr(src_offset, size))
  506. return dst_offset;
  507. dst = addr_app_to_native(dst_offset);
  508. src = addr_app_to_native(src_offset);
  509. memmove(dst, src, size);
  510. return dst_offset;
  511. }
  512. static int32
  513. _memset_wrapper(wasm_module_inst_t module_inst,
  514. int32 s_offset, int32 c, int32 size)
  515. {
  516. void *s;
  517. if (!validate_app_addr(s_offset, size))
  518. return s_offset;
  519. s = addr_app_to_native(s_offset);
  520. memset(s, c, size);
  521. return s_offset;
  522. }
  523. static int32
  524. _strchr_wrapper(wasm_module_inst_t module_inst,
  525. int32 s_offset, int32 c)
  526. {
  527. const char *s;
  528. char *ret;
  529. if (!validate_app_str_addr(s_offset))
  530. return s_offset;
  531. s = addr_app_to_native(s_offset);
  532. ret = strchr(s, c);
  533. return ret ? addr_native_to_app(ret) : 0;
  534. }
  535. static int32
  536. _strcmp_wrapper(wasm_module_inst_t module_inst,
  537. int32 s1_offset, int32 s2_offset)
  538. {
  539. void *s1, *s2;
  540. if (!validate_app_str_addr(s1_offset)
  541. || !validate_app_str_addr(s2_offset))
  542. return 0;
  543. s1 = addr_app_to_native(s1_offset);
  544. s2 = addr_app_to_native(s2_offset);
  545. return strcmp(s1, s2);
  546. }
  547. static int32
  548. _strncmp_wrapper(wasm_module_inst_t module_inst,
  549. int32 s1_offset, int32 s2_offset, uint32 size)
  550. {
  551. void *s1, *s2;
  552. if (!validate_app_addr(s1_offset, size)
  553. || !validate_app_addr(s2_offset, size))
  554. return 0;
  555. s1 = addr_app_to_native(s1_offset);
  556. s2 = addr_app_to_native(s2_offset);
  557. return strncmp(s1, s2, size);
  558. }
  559. static int32
  560. _strcpy_wrapper(wasm_module_inst_t module_inst,
  561. int32 dst_offset, int32 src_offset)
  562. {
  563. char *dst, *src;
  564. uint32 len;
  565. if (!validate_app_str_addr(src_offset))
  566. return 0;
  567. src = addr_app_to_native(src_offset);
  568. len = strlen(src);
  569. if (!validate_app_addr(dst_offset, len + 1))
  570. return 0;
  571. dst = addr_app_to_native(dst_offset);
  572. strncpy(dst, src, len + 1);
  573. return dst_offset;
  574. }
  575. static int32
  576. _strncpy_wrapper(wasm_module_inst_t module_inst,
  577. int32 dst_offset, int32 src_offset, uint32 size)
  578. {
  579. char *dst, *src;
  580. if (!validate_app_addr(dst_offset, size)
  581. || !validate_app_addr(src_offset, size))
  582. return 0;
  583. dst = addr_app_to_native(dst_offset);
  584. src = addr_app_to_native(src_offset);
  585. strncpy(dst, src, size);
  586. return dst_offset;
  587. }
  588. static uint32
  589. _strlen_wrapper(wasm_module_inst_t module_inst,
  590. int32 s_offset)
  591. {
  592. char *s;
  593. if (!validate_app_str_addr(s_offset))
  594. return 0;
  595. s = addr_app_to_native(s_offset);
  596. return strlen(s);
  597. }
  598. static int32
  599. _malloc_wrapper(wasm_module_inst_t module_inst,
  600. uint32 size)
  601. {
  602. return module_malloc(size);
  603. }
  604. static int32
  605. _calloc_wrapper(wasm_module_inst_t module_inst,
  606. uint32 nmemb, uint32 size)
  607. {
  608. uint64 total_size = (uint64) nmemb * (uint64) size;
  609. uint32 ret_offset = 0;
  610. uint8 *ret_ptr;
  611. if (total_size > UINT32_MAX)
  612. total_size = UINT32_MAX;
  613. ret_offset = module_malloc((uint32 )total_size);
  614. if (ret_offset) {
  615. ret_ptr = addr_app_to_native(ret_offset);
  616. memset(ret_ptr, 0, (uint32) total_size);
  617. }
  618. return ret_offset;
  619. }
  620. static void
  621. _free_wrapper(wasm_module_inst_t module_inst,
  622. int32 ptr_offset)
  623. {
  624. if (!validate_app_addr(ptr_offset, 4))
  625. return;
  626. return module_free(ptr_offset);
  627. }
  628. static void
  629. setTempRet0_wrapper(wasm_module_inst_t module_inst,
  630. uint32 temp_ret)
  631. {
  632. wasm_runtime_set_temp_ret(module_inst, temp_ret);
  633. }
  634. static uint32
  635. getTempRet0_wrapper(wasm_module_inst_t module_inst)
  636. {
  637. return wasm_runtime_get_temp_ret(module_inst);
  638. }
  639. static uint32
  640. _llvm_bswap_i16_wrapper(wasm_module_inst_t module_inst,
  641. uint32 data)
  642. {
  643. return (data & 0xFFFF0000)
  644. | ((data & 0xFF) << 8)
  645. | ((data & 0xFF00) >> 8);
  646. }
  647. static uint32
  648. _llvm_bswap_i32_wrapper(wasm_module_inst_t module_inst,
  649. uint32 data)
  650. {
  651. return ((data & 0xFF) << 24)
  652. | ((data & 0xFF00) << 8)
  653. | ((data & 0xFF0000) >> 8)
  654. | ((data & 0xFF000000) >> 24);
  655. }
  656. static uint32
  657. _bitshift64Lshr_wrapper(wasm_module_inst_t module_inst,
  658. uint32 uint64_part0, uint32 uint64_part1,
  659. uint32 bits)
  660. {
  661. union {
  662. uint64 value;
  663. uint32 parts[2];
  664. } u;
  665. u.parts[0] = uint64_part0;
  666. u.parts[1] = uint64_part1;
  667. u.value >>= bits;
  668. /* return low 32bit and save high 32bit to temp ret */
  669. wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
  670. return (uint32) u.value;
  671. }
  672. static uint32
  673. _bitshift64Shl_wrapper(wasm_module_inst_t module_inst,
  674. uint32 int64_part0, uint32 int64_part1,
  675. uint32 bits)
  676. {
  677. union {
  678. int64 value;
  679. uint32 parts[2];
  680. } u;
  681. u.parts[0] = int64_part0;
  682. u.parts[1] = int64_part1;
  683. u.value <<= bits;
  684. /* return low 32bit and save high 32bit to temp ret */
  685. wasm_runtime_set_temp_ret(module_inst, (uint32) (u.value >> 32));
  686. return (uint32) u.value;
  687. }
  688. static void
  689. _llvm_stackrestore_wrapper(wasm_module_inst_t module_inst,
  690. uint32 llvm_stack)
  691. {
  692. bh_printf("_llvm_stackrestore called!\n");
  693. wasm_runtime_set_llvm_stack(module_inst, llvm_stack);
  694. }
  695. static uint32
  696. _llvm_stacksave_wrapper(wasm_module_inst_t module_inst)
  697. {
  698. bh_printf("_llvm_stacksave called!\n");
  699. return wasm_runtime_get_llvm_stack(module_inst);
  700. }
  701. static int32
  702. _emscripten_memcpy_big_wrapper(wasm_module_inst_t module_inst,
  703. int32 dst_offset, int32 src_offset,
  704. uint32 size)
  705. {
  706. void *dst, *src;
  707. if (!validate_app_addr(dst_offset, size)
  708. || !validate_app_addr(src_offset, size))
  709. return dst_offset;
  710. dst = addr_app_to_native(dst_offset);
  711. src = addr_app_to_native(src_offset);
  712. memcpy(dst, src, size);
  713. return dst_offset;
  714. }
  715. static void
  716. abort_wrapper(wasm_module_inst_t module_inst,
  717. int32 code)
  718. {
  719. char buf[32];
  720. snprintf(buf, sizeof(buf), "env.abort(%i)", code);
  721. wasm_runtime_set_exception(module_inst, buf);
  722. }
  723. static void
  724. abortStackOverflow_wrapper(wasm_module_inst_t module_inst,
  725. int32 code)
  726. {
  727. char buf[32];
  728. snprintf(buf, sizeof(buf), "env.abortStackOverflow(%i)", code);
  729. wasm_runtime_set_exception(module_inst, buf);
  730. }
  731. static void
  732. nullFunc_X_wrapper(wasm_module_inst_t module_inst,
  733. int32 code)
  734. {
  735. char buf[32];
  736. snprintf(buf, sizeof(buf), "env.nullFunc_X(%i)", code);
  737. wasm_runtime_set_exception(module_inst, buf);
  738. }
  739. /*#define ENABLE_SPEC_TEST 1*/
  740. #ifdef ENABLE_SPEC_TEST
  741. static void
  742. print_i32_wrapper(wasm_module_inst_t module_inst, int i32)
  743. {
  744. bh_printf("%d\n", i32);
  745. }
  746. static void
  747. print_wrapper(wasm_module_inst_t module_inst, int i32)
  748. {
  749. bh_printf("%d\n", i32);
  750. }
  751. #endif
  752. /* TODO: add function parameter/result types check */
  753. #define REG_NATIVE_FUNC(module_name, func_name) \
  754. { #module_name, #func_name, func_name##_wrapper }
  755. typedef struct WASMNativeFuncDef {
  756. const char *module_name;
  757. const char *func_name;
  758. void *func_ptr;
  759. } WASMNativeFuncDef;
  760. static WASMNativeFuncDef native_func_defs[] = {
  761. #ifdef ENABLE_SPEC_TEST
  762. REG_NATIVE_FUNC(spectest, print_i32),
  763. REG_NATIVE_FUNC(spectest, print),
  764. #endif
  765. REG_NATIVE_FUNC(env, _printf),
  766. REG_NATIVE_FUNC(env, _sprintf),
  767. REG_NATIVE_FUNC(env, _snprintf),
  768. REG_NATIVE_FUNC(env, _puts),
  769. REG_NATIVE_FUNC(env, _putchar),
  770. REG_NATIVE_FUNC(env, _memcmp),
  771. REG_NATIVE_FUNC(env, _memcpy),
  772. REG_NATIVE_FUNC(env, _memmove),
  773. REG_NATIVE_FUNC(env, _memset),
  774. REG_NATIVE_FUNC(env, _strchr),
  775. REG_NATIVE_FUNC(env, _strcmp),
  776. REG_NATIVE_FUNC(env, _strcpy),
  777. REG_NATIVE_FUNC(env, _strlen),
  778. REG_NATIVE_FUNC(env, _strncmp),
  779. REG_NATIVE_FUNC(env, _strncpy),
  780. REG_NATIVE_FUNC(env, _malloc),
  781. REG_NATIVE_FUNC(env, _calloc),
  782. REG_NATIVE_FUNC(env, _strdup),
  783. REG_NATIVE_FUNC(env, _free),
  784. REG_NATIVE_FUNC(env, setTempRet0),
  785. REG_NATIVE_FUNC(env, getTempRet0),
  786. REG_NATIVE_FUNC(env, _llvm_bswap_i16),
  787. REG_NATIVE_FUNC(env, _llvm_bswap_i32),
  788. REG_NATIVE_FUNC(env, _bitshift64Lshr),
  789. REG_NATIVE_FUNC(env, _bitshift64Shl),
  790. REG_NATIVE_FUNC(env, _llvm_stackrestore),
  791. REG_NATIVE_FUNC(env, _llvm_stacksave),
  792. REG_NATIVE_FUNC(env, _emscripten_memcpy_big),
  793. REG_NATIVE_FUNC(env, abort),
  794. REG_NATIVE_FUNC(env, abortStackOverflow),
  795. REG_NATIVE_FUNC(env, nullFunc_X)
  796. };
  797. void*
  798. wasm_native_func_lookup(const char *module_name, const char *func_name)
  799. {
  800. uint32 size = sizeof(native_func_defs) / sizeof(WASMNativeFuncDef);
  801. WASMNativeFuncDef *func_def = native_func_defs;
  802. WASMNativeFuncDef *func_def_end = func_def + size;
  803. void *ret;
  804. if (!module_name || !func_name)
  805. return NULL;
  806. while (func_def < func_def_end) {
  807. if (!strcmp(func_def->module_name, module_name)
  808. && (!strcmp(func_def->func_name, func_name)
  809. || (func_def->func_name[0] == '_'
  810. && !strcmp(func_def->func_name + 1, func_name))))
  811. return (void*) (uintptr_t) func_def->func_ptr;
  812. func_def++;
  813. }
  814. if ((ret = wasm_platform_native_func_lookup(module_name, func_name)))
  815. return ret;
  816. return NULL;
  817. }
  818. /*************************************
  819. * Global Variables *
  820. *************************************/
  821. typedef struct WASMNativeGlobalDef {
  822. const char *module_name;
  823. const char *global_name;
  824. WASMValue global_data;
  825. } WASMNativeGlobalDef;
  826. static WASMNativeGlobalDef native_global_defs[] = {
  827. #ifdef ENABLE_SPEC_TEST
  828. { "spectest", "global_i32", .global_data.u32 = 0 },
  829. #endif
  830. { "env", "STACKTOP", .global_data.u32 = 0 },
  831. { "env", "STACK_MAX", .global_data.u32 = 0 },
  832. { "env", "ABORT", .global_data.u32 = 0 },
  833. { "env", "memoryBase", .global_data.u32 = 0 },
  834. { "env", "__memory_base", .global_data.u32 = 0 },
  835. { "env", "tableBase", .global_data.u32 = 0 },
  836. { "env", "__table_base", .global_data.u32 = 0 },
  837. { "env", "DYNAMICTOP_PTR", .global_data.addr = 0 },
  838. { "env", "tempDoublePtr", .global_data.addr = 0 },
  839. { "global", "NaN", .global_data.u64 = 0x7FF8000000000000LL },
  840. { "global", "Infinity", .global_data.u64 = 0x7FF0000000000000LL }
  841. };
  842. bool
  843. wasm_native_global_lookup(const char *module_name, const char *global_name,
  844. WASMGlobalImport *global)
  845. {
  846. uint32 size = sizeof(native_global_defs) / sizeof(WASMNativeGlobalDef);
  847. WASMNativeGlobalDef *global_def = native_global_defs;
  848. WASMNativeGlobalDef *global_def_end = global_def + size;
  849. if (!module_name || !global_name || !global)
  850. return false;
  851. /* Lookup constant globals which can be defined by table */
  852. while (global_def < global_def_end) {
  853. if (!strcmp(global_def->module_name, module_name)
  854. && !strcmp(global_def->global_name, global_name)) {
  855. global->global_data_linked = global_def->global_data;
  856. return true;
  857. }
  858. global_def++;
  859. }
  860. return false;
  861. }
  862. bool
  863. wasm_native_init()
  864. {
  865. /* TODO: qsort the function defs and global defs. */
  866. return true;
  867. }