wasm_c_api.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028
  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 ASSERT_NOT_IMPLEMENTED() bh_assert(!"not implemented")
  14. typedef struct wasm_module_ex_t wasm_module_ex_t;
  15. static void
  16. wasm_module_delete_internal(wasm_module_t *);
  17. static void
  18. wasm_instance_delete_internal(wasm_instance_t *);
  19. /* temporarily put stubs here */
  20. static wasm_store_t *
  21. wasm_store_copy(const wasm_store_t *src)
  22. {
  23. (void)src;
  24. LOG_WARNING("in the stub of %s", __FUNCTION__);
  25. return NULL;
  26. }
  27. wasm_module_t *
  28. wasm_module_copy(const wasm_module_t *src)
  29. {
  30. (void)src;
  31. LOG_WARNING("in the stub of %s", __FUNCTION__);
  32. return NULL;
  33. }
  34. wasm_instance_t *
  35. wasm_instance_copy(const wasm_instance_t *src)
  36. {
  37. (void)src;
  38. LOG_WARNING("in the stub of %s", __FUNCTION__);
  39. return NULL;
  40. }
  41. static void *
  42. malloc_internal(uint64 size)
  43. {
  44. void *mem = NULL;
  45. if (size < UINT32_MAX && (mem = wasm_runtime_malloc((uint32)size))) {
  46. memset(mem, 0, size);
  47. }
  48. return mem;
  49. }
  50. #define FREEIF(p) \
  51. if (p) { \
  52. wasm_runtime_free(p); \
  53. }
  54. /* clang-format off */
  55. #define RETURN_OBJ(obj, obj_del_func) \
  56. return obj; \
  57. failed: \
  58. obj_del_func(obj); \
  59. return NULL;
  60. #define RETURN_VOID(obj, obj_del_func) \
  61. return; \
  62. failed: \
  63. obj_del_func(obj); \
  64. return;
  65. /* clang-format on */
  66. /* Vectors */
  67. #define INIT_VEC(vector_p, init_func, ...) \
  68. do { \
  69. if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \
  70. goto failed; \
  71. } \
  72. \
  73. init_func(vector_p, ##__VA_ARGS__); \
  74. if (vector_p->size && !vector_p->data) { \
  75. LOG_DEBUG("%s failed", #init_func); \
  76. goto failed; \
  77. } \
  78. } while (false)
  79. #define DEINIT_VEC(vector_p, deinit_func) \
  80. if ((vector_p)) { \
  81. deinit_func(vector_p); \
  82. wasm_runtime_free(vector_p); \
  83. vector_p = NULL; \
  84. }
  85. #define WASM_DEFINE_VEC(name) \
  86. void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t *out) \
  87. { \
  88. wasm_##name##_vec_new_uninitialized(out, 0); \
  89. } \
  90. void wasm_##name##_vec_new_uninitialized(own wasm_##name##_vec_t *out, \
  91. size_t size) \
  92. { \
  93. wasm_##name##_vec_new(out, size, NULL); \
  94. }
  95. /* vectors with no ownership management of elements */
  96. #define WASM_DEFINE_VEC_PLAIN(name) \
  97. WASM_DEFINE_VEC(name) \
  98. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  99. own wasm_##name##_t const data[]) \
  100. { \
  101. if (!out) { \
  102. return; \
  103. } \
  104. \
  105. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  106. \
  107. if (!size) { \
  108. return; \
  109. } \
  110. \
  111. if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t))) { \
  112. LOG_DEBUG("bh_vector_init failed"); \
  113. goto failed; \
  114. } \
  115. \
  116. if (data) { \
  117. unsigned int size_in_bytes = 0; \
  118. size_in_bytes = size * sizeof(wasm_##name##_t); \
  119. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  120. out->num_elems = size; \
  121. } \
  122. \
  123. RETURN_VOID(out, wasm_##name##_vec_delete) \
  124. } \
  125. void wasm_##name##_vec_copy(wasm_##name##_vec_t *out, \
  126. const wasm_##name##_vec_t *src) \
  127. { \
  128. wasm_##name##_vec_new(out, src->size, src->data); \
  129. } \
  130. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  131. { \
  132. if (v) { \
  133. bh_vector_destroy((Vector *)v); \
  134. } \
  135. }
  136. /* vectors that own their elements */
  137. #define WASM_DEFINE_VEC_OWN(name, elem_destroy_func) \
  138. WASM_DEFINE_VEC(name) \
  139. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  140. own wasm_##name##_t *const data[]) \
  141. { \
  142. if (!out) { \
  143. return; \
  144. } \
  145. \
  146. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  147. \
  148. if (!size) { \
  149. return; \
  150. } \
  151. \
  152. if (!bh_vector_init((Vector *)out, size, \
  153. sizeof(wasm_##name##_t *))) { \
  154. LOG_DEBUG("bh_vector_init failed"); \
  155. goto failed; \
  156. } \
  157. \
  158. if (data) { \
  159. unsigned int size_in_bytes = 0; \
  160. size_in_bytes = size * sizeof(wasm_##name##_t *); \
  161. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  162. out->num_elems = size; \
  163. } \
  164. \
  165. RETURN_VOID(out, wasm_##name##_vec_delete) \
  166. } \
  167. void wasm_##name##_vec_copy(own wasm_##name##_vec_t *out, \
  168. const wasm_##name##_vec_t *src) \
  169. { \
  170. size_t i = 0; \
  171. memset(out, 0, sizeof(Vector)); \
  172. \
  173. if (!src || !src->size) { \
  174. return; \
  175. } \
  176. \
  177. if (!bh_vector_init((Vector *)out, src->size, \
  178. sizeof(wasm_##name##_t *))) { \
  179. LOG_DEBUG("bh_vector_init failed"); \
  180. goto failed; \
  181. } \
  182. \
  183. for (i = 0; i != src->num_elems; ++i) { \
  184. if (!(out->data[i] = wasm_##name##_copy(src->data[i]))) { \
  185. LOG_DEBUG("wasm_%s_copy failed", #name); \
  186. goto failed; \
  187. } \
  188. } \
  189. out->num_elems = src->num_elems; \
  190. \
  191. RETURN_VOID(out, wasm_##name##_vec_delete) \
  192. } \
  193. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  194. { \
  195. size_t i = 0; \
  196. if (!v) { \
  197. return; \
  198. } \
  199. for (i = 0; i != v->num_elems; ++i) { \
  200. elem_destroy_func(*(v->data + i)); \
  201. } \
  202. bh_vector_destroy((Vector *)v); \
  203. }
  204. WASM_DEFINE_VEC_PLAIN(byte)
  205. WASM_DEFINE_VEC_PLAIN(val)
  206. WASM_DEFINE_VEC_OWN(valtype, wasm_valtype_delete)
  207. WASM_DEFINE_VEC_OWN(functype, wasm_functype_delete)
  208. WASM_DEFINE_VEC_OWN(exporttype, wasm_exporttype_delete)
  209. WASM_DEFINE_VEC_OWN(importtype, wasm_importtype_delete)
  210. WASM_DEFINE_VEC_OWN(store, wasm_store_delete)
  211. WASM_DEFINE_VEC_OWN(module, wasm_module_delete_internal)
  212. WASM_DEFINE_VEC_OWN(instance, wasm_instance_delete_internal)
  213. WASM_DEFINE_VEC_OWN(extern, wasm_extern_delete)
  214. WASM_DEFINE_VEC_OWN(frame, wasm_frame_delete)
  215. static inline bool
  216. valid_module_type(uint32 module_type)
  217. {
  218. bool result = false;
  219. #if WASM_ENABLE_INTERP != 0
  220. result = result || (module_type == Wasm_Module_Bytecode);
  221. #endif
  222. #if WASM_ENABLE_AOT != 0
  223. result = result || (module_type == Wasm_Module_AoT);
  224. #endif
  225. if (!result) {
  226. LOG_VERBOSE(
  227. "current building isn't compatiable with the module, may need "
  228. "recompile");
  229. }
  230. return result;
  231. }
  232. /* conflicting declaration between aot_export.h and aot.h */
  233. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  234. bool
  235. aot_compile_wasm_file_init();
  236. void
  237. aot_compile_wasm_file_destroy();
  238. uint8*
  239. aot_compile_wasm_file(const uint8 *wasm_file_buf, uint32 wasm_file_size,
  240. uint32 opt_level, uint32 size_level,
  241. char *error_buf, uint32 error_buf_size,
  242. uint32 *p_aot_file_size);
  243. #endif
  244. /* Runtime Environment */
  245. static void
  246. wasm_engine_delete_internal(wasm_engine_t *engine)
  247. {
  248. if (engine) {
  249. DEINIT_VEC(engine->stores, wasm_store_vec_delete);
  250. wasm_runtime_free(engine);
  251. }
  252. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  253. aot_compile_wasm_file_destroy();
  254. #endif
  255. wasm_runtime_destroy();
  256. }
  257. static wasm_engine_t *
  258. wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts)
  259. {
  260. wasm_engine_t *engine = NULL;
  261. /* init runtime */
  262. RuntimeInitArgs init_args = { 0 };
  263. init_args.mem_alloc_type = type;
  264. if (type == Alloc_With_Pool) {
  265. if (!opts) {
  266. return NULL;
  267. }
  268. init_args.mem_alloc_option.pool.heap_buf = opts->pool.heap_buf;
  269. init_args.mem_alloc_option.pool.heap_size = opts->pool.heap_size;
  270. }
  271. else if (type == Alloc_With_Allocator) {
  272. if (!opts) {
  273. return NULL;
  274. }
  275. init_args.mem_alloc_option.allocator.malloc_func =
  276. opts->allocator.malloc_func;
  277. init_args.mem_alloc_option.allocator.free_func =
  278. opts->allocator.free_func;
  279. init_args.mem_alloc_option.allocator.realloc_func =
  280. opts->allocator.realloc_func;
  281. }
  282. else {
  283. init_args.mem_alloc_option.pool.heap_buf = NULL;
  284. init_args.mem_alloc_option.pool.heap_size = 0;
  285. }
  286. if (!wasm_runtime_full_init(&init_args)) {
  287. LOG_DEBUG("wasm_runtime_full_init failed");
  288. goto failed;
  289. }
  290. #if BH_DEBUG != 0
  291. bh_log_set_verbose_level(5);
  292. #else
  293. bh_log_set_verbose_level(3);
  294. #endif
  295. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  296. if (!aot_compile_wasm_file_init()) {
  297. goto failed;
  298. }
  299. #endif
  300. /* create wasm_engine_t */
  301. if (!(engine = malloc_internal(sizeof(wasm_engine_t)))) {
  302. goto failed;
  303. }
  304. /* create wasm_store_vec_t */
  305. INIT_VEC(engine->stores, wasm_store_vec_new_uninitialized, 1);
  306. RETURN_OBJ(engine, wasm_engine_delete_internal)
  307. }
  308. /* global engine instance */
  309. static wasm_engine_t *singleton_engine = NULL;
  310. wasm_engine_t *
  311. wasm_engine_new()
  312. {
  313. if (!singleton_engine) {
  314. singleton_engine =
  315. wasm_engine_new_internal(Alloc_With_System_Allocator, NULL);
  316. }
  317. return singleton_engine;
  318. }
  319. wasm_engine_t *
  320. wasm_engine_new_with_args(mem_alloc_type_t type, const MemAllocOption *opts)
  321. {
  322. if (!singleton_engine) {
  323. singleton_engine = wasm_engine_new_internal(type, opts);
  324. }
  325. return singleton_engine;
  326. }
  327. /* BE AWARE: will RESET the singleton */
  328. void
  329. wasm_engine_delete(wasm_engine_t *engine)
  330. {
  331. if (engine) {
  332. wasm_engine_delete_internal(engine);
  333. singleton_engine = NULL;
  334. }
  335. }
  336. wasm_store_t *
  337. wasm_store_new(wasm_engine_t *engine)
  338. {
  339. wasm_store_t *store = NULL;
  340. if (!engine || singleton_engine != engine) {
  341. return NULL;
  342. }
  343. if (!wasm_runtime_init_thread_env()) {
  344. LOG_ERROR("init thread environment failed");
  345. return NULL;
  346. }
  347. if (!(store = malloc_internal(sizeof(wasm_store_t)))) {
  348. wasm_runtime_destroy_thread_env();
  349. return NULL;
  350. }
  351. /* new a vector, and new its data */
  352. INIT_VEC(store->modules, wasm_module_vec_new_uninitialized,
  353. DEFAULT_VECTOR_INIT_LENGTH);
  354. INIT_VEC(store->instances, wasm_instance_vec_new_uninitialized,
  355. DEFAULT_VECTOR_INIT_LENGTH);
  356. /* append to a store list of engine */
  357. if (!bh_vector_append((Vector *)singleton_engine->stores, &store)) {
  358. LOG_DEBUG("bh_vector_append failed");
  359. goto failed;
  360. }
  361. return store;
  362. failed:
  363. wasm_store_delete(store);
  364. return NULL;
  365. }
  366. void
  367. wasm_store_delete(wasm_store_t *store)
  368. {
  369. size_t i, store_count;
  370. if (!store) {
  371. return;
  372. }
  373. /* remove it from the list in the engine */
  374. store_count = bh_vector_size((Vector *)singleton_engine->stores);
  375. for (i = 0; i != store_count; ++i) {
  376. wasm_store_t *tmp;
  377. if (!bh_vector_get((Vector *)singleton_engine->stores, i, &tmp)) {
  378. break;
  379. }
  380. if (tmp == store) {
  381. bh_vector_remove((Vector *)singleton_engine->stores, i, NULL);
  382. break;
  383. }
  384. }
  385. DEINIT_VEC(store->modules, wasm_module_vec_delete);
  386. DEINIT_VEC(store->instances, wasm_instance_vec_delete);
  387. wasm_runtime_free(store);
  388. wasm_runtime_destroy_thread_env();
  389. }
  390. /* Type Representations */
  391. static wasm_valkind_t
  392. val_type_rt_2_valkind(uint8 val_type_rt)
  393. {
  394. switch (val_type_rt) {
  395. case VALUE_TYPE_I32:
  396. return WASM_I32;
  397. case VALUE_TYPE_I64:
  398. return WASM_I64;
  399. case VALUE_TYPE_F32:
  400. return WASM_F32;
  401. case VALUE_TYPE_F64:
  402. return WASM_F64;
  403. case VALUE_TYPE_ANY:
  404. return WASM_ANYREF;
  405. case VALUE_TYPE_FUNCREF:
  406. return WASM_FUNCREF;
  407. default:
  408. LOG_WARNING("%s meets unsupported type: %d", __FUNCTION__,
  409. val_type_rt);
  410. return WASM_ANYREF;
  411. }
  412. }
  413. static wasm_valtype_t *
  414. wasm_valtype_new_internal(uint8 val_type_rt)
  415. {
  416. return wasm_valtype_new(val_type_rt_2_valkind(val_type_rt));
  417. }
  418. wasm_valtype_t *
  419. wasm_valtype_new(wasm_valkind_t kind)
  420. {
  421. wasm_valtype_t *val_type;
  422. if (!(val_type = malloc_internal(sizeof(wasm_valtype_t)))) {
  423. return NULL;
  424. }
  425. val_type->kind = kind;
  426. return val_type;
  427. }
  428. void
  429. wasm_valtype_delete(wasm_valtype_t *val_type)
  430. {
  431. FREEIF(val_type);
  432. }
  433. wasm_valtype_t *
  434. wasm_valtype_copy(const wasm_valtype_t *src)
  435. {
  436. if (!src) {
  437. return NULL;
  438. }
  439. return wasm_valtype_new(src->kind);
  440. }
  441. wasm_valkind_t
  442. wasm_valtype_kind(const wasm_valtype_t *val_type)
  443. {
  444. if (!val_type) {
  445. return WASM_ANYREF;
  446. }
  447. return val_type->kind;
  448. }
  449. bool
  450. wasm_valtype_same(const wasm_valtype_t *vt1, const wasm_valtype_t *vt2)
  451. {
  452. if (!vt1 && !vt2) {
  453. return true;
  454. }
  455. if (!vt1 || !vt2) {
  456. return false;
  457. }
  458. return vt1->kind == vt2->kind;
  459. }
  460. static wasm_functype_t *
  461. wasm_functype_new_internal(WASMType *type_rt)
  462. {
  463. wasm_functype_t *type = NULL;
  464. wasm_valtype_t *param_type = NULL, *result_type = NULL;
  465. uint32 i = 0;
  466. if (!type_rt) {
  467. return NULL;
  468. }
  469. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  470. return NULL;
  471. }
  472. type->extern_kind = WASM_EXTERN_FUNC;
  473. /* WASMType->types[0 : type_rt->param_count) -> type->params */
  474. INIT_VEC(type->params, wasm_valtype_vec_new_uninitialized,
  475. type_rt->param_count);
  476. for (i = 0; i < type_rt->param_count; ++i) {
  477. if (!(param_type = wasm_valtype_new_internal(*(type_rt->types + i)))) {
  478. goto failed;
  479. }
  480. if (!bh_vector_append((Vector *)type->params, &param_type)) {
  481. LOG_DEBUG("bh_vector_append failed");
  482. goto failed;
  483. }
  484. }
  485. /* WASMType->types[type_rt->param_count : type_rt->result_count) -> type->results */
  486. INIT_VEC(type->results, wasm_valtype_vec_new_uninitialized,
  487. type_rt->result_count);
  488. for (i = 0; i < type_rt->result_count; ++i) {
  489. if (!(result_type = wasm_valtype_new_internal(
  490. *(type_rt->types + type_rt->param_count + i)))) {
  491. goto failed;
  492. }
  493. if (!bh_vector_append((Vector *)type->results, &result_type)) {
  494. LOG_DEBUG("bh_vector_append failed");
  495. goto failed;
  496. }
  497. }
  498. return type;
  499. failed:
  500. wasm_valtype_delete(param_type);
  501. wasm_valtype_delete(result_type);
  502. wasm_functype_delete(type);
  503. return NULL;
  504. }
  505. wasm_functype_t *
  506. wasm_functype_new(own wasm_valtype_vec_t *params,
  507. own wasm_valtype_vec_t *results)
  508. {
  509. wasm_functype_t *type = NULL;
  510. if (!params) {
  511. return NULL;
  512. }
  513. if (!results) {
  514. return NULL;
  515. }
  516. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  517. goto failed;
  518. }
  519. type->extern_kind = WASM_EXTERN_FUNC;
  520. /* take ownership */
  521. if (!(type->params = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  522. goto failed;
  523. }
  524. bh_memcpy_s(type->params, sizeof(wasm_valtype_vec_t), params,
  525. sizeof(wasm_valtype_vec_t));
  526. if (!(type->results = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  527. goto failed;
  528. }
  529. bh_memcpy_s(type->results, sizeof(wasm_valtype_vec_t), results,
  530. sizeof(wasm_valtype_vec_t));
  531. return type;
  532. failed:
  533. wasm_functype_delete(type);
  534. return NULL;
  535. }
  536. wasm_functype_t *
  537. wasm_functype_copy(const wasm_functype_t *src)
  538. {
  539. wasm_functype_t *functype;
  540. wasm_valtype_vec_t params = { 0 }, results = { 0 };
  541. if (!src) {
  542. return NULL;
  543. }
  544. wasm_valtype_vec_copy(&params, src->params);
  545. if (src->params->size && !params.data) {
  546. goto failed;
  547. }
  548. wasm_valtype_vec_copy(&results, src->results);
  549. if (src->results->size && !results.data) {
  550. goto failed;
  551. }
  552. if (!(functype = wasm_functype_new(&params, &results))) {
  553. goto failed;
  554. }
  555. return functype;
  556. failed:
  557. wasm_valtype_vec_delete(&params);
  558. wasm_valtype_vec_delete(&results);
  559. return NULL;
  560. }
  561. void
  562. wasm_functype_delete(wasm_functype_t *func_type)
  563. {
  564. if (!func_type) {
  565. return;
  566. }
  567. DEINIT_VEC(func_type->params, wasm_valtype_vec_delete);
  568. DEINIT_VEC(func_type->results, wasm_valtype_vec_delete);
  569. wasm_runtime_free(func_type);
  570. }
  571. const wasm_valtype_vec_t *
  572. wasm_functype_params(const wasm_functype_t *func_type)
  573. {
  574. if (!func_type) {
  575. return NULL;
  576. }
  577. return func_type->params;
  578. }
  579. const wasm_valtype_vec_t *
  580. wasm_functype_results(const wasm_functype_t *func_type)
  581. {
  582. if (!func_type) {
  583. return NULL;
  584. }
  585. return func_type->results;
  586. }
  587. wasm_globaltype_t *
  588. wasm_globaltype_new(own wasm_valtype_t *val_type, wasm_mutability_t mut)
  589. {
  590. wasm_globaltype_t *global_type = NULL;
  591. if (!val_type) {
  592. return NULL;
  593. }
  594. if (!(global_type = malloc_internal(sizeof(wasm_globaltype_t)))) {
  595. return NULL;
  596. }
  597. global_type->extern_kind = WASM_EXTERN_GLOBAL;
  598. global_type->val_type = val_type;
  599. global_type->mutability = mut;
  600. return global_type;
  601. }
  602. wasm_globaltype_t *
  603. wasm_globaltype_new_internal(uint8 val_type_rt, bool is_mutable)
  604. {
  605. wasm_globaltype_t *globaltype;
  606. wasm_valtype_t *val_type;
  607. if (!(val_type = wasm_valtype_new(val_type_rt_2_valkind(val_type_rt)))) {
  608. return NULL;
  609. }
  610. if (!(globaltype = wasm_globaltype_new(
  611. val_type, is_mutable ? WASM_VAR : WASM_CONST))) {
  612. wasm_valtype_delete(val_type);
  613. }
  614. return globaltype;
  615. }
  616. void
  617. wasm_globaltype_delete(wasm_globaltype_t *global_type)
  618. {
  619. if (!global_type) {
  620. return;
  621. }
  622. if (global_type->val_type) {
  623. wasm_valtype_delete(global_type->val_type);
  624. global_type->val_type = NULL;
  625. }
  626. wasm_runtime_free(global_type);
  627. }
  628. wasm_globaltype_t *
  629. wasm_globaltype_copy(const wasm_globaltype_t *src)
  630. {
  631. wasm_globaltype_t *global_type;
  632. wasm_valtype_t *val_type;
  633. if (!src) {
  634. return NULL;
  635. }
  636. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  637. return NULL;
  638. }
  639. if (!(global_type = wasm_globaltype_new(val_type, src->mutability))) {
  640. wasm_valtype_delete(val_type);
  641. }
  642. return global_type;
  643. }
  644. const wasm_valtype_t *
  645. wasm_globaltype_content(const wasm_globaltype_t *global_type)
  646. {
  647. if (!global_type) {
  648. return NULL;
  649. }
  650. return global_type->val_type;
  651. }
  652. wasm_mutability_t
  653. wasm_globaltype_mutability(const wasm_globaltype_t *global_type)
  654. {
  655. if (!global_type) {
  656. return false;
  657. }
  658. return global_type->mutability;
  659. }
  660. bool
  661. wasm_globaltype_same(const wasm_globaltype_t *gt1,
  662. const wasm_globaltype_t *gt2)
  663. {
  664. if (!gt1 && !gt2) {
  665. return true;
  666. }
  667. if (!gt1 || !gt2) {
  668. return false;
  669. }
  670. return wasm_valtype_same(gt1->val_type, gt2->val_type)
  671. || gt1->mutability == gt2->mutability;
  672. }
  673. static wasm_tabletype_t *
  674. wasm_tabletype_new_internal(uint8 val_type_rt,
  675. uint32 init_size,
  676. uint32 max_size)
  677. {
  678. wasm_tabletype_t *table_type;
  679. wasm_limits_t limits = { init_size, max_size };
  680. wasm_valtype_t *val_type;
  681. if (!(val_type = wasm_valtype_new_internal(val_type_rt))) {
  682. return NULL;
  683. }
  684. if (!(table_type = wasm_tabletype_new(val_type, &limits))) {
  685. wasm_valtype_delete(val_type);
  686. }
  687. return table_type;
  688. }
  689. wasm_tabletype_t *
  690. wasm_tabletype_new(own wasm_valtype_t *val_type, const wasm_limits_t *limits)
  691. {
  692. wasm_tabletype_t *table_type = NULL;
  693. if (!val_type) {
  694. return NULL;
  695. }
  696. if (!(table_type = malloc_internal(sizeof(wasm_tabletype_t)))) {
  697. return NULL;
  698. }
  699. table_type->extern_kind = WASM_EXTERN_TABLE;
  700. table_type->val_type = val_type;
  701. table_type->limits.min = limits->min;
  702. table_type->limits.max = limits->max;
  703. return table_type;
  704. }
  705. wasm_tabletype_t *
  706. wasm_tabletype_copy(const wasm_tabletype_t *src)
  707. {
  708. wasm_tabletype_t *table_type;
  709. wasm_valtype_t *val_type;
  710. if (!src) {
  711. return NULL;
  712. }
  713. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  714. return NULL;
  715. }
  716. if (!(table_type = wasm_tabletype_new(val_type, &src->limits))) {
  717. wasm_valtype_delete(val_type);
  718. }
  719. return table_type;
  720. }
  721. void
  722. wasm_tabletype_delete(wasm_tabletype_t *table_type)
  723. {
  724. if (!table_type) {
  725. return;
  726. }
  727. if (table_type->val_type) {
  728. wasm_valtype_delete(table_type->val_type);
  729. table_type->val_type = NULL;
  730. }
  731. wasm_runtime_free(table_type);
  732. }
  733. const wasm_valtype_t *
  734. wasm_tabletype_element(const wasm_tabletype_t *table_type)
  735. {
  736. if (!table_type) {
  737. return NULL;
  738. }
  739. return table_type->val_type;
  740. }
  741. const wasm_limits_t *
  742. wasm_tabletype_limits(const wasm_tabletype_t *table_type)
  743. {
  744. if (!table_type) {
  745. return NULL;
  746. }
  747. return &(table_type->limits);
  748. }
  749. static wasm_memorytype_t *
  750. wasm_memorytype_new_internal(uint32 min_pages, uint32 max_pages)
  751. {
  752. wasm_limits_t limits = { min_pages, max_pages };
  753. return wasm_memorytype_new(&limits);
  754. }
  755. wasm_memorytype_t *
  756. wasm_memorytype_new(const wasm_limits_t *limits)
  757. {
  758. wasm_memorytype_t *memory_type = NULL;
  759. if (!limits) {
  760. return NULL;
  761. }
  762. if (!(memory_type = malloc_internal(sizeof(wasm_memorytype_t)))) {
  763. return NULL;
  764. }
  765. memory_type->extern_kind = WASM_EXTERN_MEMORY;
  766. memory_type->limits.min = limits->min;
  767. memory_type->limits.max = limits->max;
  768. return memory_type;
  769. }
  770. wasm_memorytype_t *
  771. wasm_memorytype_copy(const wasm_memorytype_t *src)
  772. {
  773. if (!src) {
  774. return NULL;
  775. }
  776. return wasm_memorytype_new(&src->limits);
  777. }
  778. void
  779. wasm_memorytype_delete(wasm_memorytype_t *memory_type)
  780. {
  781. FREEIF(memory_type);
  782. }
  783. const wasm_limits_t *
  784. wasm_memorytype_limits(const wasm_memorytype_t *memory_type)
  785. {
  786. if (!memory_type) {
  787. return NULL;
  788. }
  789. return &(memory_type->limits);
  790. }
  791. wasm_externkind_t
  792. wasm_externtype_kind(const wasm_externtype_t *extern_type)
  793. {
  794. if (!extern_type) {
  795. return WASM_EXTERN_FUNC;
  796. }
  797. return extern_type->extern_kind;
  798. }
  799. #define BASIC_FOUR_TYPE_LIST(V) \
  800. V(functype) \
  801. V(globaltype) \
  802. V(memorytype) \
  803. V(tabletype)
  804. #define WASM_EXTERNTYPE_AS_OTHERTYPE(name) \
  805. wasm_##name##_t *wasm_externtype_as_##name( \
  806. wasm_externtype_t *extern_type) \
  807. { \
  808. return (wasm_##name##_t *)extern_type; \
  809. }
  810. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE)
  811. #undef WASM_EXTERNTYPE_AS_OTHERTYPE
  812. #define WASM_OTHERTYPE_AS_EXTERNTYPE(name) \
  813. wasm_externtype_t *wasm_##name##_as_externtype(wasm_##name##_t *other) \
  814. { \
  815. return (wasm_externtype_t *)other; \
  816. }
  817. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE)
  818. #undef WASM_OTHERTYPE_AS_EXTERNTYPE
  819. #define WASM_EXTERNTYPE_AS_OTHERTYPE_CONST(name) \
  820. const wasm_##name##_t *wasm_externtype_as_##name##_const( \
  821. const wasm_externtype_t *extern_type) \
  822. { \
  823. return (const wasm_##name##_t *)extern_type; \
  824. }
  825. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE_CONST)
  826. #undef WASM_EXTERNTYPE_AS_OTHERTYPE_CONST
  827. #define WASM_OTHERTYPE_AS_EXTERNTYPE_CONST(name) \
  828. const wasm_externtype_t *wasm_##name##_as_externtype_const( \
  829. const wasm_##name##_t *other) \
  830. { \
  831. return (const wasm_externtype_t *)other; \
  832. }
  833. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE_CONST)
  834. #undef WASM_OTHERTYPE_AS_EXTERNTYPE_CONST
  835. wasm_externtype_t *
  836. wasm_externtype_copy(const wasm_externtype_t *src)
  837. {
  838. wasm_externtype_t *extern_type = NULL;
  839. if (!src) {
  840. return NULL;
  841. }
  842. switch (src->extern_kind) {
  843. #define COPY_EXTERNTYPE(NAME, name) \
  844. case WASM_EXTERN_##NAME: \
  845. { \
  846. extern_type = wasm_##name##_as_externtype( \
  847. wasm_##name##_copy(wasm_externtype_as_##name##_const(src))); \
  848. break; \
  849. }
  850. COPY_EXTERNTYPE(FUNC, functype)
  851. COPY_EXTERNTYPE(GLOBAL, globaltype)
  852. COPY_EXTERNTYPE(MEMORY, memorytype)
  853. COPY_EXTERNTYPE(TABLE, tabletype)
  854. #undef COPY_EXTERNTYPE
  855. default:
  856. LOG_WARNING("%s meets unsupported kind", __FUNCTION__,
  857. src->extern_kind);
  858. break;
  859. }
  860. return extern_type;
  861. }
  862. void
  863. wasm_externtype_delete(wasm_externtype_t *extern_type)
  864. {
  865. if (!extern_type) {
  866. return;
  867. }
  868. switch (wasm_externtype_kind(extern_type)) {
  869. case WASM_EXTERN_FUNC:
  870. wasm_functype_delete(wasm_externtype_as_functype(extern_type));
  871. break;
  872. case WASM_EXTERN_GLOBAL:
  873. wasm_globaltype_delete(wasm_externtype_as_globaltype(extern_type));
  874. break;
  875. case WASM_EXTERN_MEMORY:
  876. wasm_memorytype_delete(wasm_externtype_as_memorytype(extern_type));
  877. break;
  878. case WASM_EXTERN_TABLE:
  879. wasm_tabletype_delete(wasm_externtype_as_tabletype(extern_type));
  880. break;
  881. default:
  882. LOG_WARNING("%s meets unsupported type", __FUNCTION__,
  883. extern_type);
  884. break;
  885. }
  886. }
  887. own wasm_importtype_t *
  888. wasm_importtype_new(own wasm_byte_vec_t *module_name,
  889. own wasm_byte_vec_t *field_name,
  890. own wasm_externtype_t *extern_type)
  891. {
  892. wasm_importtype_t *import_type = NULL;
  893. if (!(import_type = malloc_internal(sizeof(wasm_importtype_t)))) {
  894. return NULL;
  895. }
  896. /* take ownership */
  897. if (!(import_type->module_name =
  898. malloc_internal(sizeof(wasm_byte_vec_t)))) {
  899. goto failed;
  900. }
  901. bh_memcpy_s(import_type->module_name, sizeof(wasm_byte_vec_t), module_name,
  902. sizeof(wasm_byte_vec_t));
  903. if (!(import_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  904. goto failed;
  905. }
  906. bh_memcpy_s(import_type->name, sizeof(wasm_byte_vec_t), field_name,
  907. sizeof(wasm_byte_vec_t));
  908. import_type->extern_type = extern_type;
  909. return import_type;
  910. failed:
  911. wasm_importtype_delete(import_type);
  912. return NULL;
  913. }
  914. void
  915. wasm_importtype_delete(own wasm_importtype_t *import_type)
  916. {
  917. if (!import_type) {
  918. return;
  919. }
  920. DEINIT_VEC(import_type->module_name, wasm_byte_vec_delete);
  921. DEINIT_VEC(import_type->name, wasm_byte_vec_delete);
  922. wasm_externtype_delete(import_type->extern_type);
  923. wasm_runtime_free(import_type);
  924. }
  925. own wasm_importtype_t *
  926. wasm_importtype_copy(const wasm_importtype_t *src)
  927. {
  928. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  929. wasm_externtype_t *extern_type = NULL;
  930. wasm_importtype_t *import_type = NULL;
  931. if (!src) {
  932. return NULL;
  933. }
  934. wasm_byte_vec_copy(&module_name, src->module_name);
  935. if (src->module_name->size && !module_name.data) {
  936. goto failed;
  937. }
  938. wasm_byte_vec_copy(&name, src->name);
  939. if (src->name->size && !name.data) {
  940. goto failed;
  941. }
  942. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  943. goto failed;
  944. }
  945. if (!(import_type =
  946. wasm_importtype_new(&module_name, &name, extern_type))) {
  947. goto failed;
  948. }
  949. return import_type;
  950. failed:
  951. wasm_byte_vec_delete(&module_name);
  952. wasm_byte_vec_delete(&name);
  953. wasm_externtype_delete(extern_type);
  954. wasm_importtype_delete(import_type);
  955. return NULL;
  956. }
  957. const wasm_byte_vec_t *
  958. wasm_importtype_module(const wasm_importtype_t *import_type)
  959. {
  960. if (!import_type) {
  961. return NULL;
  962. }
  963. return import_type->module_name;
  964. }
  965. const wasm_byte_vec_t *
  966. wasm_importtype_name(const wasm_importtype_t *import_type)
  967. {
  968. if (!import_type) {
  969. return NULL;
  970. }
  971. return import_type->name;
  972. }
  973. const wasm_externtype_t *
  974. wasm_importtype_type(const wasm_importtype_t *import_type)
  975. {
  976. if (!import_type) {
  977. return NULL;
  978. }
  979. return import_type->extern_type;
  980. }
  981. own wasm_exporttype_t *
  982. wasm_exporttype_new(own wasm_byte_vec_t *name,
  983. own wasm_externtype_t *extern_type)
  984. {
  985. wasm_exporttype_t *export_type = NULL;
  986. if (!(export_type = malloc_internal(sizeof(wasm_exporttype_t)))) {
  987. return NULL;
  988. }
  989. if (!(export_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  990. wasm_exporttype_delete(export_type);
  991. return NULL;
  992. }
  993. bh_memcpy_s(export_type->name, sizeof(wasm_byte_vec_t), name,
  994. sizeof(wasm_byte_vec_t));
  995. export_type->extern_type = extern_type;
  996. return export_type;
  997. }
  998. wasm_exporttype_t *
  999. wasm_exporttype_copy(const wasm_exporttype_t *src)
  1000. {
  1001. wasm_exporttype_t *export_type;
  1002. wasm_byte_vec_t name = { 0 };
  1003. wasm_externtype_t *extern_type = NULL;
  1004. if (!src) {
  1005. return NULL;
  1006. }
  1007. wasm_byte_vec_copy(&name, src->name);
  1008. if (src->name->size && !name.data) {
  1009. goto failed;
  1010. }
  1011. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  1012. goto failed;
  1013. }
  1014. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  1015. goto failed;
  1016. }
  1017. return export_type;
  1018. failed:
  1019. wasm_byte_vec_delete(&name);
  1020. wasm_externtype_delete(extern_type);
  1021. return NULL;
  1022. }
  1023. void
  1024. wasm_exporttype_delete(wasm_exporttype_t *export_type)
  1025. {
  1026. if (!export_type) {
  1027. return;
  1028. }
  1029. DEINIT_VEC(export_type->name, wasm_byte_vec_delete);
  1030. wasm_externtype_delete(export_type->extern_type);
  1031. wasm_runtime_free(export_type);
  1032. }
  1033. const wasm_byte_vec_t *
  1034. wasm_exporttype_name(const wasm_exporttype_t *export_type)
  1035. {
  1036. if (!export_type) {
  1037. return NULL;
  1038. }
  1039. return export_type->name;
  1040. }
  1041. const wasm_externtype_t *
  1042. wasm_exporttype_type(const wasm_exporttype_t *export_type)
  1043. {
  1044. if (!export_type) {
  1045. return NULL;
  1046. }
  1047. return export_type->extern_type;
  1048. }
  1049. /* Runtime Objects */
  1050. void
  1051. wasm_val_delete(wasm_val_t *v)
  1052. {
  1053. FREEIF(v);
  1054. }
  1055. void
  1056. wasm_val_copy(wasm_val_t *out, const wasm_val_t *src)
  1057. {
  1058. if (!out || !src) {
  1059. return;
  1060. }
  1061. bh_memcpy_s(out, sizeof(wasm_val_t), src, sizeof(wasm_val_t));
  1062. }
  1063. bool
  1064. wasm_val_same(const wasm_val_t *v1, const wasm_val_t *v2)
  1065. {
  1066. if (!v1 && !v2) {
  1067. return true;
  1068. }
  1069. if (!v1 || !v2) {
  1070. return false;
  1071. }
  1072. if (v1->kind != v2->kind) {
  1073. return false;
  1074. }
  1075. switch (v1->kind) {
  1076. case WASM_I32:
  1077. return v1->of.i32 == v2->of.i32;
  1078. case WASM_I64:
  1079. return v1->of.i64 == v2->of.i64;
  1080. case WASM_F32:
  1081. return v1->of.f32 == v2->of.f32;
  1082. case WASM_F64:
  1083. return v1->of.f64 == v2->of.f64;
  1084. case WASM_FUNCREF:
  1085. return v1->of.ref == v2->of.ref;
  1086. default:
  1087. break;
  1088. }
  1089. return false;
  1090. }
  1091. static wasm_frame_t *
  1092. wasm_frame_new(wasm_instance_t *instance,
  1093. size_t module_offset,
  1094. uint32 func_index,
  1095. size_t func_offset)
  1096. {
  1097. wasm_frame_t *frame;
  1098. if (!(frame = malloc_internal(sizeof(wasm_frame_t)))) {
  1099. return NULL;
  1100. }
  1101. frame->instance = instance;
  1102. frame->module_offset = module_offset;
  1103. frame->func_index = func_index;
  1104. frame->func_offset = func_offset;
  1105. return frame;
  1106. }
  1107. own wasm_frame_t *
  1108. wasm_frame_copy(const wasm_frame_t *src)
  1109. {
  1110. if (!src) {
  1111. return NULL;
  1112. }
  1113. return wasm_frame_new(src->instance, src->module_offset, src->func_index,
  1114. src->func_offset);
  1115. }
  1116. void
  1117. wasm_frame_delete(own wasm_frame_t *frame)
  1118. {
  1119. if (!frame) {
  1120. return;
  1121. }
  1122. wasm_runtime_free(frame);
  1123. }
  1124. struct wasm_instance_t *
  1125. wasm_frame_instance(const wasm_frame_t *frame)
  1126. {
  1127. return frame ? frame->instance : NULL;
  1128. }
  1129. size_t
  1130. wasm_frame_module_offset(const wasm_frame_t *frame)
  1131. {
  1132. return frame ? frame->module_offset : 0;
  1133. }
  1134. uint32_t
  1135. wasm_frame_func_index(const wasm_frame_t *frame)
  1136. {
  1137. return frame ? frame->func_index : 0;
  1138. }
  1139. size_t
  1140. wasm_frame_func_offset(const wasm_frame_t *frame)
  1141. {
  1142. return frame ? frame->func_offset : 0;
  1143. }
  1144. static wasm_trap_t *
  1145. wasm_trap_new_internal(WASMModuleInstanceCommon *inst_comm_rt,
  1146. const char *default_error_info)
  1147. {
  1148. wasm_trap_t *trap;
  1149. const char *error_info = NULL;
  1150. wasm_instance_vec_t *instances;
  1151. wasm_instance_t *frame_instance = NULL;
  1152. uint32 i;
  1153. if (!singleton_engine || !singleton_engine->stores
  1154. || !singleton_engine->stores->num_elems) {
  1155. return NULL;
  1156. }
  1157. #if WASM_ENABLE_INTERP != 0
  1158. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  1159. if (!(error_info =
  1160. wasm_get_exception((WASMModuleInstance *)inst_comm_rt))) {
  1161. return NULL;
  1162. }
  1163. }
  1164. #endif
  1165. #if WASM_ENABLE_AOT != 0
  1166. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  1167. if (!(error_info =
  1168. aot_get_exception((AOTModuleInstance *)inst_comm_rt))) {
  1169. return NULL;
  1170. }
  1171. }
  1172. #endif
  1173. if (!error_info && !(error_info = default_error_info)) {
  1174. return NULL;
  1175. }
  1176. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1177. return NULL;
  1178. }
  1179. if (!(trap->message = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1180. goto failed;
  1181. }
  1182. wasm_name_new_from_string_nt(trap->message, error_info);
  1183. if (strlen(error_info) && !trap->message->data) {
  1184. goto failed;
  1185. }
  1186. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1187. #if WASM_ENABLE_INTERP != 0
  1188. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  1189. trap->frames = ((WASMModuleInstance *)inst_comm_rt)->frames;
  1190. }
  1191. #endif
  1192. #if WASM_ENABLE_AOT != 0
  1193. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  1194. trap->frames = ((AOTModuleInstance *)inst_comm_rt)->frames.ptr;
  1195. }
  1196. #endif
  1197. #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1198. /* allow a NULL frames list */
  1199. if (!trap->frames) {
  1200. return trap;
  1201. }
  1202. if (!(instances = singleton_engine->stores->data[0]->instances)) {
  1203. goto failed;
  1204. }
  1205. for (i = 0; i < instances->num_elems; i++) {
  1206. if (instances->data[i]->inst_comm_rt == inst_comm_rt) {
  1207. frame_instance = instances->data[i];
  1208. break;
  1209. }
  1210. }
  1211. for (i = 0; i < trap->frames->num_elems; i++) {
  1212. (((wasm_frame_t *)trap->frames->data) + i)->instance = frame_instance;
  1213. }
  1214. return trap;
  1215. failed:
  1216. wasm_trap_delete(trap);
  1217. return NULL;
  1218. }
  1219. wasm_trap_t *
  1220. wasm_trap_new(wasm_store_t *store, const wasm_message_t *message)
  1221. {
  1222. wasm_trap_t *trap;
  1223. if (!store || !message) {
  1224. return NULL;
  1225. }
  1226. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1227. return NULL;
  1228. }
  1229. INIT_VEC(trap->message, wasm_byte_vec_new, message->size, message->data);
  1230. return trap;
  1231. failed:
  1232. wasm_trap_delete(trap);
  1233. return NULL;
  1234. }
  1235. void
  1236. wasm_trap_delete(wasm_trap_t *trap)
  1237. {
  1238. if (!trap) {
  1239. return;
  1240. }
  1241. DEINIT_VEC(trap->message, wasm_byte_vec_delete);
  1242. /* reuse frames of WASMModuleInstance, do not free it here */
  1243. wasm_runtime_free(trap);
  1244. }
  1245. void
  1246. wasm_trap_message(const wasm_trap_t *trap, own wasm_message_t *out)
  1247. {
  1248. if (!trap || !out) {
  1249. return;
  1250. }
  1251. wasm_byte_vec_copy(out, trap->message);
  1252. }
  1253. own wasm_frame_t *
  1254. wasm_trap_origin(const wasm_trap_t *trap)
  1255. {
  1256. wasm_frame_t *latest_frame;
  1257. if (!trap || !trap->frames || !trap->frames->num_elems) {
  1258. return NULL;
  1259. }
  1260. /* first frame is the latest frame */
  1261. latest_frame = (wasm_frame_t *)trap->frames->data;
  1262. return wasm_frame_copy(latest_frame);
  1263. }
  1264. void
  1265. wasm_trap_trace(const wasm_trap_t *trap, own wasm_frame_vec_t *out)
  1266. {
  1267. uint32 i;
  1268. if (!trap || !out) {
  1269. return;
  1270. }
  1271. if (!trap->frames || !trap->frames->num_elems) {
  1272. wasm_frame_vec_new_empty(out);
  1273. return;
  1274. }
  1275. wasm_frame_vec_new_uninitialized(out, trap->frames->num_elems);
  1276. if (out->size && !out->data) {
  1277. return;
  1278. }
  1279. for (i = 0; i < trap->frames->num_elems; i++) {
  1280. wasm_frame_t *frame;
  1281. frame = ((wasm_frame_t *)trap->frames->data) + i;
  1282. if (!(out->data[i] =
  1283. wasm_frame_new(frame->instance, frame->module_offset,
  1284. frame->func_index, frame->func_offset))) {
  1285. goto failed;
  1286. }
  1287. out->num_elems++;
  1288. }
  1289. return;
  1290. failed:
  1291. for (i = 0; i < out->num_elems; i++) {
  1292. if (out->data[i]) {
  1293. wasm_runtime_free(out->data[i]);
  1294. }
  1295. }
  1296. if (out->data) {
  1297. wasm_runtime_free(out->data);
  1298. }
  1299. }
  1300. struct wasm_module_ex_t {
  1301. struct WASMModuleCommon *module_comm_rt;
  1302. wasm_byte_vec_t *binary;
  1303. };
  1304. static inline wasm_module_t *
  1305. module_ext_to_module(wasm_module_ex_t *module_ex)
  1306. {
  1307. return (wasm_module_t *)module_ex;
  1308. }
  1309. static inline wasm_module_ex_t *
  1310. module_to_module_ext(wasm_module_t *module)
  1311. {
  1312. return (wasm_module_ex_t *)module;
  1313. }
  1314. #if WASM_ENABLE_INTERP != 0
  1315. #define MODULE_INTERP(module) ((WASMModule *)(*module))
  1316. #endif
  1317. #if WASM_ENABLE_AOT != 0
  1318. #define MODULE_AOT(module) ((AOTModule *)(*module))
  1319. #endif
  1320. wasm_module_t *
  1321. wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
  1322. {
  1323. char error_buf[128] = { 0 };
  1324. wasm_module_ex_t *module_ex = NULL;
  1325. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  1326. uint8 *aot_file_buf = NULL;
  1327. uint32 aot_file_size;
  1328. #endif
  1329. bh_assert(singleton_engine);
  1330. if (!store || !binary || binary->size > UINT32_MAX) {
  1331. LOG_ERROR("%s failed", __FUNCTION__);
  1332. return NULL;
  1333. }
  1334. module_ex = malloc_internal(sizeof(wasm_module_ex_t));
  1335. if (!module_ex) {
  1336. goto failed;
  1337. }
  1338. INIT_VEC(module_ex->binary, wasm_byte_vec_new, binary->size, binary->data);
  1339. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  1340. if (get_package_type((uint8 *)module_ex->binary->data,
  1341. (uint32)module_ex->binary->size)
  1342. == Wasm_Module_Bytecode) {
  1343. if (!(aot_file_buf = aot_compile_wasm_file(
  1344. (uint8 *)module_ex->binary->data,
  1345. (uint32)module_ex->binary->size, 3, 3, error_buf,
  1346. (uint32)sizeof(error_buf), &aot_file_size))) {
  1347. LOG_ERROR(error_buf);
  1348. goto failed;
  1349. }
  1350. if (!(module_ex->module_comm_rt =
  1351. wasm_runtime_load(aot_file_buf, aot_file_size, error_buf,
  1352. (uint32)sizeof(error_buf)))) {
  1353. LOG_ERROR(error_buf);
  1354. goto failed;
  1355. }
  1356. }
  1357. else
  1358. #endif
  1359. {
  1360. module_ex->module_comm_rt = wasm_runtime_load(
  1361. (uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size,
  1362. error_buf, (uint32)sizeof(error_buf));
  1363. if (!(module_ex->module_comm_rt)) {
  1364. LOG_ERROR(error_buf);
  1365. goto failed;
  1366. }
  1367. }
  1368. /* add it to a watching list in store */
  1369. if (!bh_vector_append((Vector *)store->modules, &module_ex)) {
  1370. goto failed;
  1371. }
  1372. return module_ext_to_module(module_ex);
  1373. failed:
  1374. LOG_ERROR("%s failed", __FUNCTION__);
  1375. wasm_module_delete_internal(module_ext_to_module(module_ex));
  1376. return NULL;
  1377. }
  1378. static void
  1379. wasm_module_delete_internal(wasm_module_t *module)
  1380. {
  1381. wasm_module_ex_t *module_ex;
  1382. if (!module) {
  1383. return;
  1384. }
  1385. module_ex = module_to_module_ext(module);
  1386. DEINIT_VEC(module_ex->binary, wasm_byte_vec_delete);
  1387. if (module_ex->module_comm_rt) {
  1388. wasm_runtime_unload(module_ex->module_comm_rt);
  1389. module_ex->module_comm_rt = NULL;
  1390. }
  1391. wasm_runtime_free(module_ex);
  1392. }
  1393. void
  1394. wasm_module_delete(wasm_module_t *module)
  1395. {
  1396. /* the module will be released when releasing the store */
  1397. }
  1398. void
  1399. wasm_module_imports(const wasm_module_t *module,
  1400. own wasm_importtype_vec_t *out)
  1401. {
  1402. uint32 i, import_func_count = 0, import_memory_count = 0,
  1403. import_global_count = 0, import_table_count = 0,
  1404. import_count = 0;
  1405. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  1406. wasm_externtype_t *extern_type = NULL;
  1407. wasm_importtype_t *import_type = NULL;
  1408. if (!module || !out || !valid_module_type((*module)->module_type)) {
  1409. return;
  1410. }
  1411. #if WASM_ENABLE_INTERP != 0
  1412. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1413. import_func_count = MODULE_INTERP(module)->import_function_count;
  1414. import_global_count = MODULE_INTERP(module)->import_global_count;
  1415. import_memory_count = MODULE_INTERP(module)->import_memory_count;
  1416. import_table_count = MODULE_INTERP(module)->import_table_count;
  1417. }
  1418. #endif
  1419. #if WASM_ENABLE_AOT != 0
  1420. if ((*module)->module_type == Wasm_Module_AoT) {
  1421. import_func_count = MODULE_AOT(module)->import_func_count;
  1422. import_global_count = MODULE_AOT(module)->import_global_count;
  1423. import_memory_count = MODULE_AOT(module)->import_memory_count;
  1424. import_table_count = MODULE_AOT(module)->import_table_count;
  1425. }
  1426. #endif
  1427. import_count = import_func_count + import_global_count + import_table_count
  1428. + import_memory_count;
  1429. wasm_importtype_vec_new_uninitialized(out, import_count);
  1430. if (import_count && !out->data) {
  1431. return;
  1432. }
  1433. for (i = 0; i != import_count; ++i) {
  1434. char *module_name_rt = NULL, *field_name_rt = NULL;
  1435. if (i < import_func_count) {
  1436. wasm_functype_t *type = NULL;
  1437. WASMType *type_rt = NULL;
  1438. #if WASM_ENABLE_INTERP != 0
  1439. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1440. WASMImport *import =
  1441. MODULE_INTERP(module)->import_functions + i;
  1442. module_name_rt = import->u.names.module_name;
  1443. field_name_rt = import->u.names.field_name;
  1444. type_rt = import->u.function.func_type;
  1445. }
  1446. #endif
  1447. #if WASM_ENABLE_AOT != 0
  1448. if ((*module)->module_type == Wasm_Module_AoT) {
  1449. AOTImportFunc *import = MODULE_AOT(module)->import_funcs + i;
  1450. module_name_rt = import->module_name;
  1451. field_name_rt = import->func_name;
  1452. type_rt = import->func_type;
  1453. }
  1454. #endif
  1455. if (!module_name_rt || !field_name_rt || !type_rt) {
  1456. continue;
  1457. }
  1458. wasm_name_new_from_string(&module_name, module_name_rt);
  1459. if (strlen(module_name_rt) && !module_name.data) {
  1460. goto failed;
  1461. }
  1462. wasm_name_new_from_string(&name, field_name_rt);
  1463. if (strlen(field_name_rt) && !name.data) {
  1464. goto failed;
  1465. }
  1466. if (!(type = wasm_functype_new_internal(type_rt))) {
  1467. goto failed;
  1468. }
  1469. extern_type = wasm_functype_as_externtype(type);
  1470. }
  1471. else if (i < import_func_count + import_global_count) {
  1472. wasm_globaltype_t *type = NULL;
  1473. uint8 val_type_rt = 0;
  1474. bool mutability_rt = 0;
  1475. #if WASM_ENABLE_INTERP != 0
  1476. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1477. WASMImport *import = MODULE_INTERP(module)->import_globals
  1478. + (i - import_func_count);
  1479. module_name_rt = import->u.names.module_name;
  1480. field_name_rt = import->u.names.field_name;
  1481. val_type_rt = import->u.global.type;
  1482. mutability_rt = import->u.global.is_mutable;
  1483. }
  1484. #endif
  1485. #if WASM_ENABLE_AOT != 0
  1486. if ((*module)->module_type == Wasm_Module_AoT) {
  1487. AOTImportGlobal *import =
  1488. MODULE_AOT(module)->import_globals + (i - import_func_count);
  1489. module_name_rt = import->module_name;
  1490. field_name_rt = import->global_name;
  1491. val_type_rt = import->type;
  1492. mutability_rt = import->is_mutable;
  1493. }
  1494. #endif
  1495. if (!module_name_rt || !field_name_rt) {
  1496. continue;
  1497. }
  1498. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  1499. mutability_rt))) {
  1500. goto failed;
  1501. }
  1502. extern_type = wasm_globaltype_as_externtype(type);
  1503. }
  1504. else if (i < import_func_count + import_global_count
  1505. + import_memory_count) {
  1506. wasm_memorytype_t *type = NULL;
  1507. uint32 min_page = 0, max_page = 0;
  1508. #if WASM_ENABLE_INTERP != 0
  1509. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1510. WASMImport *import =
  1511. MODULE_INTERP(module)->import_memories
  1512. + (i - import_func_count - import_global_count);
  1513. module_name_rt = import->u.names.module_name;
  1514. field_name_rt = import->u.names.field_name;
  1515. min_page = import->u.memory.init_page_count;
  1516. max_page = import->u.memory.max_page_count;
  1517. }
  1518. #endif
  1519. #if WASM_ENABLE_AOT != 0
  1520. if ((*module)->module_type == Wasm_Module_AoT) {
  1521. AOTImportMemory *import =
  1522. MODULE_AOT(module)->import_memories
  1523. + (i - import_func_count - import_global_count);
  1524. module_name_rt = import->module_name;
  1525. field_name_rt = import->memory_name;
  1526. min_page = import->mem_init_page_count;
  1527. max_page = import->mem_max_page_count;
  1528. }
  1529. #endif
  1530. if (!module_name_rt || !field_name_rt) {
  1531. continue;
  1532. }
  1533. wasm_name_new_from_string(&module_name, module_name_rt);
  1534. if (strlen(module_name_rt) && !module_name.data) {
  1535. goto failed;
  1536. }
  1537. wasm_name_new_from_string(&name, field_name_rt);
  1538. if (strlen(field_name_rt) && !name.data) {
  1539. goto failed;
  1540. }
  1541. if (!(type = wasm_memorytype_new_internal(min_page, max_page))) {
  1542. goto failed;
  1543. }
  1544. extern_type = wasm_memorytype_as_externtype(type);
  1545. }
  1546. else {
  1547. wasm_tabletype_t *type = NULL;
  1548. uint8 elem_type_rt = 0;
  1549. uint32 min_size = 0, max_size = 0;
  1550. #if WASM_ENABLE_INTERP != 0
  1551. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1552. WASMImport *import =
  1553. MODULE_INTERP(module)->import_tables
  1554. + (i - import_func_count - import_global_count
  1555. - import_memory_count);
  1556. module_name_rt = import->u.names.module_name;
  1557. field_name_rt = import->u.names.field_name;
  1558. elem_type_rt = import->u.table.elem_type;
  1559. min_size = import->u.table.init_size;
  1560. max_size = import->u.table.max_size;
  1561. }
  1562. #endif
  1563. #if WASM_ENABLE_AOT != 0
  1564. if ((*module)->module_type == Wasm_Module_AoT) {
  1565. AOTImportTable *import =
  1566. MODULE_AOT(module)->import_tables
  1567. + (i - import_func_count - import_global_count
  1568. - import_memory_count);
  1569. module_name_rt = import->module_name;
  1570. field_name_rt = import->table_name;
  1571. elem_type_rt = VALUE_TYPE_FUNCREF;
  1572. min_size = import->table_init_size;
  1573. max_size = import->table_max_size;
  1574. }
  1575. #endif
  1576. if (!module_name_rt || !field_name_rt) {
  1577. continue;
  1578. }
  1579. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  1580. max_size))) {
  1581. goto failed;
  1582. }
  1583. extern_type = wasm_tabletype_as_externtype(type);
  1584. }
  1585. if (!extern_type) {
  1586. continue;
  1587. }
  1588. if (!(import_type =
  1589. wasm_importtype_new(&module_name, &name, extern_type))) {
  1590. goto failed;
  1591. }
  1592. if (!bh_vector_append((Vector *)out, &import_type)) {
  1593. goto failed_importtype_new;
  1594. }
  1595. }
  1596. return;
  1597. failed:
  1598. wasm_byte_vec_delete(&module_name);
  1599. wasm_byte_vec_delete(&name);
  1600. wasm_externtype_delete(extern_type);
  1601. failed_importtype_new:
  1602. wasm_importtype_delete(import_type);
  1603. wasm_importtype_vec_delete(out);
  1604. }
  1605. void
  1606. wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out)
  1607. {
  1608. uint32 i, export_count = 0;
  1609. wasm_byte_vec_t name = { 0 };
  1610. wasm_externtype_t *extern_type = NULL;
  1611. wasm_exporttype_t *export_type = NULL;
  1612. if (!module || !out || !valid_module_type((*module)->module_type)) {
  1613. return;
  1614. }
  1615. #if WASM_ENABLE_INTERP != 0
  1616. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1617. export_count = MODULE_INTERP(module)->export_count;
  1618. }
  1619. #endif
  1620. #if WASM_ENABLE_AOT != 0
  1621. if ((*module)->module_type == Wasm_Module_AoT) {
  1622. export_count = MODULE_AOT(module)->export_count;
  1623. }
  1624. #endif
  1625. wasm_exporttype_vec_new_uninitialized(out, export_count);
  1626. if (export_count && !out->data) {
  1627. return;
  1628. }
  1629. for (i = 0; i != export_count; i++) {
  1630. WASMExport *export = NULL;
  1631. #if WASM_ENABLE_INTERP != 0
  1632. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1633. export = MODULE_INTERP(module)->exports + i;
  1634. }
  1635. #endif
  1636. #if WASM_ENABLE_AOT != 0
  1637. if ((*module)->module_type == Wasm_Module_AoT) {
  1638. export = MODULE_AOT(module)->exports + i;
  1639. }
  1640. #endif
  1641. if (!export) {
  1642. continue;
  1643. }
  1644. /* byte* -> wasm_byte_vec_t */
  1645. wasm_name_new_from_string(&name, export->name);
  1646. if (strlen(export->name) && !name.data) {
  1647. goto failed;
  1648. }
  1649. /* WASMExport -> (WASMType, (uint8, bool)) -> (wasm_functype_t, wasm_globaltype_t) -> wasm_externtype_t*/
  1650. switch (export->kind) {
  1651. case EXPORT_KIND_FUNC:
  1652. {
  1653. wasm_functype_t *type = NULL;
  1654. WASMType *type_rt;
  1655. if (!wasm_runtime_get_export_func_type(*module, export,
  1656. &type_rt)) {
  1657. goto failed;
  1658. }
  1659. if (!(type = wasm_functype_new_internal(type_rt))) {
  1660. goto failed;
  1661. }
  1662. extern_type = wasm_functype_as_externtype(type);
  1663. break;
  1664. }
  1665. case EXPORT_KIND_GLOBAL:
  1666. {
  1667. wasm_globaltype_t *type = NULL;
  1668. uint8 val_type_rt = 0;
  1669. bool mutability_rt = 0;
  1670. if (!wasm_runtime_get_export_global_type(
  1671. *module, export, &val_type_rt, &mutability_rt)) {
  1672. goto failed;
  1673. }
  1674. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  1675. mutability_rt))) {
  1676. goto failed;
  1677. }
  1678. extern_type = wasm_globaltype_as_externtype(type);
  1679. break;
  1680. }
  1681. case EXPORT_KIND_MEMORY:
  1682. {
  1683. wasm_memorytype_t *type = NULL;
  1684. uint32 min_page = 0, max_page = 0;
  1685. if (!wasm_runtime_get_export_memory_type(
  1686. *module, export, &min_page, &max_page)) {
  1687. goto failed;
  1688. }
  1689. if (!(type =
  1690. wasm_memorytype_new_internal(min_page, max_page))) {
  1691. goto failed;
  1692. }
  1693. extern_type = wasm_memorytype_as_externtype(type);
  1694. break;
  1695. }
  1696. case EXPORT_KIND_TABLE:
  1697. {
  1698. wasm_tabletype_t *type = NULL;
  1699. uint8 elem_type_rt = 0;
  1700. uint32 min_size = 0, max_size = 0;
  1701. if (!wasm_runtime_get_export_table_type(
  1702. *module, export, &elem_type_rt, &min_size, &max_size)) {
  1703. goto failed;
  1704. }
  1705. if (!(type = wasm_tabletype_new_internal(
  1706. elem_type_rt, min_size, max_size))) {
  1707. goto failed;
  1708. }
  1709. extern_type = wasm_tabletype_as_externtype(type);
  1710. break;
  1711. }
  1712. default:
  1713. {
  1714. LOG_WARNING("%s meets unsupported type", __FUNCTION__,
  1715. export->kind);
  1716. break;
  1717. }
  1718. }
  1719. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  1720. goto failed;
  1721. }
  1722. if (!(bh_vector_append((Vector *)out, &export_type))) {
  1723. goto failed_exporttype_new;
  1724. }
  1725. }
  1726. return;
  1727. failed:
  1728. wasm_byte_vec_delete(&name);
  1729. wasm_externtype_delete(extern_type);
  1730. failed_exporttype_new:
  1731. wasm_exporttype_delete(export_type);
  1732. wasm_exporttype_vec_delete(out);
  1733. }
  1734. static wasm_func_t *
  1735. wasm_func_new_basic(const wasm_functype_t *type,
  1736. wasm_func_callback_t func_callback)
  1737. {
  1738. wasm_func_t *func = NULL;
  1739. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  1740. goto failed;
  1741. }
  1742. func->kind = WASM_EXTERN_FUNC;
  1743. func->with_env = false;
  1744. func->u.cb = func_callback;
  1745. if (!(func->type = wasm_functype_copy(type))) {
  1746. goto failed;
  1747. }
  1748. RETURN_OBJ(func, wasm_func_delete)
  1749. }
  1750. static wasm_func_t *
  1751. wasm_func_new_with_env_basic(const wasm_functype_t *type,
  1752. wasm_func_callback_with_env_t callback,
  1753. void *env,
  1754. void (*finalizer)(void *))
  1755. {
  1756. wasm_func_t *func = NULL;
  1757. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  1758. goto failed;
  1759. }
  1760. func->kind = WASM_EXTERN_FUNC;
  1761. func->with_env = true;
  1762. func->u.cb_env.cb = callback;
  1763. func->u.cb_env.env = env;
  1764. func->u.cb_env.finalizer = finalizer;
  1765. if (!(func->type = wasm_functype_copy(type))) {
  1766. goto failed;
  1767. }
  1768. RETURN_OBJ(func, wasm_func_delete)
  1769. }
  1770. wasm_func_t *
  1771. wasm_func_new(wasm_store_t *store,
  1772. const wasm_functype_t *type,
  1773. wasm_func_callback_t callback)
  1774. {
  1775. bh_assert(singleton_engine);
  1776. return wasm_func_new_basic(type, callback);
  1777. }
  1778. wasm_func_t *
  1779. wasm_func_new_with_env(wasm_store_t *store,
  1780. const wasm_functype_t *type,
  1781. wasm_func_callback_with_env_t callback,
  1782. void *env,
  1783. void (*finalizer)(void *))
  1784. {
  1785. bh_assert(singleton_engine);
  1786. return wasm_func_new_with_env_basic(type, callback, env, finalizer);
  1787. }
  1788. static wasm_func_t *
  1789. wasm_func_new_internal(wasm_store_t *store,
  1790. uint16 func_idx_rt,
  1791. WASMModuleInstanceCommon *inst_comm_rt)
  1792. {
  1793. wasm_func_t *func = NULL;
  1794. WASMType *type_rt = NULL;
  1795. bh_assert(singleton_engine);
  1796. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  1797. return NULL;
  1798. }
  1799. func = malloc_internal(sizeof(wasm_func_t));
  1800. if (!func) {
  1801. goto failed;
  1802. }
  1803. func->kind = WASM_EXTERN_FUNC;
  1804. #if WASM_ENABLE_INTERP != 0
  1805. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  1806. bh_assert(func_idx_rt
  1807. < ((WASMModuleInstance *)inst_comm_rt)->function_count);
  1808. WASMFunctionInstance *func_interp =
  1809. ((WASMModuleInstance *)inst_comm_rt)->functions + func_idx_rt;
  1810. type_rt = func_interp->is_import_func
  1811. ? func_interp->u.func_import->func_type
  1812. : func_interp->u.func->func_type;
  1813. }
  1814. #endif
  1815. #if WASM_ENABLE_AOT != 0
  1816. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  1817. /* use same index to trace the function type in AOTFuncType **func_types */
  1818. AOTModule *module_aot =
  1819. ((AOTModuleInstance *)inst_comm_rt)->aot_module.ptr;
  1820. if (func_idx_rt < module_aot->import_func_count) {
  1821. type_rt = (module_aot->import_funcs + func_idx_rt)->func_type;
  1822. }
  1823. else {
  1824. type_rt =
  1825. module_aot
  1826. ->func_types[module_aot->func_type_indexes
  1827. [func_idx_rt - module_aot->import_func_count]];
  1828. }
  1829. }
  1830. #endif
  1831. if (!type_rt) {
  1832. goto failed;
  1833. }
  1834. func->type = wasm_functype_new_internal(type_rt);
  1835. if (!func->type) {
  1836. goto failed;
  1837. }
  1838. /* will add name information when processing "exports" */
  1839. func->module_name = NULL;
  1840. func->name = NULL;
  1841. func->func_idx_rt = func_idx_rt;
  1842. func->inst_comm_rt = inst_comm_rt;
  1843. return func;
  1844. failed:
  1845. LOG_DEBUG("%s failed", __FUNCTION__);
  1846. wasm_func_delete(func);
  1847. return NULL;
  1848. }
  1849. void
  1850. wasm_func_delete(wasm_func_t *func)
  1851. {
  1852. if (!func) {
  1853. return;
  1854. }
  1855. if (func->type) {
  1856. wasm_functype_delete(func->type);
  1857. func->type = NULL;
  1858. }
  1859. if (func->with_env) {
  1860. if (func->u.cb_env.finalizer) {
  1861. func->u.cb_env.finalizer(func->u.cb_env.env);
  1862. func->u.cb_env.finalizer = NULL;
  1863. func->u.cb_env.env = NULL;
  1864. }
  1865. }
  1866. wasm_runtime_free(func);
  1867. }
  1868. own wasm_func_t *
  1869. wasm_func_copy(const wasm_func_t *func)
  1870. {
  1871. wasm_func_t *cloned = NULL;
  1872. if (!func) {
  1873. return NULL;
  1874. }
  1875. if (!(cloned = func->with_env
  1876. ? wasm_func_new_with_env_basic(
  1877. func->type, func->u.cb_env.cb, func->u.cb_env.env,
  1878. func->u.cb_env.finalizer)
  1879. : wasm_func_new_basic(func->type, func->u.cb))) {
  1880. goto failed;
  1881. }
  1882. cloned->func_idx_rt = func->func_idx_rt;
  1883. cloned->inst_comm_rt = func->inst_comm_rt;
  1884. RETURN_OBJ(cloned, wasm_func_delete)
  1885. }
  1886. own wasm_functype_t *
  1887. wasm_func_type(const wasm_func_t *func)
  1888. {
  1889. if (!func) {
  1890. return NULL;
  1891. }
  1892. return wasm_functype_copy(func->type);
  1893. }
  1894. static uint32
  1895. params_to_argv(const wasm_val_t *params,
  1896. const wasm_valtype_vec_t *param_defs,
  1897. size_t param_arity,
  1898. uint32 *out)
  1899. {
  1900. size_t i = 0;
  1901. uint32 argc = 0;
  1902. const wasm_val_t *param = NULL;
  1903. if (!param_arity) {
  1904. return 0;
  1905. }
  1906. bh_assert(params && param_defs && out);
  1907. bh_assert(param_defs->num_elems == param_arity);
  1908. for (i = 0; out && i < param_arity; ++i) {
  1909. param = params + i;
  1910. bh_assert((*(param_defs->data + i))->kind == param->kind);
  1911. switch (param->kind) {
  1912. case WASM_I32:
  1913. *(int32 *)out = param->of.i32;
  1914. out += 1;
  1915. argc += 1;
  1916. break;
  1917. case WASM_I64:
  1918. *(int64 *)out = param->of.i64;
  1919. out += 2;
  1920. argc += 2;
  1921. break;
  1922. case WASM_F32:
  1923. *(float32 *)out = param->of.f32;
  1924. out += 1;
  1925. argc += 1;
  1926. break;
  1927. case WASM_F64:
  1928. *(float64 *)out = param->of.f64;
  1929. out += 2;
  1930. argc += 2;
  1931. break;
  1932. default:
  1933. LOG_DEBUG("unexpected parameter val type %d", param->kind);
  1934. goto failed;
  1935. }
  1936. }
  1937. return argc;
  1938. failed:
  1939. LOG_DEBUG("%s failed", __FUNCTION__);
  1940. return 0;
  1941. }
  1942. static uint32
  1943. argv_to_results(const uint32 *results,
  1944. const wasm_valtype_vec_t *result_defs,
  1945. size_t result_arity,
  1946. wasm_val_t *out)
  1947. {
  1948. size_t i = 0;
  1949. uint32 argc = 0;
  1950. const uint32 *result = results;
  1951. const wasm_valtype_t *def = NULL;
  1952. if (!result_arity) {
  1953. return 0;
  1954. }
  1955. bh_assert(results && result_defs && out);
  1956. bh_assert(result_arity == result_defs->num_elems);
  1957. for (i = 0; out && i < result_arity; i++) {
  1958. def = *(result_defs->data + i);
  1959. switch (def->kind) {
  1960. case WASM_I32:
  1961. {
  1962. out->kind = WASM_I32;
  1963. out->of.i32 = *(int32 *)result;
  1964. result += 1;
  1965. break;
  1966. }
  1967. case WASM_I64:
  1968. {
  1969. out->kind = WASM_I64;
  1970. out->of.i64 = *(int64 *)result;
  1971. result += 2;
  1972. break;
  1973. }
  1974. case WASM_F32:
  1975. {
  1976. out->kind = WASM_F32;
  1977. out->of.f32 = *(float32 *)result;
  1978. result += 1;
  1979. break;
  1980. }
  1981. case WASM_F64:
  1982. {
  1983. out->kind = WASM_F64;
  1984. out->of.f64 = *(float64 *)result;
  1985. result += 2;
  1986. break;
  1987. }
  1988. default:
  1989. LOG_WARNING("%s meets unsupported type: %d", __FUNCTION__,
  1990. def->kind);
  1991. goto failed;
  1992. }
  1993. out++;
  1994. argc++;
  1995. }
  1996. return argc;
  1997. failed:
  1998. LOG_DEBUG("%s failed", __FUNCTION__);
  1999. return 0;
  2000. }
  2001. wasm_trap_t *
  2002. wasm_func_call(const wasm_func_t *func,
  2003. const wasm_val_t params[],
  2004. wasm_val_t results[])
  2005. {
  2006. /* parameters count as if all are uint32 */
  2007. /* a int64 or float64 parameter means 2 */
  2008. uint32 argc = 0;
  2009. /* a parameter list and a return value list */
  2010. uint32 argv_buf[32], *argv = argv_buf;
  2011. WASMFunctionInstanceCommon *func_comm_rt = NULL;
  2012. WASMExecEnv *exec_env = NULL;
  2013. size_t param_count, result_count, alloc_count;
  2014. bh_assert(func && func->type && func->inst_comm_rt);
  2015. #if WASM_ENABLE_INTERP != 0
  2016. if (func->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2017. func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->functions
  2018. + func->func_idx_rt;
  2019. }
  2020. #endif
  2021. #if WASM_ENABLE_AOT != 0
  2022. if (func->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2023. if (!(func_comm_rt = func->func_comm_rt)) {
  2024. AOTModuleInstance *inst_aot =
  2025. (AOTModuleInstance *)func->inst_comm_rt;
  2026. AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2027. uint32 export_i = 0, export_func_j = 0;
  2028. for (; export_i < module_aot->export_count; ++export_i) {
  2029. AOTExport *export = module_aot->exports + export_i;
  2030. if (export->kind == EXPORT_KIND_FUNC) {
  2031. if (export->index == func->func_idx_rt) {
  2032. func_comm_rt =
  2033. (AOTFunctionInstance *)inst_aot->export_funcs.ptr
  2034. + export_func_j;
  2035. ((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
  2036. break;
  2037. }
  2038. export_func_j++;
  2039. }
  2040. }
  2041. }
  2042. }
  2043. #endif
  2044. if (!func_comm_rt) {
  2045. goto failed;
  2046. }
  2047. param_count = wasm_func_param_arity(func);
  2048. result_count = wasm_func_result_arity(func);
  2049. alloc_count = (param_count > result_count) ? param_count : result_count;
  2050. if (alloc_count > sizeof(argv_buf) / sizeof(uint64)) {
  2051. if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
  2052. goto failed;
  2053. }
  2054. }
  2055. /* copy parametes */
  2056. if (param_count
  2057. && !(argc = params_to_argv(params, wasm_functype_params(func->type),
  2058. param_count, argv))) {
  2059. goto failed;
  2060. }
  2061. exec_env = wasm_runtime_get_exec_env_singleton(func->inst_comm_rt);
  2062. if (!exec_env) {
  2063. goto failed;
  2064. }
  2065. if (!wasm_runtime_call_wasm(exec_env, func_comm_rt, argc, argv)) {
  2066. if (wasm_runtime_get_exception(func->inst_comm_rt)) {
  2067. LOG_DEBUG(wasm_runtime_get_exception(func->inst_comm_rt));
  2068. goto failed;
  2069. }
  2070. }
  2071. /* copy results */
  2072. if (result_count) {
  2073. if (!(argc = argv_to_results(argv, wasm_functype_results(func->type),
  2074. result_count, results))) {
  2075. goto failed;
  2076. }
  2077. }
  2078. if (argv != argv_buf)
  2079. wasm_runtime_free(argv);
  2080. return NULL;
  2081. failed:
  2082. if (argv != argv_buf)
  2083. wasm_runtime_free(argv);
  2084. /* trap -> exception -> trap */
  2085. if (wasm_runtime_get_exception(func->inst_comm_rt)) {
  2086. return wasm_trap_new_internal(func->inst_comm_rt, NULL);
  2087. }
  2088. else {
  2089. return wasm_trap_new_internal(func->inst_comm_rt,
  2090. "wasm_func_call failed");
  2091. }
  2092. }
  2093. size_t
  2094. wasm_func_param_arity(const wasm_func_t *func)
  2095. {
  2096. if (!func || !func->type || !func->type->params) {
  2097. return 0;
  2098. }
  2099. return func->type->params->num_elems;
  2100. }
  2101. size_t
  2102. wasm_func_result_arity(const wasm_func_t *func)
  2103. {
  2104. if (!func || !func->type || !func->type->results) {
  2105. return 0;
  2106. }
  2107. return func->type->results->num_elems;
  2108. }
  2109. wasm_global_t *
  2110. wasm_global_new(wasm_store_t *store,
  2111. const wasm_globaltype_t *global_type,
  2112. const wasm_val_t *init)
  2113. {
  2114. wasm_global_t *global = NULL;
  2115. bh_assert(singleton_engine);
  2116. global = malloc_internal(sizeof(wasm_global_t));
  2117. if (!global) {
  2118. goto failed;
  2119. }
  2120. global->kind = WASM_EXTERN_GLOBAL;
  2121. global->type = wasm_globaltype_copy(global_type);
  2122. if (!global->type) {
  2123. goto failed;
  2124. }
  2125. global->init = malloc_internal(sizeof(wasm_val_t));
  2126. if (!global->init) {
  2127. goto failed;
  2128. }
  2129. wasm_val_copy(global->init, init);
  2130. /* TODO: how to check if above is failed */
  2131. return global;
  2132. failed:
  2133. LOG_DEBUG("%s failed", __FUNCTION__);
  2134. wasm_global_delete(global);
  2135. return NULL;
  2136. }
  2137. /* almost same with wasm_global_new */
  2138. wasm_global_t *
  2139. wasm_global_copy(const wasm_global_t *src)
  2140. {
  2141. wasm_global_t *global = NULL;
  2142. if (!src) {
  2143. return NULL;
  2144. }
  2145. global = malloc_internal(sizeof(wasm_global_t));
  2146. if (!global) {
  2147. goto failed;
  2148. }
  2149. global->kind = WASM_EXTERN_GLOBAL;
  2150. global->type = wasm_globaltype_copy(src->type);
  2151. if (!global->type) {
  2152. goto failed;
  2153. }
  2154. global->init = malloc_internal(sizeof(wasm_val_t));
  2155. if (!global->init) {
  2156. goto failed;
  2157. }
  2158. wasm_val_copy(global->init, src->init);
  2159. global->global_idx_rt = src->global_idx_rt;
  2160. global->inst_comm_rt = src->inst_comm_rt;
  2161. return global;
  2162. failed:
  2163. LOG_DEBUG("%s failed", __FUNCTION__);
  2164. wasm_global_delete(global);
  2165. return NULL;
  2166. }
  2167. void
  2168. wasm_global_delete(wasm_global_t *global)
  2169. {
  2170. if (!global) {
  2171. return;
  2172. }
  2173. if (global->init) {
  2174. wasm_val_delete(global->init);
  2175. global->init = NULL;
  2176. }
  2177. if (global->type) {
  2178. wasm_globaltype_delete(global->type);
  2179. global->type = NULL;
  2180. }
  2181. wasm_runtime_free(global);
  2182. }
  2183. bool
  2184. wasm_global_same(const wasm_global_t *g1, const wasm_global_t *g2)
  2185. {
  2186. if (!g1 && !g2) {
  2187. return true;
  2188. }
  2189. if (!g1 || !g2) {
  2190. return false;
  2191. }
  2192. return g1->kind == g2->kind && wasm_globaltype_same(g1->type, g2->type)
  2193. && wasm_val_same(g1->init, g2->init);
  2194. }
  2195. #if WASM_ENABLE_INTERP != 0
  2196. static bool
  2197. interp_global_set(const WASMModuleInstance *inst_interp,
  2198. uint16 global_idx_rt,
  2199. const wasm_val_t *v)
  2200. {
  2201. const WASMGlobalInstance *global_interp =
  2202. inst_interp->globals + global_idx_rt;
  2203. uint8 val_type_rt = global_interp->type;
  2204. #if WASM_ENABLE_MULTI_MODULE != 0
  2205. uint8 *data = global_interp->import_global_inst
  2206. ? global_interp->import_module_inst->global_data
  2207. + global_interp->import_global_inst->data_offset
  2208. : inst_interp->global_data + global_interp->data_offset;
  2209. #else
  2210. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2211. #endif
  2212. bool ret = true;
  2213. switch (val_type_rt) {
  2214. case VALUE_TYPE_I32:
  2215. bh_assert(WASM_I32 == v->kind);
  2216. *((int32 *)data) = v->of.i32;
  2217. break;
  2218. case VALUE_TYPE_F32:
  2219. bh_assert(WASM_F32 == v->kind);
  2220. *((float32 *)data) = v->of.f32;
  2221. break;
  2222. case VALUE_TYPE_I64:
  2223. bh_assert(WASM_I64 == v->kind);
  2224. *((int64 *)data) = v->of.i64;
  2225. break;
  2226. case VALUE_TYPE_F64:
  2227. bh_assert(WASM_F64 == v->kind);
  2228. *((float64 *)data) = v->of.f64;
  2229. break;
  2230. default:
  2231. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2232. ret = false;
  2233. break;
  2234. }
  2235. return ret;
  2236. }
  2237. static bool
  2238. interp_global_get(const WASMModuleInstance *inst_interp,
  2239. uint16 global_idx_rt,
  2240. wasm_val_t *out)
  2241. {
  2242. WASMGlobalInstance *global_interp = inst_interp->globals + global_idx_rt;
  2243. uint8 val_type_rt = global_interp->type;
  2244. #if WASM_ENABLE_MULTI_MODULE != 0
  2245. uint8 *data = global_interp->import_global_inst
  2246. ? global_interp->import_module_inst->global_data
  2247. + global_interp->import_global_inst->data_offset
  2248. : inst_interp->global_data + global_interp->data_offset;
  2249. #else
  2250. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2251. #endif
  2252. bool ret = true;
  2253. switch (val_type_rt) {
  2254. case VALUE_TYPE_I32:
  2255. out->kind = WASM_I32;
  2256. out->of.i32 = *((int32 *)data);
  2257. break;
  2258. case VALUE_TYPE_F32:
  2259. out->kind = WASM_F32;
  2260. out->of.f32 = *((float32 *)data);
  2261. break;
  2262. case VALUE_TYPE_I64:
  2263. out->kind = WASM_I64;
  2264. out->of.i64 = *((int64 *)data);
  2265. break;
  2266. case VALUE_TYPE_F64:
  2267. out->kind = WASM_F64;
  2268. out->of.f64 = *((float64 *)data);
  2269. break;
  2270. default:
  2271. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2272. ret = false;
  2273. }
  2274. return ret;
  2275. }
  2276. #endif
  2277. #if WASM_ENABLE_AOT != 0
  2278. static bool
  2279. aot_global_set(const AOTModuleInstance *inst_aot,
  2280. uint16 global_idx_rt,
  2281. const wasm_val_t *v)
  2282. {
  2283. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2284. uint8 val_type_rt = 0;
  2285. uint32 data_offset = 0;
  2286. void *data = NULL;
  2287. bool ret = true;
  2288. if (global_idx_rt < module_aot->import_global_count) {
  2289. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2290. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2291. }
  2292. else {
  2293. data_offset =
  2294. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2295. .data_offset;
  2296. val_type_rt =
  2297. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2298. .type;
  2299. }
  2300. data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
  2301. switch (val_type_rt) {
  2302. case VALUE_TYPE_I32:
  2303. bh_assert(WASM_I32 == v->kind);
  2304. *((int32 *)data) = v->of.i32;
  2305. break;
  2306. case VALUE_TYPE_F32:
  2307. bh_assert(WASM_F32 == v->kind);
  2308. *((float32 *)data) = v->of.f32;
  2309. break;
  2310. case VALUE_TYPE_I64:
  2311. bh_assert(WASM_I64 == v->kind);
  2312. *((int64 *)data) = v->of.i64;
  2313. break;
  2314. case VALUE_TYPE_F64:
  2315. bh_assert(WASM_F64 == v->kind);
  2316. *((float64 *)data) = v->of.f64;
  2317. break;
  2318. default:
  2319. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2320. ret = false;
  2321. }
  2322. return ret;
  2323. }
  2324. static bool
  2325. aot_global_get(const AOTModuleInstance *inst_aot,
  2326. uint16 global_idx_rt,
  2327. wasm_val_t *out)
  2328. {
  2329. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2330. uint8 val_type_rt = 0;
  2331. uint32 data_offset = 0;
  2332. void *data = NULL;
  2333. bool ret = true;
  2334. if (global_idx_rt < module_aot->import_global_count) {
  2335. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2336. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2337. }
  2338. else {
  2339. data_offset =
  2340. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2341. .data_offset;
  2342. val_type_rt =
  2343. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2344. .type;
  2345. }
  2346. data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
  2347. switch (val_type_rt) {
  2348. case VALUE_TYPE_I32:
  2349. out->kind = WASM_I32;
  2350. out->of.i32 = *((int32 *)data);
  2351. break;
  2352. case VALUE_TYPE_F32:
  2353. out->kind = WASM_F32;
  2354. out->of.f32 = *((float32 *)data);
  2355. break;
  2356. case VALUE_TYPE_I64:
  2357. out->kind = WASM_I64;
  2358. out->of.i64 = *((int64 *)data);
  2359. break;
  2360. case VALUE_TYPE_F64:
  2361. out->kind = WASM_F64;
  2362. out->of.f64 = *((float64 *)data);
  2363. break;
  2364. default:
  2365. LOG_DEBUG("unexpected value type %d", val_type_rt);
  2366. ret = false;
  2367. }
  2368. return ret;
  2369. }
  2370. #endif
  2371. void
  2372. wasm_global_set(wasm_global_t *global, const wasm_val_t *v)
  2373. {
  2374. if (!global || !v
  2375. || !valid_module_type(global->inst_comm_rt->module_type)) {
  2376. return;
  2377. }
  2378. #if WASM_ENABLE_INTERP != 0
  2379. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2380. (void)interp_global_set((WASMModuleInstance *)global->inst_comm_rt,
  2381. global->global_idx_rt, v);
  2382. }
  2383. #endif
  2384. #if WASM_ENABLE_AOT != 0
  2385. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2386. (void)aot_global_set((AOTModuleInstance *)global->inst_comm_rt,
  2387. global->global_idx_rt, v);
  2388. }
  2389. #endif
  2390. }
  2391. void
  2392. wasm_global_get(const wasm_global_t *global, wasm_val_t *out)
  2393. {
  2394. if (!global || !out
  2395. || !valid_module_type(global->inst_comm_rt->module_type)) {
  2396. return;
  2397. }
  2398. memset(out, 0, sizeof(wasm_val_t));
  2399. #if WASM_ENABLE_INTERP != 0
  2400. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2401. (void)interp_global_get((WASMModuleInstance *)global->inst_comm_rt,
  2402. global->global_idx_rt, out);
  2403. }
  2404. #endif
  2405. #if WASM_ENABLE_AOT != 0
  2406. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2407. (void)aot_global_get((AOTModuleInstance *)global->inst_comm_rt,
  2408. global->global_idx_rt, out);
  2409. }
  2410. #endif
  2411. bh_assert(global->init->kind == out->kind);
  2412. }
  2413. static wasm_global_t *
  2414. wasm_global_new_internal(wasm_store_t *store,
  2415. uint16 global_idx_rt,
  2416. WASMModuleInstanceCommon *inst_comm_rt)
  2417. {
  2418. wasm_global_t *global = NULL;
  2419. uint8 val_type_rt = 0;
  2420. bool is_mutable = 0;
  2421. bh_assert(singleton_engine);
  2422. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  2423. return NULL;
  2424. }
  2425. global = malloc_internal(sizeof(wasm_global_t));
  2426. if (!global) {
  2427. goto failed;
  2428. }
  2429. /*
  2430. * global->module_name = NULL;
  2431. * global->name = NULL;
  2432. */
  2433. global->kind = WASM_EXTERN_GLOBAL;
  2434. #if WASM_ENABLE_INTERP != 0
  2435. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2436. WASMGlobalInstance *global_interp =
  2437. ((WASMModuleInstance *)inst_comm_rt)->globals + global_idx_rt;
  2438. val_type_rt = global_interp->type;
  2439. is_mutable = global_interp->is_mutable;
  2440. }
  2441. #endif
  2442. #if WASM_ENABLE_AOT != 0
  2443. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2444. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2445. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2446. if (global_idx_rt < module_aot->import_global_count) {
  2447. AOTImportGlobal *global_import_aot =
  2448. module_aot->import_globals + global_idx_rt;
  2449. val_type_rt = global_import_aot->type;
  2450. is_mutable = global_import_aot->is_mutable;
  2451. }
  2452. else {
  2453. AOTGlobal *global_aot =
  2454. module_aot->globals
  2455. + (global_idx_rt - module_aot->import_global_count);
  2456. val_type_rt = global_aot->type;
  2457. is_mutable = global_aot->is_mutable;
  2458. }
  2459. }
  2460. #endif
  2461. global->type = wasm_globaltype_new_internal(val_type_rt, is_mutable);
  2462. if (!global->type) {
  2463. goto failed;
  2464. }
  2465. global->init = malloc_internal(sizeof(wasm_val_t));
  2466. if (!global->init) {
  2467. goto failed;
  2468. }
  2469. #if WASM_ENABLE_INTERP != 0
  2470. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2471. interp_global_get((WASMModuleInstance *)inst_comm_rt, global_idx_rt,
  2472. global->init);
  2473. }
  2474. #endif
  2475. #if WASM_ENABLE_AOT != 0
  2476. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2477. aot_global_get((AOTModuleInstance *)inst_comm_rt, global_idx_rt,
  2478. global->init);
  2479. }
  2480. #endif
  2481. global->inst_comm_rt = inst_comm_rt;
  2482. global->global_idx_rt = global_idx_rt;
  2483. return global;
  2484. failed:
  2485. LOG_DEBUG("%s failed", __FUNCTION__);
  2486. wasm_global_delete(global);
  2487. return NULL;
  2488. }
  2489. wasm_globaltype_t *
  2490. wasm_global_type(const wasm_global_t *global)
  2491. {
  2492. if (!global) {
  2493. return NULL;
  2494. }
  2495. return wasm_globaltype_copy(global->type);
  2496. }
  2497. static wasm_table_t *
  2498. wasm_table_new_basic(const wasm_tabletype_t *type)
  2499. {
  2500. wasm_table_t *table = NULL;
  2501. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  2502. goto failed;
  2503. }
  2504. table->kind = WASM_EXTERN_TABLE;
  2505. if (!(table->type = wasm_tabletype_copy(type))) {
  2506. goto failed;
  2507. }
  2508. RETURN_OBJ(table, wasm_table_delete);
  2509. }
  2510. static wasm_table_t *
  2511. wasm_table_new_internal(wasm_store_t *store,
  2512. uint16 table_idx_rt,
  2513. WASMModuleInstanceCommon *inst_comm_rt)
  2514. {
  2515. wasm_table_t *table = NULL;
  2516. uint8 val_type_rt = 0;
  2517. uint32 init_size = 0, max_size = 0;
  2518. bh_assert(singleton_engine);
  2519. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  2520. return NULL;
  2521. }
  2522. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  2523. goto failed;
  2524. }
  2525. table->kind = WASM_EXTERN_TABLE;
  2526. #if WASM_ENABLE_INTERP != 0
  2527. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2528. WASMTableInstance *table_interp =
  2529. ((WASMModuleInstance *)inst_comm_rt)->tables[table_idx_rt];
  2530. val_type_rt = table_interp->elem_type;
  2531. init_size = table_interp->cur_size;
  2532. max_size = table_interp->max_size;
  2533. }
  2534. #endif
  2535. #if WASM_ENABLE_AOT != 0
  2536. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2537. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2538. AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2539. if (table_idx_rt < module_aot->import_table_count) {
  2540. AOTImportTable *table_aot =
  2541. module_aot->import_tables + table_idx_rt;
  2542. val_type_rt = VALUE_TYPE_FUNCREF;
  2543. init_size = table_aot->table_init_size;
  2544. max_size = table_aot->table_max_size;
  2545. }
  2546. else {
  2547. AOTTable *table_aot =
  2548. module_aot->tables
  2549. + (table_idx_rt - module_aot->import_table_count);
  2550. val_type_rt = VALUE_TYPE_FUNCREF;
  2551. init_size = table_aot->table_init_size;
  2552. max_size = table_aot->table_max_size;
  2553. }
  2554. }
  2555. #endif
  2556. if (!(table->type =
  2557. wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) {
  2558. goto failed;
  2559. }
  2560. table->inst_comm_rt = inst_comm_rt;
  2561. table->table_idx_rt = table_idx_rt;
  2562. RETURN_OBJ(table, wasm_table_delete);
  2563. }
  2564. wasm_table_t *
  2565. wasm_table_new(wasm_store_t *store,
  2566. const wasm_tabletype_t *table_type,
  2567. wasm_ref_t *init)
  2568. {
  2569. (void)init;
  2570. bh_assert(singleton_engine);
  2571. return wasm_table_new_basic(table_type);
  2572. }
  2573. wasm_table_t *
  2574. wasm_table_copy(const wasm_table_t *src)
  2575. {
  2576. return wasm_table_new_basic(src->type);
  2577. }
  2578. void
  2579. wasm_table_delete(wasm_table_t *table)
  2580. {
  2581. if (!table) {
  2582. return;
  2583. }
  2584. if (table->type) {
  2585. wasm_tabletype_delete(table->type);
  2586. table->type = NULL;
  2587. }
  2588. wasm_runtime_free(table);
  2589. }
  2590. wasm_tabletype_t *
  2591. wasm_table_type(const wasm_table_t *table)
  2592. {
  2593. if (!table) {
  2594. return NULL;
  2595. }
  2596. return wasm_tabletype_copy(table->type);
  2597. }
  2598. static wasm_memory_t *
  2599. wasm_memory_new_basic(const wasm_memorytype_t *type)
  2600. {
  2601. wasm_memory_t *memory = NULL;
  2602. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  2603. goto failed;
  2604. }
  2605. memory->kind = WASM_EXTERN_MEMORY;
  2606. memory->type = wasm_memorytype_copy(type);
  2607. RETURN_OBJ(memory, wasm_memory_delete)
  2608. }
  2609. wasm_memory_t *
  2610. wasm_memory_new(wasm_store_t *store, const wasm_memorytype_t *type)
  2611. {
  2612. bh_assert(singleton_engine);
  2613. return wasm_memory_new_basic(type);
  2614. }
  2615. wasm_memory_t *
  2616. wasm_memory_copy(const wasm_memory_t *src)
  2617. {
  2618. wasm_memory_t *dst = NULL;
  2619. if (!src) {
  2620. return NULL;
  2621. }
  2622. if (!(dst = wasm_memory_new_basic(src->type))) {
  2623. goto failed;
  2624. }
  2625. dst->memory_idx_rt = src->memory_idx_rt;
  2626. dst->inst_comm_rt = src->inst_comm_rt;
  2627. RETURN_OBJ(dst, wasm_memory_delete)
  2628. }
  2629. static wasm_memory_t *
  2630. wasm_memory_new_internal(wasm_store_t *store,
  2631. uint16 memory_idx_rt,
  2632. WASMModuleInstanceCommon *inst_comm_rt)
  2633. {
  2634. wasm_memory_t *memory = NULL;
  2635. uint32 min_pages = 0, max_pages = 0;
  2636. bh_assert(singleton_engine);
  2637. if (!inst_comm_rt || !valid_module_type(inst_comm_rt->module_type)) {
  2638. return NULL;
  2639. }
  2640. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  2641. goto failed;
  2642. }
  2643. memory->kind = WASM_EXTERN_MEMORY;
  2644. #if WASM_ENABLE_INTERP != 0
  2645. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2646. WASMMemoryInstance *memory_interp =
  2647. ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt];
  2648. min_pages = memory_interp->cur_page_count;
  2649. max_pages = memory_interp->max_page_count;
  2650. }
  2651. #endif
  2652. #if WASM_ENABLE_AOT != 0
  2653. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2654. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2655. AOTModule *module_aot = (AOTModule *)(inst_aot->aot_module.ptr);
  2656. if (memory_idx_rt < module_aot->import_memory_count) {
  2657. min_pages = module_aot->import_memories->mem_init_page_count;
  2658. max_pages = module_aot->import_memories->mem_max_page_count;
  2659. }
  2660. else {
  2661. min_pages = module_aot->memories->mem_init_page_count;
  2662. max_pages = module_aot->memories->mem_max_page_count;
  2663. }
  2664. }
  2665. #endif
  2666. if (!(memory->type = wasm_memorytype_new_internal(min_pages, max_pages))) {
  2667. goto failed;
  2668. }
  2669. memory->inst_comm_rt = inst_comm_rt;
  2670. memory->memory_idx_rt = memory_idx_rt;
  2671. RETURN_OBJ(memory, wasm_memory_delete);
  2672. }
  2673. void
  2674. wasm_memory_delete(wasm_memory_t *memory)
  2675. {
  2676. if (!memory) {
  2677. return;
  2678. }
  2679. if (memory->type) {
  2680. wasm_memorytype_delete(memory->type);
  2681. memory->type = NULL;
  2682. }
  2683. wasm_runtime_free(memory);
  2684. }
  2685. wasm_memorytype_t *
  2686. wasm_memory_type(const wasm_memory_t *memory)
  2687. {
  2688. if (!memory) {
  2689. return NULL;
  2690. }
  2691. return wasm_memorytype_copy(memory->type);
  2692. }
  2693. byte_t *
  2694. wasm_memory_data(wasm_memory_t *memory)
  2695. {
  2696. return (byte_t *)wasm_runtime_get_memory_data(memory->inst_comm_rt,
  2697. memory->memory_idx_rt);
  2698. }
  2699. size_t
  2700. wasm_memory_data_size(const wasm_memory_t *memory)
  2701. {
  2702. return wasm_runtime_get_memory_data_size(memory->inst_comm_rt,
  2703. memory->memory_idx_rt);
  2704. }
  2705. #if WASM_ENABLE_INTERP != 0
  2706. static bool
  2707. interp_link_func(const wasm_instance_t *inst,
  2708. const WASMModule *module_interp,
  2709. uint16 func_idx_rt,
  2710. wasm_func_t *import)
  2711. {
  2712. WASMImport *imported_func_interp = NULL;
  2713. wasm_func_t *cloned = NULL;
  2714. bh_assert(inst && module_interp && import);
  2715. bh_assert(func_idx_rt < module_interp->import_function_count);
  2716. bh_assert(WASM_EXTERN_FUNC == import->kind);
  2717. imported_func_interp = module_interp->import_functions + func_idx_rt;
  2718. bh_assert(imported_func_interp);
  2719. if (!(cloned = wasm_func_copy(import))) {
  2720. return false;
  2721. }
  2722. if (!bh_vector_append((Vector *)inst->imports, &cloned)) {
  2723. wasm_func_delete(cloned);
  2724. return false;
  2725. }
  2726. imported_func_interp->u.function.call_conv_wasm_c_api = true;
  2727. imported_func_interp->u.function.wasm_c_api_with_env = import->with_env;
  2728. if (import->with_env) {
  2729. imported_func_interp->u.function.func_ptr_linked = import->u.cb_env.cb;
  2730. imported_func_interp->u.function.attachment = import->u.cb_env.env;
  2731. }
  2732. else {
  2733. imported_func_interp->u.function.func_ptr_linked = import->u.cb;
  2734. imported_func_interp->u.function.attachment = NULL;
  2735. }
  2736. import->func_idx_rt = func_idx_rt;
  2737. return true;
  2738. }
  2739. static bool
  2740. interp_link_global(const WASMModule *module_interp,
  2741. uint16 global_idx_rt,
  2742. wasm_global_t *import)
  2743. {
  2744. WASMImport *imported_global_interp = NULL;
  2745. bh_assert(module_interp && import);
  2746. bh_assert(global_idx_rt < module_interp->import_global_count);
  2747. bh_assert(WASM_EXTERN_GLOBAL == import->kind);
  2748. imported_global_interp = module_interp->import_globals + global_idx_rt;
  2749. bh_assert(imported_global_interp);
  2750. /* set init value */
  2751. switch (wasm_valtype_kind(import->type->val_type)) {
  2752. case WASM_I32:
  2753. bh_assert(VALUE_TYPE_I32 == imported_global_interp->u.global.type);
  2754. imported_global_interp->u.global.global_data_linked.i32 =
  2755. import->init->of.i32;
  2756. break;
  2757. case WASM_I64:
  2758. bh_assert(VALUE_TYPE_I64 == imported_global_interp->u.global.type);
  2759. imported_global_interp->u.global.global_data_linked.i64 =
  2760. import->init->of.i64;
  2761. break;
  2762. case WASM_F32:
  2763. bh_assert(VALUE_TYPE_F32 == imported_global_interp->u.global.type);
  2764. imported_global_interp->u.global.global_data_linked.f32 =
  2765. import->init->of.f32;
  2766. break;
  2767. case WASM_F64:
  2768. bh_assert(VALUE_TYPE_F64 == imported_global_interp->u.global.type);
  2769. imported_global_interp->u.global.global_data_linked.f64 =
  2770. import->init->of.f64;
  2771. break;
  2772. default:
  2773. return false;
  2774. }
  2775. import->global_idx_rt = global_idx_rt;
  2776. imported_global_interp->u.global.is_linked = true;
  2777. return true;
  2778. }
  2779. static uint32
  2780. interp_link(const wasm_instance_t *inst,
  2781. const WASMModule *module_interp,
  2782. wasm_extern_t *imports[])
  2783. {
  2784. uint32 i = 0;
  2785. uint32 import_func_i = 0;
  2786. uint32 import_global_i = 0;
  2787. bh_assert(inst && module_interp && imports);
  2788. for (i = 0; i < module_interp->import_count; ++i) {
  2789. wasm_extern_t *import = imports[i];
  2790. WASMImport *import_rt = module_interp->imports + i;
  2791. switch (import_rt->kind) {
  2792. case IMPORT_KIND_FUNC:
  2793. {
  2794. if (!interp_link_func(inst, module_interp, import_func_i++,
  2795. wasm_extern_as_func(import))) {
  2796. goto failed;
  2797. }
  2798. break;
  2799. }
  2800. case IMPORT_KIND_GLOBAL:
  2801. {
  2802. if (!interp_link_global(module_interp, import_global_i++,
  2803. wasm_extern_as_global(import))) {
  2804. goto failed;
  2805. }
  2806. break;
  2807. }
  2808. case IMPORT_KIND_MEMORY:
  2809. case IMPORT_KIND_TABLE:
  2810. ASSERT_NOT_IMPLEMENTED();
  2811. break;
  2812. default:
  2813. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  2814. import_rt->kind);
  2815. goto failed;
  2816. }
  2817. }
  2818. return i;
  2819. failed:
  2820. LOG_DEBUG("%s failed", __FUNCTION__);
  2821. return (uint32)-1;
  2822. }
  2823. static bool
  2824. interp_process_export(wasm_store_t *store,
  2825. const WASMModuleInstance *inst_interp,
  2826. wasm_extern_vec_t *externals)
  2827. {
  2828. WASMExport *exports = NULL;
  2829. WASMExport *export = NULL;
  2830. wasm_extern_t *external = NULL;
  2831. uint32 export_cnt = 0;
  2832. uint32 i = 0;
  2833. bh_assert(store && inst_interp && inst_interp->module && externals);
  2834. exports = inst_interp->module->exports;
  2835. export_cnt = inst_interp->module->export_count;
  2836. for (i = 0; i < export_cnt; ++i) {
  2837. export = exports + i;
  2838. switch (export->kind) {
  2839. case EXPORT_KIND_FUNC:
  2840. {
  2841. wasm_func_t *func;
  2842. if (!(func = wasm_func_new_internal(
  2843. store, export->index,
  2844. (WASMModuleInstanceCommon *)inst_interp))) {
  2845. goto failed;
  2846. }
  2847. external = wasm_func_as_extern(func);
  2848. break;
  2849. }
  2850. case EXPORT_KIND_GLOBAL:
  2851. {
  2852. wasm_global_t *global;
  2853. if (!(global = wasm_global_new_internal(
  2854. store, export->index,
  2855. (WASMModuleInstanceCommon *)inst_interp))) {
  2856. goto failed;
  2857. }
  2858. external = wasm_global_as_extern(global);
  2859. break;
  2860. }
  2861. case EXPORT_KIND_TABLE:
  2862. {
  2863. wasm_table_t *table;
  2864. if (!(table = wasm_table_new_internal(
  2865. store, export->index,
  2866. (WASMModuleInstanceCommon *)inst_interp))) {
  2867. goto failed;
  2868. }
  2869. external = wasm_table_as_extern(table);
  2870. break;
  2871. }
  2872. case EXPORT_KIND_MEMORY:
  2873. {
  2874. wasm_memory_t *memory;
  2875. if (!(memory = wasm_memory_new_internal(
  2876. store, export->index,
  2877. (WASMModuleInstanceCommon *)inst_interp))) {
  2878. goto failed;
  2879. }
  2880. external = wasm_memory_as_extern(memory);
  2881. break;
  2882. }
  2883. default:
  2884. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  2885. export->kind);
  2886. goto failed;
  2887. }
  2888. if (!bh_vector_append((Vector *)externals, &external)) {
  2889. goto failed;
  2890. }
  2891. }
  2892. return true;
  2893. failed:
  2894. wasm_extern_delete(external);
  2895. return false;
  2896. }
  2897. #endif /* WASM_ENABLE_INTERP */
  2898. #if WASM_ENABLE_AOT != 0
  2899. static bool
  2900. aot_link_func(const wasm_instance_t *inst,
  2901. const AOTModule *module_aot,
  2902. uint32 import_func_idx_rt,
  2903. wasm_func_t *import)
  2904. {
  2905. AOTImportFunc *import_aot_func = NULL;
  2906. wasm_func_t *cloned = NULL;
  2907. bh_assert(inst && module_aot && import);
  2908. import_aot_func = module_aot->import_funcs + import_func_idx_rt;
  2909. bh_assert(import_aot_func);
  2910. if (!(cloned = wasm_func_copy(import))) {
  2911. return false;
  2912. }
  2913. if (!bh_vector_append((Vector *)inst->imports, &cloned)) {
  2914. wasm_func_delete(cloned);
  2915. return false;
  2916. }
  2917. import_aot_func->call_conv_wasm_c_api = true;
  2918. import_aot_func->wasm_c_api_with_env = import->with_env;
  2919. if (import->with_env) {
  2920. import_aot_func->func_ptr_linked = import->u.cb_env.cb;
  2921. import_aot_func->attachment = import->u.cb_env.env;
  2922. }
  2923. else
  2924. import_aot_func->func_ptr_linked = import->u.cb;
  2925. import->func_idx_rt = import_func_idx_rt;
  2926. return true;
  2927. }
  2928. static bool
  2929. aot_link_global(const AOTModule *module_aot,
  2930. uint16 global_idx_rt,
  2931. wasm_global_t *import)
  2932. {
  2933. AOTImportGlobal *import_aot_global = NULL;
  2934. const wasm_valtype_t *val_type = NULL;
  2935. bh_assert(module_aot && import);
  2936. import_aot_global = module_aot->import_globals + global_idx_rt;
  2937. bh_assert(import_aot_global);
  2938. //TODO: import->type ?
  2939. val_type = wasm_globaltype_content(import->type);
  2940. bh_assert(val_type);
  2941. switch (wasm_valtype_kind(val_type)) {
  2942. case WASM_I32:
  2943. bh_assert(VALUE_TYPE_I32 == import_aot_global->type);
  2944. import_aot_global->global_data_linked.i32 = import->init->of.i32;
  2945. break;
  2946. case WASM_I64:
  2947. bh_assert(VALUE_TYPE_I64 == import_aot_global->type);
  2948. import_aot_global->global_data_linked.i64 = import->init->of.i64;
  2949. break;
  2950. case WASM_F32:
  2951. bh_assert(VALUE_TYPE_F32 == import_aot_global->type);
  2952. import_aot_global->global_data_linked.f32 = import->init->of.f32;
  2953. break;
  2954. case WASM_F64:
  2955. bh_assert(VALUE_TYPE_F64 == import_aot_global->type);
  2956. import_aot_global->global_data_linked.f64 = import->init->of.f64;
  2957. break;
  2958. default:
  2959. goto failed;
  2960. }
  2961. import->global_idx_rt = global_idx_rt;
  2962. return true;
  2963. failed:
  2964. LOG_DEBUG("%s failed", __FUNCTION__);
  2965. return false;
  2966. }
  2967. static uint32
  2968. aot_link(const wasm_instance_t *inst,
  2969. const AOTModule *module_aot,
  2970. wasm_extern_t *imports[])
  2971. {
  2972. uint32 i = 0;
  2973. uint32 import_func_i = 0;
  2974. uint32 import_global_i = 0;
  2975. wasm_extern_t *import = NULL;
  2976. wasm_func_t *func = NULL;
  2977. wasm_global_t *global = NULL;
  2978. bh_assert(inst && module_aot && imports);
  2979. while (import_func_i < module_aot->import_func_count
  2980. || import_global_i < module_aot->import_global_count) {
  2981. import = imports[i++];
  2982. bh_assert(import);
  2983. switch (wasm_extern_kind(import)) {
  2984. case WASM_EXTERN_FUNC:
  2985. bh_assert(import_func_i < module_aot->import_func_count);
  2986. func = wasm_extern_as_func((wasm_extern_t *)import);
  2987. if (!aot_link_func(inst, module_aot, import_func_i++, func)) {
  2988. goto failed;
  2989. }
  2990. break;
  2991. case WASM_EXTERN_GLOBAL:
  2992. bh_assert(import_global_i < module_aot->import_global_count);
  2993. global = wasm_extern_as_global((wasm_extern_t *)import);
  2994. if (!aot_link_global(module_aot, import_global_i++, global)) {
  2995. goto failed;
  2996. }
  2997. break;
  2998. case WASM_EXTERN_MEMORY:
  2999. case WASM_EXTERN_TABLE:
  3000. ASSERT_NOT_IMPLEMENTED();
  3001. break;
  3002. default:
  3003. goto failed;
  3004. }
  3005. }
  3006. return i;
  3007. failed:
  3008. LOG_DEBUG("%s failed", __FUNCTION__);
  3009. return (uint32)-1;
  3010. }
  3011. static bool
  3012. aot_process_export(wasm_store_t *store,
  3013. const AOTModuleInstance *inst_aot,
  3014. wasm_extern_vec_t *externals)
  3015. {
  3016. uint32 i;
  3017. wasm_extern_t *external = NULL;
  3018. AOTModule *module_aot = NULL;
  3019. bh_assert(store && inst_aot && externals);
  3020. module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  3021. bh_assert(module_aot);
  3022. for (i = 0; i < module_aot->export_count; ++i) {
  3023. AOTExport *export = module_aot->exports + i;
  3024. switch (export->kind) {
  3025. case EXPORT_KIND_FUNC:
  3026. {
  3027. wasm_func_t *func = NULL;
  3028. if (!(func = wasm_func_new_internal(
  3029. store, export->index,
  3030. (WASMModuleInstanceCommon *)inst_aot))) {
  3031. goto failed;
  3032. }
  3033. external = wasm_func_as_extern(func);
  3034. break;
  3035. }
  3036. case EXPORT_KIND_GLOBAL:
  3037. {
  3038. wasm_global_t *global = NULL;
  3039. if (!(global = wasm_global_new_internal(
  3040. store, export->index,
  3041. (WASMModuleInstanceCommon *)inst_aot))) {
  3042. goto failed;
  3043. }
  3044. external = wasm_global_as_extern(global);
  3045. break;
  3046. }
  3047. case EXPORT_KIND_TABLE:
  3048. {
  3049. wasm_table_t *table;
  3050. if (!(table = wasm_table_new_internal(
  3051. store, export->index,
  3052. (WASMModuleInstanceCommon *)inst_aot))) {
  3053. goto failed;
  3054. }
  3055. external = wasm_table_as_extern(table);
  3056. break;
  3057. }
  3058. case EXPORT_KIND_MEMORY:
  3059. {
  3060. wasm_memory_t *memory;
  3061. if (!(memory = wasm_memory_new_internal(
  3062. store, export->index,
  3063. (WASMModuleInstanceCommon *)inst_aot))) {
  3064. goto failed;
  3065. }
  3066. external = wasm_memory_as_extern(memory);
  3067. break;
  3068. }
  3069. default:
  3070. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3071. export->kind);
  3072. goto failed;
  3073. }
  3074. if (!(external->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  3075. goto failed;
  3076. }
  3077. wasm_name_new_from_string(external->name, export->name);
  3078. if (strlen(export->name) && !external->name->data) {
  3079. goto failed;
  3080. }
  3081. if (!bh_vector_append((Vector *)externals, &external)) {
  3082. goto failed;
  3083. }
  3084. }
  3085. return true;
  3086. failed:
  3087. wasm_extern_delete(external);
  3088. return false;
  3089. }
  3090. #endif /* WASM_ENABLE_AOT */
  3091. wasm_instance_t *
  3092. wasm_instance_new(wasm_store_t *store,
  3093. const wasm_module_t *module,
  3094. const wasm_extern_t *const imports[],
  3095. own wasm_trap_t **traps)
  3096. {
  3097. char error_buf[128] = { 0 };
  3098. const uint32 stack_size = 32 * 1024;
  3099. const uint32 heap_size = 32 * 1024;
  3100. uint32 import_count = 0;
  3101. wasm_instance_t *instance = NULL;
  3102. uint32 i = 0;
  3103. (void)traps;
  3104. bh_assert(singleton_engine);
  3105. if (!module || !valid_module_type((*module)->module_type)) {
  3106. return NULL;
  3107. }
  3108. instance = malloc_internal(sizeof(wasm_instance_t));
  3109. if (!instance) {
  3110. goto failed;
  3111. }
  3112. /* link module and imports */
  3113. if (imports) {
  3114. #if WASM_ENABLE_INTERP != 0
  3115. if ((*module)->module_type == Wasm_Module_Bytecode) {
  3116. import_count = MODULE_INTERP(module)->import_count;
  3117. INIT_VEC(instance->imports, wasm_extern_vec_new_uninitialized,
  3118. import_count);
  3119. if (import_count) {
  3120. uint32 actual_link_import_count = interp_link(
  3121. instance, MODULE_INTERP(module), (wasm_extern_t **)imports);
  3122. /* make sure a complete import list */
  3123. if ((int32)import_count < 0
  3124. || import_count != actual_link_import_count) {
  3125. goto failed;
  3126. }
  3127. }
  3128. }
  3129. #endif
  3130. #if WASM_ENABLE_AOT != 0
  3131. if ((*module)->module_type == Wasm_Module_AoT) {
  3132. import_count = MODULE_AOT(module)->import_func_count
  3133. + MODULE_AOT(module)->import_global_count
  3134. + MODULE_AOT(module)->import_memory_count
  3135. + MODULE_AOT(module)->import_table_count;
  3136. INIT_VEC(instance->imports, wasm_extern_vec_new_uninitialized,
  3137. import_count);
  3138. if (import_count) {
  3139. import_count = aot_link(instance, MODULE_AOT(module),
  3140. (wasm_extern_t **)imports);
  3141. if ((int32)import_count < 0) {
  3142. goto failed;
  3143. }
  3144. }
  3145. }
  3146. #endif
  3147. }
  3148. instance->inst_comm_rt = wasm_runtime_instantiate(
  3149. *module, stack_size, heap_size, error_buf, sizeof(error_buf));
  3150. if (!instance->inst_comm_rt) {
  3151. LOG_ERROR(error_buf);
  3152. goto failed;
  3153. }
  3154. if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
  3155. goto failed;
  3156. }
  3157. /* fill with inst */
  3158. for (i = 0; imports && i < (uint32)import_count; ++i) {
  3159. wasm_extern_t *import = (wasm_extern_t *)imports[i];
  3160. switch (import->kind) {
  3161. case WASM_EXTERN_FUNC:
  3162. wasm_extern_as_func(import)->inst_comm_rt =
  3163. instance->inst_comm_rt;
  3164. break;
  3165. case WASM_EXTERN_GLOBAL:
  3166. wasm_extern_as_global(import)->inst_comm_rt =
  3167. instance->inst_comm_rt;
  3168. break;
  3169. case WASM_EXTERN_MEMORY:
  3170. wasm_extern_as_memory(import)->inst_comm_rt =
  3171. instance->inst_comm_rt;
  3172. break;
  3173. case WASM_EXTERN_TABLE:
  3174. wasm_extern_as_table(import)->inst_comm_rt =
  3175. instance->inst_comm_rt;
  3176. break;
  3177. default:
  3178. goto failed;
  3179. }
  3180. }
  3181. /* build the exports list */
  3182. #if WASM_ENABLE_INTERP != 0
  3183. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3184. uint32 export_cnt =
  3185. ((WASMModuleInstance *)instance->inst_comm_rt)->module->export_count;
  3186. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  3187. export_cnt);
  3188. if (!interp_process_export(
  3189. store, (WASMModuleInstance *)instance->inst_comm_rt,
  3190. instance->exports)) {
  3191. goto failed;
  3192. }
  3193. }
  3194. #endif
  3195. #if WASM_ENABLE_AOT != 0
  3196. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3197. uint32 export_cnt =
  3198. ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count
  3199. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count
  3200. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_tab_count
  3201. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_mem_count;
  3202. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  3203. export_cnt);
  3204. if (!aot_process_export(store,
  3205. (AOTModuleInstance *)instance->inst_comm_rt,
  3206. instance->exports)) {
  3207. goto failed;
  3208. }
  3209. }
  3210. #endif
  3211. /* add it to a watching list in store */
  3212. if (!bh_vector_append((Vector *)store->instances, &instance)) {
  3213. goto failed;
  3214. }
  3215. return instance;
  3216. failed:
  3217. LOG_DEBUG("%s failed", __FUNCTION__);
  3218. wasm_instance_delete_internal(instance);
  3219. return NULL;
  3220. }
  3221. static void
  3222. wasm_instance_delete_internal(wasm_instance_t *instance)
  3223. {
  3224. if (!instance) {
  3225. return;
  3226. }
  3227. DEINIT_VEC(instance->imports, wasm_extern_vec_delete);
  3228. DEINIT_VEC(instance->exports, wasm_extern_vec_delete);
  3229. if (instance->inst_comm_rt) {
  3230. wasm_runtime_deinstantiate(instance->inst_comm_rt);
  3231. instance->inst_comm_rt = NULL;
  3232. }
  3233. wasm_runtime_free(instance);
  3234. }
  3235. void
  3236. wasm_instance_delete(wasm_instance_t *module)
  3237. {
  3238. /* will release instance when releasing the store */
  3239. }
  3240. void
  3241. wasm_instance_exports(const wasm_instance_t *instance,
  3242. own wasm_extern_vec_t *out)
  3243. {
  3244. if (!instance || !out) {
  3245. return;
  3246. }
  3247. wasm_extern_vec_copy(out, instance->exports);
  3248. }
  3249. wasm_extern_t *
  3250. wasm_extern_copy(const wasm_extern_t *src)
  3251. {
  3252. wasm_extern_t *dst = NULL;
  3253. if (!src) {
  3254. return NULL;
  3255. }
  3256. switch (wasm_extern_kind(src)) {
  3257. case WASM_EXTERN_FUNC:
  3258. dst = wasm_func_as_extern(
  3259. wasm_func_copy(wasm_extern_as_func_const(src)));
  3260. break;
  3261. case WASM_EXTERN_GLOBAL:
  3262. dst = wasm_global_as_extern(
  3263. wasm_global_copy(wasm_extern_as_global_const(src)));
  3264. break;
  3265. case WASM_EXTERN_MEMORY:
  3266. dst = wasm_memory_as_extern(
  3267. wasm_memory_copy(wasm_extern_as_memory_const(src)));
  3268. break;
  3269. case WASM_EXTERN_TABLE:
  3270. dst = wasm_table_as_extern(
  3271. wasm_table_copy(wasm_extern_as_table_const(src)));
  3272. break;
  3273. default:
  3274. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3275. src->kind);
  3276. break;
  3277. }
  3278. if (!dst) {
  3279. goto failed;
  3280. }
  3281. return dst;
  3282. failed:
  3283. LOG_DEBUG("%s failed", __FUNCTION__);
  3284. wasm_extern_delete(dst);
  3285. return NULL;
  3286. }
  3287. void
  3288. wasm_extern_delete(wasm_extern_t *external)
  3289. {
  3290. if (!external) {
  3291. return;
  3292. }
  3293. if (external->name) {
  3294. wasm_byte_vec_delete(external->name);
  3295. wasm_runtime_free(external->name);
  3296. external->name = NULL;
  3297. }
  3298. switch (wasm_extern_kind(external)) {
  3299. case WASM_EXTERN_FUNC:
  3300. wasm_func_delete(wasm_extern_as_func(external));
  3301. break;
  3302. case WASM_EXTERN_GLOBAL:
  3303. wasm_global_delete(wasm_extern_as_global(external));
  3304. break;
  3305. case WASM_EXTERN_MEMORY:
  3306. wasm_memory_delete(wasm_extern_as_memory(external));
  3307. break;
  3308. case WASM_EXTERN_TABLE:
  3309. wasm_table_delete(wasm_extern_as_table(external));
  3310. break;
  3311. default:
  3312. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3313. external->kind);
  3314. break;
  3315. }
  3316. }
  3317. wasm_externkind_t
  3318. wasm_extern_kind(const wasm_extern_t *external)
  3319. {
  3320. if (!external) {
  3321. return WASM_ANYREF;
  3322. }
  3323. return external->kind;
  3324. }
  3325. own wasm_externtype_t *
  3326. wasm_extern_type(const wasm_extern_t *external)
  3327. {
  3328. if (!external) {
  3329. return NULL;
  3330. }
  3331. switch (wasm_extern_kind(external)) {
  3332. case WASM_EXTERN_FUNC:
  3333. return wasm_functype_as_externtype(
  3334. wasm_func_type(wasm_extern_as_func_const(external)));
  3335. case WASM_EXTERN_GLOBAL:
  3336. return wasm_globaltype_as_externtype(
  3337. wasm_global_type(wasm_extern_as_global_const(external)));
  3338. case WASM_EXTERN_MEMORY:
  3339. return wasm_memorytype_as_externtype(
  3340. wasm_memory_type(wasm_extern_as_memory_const(external)));
  3341. case WASM_EXTERN_TABLE:
  3342. return wasm_tabletype_as_externtype(
  3343. wasm_table_type(wasm_extern_as_table_const(external)));
  3344. default:
  3345. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3346. external->kind);
  3347. break;
  3348. }
  3349. return NULL;
  3350. }
  3351. #define BASIC_FOUR_LIST(V) \
  3352. V(func) \
  3353. V(global) \
  3354. V(memory) \
  3355. V(table)
  3356. #define WASM_EXTERN_AS_OTHER(name) \
  3357. wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \
  3358. { \
  3359. return (wasm_##name##_t *)external; \
  3360. }
  3361. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER)
  3362. #undef WASM_EXTERN_AS_OTHER
  3363. #define WASM_OTHER_AS_EXTERN(name) \
  3364. wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \
  3365. { \
  3366. return (wasm_extern_t *)other; \
  3367. }
  3368. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN)
  3369. #undef WASM_OTHER_AS_EXTERN
  3370. #define WASM_EXTERN_AS_OTHER_CONST(name) \
  3371. const wasm_##name##_t *wasm_extern_as_##name##_const( \
  3372. const wasm_extern_t *external) \
  3373. { \
  3374. return (const wasm_##name##_t *)external; \
  3375. }
  3376. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST)
  3377. #undef WASM_EXTERN_AS_OTHER_CONST
  3378. #define WASM_OTHER_AS_EXTERN_CONST(name) \
  3379. const wasm_extern_t *wasm_##name##_as_extern_const( \
  3380. const wasm_##name##_t *other) \
  3381. { \
  3382. return (const wasm_extern_t *)other; \
  3383. }
  3384. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST)
  3385. #undef WASM_OTHER_AS_EXTERN_CONST