module_wasm_app.c 57 KB

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