wasm_c_api.c 69 KB

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