wasm_c_api.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_c_api_internal.h"
  6. #include "wasm_memory.h"
  7. #if WASM_ENABLE_INTERP != 0
  8. #include "wasm_runtime.h"
  9. #endif
  10. #if WASM_ENABLE_AOT != 0
  11. #include "aot_runtime.h"
  12. #endif
  13. #define NOT_REACHED() bh_assert(!"should not be reached")
  14. typedef struct wasm_module_ex_t wasm_module_ex_t;
  15. void
  16. wasm_module_delete_internal(wasm_module_t *);
  17. void
  18. wasm_instance_delete_internal(wasm_instance_t *);
  19. static void *
  20. malloc_internal(uint64 size)
  21. {
  22. void *mem = NULL;
  23. if (size >= UINT32_MAX) {
  24. return NULL;
  25. }
  26. mem = wasm_runtime_malloc((uint32)size);
  27. if (mem) {
  28. memset(mem, 0, size);
  29. }
  30. return mem;
  31. }
  32. #define FREEIF(p) \
  33. if (p) { \
  34. wasm_runtime_free(p); \
  35. }
  36. /* Vectors */
  37. #define INIT_VEC(vector_p, func_prefix, size) \
  38. do { \
  39. if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \
  40. goto failed; \
  41. } \
  42. func_prefix##_new_uninitialized(vector_p, size); \
  43. if (!(vector_p)->data) { \
  44. goto failed; \
  45. } \
  46. } while (false)
  47. #define DEINIT_VEC(vector_p, delete_func) \
  48. if ((vector_p)) { \
  49. if ((vector_p)->data) { \
  50. delete_func(vector_p); \
  51. } \
  52. wasm_runtime_free(vector_p); \
  53. vector_p = NULL; \
  54. }
  55. #define FREE_VEC_ELEMS(vec, del_func) \
  56. for (i = 0; i < (vec)->num_elems; ++i) { \
  57. del_func(*((vec)->data + i)); \
  58. *((vec)->data + i) = NULL; \
  59. }
  60. static inline void
  61. generic_vec_init_data(Vector *out, size_t num_of_elems, size_t size_of_elem)
  62. {
  63. /* size 0 is meaningless for a elemment */
  64. if (!size_of_elem || !bh_vector_init(out, num_of_elems, size_of_elem)) {
  65. out->data = NULL;
  66. out->max_elems = 0;
  67. out->num_elems = 0;
  68. }
  69. else {
  70. memset(out->data, 0, num_of_elems * size_of_elem);
  71. }
  72. }
  73. void
  74. wasm_byte_vec_new_uninitialized(wasm_byte_vec_t *out, size_t size)
  75. {
  76. bh_assert(out);
  77. generic_vec_init_data((Vector *)out, size, sizeof(wasm_byte_t));
  78. }
  79. void
  80. wasm_byte_vec_copy(wasm_byte_vec_t *out, const wasm_byte_vec_t *src)
  81. {
  82. uint32 len = 0;
  83. bh_assert(out && src);
  84. generic_vec_init_data((Vector *)out, src->size, sizeof(wasm_byte_t));
  85. if (!out->data) {
  86. goto failed;
  87. }
  88. /* integer overflow has been checked in generic_vec_init_data,
  89. no need to check again */
  90. len = (uint32)(src->size * src->size_of_elem);
  91. bh_memcpy_s(out->data, len, src->data, len);
  92. out->num_elems = src->num_elems;
  93. return;
  94. failed:
  95. LOG_DEBUG("%s failed", __FUNCTION__);
  96. wasm_byte_vec_delete(out);
  97. }
  98. void
  99. wasm_byte_vec_new(wasm_byte_vec_t *out, size_t size, const wasm_byte_t *data)
  100. {
  101. uint32 size_in_bytes = 0;
  102. bh_assert(out && data);
  103. generic_vec_init_data((Vector *)out, size, sizeof(wasm_byte_t));
  104. if (!out->data) {
  105. goto failed;
  106. }
  107. /* integer overflow has been checked in generic_vec_init_data,
  108. no need to check again */
  109. size_in_bytes = (uint32)(size * sizeof(wasm_byte_t));
  110. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes);
  111. out->num_elems = size;
  112. return;
  113. failed:
  114. LOG_DEBUG("%s failed", __FUNCTION__);
  115. wasm_byte_vec_delete(out);
  116. }
  117. void
  118. wasm_byte_vec_delete(wasm_byte_vec_t *byte_vec)
  119. {
  120. if (byte_vec && byte_vec->data) {
  121. bh_vector_destroy((Vector *)byte_vec);
  122. }
  123. }
  124. /* Runtime Environment */
  125. static void
  126. wasm_engine_delete_internal(wasm_engine_t *engine)
  127. {
  128. if (engine) {
  129. DEINIT_VEC(engine->stores, wasm_store_vec_delete);
  130. wasm_runtime_free(engine);
  131. }
  132. wasm_runtime_destroy();
  133. }
  134. static wasm_engine_t *
  135. wasm_engine_new_internal(mem_alloc_type_t type,
  136. const MemAllocOption *opts,
  137. runtime_mode_e mode)
  138. {
  139. wasm_engine_t *engine = NULL;
  140. /* init runtime */
  141. RuntimeInitArgs init_args = { 0 };
  142. init_args.mem_alloc_type = type;
  143. if (type == Alloc_With_Pool) {
  144. init_args.mem_alloc_option.pool.heap_buf = opts->pool.heap_buf;
  145. init_args.mem_alloc_option.pool.heap_size = opts->pool.heap_size;
  146. }
  147. else if (type == Alloc_With_Allocator) {
  148. init_args.mem_alloc_option.allocator.malloc_func =
  149. opts->allocator.malloc_func;
  150. init_args.mem_alloc_option.allocator.free_func =
  151. opts->allocator.free_func;
  152. init_args.mem_alloc_option.allocator.realloc_func =
  153. opts->allocator.realloc_func;
  154. }
  155. else {
  156. init_args.mem_alloc_option.pool.heap_buf = NULL;
  157. init_args.mem_alloc_option.pool.heap_size = 0;
  158. }
  159. if (!wasm_runtime_full_init(&init_args)) {
  160. goto failed;
  161. }
  162. #if BH_DEBUG != 0
  163. bh_log_set_verbose_level(5);
  164. #else
  165. bh_log_set_verbose_level(3);
  166. #endif
  167. /* create wasm_engine_t */
  168. engine = malloc_internal(sizeof(wasm_engine_t));
  169. if (!engine) {
  170. goto failed;
  171. }
  172. /* set running mode */
  173. LOG_WARNING("running under mode %d", mode);
  174. engine->mode = mode;
  175. /* create wasm_store_vec_t */
  176. INIT_VEC(engine->stores, wasm_store_vec, 1);
  177. return engine;
  178. failed:
  179. LOG_DEBUG("%s failed", __FUNCTION__);
  180. wasm_engine_delete_internal(engine);
  181. return NULL;
  182. }
  183. /* global engine instance */
  184. static wasm_engine_t *singleton_engine = NULL;
  185. static inline runtime_mode_e
  186. current_runtime_mode()
  187. {
  188. bh_assert(singleton_engine);
  189. return singleton_engine->mode;
  190. }
  191. wasm_engine_t *
  192. wasm_engine_new()
  193. {
  194. runtime_mode_e mode = INTERP_MODE;
  195. #if WASM_ENABLE_INTERP == 0 && WASM_ENABLE_AOT != 0
  196. mode = AOT_MODE;
  197. #endif
  198. if (INTERP_MODE == mode) {
  199. #if WASM_ENABLE_INTERP == 0
  200. bh_assert(!"does not support INTERP_MODE. Please recompile");
  201. #endif
  202. }
  203. else {
  204. #if WASM_ENABLE_AOT == 0
  205. bh_assert(!"does not support AOT_MODE. Please recompile");
  206. #endif
  207. }
  208. if (!singleton_engine) {
  209. singleton_engine =
  210. wasm_engine_new_internal(Alloc_With_System_Allocator, NULL, mode);
  211. }
  212. return singleton_engine;
  213. }
  214. wasm_engine_t *
  215. wasm_engine_new_with_args(mem_alloc_type_t type,
  216. const MemAllocOption *opts,
  217. runtime_mode_e mode)
  218. {
  219. if (!singleton_engine) {
  220. singleton_engine = wasm_engine_new_internal(type, opts, mode);
  221. }
  222. return singleton_engine;
  223. }
  224. void
  225. wasm_engine_delete(wasm_engine_t *engine)
  226. {
  227. if (engine) {
  228. wasm_engine_delete_internal(engine);
  229. singleton_engine = NULL;
  230. }
  231. }
  232. wasm_store_t *
  233. wasm_store_new(wasm_engine_t *engine)
  234. {
  235. wasm_store_t *store = NULL;
  236. bh_assert(engine && singleton_engine == engine);
  237. /* always return the first store */
  238. if (bh_vector_size((Vector *)singleton_engine->stores) > 0) {
  239. /*
  240. * although it is a copy of the first store, but still point to
  241. * the wasm_store_t
  242. */
  243. if (!bh_vector_get((Vector *)singleton_engine->stores, 0, &store)) {
  244. goto failed;
  245. }
  246. return store;
  247. }
  248. store = malloc_internal(sizeof(wasm_store_t));
  249. if (!store) {
  250. goto failed;
  251. }
  252. /* new a vector, and new its data */
  253. INIT_VEC(store->modules, wasm_module_vec, DEFAULT_VECTOR_INIT_LENGTH);
  254. INIT_VEC(store->instances, wasm_instance_vec, DEFAULT_VECTOR_INIT_LENGTH);
  255. /* append to a store list of engine */
  256. if (!bh_vector_append((Vector *)singleton_engine->stores, &store)) {
  257. goto failed;
  258. }
  259. return store;
  260. failed:
  261. LOG_DEBUG("%s failed", __FUNCTION__);
  262. wasm_store_delete(store);
  263. return NULL;
  264. }
  265. void
  266. wasm_store_delete(wasm_store_t *store)
  267. {
  268. if (!store || singleton_engine->stores->num_elems == 0) {
  269. return;
  270. }
  271. DEINIT_VEC(store->modules, wasm_module_vec_delete);
  272. DEINIT_VEC(store->instances, wasm_instance_vec_delete);
  273. wasm_runtime_free(store);
  274. bh_vector_remove((Vector *)singleton_engine->stores, 0, NULL);
  275. }
  276. void
  277. wasm_store_vec_new_uninitialized(wasm_store_vec_t *out, size_t size)
  278. {
  279. bh_assert(out);
  280. generic_vec_init_data((Vector *)out, size, sizeof(wasm_store_t *));
  281. }
  282. void
  283. wasm_store_vec_delete(wasm_store_vec_t *store_vec)
  284. {
  285. size_t i = 0;
  286. if (!store_vec || !store_vec->data) {
  287. return;
  288. }
  289. FREE_VEC_ELEMS(store_vec, wasm_store_delete);
  290. bh_vector_destroy((Vector *)store_vec);
  291. }
  292. static inline void
  293. check_engine_and_store(wasm_engine_t *engine, wasm_store_t *store)
  294. {
  295. /* remove it if we are supporting more than one store */
  296. bh_assert(engine && store && engine->stores->data[0] == store);
  297. }
  298. /* Type Representations */
  299. static wasm_valtype_t *
  300. wasm_valtype_new_internal(uint8 val_type_rt)
  301. {
  302. switch (val_type_rt) {
  303. case VALUE_TYPE_I32:
  304. return wasm_valtype_new_i32();
  305. case VALUE_TYPE_I64:
  306. return wasm_valtype_new_i64();
  307. case VALUE_TYPE_F32:
  308. return wasm_valtype_new_f32();
  309. case VALUE_TYPE_F64:
  310. return wasm_valtype_new_f64();
  311. case VALUE_TYPE_ANY:
  312. return wasm_valtype_new_anyref();
  313. default:
  314. return NULL;
  315. }
  316. }
  317. wasm_valtype_t *
  318. wasm_valtype_new(wasm_valkind_t kind)
  319. {
  320. wasm_valtype_t *val_type = malloc_internal(sizeof(wasm_valtype_t));
  321. if (val_type) {
  322. val_type->kind = kind;
  323. }
  324. return val_type;
  325. }
  326. void
  327. wasm_valtype_delete(wasm_valtype_t *val_type)
  328. {
  329. bh_assert(val_type);
  330. wasm_runtime_free(val_type);
  331. }
  332. wasm_valtype_t *
  333. wasm_valtype_copy(wasm_valtype_t *src)
  334. {
  335. bh_assert(src);
  336. return wasm_valtype_new(src->kind);
  337. }
  338. wasm_valkind_t
  339. wasm_valtype_kind(const wasm_valtype_t *val_type)
  340. {
  341. bh_assert(val_type);
  342. return val_type->kind;
  343. }
  344. bool
  345. wasm_valtype_same(const wasm_valtype_t *vt1, const wasm_valtype_t *vt2)
  346. {
  347. if (!vt1 && !vt2) {
  348. return true;
  349. }
  350. if (!vt1 || !vt2) {
  351. return false;
  352. }
  353. return vt1->kind == vt2->kind;
  354. }
  355. void
  356. wasm_valtype_vec_new_uninitialized(wasm_valtype_vec_t *out, size_t size)
  357. {
  358. bh_assert(out);
  359. generic_vec_init_data((Vector *)out, size, sizeof(wasm_valtype_t *));
  360. }
  361. void
  362. wasm_valtype_vec_new_empty(wasm_valtype_vec_t *out)
  363. {
  364. bh_assert(out);
  365. memset(out, 0, sizeof(wasm_valtype_vec_t));
  366. }
  367. void
  368. wasm_valtype_vec_new(wasm_valtype_vec_t *out,
  369. size_t size,
  370. wasm_valtype_t *const data[])
  371. {
  372. uint32 size_in_bytes = 0;
  373. bh_assert(out && data);
  374. generic_vec_init_data((Vector *)out, size, sizeof(wasm_valtype_t *));
  375. if (!out->data) {
  376. goto failed;
  377. }
  378. /* integer overflow has been checked in generic_vec_init_data,
  379. no need to check again */
  380. size_in_bytes = (uint32)(size * sizeof(wasm_valtype_t *));
  381. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes);
  382. out->num_elems = size;
  383. return;
  384. failed:
  385. LOG_DEBUG("%s failed", __FUNCTION__);
  386. wasm_valtype_vec_delete(out);
  387. }
  388. void
  389. wasm_valtype_vec_copy(wasm_valtype_vec_t *out, const wasm_valtype_vec_t *src)
  390. {
  391. size_t i = 0;
  392. bh_assert(out && src);
  393. generic_vec_init_data((Vector *)out, src->size, sizeof(wasm_valtype_t *));
  394. if (!out->data) {
  395. goto failed;
  396. }
  397. /* clone every wasm_valtype_t */
  398. for (i = 0; i < src->num_elems; i++) {
  399. wasm_valtype_t *cloned = wasm_valtype_copy(*(src->data + i));
  400. if (!cloned) {
  401. goto failed;
  402. }
  403. if (!bh_vector_append((Vector *)out, &cloned)) {
  404. goto failed;
  405. }
  406. }
  407. return;
  408. failed:
  409. LOG_DEBUG("%s failed", __FUNCTION__);
  410. wasm_valtype_vec_delete(out);
  411. }
  412. void
  413. wasm_valtype_vec_delete(wasm_valtype_vec_t *val_type_vec)
  414. {
  415. size_t i = 0;
  416. if (!val_type_vec || !val_type_vec->data) {
  417. return;
  418. }
  419. FREE_VEC_ELEMS(val_type_vec, wasm_valtype_delete);
  420. bh_vector_destroy((Vector *)val_type_vec);
  421. }
  422. static wasm_functype_t *
  423. wasm_functype_new_internal(WASMType *type_rt)
  424. {
  425. wasm_functype_t *func_type = NULL;
  426. uint32 i = 0;
  427. bh_assert(type_rt);
  428. func_type = malloc_internal(sizeof(wasm_functype_t));
  429. if (!func_type) {
  430. goto failed;
  431. }
  432. /* WASMType->types[0 : type_rt->param_count) -> func_type->params */
  433. INIT_VEC(func_type->params, wasm_valtype_vec, type_rt->param_count);
  434. for (i = 0; i < type_rt->param_count; ++i) {
  435. wasm_valtype_t *param_type =
  436. wasm_valtype_new_internal(*(type_rt->types + i));
  437. if (!param_type) {
  438. goto failed;
  439. }
  440. if (!bh_vector_append((Vector *)func_type->params, &param_type)) {
  441. goto failed;
  442. }
  443. }
  444. /* WASMType->types[type_rt->param_count : type_rt->result_count) -> func_type->results */
  445. INIT_VEC(func_type->results, wasm_valtype_vec, type_rt->result_count);
  446. for (i = 0; i < type_rt->result_count; ++i) {
  447. wasm_valtype_t *result_type =
  448. wasm_valtype_new_internal(*(type_rt->types + type_rt->param_count + i));
  449. if (!result_type) {
  450. goto failed;
  451. }
  452. if (!bh_vector_append((Vector *)func_type->results, &result_type)) {
  453. goto failed;
  454. }
  455. }
  456. return func_type;
  457. failed:
  458. LOG_DEBUG("%s failed", __FUNCTION__);
  459. wasm_functype_delete(func_type);
  460. return NULL;
  461. }
  462. wasm_functype_t *
  463. wasm_functype_new(wasm_valtype_vec_t *params, wasm_valtype_vec_t *results)
  464. {
  465. wasm_functype_t *func_type = NULL;
  466. bh_assert(params);
  467. bh_assert(results);
  468. func_type = malloc_internal(sizeof(wasm_functype_t));
  469. if (!func_type) {
  470. goto failed;
  471. }
  472. func_type->extern_kind = WASM_EXTERN_FUNC;
  473. func_type->params = malloc_internal(sizeof(wasm_valtype_vec_t));
  474. if (!func_type->params) {
  475. goto failed;
  476. }
  477. /* transfer ownership of contents of params and results */
  478. bh_memcpy_s(func_type->params, sizeof(wasm_valtype_vec_t), params,
  479. sizeof(wasm_valtype_vec_t));
  480. func_type->results = malloc_internal(sizeof(wasm_valtype_vec_t));
  481. if (!func_type->results) {
  482. goto failed;
  483. }
  484. bh_memcpy_s(func_type->results, sizeof(wasm_valtype_vec_t), results,
  485. sizeof(wasm_valtype_vec_t));
  486. return func_type;
  487. failed:
  488. LOG_DEBUG("%s failed", __FUNCTION__);
  489. if (func_type)
  490. FREEIF(func_type->params);
  491. if (func_type)
  492. FREEIF(func_type->results);
  493. FREEIF(func_type);
  494. return NULL;
  495. }
  496. wasm_functype_t *
  497. wasm_functype_copy(wasm_functype_t *src)
  498. {
  499. wasm_functype_t *dst = NULL;
  500. bh_assert(src);
  501. dst = malloc_internal(sizeof(wasm_functype_t));
  502. if (!dst) {
  503. goto failed;
  504. }
  505. dst->extern_kind = src->extern_kind;
  506. dst->params = malloc_internal(sizeof(wasm_valtype_vec_t));
  507. if (!dst->params) {
  508. goto failed;
  509. }
  510. wasm_valtype_vec_copy(dst->params, src->params);
  511. if (!dst->params) {
  512. goto failed;
  513. }
  514. dst->results = malloc_internal(sizeof(wasm_valtype_vec_t));
  515. if (!dst->results) {
  516. goto failed;
  517. }
  518. wasm_valtype_vec_copy(dst->results, src->results);
  519. if (!dst->results) {
  520. goto failed;
  521. }
  522. return dst;
  523. failed:
  524. LOG_DEBUG("%s failed", __FUNCTION__);
  525. wasm_functype_delete(dst);
  526. return NULL;
  527. }
  528. void
  529. wasm_functype_delete(wasm_functype_t *func_type)
  530. {
  531. if (!func_type) {
  532. return;
  533. }
  534. if (func_type->params) {
  535. wasm_valtype_vec_delete(func_type->params);
  536. wasm_runtime_free(func_type->params);
  537. func_type->params = NULL;
  538. }
  539. if (func_type->results) {
  540. wasm_valtype_vec_delete(func_type->results);
  541. wasm_runtime_free(func_type->results);
  542. func_type->results = NULL;
  543. }
  544. wasm_runtime_free(func_type);
  545. }
  546. const wasm_valtype_vec_t *
  547. wasm_functype_params(const wasm_functype_t *func_type)
  548. {
  549. bh_assert(func_type);
  550. return func_type->params;
  551. }
  552. const wasm_valtype_vec_t *
  553. wasm_functype_results(const wasm_functype_t *func_type)
  554. {
  555. bh_assert(func_type);
  556. return func_type->results;
  557. }
  558. wasm_globaltype_t *
  559. wasm_globaltype_new(wasm_valtype_t *val_type, wasm_mutability_t mut)
  560. {
  561. wasm_globaltype_t *global_type = NULL;
  562. bh_assert(val_type);
  563. global_type = malloc_internal(sizeof(wasm_globaltype_t));
  564. if (!global_type) {
  565. goto failed;
  566. }
  567. global_type->val_type = val_type;
  568. global_type->mutability = mut;
  569. return global_type;
  570. failed:
  571. LOG_DEBUG("%s failed", __FUNCTION__);
  572. wasm_globaltype_delete(global_type);
  573. return NULL;
  574. }
  575. wasm_globaltype_t *
  576. wasm_globaltype_new_internal(uint8 val_type_rt, bool is_mutable)
  577. {
  578. wasm_globaltype_t *global_type = NULL;
  579. global_type = malloc_internal(sizeof(wasm_globaltype_t));
  580. if (!global_type) {
  581. goto failed;
  582. }
  583. global_type->val_type = wasm_valtype_new_internal(val_type_rt);
  584. if (!global_type->val_type) {
  585. goto failed;
  586. }
  587. global_type->mutability = is_mutable ? WASM_VAR : WASM_CONST;
  588. return global_type;
  589. failed:
  590. LOG_DEBUG("%s failed", __FUNCTION__);
  591. wasm_globaltype_delete(global_type);
  592. return NULL;
  593. }
  594. void
  595. wasm_globaltype_delete(wasm_globaltype_t *global_type)
  596. {
  597. if (!global_type) {
  598. return;
  599. }
  600. if (global_type->val_type) {
  601. wasm_valtype_delete(global_type->val_type);
  602. global_type->val_type = NULL;
  603. }
  604. wasm_runtime_free(global_type);
  605. }
  606. wasm_globaltype_t *
  607. wasm_globaltype_copy(wasm_globaltype_t *src)
  608. {
  609. wasm_globaltype_t *dst = NULL;
  610. bh_assert(src);
  611. dst = malloc_internal(sizeof(wasm_globaltype_t));
  612. if (!dst) {
  613. goto failed;
  614. }
  615. dst->val_type = wasm_valtype_copy(src->val_type);
  616. if (!dst->val_type) {
  617. goto failed;
  618. }
  619. dst->mutability = src->mutability;
  620. return dst;
  621. failed:
  622. LOG_DEBUG("%s failed", __FUNCTION__);
  623. wasm_globaltype_delete(dst);
  624. return NULL;
  625. }
  626. const wasm_valtype_t *
  627. wasm_globaltype_content(const wasm_globaltype_t *global_type)
  628. {
  629. bh_assert(global_type);
  630. return global_type->val_type;
  631. }
  632. wasm_mutability_t
  633. wasm_globaltype_mutability(const wasm_globaltype_t *global_type)
  634. {
  635. bh_assert(global_type);
  636. return global_type->mutability;
  637. }
  638. bool
  639. wasm_globaltype_same(const wasm_globaltype_t *gt1,
  640. const wasm_globaltype_t *gt2)
  641. {
  642. if (!gt1 && !gt2) {
  643. return true;
  644. }
  645. if (!gt1 || !gt2) {
  646. return false;
  647. }
  648. return wasm_valtype_same(gt1->val_type, gt2->val_type)
  649. || gt1->mutability == gt2->mutability;
  650. }
  651. wasm_tabletype_t *
  652. wasm_tabletype_new(wasm_valtype_t *val_type, const wasm_limits_t *limits)
  653. {
  654. return NULL;
  655. }
  656. void
  657. wasm_tabletype_delete(wasm_tabletype_t *table_type)
  658. {}
  659. wasm_memorytype_t *
  660. wasm_memorytype_new(const wasm_limits_t *limits)
  661. {
  662. return NULL;
  663. }
  664. void
  665. wasm_memorytype_delete(wasm_memorytype_t *memory_type)
  666. {}
  667. /* Runtime Objects */
  668. void
  669. wasm_val_delete(wasm_val_t *v)
  670. {
  671. /* do nothing */
  672. }
  673. void
  674. wasm_val_copy(wasm_val_t *out, const wasm_val_t *src)
  675. {
  676. bh_assert(out && src);
  677. bh_memcpy_s(out, sizeof(wasm_val_t), src, sizeof(wasm_val_t));
  678. }
  679. bool
  680. wasm_val_same(const wasm_val_t *v1, const wasm_val_t *v2)
  681. {
  682. if (!v1 && !v2) {
  683. return true;
  684. }
  685. if (!v1 || !v2) {
  686. return false;
  687. }
  688. if (v1->kind != v2->kind) {
  689. return false;
  690. }
  691. switch (v1->kind) {
  692. case WASM_I32:
  693. return v1->of.i32 == v2->of.i32;
  694. case WASM_I64:
  695. return v1->of.i64 == v2->of.i64;
  696. case WASM_F32:
  697. return v1->of.f32 == v2->of.f32;
  698. case WASM_F64:
  699. return v1->of.f64 == v2->of.f64;
  700. case WASM_FUNCREF:
  701. return v1->of.ref == v2->of.ref;
  702. default:
  703. break;
  704. }
  705. return false;
  706. }
  707. wasm_trap_t *
  708. wasm_trap_new(wasm_store_t *store, const wasm_message_t *message)
  709. {
  710. wasm_trap_t *trap;
  711. bh_assert(store && message);
  712. trap = malloc_internal(sizeof(wasm_trap_t));
  713. if (!trap) {
  714. goto failed;
  715. }
  716. wasm_byte_vec_new(trap->message, message->num_elems, message->data);
  717. if (!trap->message->data) {
  718. goto failed;
  719. }
  720. return trap;
  721. failed:
  722. wasm_trap_delete(trap);
  723. return NULL;
  724. }
  725. void
  726. wasm_trap_delete(wasm_trap_t *trap)
  727. {
  728. if (!trap) {
  729. return;
  730. }
  731. if (trap->message) {
  732. wasm_byte_vec_delete(trap->message);
  733. }
  734. wasm_runtime_free(trap);
  735. }
  736. void
  737. wasm_trap_message(const wasm_trap_t *trap, wasm_message_t *out)
  738. {
  739. bh_assert(trap && out);
  740. wasm_byte_vec_copy(out, trap->message);
  741. }
  742. struct wasm_module_ex_t {
  743. struct WASMModuleCommon *module_comm_rt;
  744. wasm_byte_vec_t *binary;
  745. };
  746. static inline wasm_module_t *
  747. module_ext_to_module(wasm_module_ex_t *module_ex)
  748. {
  749. return (wasm_module_t *)module_ex;
  750. }
  751. static inline wasm_module_ex_t *
  752. module_to_module_ext(wasm_module_t *module)
  753. {
  754. return (wasm_module_ex_t *)module;
  755. }
  756. wasm_module_t *
  757. wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
  758. {
  759. char error[128] = { 0 };
  760. wasm_module_ex_t *module_ex = NULL;
  761. PackageType pkg_type = Package_Type_Unknown;
  762. check_engine_and_store(singleton_engine, store);
  763. bh_assert(binary && binary->data && binary->size);
  764. if (binary->size > UINT32_MAX) {
  765. LOG_ERROR("%s failed", __FUNCTION__);
  766. return NULL;
  767. }
  768. pkg_type = get_package_type((uint8 *)binary->data, (uint32)binary->size);
  769. if (Package_Type_Unknown == pkg_type
  770. || (Wasm_Module_Bytecode == pkg_type
  771. && INTERP_MODE != current_runtime_mode())
  772. || (Wasm_Module_AoT == pkg_type
  773. && INTERP_MODE == current_runtime_mode())) {
  774. LOG_ERROR(
  775. "current runtime mode %d doesn\'t support the package type %d",
  776. current_runtime_mode(), pkg_type);
  777. return NULL;
  778. }
  779. module_ex = malloc_internal(sizeof(wasm_module_ex_t));
  780. if (!module_ex) {
  781. goto failed;
  782. }
  783. module_ex->binary = malloc_internal(sizeof(wasm_byte_vec_t));
  784. if (!module_ex->binary) {
  785. goto failed;
  786. }
  787. wasm_byte_vec_copy(module_ex->binary, binary);
  788. if (!module_ex->binary->data) {
  789. goto failed;
  790. }
  791. module_ex->module_comm_rt =
  792. wasm_runtime_load((uint8 *)module_ex->binary->data,
  793. (uint32)module_ex->binary->size,
  794. error, (uint32)sizeof(error));
  795. if (!(module_ex->module_comm_rt)) {
  796. LOG_ERROR(error);
  797. wasm_module_delete_internal(module_ext_to_module(module_ex));
  798. return NULL;
  799. }
  800. /* add it to a watching list in store */
  801. if (!bh_vector_append((Vector *)store->modules, &module_ex)) {
  802. goto failed;
  803. }
  804. return module_ext_to_module(module_ex);
  805. failed:
  806. LOG_ERROR("%s failed", __FUNCTION__);
  807. wasm_module_delete_internal(module_ext_to_module(module_ex));
  808. return NULL;
  809. }
  810. void
  811. wasm_module_delete_internal(wasm_module_t *module)
  812. {
  813. wasm_module_ex_t *module_ex;
  814. if (!module) {
  815. return;
  816. }
  817. module_ex = module_to_module_ext(module);
  818. if (module_ex->binary) {
  819. wasm_byte_vec_delete(module_ex->binary);
  820. wasm_runtime_free(module_ex->binary);
  821. module_ex->binary = NULL;
  822. }
  823. if (module_ex->module_comm_rt) {
  824. wasm_runtime_unload(module_ex->module_comm_rt);
  825. module_ex->module_comm_rt = NULL;
  826. }
  827. wasm_runtime_free(module_ex);
  828. }
  829. /* will release module when releasing the store */
  830. void
  831. wasm_module_delete(wasm_module_t *module)
  832. {
  833. /* pass */
  834. }
  835. void
  836. wasm_module_vec_new_uninitialized(wasm_module_vec_t *out, size_t size)
  837. {
  838. generic_vec_init_data((Vector *)out, size, sizeof(wasm_module_t *));
  839. }
  840. void
  841. wasm_module_vec_delete(wasm_module_vec_t *module_vec)
  842. {
  843. size_t i = 0;
  844. if (!module_vec || !module_vec->data) {
  845. return;
  846. }
  847. FREE_VEC_ELEMS(module_vec, wasm_module_delete_internal);
  848. bh_vector_destroy((Vector *)module_vec);
  849. }
  850. static uint32
  851. argv_to_params(const uint64 *argv,
  852. const wasm_valtype_vec_t *param_defs,
  853. wasm_val_t *out)
  854. {
  855. size_t i = 0;
  856. uint32 argc = 0;
  857. void *argv_p = (void *)argv;
  858. for (i = 0; i < param_defs->num_elems; i++) {
  859. wasm_valtype_t *param_def = param_defs->data[i];
  860. wasm_val_t *param = out + i;
  861. switch (param_def->kind) {
  862. case WASM_I32:
  863. param->kind = WASM_I32;
  864. param->of.i32 = *(int32 *)argv_p;
  865. argv_p = (uint32 *)argv_p + 1;
  866. argc++;
  867. break;
  868. case WASM_I64:
  869. param->kind = WASM_I64;
  870. param->of.i64 = *(int64 *)argv_p;
  871. argv_p = (uint64 *)argv_p + 1;
  872. argc++;
  873. break;
  874. case WASM_F32:
  875. param->kind = WASM_F32;
  876. param->of.f32 = *(float32 *)argv_p;
  877. argv_p = (float32 *)argv_p + 1;
  878. argc++;
  879. break;
  880. case WASM_F64:
  881. param->kind = WASM_F64;
  882. param->of.f64 = *(float64 *)argv_p;
  883. argv_p = (float64 *)argv_p + 1;
  884. argc++;
  885. break;
  886. default:
  887. NOT_REACHED();
  888. goto failed;
  889. }
  890. }
  891. return argc;
  892. failed:
  893. return 0;
  894. }
  895. static uint32
  896. results_to_argv(const wasm_val_t *results,
  897. const wasm_valtype_vec_t *result_defs,
  898. uint64 *out)
  899. {
  900. size_t i = 0;
  901. uint32 argc = 0;
  902. void *argv_p = out;
  903. for (i = 0; i < result_defs->num_elems; ++i) {
  904. wasm_valtype_t *result_def = result_defs->data[i];
  905. const wasm_val_t *result = results + i;
  906. switch (result_def->kind) {
  907. case WASM_I32:
  908. *(int32 *)argv_p = result->of.i32;
  909. argv_p = (uint32 *)argv_p + 1;
  910. argc++;
  911. break;
  912. case WASM_I64:
  913. *(int64 *)argv_p = result->of.i64;
  914. argv_p = (uint64 *)argv_p + 1;
  915. argc++;
  916. break;
  917. case WASM_F32:
  918. *(float32 *)argv_p = result->of.f32;
  919. argv_p = (float32 *)argv_p + 1;
  920. argc++;
  921. break;
  922. case WASM_F64:
  923. *(float64 *)argv_p = result->of.f64;
  924. argv_p = (float64 *)argv_p + 1;
  925. argc++;
  926. break;
  927. default:
  928. NOT_REACHED();
  929. goto failed;
  930. }
  931. }
  932. return argc;
  933. failed:
  934. return 0;
  935. }
  936. static void
  937. native_func_trampoline(wasm_exec_env_t exec_env, uint64 *argv)
  938. {
  939. wasm_val_t *params = NULL, *results = NULL;
  940. uint32 argc = 0;
  941. const wasm_func_t *func = NULL;
  942. wasm_trap_t *trap = NULL;
  943. size_t param_count, result_count;
  944. func = wasm_runtime_get_function_attachment(exec_env);
  945. bh_assert(func);
  946. param_count = wasm_func_param_arity(func);
  947. if (param_count) {
  948. if (!argv) {
  949. goto failed;
  950. }
  951. if (!(params = malloc_internal(param_count * sizeof(wasm_val_t)))) {
  952. goto failed;
  953. }
  954. /* argv -> const wasm_val_t params[] */
  955. if (!(argc = argv_to_params(
  956. argv, wasm_functype_params(wasm_func_type(func)), params))) {
  957. goto failed;
  958. }
  959. }
  960. result_count = wasm_func_result_arity(func);
  961. if (result_count) {
  962. if (!argv) {
  963. goto failed;
  964. }
  965. if (!(results = malloc_internal(result_count * sizeof(wasm_val_t)))) {
  966. goto failed;
  967. }
  968. }
  969. if (func->with_env) {
  970. trap = func->u.cb_env.cb(func->u.cb_env.env, params, results);
  971. }
  972. else {
  973. trap = func->u.cb(params, results);
  974. }
  975. if (trap) {
  976. wasm_name_t *message = malloc_internal(sizeof(wasm_name_t));
  977. if (message) {
  978. wasm_trap_message(trap, message);
  979. if (message->data) {
  980. LOG_WARNING("got a trap %s", message->data);
  981. wasm_name_delete(message);
  982. }
  983. FREEIF(message);
  984. }
  985. }
  986. if (argv) {
  987. memset(argv, 0, wasm_func_param_arity(func) * sizeof(uint64));
  988. }
  989. /* there is no trap and there is return values */
  990. if (!trap && result_count) {
  991. /* wasm_val_t results[] -> argv */
  992. if (!(argc = results_to_argv(
  993. results, wasm_functype_results(wasm_func_type(func)), argv))) {
  994. goto failed;
  995. }
  996. }
  997. failed:
  998. FREEIF(params);
  999. FREEIF(results);
  1000. return;
  1001. }
  1002. wasm_func_t *
  1003. wasm_func_new(wasm_store_t *store,
  1004. const wasm_functype_t *func_type,
  1005. wasm_func_callback_t callback)
  1006. {
  1007. wasm_func_t *func = NULL;
  1008. check_engine_and_store(singleton_engine, store);
  1009. bh_assert(func_type);
  1010. func = malloc_internal(sizeof(wasm_func_t));
  1011. if (!func) {
  1012. goto failed;
  1013. }
  1014. func->kind = WASM_EXTERN_FUNC;
  1015. func->with_env = false;
  1016. func->u.cb = callback;
  1017. func->func_type = wasm_functype_copy((wasm_functype_t *)func_type);
  1018. if (!func->func_type) {
  1019. goto failed;
  1020. }
  1021. return func;
  1022. failed:
  1023. LOG_DEBUG("%s failed", __FUNCTION__);
  1024. wasm_func_delete(func);
  1025. return NULL;
  1026. }
  1027. wasm_func_t *
  1028. wasm_func_new_with_env(wasm_store_t *store,
  1029. const wasm_functype_t *func_type,
  1030. wasm_func_callback_with_env_t callback,
  1031. void *env,
  1032. void (*finalizer)(void *))
  1033. {
  1034. wasm_func_t *func = NULL;
  1035. check_engine_and_store(singleton_engine, store);
  1036. bh_assert(func_type);
  1037. func = malloc_internal(sizeof(wasm_func_t));
  1038. if (!func) {
  1039. goto failed;
  1040. }
  1041. func->kind = WASM_EXTERN_FUNC;
  1042. func->with_env = true;
  1043. func->u.cb_env.cb = callback;
  1044. func->u.cb_env.env = env;
  1045. func->u.cb_env.finalizer = finalizer;
  1046. func->func_type = wasm_functype_copy((wasm_functype_t *)func_type);
  1047. if (!func->func_type) {
  1048. goto failed;
  1049. }
  1050. return func;
  1051. failed:
  1052. LOG_DEBUG("%s failed", __FUNCTION__);
  1053. wasm_func_delete(func);
  1054. return NULL;
  1055. }
  1056. static wasm_func_t *
  1057. wasm_func_new_internal(wasm_store_t *store,
  1058. uint16 func_idx_rt,
  1059. WASMModuleInstanceCommon *inst_comm_rt)
  1060. {
  1061. wasm_func_t *func = NULL;
  1062. WASMType *type_rt = NULL;
  1063. check_engine_and_store(singleton_engine, store);
  1064. bh_assert(inst_comm_rt);
  1065. func = malloc_internal(sizeof(wasm_func_t));
  1066. if (!func) {
  1067. goto failed;
  1068. }
  1069. func->kind = WASM_EXTERN_FUNC;
  1070. if (INTERP_MODE == current_runtime_mode()) {
  1071. #if WASM_ENABLE_INTERP != 0
  1072. bh_assert(func_idx_rt
  1073. < ((WASMModuleInstance *)inst_comm_rt)->function_count);
  1074. WASMFunctionInstance *func_interp =
  1075. ((WASMModuleInstance *)inst_comm_rt)->functions + func_idx_rt;
  1076. type_rt = func_interp->is_import_func
  1077. ? func_interp->u.func_import->func_type
  1078. : func_interp->u.func->func_type;
  1079. #endif
  1080. }
  1081. else {
  1082. #if WASM_ENABLE_AOT != 0
  1083. /* use same index to trace the function type in AOTFuncType **func_types */
  1084. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  1085. AOTFunctionInstance *func_aot =
  1086. (AOTFunctionInstance *)inst_aot->export_funcs.ptr + func_idx_rt;
  1087. type_rt = func_aot->is_import_func ? func_aot->u.func_import->func_type
  1088. : func_aot->u.func.func_type;
  1089. #endif
  1090. }
  1091. if (!type_rt) {
  1092. goto failed;
  1093. }
  1094. func->func_type = wasm_functype_new_internal(type_rt);
  1095. if (!func->func_type) {
  1096. goto failed;
  1097. }
  1098. /* will add name information when processing "exports" */
  1099. func->module_name = NULL;
  1100. func->name = NULL;
  1101. func->func_idx_rt = func_idx_rt;
  1102. func->inst_comm_rt = inst_comm_rt;
  1103. return func;
  1104. failed:
  1105. LOG_DEBUG("%s failed", __FUNCTION__);
  1106. wasm_func_delete(func);
  1107. return NULL;
  1108. }
  1109. void
  1110. wasm_func_delete(wasm_func_t *func)
  1111. {
  1112. if (!func) {
  1113. return;
  1114. }
  1115. if (func->func_type) {
  1116. wasm_functype_delete(func->func_type);
  1117. func->func_type = NULL;
  1118. }
  1119. if (func->with_env) {
  1120. if (func->u.cb_env.finalizer) {
  1121. func->u.cb_env.finalizer(func->u.cb_env.env);
  1122. func->u.cb_env.finalizer = NULL;
  1123. func->u.cb_env.env = NULL;
  1124. }
  1125. }
  1126. wasm_runtime_free(func);
  1127. }
  1128. wasm_func_t *
  1129. wasm_func_copy(const wasm_func_t *func)
  1130. {
  1131. wasm_func_t *cloned = NULL;
  1132. bh_assert(func);
  1133. cloned = malloc_internal(sizeof(wasm_func_t));
  1134. if (!cloned) {
  1135. goto failed;
  1136. }
  1137. cloned->kind = func->kind;
  1138. cloned->with_env = func->with_env;
  1139. if (cloned->with_env) {
  1140. cloned->u.cb_env.cb = func->u.cb_env.cb;
  1141. cloned->u.cb_env.env = func->u.cb_env.env;
  1142. cloned->u.cb_env.finalizer = func->u.cb_env.finalizer;
  1143. }
  1144. else {
  1145. cloned->u.cb = func->u.cb;
  1146. }
  1147. cloned->func_idx_rt = func->func_idx_rt;
  1148. cloned->inst_comm_rt = func->inst_comm_rt;
  1149. cloned->func_type = wasm_functype_copy(func->func_type);
  1150. if (!cloned->func_type) {
  1151. goto failed;
  1152. }
  1153. return cloned;
  1154. failed:
  1155. LOG_DEBUG("%s failed", __FUNCTION__);
  1156. wasm_func_delete(cloned);
  1157. return NULL;
  1158. }
  1159. wasm_functype_t *
  1160. wasm_func_type(const wasm_func_t *func)
  1161. {
  1162. bh_assert(func);
  1163. return func->func_type;
  1164. }
  1165. static uint32
  1166. params_to_argv(const wasm_val_t *params,
  1167. const wasm_valtype_vec_t *param_defs,
  1168. size_t param_arity,
  1169. uint32 *out)
  1170. {
  1171. size_t i = 0;
  1172. uint32 argc = 0;
  1173. const wasm_val_t *param = NULL;
  1174. if (!param_arity) {
  1175. return 0;
  1176. }
  1177. bh_assert(params && param_defs && out);
  1178. bh_assert(param_defs->num_elems == param_arity);
  1179. for (i = 0; out && i < param_arity; ++i) {
  1180. param = params + i;
  1181. bh_assert((*(param_defs->data + i))->kind == param->kind);
  1182. switch (param->kind) {
  1183. case WASM_I32:
  1184. *(int32 *)out = param->of.i32;
  1185. out += 1;
  1186. argc += 1;
  1187. break;
  1188. case WASM_I64:
  1189. *(int64 *)out = param->of.i64;
  1190. out += 2;
  1191. argc += 2;
  1192. break;
  1193. case WASM_F32:
  1194. *(float32 *)out = param->of.f32;
  1195. out += 1;
  1196. argc += 1;
  1197. break;
  1198. case WASM_F64:
  1199. *(float64 *)out = param->of.f64;
  1200. out += 2;
  1201. argc += 2;
  1202. break;
  1203. default:
  1204. LOG_DEBUG("unexpected parameter val type %d", param->kind);
  1205. goto failed;
  1206. }
  1207. }
  1208. return argc;
  1209. failed:
  1210. LOG_DEBUG("%s failed", __FUNCTION__);
  1211. return 0;
  1212. }
  1213. static uint32
  1214. argv_to_results(const uint32 *results,
  1215. const wasm_valtype_vec_t *result_defs,
  1216. size_t result_arity,
  1217. wasm_val_t *out)
  1218. {
  1219. size_t i = 0;
  1220. uint32 argc = 0;
  1221. const uint32 *result = results;
  1222. const wasm_valtype_t *def = NULL;
  1223. if (!result_arity) {
  1224. return 0;
  1225. }
  1226. bh_assert(results && result_defs && out);
  1227. bh_assert(result_arity == result_defs->num_elems);
  1228. for (i = 0; out && i < result_arity; i++) {
  1229. def = *(result_defs->data + i);
  1230. switch (def->kind) {
  1231. case WASM_I32:
  1232. out->kind = WASM_I32;
  1233. out->of.i32 = *(int32 *)result;
  1234. result += 1;
  1235. break;
  1236. case WASM_I64:
  1237. out->kind = WASM_I64;
  1238. out->of.i64 = *(int64 *)result;
  1239. result += 2;
  1240. break;
  1241. case WASM_F32:
  1242. out->kind = WASM_F32;
  1243. out->of.f32 = *(float32 *)result;
  1244. result += 1;
  1245. break;
  1246. case WASM_F64:
  1247. out->kind = WASM_F64;
  1248. out->of.f64 = *(float64 *)result;
  1249. result += 2;
  1250. break;
  1251. default:
  1252. LOG_DEBUG("unexpected parameter val type %d", def->kind);
  1253. goto failed;
  1254. }
  1255. out++;
  1256. argc++;
  1257. }
  1258. return argc;
  1259. failed:
  1260. LOG_DEBUG("%s failed", __FUNCTION__);
  1261. return 0;
  1262. }
  1263. wasm_trap_t *
  1264. wasm_func_call(const wasm_func_t *func,
  1265. const wasm_val_t params[],
  1266. wasm_val_t results[])
  1267. {
  1268. /* parameters count as if all are uint32 */
  1269. /* a int64 or float64 parameter means 2 */
  1270. uint32 argc = 0;
  1271. /* a parameter list and a return value list */
  1272. uint32 *argv = NULL;
  1273. WASMFunctionInstanceCommon *func_comm_rt = NULL;
  1274. size_t param_count, result_count, alloc_count;
  1275. bh_assert(func && func->func_type && func->inst_comm_rt);
  1276. if (INTERP_MODE == current_runtime_mode()) {
  1277. #if WASM_ENABLE_INTERP != 0
  1278. func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->functions
  1279. + func->func_idx_rt;
  1280. #endif
  1281. }
  1282. else {
  1283. #if WASM_ENABLE_AOT != 0
  1284. AOTModuleInstance *inst_aot = (AOTModuleInstance *)func->inst_comm_rt;
  1285. func_comm_rt = (AOTFunctionInstance *)inst_aot->export_funcs.ptr
  1286. + func->func_idx_rt;
  1287. #endif
  1288. }
  1289. if (!func_comm_rt) {
  1290. goto failed;
  1291. }
  1292. param_count = wasm_func_param_arity(func);
  1293. result_count = wasm_func_result_arity(func);
  1294. alloc_count = (param_count > result_count) ? param_count : result_count;
  1295. if (alloc_count) {
  1296. if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
  1297. goto failed;
  1298. }
  1299. }
  1300. /* copy parametes */
  1301. if (param_count
  1302. && !(argc = params_to_argv(params,
  1303. wasm_functype_params(wasm_func_type(func)),
  1304. wasm_func_param_arity(func), argv))) {
  1305. goto failed;
  1306. }
  1307. if (!wasm_runtime_create_exec_env_and_call_wasm(
  1308. func->inst_comm_rt, func_comm_rt, argc, argv)) {
  1309. LOG_DEBUG("wasm_runtime_create_exec_env_and_call_wasm failed");
  1310. goto failed;
  1311. }
  1312. /* copy results */
  1313. if (result_count
  1314. && !(argc = argv_to_results(
  1315. argv, wasm_functype_results(wasm_func_type(func)),
  1316. wasm_func_result_arity(func), results))) {
  1317. goto failed;
  1318. }
  1319. FREEIF(argv);
  1320. /* should return trap */
  1321. return NULL;
  1322. failed:
  1323. LOG_DEBUG("%s failed", __FUNCTION__);
  1324. FREEIF(argv);
  1325. return NULL;
  1326. }
  1327. size_t
  1328. wasm_func_param_arity(const wasm_func_t *func)
  1329. {
  1330. bh_assert(func && func->func_type && func->func_type->params);
  1331. return func->func_type->params->num_elems;
  1332. }
  1333. size_t
  1334. wasm_func_result_arity(const wasm_func_t *func)
  1335. {
  1336. bh_assert(func && func->func_type && func->func_type->results);
  1337. return func->func_type->results->num_elems;
  1338. }
  1339. wasm_extern_t *
  1340. wasm_func_as_extern(wasm_func_t *func)
  1341. {
  1342. return (wasm_extern_t *)func;
  1343. }
  1344. wasm_global_t *
  1345. wasm_global_new(wasm_store_t *store,
  1346. const wasm_globaltype_t *global_type,
  1347. const wasm_val_t *init)
  1348. {
  1349. wasm_global_t *global = NULL;
  1350. check_engine_and_store(singleton_engine, store);
  1351. bh_assert(store && global_type && init);
  1352. global = malloc_internal(sizeof(wasm_global_t));
  1353. if (!global) {
  1354. goto failed;
  1355. }
  1356. global->kind = WASM_EXTERN_GLOBAL;
  1357. global->type = wasm_globaltype_copy((wasm_globaltype_t *)global_type);
  1358. if (!global->type) {
  1359. goto failed;
  1360. }
  1361. global->init = malloc_internal(sizeof(wasm_val_t));
  1362. if (!global->init) {
  1363. goto failed;
  1364. }
  1365. wasm_val_copy(global->init, init);
  1366. /* TODO: how to check if above is failed */
  1367. return global;
  1368. failed:
  1369. LOG_DEBUG("%s failed", __FUNCTION__);
  1370. wasm_global_delete(global);
  1371. return NULL;
  1372. }
  1373. /* almost same with wasm_global_new */
  1374. wasm_global_t *
  1375. wasm_global_copy(const wasm_global_t *src)
  1376. {
  1377. wasm_global_t *global = NULL;
  1378. bh_assert(src);
  1379. global = malloc_internal(sizeof(wasm_global_t));
  1380. if (!global) {
  1381. goto failed;
  1382. }
  1383. global->kind = WASM_EXTERN_GLOBAL;
  1384. global->type = wasm_globaltype_copy((wasm_globaltype_t *)src->type);
  1385. if (!global->type) {
  1386. goto failed;
  1387. }
  1388. global->init = malloc_internal(sizeof(wasm_val_t));
  1389. if (!global->init) {
  1390. goto failed;
  1391. }
  1392. wasm_val_copy(global->init, src->init);
  1393. global->global_idx_rt = src->global_idx_rt;
  1394. global->inst_comm_rt = src->inst_comm_rt;
  1395. return global;
  1396. failed:
  1397. LOG_DEBUG("%s failed", __FUNCTION__);
  1398. wasm_global_delete(global);
  1399. return NULL;
  1400. }
  1401. void
  1402. wasm_global_delete(wasm_global_t *global)
  1403. {
  1404. if (!global) {
  1405. return;
  1406. }
  1407. if (global->init) {
  1408. wasm_val_delete(global->init);
  1409. wasm_runtime_free(global->init);
  1410. global->init = NULL;
  1411. }
  1412. if (global->type) {
  1413. wasm_globaltype_delete(global->type);
  1414. global->type = NULL;
  1415. }
  1416. wasm_runtime_free(global);
  1417. }
  1418. bool
  1419. wasm_global_same(const wasm_global_t *g1, const wasm_global_t *g2)
  1420. {
  1421. if (!g1 && !g2) {
  1422. return true;
  1423. }
  1424. if (!g1 || !g2) {
  1425. return false;
  1426. }
  1427. return g1->kind == g2->kind && wasm_globaltype_same(g1->type, g2->type)
  1428. && wasm_val_same(g1->init, g2->init);
  1429. }
  1430. #if WASM_ENABLE_INTERP != 0
  1431. static bool
  1432. interp_global_set(const WASMModuleInstance *inst_interp,
  1433. uint16 global_idx_rt,
  1434. const wasm_val_t *v)
  1435. {
  1436. const WASMGlobalInstance *global_interp =
  1437. inst_interp->globals + global_idx_rt;
  1438. uint8 val_type_rt = global_interp->type;
  1439. #if WASM_ENABLE_MULTI_MODULE != 0
  1440. uint8 *data = global_interp->import_global_inst
  1441. ? global_interp->import_module_inst->global_data
  1442. + global_interp->import_global_inst->data_offset
  1443. : inst_interp->global_data + global_interp->data_offset;
  1444. #else
  1445. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  1446. #endif
  1447. bool ret = true;
  1448. switch (val_type_rt) {
  1449. case VALUE_TYPE_I32:
  1450. bh_assert(WASM_I32 == v->kind);
  1451. *((int32 *)data) = v->of.i32;
  1452. break;
  1453. case VALUE_TYPE_F32:
  1454. bh_assert(WASM_F32 == v->kind);
  1455. *((float32 *)data) = v->of.f32;
  1456. break;
  1457. case VALUE_TYPE_I64:
  1458. bh_assert(WASM_I64 == v->kind);
  1459. *((int64 *)data) = v->of.i64;
  1460. break;
  1461. case VALUE_TYPE_F64:
  1462. bh_assert(WASM_F64 == v->kind);
  1463. *((float64 *)data) = v->of.f64;
  1464. break;
  1465. default:
  1466. LOG_DEBUG("unexpected value type %d", val_type_rt);
  1467. ret = false;
  1468. break;
  1469. }
  1470. return ret;
  1471. }
  1472. static bool
  1473. interp_global_get(const WASMModuleInstance *inst_interp,
  1474. uint16 global_idx_rt,
  1475. wasm_val_t *out)
  1476. {
  1477. WASMGlobalInstance *global_interp = inst_interp->globals + global_idx_rt;
  1478. uint8 val_type_rt = global_interp->type;
  1479. #if WASM_ENABLE_MULTI_MODULE != 0
  1480. uint8 *data = global_interp->import_global_inst
  1481. ? global_interp->import_module_inst->global_data
  1482. + global_interp->import_global_inst->data_offset
  1483. : inst_interp->global_data + global_interp->data_offset;
  1484. #else
  1485. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  1486. #endif
  1487. bool ret = true;
  1488. switch (val_type_rt) {
  1489. case VALUE_TYPE_I32:
  1490. out->kind = WASM_I32;
  1491. out->of.i32 = *((int32 *)data);
  1492. break;
  1493. case VALUE_TYPE_F32:
  1494. out->kind = WASM_F32;
  1495. out->of.f32 = *((float32 *)data);
  1496. break;
  1497. case VALUE_TYPE_I64:
  1498. out->kind = WASM_I64;
  1499. out->of.i64 = *((int64 *)data);
  1500. break;
  1501. case VALUE_TYPE_F64:
  1502. out->kind = WASM_F64;
  1503. out->of.f64 = *((float64 *)data);
  1504. break;
  1505. default:
  1506. LOG_DEBUG("unexpected value type %d", val_type_rt);
  1507. ret = false;
  1508. }
  1509. return ret;
  1510. }
  1511. #endif
  1512. #if WASM_ENABLE_AOT != 0
  1513. static bool
  1514. aot_global_set(const AOTModuleInstance *inst_aot,
  1515. uint16 global_idx_rt,
  1516. const wasm_val_t *v)
  1517. {
  1518. AOTModule *module_aot = inst_aot->aot_module.ptr;
  1519. uint8 val_type_rt = 0;
  1520. uint32 data_offset = 0;
  1521. void *data = NULL;
  1522. bool ret = true;
  1523. if (global_idx_rt < module_aot->import_global_count) {
  1524. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  1525. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  1526. }
  1527. else {
  1528. data_offset =
  1529. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  1530. .data_offset;
  1531. val_type_rt =
  1532. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  1533. .type;
  1534. }
  1535. data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
  1536. switch (val_type_rt) {
  1537. case VALUE_TYPE_I32:
  1538. bh_assert(WASM_I32 == v->kind);
  1539. *((int32 *)data) = v->of.i32;
  1540. break;
  1541. case VALUE_TYPE_F32:
  1542. bh_assert(WASM_F32 == v->kind);
  1543. *((float32 *)data) = v->of.f32;
  1544. break;
  1545. case VALUE_TYPE_I64:
  1546. bh_assert(WASM_I64 == v->kind);
  1547. *((int64 *)data) = v->of.i64;
  1548. break;
  1549. case VALUE_TYPE_F64:
  1550. bh_assert(WASM_F64 == v->kind);
  1551. *((float64 *)data) = v->of.f64;
  1552. break;
  1553. default:
  1554. LOG_DEBUG("unexpected value type %d", val_type_rt);
  1555. ret = false;
  1556. }
  1557. return ret;
  1558. }
  1559. static bool
  1560. aot_global_get(const AOTModuleInstance *inst_aot,
  1561. uint16 global_idx_rt,
  1562. wasm_val_t *out)
  1563. {
  1564. AOTModule *module_aot = inst_aot->aot_module.ptr;
  1565. uint8 val_type_rt = 0;
  1566. uint32 data_offset = 0;
  1567. void *data = NULL;
  1568. bool ret = true;
  1569. if (global_idx_rt < module_aot->import_global_count) {
  1570. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  1571. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  1572. }
  1573. else {
  1574. data_offset =
  1575. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  1576. .data_offset;
  1577. val_type_rt =
  1578. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  1579. .type;
  1580. }
  1581. data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
  1582. switch (val_type_rt) {
  1583. case VALUE_TYPE_I32:
  1584. out->kind = WASM_I32;
  1585. out->of.i32 = *((int32 *)data);
  1586. break;
  1587. case VALUE_TYPE_F32:
  1588. out->kind = WASM_F32;
  1589. out->of.f32 = *((float32 *)data);
  1590. break;
  1591. case VALUE_TYPE_I64:
  1592. out->kind = WASM_I64;
  1593. out->of.i64 = *((int64 *)data);
  1594. break;
  1595. case VALUE_TYPE_F64:
  1596. out->kind = WASM_F64;
  1597. out->of.f64 = *((float64 *)data);
  1598. break;
  1599. default:
  1600. LOG_DEBUG("unexpected value type %d", val_type_rt);
  1601. ret = false;
  1602. }
  1603. return ret;
  1604. }
  1605. #endif
  1606. void
  1607. wasm_global_set(wasm_global_t *global, const wasm_val_t *v)
  1608. {
  1609. bh_assert(global && v);
  1610. if (INTERP_MODE == current_runtime_mode()) {
  1611. #if WASM_ENABLE_INTERP != 0
  1612. (void)interp_global_set((WASMModuleInstance *)global->inst_comm_rt,
  1613. global->global_idx_rt, v);
  1614. #endif
  1615. }
  1616. else {
  1617. #if WASM_ENABLE_AOT != 0
  1618. (void)aot_global_set((AOTModuleInstance *)global->inst_comm_rt,
  1619. global->global_idx_rt, v);
  1620. #endif
  1621. }
  1622. }
  1623. void
  1624. wasm_global_get(const wasm_global_t *global, wasm_val_t *out)
  1625. {
  1626. bh_assert(global && out);
  1627. memset(out, 0, sizeof(wasm_val_t));
  1628. if (INTERP_MODE == current_runtime_mode()) {
  1629. #if WASM_ENABLE_INTERP != 0
  1630. (void)interp_global_get((WASMModuleInstance *)global->inst_comm_rt,
  1631. global->global_idx_rt, out);
  1632. #endif
  1633. }
  1634. else {
  1635. #if WASM_ENABLE_AOT != 0
  1636. (void)aot_global_get((AOTModuleInstance *)global->inst_comm_rt,
  1637. global->global_idx_rt, out);
  1638. #endif
  1639. }
  1640. bh_assert(global->init->kind == out->kind);
  1641. }
  1642. static wasm_global_t *
  1643. wasm_global_new_internal(wasm_store_t *store,
  1644. uint16 global_idx_rt,
  1645. WASMModuleInstanceCommon *inst_comm_rt)
  1646. {
  1647. wasm_global_t *global = NULL;
  1648. uint8 val_type_rt = 0;
  1649. bool is_mutable = 0;
  1650. check_engine_and_store(singleton_engine, store);
  1651. bh_assert(inst_comm_rt);
  1652. global = malloc_internal(sizeof(wasm_global_t));
  1653. if (!global) {
  1654. goto failed;
  1655. }
  1656. /*
  1657. * global->module_name = NULL;
  1658. * global->name = NULL;
  1659. */
  1660. global->kind = WASM_EXTERN_GLOBAL;
  1661. if (INTERP_MODE == current_runtime_mode()) {
  1662. #if WASM_ENABLE_INTERP != 0
  1663. WASMGlobalInstance *global_interp =
  1664. ((WASMModuleInstance *)inst_comm_rt)->globals + global_idx_rt;
  1665. val_type_rt = global_interp->type;
  1666. is_mutable = global_interp->is_mutable;
  1667. #endif
  1668. }
  1669. else {
  1670. #if WASM_ENABLE_AOT != 0
  1671. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  1672. AOTModule *module_aot = inst_aot->aot_module.ptr;
  1673. if (global_idx_rt < module_aot->import_global_count) {
  1674. AOTImportGlobal *global_import_aot =
  1675. module_aot->import_globals + global_idx_rt;
  1676. val_type_rt = global_import_aot->type;
  1677. is_mutable = global_import_aot->is_mutable;
  1678. }
  1679. else {
  1680. AOTGlobal *global_aot =
  1681. module_aot->globals
  1682. + (global_idx_rt - module_aot->import_global_count);
  1683. val_type_rt = global_aot->type;
  1684. is_mutable = global_aot->is_mutable;
  1685. }
  1686. #endif
  1687. }
  1688. global->type = wasm_globaltype_new_internal(val_type_rt, is_mutable);
  1689. if (!global->type) {
  1690. goto failed;
  1691. }
  1692. global->init = malloc_internal(sizeof(wasm_val_t));
  1693. if (!global->init) {
  1694. goto failed;
  1695. }
  1696. if (INTERP_MODE == current_runtime_mode()) {
  1697. #if WASM_ENABLE_INTERP != 0
  1698. interp_global_get((WASMModuleInstance *)inst_comm_rt, global_idx_rt,
  1699. global->init);
  1700. #endif
  1701. }
  1702. else {
  1703. #if WASM_ENABLE_AOT != 0
  1704. aot_global_get((AOTModuleInstance *)inst_comm_rt, global_idx_rt,
  1705. global->init);
  1706. #endif
  1707. }
  1708. global->inst_comm_rt = inst_comm_rt;
  1709. global->global_idx_rt = global_idx_rt;
  1710. return global;
  1711. failed:
  1712. LOG_DEBUG("%s failed", __FUNCTION__);
  1713. wasm_global_delete(global);
  1714. return NULL;
  1715. }
  1716. wasm_globaltype_t *
  1717. wasm_global_type(const wasm_global_t *global)
  1718. {
  1719. bh_assert(global);
  1720. return global->type;
  1721. }
  1722. wasm_extern_t *
  1723. wasm_global_as_extern(wasm_global_t *global)
  1724. {
  1725. return (wasm_extern_t *)global;
  1726. }
  1727. void
  1728. wasm_table_delete(wasm_table_t *table)
  1729. {}
  1730. void
  1731. wasm_memory_delete(wasm_memory_t *memory)
  1732. {}
  1733. #if WASM_ENABLE_INTERP != 0
  1734. static bool
  1735. interp_link_func(const wasm_instance_t *inst,
  1736. const WASMModule *module_interp,
  1737. uint16 func_idx_rt,
  1738. wasm_func_t *import)
  1739. {
  1740. WASMImport *imported_func_interp = NULL;
  1741. wasm_func_t *cloned = NULL;
  1742. bh_assert(inst && module_interp && import);
  1743. bh_assert(func_idx_rt < module_interp->import_function_count);
  1744. bh_assert(WASM_EXTERN_FUNC == import->kind);
  1745. imported_func_interp = module_interp->import_functions + func_idx_rt;
  1746. bh_assert(imported_func_interp);
  1747. cloned = wasm_func_copy(import);
  1748. if (!cloned || !bh_vector_append((Vector *)inst->imports, &cloned)) {
  1749. return false;
  1750. }
  1751. /* add native_func_trampoline as a NativeSymbol */
  1752. imported_func_interp->u.function.call_conv_raw = true;
  1753. imported_func_interp->u.function.attachment = cloned;
  1754. imported_func_interp->u.function.func_ptr_linked = native_func_trampoline;
  1755. import->func_idx_rt = func_idx_rt;
  1756. return true;
  1757. }
  1758. static bool
  1759. interp_link_global(const WASMModule *module_interp,
  1760. uint16 global_idx_rt,
  1761. wasm_global_t *import)
  1762. {
  1763. WASMImport *imported_global_interp = NULL;
  1764. bh_assert(module_interp && import);
  1765. bh_assert(global_idx_rt < module_interp->import_global_count);
  1766. bh_assert(WASM_EXTERN_GLOBAL == import->kind);
  1767. imported_global_interp = module_interp->import_globals + global_idx_rt;
  1768. bh_assert(imported_global_interp);
  1769. /* set init value */
  1770. switch (wasm_valtype_kind(import->type->val_type)) {
  1771. case WASM_I32:
  1772. bh_assert(VALUE_TYPE_I32 == imported_global_interp->u.global.type);
  1773. imported_global_interp->u.global.global_data_linked.i32 =
  1774. import->init->of.i32;
  1775. break;
  1776. case WASM_I64:
  1777. bh_assert(VALUE_TYPE_I64 == imported_global_interp->u.global.type);
  1778. imported_global_interp->u.global.global_data_linked.i64 =
  1779. import->init->of.i64;
  1780. break;
  1781. case WASM_F32:
  1782. bh_assert(VALUE_TYPE_F32 == imported_global_interp->u.global.type);
  1783. imported_global_interp->u.global.global_data_linked.f32 =
  1784. import->init->of.f32;
  1785. break;
  1786. case WASM_F64:
  1787. bh_assert(VALUE_TYPE_F64 == imported_global_interp->u.global.type);
  1788. imported_global_interp->u.global.global_data_linked.f64 =
  1789. import->init->of.f64;
  1790. break;
  1791. default:
  1792. return false;
  1793. }
  1794. import->global_idx_rt = global_idx_rt;
  1795. imported_global_interp->u.global.is_linked = true;
  1796. return true;
  1797. }
  1798. static bool
  1799. interp_link_memory(const WASMModule *module_interp,
  1800. uint16 memory_inst_index,
  1801. wasm_memory_t *import)
  1802. {
  1803. return false;
  1804. }
  1805. static bool
  1806. interp_link_table(const WASMModule *module_interp,
  1807. uint16 table_inst_index,
  1808. wasm_table_t *import)
  1809. {
  1810. return false;
  1811. }
  1812. static uint32
  1813. interp_link(const wasm_instance_t *inst,
  1814. const WASMModule *module_interp,
  1815. wasm_extern_t *imports[])
  1816. {
  1817. uint32 i = 0;
  1818. uint32 import_func_i = 0;
  1819. uint32 import_global_i = 0;
  1820. uint32 import_table_i = 0;
  1821. uint32 import_memory_i = 0;
  1822. wasm_func_t *func = NULL;
  1823. wasm_global_t *global = NULL;
  1824. bh_assert(inst && module_interp && imports);
  1825. for (i = 0; i < module_interp->import_count; ++i) {
  1826. wasm_extern_t *import = imports[i];
  1827. WASMImport *import_rt = module_interp->imports + i;
  1828. switch (import_rt->kind) {
  1829. case IMPORT_KIND_FUNC:
  1830. func = wasm_extern_as_func(import);
  1831. if (!interp_link_func(inst, module_interp, import_func_i++,
  1832. func)) {
  1833. goto failed;
  1834. }
  1835. break;
  1836. case IMPORT_KIND_GLOBAL:
  1837. global = wasm_extern_as_global(import);
  1838. if (!interp_link_global(module_interp, import_global_i++,
  1839. global)) {
  1840. goto failed;
  1841. }
  1842. break;
  1843. case IMPORT_KIND_MEMORY:
  1844. if (!interp_link_memory(module_interp, import_memory_i++,
  1845. wasm_extern_as_memory(import))) {
  1846. goto failed;
  1847. };
  1848. break;
  1849. case IMPORT_KIND_TABLE:
  1850. if (!interp_link_table(module_interp, import_table_i++,
  1851. wasm_extern_as_table(import))) {
  1852. goto failed;
  1853. }
  1854. break;
  1855. default:
  1856. NOT_REACHED();
  1857. break;
  1858. }
  1859. }
  1860. return module_interp->import_count;
  1861. failed:
  1862. LOG_DEBUG("%s failed", __FUNCTION__);
  1863. return (uint32)-1;
  1864. }
  1865. static bool
  1866. interp_process_export(wasm_store_t *store,
  1867. const WASMModuleInstance *inst_interp,
  1868. wasm_extern_vec_t *externals)
  1869. {
  1870. WASMExport *exports = NULL;
  1871. WASMExport *export = NULL;
  1872. wasm_func_t *func = NULL;
  1873. wasm_global_t *global = NULL;
  1874. wasm_extern_t *external = NULL;
  1875. uint32 export_cnt = 0;
  1876. uint32 i = 0;
  1877. bh_assert(store && inst_interp && inst_interp->module && externals);
  1878. exports = inst_interp->module->exports;
  1879. export_cnt = inst_interp->module->export_count;
  1880. for (i = 0; i < export_cnt; ++i) {
  1881. export = exports + i;
  1882. switch (export->kind) {
  1883. case EXPORT_KIND_FUNC:
  1884. func = wasm_func_new_internal(
  1885. store, export->index,
  1886. (WASMModuleInstanceCommon *)inst_interp);
  1887. if (!func) {
  1888. goto failed;
  1889. }
  1890. external = wasm_func_as_extern(func);
  1891. break;
  1892. case EXPORT_KIND_GLOBAL:
  1893. global = wasm_global_new_internal(
  1894. store, export->index,
  1895. (WASMModuleInstanceCommon *)inst_interp);
  1896. if (!global) {
  1897. goto failed;
  1898. }
  1899. external = wasm_global_as_extern(global);
  1900. break;
  1901. case EXPORT_KIND_MEMORY:
  1902. case EXPORT_KIND_TABLE:
  1903. /* TODO: */
  1904. break;
  1905. default:
  1906. goto failed;
  1907. }
  1908. if (!bh_vector_append((Vector *)externals, &external)) {
  1909. goto failed;
  1910. }
  1911. }
  1912. return true;
  1913. failed:
  1914. LOG_DEBUG("%s failed", __FUNCTION__);
  1915. return false;
  1916. }
  1917. #endif /* WASM_ENABLE_INTERP */
  1918. #if WASM_ENABLE_AOT != 0
  1919. static bool
  1920. aot_link_func(const wasm_instance_t *inst,
  1921. const AOTModule *module_aot,
  1922. uint32 import_func_idx_rt,
  1923. wasm_func_t *import)
  1924. {
  1925. AOTImportFunc *import_aot_func = NULL;
  1926. wasm_func_t *cloned = NULL;
  1927. bh_assert(inst && module_aot && import);
  1928. import_aot_func = module_aot->import_funcs + import_func_idx_rt;
  1929. bh_assert(import_aot_func);
  1930. cloned = wasm_func_copy(import);
  1931. if (!cloned || !bh_vector_append((Vector *)inst->imports, &cloned)) {
  1932. return false;
  1933. }
  1934. import_aot_func->call_conv_raw = true;
  1935. import_aot_func->attachment = wasm_func_copy(import);
  1936. import_aot_func->func_ptr_linked = native_func_trampoline;
  1937. import->func_idx_rt = import_func_idx_rt;
  1938. return true;
  1939. }
  1940. static bool
  1941. aot_link_global(const AOTModule *module_aot,
  1942. uint16 global_idx_rt,
  1943. wasm_global_t *import)
  1944. {
  1945. AOTImportGlobal *import_aot_global = NULL;
  1946. const wasm_valtype_t *val_type = NULL;
  1947. bh_assert(module_aot && import);
  1948. import_aot_global = module_aot->import_globals + global_idx_rt;
  1949. bh_assert(import_aot_global);
  1950. val_type = wasm_globaltype_content(wasm_global_type(import));
  1951. bh_assert(val_type);
  1952. switch (wasm_valtype_kind(val_type)) {
  1953. case WASM_I32:
  1954. bh_assert(VALUE_TYPE_I32 == import_aot_global->type);
  1955. import_aot_global->global_data_linked.i32 = import->init->of.i32;
  1956. break;
  1957. case WASM_I64:
  1958. bh_assert(VALUE_TYPE_I64 == import_aot_global->type);
  1959. import_aot_global->global_data_linked.i64 = import->init->of.i64;
  1960. break;
  1961. case WASM_F32:
  1962. bh_assert(VALUE_TYPE_F32 == import_aot_global->type);
  1963. import_aot_global->global_data_linked.f32 = import->init->of.f32;
  1964. break;
  1965. case WASM_F64:
  1966. bh_assert(VALUE_TYPE_F64 == import_aot_global->type);
  1967. import_aot_global->global_data_linked.f64 = import->init->of.f64;
  1968. break;
  1969. default:
  1970. goto failed;
  1971. }
  1972. import->global_idx_rt = global_idx_rt;
  1973. return true;
  1974. failed:
  1975. LOG_DEBUG("%s failed", __FUNCTION__);
  1976. return false;
  1977. }
  1978. static uint32
  1979. aot_link(const wasm_instance_t *inst,
  1980. const AOTModule *module_aot,
  1981. wasm_extern_t *imports[])
  1982. {
  1983. uint32 i = 0;
  1984. uint32 import_func_i = 0;
  1985. uint32 import_global_i = 0;
  1986. wasm_extern_t *import = NULL;
  1987. wasm_func_t *func = NULL;
  1988. wasm_global_t *global = NULL;
  1989. bh_assert(inst && module_aot && imports);
  1990. while (import_func_i < module_aot->import_func_count
  1991. || import_global_i < module_aot->import_global_count) {
  1992. import = imports[i++];
  1993. bh_assert(import);
  1994. switch (wasm_extern_kind(import)) {
  1995. case WASM_EXTERN_FUNC:
  1996. bh_assert(import_func_i < module_aot->import_func_count);
  1997. func = wasm_extern_as_func((wasm_extern_t *)import);
  1998. if (!aot_link_func(inst, module_aot, import_func_i++, func)) {
  1999. goto failed;
  2000. }
  2001. break;
  2002. case WASM_EXTERN_GLOBAL:
  2003. bh_assert(import_global_i < module_aot->import_global_count);
  2004. global = wasm_extern_as_global((wasm_extern_t *)import);
  2005. if (!aot_link_global(module_aot, import_global_i++, global)) {
  2006. goto failed;
  2007. }
  2008. break;
  2009. case WASM_EXTERN_MEMORY:
  2010. break;
  2011. case WASM_EXTERN_TABLE:
  2012. break;
  2013. default:
  2014. goto failed;
  2015. }
  2016. }
  2017. return i;
  2018. failed:
  2019. LOG_DEBUG("%s failed", __FUNCTION__);
  2020. return (uint32)-1;
  2021. }
  2022. static bool
  2023. aot_process_export(wasm_store_t *store,
  2024. const AOTModuleInstance *inst_aot,
  2025. wasm_extern_vec_t *externals)
  2026. {
  2027. uint32 i = 0;
  2028. uint32 export_func_i = 0;
  2029. wasm_extern_t *external = NULL;
  2030. AOTModule *module_aot = NULL;
  2031. bh_assert(store && inst_aot && externals);
  2032. module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2033. bh_assert(module_aot);
  2034. for (i = 0; i < module_aot->export_count; ++i) {
  2035. AOTExport *export = module_aot->exports + i;
  2036. wasm_func_t *func = NULL;
  2037. wasm_global_t *global = NULL;
  2038. switch (export->kind) {
  2039. case EXPORT_KIND_FUNC:
  2040. func =
  2041. wasm_func_new_internal(store, export_func_i++,
  2042. (WASMModuleInstanceCommon *)inst_aot);
  2043. if (!func) {
  2044. goto failed;
  2045. }
  2046. external = wasm_func_as_extern(func);
  2047. break;
  2048. case EXPORT_KIND_GLOBAL:
  2049. global = wasm_global_new_internal(
  2050. store, export->index, (WASMModuleInstanceCommon *)inst_aot);
  2051. if (!global) {
  2052. goto failed;
  2053. }
  2054. external = wasm_global_as_extern(global);
  2055. break;
  2056. case EXPORT_KIND_MEMORY:
  2057. case EXPORT_KIND_TABLE:
  2058. break;
  2059. default:
  2060. NOT_REACHED();
  2061. goto failed;
  2062. }
  2063. if (!bh_vector_append((Vector *)externals, &external)) {
  2064. goto failed;
  2065. }
  2066. }
  2067. return true;
  2068. failed:
  2069. LOG_DEBUG("%s failed", __FUNCTION__);
  2070. return false;
  2071. }
  2072. #endif /* WASM_ENABLE_AOT */
  2073. wasm_instance_t *
  2074. wasm_instance_new(wasm_store_t *store,
  2075. const wasm_module_t *module,
  2076. const wasm_extern_t *const imports[],
  2077. wasm_trap_t **traps)
  2078. {
  2079. char error[128] = { 0 };
  2080. const uint32 stack_size = 16 * 1024;
  2081. const uint32 heap_size = 16 * 1024;
  2082. uint32 import_count = 0;
  2083. wasm_instance_t *instance = NULL;
  2084. uint32 i = 0;
  2085. (void)traps;
  2086. check_engine_and_store(singleton_engine, store);
  2087. instance = malloc_internal(sizeof(wasm_instance_t));
  2088. if (!instance) {
  2089. goto failed;
  2090. }
  2091. /* link module and imports */
  2092. if (imports) {
  2093. if (INTERP_MODE == current_runtime_mode()) {
  2094. #if WASM_ENABLE_INTERP != 0
  2095. import_count = ((WASMModule *)*module)->import_count;
  2096. INIT_VEC(instance->imports, wasm_extern_vec, import_count);
  2097. if (!instance->imports) {
  2098. goto failed;
  2099. }
  2100. if (import_count) {
  2101. import_count = interp_link(instance, (WASMModule *)*module,
  2102. (wasm_extern_t **)imports);
  2103. if ((int32)import_count < 0) {
  2104. goto failed;
  2105. }
  2106. }
  2107. #endif
  2108. }
  2109. else {
  2110. #if WASM_ENABLE_AOT != 0
  2111. import_count = ((AOTModule *)*module)->import_func_count
  2112. + ((AOTModule *)*module)->import_global_count
  2113. + ((AOTModule *)*module)->import_memory_count
  2114. + ((AOTModule *)*module)->import_table_count;
  2115. INIT_VEC(instance->imports, wasm_extern_vec, import_count);
  2116. if (!instance->imports) {
  2117. goto failed;
  2118. }
  2119. if (import_count) {
  2120. import_count = aot_link(instance, (AOTModule *)*module,
  2121. (wasm_extern_t **)imports);
  2122. if ((int32)import_count < 0) {
  2123. goto failed;
  2124. }
  2125. }
  2126. #endif
  2127. }
  2128. }
  2129. instance->inst_comm_rt = wasm_runtime_instantiate(
  2130. *module, stack_size, heap_size, error, sizeof(error));
  2131. if (!instance->inst_comm_rt) {
  2132. LOG_ERROR(error);
  2133. goto failed;
  2134. }
  2135. /* fill in inst */
  2136. for (i = 0; i < (uint32)import_count; ++i) {
  2137. wasm_extern_t *import = (wasm_extern_t *)imports[i];
  2138. switch (import->kind) {
  2139. case WASM_EXTERN_FUNC:
  2140. wasm_extern_as_func(import)->inst_comm_rt =
  2141. instance->inst_comm_rt;
  2142. break;
  2143. case WASM_EXTERN_GLOBAL:
  2144. wasm_extern_as_global(import)->inst_comm_rt =
  2145. instance->inst_comm_rt;
  2146. break;
  2147. case WASM_EXTERN_MEMORY:
  2148. wasm_extern_as_memory(import)->inst_comm_rt =
  2149. instance->inst_comm_rt;
  2150. break;
  2151. case WASM_EXTERN_TABLE:
  2152. wasm_extern_as_table(import)->inst_comm_rt =
  2153. instance->inst_comm_rt;
  2154. break;
  2155. default:
  2156. goto failed;
  2157. }
  2158. }
  2159. /* build the exports list */
  2160. if (INTERP_MODE == current_runtime_mode()) {
  2161. #if WASM_ENABLE_INTERP != 0
  2162. uint32 export_cnt =
  2163. ((WASMModuleInstance *)instance->inst_comm_rt)->module->export_count;
  2164. INIT_VEC(instance->exports, wasm_extern_vec, export_cnt);
  2165. if (!interp_process_export(
  2166. store, (WASMModuleInstance *)instance->inst_comm_rt,
  2167. instance->exports)) {
  2168. goto failed;
  2169. }
  2170. #endif
  2171. }
  2172. else {
  2173. #if WASM_ENABLE_AOT != 0
  2174. uint32 export_cnt =
  2175. ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count;
  2176. INIT_VEC(instance->exports, wasm_extern_vec, export_cnt);
  2177. if (!aot_process_export(store,
  2178. (AOTModuleInstance *)instance->inst_comm_rt,
  2179. instance->exports)) {
  2180. goto failed;
  2181. }
  2182. #endif
  2183. }
  2184. /* add it to a watching list in store */
  2185. if (!bh_vector_append((Vector *)store->instances, &instance)) {
  2186. goto failed;
  2187. }
  2188. return instance;
  2189. failed:
  2190. LOG_DEBUG("%s failed", __FUNCTION__);
  2191. wasm_instance_delete_internal(instance);
  2192. return NULL;
  2193. }
  2194. void
  2195. wasm_instance_delete_internal(wasm_instance_t *instance)
  2196. {
  2197. if (!instance) {
  2198. return;
  2199. }
  2200. DEINIT_VEC(instance->imports, wasm_extern_vec_delete);
  2201. DEINIT_VEC(instance->exports, wasm_extern_vec_delete);
  2202. if (instance->inst_comm_rt) {
  2203. wasm_runtime_deinstantiate(instance->inst_comm_rt);
  2204. instance->inst_comm_rt = NULL;
  2205. }
  2206. wasm_runtime_free(instance);
  2207. }
  2208. /* will release instance when releasing the store */
  2209. void
  2210. wasm_instance_delete(wasm_instance_t *module)
  2211. {
  2212. /* pass */
  2213. }
  2214. void
  2215. wasm_instance_exports(const wasm_instance_t *instance, wasm_extern_vec_t *out)
  2216. {
  2217. bh_assert(instance && out);
  2218. wasm_extern_vec_copy(out, instance->exports);
  2219. }
  2220. void
  2221. wasm_instance_vec_new_uninitialized(wasm_instance_vec_t *out, size_t size)
  2222. {
  2223. generic_vec_init_data((Vector *)out, size, sizeof(wasm_instance_t *));
  2224. }
  2225. void
  2226. wasm_instance_vec_delete(wasm_instance_vec_t *instance_vec)
  2227. {
  2228. size_t i = 0;
  2229. if (!instance_vec || !instance_vec->data) {
  2230. return;
  2231. }
  2232. FREE_VEC_ELEMS(instance_vec, wasm_instance_delete_internal);
  2233. bh_vector_destroy((Vector *)instance_vec);
  2234. }
  2235. wasm_extern_t *
  2236. wasm_extern_copy(const wasm_extern_t *src)
  2237. {
  2238. wasm_extern_t *dst = NULL;
  2239. wasm_func_t *func = NULL;
  2240. wasm_global_t *global = NULL;
  2241. bh_assert(src);
  2242. switch (wasm_extern_kind(src)) {
  2243. case WASM_EXTERN_FUNC:
  2244. func = wasm_func_copy(wasm_extern_as_func_const(src));
  2245. dst = wasm_func_as_extern(func);
  2246. break;
  2247. case WASM_EXTERN_GLOBAL:
  2248. global = wasm_global_copy(wasm_extern_as_global_const(src));
  2249. dst = wasm_global_as_extern(global);
  2250. break;
  2251. case WASM_EXTERN_MEMORY:
  2252. case WASM_EXTERN_TABLE:
  2253. default:
  2254. break;
  2255. }
  2256. if (!dst) {
  2257. goto failed;
  2258. }
  2259. return dst;
  2260. failed:
  2261. LOG_DEBUG("%s failed", __FUNCTION__);
  2262. wasm_extern_delete(dst);
  2263. return NULL;
  2264. }
  2265. void
  2266. wasm_extern_delete(wasm_extern_t *external)
  2267. {
  2268. if (!external) {
  2269. return;
  2270. }
  2271. switch (wasm_extern_kind(external)) {
  2272. case WASM_EXTERN_FUNC:
  2273. wasm_func_delete(wasm_extern_as_func(external));
  2274. break;
  2275. case WASM_EXTERN_GLOBAL:
  2276. wasm_global_delete(wasm_extern_as_global(external));
  2277. break;
  2278. case WASM_EXTERN_MEMORY:
  2279. case WASM_EXTERN_TABLE:
  2280. default:
  2281. break;
  2282. }
  2283. }
  2284. wasm_externkind_t
  2285. wasm_extern_kind(const wasm_extern_t *extrenal)
  2286. {
  2287. return (wasm_externkind_t)extrenal->kind;
  2288. }
  2289. wasm_func_t *
  2290. wasm_extern_as_func(wasm_extern_t *external)
  2291. {
  2292. return (wasm_func_t *)external;
  2293. }
  2294. const wasm_func_t *
  2295. wasm_extern_as_func_const(const wasm_extern_t *external)
  2296. {
  2297. return (const wasm_func_t *)external;
  2298. }
  2299. wasm_global_t *
  2300. wasm_extern_as_global(wasm_extern_t *external)
  2301. {
  2302. return (wasm_global_t *)external;
  2303. }
  2304. const wasm_global_t *
  2305. wasm_extern_as_global_const(const wasm_extern_t *external)
  2306. {
  2307. return (const wasm_global_t *)external;
  2308. }
  2309. wasm_memory_t *
  2310. wasm_extern_as_memory(wasm_extern_t *external)
  2311. {
  2312. return (wasm_memory_t *)external;
  2313. }
  2314. const wasm_memory_t *
  2315. wasm_extern_as_memory_const(const wasm_extern_t *external)
  2316. {
  2317. return (const wasm_memory_t *)external;
  2318. }
  2319. wasm_table_t *
  2320. wasm_extern_as_table(wasm_extern_t *external)
  2321. {
  2322. return (wasm_table_t *)external;
  2323. }
  2324. const wasm_table_t *
  2325. wasm_extern_as_table_const(const wasm_extern_t *external)
  2326. {
  2327. return (const wasm_table_t *)external;
  2328. }
  2329. void
  2330. wasm_extern_vec_copy(wasm_extern_vec_t *out, const wasm_extern_vec_t *src)
  2331. {
  2332. size_t i = 0;
  2333. bh_assert(out && src);
  2334. generic_vec_init_data((Vector *)out, src->size, sizeof(wasm_extern_t *));
  2335. if (!out->data) {
  2336. goto failed;
  2337. }
  2338. for (i = 0; i < src->num_elems; ++i) {
  2339. wasm_extern_t *orig = *(src->data + i);
  2340. wasm_extern_t *cloned = wasm_extern_copy(orig);
  2341. if (!cloned) {
  2342. goto failed;
  2343. }
  2344. if (!bh_vector_append((Vector *)out, &cloned)) {
  2345. goto failed;
  2346. }
  2347. }
  2348. return;
  2349. failed:
  2350. LOG_DEBUG("%s failed", __FUNCTION__);
  2351. wasm_extern_vec_delete(out);
  2352. return;
  2353. }
  2354. void
  2355. wasm_extern_vec_new_uninitialized(wasm_extern_vec_t *out, size_t size)
  2356. {
  2357. generic_vec_init_data((Vector *)out, size, sizeof(wasm_extern_t *));
  2358. }
  2359. void
  2360. wasm_extern_vec_delete(wasm_extern_vec_t *extern_vec)
  2361. {
  2362. size_t i = 0;
  2363. if (!extern_vec || !extern_vec->data) {
  2364. return;
  2365. }
  2366. FREE_VEC_ELEMS(extern_vec, wasm_extern_delete);
  2367. bh_vector_destroy((Vector *)extern_vec);
  2368. }