module_wasm_app.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "module_wasm_app.h"
  6. #include "native_interface.h" /* for request_t type */
  7. #include "app_manager_host.h"
  8. #include "bh_platform.h"
  9. #include "bi-inc/attr_container.h"
  10. #include "coap_ext.h"
  11. #include "event.h"
  12. #include "watchdog.h"
  13. #include "runtime_lib.h"
  14. #if WASM_ENABLE_INTERP != 0
  15. #include "wasm.h"
  16. #endif
  17. #if WASM_ENABLE_AOT != 0
  18. #include "aot_export.h"
  19. #endif
  20. /* clang-format off */
  21. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  22. /* Wasm bytecode file 4 version bytes */
  23. static uint8 wasm_bytecode_version[4] = {
  24. (uint8)0x01,
  25. (uint8)0x00,
  26. (uint8)0x00,
  27. (uint8)0x00
  28. };
  29. #endif
  30. #if WASM_ENABLE_AOT != 0
  31. /* Wasm aot file 4 version bytes */
  32. static uint8 wasm_aot_version[4] = {
  33. (uint8)0x02,
  34. (uint8)0x00,
  35. (uint8)0x00,
  36. (uint8)0x00
  37. };
  38. #endif
  39. /* clang-format on */
  40. static union {
  41. int a;
  42. char b;
  43. } __ue = { .a = 1 };
  44. #define is_little_endian() (__ue.b == 1)
  45. /* Wasm App Install Request Receiving Phase */
  46. typedef enum wasm_app_install_req_recv_phase_t {
  47. Phase_Req_Ver,
  48. Phase_Req_Action,
  49. Phase_Req_Fmt,
  50. Phase_Req_Mid,
  51. Phase_Req_Sender,
  52. Phase_Req_Url_Len,
  53. Phase_Req_Payload_Len, /* payload is wasm app binary */
  54. Phase_Req_Url,
  55. /* Magic phase */
  56. Phase_App_Magic,
  57. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  58. /* Phases of wasm bytecode file */
  59. Phase_Wasm_Version,
  60. Phase_Wasm_Section_Type,
  61. Phase_Wasm_Section_Size,
  62. Phase_Wasm_Section_Content,
  63. #endif
  64. #if WASM_ENABLE_AOT != 0
  65. /* Phases of wasm AOT file */
  66. Phase_AOT_Version,
  67. Phase_AOT_Section_ID,
  68. Phase_AOT_Section_Size,
  69. Phase_AOT_Section_Content
  70. #endif
  71. } wasm_app_install_req_recv_phase_t;
  72. /* Message for insall wasm app */
  73. typedef struct install_wasm_app_msg_t {
  74. uint8 request_version;
  75. uint8 request_action;
  76. uint16 request_fmt;
  77. uint32 request_mid;
  78. uint32 request_sender;
  79. uint16 request_url_len;
  80. uint32 wasm_app_size; /* payload size is just wasm app binary size */
  81. char *request_url;
  82. wasm_app_file_t app_file;
  83. int app_file_magic;
  84. } install_wasm_app_msg_t;
  85. /* Wasm App Install Request Receive Context */
  86. typedef struct wasm_app_install_req_recv_ctx_t {
  87. wasm_app_install_req_recv_phase_t phase;
  88. int size_in_phase;
  89. install_wasm_app_msg_t message;
  90. int total_received_size;
  91. } wasm_app_install_req_recv_ctx_t;
  92. /* Current wasm app install request receive context */
  93. static wasm_app_install_req_recv_ctx_t recv_ctx;
  94. static bool
  95. wasm_app_module_init(void);
  96. static bool
  97. wasm_app_module_install(request_t *msg);
  98. static bool
  99. wasm_app_module_uninstall(request_t *msg);
  100. static void
  101. wasm_app_module_watchdog_kill(module_data *module_data);
  102. static bool
  103. wasm_app_module_handle_host_url(void *queue_msg);
  104. static module_data *
  105. wasm_app_module_get_module_data(void *inst);
  106. static bool
  107. wasm_app_module_on_install_request_byte_arrive(uint8 ch, int request_total_size,
  108. int *received_size);
  109. static bool
  110. module_wasm_app_handle_install_msg(install_wasm_app_msg_t *message);
  111. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  112. static void
  113. destroy_all_wasm_sections(wasm_section_list_t sections);
  114. static void
  115. destroy_part_wasm_sections(wasm_section_list_t *p_sections,
  116. uint8 *section_types, int section_cnt);
  117. #endif
  118. #if WASM_ENABLE_AOT != 0
  119. static void
  120. destroy_all_aot_sections(aot_section_list_t sections);
  121. static void
  122. destroy_part_aot_sections(aot_section_list_t *p_sections, uint8 *section_types,
  123. int section_cnt);
  124. #endif
  125. #define Max_Msg_Callback 10
  126. int g_msg_type[Max_Msg_Callback] = { 0 };
  127. message_type_handler_t g_msg_callbacks[Max_Msg_Callback] = { 0 };
  128. #define Max_Cleanup_Callback 10
  129. static resource_cleanup_handler_t g_cleanup_callbacks[Max_Cleanup_Callback] = {
  130. 0
  131. };
  132. module_interface wasm_app_module_interface = {
  133. wasm_app_module_init,
  134. wasm_app_module_install,
  135. wasm_app_module_uninstall,
  136. wasm_app_module_watchdog_kill,
  137. wasm_app_module_handle_host_url,
  138. wasm_app_module_get_module_data,
  139. wasm_app_module_on_install_request_byte_arrive
  140. };
  141. #if WASM_ENABLE_INTERP == 0
  142. static unsigned
  143. align_uint(unsigned v, unsigned b)
  144. {
  145. unsigned m = b - 1;
  146. return (v + m) & ~m;
  147. }
  148. #endif
  149. static void
  150. exchange_uint32(uint8 *p_data)
  151. {
  152. uint8 value = *p_data;
  153. *p_data = *(p_data + 3);
  154. *(p_data + 3) = value;
  155. value = *(p_data + 1);
  156. *(p_data + 1) = *(p_data + 2);
  157. *(p_data + 2) = value;
  158. }
  159. static wasm_function_inst_t
  160. app_manager_lookup_function(const wasm_module_inst_t module_inst,
  161. const char *name, const char *signature)
  162. {
  163. wasm_function_inst_t func;
  164. func = wasm_runtime_lookup_function(module_inst, name, signature);
  165. if (!func && name[0] == '_')
  166. func = wasm_runtime_lookup_function(module_inst, name + 1, signature);
  167. return func;
  168. }
  169. static void
  170. app_instance_queue_callback(void *queue_msg, void *arg)
  171. {
  172. uint32 argv[2];
  173. wasm_function_inst_t func_onRequest, func_onTimer;
  174. wasm_module_inst_t inst = (wasm_module_inst_t)arg;
  175. module_data *m_data = app_manager_get_module_data(Module_WASM_App, inst);
  176. wasm_data *wasm_app_data;
  177. int message_type;
  178. bh_assert(m_data);
  179. wasm_app_data = (wasm_data *)m_data->internal_data;
  180. message_type = bh_message_type(queue_msg);
  181. if (message_type < BASE_EVENT_MAX) {
  182. switch (message_type) {
  183. case RESTFUL_REQUEST:
  184. {
  185. request_t *request = (request_t *)bh_message_payload(queue_msg);
  186. int size;
  187. char *buffer;
  188. int32 buffer_offset;
  189. app_manager_printf("App %s got request, url %s, action %d\n",
  190. m_data->module_name, request->url,
  191. request->action);
  192. func_onRequest = app_manager_lookup_function(
  193. inst, "_on_request", "(i32i32)");
  194. if (!func_onRequest) {
  195. app_manager_printf("Cannot find function onRequest\n");
  196. break;
  197. }
  198. buffer = pack_request(request, &size);
  199. if (buffer == NULL)
  200. break;
  201. buffer_offset =
  202. wasm_runtime_module_dup_data(inst, buffer, size);
  203. if (buffer_offset == 0) {
  204. const char *exception = wasm_runtime_get_exception(inst);
  205. if (exception) {
  206. app_manager_printf(
  207. "Got exception running wasm code: %s\n", exception);
  208. wasm_runtime_clear_exception(inst);
  209. }
  210. free_req_resp_packet(buffer);
  211. break;
  212. }
  213. free_req_resp_packet(buffer);
  214. argv[0] = (uint32)buffer_offset;
  215. argv[1] = (uint32)size;
  216. if (!wasm_runtime_call_wasm(wasm_app_data->exec_env,
  217. func_onRequest, 2, argv)) {
  218. const char *exception = wasm_runtime_get_exception(inst);
  219. bh_assert(exception);
  220. app_manager_printf("Got exception running wasm code: %s\n",
  221. exception);
  222. wasm_runtime_clear_exception(inst);
  223. wasm_runtime_module_free(inst, buffer_offset);
  224. break;
  225. }
  226. wasm_runtime_module_free(inst, buffer_offset);
  227. app_manager_printf("Wasm app process request success.\n");
  228. break;
  229. }
  230. case RESTFUL_RESPONSE:
  231. {
  232. wasm_function_inst_t func_onResponse;
  233. response_t *response =
  234. (response_t *)bh_message_payload(queue_msg);
  235. int size;
  236. char *buffer;
  237. int32 buffer_offset;
  238. app_manager_printf("App %s got response_t,status %d\n",
  239. m_data->module_name, response->status);
  240. func_onResponse = app_manager_lookup_function(
  241. inst, "_on_response", "(i32i32)");
  242. if (!func_onResponse) {
  243. app_manager_printf("Cannot find function on_response\n");
  244. break;
  245. }
  246. buffer = pack_response(response, &size);
  247. if (buffer == NULL)
  248. break;
  249. buffer_offset =
  250. wasm_runtime_module_dup_data(inst, buffer, size);
  251. if (buffer_offset == 0) {
  252. const char *exception = wasm_runtime_get_exception(inst);
  253. if (exception) {
  254. app_manager_printf(
  255. "Got exception running wasm code: %s\n", exception);
  256. wasm_runtime_clear_exception(inst);
  257. }
  258. free_req_resp_packet(buffer);
  259. break;
  260. }
  261. free_req_resp_packet(buffer);
  262. argv[0] = (uint32)buffer_offset;
  263. argv[1] = (uint32)size;
  264. if (!wasm_runtime_call_wasm(wasm_app_data->exec_env,
  265. func_onResponse, 2, argv)) {
  266. const char *exception = wasm_runtime_get_exception(inst);
  267. bh_assert(exception);
  268. app_manager_printf("Got exception running wasm code: %s\n",
  269. exception);
  270. wasm_runtime_clear_exception(inst);
  271. wasm_runtime_module_free(inst, buffer_offset);
  272. break;
  273. }
  274. wasm_runtime_module_free(inst, buffer_offset);
  275. app_manager_printf("Wasm app process response success.\n");
  276. break;
  277. }
  278. default:
  279. {
  280. for (int i = 0; i < Max_Msg_Callback; i++) {
  281. if (g_msg_type[i] == message_type) {
  282. g_msg_callbacks[i](m_data, queue_msg);
  283. return;
  284. }
  285. }
  286. app_manager_printf(
  287. "Invalid message type of WASM app queue message.\n");
  288. break;
  289. }
  290. }
  291. }
  292. else {
  293. switch (message_type) {
  294. case TIMER_EVENT_WASM:
  295. {
  296. unsigned int timer_id;
  297. if (bh_message_payload(queue_msg)) {
  298. /* Call Timer.callOnTimer() method */
  299. func_onTimer = app_manager_lookup_function(
  300. inst, "_on_timer_callback", "(i32)");
  301. if (!func_onTimer) {
  302. app_manager_printf(
  303. "Cannot find function _on_timer_callback\n");
  304. break;
  305. }
  306. timer_id =
  307. (unsigned int)(uintptr_t)bh_message_payload(queue_msg);
  308. argv[0] = timer_id;
  309. if (!wasm_runtime_call_wasm(wasm_app_data->exec_env,
  310. func_onTimer, 1, argv)) {
  311. const char *exception =
  312. wasm_runtime_get_exception(inst);
  313. bh_assert(exception);
  314. app_manager_printf(
  315. "Got exception running wasm code: %s\n", exception);
  316. wasm_runtime_clear_exception(inst);
  317. }
  318. }
  319. break;
  320. }
  321. default:
  322. {
  323. for (int i = 0; i < Max_Msg_Callback; i++) {
  324. if (g_msg_type[i] == message_type) {
  325. g_msg_callbacks[i](m_data, queue_msg);
  326. return;
  327. }
  328. }
  329. app_manager_printf(
  330. "Invalid message type of WASM app queue message.\n");
  331. break;
  332. }
  333. }
  334. }
  335. }
  336. #if WASM_ENABLE_LIBC_WASI != 0
  337. static bool
  338. wasm_app_prepare_wasi_dir(wasm_module_t module, const char *module_name,
  339. char *wasi_dir_buf, uint32 buf_size)
  340. {
  341. const char *wasi_root = wasm_get_wasi_root_dir();
  342. char *p = wasi_dir_buf;
  343. uint32 module_name_len = strlen(module_name);
  344. uint32 wasi_root_len = strlen(wasi_root);
  345. uint32 total_size;
  346. struct stat st = { 0 };
  347. bh_assert(wasi_root);
  348. /* wasi_dir: wasi_root/module_name */
  349. total_size = wasi_root_len + 1 + module_name_len + 1;
  350. if (total_size > buf_size)
  351. return false;
  352. memcpy(p, wasi_root, wasi_root_len);
  353. p += wasi_root_len;
  354. *p++ = '/';
  355. memcpy(p, module_name, module_name_len);
  356. p += module_name_len;
  357. *p++ = '\0';
  358. if (mkdir(wasi_dir_buf, 0777) != 0) {
  359. if (errno == EEXIST) {
  360. /* Failed due to dir already exist */
  361. if ((stat(wasi_dir_buf, &st) == 0) && (st.st_mode & S_IFDIR)) {
  362. return true;
  363. }
  364. }
  365. return false;
  366. }
  367. return true;
  368. }
  369. #endif
  370. /* WASM app thread main routine */
  371. static void *
  372. wasm_app_routine(void *arg)
  373. {
  374. wasm_function_inst_t func_onInit;
  375. wasm_function_inst_t func_onDestroy;
  376. module_data *m_data = (module_data *)arg;
  377. wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
  378. wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
  379. /* Set m_data to the VM managed instance's custom data */
  380. wasm_runtime_set_custom_data(inst, m_data);
  381. app_manager_printf("WASM app '%s' started\n", m_data->module_name);
  382. #if WASM_ENABLE_LIBC_WASI != 0
  383. if (wasm_runtime_is_wasi_mode(inst)) {
  384. wasm_function_inst_t func_start;
  385. /* In wasi mode, we should call function named "_start"
  386. which initializes the wasi envrionment. The "_start" function
  387. will call "main" function */
  388. if ((func_start = wasm_runtime_lookup_wasi_start_function(inst))) {
  389. if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_start, 0,
  390. NULL)) {
  391. const char *exception = wasm_runtime_get_exception(inst);
  392. bh_assert(exception);
  393. app_manager_printf(
  394. "Got exception running wasi start function: %s\n",
  395. exception);
  396. wasm_runtime_clear_exception(inst);
  397. goto fail1;
  398. }
  399. }
  400. /* if no start function is found, we execute
  401. the _on_init function as normal */
  402. }
  403. #endif
  404. /* Call app's onInit() method */
  405. func_onInit = app_manager_lookup_function(inst, "_on_init", "()");
  406. if (!func_onInit) {
  407. app_manager_printf("Cannot find function on_init().\n");
  408. goto fail1;
  409. }
  410. if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onInit, 0,
  411. NULL)) {
  412. const char *exception = wasm_runtime_get_exception(inst);
  413. bh_assert(exception);
  414. app_manager_printf("Got exception running WASM code: %s\n", exception);
  415. wasm_runtime_clear_exception(inst);
  416. /* call on_destroy() in case some resources are opened in on_init()
  417. * and then exception thrown */
  418. goto fail2;
  419. }
  420. /* Enter queue loop run to receive and process applet queue message */
  421. bh_queue_enter_loop_run(m_data->queue, app_instance_queue_callback, inst);
  422. app_manager_printf("App instance main thread exit.\n");
  423. fail2:
  424. /* Call WASM app onDestroy() method if there is */
  425. func_onDestroy = app_manager_lookup_function(inst, "_on_destroy", "()");
  426. if (func_onDestroy) {
  427. if (!wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0,
  428. NULL)) {
  429. const char *exception = wasm_runtime_get_exception(inst);
  430. bh_assert(exception);
  431. app_manager_printf("Got exception running WASM code: %s\n",
  432. exception);
  433. wasm_runtime_clear_exception(inst);
  434. }
  435. }
  436. fail1:
  437. return NULL;
  438. }
  439. static void
  440. cleanup_app_resource(module_data *m_data)
  441. {
  442. int i;
  443. wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
  444. bool is_bytecode = wasm_app_data->is_bytecode;
  445. am_cleanup_registeration(m_data->id);
  446. am_unregister_event(NULL, m_data->id);
  447. for (i = 0; i < Max_Cleanup_Callback; i++) {
  448. if (g_cleanup_callbacks[i] != NULL)
  449. g_cleanup_callbacks[i](m_data->id);
  450. else
  451. break;
  452. }
  453. wasm_runtime_deinstantiate(wasm_app_data->wasm_module_inst);
  454. /* Destroy remain sections (i.e. data segment section for bytecode file
  455. * or text section of aot file) from app file's section list. */
  456. if (is_bytecode) {
  457. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  458. destroy_all_wasm_sections(
  459. (wasm_section_list_t)(wasm_app_data->sections));
  460. #else
  461. bh_assert(0);
  462. #endif
  463. }
  464. else {
  465. #if WASM_ENABLE_AOT != 0
  466. destroy_all_aot_sections((aot_section_list_t)(wasm_app_data->sections));
  467. #else
  468. bh_assert(0);
  469. #endif
  470. }
  471. if (wasm_app_data->wasm_module)
  472. wasm_runtime_unload(wasm_app_data->wasm_module);
  473. if (wasm_app_data->exec_env)
  474. wasm_runtime_destroy_exec_env(wasm_app_data->exec_env);
  475. /* Destroy watchdog timer */
  476. watchdog_timer_destroy(&m_data->wd_timer);
  477. /* Remove module data from module data list and free it */
  478. app_manager_del_module_data(m_data);
  479. }
  480. /************************************************************/
  481. /* Module specific functions implementation */
  482. /************************************************************/
  483. static bool
  484. wasm_app_module_init(void)
  485. {
  486. uint32 version;
  487. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  488. version = WASM_CURRENT_VERSION;
  489. if (!is_little_endian())
  490. exchange_uint32((uint8 *)&version);
  491. bh_memcpy_s(wasm_bytecode_version, 4, &version, 4);
  492. #endif
  493. #if WASM_ENABLE_AOT != 0
  494. version = AOT_CURRENT_VERSION;
  495. if (!is_little_endian())
  496. exchange_uint32((uint8 *)&version);
  497. bh_memcpy_s(wasm_aot_version, 4, &version, 4);
  498. #endif
  499. return true;
  500. }
  501. #define APP_NAME_MAX_LEN 128
  502. #define MAX_INT_STR_LEN 11
  503. static bool
  504. wasm_app_module_install(request_t *msg)
  505. {
  506. unsigned int m_data_size, heap_size, stack_size;
  507. unsigned int timeout, timers, err_size;
  508. char *properties;
  509. int properties_offset;
  510. wasm_app_file_t *wasm_app_file;
  511. wasm_data *wasm_app_data;
  512. package_type_t package_type;
  513. module_data *m_data = NULL;
  514. wasm_module_t module = NULL;
  515. wasm_module_inst_t inst = NULL;
  516. wasm_exec_env_t exec_env = NULL;
  517. char m_name[APP_NAME_MAX_LEN] = { 0 };
  518. char timeout_str[MAX_INT_STR_LEN] = { 0 };
  519. char heap_size_str[MAX_INT_STR_LEN] = { 0 };
  520. char timers_str[MAX_INT_STR_LEN] = { 0 }, err[128], err_resp[256];
  521. #if WASM_ENABLE_LIBC_WASI != 0
  522. char wasi_dir_buf[PATH_MAX] = { 0 };
  523. const char *wasi_dir_list[] = { wasi_dir_buf };
  524. #endif
  525. err_size = sizeof(err);
  526. /* Check payload */
  527. if (!msg->payload || msg->payload_len == 0) {
  528. SEND_ERR_RESPONSE(msg->mid,
  529. "Install WASM app failed: invalid wasm file.");
  530. return false;
  531. }
  532. /* Judge the app type is AOTed or not */
  533. package_type = get_package_type((uint8 *)msg->payload, msg->payload_len);
  534. wasm_app_file = (wasm_app_file_t *)msg->payload;
  535. /* Check app name */
  536. properties_offset = check_url_start(msg->url, strlen(msg->url), "/applet");
  537. bh_assert(properties_offset > 0);
  538. if (properties_offset <= 0) {
  539. SEND_ERR_RESPONSE(msg->mid,
  540. "Install WASM app failed: invalid app name.");
  541. goto fail;
  542. }
  543. properties = msg->url + properties_offset;
  544. find_key_value(properties, strlen(properties), "name", m_name,
  545. sizeof(m_name) - 1, '&');
  546. if (strlen(m_name) == 0) {
  547. SEND_ERR_RESPONSE(msg->mid,
  548. "Install WASM app failed: invalid app name.");
  549. goto fail;
  550. }
  551. if (app_manager_lookup_module_data(m_name)) {
  552. SEND_ERR_RESPONSE(msg->mid,
  553. "Install WASM app failed: app already installed.");
  554. goto fail;
  555. }
  556. /* Parse heap size */
  557. heap_size = APP_HEAP_SIZE_DEFAULT;
  558. find_key_value(properties, strlen(properties), "heap", heap_size_str,
  559. sizeof(heap_size_str) - 1, '&');
  560. if (strlen(heap_size_str) > 0) {
  561. heap_size = atoi(heap_size_str);
  562. if (heap_size < APP_HEAP_SIZE_MIN)
  563. heap_size = APP_HEAP_SIZE_MIN;
  564. else if (heap_size > APP_HEAP_SIZE_MAX)
  565. heap_size = APP_HEAP_SIZE_MAX;
  566. }
  567. /* Load WASM file and instantiate*/
  568. switch (package_type) {
  569. #if WASM_ENABLE_AOT != 0
  570. case Wasm_Module_AoT:
  571. {
  572. wasm_aot_file_t *aot_file;
  573. /* clang-format off */
  574. /* Sections to be released after loading */
  575. uint8 sections1[] = {
  576. AOT_SECTION_TYPE_TARGET_INFO,
  577. AOT_SECTION_TYPE_INIT_DATA,
  578. AOT_SECTION_TYPE_FUNCTION,
  579. AOT_SECTION_TYPE_EXPORT,
  580. AOT_SECTION_TYPE_RELOCATION,
  581. AOT_SECTION_TYPE_SIGANATURE,
  582. AOT_SECTION_TYPE_CUSTOM,
  583. };
  584. /* clang-format on */
  585. aot_file = &wasm_app_file->u.aot;
  586. /* Load AOT module from sections */
  587. module = wasm_runtime_load_from_sections(aot_file->sections, true,
  588. err, err_size);
  589. if (!module) {
  590. snprintf(err_resp, sizeof(err_resp),
  591. "Install WASM app failed: %s", err);
  592. SEND_ERR_RESPONSE(msg->mid, err_resp);
  593. goto fail;
  594. }
  595. /* Destroy useless sections from list after load */
  596. destroy_part_aot_sections(&aot_file->sections, sections1,
  597. sizeof(sections1) / sizeof(uint8));
  598. #if WASM_ENABLE_LIBC_WASI != 0
  599. if (!wasm_app_prepare_wasi_dir(module, m_name, wasi_dir_buf,
  600. sizeof(wasi_dir_buf))) {
  601. SEND_ERR_RESPONSE(
  602. msg->mid,
  603. "Install WASM app failed: prepare wasi env failed.");
  604. goto fail;
  605. }
  606. wasm_runtime_set_wasi_args(module, wasi_dir_list, 1, NULL, 0, NULL,
  607. 0, NULL, 0);
  608. #endif
  609. /* Instantiate the AOT module */
  610. inst =
  611. wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
  612. if (!inst) {
  613. snprintf(err_resp, sizeof(err_resp),
  614. "Install WASM app failed: %s", err);
  615. SEND_ERR_RESPONSE(msg->mid, err);
  616. goto fail;
  617. }
  618. break;
  619. }
  620. #endif /* endof WASM_ENABLE_AOT != 0 */
  621. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  622. case Wasm_Module_Bytecode:
  623. {
  624. wasm_bytecode_file_t *bytecode_file;
  625. /* Sections to be released after loading */
  626. uint8 sections1[] = {
  627. SECTION_TYPE_USER,
  628. SECTION_TYPE_TYPE,
  629. SECTION_TYPE_IMPORT,
  630. SECTION_TYPE_FUNC,
  631. SECTION_TYPE_TABLE,
  632. SECTION_TYPE_MEMORY,
  633. SECTION_TYPE_GLOBAL,
  634. SECTION_TYPE_EXPORT,
  635. SECTION_TYPE_START,
  636. SECTION_TYPE_ELEM,
  637. #if WASM_ENABLE_BULK_MEMORY != 0
  638. SECTION_TYPE_DATACOUNT
  639. #endif
  640. };
  641. /* Sections to be released after instantiating */
  642. uint8 sections2[] = { SECTION_TYPE_DATA };
  643. bytecode_file = &wasm_app_file->u.bytecode;
  644. /* Load wasm module from sections */
  645. module = wasm_runtime_load_from_sections(bytecode_file->sections,
  646. false, err, err_size);
  647. if (!module) {
  648. snprintf(err_resp, sizeof(err_resp),
  649. "Install WASM app failed: %s", err);
  650. SEND_ERR_RESPONSE(msg->mid, err_resp);
  651. goto fail;
  652. }
  653. /* Destroy useless sections from list after load */
  654. destroy_part_wasm_sections(&bytecode_file->sections, sections1,
  655. sizeof(sections1) / sizeof(uint8));
  656. #if WASM_ENABLE_LIBC_WASI != 0
  657. if (!wasm_app_prepare_wasi_dir(module, m_name, wasi_dir_buf,
  658. sizeof(wasi_dir_buf))) {
  659. SEND_ERR_RESPONSE(
  660. msg->mid,
  661. "Install WASM app failed: prepare wasi env failed.");
  662. goto fail;
  663. }
  664. wasm_runtime_set_wasi_args(module, wasi_dir_list, 1, NULL, 0, NULL,
  665. 0, NULL, 0);
  666. #endif
  667. /* Instantiate the wasm module */
  668. inst =
  669. wasm_runtime_instantiate(module, 0, heap_size, err, err_size);
  670. if (!inst) {
  671. snprintf(err_resp, sizeof(err_resp),
  672. "Install WASM app failed: %s", err);
  673. SEND_ERR_RESPONSE(msg->mid, err_resp);
  674. goto fail;
  675. }
  676. /* Destroy useless sections from list after instantiate */
  677. destroy_part_wasm_sections(&bytecode_file->sections, sections2,
  678. sizeof(sections2) / sizeof(uint8));
  679. break;
  680. }
  681. #endif /* endof WASM_ENALBE_INTERP != 0 || WASM_ENABLE_JIT != 0 */
  682. default:
  683. SEND_ERR_RESPONSE(
  684. msg->mid,
  685. "Install WASM app failed: invalid wasm package type.");
  686. goto fail;
  687. }
  688. /* Create module data including the wasm_app_data as its internal_data*/
  689. m_data_size = offsetof(module_data, module_name) + strlen(m_name) + 1;
  690. m_data_size = align_uint(m_data_size, 4);
  691. m_data = APP_MGR_MALLOC(m_data_size + sizeof(wasm_data));
  692. if (!m_data) {
  693. SEND_ERR_RESPONSE(msg->mid,
  694. "Install WASM app failed: allocate memory failed.");
  695. goto fail;
  696. }
  697. memset(m_data, 0, m_data_size + sizeof(wasm_data));
  698. m_data->module_type = Module_WASM_App;
  699. m_data->internal_data = (uint8 *)m_data + m_data_size;
  700. wasm_app_data = (wasm_data *)m_data->internal_data;
  701. wasm_app_data->wasm_module_inst = inst;
  702. wasm_app_data->wasm_module = module;
  703. wasm_app_data->m_data = m_data;
  704. if (package_type == Wasm_Module_Bytecode) {
  705. wasm_app_data->is_bytecode = true;
  706. wasm_app_data->sections = wasm_app_file->u.bytecode.sections;
  707. }
  708. else {
  709. wasm_app_data->is_bytecode = false;
  710. wasm_app_data->sections = wasm_app_file->u.aot.sections;
  711. }
  712. if (!(wasm_app_data->exec_env = exec_env =
  713. wasm_runtime_create_exec_env(inst, DEFAULT_WASM_STACK_SIZE))) {
  714. SEND_ERR_RESPONSE(msg->mid,
  715. "Install WASM app failed: create exec env failed.");
  716. goto fail;
  717. }
  718. /* Set module data - name and module type */
  719. bh_strcpy_s(m_data->module_name, strlen(m_name) + 1, m_name);
  720. /* Set module data - execution timeout */
  721. timeout = DEFAULT_WATCHDOG_INTERVAL;
  722. find_key_value(properties, strlen(properties), "wd", timeout_str,
  723. sizeof(timeout_str) - 1, '&');
  724. if (strlen(timeout_str) > 0)
  725. timeout = atoi(timeout_str);
  726. m_data->timeout = timeout;
  727. /* Set module data - create queue */
  728. m_data->queue = bh_queue_create();
  729. if (!m_data->queue) {
  730. SEND_ERR_RESPONSE(msg->mid,
  731. "Install WASM app failed: create app queue failed.");
  732. goto fail;
  733. }
  734. /* Set heap size */
  735. m_data->heap_size = heap_size;
  736. /* Set module data - timers number */
  737. timers = DEFAULT_TIMERS_PER_APP;
  738. find_key_value(properties, strlen(properties), "timers", timers_str,
  739. sizeof(timers_str) - 1, '&');
  740. if (strlen(timers_str) > 0) {
  741. timers = atoi(timers_str);
  742. if (timers > MAX_TIMERS_PER_APP)
  743. timers = MAX_TIMERS_PER_APP;
  744. }
  745. /* Attention: must add the module before start the thread! */
  746. app_manager_add_module_data(m_data);
  747. m_data->timer_ctx = create_wasm_timer_ctx(m_data->id, timers);
  748. if (!m_data->timer_ctx) {
  749. SEND_ERR_RESPONSE(msg->mid,
  750. "Install WASM app failed: create app timers failed.");
  751. goto fail;
  752. }
  753. /* Initialize watchdog timer */
  754. if (!watchdog_timer_init(m_data)) {
  755. SEND_ERR_RESPONSE(
  756. msg->mid,
  757. "Install WASM app failed: create app watchdog timer failed.");
  758. goto fail;
  759. }
  760. stack_size = APP_THREAD_STACK_SIZE_DEFAULT;
  761. #ifdef OS_ENABLE_HW_BOUND_CHECK
  762. stack_size += 4 * BH_KB;
  763. #endif
  764. /* Create WASM app thread. */
  765. if (os_thread_create(&wasm_app_data->thread_id, wasm_app_routine,
  766. (void *)m_data, stack_size)
  767. != 0) {
  768. module_data_list_remove(m_data);
  769. SEND_ERR_RESPONSE(msg->mid,
  770. "Install WASM app failed: create app thread failed.");
  771. goto fail;
  772. }
  773. /* only when thread is created it is the flag of installation success */
  774. app_manager_post_applets_update_event();
  775. app_manager_printf("Install WASM app success!\n");
  776. send_error_response_to_host(msg->mid, CREATED_2_01, NULL); /* CREATED */
  777. return true;
  778. fail:
  779. if (m_data)
  780. release_module(m_data);
  781. if (inst)
  782. wasm_runtime_deinstantiate(inst);
  783. if (module)
  784. wasm_runtime_unload(module);
  785. if (exec_env)
  786. wasm_runtime_destroy_exec_env(exec_env);
  787. switch (package_type) {
  788. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  789. case Wasm_Module_Bytecode:
  790. destroy_all_wasm_sections(wasm_app_file->u.bytecode.sections);
  791. break;
  792. #endif
  793. #if WASM_ENABLE_AOT != 0
  794. case Wasm_Module_AoT:
  795. destroy_all_aot_sections(wasm_app_file->u.aot.sections);
  796. break;
  797. #endif
  798. default:
  799. break;
  800. }
  801. return false;
  802. }
  803. /* For internal use: if defined to 1, the process will
  804. * exit when wasm app is uninstalled. Hence valgrind can
  805. * print memory leak report. */
  806. #ifndef VALGRIND_CHECK
  807. #define VALGRIND_CHECK 0
  808. #endif
  809. /* Uninstall WASM app */
  810. static bool
  811. wasm_app_module_uninstall(request_t *msg)
  812. {
  813. module_data *m_data;
  814. wasm_data *wasm_app_data;
  815. char m_name[APP_NAME_MAX_LEN] = { 0 };
  816. char *properties;
  817. int properties_offset;
  818. properties_offset = check_url_start(msg->url, strlen(msg->url), "/applet");
  819. /* TODO: assert(properties_offset > 0) */
  820. if (properties_offset <= 0)
  821. return false;
  822. properties = msg->url + properties_offset;
  823. find_key_value(properties, strlen(properties), "name", m_name,
  824. sizeof(m_name) - 1, '&');
  825. if (strlen(m_name) == 0) {
  826. SEND_ERR_RESPONSE(msg->mid,
  827. "Uninstall WASM app failed: invalid app name.");
  828. return false;
  829. }
  830. m_data = app_manager_lookup_module_data(m_name);
  831. if (!m_data) {
  832. SEND_ERR_RESPONSE(msg->mid, "Uninstall WASM app failed: no app found.");
  833. return false;
  834. }
  835. if (m_data->module_type != Module_WASM_App) {
  836. SEND_ERR_RESPONSE(msg->mid,
  837. "Uninstall WASM app failed: invalid module type.");
  838. return false;
  839. }
  840. if (m_data->wd_timer.is_interrupting) {
  841. SEND_ERR_RESPONSE(
  842. msg->mid,
  843. "Uninstall WASM app failed: app is being interrupted by watchdog.");
  844. return false;
  845. }
  846. /* Exit app queue loop run */
  847. bh_queue_exit_loop_run(m_data->queue);
  848. /* Wait for wasm app thread to exit */
  849. wasm_app_data = (wasm_data *)m_data->internal_data;
  850. os_thread_join(wasm_app_data->thread_id, NULL);
  851. cleanup_app_resource(m_data);
  852. app_manager_post_applets_update_event();
  853. app_manager_printf("Uninstall WASM app successful!\n");
  854. #ifdef COLLECT_CODE_COVERAGE
  855. /* Exit app manager so as to collect code coverage data */
  856. if (!strcmp(m_name, "__exit_app_manager__")) {
  857. app_manager_printf("Exit app manager\n");
  858. bh_queue_exit_loop_run(get_app_manager_queue());
  859. }
  860. #endif
  861. #if VALGRIND_CHECK != 0
  862. bh_queue_exit_loop_run(get_app_manager_queue());
  863. #endif
  864. send_error_response_to_host(msg->mid, DELETED_2_02, NULL); /* DELETED */
  865. return true;
  866. }
  867. static bool
  868. wasm_app_module_handle_host_url(void *queue_msg)
  869. {
  870. /* TODO: implement in future */
  871. app_manager_printf("App handles host url address %d\n",
  872. (int)(uintptr_t)queue_msg);
  873. return false;
  874. }
  875. static module_data *
  876. wasm_app_module_get_module_data(void *inst)
  877. {
  878. wasm_module_inst_t module_inst = (wasm_module_inst_t)inst;
  879. return (module_data *)wasm_runtime_get_custom_data(module_inst);
  880. }
  881. static void
  882. wasm_app_module_watchdog_kill(module_data *m_data)
  883. {
  884. /* TODO: implement in future */
  885. app_manager_printf("Watchdog kills app: %s\n", m_data->module_name);
  886. return;
  887. }
  888. bool
  889. wasm_register_msg_callback(int message_type,
  890. message_type_handler_t message_handler)
  891. {
  892. int i;
  893. int freeslot = -1;
  894. for (i = 0; i < Max_Msg_Callback; i++) {
  895. /* replace handler for the same event registered */
  896. if (g_msg_type[i] == message_type)
  897. break;
  898. if (g_msg_callbacks[i] == NULL && freeslot == -1)
  899. freeslot = i;
  900. }
  901. if (i != Max_Msg_Callback)
  902. g_msg_callbacks[i] = message_handler;
  903. else if (freeslot != -1) {
  904. g_msg_callbacks[freeslot] = message_handler;
  905. g_msg_type[freeslot] = message_type;
  906. }
  907. else
  908. return false;
  909. return true;
  910. }
  911. bool
  912. wasm_register_cleanup_callback(resource_cleanup_handler_t handler)
  913. {
  914. int i;
  915. for (i = 0; i < Max_Cleanup_Callback; i++) {
  916. if (g_cleanup_callbacks[i] == NULL) {
  917. g_cleanup_callbacks[i] = handler;
  918. return true;
  919. }
  920. }
  921. return false;
  922. }
  923. #define RECV_INTEGER(value, next_phase) \
  924. do { \
  925. uint8 *p = (uint8 *)&value; \
  926. p[recv_ctx.size_in_phase++] = ch; \
  927. if (recv_ctx.size_in_phase == sizeof(value)) { \
  928. if (sizeof(value) == 4) \
  929. value = ntohl(value); \
  930. else if (sizeof(value) == 2) \
  931. value = ntohs(value); \
  932. recv_ctx.phase = next_phase; \
  933. recv_ctx.size_in_phase = 0; \
  934. } \
  935. } while (0)
  936. /* return:
  937. * 1: whole wasm app arrived
  938. * 0: one valid byte arrived
  939. * -1: fail to process the byte arrived, e.g. allocate memory fail
  940. */
  941. static bool
  942. wasm_app_module_on_install_request_byte_arrive(uint8 ch, int request_total_size,
  943. int *received_size)
  944. {
  945. uint8 *p;
  946. int magic;
  947. package_type_t package_type = Package_Type_Unknown;
  948. if (recv_ctx.phase == Phase_Req_Ver) {
  949. recv_ctx.phase = Phase_Req_Ver;
  950. recv_ctx.size_in_phase = 0;
  951. recv_ctx.total_received_size = 0;
  952. }
  953. recv_ctx.total_received_size++;
  954. *received_size = recv_ctx.total_received_size;
  955. if (recv_ctx.phase == Phase_Req_Ver) {
  956. if (ch != 1 /* REQUES_PACKET_VER from restful_utils.c */)
  957. return false;
  958. recv_ctx.phase = Phase_Req_Action;
  959. return true;
  960. }
  961. else if (recv_ctx.phase == Phase_Req_Action) {
  962. recv_ctx.message.request_action = ch;
  963. recv_ctx.phase = Phase_Req_Fmt;
  964. recv_ctx.size_in_phase = 0;
  965. return true;
  966. }
  967. else if (recv_ctx.phase == Phase_Req_Fmt) {
  968. RECV_INTEGER(recv_ctx.message.request_fmt, Phase_Req_Mid);
  969. return true;
  970. }
  971. else if (recv_ctx.phase == Phase_Req_Mid) {
  972. RECV_INTEGER(recv_ctx.message.request_mid, Phase_Req_Sender);
  973. return true;
  974. }
  975. else if (recv_ctx.phase == Phase_Req_Sender) {
  976. RECV_INTEGER(recv_ctx.message.request_sender, Phase_Req_Url_Len);
  977. return true;
  978. }
  979. else if (recv_ctx.phase == Phase_Req_Url_Len) {
  980. p = (uint8 *)&recv_ctx.message.request_url_len;
  981. p[recv_ctx.size_in_phase++] = ch;
  982. if (recv_ctx.size_in_phase
  983. == sizeof(recv_ctx.message.request_url_len)) {
  984. recv_ctx.message.request_url_len =
  985. ntohs(recv_ctx.message.request_url_len);
  986. recv_ctx.message.request_url =
  987. APP_MGR_MALLOC(recv_ctx.message.request_url_len + 1);
  988. if (NULL == recv_ctx.message.request_url) {
  989. app_manager_printf("Allocate memory failed!\n");
  990. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  991. "Install WASM app failed: "
  992. "allocate memory failed.");
  993. goto fail;
  994. }
  995. memset(recv_ctx.message.request_url, 0,
  996. recv_ctx.message.request_url_len + 1);
  997. recv_ctx.phase = Phase_Req_Payload_Len;
  998. recv_ctx.size_in_phase = 0;
  999. }
  1000. return true;
  1001. }
  1002. else if (recv_ctx.phase == Phase_Req_Payload_Len) {
  1003. RECV_INTEGER(recv_ctx.message.wasm_app_size, Phase_Req_Url);
  1004. return true;
  1005. }
  1006. else if (recv_ctx.phase == Phase_Req_Url) {
  1007. recv_ctx.message.request_url[recv_ctx.size_in_phase++] = ch;
  1008. if (recv_ctx.size_in_phase == recv_ctx.message.request_url_len) {
  1009. recv_ctx.phase = Phase_App_Magic;
  1010. recv_ctx.size_in_phase = 0;
  1011. }
  1012. return true;
  1013. }
  1014. else if (recv_ctx.phase == Phase_App_Magic) {
  1015. /* start to receive wasm app magic: bytecode or aot */
  1016. p = (uint8 *)&recv_ctx.message.app_file_magic;
  1017. p[recv_ctx.size_in_phase++] = ch;
  1018. if (recv_ctx.size_in_phase == sizeof(recv_ctx.message.app_file_magic)) {
  1019. magic = recv_ctx.message.app_file_magic;
  1020. package_type = get_package_type((uint8 *)&magic, sizeof(magic) + 1);
  1021. switch (package_type) {
  1022. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1023. case Wasm_Module_Bytecode:
  1024. recv_ctx.message.app_file.u.bytecode.magic =
  1025. recv_ctx.message.app_file_magic;
  1026. recv_ctx.phase = Phase_Wasm_Version;
  1027. recv_ctx.size_in_phase = 0;
  1028. break;
  1029. #endif
  1030. #if WASM_ENABLE_AOT != 0
  1031. case Wasm_Module_AoT:
  1032. recv_ctx.message.app_file.u.aot.magic =
  1033. recv_ctx.message.app_file_magic;
  1034. recv_ctx.phase = Phase_AOT_Version;
  1035. recv_ctx.size_in_phase = 0;
  1036. break;
  1037. #endif
  1038. default:
  1039. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1040. "Install WASM app failed: "
  1041. "invalid file format.");
  1042. goto fail;
  1043. }
  1044. }
  1045. return true;
  1046. }
  1047. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1048. else if (recv_ctx.phase == Phase_Wasm_Version) {
  1049. p = (uint8 *)&recv_ctx.message.app_file.u.bytecode.version;
  1050. if (ch == wasm_bytecode_version[recv_ctx.size_in_phase])
  1051. p[recv_ctx.size_in_phase++] = ch;
  1052. else {
  1053. app_manager_printf("Invalid WASM version!\n");
  1054. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1055. "Install WASM app failed: invalid WASM version.");
  1056. goto fail;
  1057. }
  1058. if (recv_ctx.size_in_phase
  1059. == sizeof(recv_ctx.message.app_file.u.bytecode.version)) {
  1060. recv_ctx.phase = Phase_Wasm_Section_Type;
  1061. recv_ctx.size_in_phase = 0;
  1062. }
  1063. return true;
  1064. }
  1065. else if (recv_ctx.phase == Phase_Wasm_Section_Type) {
  1066. uint8 section_type = ch;
  1067. #if WASM_ENABLE_BULK_MEMORY == 0
  1068. uint8 section_type_max = SECTION_TYPE_DATA;
  1069. #else
  1070. uint8 section_type_max = SECTION_TYPE_DATACOUNT;
  1071. #endif
  1072. if (section_type <= section_type_max) {
  1073. wasm_section_t *new_section;
  1074. if (!(new_section = (wasm_section_t *)APP_MGR_MALLOC(
  1075. sizeof(wasm_section_t)))) {
  1076. app_manager_printf("Allocate memory failed!\n");
  1077. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1078. "Install WASM app failed: "
  1079. "allocate memory failed.");
  1080. goto fail;
  1081. }
  1082. memset(new_section, 0, sizeof(wasm_section_t));
  1083. new_section->section_type = section_type;
  1084. new_section->next = NULL;
  1085. /* add the section to tail of link list */
  1086. if (NULL == recv_ctx.message.app_file.u.bytecode.sections) {
  1087. recv_ctx.message.app_file.u.bytecode.sections = new_section;
  1088. recv_ctx.message.app_file.u.bytecode.section_end = new_section;
  1089. }
  1090. else {
  1091. recv_ctx.message.app_file.u.bytecode.section_end->next =
  1092. new_section;
  1093. recv_ctx.message.app_file.u.bytecode.section_end = new_section;
  1094. }
  1095. recv_ctx.phase = Phase_Wasm_Section_Size;
  1096. recv_ctx.size_in_phase = 0;
  1097. return true;
  1098. }
  1099. else {
  1100. char error_buf[128];
  1101. app_manager_printf("Invalid wasm section type: %d\n", section_type);
  1102. snprintf(error_buf, sizeof(error_buf),
  1103. "Install WASM app failed: invalid wasm section type %d",
  1104. section_type);
  1105. SEND_ERR_RESPONSE(recv_ctx.message.request_mid, error_buf);
  1106. goto fail;
  1107. }
  1108. }
  1109. else if (recv_ctx.phase == Phase_Wasm_Section_Size) {
  1110. /* the last section is the current receiving one */
  1111. wasm_section_t *section =
  1112. recv_ctx.message.app_file.u.bytecode.section_end;
  1113. uint32 byte;
  1114. bh_assert(section);
  1115. byte = ch;
  1116. section->section_body_size |=
  1117. ((byte & 0x7f) << recv_ctx.size_in_phase * 7);
  1118. recv_ctx.size_in_phase++;
  1119. /* check leab128 overflow for uint32 value */
  1120. if (recv_ctx.size_in_phase
  1121. > (sizeof(section->section_body_size) * 8 + 7 - 1) / 7) {
  1122. app_manager_printf("LEB overflow when parsing section size\n");
  1123. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1124. "Install WASM app failed: "
  1125. "LEB overflow when parsing section size");
  1126. goto fail;
  1127. }
  1128. if ((byte & 0x80) == 0) {
  1129. /* leb128 encoded section size parsed done */
  1130. if (!(section->section_body =
  1131. APP_MGR_MALLOC(section->section_body_size))) {
  1132. app_manager_printf("Allocate memory failed!\n");
  1133. SEND_ERR_RESPONSE(
  1134. recv_ctx.message.request_mid,
  1135. "Install WASM app failed: allocate memory failed");
  1136. goto fail;
  1137. }
  1138. recv_ctx.phase = Phase_Wasm_Section_Content;
  1139. recv_ctx.size_in_phase = 0;
  1140. }
  1141. return true;
  1142. }
  1143. else if (recv_ctx.phase == Phase_Wasm_Section_Content) {
  1144. /* the last section is the current receiving one */
  1145. wasm_section_t *section =
  1146. recv_ctx.message.app_file.u.bytecode.section_end;
  1147. bh_assert(section);
  1148. section->section_body[recv_ctx.size_in_phase++] = ch;
  1149. if (recv_ctx.size_in_phase == section->section_body_size) {
  1150. if (recv_ctx.total_received_size == request_total_size) {
  1151. /* whole wasm app received */
  1152. if (module_wasm_app_handle_install_msg(&recv_ctx.message)) {
  1153. APP_MGR_FREE(recv_ctx.message.request_url);
  1154. recv_ctx.message.request_url = NULL;
  1155. memset(&recv_ctx, 0, sizeof(recv_ctx));
  1156. return true;
  1157. }
  1158. else {
  1159. app_manager_printf("Handle install message failed!\n");
  1160. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1161. "Install WASM app failed: "
  1162. "handle install message failed");
  1163. /**
  1164. * The sections were destroyed inside
  1165. * module_wasm_app_handle_install_msg(),
  1166. * no need to destroy again.
  1167. */
  1168. return false;
  1169. }
  1170. }
  1171. else {
  1172. recv_ctx.phase = Phase_Wasm_Section_Type;
  1173. recv_ctx.size_in_phase = 0;
  1174. return true;
  1175. }
  1176. }
  1177. return true;
  1178. }
  1179. #endif /* end of WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0 */
  1180. #if WASM_ENABLE_AOT != 0
  1181. else if (recv_ctx.phase == Phase_AOT_Version) {
  1182. p = (uint8 *)&recv_ctx.message.app_file.u.aot.version;
  1183. if (ch == wasm_aot_version[recv_ctx.size_in_phase])
  1184. p[recv_ctx.size_in_phase++] = ch;
  1185. else {
  1186. app_manager_printf("Invalid AOT version!\n");
  1187. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1188. "Install WASM app failed: invalid AOT version");
  1189. goto fail;
  1190. }
  1191. if (recv_ctx.size_in_phase
  1192. == sizeof(recv_ctx.message.app_file.u.aot.version)) {
  1193. recv_ctx.phase = Phase_AOT_Section_ID;
  1194. recv_ctx.size_in_phase = 0;
  1195. }
  1196. return true;
  1197. }
  1198. else if (recv_ctx.phase == Phase_AOT_Section_ID) {
  1199. aot_section_t *cur_section;
  1200. uint32 aot_file_cur_offset =
  1201. recv_ctx.total_received_size - 1
  1202. - 18 /* Request fixed part */ - recv_ctx.message.request_url_len;
  1203. if (recv_ctx.size_in_phase == 0) {
  1204. /* Skip paddings */
  1205. if (aot_file_cur_offset % 4)
  1206. return true;
  1207. if (!(cur_section =
  1208. (aot_section_t *)APP_MGR_MALLOC(sizeof(aot_section_t)))) {
  1209. app_manager_printf("Allocate memory failed!\n");
  1210. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1211. "Install WASM app failed: "
  1212. "allocate memory failed");
  1213. goto fail;
  1214. }
  1215. memset(cur_section, 0, sizeof(aot_section_t));
  1216. /* add the section to tail of link list */
  1217. if (NULL == recv_ctx.message.app_file.u.aot.sections) {
  1218. recv_ctx.message.app_file.u.aot.sections = cur_section;
  1219. recv_ctx.message.app_file.u.aot.section_end = cur_section;
  1220. }
  1221. else {
  1222. recv_ctx.message.app_file.u.aot.section_end->next = cur_section;
  1223. recv_ctx.message.app_file.u.aot.section_end = cur_section;
  1224. }
  1225. }
  1226. else {
  1227. cur_section = recv_ctx.message.app_file.u.aot.section_end;
  1228. bh_assert(cur_section);
  1229. }
  1230. p = (uint8 *)&cur_section->section_type;
  1231. p[recv_ctx.size_in_phase++] = ch;
  1232. if (recv_ctx.size_in_phase == sizeof(cur_section->section_type)) {
  1233. /* Notes: integers are always little endian encoded in AOT file */
  1234. if (!is_little_endian())
  1235. exchange_uint32(p);
  1236. if (cur_section->section_type < AOT_SECTION_TYPE_SIGANATURE
  1237. || cur_section->section_type == AOT_SECTION_TYPE_CUSTOM) {
  1238. recv_ctx.phase = Phase_AOT_Section_Size;
  1239. recv_ctx.size_in_phase = 0;
  1240. }
  1241. else {
  1242. char error_buf[128];
  1243. app_manager_printf("Invalid AOT section id: %d\n",
  1244. cur_section->section_type);
  1245. snprintf(error_buf, sizeof(error_buf),
  1246. "Install WASM app failed: invalid AOT section id %d",
  1247. cur_section->section_type);
  1248. SEND_ERR_RESPONSE(recv_ctx.message.request_mid, error_buf);
  1249. goto fail;
  1250. }
  1251. }
  1252. return true;
  1253. }
  1254. else if (recv_ctx.phase == Phase_AOT_Section_Size) {
  1255. /* the last section is the current receiving one */
  1256. aot_section_t *section = recv_ctx.message.app_file.u.aot.section_end;
  1257. bh_assert(section);
  1258. p = (uint8 *)&section->section_body_size;
  1259. p[recv_ctx.size_in_phase++] = ch;
  1260. if (recv_ctx.size_in_phase == sizeof(section->section_body_size)) {
  1261. /* Notes: integers are always little endian encoded in AOT file */
  1262. if (!is_little_endian())
  1263. exchange_uint32(p);
  1264. /* Allocate memory for section body */
  1265. if (section->section_body_size > 0) {
  1266. if (section->section_type == AOT_SECTION_TYPE_TEXT) {
  1267. int map_prot =
  1268. MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  1269. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1270. || defined(BUILD_TARGET_RISCV64_LP64D) \
  1271. || defined(BUILD_TARGET_RISCV64_LP64)
  1272. /* aot code and data in x86_64 must be in range 0 to 2G due
  1273. to relocation for R_X86_64_32/32S/PC32 */
  1274. int map_flags = MMAP_MAP_32BIT;
  1275. #else
  1276. int map_flags = MMAP_MAP_NONE;
  1277. #endif
  1278. uint64 total_size = (uint64)section->section_body_size
  1279. + aot_get_plt_table_size();
  1280. total_size = (total_size + 3) & ~((uint64)3);
  1281. if (total_size >= UINT32_MAX
  1282. || !(section->section_body =
  1283. os_mmap(NULL, (uint32)total_size, map_prot,
  1284. map_flags, os_get_invalid_handle()))) {
  1285. app_manager_printf(
  1286. "Allocate executable memory failed!\n");
  1287. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1288. "Install WASM app failed: "
  1289. "allocate memory failed");
  1290. goto fail;
  1291. }
  1292. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1293. /* address must be in the first 2 Gigabytes of
  1294. the process address space */
  1295. bh_assert((uintptr_t)section->section_body < INT32_MAX);
  1296. #endif
  1297. }
  1298. else {
  1299. if (!(section->section_body =
  1300. APP_MGR_MALLOC(section->section_body_size))) {
  1301. app_manager_printf("Allocate memory failed!\n");
  1302. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1303. "Install WASM app failed: "
  1304. "allocate memory failed");
  1305. goto fail;
  1306. }
  1307. }
  1308. }
  1309. recv_ctx.phase = Phase_AOT_Section_Content;
  1310. recv_ctx.size_in_phase = 0;
  1311. }
  1312. return true;
  1313. }
  1314. else if (recv_ctx.phase == Phase_AOT_Section_Content) {
  1315. /* the last section is the current receiving one */
  1316. aot_section_t *section = recv_ctx.message.app_file.u.aot.section_end;
  1317. bh_assert(section && section->section_body);
  1318. section->section_body[recv_ctx.size_in_phase++] = ch;
  1319. if (recv_ctx.size_in_phase == section->section_body_size) {
  1320. if (section->section_type == AOT_SECTION_TYPE_TEXT) {
  1321. uint32 total_size =
  1322. section->section_body_size + aot_get_plt_table_size();
  1323. total_size = (total_size + 3) & ~3;
  1324. if (total_size > section->section_body_size) {
  1325. memset(section->section_body + section->section_body_size,
  1326. 0, total_size - section->section_body_size);
  1327. section->section_body_size = total_size;
  1328. }
  1329. }
  1330. if (recv_ctx.total_received_size == request_total_size) {
  1331. /* whole aot file received */
  1332. if (module_wasm_app_handle_install_msg(&recv_ctx.message)) {
  1333. APP_MGR_FREE(recv_ctx.message.request_url);
  1334. recv_ctx.message.request_url = NULL;
  1335. memset(&recv_ctx, 0, sizeof(recv_ctx));
  1336. return true;
  1337. }
  1338. else {
  1339. app_manager_printf("Handle install message failed!\n");
  1340. SEND_ERR_RESPONSE(recv_ctx.message.request_mid,
  1341. "Install WASM app failed: "
  1342. "handle install message failed");
  1343. /**
  1344. * The sections were destroyed inside
  1345. * module_wasm_app_handle_install_msg(),
  1346. * no need to destroy again.
  1347. */
  1348. return false;
  1349. }
  1350. }
  1351. else {
  1352. recv_ctx.phase = Phase_AOT_Section_ID;
  1353. recv_ctx.size_in_phase = 0;
  1354. return true;
  1355. }
  1356. }
  1357. return true;
  1358. }
  1359. #endif /* end of WASM_ENABLE_AOT != 0 */
  1360. fail:
  1361. /* Restore the package type */
  1362. magic = recv_ctx.message.app_file_magic;
  1363. package_type = get_package_type((uint8 *)&magic, sizeof(magic) + 1);
  1364. switch (package_type) {
  1365. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1366. case Wasm_Module_Bytecode:
  1367. destroy_all_wasm_sections(
  1368. recv_ctx.message.app_file.u.bytecode.sections);
  1369. break;
  1370. #endif
  1371. #if WASM_ENABLE_AOT != 0
  1372. case Wasm_Module_AoT:
  1373. destroy_all_aot_sections(recv_ctx.message.app_file.u.aot.sections);
  1374. break;
  1375. #endif
  1376. default:
  1377. break;
  1378. }
  1379. if (recv_ctx.message.request_url != NULL) {
  1380. APP_MGR_FREE(recv_ctx.message.request_url);
  1381. recv_ctx.message.request_url = NULL;
  1382. }
  1383. memset(&recv_ctx, 0, sizeof(recv_ctx));
  1384. return false;
  1385. }
  1386. static bool
  1387. module_wasm_app_handle_install_msg(install_wasm_app_msg_t *message)
  1388. {
  1389. request_t *request = NULL;
  1390. bh_message_t msg;
  1391. request = (request_t *)APP_MGR_MALLOC(sizeof(request_t));
  1392. if (request == NULL)
  1393. return false;
  1394. memset(request, 0, sizeof(*request));
  1395. request->action = message->request_action;
  1396. request->fmt = message->request_fmt;
  1397. request->url = bh_strdup(message->request_url);
  1398. request->sender = ID_HOST;
  1399. request->mid = message->request_mid;
  1400. request->payload_len = sizeof(message->app_file);
  1401. request->payload = APP_MGR_MALLOC(request->payload_len);
  1402. if (request->url == NULL || request->payload == NULL) {
  1403. request_cleaner(request);
  1404. return false;
  1405. }
  1406. /* Request payload is set to wasm_app_file_t struct,
  1407. * but not whole app buffer */
  1408. bh_memcpy_s(request->payload, request->payload_len, &message->app_file,
  1409. request->payload_len);
  1410. /* Since it's a wasm app install request, so directly post to app-mgr's
  1411. * queue. The benefit is that section list can be freed when the msg
  1412. * failed to post to app-mgr's queue. The defect is missing url check. */
  1413. if (!(msg = bh_new_msg(RESTFUL_REQUEST, request, sizeof(*request),
  1414. request_cleaner))) {
  1415. request_cleaner(request);
  1416. return false;
  1417. }
  1418. if (!bh_post_msg2(get_app_manager_queue(), msg))
  1419. return false;
  1420. return true;
  1421. }
  1422. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1423. static void
  1424. destroy_all_wasm_sections(wasm_section_list_t sections)
  1425. {
  1426. wasm_section_t *cur = sections;
  1427. while (cur) {
  1428. wasm_section_t *next = cur->next;
  1429. if (cur->section_body != NULL)
  1430. APP_MGR_FREE(cur->section_body);
  1431. APP_MGR_FREE(cur);
  1432. cur = next;
  1433. }
  1434. }
  1435. static void
  1436. destroy_part_wasm_sections(wasm_section_list_t *p_sections,
  1437. uint8 *section_types, int section_cnt)
  1438. {
  1439. int i;
  1440. for (i = 0; i < section_cnt; i++) {
  1441. uint8 section_type = section_types[i];
  1442. wasm_section_t *cur = *p_sections, *prev = NULL;
  1443. while (cur) {
  1444. wasm_section_t *next = cur->next;
  1445. if (cur->section_type == section_type) {
  1446. if (prev)
  1447. prev->next = next;
  1448. else
  1449. *p_sections = next;
  1450. if (cur->section_body != NULL)
  1451. APP_MGR_FREE(cur->section_body);
  1452. APP_MGR_FREE(cur);
  1453. break;
  1454. }
  1455. else {
  1456. prev = cur;
  1457. cur = next;
  1458. }
  1459. }
  1460. }
  1461. }
  1462. #endif
  1463. #if WASM_ENABLE_AOT != 0
  1464. static void
  1465. destroy_all_aot_sections(aot_section_list_t sections)
  1466. {
  1467. aot_section_t *cur = sections;
  1468. while (cur) {
  1469. aot_section_t *next = cur->next;
  1470. if (cur->section_body != NULL) {
  1471. if (cur->section_type == AOT_SECTION_TYPE_TEXT)
  1472. os_munmap(cur->section_body, cur->section_body_size);
  1473. else
  1474. APP_MGR_FREE(cur->section_body);
  1475. }
  1476. APP_MGR_FREE(cur);
  1477. cur = next;
  1478. }
  1479. }
  1480. static void
  1481. destroy_part_aot_sections(aot_section_list_t *p_sections, uint8 *section_types,
  1482. int section_cnt)
  1483. {
  1484. int i;
  1485. for (i = 0; i < section_cnt; i++) {
  1486. uint8 section_type = section_types[i];
  1487. aot_section_t *cur = *p_sections, *prev = NULL;
  1488. while (cur) {
  1489. aot_section_t *next = cur->next;
  1490. if (cur->section_type == section_type) {
  1491. if (prev)
  1492. prev->next = next;
  1493. else
  1494. *p_sections = next;
  1495. if (cur->section_body != NULL) {
  1496. if (cur->section_type == AOT_SECTION_TYPE_TEXT)
  1497. os_munmap(cur->section_body, cur->section_body_size);
  1498. else
  1499. APP_MGR_FREE(cur->section_body);
  1500. }
  1501. APP_MGR_FREE(cur);
  1502. break;
  1503. }
  1504. else {
  1505. prev = cur;
  1506. cur = next;
  1507. }
  1508. }
  1509. }
  1510. }
  1511. #endif
  1512. #if WASM_ENABLE_LIBC_WASI != 0
  1513. static char wasi_root_dir[PATH_MAX] = { '.' };
  1514. bool
  1515. wasm_set_wasi_root_dir(const char *root_dir)
  1516. {
  1517. char *path, resolved_path[PATH_MAX];
  1518. if (!(path = realpath(root_dir, resolved_path)))
  1519. return false;
  1520. snprintf(wasi_root_dir, sizeof(wasi_root_dir), "%s", path);
  1521. return true;
  1522. }
  1523. const char *
  1524. wasm_get_wasi_root_dir()
  1525. {
  1526. return wasi_root_dir;
  1527. }
  1528. #endif