libc_wrapper.c 27 KB

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