wasm_interp_fast.c 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_interp.h"
  6. #include "bh_log.h"
  7. #include "wasm_runtime.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_loader.h"
  10. #include "../common/wasm_exec_env.h"
  11. #if WASM_ENABLE_SHARED_MEMORY != 0
  12. #include "../common/wasm_shared_memory.h"
  13. #endif
  14. typedef int32 CellType_I32;
  15. typedef int64 CellType_I64;
  16. typedef float32 CellType_F32;
  17. typedef float64 CellType_F64;
  18. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  19. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  20. #define CHECK_MEMORY_OVERFLOW(bytes) \
  21. do { \
  22. uint64 offset1 = (uint64)offset + (uint64)addr; \
  23. if (offset1 + bytes <= (uint64)linear_mem_size) \
  24. /* If offset1 is in valid range, maddr must also \
  25. be in valid range, no need to check it again. */ \
  26. maddr = memory->memory_data + offset1; \
  27. else \
  28. goto out_of_bounds; \
  29. } while (0)
  30. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  31. do { \
  32. uint64 offset1 = (uint32)(start); \
  33. if (offset1 + bytes <= linear_mem_size) \
  34. /* App heap space is not valid space for \
  35. bulk memory operation */ \
  36. maddr = memory->memory_data + offset1; \
  37. else \
  38. goto out_of_bounds; \
  39. } while (0)
  40. #else
  41. #define CHECK_MEMORY_OVERFLOW(bytes) \
  42. do { \
  43. uint64 offset1 = (uint64)offset + (uint64)addr; \
  44. maddr = memory->memory_data + offset1; \
  45. } while (0)
  46. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  47. do { \
  48. maddr = memory->memory_data + (uint32)(start); \
  49. } while (0)
  50. #endif /* !defined(OS_ENABLE_HW_BOUND_CHECK) \
  51. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  52. #define CHECK_ATOMIC_MEMORY_ACCESS(align) \
  53. do { \
  54. if (((uintptr_t)maddr & (align - 1)) != 0) \
  55. goto unaligned_atomic; \
  56. } while (0)
  57. static inline uint32
  58. rotl32(uint32 n, uint32 c)
  59. {
  60. const uint32 mask = (31);
  61. c = c % 32;
  62. c &= mask;
  63. return (n << c) | (n >> ((0 - c) & mask));
  64. }
  65. static inline uint32
  66. rotr32(uint32 n, uint32 c)
  67. {
  68. const uint32 mask = (31);
  69. c = c % 32;
  70. c &= mask;
  71. return (n >> c) | (n << ((0 - c) & mask));
  72. }
  73. static inline uint64
  74. rotl64(uint64 n, uint64 c)
  75. {
  76. const uint64 mask = (63);
  77. c = c % 64;
  78. c &= mask;
  79. return (n << c) | (n >> ((0 - c) & mask));
  80. }
  81. static inline uint64
  82. rotr64(uint64 n, uint64 c)
  83. {
  84. const uint64 mask = (63);
  85. c = c % 64;
  86. c &= mask;
  87. return (n >> c) | (n << ((0 - c) & mask));
  88. }
  89. static inline double
  90. wa_fmax(double a, double b)
  91. {
  92. double c = fmax(a, b);
  93. if (c == 0 && a == b)
  94. return signbit(a) ? b : a;
  95. return c;
  96. }
  97. static inline double
  98. wa_fmin(double a, double b)
  99. {
  100. double c = fmin(a, b);
  101. if (c == 0 && a == b)
  102. return signbit(a) ? a : b;
  103. return c;
  104. }
  105. static inline uint32
  106. clz32(uint32 type)
  107. {
  108. uint32 num = 0;
  109. if (type == 0)
  110. return 32;
  111. while (!(type & 0x80000000)) {
  112. num++;
  113. type <<= 1;
  114. }
  115. return num;
  116. }
  117. static inline uint32
  118. clz64(uint64 type)
  119. {
  120. uint32 num = 0;
  121. if (type == 0)
  122. return 64;
  123. while (!(type & 0x8000000000000000LL)) {
  124. num++;
  125. type <<= 1;
  126. }
  127. return num;
  128. }
  129. static inline uint32
  130. ctz32(uint32 type)
  131. {
  132. uint32 num = 0;
  133. if (type == 0)
  134. return 32;
  135. while (!(type & 1)) {
  136. num++;
  137. type >>= 1;
  138. }
  139. return num;
  140. }
  141. static inline uint32
  142. ctz64(uint64 type)
  143. {
  144. uint32 num = 0;
  145. if (type == 0)
  146. return 64;
  147. while (!(type & 1)) {
  148. num++;
  149. type >>= 1;
  150. }
  151. return num;
  152. }
  153. static inline uint32
  154. popcount32(uint32 u)
  155. {
  156. uint32 ret = 0;
  157. while (u) {
  158. u = (u & (u - 1));
  159. ret++;
  160. }
  161. return ret;
  162. }
  163. static inline uint32
  164. popcount64(uint64 u)
  165. {
  166. uint32 ret = 0;
  167. while (u) {
  168. u = (u & (u - 1));
  169. ret++;
  170. }
  171. return ret;
  172. }
  173. static float
  174. local_copysignf(float x, float y)
  175. {
  176. union {
  177. float f;
  178. uint32_t i;
  179. } ux = { x }, uy = { y };
  180. ux.i &= 0x7fffffff;
  181. ux.i |= uy.i & 0x80000000;
  182. return ux.f;
  183. }
  184. static double
  185. local_copysign(double x, double y)
  186. {
  187. union {
  188. double f;
  189. uint64_t i;
  190. } ux = { x }, uy = { y };
  191. ux.i &= -1ULL / 2;
  192. ux.i |= uy.i & 1ULL << 63;
  193. return ux.f;
  194. }
  195. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  196. #define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr))
  197. #define LOAD_PTR(addr) (*(void **)(addr))
  198. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  199. static inline uint32
  200. LOAD_U32_WITH_2U16S(void *addr)
  201. {
  202. union {
  203. uint32 val;
  204. uint16 u16[2];
  205. } u;
  206. bh_assert(((uintptr_t)addr & 1) == 0);
  207. u.u16[0] = ((uint16 *)addr)[0];
  208. u.u16[1] = ((uint16 *)addr)[1];
  209. return u.val;
  210. }
  211. #if UINTPTR_MAX == UINT32_MAX
  212. #define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr))
  213. #elif UINTPTR_MAX == UINT64_MAX
  214. static inline void *
  215. LOAD_PTR(void *addr)
  216. {
  217. uintptr_t addr1 = (uintptr_t)addr;
  218. union {
  219. void *val;
  220. uint32 u32[2];
  221. uint16 u16[4];
  222. } u;
  223. bh_assert(((uintptr_t)addr & 1) == 0);
  224. if ((addr1 & (uintptr_t)7) == 0)
  225. return *(void **)addr;
  226. if ((addr1 & (uintptr_t)3) == 0) {
  227. u.u32[0] = ((uint32 *)addr)[0];
  228. u.u32[1] = ((uint32 *)addr)[1];
  229. }
  230. else {
  231. u.u16[0] = ((uint16 *)addr)[0];
  232. u.u16[1] = ((uint16 *)addr)[1];
  233. u.u16[2] = ((uint16 *)addr)[2];
  234. u.u16[3] = ((uint16 *)addr)[3];
  235. }
  236. return u.val;
  237. }
  238. #endif /* end of UINTPTR_MAX */
  239. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  240. #define read_uint32(p) \
  241. (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32)))
  242. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  243. do { \
  244. uint32 param_count = cur_func->param_count; \
  245. local_idx = read_uint32(frame_ip); \
  246. bh_assert(local_idx < param_count + cur_func->local_count); \
  247. local_offset = cur_func->local_offsets[local_idx]; \
  248. if (local_idx < param_count) \
  249. local_type = cur_func->param_types[local_idx]; \
  250. else \
  251. local_type = cur_func->local_types[local_idx - param_count]; \
  252. } while (0)
  253. #define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2))
  254. #define SET_OPERAND_I32(off, value) \
  255. do { \
  256. *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  257. } while (0)
  258. #define SET_OPERAND_F32(off, value) \
  259. do { \
  260. *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  261. } while (0)
  262. #define SET_OPERAND_I64(off, value) \
  263. do { \
  264. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  265. PUT_I64_TO_ADDR(addr_tmp, value); \
  266. } while (0)
  267. #define SET_OPERAND_F64(off, value) \
  268. do { \
  269. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  270. PUT_F64_TO_ADDR(addr_tmp, value); \
  271. } while (0)
  272. #define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value)
  273. #define GET_OPERAND_I32(type, off) \
  274. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  275. #define GET_OPERAND_F32(type, off) \
  276. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  277. #define GET_OPERAND_I64(type, off) \
  278. (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  279. #define GET_OPERAND_F64(type, off) \
  280. (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  281. #define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off)
  282. #define PUSH_I32(value) \
  283. do { \
  284. *(int32 *)(frame_lp + GET_OFFSET()) = value; \
  285. } while (0)
  286. #define PUSH_F32(value) \
  287. do { \
  288. *(float32 *)(frame_lp + GET_OFFSET()) = value; \
  289. } while (0)
  290. #define PUSH_I64(value) \
  291. do { \
  292. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  293. PUT_I64_TO_ADDR(addr_tmp, value); \
  294. } while (0)
  295. #define PUSH_F64(value) \
  296. do { \
  297. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  298. PUT_F64_TO_ADDR(addr_tmp, value); \
  299. } while (0)
  300. #define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET()))
  301. #define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET()))
  302. #define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()))
  303. #define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET()))
  304. #define SYNC_ALL_TO_FRAME() \
  305. do { \
  306. frame->ip = frame_ip; \
  307. } while (0)
  308. #define UPDATE_ALL_FROM_FRAME() \
  309. do { \
  310. frame_ip = frame->ip; \
  311. } while (0)
  312. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  313. #define UPDATE_FRAME_IP_END() (void)0
  314. #else
  315. #define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  316. #endif
  317. #define RECOVER_CONTEXT(new_frame) \
  318. do { \
  319. frame = (new_frame); \
  320. cur_func = frame->function; \
  321. prev_frame = frame->prev_frame; \
  322. frame_ip = frame->ip; \
  323. UPDATE_FRAME_IP_END(); \
  324. frame_lp = frame->lp; \
  325. } while (0)
  326. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  327. #define GET_OPCODE() opcode = *frame_ip++;
  328. #else
  329. #define GET_OPCODE() \
  330. opcode = *frame_ip; \
  331. frame_ip += 2;
  332. #endif
  333. #define DEF_OP_EQZ(ctype, src_op_type) \
  334. do { \
  335. SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \
  336. frame_ip += 4; \
  337. } while (0)
  338. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  339. do { \
  340. SET_OPERAND(I32, 4, \
  341. GET_OPERAND(src_type, src_op_type, 2) \
  342. cond GET_OPERAND(src_type, src_op_type, 0)); \
  343. frame_ip += 6; \
  344. } while (0)
  345. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  346. do { \
  347. SET_OPERAND( \
  348. src_op_type, 2, \
  349. (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \
  350. frame_ip += 4; \
  351. } while (0)
  352. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  353. do { \
  354. SET_OPERAND(src_op_type, 4, \
  355. GET_OPERAND(src_type1, src_op_type, 2) \
  356. operation GET_OPERAND(src_type2, src_op_type, 0)); \
  357. frame_ip += 6; \
  358. } while (0)
  359. #define DEF_OP_REINTERPRET(src_type, src_op_type) \
  360. do { \
  361. SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \
  362. frame_ip += 4; \
  363. } while (0)
  364. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  365. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  366. do { \
  367. SET_OPERAND(src_op_type, 4, \
  368. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  369. GET_OPERAND(src_type2, src_op_type, 0) % 32)); \
  370. frame_ip += 6; \
  371. } while (0)
  372. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  373. do { \
  374. SET_OPERAND(src_op_type, 4, \
  375. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  376. GET_OPERAND(src_type2, src_op_type, 0) % 64)); \
  377. frame_ip += 6; \
  378. } while (0)
  379. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  380. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  381. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  382. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  383. { \
  384. uint32 readv, sval; \
  385. \
  386. sval = POP_I32(); \
  387. addr = POP_I32(); \
  388. \
  389. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  390. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  391. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  392. \
  393. os_mutex_lock(&module->e->mem_lock); \
  394. readv = (uint32)(*(uint8 *)maddr); \
  395. *(uint8 *)maddr = (uint8)(readv op sval); \
  396. os_mutex_unlock(&module->e->mem_lock); \
  397. } \
  398. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  399. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  400. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  401. \
  402. os_mutex_lock(&module->e->mem_lock); \
  403. readv = (uint32)LOAD_U16(maddr); \
  404. STORE_U16(maddr, (uint16)(readv op sval)); \
  405. os_mutex_unlock(&module->e->mem_lock); \
  406. } \
  407. else { \
  408. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  409. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  410. \
  411. os_mutex_lock(&module->e->mem_lock); \
  412. readv = LOAD_I32(maddr); \
  413. STORE_U32(maddr, readv op sval); \
  414. os_mutex_unlock(&module->e->mem_lock); \
  415. } \
  416. PUSH_I32(readv); \
  417. break; \
  418. } \
  419. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  420. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  421. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  422. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  423. { \
  424. uint64 readv, sval; \
  425. \
  426. sval = (uint64)POP_I64(); \
  427. addr = POP_I32(); \
  428. \
  429. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  430. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  431. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  432. \
  433. os_mutex_lock(&module->e->mem_lock); \
  434. readv = (uint64)(*(uint8 *)maddr); \
  435. *(uint8 *)maddr = (uint8)(readv op sval); \
  436. os_mutex_unlock(&module->e->mem_lock); \
  437. } \
  438. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  439. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  440. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  441. \
  442. os_mutex_lock(&module->e->mem_lock); \
  443. readv = (uint64)LOAD_U16(maddr); \
  444. STORE_U16(maddr, (uint16)(readv op sval)); \
  445. os_mutex_unlock(&module->e->mem_lock); \
  446. } \
  447. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  448. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  449. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  450. \
  451. os_mutex_lock(&module->e->mem_lock); \
  452. readv = (uint64)LOAD_U32(maddr); \
  453. STORE_U32(maddr, (uint32)(readv op sval)); \
  454. os_mutex_unlock(&module->e->mem_lock); \
  455. } \
  456. else { \
  457. uint64 op_result; \
  458. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \
  459. CHECK_ATOMIC_MEMORY_ACCESS(8); \
  460. \
  461. os_mutex_lock(&module->e->mem_lock); \
  462. readv = (uint64)LOAD_I64(maddr); \
  463. op_result = readv op sval; \
  464. STORE_I64(maddr, op_result); \
  465. os_mutex_unlock(&module->e->mem_lock); \
  466. } \
  467. PUSH_I64(readv); \
  468. break; \
  469. }
  470. #define DEF_OP_MATH(src_type, src_op_type, method) \
  471. do { \
  472. SET_OPERAND(src_op_type, 2, \
  473. (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \
  474. frame_ip += 4; \
  475. } while (0)
  476. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  477. static dst_type func_name(src_type src_value, src_type src_min, \
  478. src_type src_max, dst_type dst_min, \
  479. dst_type dst_max, bool is_sign) \
  480. { \
  481. dst_type dst_value = 0; \
  482. if (!isnan(src_value)) { \
  483. if (src_value <= src_min) \
  484. dst_value = dst_min; \
  485. else if (src_value >= src_max) \
  486. dst_value = dst_max; \
  487. else { \
  488. if (is_sign) \
  489. dst_value = (dst_type)(signed_type)src_value; \
  490. else \
  491. dst_value = (dst_type)src_value; \
  492. } \
  493. } \
  494. return dst_value; \
  495. }
  496. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  497. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  498. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  499. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  500. static bool
  501. trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  502. float32 src_min, float32 src_max, bool saturating, bool is_i32,
  503. bool is_sign)
  504. {
  505. float32 src_value = GET_OPERAND(float32, F32, 0);
  506. uint64 dst_value_i64;
  507. uint32 dst_value_i32;
  508. if (!saturating) {
  509. if (isnan(src_value)) {
  510. wasm_set_exception(module, "invalid conversion to integer");
  511. return true;
  512. }
  513. else if (src_value <= src_min || src_value >= src_max) {
  514. wasm_set_exception(module, "integer overflow");
  515. return true;
  516. }
  517. }
  518. if (is_i32) {
  519. uint32 dst_min = is_sign ? INT32_MIN : 0;
  520. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  521. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  522. dst_max, is_sign);
  523. SET_OPERAND(I32, 2, dst_value_i32);
  524. }
  525. else {
  526. uint64 dst_min = is_sign ? INT64_MIN : 0;
  527. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  528. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  529. dst_max, is_sign);
  530. SET_OPERAND(I64, 2, dst_value_i64);
  531. }
  532. return false;
  533. }
  534. static bool
  535. trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  536. float64 src_min, float64 src_max, bool saturating, bool is_i32,
  537. bool is_sign)
  538. {
  539. float64 src_value = GET_OPERAND(float64, F64, 0);
  540. uint64 dst_value_i64;
  541. uint32 dst_value_i32;
  542. if (!saturating) {
  543. if (isnan(src_value)) {
  544. wasm_set_exception(module, "invalid conversion to integer");
  545. return true;
  546. }
  547. else if (src_value <= src_min || src_value >= src_max) {
  548. wasm_set_exception(module, "integer overflow");
  549. return true;
  550. }
  551. }
  552. if (is_i32) {
  553. uint32 dst_min = is_sign ? INT32_MIN : 0;
  554. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  555. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  556. dst_max, is_sign);
  557. SET_OPERAND(I32, 2, dst_value_i32);
  558. }
  559. else {
  560. uint64 dst_min = is_sign ? INT64_MIN : 0;
  561. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  562. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  563. dst_max, is_sign);
  564. SET_OPERAND(I64, 2, dst_value_i64);
  565. }
  566. return false;
  567. }
  568. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  569. do { \
  570. if (trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \
  571. is_i32, is_sign)) \
  572. goto got_exception; \
  573. frame_ip += 4; \
  574. } while (0)
  575. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  576. do { \
  577. if (trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \
  578. is_i32, is_sign)) \
  579. goto got_exception; \
  580. frame_ip += 4; \
  581. } while (0)
  582. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  583. do { \
  584. (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \
  585. is_i32, is_sign); \
  586. frame_ip += 4; \
  587. } while (0)
  588. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  589. do { \
  590. (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \
  591. is_i32, is_sign); \
  592. frame_ip += 4; \
  593. } while (0)
  594. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  595. do { \
  596. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  597. PUSH_##dst_op_type(value); \
  598. } while (0)
  599. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  600. #define CELL_SIZE sizeof(uint8)
  601. #else
  602. #define CELL_SIZE (sizeof(uint8) * 2)
  603. #endif
  604. static bool
  605. copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity,
  606. uint32 total_cell_num, const uint8 *cells,
  607. const int16 *src_offsets, const uint16 *dst_offsets)
  608. {
  609. /* To avoid the overlap issue between src offsets and dst offset,
  610. * we use 2 steps to do the copy. First step, copy the src values
  611. * to a tmp buf. Second step, copy the values from tmp buf to dst.
  612. */
  613. uint32 buf[16] = { 0 }, i;
  614. uint32 *tmp_buf = buf;
  615. uint8 cell;
  616. int16 src, buf_index = 0;
  617. uint16 dst;
  618. /* Allocate memory if the buf is not large enough */
  619. if (total_cell_num > sizeof(buf) / sizeof(uint32)) {
  620. uint64 total_size = sizeof(uint32) * (uint64)total_cell_num;
  621. if (total_size >= UINT32_MAX
  622. || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) {
  623. wasm_set_exception(module, "allocate memory failed");
  624. return false;
  625. }
  626. }
  627. /* 1) Copy values from src to tmp buf */
  628. for (i = 0; i < arity; i++) {
  629. cell = cells[i * CELL_SIZE];
  630. src = src_offsets[i];
  631. if (cell == 1)
  632. tmp_buf[buf_index] = frame_lp[src];
  633. else {
  634. tmp_buf[buf_index] = frame_lp[src];
  635. tmp_buf[buf_index + 1] = frame_lp[src + 1];
  636. }
  637. buf_index += cell;
  638. }
  639. /* 2) Copy values from tmp buf to dest */
  640. buf_index = 0;
  641. for (i = 0; i < arity; i++) {
  642. cell = cells[i * CELL_SIZE];
  643. dst = dst_offsets[i];
  644. if (cell == 1)
  645. frame_lp[dst] = tmp_buf[buf_index];
  646. else {
  647. frame_lp[dst] = tmp_buf[buf_index];
  648. frame_lp[dst + 1] = tmp_buf[buf_index + 1];
  649. }
  650. buf_index += cell;
  651. }
  652. if (tmp_buf != buf) {
  653. wasm_runtime_free(tmp_buf);
  654. }
  655. return true;
  656. }
  657. #define RECOVER_BR_INFO() \
  658. do { \
  659. uint32 arity; \
  660. /* read arity */ \
  661. arity = read_uint32(frame_ip); \
  662. if (arity) { \
  663. uint32 total_cell; \
  664. uint16 *dst_offsets = NULL; \
  665. uint8 *cells; \
  666. int16 *src_offsets = NULL; \
  667. /* read total cell num */ \
  668. total_cell = read_uint32(frame_ip); \
  669. /* cells */ \
  670. cells = (uint8 *)frame_ip; \
  671. frame_ip += arity * CELL_SIZE; \
  672. /* src offsets */ \
  673. src_offsets = (int16 *)frame_ip; \
  674. frame_ip += arity * sizeof(int16); \
  675. /* dst offsets */ \
  676. dst_offsets = (uint16 *)frame_ip; \
  677. frame_ip += arity * sizeof(uint16); \
  678. if (arity == 1) { \
  679. if (cells[0] == 1) \
  680. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  681. else if (cells[0] == 2) { \
  682. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  683. frame_lp[dst_offsets[0] + 1] = \
  684. frame_lp[src_offsets[0] + 1]; \
  685. } \
  686. } \
  687. else { \
  688. if (!copy_stack_values(module, frame_lp, arity, total_cell, \
  689. cells, src_offsets, dst_offsets)) \
  690. goto got_exception; \
  691. } \
  692. } \
  693. frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
  694. } while (0)
  695. #define SKIP_BR_INFO() \
  696. do { \
  697. uint32 arity; \
  698. /* read and skip arity */ \
  699. arity = read_uint32(frame_ip); \
  700. if (arity) { \
  701. /* skip total cell num */ \
  702. frame_ip += sizeof(uint32); \
  703. /* skip cells, src offsets and dst offsets */ \
  704. frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \
  705. } \
  706. /* skip target address */ \
  707. frame_ip += sizeof(uint8 *); \
  708. } while (0)
  709. static inline int32
  710. sign_ext_8_32(int8 val)
  711. {
  712. if (val & 0x80)
  713. return (int32)val | (int32)0xffffff00;
  714. return val;
  715. }
  716. static inline int32
  717. sign_ext_16_32(int16 val)
  718. {
  719. if (val & 0x8000)
  720. return (int32)val | (int32)0xffff0000;
  721. return val;
  722. }
  723. static inline int64
  724. sign_ext_8_64(int8 val)
  725. {
  726. if (val & 0x80)
  727. return (int64)val | (int64)0xffffffffffffff00LL;
  728. return val;
  729. }
  730. static inline int64
  731. sign_ext_16_64(int16 val)
  732. {
  733. if (val & 0x8000)
  734. return (int64)val | (int64)0xffffffffffff0000LL;
  735. return val;
  736. }
  737. static inline int64
  738. sign_ext_32_64(int32 val)
  739. {
  740. if (val & (int32)0x80000000)
  741. return (int64)val | (int64)0xffffffff00000000LL;
  742. return val;
  743. }
  744. static inline void
  745. word_copy(uint32 *dest, uint32 *src, unsigned num)
  746. {
  747. bh_assert(dest != NULL);
  748. bh_assert(src != NULL);
  749. bh_assert(num > 0);
  750. if (dest != src) {
  751. /* No overlap buffer */
  752. bh_assert(!((src < dest) && (dest < src + num)));
  753. for (; num > 0; num--)
  754. *dest++ = *src++;
  755. }
  756. }
  757. static inline WASMInterpFrame *
  758. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  759. {
  760. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  761. if (frame) {
  762. frame->prev_frame = prev_frame;
  763. #if WASM_ENABLE_PERF_PROFILING != 0
  764. frame->time_started = os_time_get_boot_microsecond();
  765. #endif
  766. }
  767. else {
  768. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  769. "wasm operand stack overflow");
  770. }
  771. return frame;
  772. }
  773. static inline void
  774. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  775. {
  776. #if WASM_ENABLE_PERF_PROFILING != 0
  777. if (frame->function) {
  778. frame->function->total_exec_time +=
  779. os_time_get_boot_microsecond() - frame->time_started;
  780. frame->function->total_exec_cnt++;
  781. }
  782. #endif
  783. wasm_exec_env_free_wasm_frame(exec_env, frame);
  784. }
  785. void
  786. wasm_interp_restore_wasm_frame(WASMExecEnv *exec_env)
  787. {
  788. WASMInterpFrame *cur_frame, *prev_frame;
  789. cur_frame = wasm_exec_env_get_cur_frame(exec_env);
  790. while (cur_frame) {
  791. prev_frame = cur_frame->prev_frame;
  792. if (cur_frame->ip) {
  793. /* FREE_FRAME just set the wasm_stack.s.top pointer, we only need to
  794. * call it once */
  795. FREE_FRAME(exec_env, cur_frame);
  796. break;
  797. }
  798. cur_frame = prev_frame;
  799. }
  800. wasm_exec_env_set_cur_frame(exec_env, cur_frame);
  801. }
  802. static void
  803. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  804. WASMExecEnv *exec_env,
  805. WASMFunctionInstance *cur_func,
  806. WASMInterpFrame *prev_frame)
  807. {
  808. WASMFunctionImport *func_import = cur_func->u.func_import;
  809. unsigned local_cell_num = 2;
  810. WASMInterpFrame *frame;
  811. uint32 argv_ret[2], cur_func_index;
  812. void *native_func_pointer = NULL;
  813. bool ret;
  814. if (!(frame = ALLOC_FRAME(exec_env,
  815. wasm_interp_interp_frame_size(local_cell_num),
  816. prev_frame)))
  817. return;
  818. frame->function = cur_func;
  819. frame->ip = NULL;
  820. frame->lp = frame->operand;
  821. wasm_exec_env_set_cur_frame(exec_env, frame);
  822. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  823. bh_assert(cur_func_index < module_inst->module->import_function_count);
  824. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  825. if (!native_func_pointer) {
  826. char buf[128];
  827. snprintf(buf, sizeof(buf),
  828. "failed to call unlinked import function (%s, %s)",
  829. func_import->module_name, func_import->field_name);
  830. wasm_set_exception((WASMModuleInstance *)module_inst, buf);
  831. return;
  832. }
  833. if (func_import->call_conv_wasm_c_api) {
  834. ret = wasm_runtime_invoke_c_api_native(
  835. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  836. func_import->func_type, cur_func->param_cell_num, frame->lp,
  837. func_import->wasm_c_api_with_env, func_import->attachment);
  838. if (ret) {
  839. argv_ret[0] = frame->lp[0];
  840. argv_ret[1] = frame->lp[1];
  841. }
  842. }
  843. else if (!func_import->call_conv_raw) {
  844. ret = wasm_runtime_invoke_native(
  845. exec_env, native_func_pointer, func_import->func_type,
  846. func_import->signature, func_import->attachment, frame->lp,
  847. cur_func->param_cell_num, argv_ret);
  848. }
  849. else {
  850. ret = wasm_runtime_invoke_native_raw(
  851. exec_env, native_func_pointer, func_import->func_type,
  852. func_import->signature, func_import->attachment, frame->lp,
  853. cur_func->param_cell_num, argv_ret);
  854. }
  855. if (!ret)
  856. return;
  857. if (cur_func->ret_cell_num == 1) {
  858. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  859. }
  860. else if (cur_func->ret_cell_num == 2) {
  861. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  862. prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
  863. }
  864. FREE_FRAME(exec_env, frame);
  865. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  866. }
  867. #if WASM_ENABLE_MULTI_MODULE != 0
  868. static void
  869. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  870. WASMExecEnv *exec_env,
  871. WASMFunctionInstance *cur_func,
  872. WASMInterpFrame *prev_frame);
  873. static void
  874. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  875. WASMExecEnv *exec_env,
  876. WASMFunctionInstance *cur_func,
  877. WASMInterpFrame *prev_frame)
  878. {
  879. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  880. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  881. WASMFunctionImport *func_import = cur_func->u.func_import;
  882. uint8 *ip = prev_frame->ip;
  883. char buf[128];
  884. WASMExecEnv *sub_module_exec_env = NULL;
  885. uint32 aux_stack_origin_boundary = 0;
  886. uint32 aux_stack_origin_bottom = 0;
  887. if (!sub_func_inst) {
  888. snprintf(buf, sizeof(buf),
  889. "failed to call unlinked import function (%s, %s)",
  890. func_import->module_name, func_import->field_name);
  891. wasm_set_exception(module_inst, buf);
  892. return;
  893. }
  894. /* Switch exec_env but keep using the same one by replacing necessary
  895. * variables */
  896. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  897. (WASMModuleInstanceCommon *)sub_module_inst);
  898. if (!sub_module_exec_env) {
  899. wasm_set_exception(module_inst, "create singleton exec_env failed");
  900. return;
  901. }
  902. /* - module_inst */
  903. exec_env->module_inst = (WASMModuleInstanceCommon *)sub_module_inst;
  904. /* - aux_stack_boundary */
  905. aux_stack_origin_boundary = exec_env->aux_stack_boundary.boundary;
  906. exec_env->aux_stack_boundary.boundary =
  907. sub_module_exec_env->aux_stack_boundary.boundary;
  908. /* - aux_stack_bottom */
  909. aux_stack_origin_bottom = exec_env->aux_stack_bottom.bottom;
  910. exec_env->aux_stack_bottom.bottom =
  911. sub_module_exec_env->aux_stack_bottom.bottom;
  912. /* set ip NULL to make call_func_bytecode return after executing
  913. this function */
  914. prev_frame->ip = NULL;
  915. /* call function of sub-module*/
  916. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  917. prev_frame);
  918. /* restore ip and other replaced */
  919. prev_frame->ip = ip;
  920. exec_env->aux_stack_boundary.boundary = aux_stack_origin_boundary;
  921. exec_env->aux_stack_bottom.bottom = aux_stack_origin_bottom;
  922. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  923. /* transfer exception if it is thrown */
  924. if (wasm_get_exception(sub_module_inst)) {
  925. bh_memcpy_s(module_inst->cur_exception,
  926. sizeof(module_inst->cur_exception),
  927. sub_module_inst->cur_exception,
  928. sizeof(sub_module_inst->cur_exception));
  929. }
  930. }
  931. #endif
  932. #if WASM_ENABLE_THREAD_MGR != 0
  933. #define CHECK_SUSPEND_FLAGS() \
  934. do { \
  935. if (exec_env->suspend_flags.flags != 0) { \
  936. if (exec_env->suspend_flags.flags & 0x01) { \
  937. /* terminate current thread */ \
  938. return; \
  939. } \
  940. /* TODO: support suspend and breakpoint */ \
  941. } \
  942. } while (0)
  943. #endif
  944. #if WASM_ENABLE_OPCODE_COUNTER != 0
  945. typedef struct OpcodeInfo {
  946. char *name;
  947. uint64 count;
  948. } OpcodeInfo;
  949. /* clang-format off */
  950. #define HANDLE_OPCODE(op) \
  951. { \
  952. #op, 0 \
  953. }
  954. DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
  955. #undef HANDLE_OPCODE
  956. /* clang-format on */
  957. static void
  958. wasm_interp_dump_op_count()
  959. {
  960. uint32 i;
  961. uint64 total_count = 0;
  962. for (i = 0; i < WASM_OP_IMPDEP; i++)
  963. total_count += opcode_table[i].count;
  964. printf("total opcode count: %ld\n", total_count);
  965. for (i = 0; i < WASM_OP_IMPDEP; i++)
  966. if (opcode_table[i].count > 0)
  967. printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
  968. opcode_table[i].count,
  969. opcode_table[i].count * 100.0f / total_count);
  970. }
  971. #endif
  972. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  973. /* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
  974. #if WASM_ENABLE_OPCODE_COUNTER != 0
  975. #define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
  976. #else
  977. #define HANDLE_OP(opcode) HANDLE_##opcode:
  978. #endif
  979. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  980. #define FETCH_OPCODE_AND_DISPATCH() \
  981. do { \
  982. const void *p_label_addr = *(void **)frame_ip; \
  983. frame_ip += sizeof(void *); \
  984. goto *p_label_addr; \
  985. } while (0)
  986. #else
  987. #define FETCH_OPCODE_AND_DISPATCH() \
  988. do { \
  989. const void *p_label_addr = label_base + *(int16 *)frame_ip; \
  990. frame_ip += sizeof(int16); \
  991. goto *p_label_addr; \
  992. } while (0)
  993. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  994. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  995. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  996. #define HANDLE_OP(opcode) case opcode:
  997. #define HANDLE_OP_END() continue
  998. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  999. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1000. static void **global_handle_table;
  1001. #endif
  1002. static inline uint8 *
  1003. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1004. {
  1005. #if WASM_ENABLE_MULTI_MODULE == 0
  1006. return global_data + global->data_offset;
  1007. #else
  1008. return global->import_global_inst
  1009. ? global->import_module_inst->global_data
  1010. + global->import_global_inst->data_offset
  1011. : global_data + global->data_offset;
  1012. #endif
  1013. }
  1014. static void
  1015. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1016. WASMExecEnv *exec_env,
  1017. WASMFunctionInstance *cur_func,
  1018. WASMInterpFrame *prev_frame)
  1019. {
  1020. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1021. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1022. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1023. || WASM_ENABLE_BULK_MEMORY != 0
  1024. uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0;
  1025. uint32 linear_mem_size =
  1026. memory ? num_bytes_per_page * memory->cur_page_count : 0;
  1027. #endif
  1028. uint8 *global_data = module->global_data;
  1029. WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
  1030. WASMGlobalInstance *global;
  1031. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1032. WASMInterpFrame *frame = NULL;
  1033. /* Points to this special opcode so as to jump to the
  1034. * call_method_from_entry. */
  1035. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1036. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1037. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1038. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1039. /* cache of label base addr */
  1040. register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
  1041. #endif
  1042. #endif
  1043. uint8 *frame_ip_end = frame_ip + 1;
  1044. uint32 cond, count, fidx, tidx, frame_size = 0;
  1045. uint64 all_cell_num = 0;
  1046. int16 addr1, addr2, addr_ret = 0;
  1047. int32 didx, val;
  1048. uint8 *maddr = NULL;
  1049. uint32 local_idx, local_offset, global_idx;
  1050. uint8 opcode, local_type, *global_addr;
  1051. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1052. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1053. DEFINE_GOTO_TABLE(const void *, handle_table);
  1054. #undef HANDLE_OPCODE
  1055. if (exec_env == NULL) {
  1056. global_handle_table = (void **)handle_table;
  1057. return;
  1058. }
  1059. #endif
  1060. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1061. while (frame_ip < frame_ip_end) {
  1062. opcode = *frame_ip++;
  1063. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1064. frame_ip++;
  1065. #endif
  1066. switch (opcode) {
  1067. #else
  1068. goto *handle_table[WASM_OP_IMPDEP];
  1069. #endif
  1070. /* control instructions */
  1071. HANDLE_OP(WASM_OP_UNREACHABLE)
  1072. {
  1073. wasm_set_exception(module, "unreachable");
  1074. goto got_exception;
  1075. }
  1076. HANDLE_OP(WASM_OP_IF)
  1077. {
  1078. cond = (uint32)POP_I32();
  1079. if (cond == 0) {
  1080. uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
  1081. if (else_addr == NULL) {
  1082. frame_ip =
  1083. (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
  1084. }
  1085. else {
  1086. frame_ip = else_addr;
  1087. }
  1088. }
  1089. else {
  1090. frame_ip += sizeof(uint8 *) * 2;
  1091. }
  1092. HANDLE_OP_END();
  1093. }
  1094. HANDLE_OP(WASM_OP_ELSE)
  1095. {
  1096. frame_ip = (uint8 *)LOAD_PTR(frame_ip);
  1097. HANDLE_OP_END();
  1098. }
  1099. HANDLE_OP(WASM_OP_BR)
  1100. {
  1101. #if WASM_ENABLE_THREAD_MGR != 0
  1102. CHECK_SUSPEND_FLAGS();
  1103. #endif
  1104. recover_br_info:
  1105. RECOVER_BR_INFO();
  1106. HANDLE_OP_END();
  1107. }
  1108. HANDLE_OP(WASM_OP_BR_IF)
  1109. {
  1110. #if WASM_ENABLE_THREAD_MGR != 0
  1111. CHECK_SUSPEND_FLAGS();
  1112. #endif
  1113. cond = frame_lp[GET_OFFSET()];
  1114. if (cond)
  1115. goto recover_br_info;
  1116. else
  1117. SKIP_BR_INFO();
  1118. HANDLE_OP_END();
  1119. }
  1120. HANDLE_OP(WASM_OP_BR_TABLE)
  1121. {
  1122. uint32 arity, br_item_size;
  1123. #if WASM_ENABLE_THREAD_MGR != 0
  1124. CHECK_SUSPEND_FLAGS();
  1125. #endif
  1126. count = read_uint32(frame_ip);
  1127. didx = GET_OPERAND(uint32, I32, 0);
  1128. frame_ip += 2;
  1129. if (!(didx >= 0 && (uint32)didx < count))
  1130. didx = count;
  1131. /* all br items must have the same arity and item size,
  1132. so we only calculate the first item size */
  1133. arity = LOAD_U32_WITH_2U16S(frame_ip);
  1134. br_item_size = sizeof(uint32); /* arity */
  1135. if (arity) {
  1136. /* total cell num */
  1137. br_item_size += sizeof(uint32);
  1138. /* cells, src offsets and dst offsets */
  1139. br_item_size +=
  1140. (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
  1141. }
  1142. /* target address */
  1143. br_item_size += sizeof(uint8 *);
  1144. frame_ip += br_item_size * didx;
  1145. goto recover_br_info;
  1146. }
  1147. HANDLE_OP(WASM_OP_RETURN)
  1148. {
  1149. uint32 ret_idx;
  1150. WASMType *func_type;
  1151. uint32 off, ret_offset;
  1152. uint8 *ret_types;
  1153. if (cur_func->is_import_func)
  1154. func_type = cur_func->u.func_import->func_type;
  1155. else
  1156. func_type = cur_func->u.func->func_type;
  1157. /* types of each return value */
  1158. ret_types = func_type->types + func_type->param_count;
  1159. ret_offset = prev_frame->ret_offset;
  1160. for (ret_idx = 0,
  1161. off = sizeof(int16) * (func_type->result_count - 1);
  1162. ret_idx < func_type->result_count;
  1163. ret_idx++, off -= sizeof(int16)) {
  1164. if (ret_types[ret_idx] == VALUE_TYPE_I64
  1165. || ret_types[ret_idx] == VALUE_TYPE_F64) {
  1166. PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
  1167. GET_OPERAND(uint64, I64, off));
  1168. ret_offset += 2;
  1169. }
  1170. else {
  1171. prev_frame->lp[ret_offset] =
  1172. GET_OPERAND(uint32, I32, off);
  1173. ret_offset++;
  1174. }
  1175. }
  1176. goto return_func;
  1177. }
  1178. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  1179. #if WASM_ENABLE_TAIL_CALL != 0
  1180. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  1181. #endif
  1182. {
  1183. WASMType *cur_type, *cur_func_type;
  1184. WASMTableInstance *tbl_inst;
  1185. uint32 tbl_idx;
  1186. #if WASM_ENABLE_TAIL_CALL != 0
  1187. GET_OPCODE();
  1188. #endif
  1189. #if WASM_ENABLE_THREAD_MGR != 0
  1190. CHECK_SUSPEND_FLAGS();
  1191. #endif
  1192. tidx = read_uint32(frame_ip);
  1193. cur_type = module->module->types[tidx];
  1194. tbl_idx = read_uint32(frame_ip);
  1195. bh_assert(tbl_idx < module->table_count);
  1196. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1197. val = GET_OPERAND(uint32, I32, 0);
  1198. frame_ip += 2;
  1199. if ((uint32)val >= tbl_inst->cur_size) {
  1200. wasm_set_exception(module, "undefined element");
  1201. goto got_exception;
  1202. }
  1203. fidx = tbl_inst->elems[val];
  1204. if (fidx == NULL_REF) {
  1205. wasm_set_exception(module, "uninitialized element");
  1206. goto got_exception;
  1207. }
  1208. /*
  1209. * we might be using a table injected by host or
  1210. * another module. in that case, we don't validate
  1211. * the elem value while loading
  1212. */
  1213. if (fidx >= module->e->function_count) {
  1214. wasm_set_exception(module, "unknown function");
  1215. goto got_exception;
  1216. }
  1217. /* always call module own functions */
  1218. cur_func = module->e->functions + fidx;
  1219. if (cur_func->is_import_func)
  1220. cur_func_type = cur_func->u.func_import->func_type;
  1221. else
  1222. cur_func_type = cur_func->u.func->func_type;
  1223. if (cur_type != cur_func_type) {
  1224. wasm_set_exception(module, "indirect call type mismatch");
  1225. goto got_exception;
  1226. }
  1227. #if WASM_ENABLE_TAIL_CALL != 0
  1228. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  1229. goto call_func_from_return_call;
  1230. #endif
  1231. goto call_func_from_interp;
  1232. }
  1233. /* parametric instructions */
  1234. HANDLE_OP(WASM_OP_SELECT)
  1235. {
  1236. cond = frame_lp[GET_OFFSET()];
  1237. addr1 = GET_OFFSET();
  1238. addr2 = GET_OFFSET();
  1239. addr_ret = GET_OFFSET();
  1240. if (!cond) {
  1241. if (addr_ret != addr1)
  1242. frame_lp[addr_ret] = frame_lp[addr1];
  1243. }
  1244. else {
  1245. if (addr_ret != addr2)
  1246. frame_lp[addr_ret] = frame_lp[addr2];
  1247. }
  1248. HANDLE_OP_END();
  1249. }
  1250. HANDLE_OP(WASM_OP_SELECT_64)
  1251. {
  1252. cond = frame_lp[GET_OFFSET()];
  1253. addr1 = GET_OFFSET();
  1254. addr2 = GET_OFFSET();
  1255. addr_ret = GET_OFFSET();
  1256. if (!cond) {
  1257. if (addr_ret != addr1)
  1258. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1259. GET_I64_FROM_ADDR(frame_lp + addr1));
  1260. }
  1261. else {
  1262. if (addr_ret != addr2)
  1263. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1264. GET_I64_FROM_ADDR(frame_lp + addr2));
  1265. }
  1266. HANDLE_OP_END();
  1267. }
  1268. #if WASM_ENABLE_REF_TYPES != 0
  1269. HANDLE_OP(WASM_OP_TABLE_GET)
  1270. {
  1271. uint32 tbl_idx, elem_idx;
  1272. WASMTableInstance *tbl_inst;
  1273. tbl_idx = read_uint32(frame_ip);
  1274. bh_assert(tbl_idx < module->table_count);
  1275. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1276. elem_idx = POP_I32();
  1277. if (elem_idx >= tbl_inst->cur_size) {
  1278. wasm_set_exception(module, "out of bounds table access");
  1279. goto got_exception;
  1280. }
  1281. PUSH_I32(tbl_inst->elems[elem_idx]);
  1282. HANDLE_OP_END();
  1283. }
  1284. HANDLE_OP(WASM_OP_TABLE_SET)
  1285. {
  1286. uint32 tbl_idx, elem_idx, elem_val;
  1287. WASMTableInstance *tbl_inst;
  1288. tbl_idx = read_uint32(frame_ip);
  1289. bh_assert(tbl_idx < module->table_count);
  1290. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1291. elem_val = POP_I32();
  1292. elem_idx = POP_I32();
  1293. if (elem_idx >= tbl_inst->cur_size) {
  1294. wasm_set_exception(module, "out of bounds table access");
  1295. goto got_exception;
  1296. }
  1297. tbl_inst->elems[elem_idx] = elem_val;
  1298. HANDLE_OP_END();
  1299. }
  1300. HANDLE_OP(WASM_OP_REF_NULL)
  1301. {
  1302. PUSH_I32(NULL_REF);
  1303. HANDLE_OP_END();
  1304. }
  1305. HANDLE_OP(WASM_OP_REF_IS_NULL)
  1306. {
  1307. uint32 ref_val;
  1308. ref_val = POP_I32();
  1309. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  1310. HANDLE_OP_END();
  1311. }
  1312. HANDLE_OP(WASM_OP_REF_FUNC)
  1313. {
  1314. uint32 func_idx = read_uint32(frame_ip);
  1315. PUSH_I32(func_idx);
  1316. HANDLE_OP_END();
  1317. }
  1318. #endif /* WASM_ENABLE_REF_TYPES */
  1319. /* variable instructions */
  1320. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  1321. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  1322. {
  1323. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1324. local_offset = *frame_ip++;
  1325. #else
  1326. /* clang-format off */
  1327. local_offset = *frame_ip;
  1328. frame_ip += 2;
  1329. /* clang-format on */
  1330. #endif
  1331. *(uint32 *)(frame_lp + local_offset) =
  1332. GET_OPERAND(uint32, I32, 0);
  1333. frame_ip += 2;
  1334. HANDLE_OP_END();
  1335. }
  1336. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  1337. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  1338. {
  1339. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1340. local_offset = *frame_ip++;
  1341. #else
  1342. /* clang-format off */
  1343. local_offset = *frame_ip;
  1344. frame_ip += 2;
  1345. /* clang-format on */
  1346. #endif
  1347. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  1348. GET_OPERAND(uint64, I64, 0));
  1349. frame_ip += 2;
  1350. HANDLE_OP_END();
  1351. }
  1352. HANDLE_OP(WASM_OP_GET_GLOBAL)
  1353. {
  1354. global_idx = read_uint32(frame_ip);
  1355. bh_assert(global_idx < module->e->global_count);
  1356. global = globals + global_idx;
  1357. global_addr = get_global_addr(global_data, global);
  1358. addr_ret = GET_OFFSET();
  1359. frame_lp[addr_ret] = *(uint32 *)global_addr;
  1360. HANDLE_OP_END();
  1361. }
  1362. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  1363. {
  1364. global_idx = read_uint32(frame_ip);
  1365. bh_assert(global_idx < module->e->global_count);
  1366. global = globals + global_idx;
  1367. global_addr = get_global_addr(global_data, global);
  1368. addr_ret = GET_OFFSET();
  1369. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1370. GET_I64_FROM_ADDR((uint32 *)global_addr));
  1371. HANDLE_OP_END();
  1372. }
  1373. HANDLE_OP(WASM_OP_SET_GLOBAL)
  1374. {
  1375. global_idx = read_uint32(frame_ip);
  1376. bh_assert(global_idx < module->e->global_count);
  1377. global = globals + global_idx;
  1378. global_addr = get_global_addr(global_data, global);
  1379. addr1 = GET_OFFSET();
  1380. *(int32 *)global_addr = frame_lp[addr1];
  1381. HANDLE_OP_END();
  1382. }
  1383. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  1384. {
  1385. uint32 aux_stack_top;
  1386. global_idx = read_uint32(frame_ip);
  1387. bh_assert(global_idx < module->e->global_count);
  1388. global = globals + global_idx;
  1389. global_addr = get_global_addr(global_data, global);
  1390. aux_stack_top = frame_lp[GET_OFFSET()];
  1391. if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
  1392. wasm_set_exception(module, "wasm auxiliary stack overflow");
  1393. goto got_exception;
  1394. }
  1395. if (aux_stack_top > exec_env->aux_stack_bottom.bottom) {
  1396. wasm_set_exception(module,
  1397. "wasm auxiliary stack underflow");
  1398. goto got_exception;
  1399. }
  1400. *(int32 *)global_addr = aux_stack_top;
  1401. #if WASM_ENABLE_MEMORY_PROFILING != 0
  1402. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  1403. uint32 aux_stack_used = module->module->aux_stack_bottom
  1404. - *(uint32 *)global_addr;
  1405. if (aux_stack_used > module->e->max_aux_stack_used)
  1406. module->e->max_aux_stack_used = aux_stack_used;
  1407. }
  1408. #endif
  1409. HANDLE_OP_END();
  1410. }
  1411. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  1412. {
  1413. global_idx = read_uint32(frame_ip);
  1414. bh_assert(global_idx < module->e->global_count);
  1415. global = globals + global_idx;
  1416. global_addr = get_global_addr(global_data, global);
  1417. addr1 = GET_OFFSET();
  1418. PUT_I64_TO_ADDR((uint32 *)global_addr,
  1419. GET_I64_FROM_ADDR(frame_lp + addr1));
  1420. HANDLE_OP_END();
  1421. }
  1422. /* memory load instructions */
  1423. HANDLE_OP(WASM_OP_I32_LOAD)
  1424. {
  1425. uint32 offset, addr;
  1426. offset = read_uint32(frame_ip);
  1427. addr = GET_OPERAND(uint32, I32, 0);
  1428. frame_ip += 2;
  1429. addr_ret = GET_OFFSET();
  1430. CHECK_MEMORY_OVERFLOW(4);
  1431. frame_lp[addr_ret] = LOAD_I32(maddr);
  1432. HANDLE_OP_END();
  1433. }
  1434. HANDLE_OP(WASM_OP_I64_LOAD)
  1435. {
  1436. uint32 offset, addr;
  1437. offset = read_uint32(frame_ip);
  1438. addr = GET_OPERAND(uint32, I32, 0);
  1439. frame_ip += 2;
  1440. addr_ret = GET_OFFSET();
  1441. CHECK_MEMORY_OVERFLOW(8);
  1442. PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
  1443. HANDLE_OP_END();
  1444. }
  1445. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  1446. {
  1447. uint32 offset, addr;
  1448. offset = read_uint32(frame_ip);
  1449. addr = GET_OPERAND(uint32, I32, 0);
  1450. frame_ip += 2;
  1451. addr_ret = GET_OFFSET();
  1452. CHECK_MEMORY_OVERFLOW(1);
  1453. frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
  1454. HANDLE_OP_END();
  1455. }
  1456. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  1457. {
  1458. uint32 offset, addr;
  1459. offset = read_uint32(frame_ip);
  1460. addr = GET_OPERAND(uint32, I32, 0);
  1461. frame_ip += 2;
  1462. addr_ret = GET_OFFSET();
  1463. CHECK_MEMORY_OVERFLOW(1);
  1464. frame_lp[addr_ret] = (uint32)(*(uint8 *)maddr);
  1465. HANDLE_OP_END();
  1466. }
  1467. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  1468. {
  1469. uint32 offset, addr;
  1470. offset = read_uint32(frame_ip);
  1471. addr = GET_OPERAND(uint32, I32, 0);
  1472. frame_ip += 2;
  1473. addr_ret = GET_OFFSET();
  1474. CHECK_MEMORY_OVERFLOW(2);
  1475. frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
  1476. HANDLE_OP_END();
  1477. }
  1478. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  1479. {
  1480. uint32 offset, addr;
  1481. offset = read_uint32(frame_ip);
  1482. addr = GET_OPERAND(uint32, I32, 0);
  1483. frame_ip += 2;
  1484. addr_ret = GET_OFFSET();
  1485. CHECK_MEMORY_OVERFLOW(2);
  1486. frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
  1487. HANDLE_OP_END();
  1488. }
  1489. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  1490. {
  1491. uint32 offset, addr;
  1492. offset = read_uint32(frame_ip);
  1493. addr = GET_OPERAND(uint32, I32, 0);
  1494. frame_ip += 2;
  1495. addr_ret = GET_OFFSET();
  1496. CHECK_MEMORY_OVERFLOW(1);
  1497. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1498. sign_ext_8_64(*(int8 *)maddr));
  1499. HANDLE_OP_END();
  1500. }
  1501. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  1502. {
  1503. uint32 offset, addr;
  1504. offset = read_uint32(frame_ip);
  1505. addr = GET_OPERAND(uint32, I32, 0);
  1506. frame_ip += 2;
  1507. addr_ret = GET_OFFSET();
  1508. CHECK_MEMORY_OVERFLOW(1);
  1509. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
  1510. HANDLE_OP_END();
  1511. }
  1512. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  1513. {
  1514. uint32 offset, addr;
  1515. offset = read_uint32(frame_ip);
  1516. addr = GET_OPERAND(uint32, I32, 0);
  1517. frame_ip += 2;
  1518. addr_ret = GET_OFFSET();
  1519. CHECK_MEMORY_OVERFLOW(2);
  1520. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1521. sign_ext_16_64(LOAD_I16(maddr)));
  1522. HANDLE_OP_END();
  1523. }
  1524. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  1525. {
  1526. uint32 offset, addr;
  1527. offset = read_uint32(frame_ip);
  1528. addr = GET_OPERAND(uint32, I32, 0);
  1529. frame_ip += 2;
  1530. addr_ret = GET_OFFSET();
  1531. CHECK_MEMORY_OVERFLOW(2);
  1532. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
  1533. HANDLE_OP_END();
  1534. }
  1535. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  1536. {
  1537. uint32 offset, addr;
  1538. offset = read_uint32(frame_ip);
  1539. addr = GET_OPERAND(uint32, I32, 0);
  1540. frame_ip += 2;
  1541. addr_ret = GET_OFFSET();
  1542. CHECK_MEMORY_OVERFLOW(4);
  1543. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1544. sign_ext_32_64(LOAD_I32(maddr)));
  1545. HANDLE_OP_END();
  1546. }
  1547. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  1548. {
  1549. uint32 offset, addr;
  1550. offset = read_uint32(frame_ip);
  1551. addr = GET_OPERAND(uint32, I32, 0);
  1552. frame_ip += 2;
  1553. addr_ret = GET_OFFSET();
  1554. CHECK_MEMORY_OVERFLOW(4);
  1555. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
  1556. HANDLE_OP_END();
  1557. }
  1558. HANDLE_OP(WASM_OP_I32_STORE)
  1559. {
  1560. uint32 offset, addr;
  1561. uint32 sval;
  1562. offset = read_uint32(frame_ip);
  1563. sval = GET_OPERAND(uint32, I32, 0);
  1564. addr = GET_OPERAND(uint32, I32, 2);
  1565. frame_ip += 4;
  1566. CHECK_MEMORY_OVERFLOW(4);
  1567. STORE_U32(maddr, sval);
  1568. HANDLE_OP_END();
  1569. }
  1570. HANDLE_OP(WASM_OP_I32_STORE8)
  1571. {
  1572. uint32 offset, addr;
  1573. uint32 sval;
  1574. offset = read_uint32(frame_ip);
  1575. sval = GET_OPERAND(uint32, I32, 0);
  1576. addr = GET_OPERAND(uint32, I32, 2);
  1577. frame_ip += 4;
  1578. CHECK_MEMORY_OVERFLOW(1);
  1579. *(uint8 *)maddr = (uint8)sval;
  1580. HANDLE_OP_END();
  1581. }
  1582. HANDLE_OP(WASM_OP_I32_STORE16)
  1583. {
  1584. uint32 offset, addr;
  1585. uint32 sval;
  1586. offset = read_uint32(frame_ip);
  1587. sval = GET_OPERAND(uint32, I32, 0);
  1588. addr = GET_OPERAND(uint32, I32, 2);
  1589. frame_ip += 4;
  1590. CHECK_MEMORY_OVERFLOW(2);
  1591. STORE_U16(maddr, (uint16)sval);
  1592. HANDLE_OP_END();
  1593. }
  1594. HANDLE_OP(WASM_OP_I64_STORE)
  1595. {
  1596. uint32 offset, addr;
  1597. uint64 sval;
  1598. offset = read_uint32(frame_ip);
  1599. sval = GET_OPERAND(uint64, I64, 0);
  1600. addr = GET_OPERAND(uint32, I32, 2);
  1601. frame_ip += 4;
  1602. CHECK_MEMORY_OVERFLOW(8);
  1603. STORE_I64(maddr, sval);
  1604. HANDLE_OP_END();
  1605. }
  1606. HANDLE_OP(WASM_OP_I64_STORE8)
  1607. {
  1608. uint32 offset, addr;
  1609. uint64 sval;
  1610. offset = read_uint32(frame_ip);
  1611. sval = GET_OPERAND(uint64, I64, 0);
  1612. addr = GET_OPERAND(uint32, I32, 2);
  1613. frame_ip += 4;
  1614. CHECK_MEMORY_OVERFLOW(1);
  1615. *(uint8 *)maddr = (uint8)sval;
  1616. HANDLE_OP_END();
  1617. }
  1618. HANDLE_OP(WASM_OP_I64_STORE16)
  1619. {
  1620. uint32 offset, addr;
  1621. uint64 sval;
  1622. offset = read_uint32(frame_ip);
  1623. sval = GET_OPERAND(uint64, I64, 0);
  1624. addr = GET_OPERAND(uint32, I32, 2);
  1625. frame_ip += 4;
  1626. CHECK_MEMORY_OVERFLOW(2);
  1627. STORE_U16(maddr, (uint16)sval);
  1628. HANDLE_OP_END();
  1629. }
  1630. HANDLE_OP(WASM_OP_I64_STORE32)
  1631. {
  1632. uint32 offset, addr;
  1633. uint64 sval;
  1634. offset = read_uint32(frame_ip);
  1635. sval = GET_OPERAND(uint64, I64, 0);
  1636. addr = GET_OPERAND(uint32, I32, 2);
  1637. frame_ip += 4;
  1638. CHECK_MEMORY_OVERFLOW(4);
  1639. STORE_U32(maddr, (uint32)sval);
  1640. HANDLE_OP_END();
  1641. }
  1642. /* memory size and memory grow instructions */
  1643. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  1644. {
  1645. uint32 reserved;
  1646. addr_ret = GET_OFFSET();
  1647. frame_lp[addr_ret] = memory->cur_page_count;
  1648. (void)reserved;
  1649. HANDLE_OP_END();
  1650. }
  1651. HANDLE_OP(WASM_OP_MEMORY_GROW)
  1652. {
  1653. uint32 reserved, delta,
  1654. prev_page_count = memory->cur_page_count;
  1655. addr1 = GET_OFFSET();
  1656. addr_ret = GET_OFFSET();
  1657. delta = (uint32)frame_lp[addr1];
  1658. if (!wasm_enlarge_memory(module, delta)) {
  1659. /* failed to memory.grow, return -1 */
  1660. frame_lp[addr_ret] = -1;
  1661. }
  1662. else {
  1663. /* success, return previous page count */
  1664. frame_lp[addr_ret] = prev_page_count;
  1665. /* update memory size, no need to update memory ptr as
  1666. it isn't changed in wasm_enlarge_memory */
  1667. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1668. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1669. || WASM_ENABLE_BULK_MEMORY != 0
  1670. linear_mem_size =
  1671. num_bytes_per_page * memory->cur_page_count;
  1672. #endif
  1673. }
  1674. (void)reserved;
  1675. HANDLE_OP_END();
  1676. }
  1677. /* constant instructions */
  1678. HANDLE_OP(WASM_OP_F64_CONST)
  1679. HANDLE_OP(WASM_OP_I64_CONST)
  1680. {
  1681. uint8 *orig_ip = frame_ip;
  1682. frame_ip += sizeof(uint64);
  1683. addr_ret = GET_OFFSET();
  1684. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
  1685. sizeof(uint64));
  1686. HANDLE_OP_END();
  1687. }
  1688. HANDLE_OP(WASM_OP_F32_CONST)
  1689. HANDLE_OP(WASM_OP_I32_CONST)
  1690. {
  1691. uint8 *orig_ip = frame_ip;
  1692. frame_ip += sizeof(uint32);
  1693. addr_ret = GET_OFFSET();
  1694. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
  1695. sizeof(uint32));
  1696. HANDLE_OP_END();
  1697. }
  1698. /* comparison instructions of i32 */
  1699. HANDLE_OP(WASM_OP_I32_EQZ)
  1700. {
  1701. DEF_OP_EQZ(int32, I32);
  1702. HANDLE_OP_END();
  1703. }
  1704. HANDLE_OP(WASM_OP_I32_EQ)
  1705. {
  1706. DEF_OP_CMP(uint32, I32, ==);
  1707. HANDLE_OP_END();
  1708. }
  1709. HANDLE_OP(WASM_OP_I32_NE)
  1710. {
  1711. DEF_OP_CMP(uint32, I32, !=);
  1712. HANDLE_OP_END();
  1713. }
  1714. HANDLE_OP(WASM_OP_I32_LT_S)
  1715. {
  1716. DEF_OP_CMP(int32, I32, <);
  1717. HANDLE_OP_END();
  1718. }
  1719. HANDLE_OP(WASM_OP_I32_LT_U)
  1720. {
  1721. DEF_OP_CMP(uint32, I32, <);
  1722. HANDLE_OP_END();
  1723. }
  1724. HANDLE_OP(WASM_OP_I32_GT_S)
  1725. {
  1726. DEF_OP_CMP(int32, I32, >);
  1727. HANDLE_OP_END();
  1728. }
  1729. HANDLE_OP(WASM_OP_I32_GT_U)
  1730. {
  1731. DEF_OP_CMP(uint32, I32, >);
  1732. HANDLE_OP_END();
  1733. }
  1734. HANDLE_OP(WASM_OP_I32_LE_S)
  1735. {
  1736. DEF_OP_CMP(int32, I32, <=);
  1737. HANDLE_OP_END();
  1738. }
  1739. HANDLE_OP(WASM_OP_I32_LE_U)
  1740. {
  1741. DEF_OP_CMP(uint32, I32, <=);
  1742. HANDLE_OP_END();
  1743. }
  1744. HANDLE_OP(WASM_OP_I32_GE_S)
  1745. {
  1746. DEF_OP_CMP(int32, I32, >=);
  1747. HANDLE_OP_END();
  1748. }
  1749. HANDLE_OP(WASM_OP_I32_GE_U)
  1750. {
  1751. DEF_OP_CMP(uint32, I32, >=);
  1752. HANDLE_OP_END();
  1753. }
  1754. /* comparison instructions of i64 */
  1755. HANDLE_OP(WASM_OP_I64_EQZ)
  1756. {
  1757. DEF_OP_EQZ(int64, I64);
  1758. HANDLE_OP_END();
  1759. }
  1760. HANDLE_OP(WASM_OP_I64_EQ)
  1761. {
  1762. DEF_OP_CMP(uint64, I64, ==);
  1763. HANDLE_OP_END();
  1764. }
  1765. HANDLE_OP(WASM_OP_I64_NE)
  1766. {
  1767. DEF_OP_CMP(uint64, I64, !=);
  1768. HANDLE_OP_END();
  1769. }
  1770. HANDLE_OP(WASM_OP_I64_LT_S)
  1771. {
  1772. DEF_OP_CMP(int64, I64, <);
  1773. HANDLE_OP_END();
  1774. }
  1775. HANDLE_OP(WASM_OP_I64_LT_U)
  1776. {
  1777. DEF_OP_CMP(uint64, I64, <);
  1778. HANDLE_OP_END();
  1779. }
  1780. HANDLE_OP(WASM_OP_I64_GT_S)
  1781. {
  1782. DEF_OP_CMP(int64, I64, >);
  1783. HANDLE_OP_END();
  1784. }
  1785. HANDLE_OP(WASM_OP_I64_GT_U)
  1786. {
  1787. DEF_OP_CMP(uint64, I64, >);
  1788. HANDLE_OP_END();
  1789. }
  1790. HANDLE_OP(WASM_OP_I64_LE_S)
  1791. {
  1792. DEF_OP_CMP(int64, I64, <=);
  1793. HANDLE_OP_END();
  1794. }
  1795. HANDLE_OP(WASM_OP_I64_LE_U)
  1796. {
  1797. DEF_OP_CMP(uint64, I64, <=);
  1798. HANDLE_OP_END();
  1799. }
  1800. HANDLE_OP(WASM_OP_I64_GE_S)
  1801. {
  1802. DEF_OP_CMP(int64, I64, >=);
  1803. HANDLE_OP_END();
  1804. }
  1805. HANDLE_OP(WASM_OP_I64_GE_U)
  1806. {
  1807. DEF_OP_CMP(uint64, I64, >=);
  1808. HANDLE_OP_END();
  1809. }
  1810. /* comparison instructions of f32 */
  1811. HANDLE_OP(WASM_OP_F32_EQ)
  1812. {
  1813. DEF_OP_CMP(float32, F32, ==);
  1814. HANDLE_OP_END();
  1815. }
  1816. HANDLE_OP(WASM_OP_F32_NE)
  1817. {
  1818. DEF_OP_CMP(float32, F32, !=);
  1819. HANDLE_OP_END();
  1820. }
  1821. HANDLE_OP(WASM_OP_F32_LT)
  1822. {
  1823. DEF_OP_CMP(float32, F32, <);
  1824. HANDLE_OP_END();
  1825. }
  1826. HANDLE_OP(WASM_OP_F32_GT)
  1827. {
  1828. DEF_OP_CMP(float32, F32, >);
  1829. HANDLE_OP_END();
  1830. }
  1831. HANDLE_OP(WASM_OP_F32_LE)
  1832. {
  1833. DEF_OP_CMP(float32, F32, <=);
  1834. HANDLE_OP_END();
  1835. }
  1836. HANDLE_OP(WASM_OP_F32_GE)
  1837. {
  1838. DEF_OP_CMP(float32, F32, >=);
  1839. HANDLE_OP_END();
  1840. }
  1841. /* comparison instructions of f64 */
  1842. HANDLE_OP(WASM_OP_F64_EQ)
  1843. {
  1844. DEF_OP_CMP(float64, F64, ==);
  1845. HANDLE_OP_END();
  1846. }
  1847. HANDLE_OP(WASM_OP_F64_NE)
  1848. {
  1849. DEF_OP_CMP(float64, F64, !=);
  1850. HANDLE_OP_END();
  1851. }
  1852. HANDLE_OP(WASM_OP_F64_LT)
  1853. {
  1854. DEF_OP_CMP(float64, F64, <);
  1855. HANDLE_OP_END();
  1856. }
  1857. HANDLE_OP(WASM_OP_F64_GT)
  1858. {
  1859. DEF_OP_CMP(float64, F64, >);
  1860. HANDLE_OP_END();
  1861. }
  1862. HANDLE_OP(WASM_OP_F64_LE)
  1863. {
  1864. DEF_OP_CMP(float64, F64, <=);
  1865. HANDLE_OP_END();
  1866. }
  1867. HANDLE_OP(WASM_OP_F64_GE)
  1868. {
  1869. DEF_OP_CMP(float64, F64, >=);
  1870. HANDLE_OP_END();
  1871. }
  1872. /* numberic instructions of i32 */
  1873. HANDLE_OP(WASM_OP_I32_CLZ)
  1874. {
  1875. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  1876. HANDLE_OP_END();
  1877. }
  1878. HANDLE_OP(WASM_OP_I32_CTZ)
  1879. {
  1880. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  1881. HANDLE_OP_END();
  1882. }
  1883. HANDLE_OP(WASM_OP_I32_POPCNT)
  1884. {
  1885. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  1886. HANDLE_OP_END();
  1887. }
  1888. HANDLE_OP(WASM_OP_I32_ADD)
  1889. {
  1890. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  1891. HANDLE_OP_END();
  1892. }
  1893. HANDLE_OP(WASM_OP_I32_SUB)
  1894. {
  1895. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  1896. HANDLE_OP_END();
  1897. }
  1898. HANDLE_OP(WASM_OP_I32_MUL)
  1899. {
  1900. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  1901. HANDLE_OP_END();
  1902. }
  1903. HANDLE_OP(WASM_OP_I32_DIV_S)
  1904. {
  1905. int32 a, b;
  1906. b = frame_lp[GET_OFFSET()];
  1907. a = frame_lp[GET_OFFSET()];
  1908. addr_ret = GET_OFFSET();
  1909. if (a == (int32)0x80000000 && b == -1) {
  1910. wasm_set_exception(module, "integer overflow");
  1911. goto got_exception;
  1912. }
  1913. if (b == 0) {
  1914. wasm_set_exception(module, "integer divide by zero");
  1915. goto got_exception;
  1916. }
  1917. frame_lp[addr_ret] = (a / b);
  1918. HANDLE_OP_END();
  1919. }
  1920. HANDLE_OP(WASM_OP_I32_DIV_U)
  1921. {
  1922. uint32 a, b;
  1923. addr1 = GET_OFFSET();
  1924. addr2 = GET_OFFSET();
  1925. addr_ret = GET_OFFSET();
  1926. b = (uint32)frame_lp[addr1];
  1927. a = (uint32)frame_lp[addr2];
  1928. if (b == 0) {
  1929. wasm_set_exception(module, "integer divide by zero");
  1930. goto got_exception;
  1931. }
  1932. frame_lp[addr_ret] = (a / b);
  1933. HANDLE_OP_END();
  1934. }
  1935. HANDLE_OP(WASM_OP_I32_REM_S)
  1936. {
  1937. int32 a, b;
  1938. addr1 = GET_OFFSET();
  1939. addr2 = GET_OFFSET();
  1940. addr_ret = GET_OFFSET();
  1941. b = frame_lp[addr1];
  1942. a = frame_lp[addr2];
  1943. if (a == (int32)0x80000000 && b == -1) {
  1944. frame_lp[addr_ret] = 0;
  1945. HANDLE_OP_END();
  1946. }
  1947. if (b == 0) {
  1948. wasm_set_exception(module, "integer divide by zero");
  1949. goto got_exception;
  1950. }
  1951. frame_lp[addr_ret] = (a % b);
  1952. HANDLE_OP_END();
  1953. }
  1954. HANDLE_OP(WASM_OP_I32_REM_U)
  1955. {
  1956. uint32 a, b;
  1957. addr1 = GET_OFFSET();
  1958. addr2 = GET_OFFSET();
  1959. addr_ret = GET_OFFSET();
  1960. b = (uint32)frame_lp[addr1];
  1961. a = (uint32)frame_lp[addr2];
  1962. if (b == 0) {
  1963. wasm_set_exception(module, "integer divide by zero");
  1964. goto got_exception;
  1965. }
  1966. frame_lp[addr_ret] = (a % b);
  1967. HANDLE_OP_END();
  1968. }
  1969. HANDLE_OP(WASM_OP_I32_AND)
  1970. {
  1971. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  1972. HANDLE_OP_END();
  1973. }
  1974. HANDLE_OP(WASM_OP_I32_OR)
  1975. {
  1976. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  1977. HANDLE_OP_END();
  1978. }
  1979. HANDLE_OP(WASM_OP_I32_XOR)
  1980. {
  1981. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  1982. HANDLE_OP_END();
  1983. }
  1984. HANDLE_OP(WASM_OP_I32_SHL)
  1985. {
  1986. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  1987. HANDLE_OP_END();
  1988. }
  1989. HANDLE_OP(WASM_OP_I32_SHR_S)
  1990. {
  1991. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  1992. HANDLE_OP_END();
  1993. }
  1994. HANDLE_OP(WASM_OP_I32_SHR_U)
  1995. {
  1996. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  1997. HANDLE_OP_END();
  1998. }
  1999. HANDLE_OP(WASM_OP_I32_ROTL)
  2000. {
  2001. uint32 a, b;
  2002. b = (uint32)frame_lp[GET_OFFSET()];
  2003. a = (uint32)frame_lp[GET_OFFSET()];
  2004. frame_lp[GET_OFFSET()] = rotl32(a, b);
  2005. HANDLE_OP_END();
  2006. }
  2007. HANDLE_OP(WASM_OP_I32_ROTR)
  2008. {
  2009. uint32 a, b;
  2010. b = (uint32)frame_lp[GET_OFFSET()];
  2011. a = (uint32)frame_lp[GET_OFFSET()];
  2012. frame_lp[GET_OFFSET()] = rotr32(a, b);
  2013. HANDLE_OP_END();
  2014. }
  2015. /* numberic instructions of i64 */
  2016. HANDLE_OP(WASM_OP_I64_CLZ)
  2017. {
  2018. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  2019. HANDLE_OP_END();
  2020. }
  2021. HANDLE_OP(WASM_OP_I64_CTZ)
  2022. {
  2023. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  2024. HANDLE_OP_END();
  2025. }
  2026. HANDLE_OP(WASM_OP_I64_POPCNT)
  2027. {
  2028. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  2029. HANDLE_OP_END();
  2030. }
  2031. HANDLE_OP(WASM_OP_I64_ADD)
  2032. {
  2033. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  2034. HANDLE_OP_END();
  2035. }
  2036. HANDLE_OP(WASM_OP_I64_SUB)
  2037. {
  2038. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  2039. HANDLE_OP_END();
  2040. }
  2041. HANDLE_OP(WASM_OP_I64_MUL)
  2042. {
  2043. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  2044. HANDLE_OP_END();
  2045. }
  2046. HANDLE_OP(WASM_OP_I64_DIV_S)
  2047. {
  2048. int64 a, b;
  2049. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2050. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2051. if (a == (int64)0x8000000000000000LL && b == -1) {
  2052. wasm_set_exception(module, "integer overflow");
  2053. goto got_exception;
  2054. }
  2055. if (b == 0) {
  2056. wasm_set_exception(module, "integer divide by zero");
  2057. goto got_exception;
  2058. }
  2059. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2060. HANDLE_OP_END();
  2061. }
  2062. HANDLE_OP(WASM_OP_I64_DIV_U)
  2063. {
  2064. uint64 a, b;
  2065. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2066. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2067. if (b == 0) {
  2068. wasm_set_exception(module, "integer divide by zero");
  2069. goto got_exception;
  2070. }
  2071. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2072. HANDLE_OP_END();
  2073. }
  2074. HANDLE_OP(WASM_OP_I64_REM_S)
  2075. {
  2076. int64 a, b;
  2077. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2078. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2079. if (a == (int64)0x8000000000000000LL && b == -1) {
  2080. *(int64 *)(frame_lp + GET_OFFSET()) = 0;
  2081. HANDLE_OP_END();
  2082. }
  2083. if (b == 0) {
  2084. wasm_set_exception(module, "integer divide by zero");
  2085. goto got_exception;
  2086. }
  2087. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2088. HANDLE_OP_END();
  2089. }
  2090. HANDLE_OP(WASM_OP_I64_REM_U)
  2091. {
  2092. uint64 a, b;
  2093. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2094. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2095. if (b == 0) {
  2096. wasm_set_exception(module, "integer divide by zero");
  2097. goto got_exception;
  2098. }
  2099. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2100. HANDLE_OP_END();
  2101. }
  2102. HANDLE_OP(WASM_OP_I64_AND)
  2103. {
  2104. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  2105. HANDLE_OP_END();
  2106. }
  2107. HANDLE_OP(WASM_OP_I64_OR)
  2108. {
  2109. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  2110. HANDLE_OP_END();
  2111. }
  2112. HANDLE_OP(WASM_OP_I64_XOR)
  2113. {
  2114. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  2115. HANDLE_OP_END();
  2116. }
  2117. HANDLE_OP(WASM_OP_I64_SHL)
  2118. {
  2119. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  2120. HANDLE_OP_END();
  2121. }
  2122. HANDLE_OP(WASM_OP_I64_SHR_S)
  2123. {
  2124. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  2125. HANDLE_OP_END();
  2126. }
  2127. HANDLE_OP(WASM_OP_I64_SHR_U)
  2128. {
  2129. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  2130. HANDLE_OP_END();
  2131. }
  2132. HANDLE_OP(WASM_OP_I64_ROTL)
  2133. {
  2134. uint64 a, b;
  2135. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2136. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2137. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
  2138. HANDLE_OP_END();
  2139. }
  2140. HANDLE_OP(WASM_OP_I64_ROTR)
  2141. {
  2142. uint64 a, b;
  2143. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2144. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2145. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
  2146. HANDLE_OP_END();
  2147. }
  2148. /* numberic instructions of f32 */
  2149. HANDLE_OP(WASM_OP_F32_ABS)
  2150. {
  2151. DEF_OP_MATH(float32, F32, fabsf);
  2152. HANDLE_OP_END();
  2153. }
  2154. HANDLE_OP(WASM_OP_F32_NEG)
  2155. {
  2156. uint32 u32 = frame_lp[GET_OFFSET()];
  2157. uint32 sign_bit = u32 & ((uint32)1 << 31);
  2158. addr_ret = GET_OFFSET();
  2159. if (sign_bit)
  2160. frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
  2161. else
  2162. frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
  2163. HANDLE_OP_END();
  2164. }
  2165. HANDLE_OP(WASM_OP_F32_CEIL)
  2166. {
  2167. DEF_OP_MATH(float32, F32, ceilf);
  2168. HANDLE_OP_END();
  2169. }
  2170. HANDLE_OP(WASM_OP_F32_FLOOR)
  2171. {
  2172. DEF_OP_MATH(float32, F32, floorf);
  2173. HANDLE_OP_END();
  2174. }
  2175. HANDLE_OP(WASM_OP_F32_TRUNC)
  2176. {
  2177. DEF_OP_MATH(float32, F32, truncf);
  2178. HANDLE_OP_END();
  2179. }
  2180. HANDLE_OP(WASM_OP_F32_NEAREST)
  2181. {
  2182. DEF_OP_MATH(float32, F32, rintf);
  2183. HANDLE_OP_END();
  2184. }
  2185. HANDLE_OP(WASM_OP_F32_SQRT)
  2186. {
  2187. DEF_OP_MATH(float32, F32, sqrtf);
  2188. HANDLE_OP_END();
  2189. }
  2190. HANDLE_OP(WASM_OP_F32_ADD)
  2191. {
  2192. DEF_OP_NUMERIC(float32, float32, F32, +);
  2193. HANDLE_OP_END();
  2194. }
  2195. HANDLE_OP(WASM_OP_F32_SUB)
  2196. {
  2197. DEF_OP_NUMERIC(float32, float32, F32, -);
  2198. HANDLE_OP_END();
  2199. }
  2200. HANDLE_OP(WASM_OP_F32_MUL)
  2201. {
  2202. DEF_OP_NUMERIC(float32, float32, F32, *);
  2203. HANDLE_OP_END();
  2204. }
  2205. HANDLE_OP(WASM_OP_F32_DIV)
  2206. {
  2207. DEF_OP_NUMERIC(float32, float32, F32, /);
  2208. HANDLE_OP_END();
  2209. }
  2210. HANDLE_OP(WASM_OP_F32_MIN)
  2211. {
  2212. float32 a, b;
  2213. b = *(float32 *)(frame_lp + GET_OFFSET());
  2214. a = *(float32 *)(frame_lp + GET_OFFSET());
  2215. if (isnan(a))
  2216. *(float32 *)(frame_lp + GET_OFFSET()) = a;
  2217. else if (isnan(b))
  2218. *(float32 *)(frame_lp + GET_OFFSET()) = b;
  2219. else
  2220. *(float32 *)(frame_lp + GET_OFFSET()) =
  2221. (float32)wa_fmin(a, b);
  2222. HANDLE_OP_END();
  2223. }
  2224. HANDLE_OP(WASM_OP_F32_MAX)
  2225. {
  2226. float32 a, b;
  2227. b = *(float32 *)(frame_lp + GET_OFFSET());
  2228. a = *(float32 *)(frame_lp + GET_OFFSET());
  2229. if (isnan(a))
  2230. *(float32 *)(frame_lp + GET_OFFSET()) = a;
  2231. else if (isnan(b))
  2232. *(float32 *)(frame_lp + GET_OFFSET()) = b;
  2233. else
  2234. *(float32 *)(frame_lp + GET_OFFSET()) =
  2235. (float32)wa_fmax(a, b);
  2236. HANDLE_OP_END();
  2237. }
  2238. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  2239. {
  2240. float32 a, b;
  2241. b = *(float32 *)(frame_lp + GET_OFFSET());
  2242. a = *(float32 *)(frame_lp + GET_OFFSET());
  2243. *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
  2244. HANDLE_OP_END();
  2245. }
  2246. /* numberic instructions of f64 */
  2247. HANDLE_OP(WASM_OP_F64_ABS)
  2248. {
  2249. DEF_OP_MATH(float64, F64, fabs);
  2250. HANDLE_OP_END();
  2251. }
  2252. HANDLE_OP(WASM_OP_F64_NEG)
  2253. {
  2254. uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2255. uint64 sign_bit = u64 & (((uint64)1) << 63);
  2256. if (sign_bit)
  2257. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2258. (u64 & ~(((uint64)1) << 63)));
  2259. else
  2260. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2261. (u64 | (((uint64)1) << 63)));
  2262. HANDLE_OP_END();
  2263. }
  2264. HANDLE_OP(WASM_OP_F64_CEIL)
  2265. {
  2266. DEF_OP_MATH(float64, F64, ceil);
  2267. HANDLE_OP_END();
  2268. }
  2269. HANDLE_OP(WASM_OP_F64_FLOOR)
  2270. {
  2271. DEF_OP_MATH(float64, F64, floor);
  2272. HANDLE_OP_END();
  2273. }
  2274. HANDLE_OP(WASM_OP_F64_TRUNC)
  2275. {
  2276. DEF_OP_MATH(float64, F64, trunc);
  2277. HANDLE_OP_END();
  2278. }
  2279. HANDLE_OP(WASM_OP_F64_NEAREST)
  2280. {
  2281. DEF_OP_MATH(float64, F64, rint);
  2282. HANDLE_OP_END();
  2283. }
  2284. HANDLE_OP(WASM_OP_F64_SQRT)
  2285. {
  2286. DEF_OP_MATH(float64, F64, sqrt);
  2287. HANDLE_OP_END();
  2288. }
  2289. HANDLE_OP(WASM_OP_F64_ADD)
  2290. {
  2291. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  2292. HANDLE_OP_END();
  2293. }
  2294. HANDLE_OP(WASM_OP_F64_SUB)
  2295. {
  2296. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  2297. HANDLE_OP_END();
  2298. }
  2299. HANDLE_OP(WASM_OP_F64_MUL)
  2300. {
  2301. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  2302. HANDLE_OP_END();
  2303. }
  2304. HANDLE_OP(WASM_OP_F64_DIV)
  2305. {
  2306. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  2307. HANDLE_OP_END();
  2308. }
  2309. HANDLE_OP(WASM_OP_F64_MIN)
  2310. {
  2311. float64 a, b;
  2312. b = POP_F64();
  2313. a = POP_F64();
  2314. if (isnan(a))
  2315. PUSH_F64(a);
  2316. else if (isnan(b))
  2317. PUSH_F64(b);
  2318. else
  2319. PUSH_F64(wa_fmin(a, b));
  2320. HANDLE_OP_END();
  2321. }
  2322. HANDLE_OP(WASM_OP_F64_MAX)
  2323. {
  2324. float64 a, b;
  2325. b = POP_F64();
  2326. a = POP_F64();
  2327. if (isnan(a))
  2328. PUSH_F64(a);
  2329. else if (isnan(b))
  2330. PUSH_F64(b);
  2331. else
  2332. PUSH_F64(wa_fmax(a, b));
  2333. HANDLE_OP_END();
  2334. }
  2335. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  2336. {
  2337. float64 a, b;
  2338. b = POP_F64();
  2339. a = POP_F64();
  2340. PUSH_F64(local_copysign(a, b));
  2341. HANDLE_OP_END();
  2342. }
  2343. /* conversions of i32 */
  2344. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  2345. {
  2346. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  2347. PUSH_I32(value);
  2348. HANDLE_OP_END();
  2349. }
  2350. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  2351. {
  2352. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  2353. since float/double values of ieee754 cannot precisely
  2354. represent all int32/uint32/int64/uint64 values, e.g.:
  2355. UINT32_MAX is 4294967295, but (float32)4294967295 is
  2356. 4294967296.0f, but not 4294967295.0f. */
  2357. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  2358. HANDLE_OP_END();
  2359. }
  2360. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  2361. {
  2362. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  2363. HANDLE_OP_END();
  2364. }
  2365. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  2366. {
  2367. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  2368. HANDLE_OP_END();
  2369. }
  2370. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  2371. {
  2372. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  2373. HANDLE_OP_END();
  2374. }
  2375. /* conversions of i64 */
  2376. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  2377. {
  2378. DEF_OP_CONVERT(int64, I64, int32, I32);
  2379. HANDLE_OP_END();
  2380. }
  2381. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  2382. {
  2383. DEF_OP_CONVERT(int64, I64, uint32, I32);
  2384. HANDLE_OP_END();
  2385. }
  2386. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  2387. {
  2388. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  2389. 9223372036854775808.0f, false, true);
  2390. HANDLE_OP_END();
  2391. }
  2392. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  2393. {
  2394. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  2395. HANDLE_OP_END();
  2396. }
  2397. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  2398. {
  2399. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  2400. false, true);
  2401. HANDLE_OP_END();
  2402. }
  2403. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  2404. {
  2405. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  2406. HANDLE_OP_END();
  2407. }
  2408. /* conversions of f32 */
  2409. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  2410. {
  2411. DEF_OP_CONVERT(float32, F32, int32, I32);
  2412. HANDLE_OP_END();
  2413. }
  2414. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  2415. {
  2416. DEF_OP_CONVERT(float32, F32, uint32, I32);
  2417. HANDLE_OP_END();
  2418. }
  2419. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  2420. {
  2421. DEF_OP_CONVERT(float32, F32, int64, I64);
  2422. HANDLE_OP_END();
  2423. }
  2424. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  2425. {
  2426. DEF_OP_CONVERT(float32, F32, uint64, I64);
  2427. HANDLE_OP_END();
  2428. }
  2429. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  2430. {
  2431. DEF_OP_CONVERT(float32, F32, float64, F64);
  2432. HANDLE_OP_END();
  2433. }
  2434. /* conversions of f64 */
  2435. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  2436. {
  2437. DEF_OP_CONVERT(float64, F64, int32, I32);
  2438. HANDLE_OP_END();
  2439. }
  2440. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  2441. {
  2442. DEF_OP_CONVERT(float64, F64, uint32, I32);
  2443. HANDLE_OP_END();
  2444. }
  2445. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  2446. {
  2447. DEF_OP_CONVERT(float64, F64, int64, I64);
  2448. HANDLE_OP_END();
  2449. }
  2450. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  2451. {
  2452. DEF_OP_CONVERT(float64, F64, uint64, I64);
  2453. HANDLE_OP_END();
  2454. }
  2455. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  2456. {
  2457. DEF_OP_CONVERT(float64, F64, float32, F32);
  2458. HANDLE_OP_END();
  2459. }
  2460. /* reinterpretations */
  2461. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  2462. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  2463. {
  2464. DEF_OP_REINTERPRET(uint32, I32);
  2465. HANDLE_OP_END();
  2466. }
  2467. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  2468. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
  2469. {
  2470. DEF_OP_REINTERPRET(int64, I64);
  2471. HANDLE_OP_END();
  2472. }
  2473. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  2474. {
  2475. addr1 = GET_OFFSET();
  2476. addr2 = GET_OFFSET();
  2477. frame_lp[addr2] = frame_lp[addr1];
  2478. HANDLE_OP_END();
  2479. }
  2480. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  2481. {
  2482. addr1 = GET_OFFSET();
  2483. addr2 = GET_OFFSET();
  2484. frame_lp[addr2] = frame_lp[addr1];
  2485. frame_lp[addr2 + 1] = frame_lp[addr1 + 1];
  2486. HANDLE_OP_END();
  2487. }
  2488. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  2489. {
  2490. uint32 values_count, total_cell;
  2491. uint8 *cells;
  2492. int16 *src_offsets = NULL;
  2493. uint16 *dst_offsets = NULL;
  2494. /* read values_count */
  2495. values_count = read_uint32(frame_ip);
  2496. /* read total cell num */
  2497. total_cell = read_uint32(frame_ip);
  2498. /* cells */
  2499. cells = (uint8 *)frame_ip;
  2500. frame_ip += values_count * CELL_SIZE;
  2501. /* src offsets */
  2502. src_offsets = (int16 *)frame_ip;
  2503. frame_ip += values_count * sizeof(int16);
  2504. /* dst offsets */
  2505. dst_offsets = (uint16 *)frame_ip;
  2506. frame_ip += values_count * sizeof(uint16);
  2507. if (!copy_stack_values(module, frame_lp, values_count,
  2508. total_cell, cells, src_offsets,
  2509. dst_offsets))
  2510. goto got_exception;
  2511. HANDLE_OP_END();
  2512. }
  2513. HANDLE_OP(WASM_OP_SET_LOCAL)
  2514. HANDLE_OP(WASM_OP_TEE_LOCAL)
  2515. {
  2516. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  2517. addr1 = GET_OFFSET();
  2518. if (local_type == VALUE_TYPE_I32
  2519. || local_type == VALUE_TYPE_F32) {
  2520. *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
  2521. }
  2522. else if (local_type == VALUE_TYPE_I64
  2523. || local_type == VALUE_TYPE_F64) {
  2524. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  2525. GET_I64_FROM_ADDR(frame_lp + addr1));
  2526. }
  2527. else {
  2528. wasm_set_exception(module, "invalid local type");
  2529. goto got_exception;
  2530. }
  2531. HANDLE_OP_END();
  2532. }
  2533. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  2534. {
  2535. DEF_OP_CONVERT(int32, I32, int8, I32);
  2536. HANDLE_OP_END();
  2537. }
  2538. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  2539. {
  2540. DEF_OP_CONVERT(int32, I32, int16, I32);
  2541. HANDLE_OP_END();
  2542. }
  2543. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  2544. {
  2545. DEF_OP_CONVERT(int64, I64, int8, I64);
  2546. HANDLE_OP_END();
  2547. }
  2548. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  2549. {
  2550. DEF_OP_CONVERT(int64, I64, int16, I64);
  2551. HANDLE_OP_END();
  2552. }
  2553. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  2554. {
  2555. DEF_OP_CONVERT(int64, I64, int32, I64);
  2556. HANDLE_OP_END();
  2557. }
  2558. HANDLE_OP(WASM_OP_MISC_PREFIX)
  2559. {
  2560. GET_OPCODE();
  2561. switch (opcode) {
  2562. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2563. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  2564. true, true);
  2565. break;
  2566. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2567. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  2568. break;
  2569. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2570. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  2571. true);
  2572. break;
  2573. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2574. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  2575. break;
  2576. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2577. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  2578. 9223372036854775808.0f, false,
  2579. true);
  2580. break;
  2581. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2582. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  2583. false, false);
  2584. break;
  2585. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2586. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  2587. 9223372036854775808.0, false,
  2588. true);
  2589. break;
  2590. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2591. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  2592. false, false);
  2593. break;
  2594. #if WASM_ENABLE_BULK_MEMORY != 0
  2595. case WASM_OP_MEMORY_INIT:
  2596. {
  2597. uint32 addr, segment;
  2598. uint64 bytes, offset, seg_len;
  2599. uint8 *data;
  2600. segment = read_uint32(frame_ip);
  2601. bytes = (uint64)POP_I32();
  2602. offset = (uint64)POP_I32();
  2603. addr = POP_I32();
  2604. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2605. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  2606. #else
  2607. if ((uint64)(uint32)addr + bytes
  2608. > (uint64)linear_mem_size)
  2609. goto out_of_bounds;
  2610. maddr = memory->memory_data + (uint32)addr;
  2611. #endif
  2612. seg_len = (uint64)module->module->data_segments[segment]
  2613. ->data_length;
  2614. data = module->module->data_segments[segment]->data;
  2615. if (offset + bytes > seg_len)
  2616. goto out_of_bounds;
  2617. bh_memcpy_s(maddr, linear_mem_size - addr,
  2618. data + offset, (uint32)bytes);
  2619. break;
  2620. }
  2621. case WASM_OP_DATA_DROP:
  2622. {
  2623. uint32 segment;
  2624. segment = read_uint32(frame_ip);
  2625. module->module->data_segments[segment]->data_length = 0;
  2626. break;
  2627. }
  2628. case WASM_OP_MEMORY_COPY:
  2629. {
  2630. uint32 dst, src, len;
  2631. uint8 *mdst, *msrc;
  2632. len = POP_I32();
  2633. src = POP_I32();
  2634. dst = POP_I32();
  2635. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2636. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  2637. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2638. #else
  2639. if ((uint64)(uint32)src + len > (uint64)linear_mem_size)
  2640. goto out_of_bounds;
  2641. msrc = memory->memory_data + (uint32)src;
  2642. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  2643. goto out_of_bounds;
  2644. mdst = memory->memory_data + (uint32)dst;
  2645. #endif
  2646. /* allowing the destination and source to overlap */
  2647. bh_memmove_s(mdst, linear_mem_size - dst, msrc, len);
  2648. break;
  2649. }
  2650. case WASM_OP_MEMORY_FILL:
  2651. {
  2652. uint32 dst, len;
  2653. uint8 fill_val, *mdst;
  2654. len = POP_I32();
  2655. fill_val = POP_I32();
  2656. dst = POP_I32();
  2657. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2658. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2659. #else
  2660. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  2661. goto out_of_bounds;
  2662. mdst = memory->memory_data + (uint32)dst;
  2663. #endif
  2664. memset(mdst, fill_val, len);
  2665. break;
  2666. }
  2667. #endif /* WASM_ENABLE_BULK_MEMORY */
  2668. #if WASM_ENABLE_REF_TYPES != 0
  2669. case WASM_OP_TABLE_INIT:
  2670. {
  2671. uint32 tbl_idx, elem_idx;
  2672. uint64 n, s, d;
  2673. WASMTableInstance *tbl_inst;
  2674. elem_idx = read_uint32(frame_ip);
  2675. bh_assert(elem_idx < module->module->table_seg_count);
  2676. tbl_idx = read_uint32(frame_ip);
  2677. bh_assert(tbl_idx < module->module->table_count);
  2678. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2679. n = (uint32)POP_I32();
  2680. s = (uint32)POP_I32();
  2681. d = (uint32)POP_I32();
  2682. if (!n) {
  2683. break;
  2684. }
  2685. if (n + s > module->module->table_segments[elem_idx]
  2686. .function_count
  2687. || d + n > tbl_inst->cur_size) {
  2688. wasm_set_exception(module,
  2689. "out of bounds table access");
  2690. goto got_exception;
  2691. }
  2692. if (module->module->table_segments[elem_idx]
  2693. .is_dropped) {
  2694. wasm_set_exception(module,
  2695. "out of bounds table access");
  2696. goto got_exception;
  2697. }
  2698. if (!wasm_elem_is_passive(
  2699. module->module->table_segments[elem_idx]
  2700. .mode)) {
  2701. wasm_set_exception(module,
  2702. "out of bounds table access");
  2703. goto got_exception;
  2704. }
  2705. bh_memcpy_s(
  2706. (uint8 *)tbl_inst
  2707. + offsetof(WASMTableInstance, elems)
  2708. + d * sizeof(uint32),
  2709. (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
  2710. module->module->table_segments[elem_idx]
  2711. .func_indexes
  2712. + s,
  2713. (uint32)(n * sizeof(uint32)));
  2714. break;
  2715. }
  2716. case WASM_OP_ELEM_DROP:
  2717. {
  2718. uint32 elem_idx = read_uint32(frame_ip);
  2719. bh_assert(elem_idx < module->module->table_seg_count);
  2720. module->module->table_segments[elem_idx].is_dropped =
  2721. true;
  2722. break;
  2723. }
  2724. case WASM_OP_TABLE_COPY:
  2725. {
  2726. uint32 src_tbl_idx, dst_tbl_idx;
  2727. uint64 n, s, d;
  2728. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  2729. dst_tbl_idx = read_uint32(frame_ip);
  2730. bh_assert(dst_tbl_idx < module->table_count);
  2731. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  2732. src_tbl_idx = read_uint32(frame_ip);
  2733. bh_assert(src_tbl_idx < module->table_count);
  2734. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  2735. n = (uint32)POP_I32();
  2736. s = (uint32)POP_I32();
  2737. d = (uint32)POP_I32();
  2738. if (d + n > dst_tbl_inst->cur_size
  2739. || s + n > src_tbl_inst->cur_size) {
  2740. wasm_set_exception(module,
  2741. "out of bounds table access");
  2742. goto got_exception;
  2743. }
  2744. /* if s >= d, copy from front to back */
  2745. /* if s < d, copy from back to front */
  2746. /* merge all together */
  2747. bh_memmove_s((uint8 *)dst_tbl_inst
  2748. + offsetof(WASMTableInstance, elems)
  2749. + d * sizeof(uint32),
  2750. (uint32)((dst_tbl_inst->cur_size - d)
  2751. * sizeof(uint32)),
  2752. (uint8 *)src_tbl_inst
  2753. + offsetof(WASMTableInstance, elems)
  2754. + s * sizeof(uint32),
  2755. (uint32)(n * sizeof(uint32)));
  2756. break;
  2757. }
  2758. case WASM_OP_TABLE_GROW:
  2759. {
  2760. uint32 tbl_idx, n, init_val, orig_tbl_sz;
  2761. WASMTableInstance *tbl_inst;
  2762. tbl_idx = read_uint32(frame_ip);
  2763. bh_assert(tbl_idx < module->table_count);
  2764. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2765. orig_tbl_sz = tbl_inst->cur_size;
  2766. n = POP_I32();
  2767. init_val = POP_I32();
  2768. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  2769. PUSH_I32(-1);
  2770. }
  2771. else {
  2772. PUSH_I32(orig_tbl_sz);
  2773. }
  2774. break;
  2775. }
  2776. case WASM_OP_TABLE_SIZE:
  2777. {
  2778. uint32 tbl_idx;
  2779. WASMTableInstance *tbl_inst;
  2780. tbl_idx = read_uint32(frame_ip);
  2781. bh_assert(tbl_idx < module->table_count);
  2782. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2783. PUSH_I32(tbl_inst->cur_size);
  2784. break;
  2785. }
  2786. case WASM_OP_TABLE_FILL:
  2787. {
  2788. uint32 tbl_idx, n, fill_val, i;
  2789. WASMTableInstance *tbl_inst;
  2790. tbl_idx = read_uint32(frame_ip);
  2791. bh_assert(tbl_idx < module->table_count);
  2792. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2793. n = POP_I32();
  2794. fill_val = POP_I32();
  2795. i = POP_I32();
  2796. if (i + n > tbl_inst->cur_size) {
  2797. wasm_set_exception(module,
  2798. "out of bounds table access");
  2799. goto got_exception;
  2800. }
  2801. for (; n != 0; i++, n--) {
  2802. tbl_inst->elems[i] = fill_val;
  2803. }
  2804. break;
  2805. }
  2806. #endif /* WASM_ENABLE_REF_TYPES */
  2807. default:
  2808. wasm_set_exception(module, "unsupported opcode");
  2809. goto got_exception;
  2810. }
  2811. HANDLE_OP_END();
  2812. }
  2813. #if WASM_ENABLE_SHARED_MEMORY != 0
  2814. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  2815. {
  2816. uint32 offset = 0, addr;
  2817. GET_OPCODE();
  2818. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2819. offset = read_uint32(frame_ip);
  2820. }
  2821. switch (opcode) {
  2822. case WASM_OP_ATOMIC_NOTIFY:
  2823. {
  2824. uint32 notify_count, ret;
  2825. notify_count = POP_I32();
  2826. addr = POP_I32();
  2827. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2828. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2829. ret = wasm_runtime_atomic_notify(
  2830. (WASMModuleInstanceCommon *)module, maddr,
  2831. notify_count);
  2832. bh_assert((int32)ret >= 0);
  2833. PUSH_I32(ret);
  2834. break;
  2835. }
  2836. case WASM_OP_ATOMIC_WAIT32:
  2837. {
  2838. uint64 timeout;
  2839. uint32 expect, ret;
  2840. timeout = POP_I64();
  2841. expect = POP_I32();
  2842. addr = POP_I32();
  2843. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2844. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2845. ret = wasm_runtime_atomic_wait(
  2846. (WASMModuleInstanceCommon *)module, maddr,
  2847. (uint64)expect, timeout, false);
  2848. if (ret == (uint32)-1)
  2849. goto got_exception;
  2850. PUSH_I32(ret);
  2851. break;
  2852. }
  2853. case WASM_OP_ATOMIC_WAIT64:
  2854. {
  2855. uint64 timeout, expect;
  2856. uint32 ret;
  2857. timeout = POP_I64();
  2858. expect = POP_I64();
  2859. addr = POP_I32();
  2860. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2861. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2862. ret = wasm_runtime_atomic_wait(
  2863. (WASMModuleInstanceCommon *)module, maddr, expect,
  2864. timeout, true);
  2865. if (ret == (uint32)-1)
  2866. goto got_exception;
  2867. PUSH_I32(ret);
  2868. break;
  2869. }
  2870. case WASM_OP_ATOMIC_I32_LOAD:
  2871. case WASM_OP_ATOMIC_I32_LOAD8_U:
  2872. case WASM_OP_ATOMIC_I32_LOAD16_U:
  2873. {
  2874. uint32 readv;
  2875. addr = POP_I32();
  2876. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  2877. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2878. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2879. os_mutex_lock(&module->e->mem_lock);
  2880. readv = (uint32)(*(uint8 *)maddr);
  2881. os_mutex_unlock(&module->e->mem_lock);
  2882. }
  2883. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  2884. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2885. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2886. os_mutex_lock(&module->e->mem_lock);
  2887. readv = (uint32)LOAD_U16(maddr);
  2888. os_mutex_unlock(&module->e->mem_lock);
  2889. }
  2890. else {
  2891. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2892. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2893. os_mutex_lock(&module->e->mem_lock);
  2894. readv = LOAD_I32(maddr);
  2895. os_mutex_unlock(&module->e->mem_lock);
  2896. }
  2897. PUSH_I32(readv);
  2898. break;
  2899. }
  2900. case WASM_OP_ATOMIC_I64_LOAD:
  2901. case WASM_OP_ATOMIC_I64_LOAD8_U:
  2902. case WASM_OP_ATOMIC_I64_LOAD16_U:
  2903. case WASM_OP_ATOMIC_I64_LOAD32_U:
  2904. {
  2905. uint64 readv;
  2906. addr = POP_I32();
  2907. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  2908. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2909. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2910. os_mutex_lock(&module->e->mem_lock);
  2911. readv = (uint64)(*(uint8 *)maddr);
  2912. os_mutex_unlock(&module->e->mem_lock);
  2913. }
  2914. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  2915. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2916. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2917. os_mutex_lock(&module->e->mem_lock);
  2918. readv = (uint64)LOAD_U16(maddr);
  2919. os_mutex_unlock(&module->e->mem_lock);
  2920. }
  2921. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  2922. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2923. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2924. os_mutex_lock(&module->e->mem_lock);
  2925. readv = (uint64)LOAD_U32(maddr);
  2926. os_mutex_unlock(&module->e->mem_lock);
  2927. }
  2928. else {
  2929. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2930. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2931. os_mutex_lock(&module->e->mem_lock);
  2932. readv = LOAD_I64(maddr);
  2933. os_mutex_unlock(&module->e->mem_lock);
  2934. }
  2935. PUSH_I64(readv);
  2936. break;
  2937. }
  2938. case WASM_OP_ATOMIC_I32_STORE:
  2939. case WASM_OP_ATOMIC_I32_STORE8:
  2940. case WASM_OP_ATOMIC_I32_STORE16:
  2941. {
  2942. uint32 sval;
  2943. sval = (uint32)POP_I32();
  2944. addr = POP_I32();
  2945. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  2946. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2947. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2948. os_mutex_lock(&module->e->mem_lock);
  2949. *(uint8 *)maddr = (uint8)sval;
  2950. os_mutex_unlock(&module->e->mem_lock);
  2951. }
  2952. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  2953. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2954. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2955. os_mutex_lock(&module->e->mem_lock);
  2956. STORE_U16(maddr, (uint16)sval);
  2957. os_mutex_unlock(&module->e->mem_lock);
  2958. }
  2959. else {
  2960. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2961. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2962. os_mutex_lock(&module->e->mem_lock);
  2963. STORE_U32(maddr, sval);
  2964. os_mutex_unlock(&module->e->mem_lock);
  2965. }
  2966. break;
  2967. }
  2968. case WASM_OP_ATOMIC_I64_STORE:
  2969. case WASM_OP_ATOMIC_I64_STORE8:
  2970. case WASM_OP_ATOMIC_I64_STORE16:
  2971. case WASM_OP_ATOMIC_I64_STORE32:
  2972. {
  2973. uint64 sval;
  2974. sval = (uint64)POP_I64();
  2975. addr = POP_I32();
  2976. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  2977. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2978. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2979. os_mutex_lock(&module->e->mem_lock);
  2980. *(uint8 *)maddr = (uint8)sval;
  2981. os_mutex_unlock(&module->e->mem_lock);
  2982. }
  2983. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  2984. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2985. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2986. os_mutex_lock(&module->e->mem_lock);
  2987. STORE_U16(maddr, (uint16)sval);
  2988. os_mutex_unlock(&module->e->mem_lock);
  2989. }
  2990. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  2991. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2992. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2993. os_mutex_lock(&module->e->mem_lock);
  2994. STORE_U32(maddr, (uint32)sval);
  2995. os_mutex_unlock(&module->e->mem_lock);
  2996. }
  2997. else {
  2998. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2999. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3000. os_mutex_lock(&module->e->mem_lock);
  3001. STORE_I64(maddr, sval);
  3002. os_mutex_unlock(&module->e->mem_lock);
  3003. }
  3004. break;
  3005. }
  3006. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  3007. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  3008. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  3009. {
  3010. uint32 readv, sval, expect;
  3011. sval = POP_I32();
  3012. expect = POP_I32();
  3013. addr = POP_I32();
  3014. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  3015. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3016. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3017. expect = (uint8)expect;
  3018. os_mutex_lock(&module->e->mem_lock);
  3019. readv = (uint32)(*(uint8 *)maddr);
  3020. if (readv == expect)
  3021. *(uint8 *)maddr = (uint8)(sval);
  3022. os_mutex_unlock(&module->e->mem_lock);
  3023. }
  3024. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  3025. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3026. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3027. expect = (uint16)expect;
  3028. os_mutex_lock(&module->e->mem_lock);
  3029. readv = (uint32)LOAD_U16(maddr);
  3030. if (readv == expect)
  3031. STORE_U16(maddr, (uint16)(sval));
  3032. os_mutex_unlock(&module->e->mem_lock);
  3033. }
  3034. else {
  3035. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3036. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3037. os_mutex_lock(&module->e->mem_lock);
  3038. readv = LOAD_I32(maddr);
  3039. if (readv == expect)
  3040. STORE_U32(maddr, sval);
  3041. os_mutex_unlock(&module->e->mem_lock);
  3042. }
  3043. PUSH_I32(readv);
  3044. break;
  3045. }
  3046. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  3047. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  3048. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  3049. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  3050. {
  3051. uint64 readv, sval, expect;
  3052. sval = (uint64)POP_I64();
  3053. expect = (uint64)POP_I64();
  3054. addr = POP_I32();
  3055. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  3056. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  3057. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3058. expect = (uint8)expect;
  3059. os_mutex_lock(&module->e->mem_lock);
  3060. readv = (uint64)(*(uint8 *)maddr);
  3061. if (readv == expect)
  3062. *(uint8 *)maddr = (uint8)(sval);
  3063. os_mutex_unlock(&module->e->mem_lock);
  3064. }
  3065. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  3066. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  3067. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3068. expect = (uint16)expect;
  3069. os_mutex_lock(&module->e->mem_lock);
  3070. readv = (uint64)LOAD_U16(maddr);
  3071. if (readv == expect)
  3072. STORE_U16(maddr, (uint16)(sval));
  3073. os_mutex_unlock(&module->e->mem_lock);
  3074. }
  3075. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  3076. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  3077. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3078. expect = (uint32)expect;
  3079. os_mutex_lock(&module->e->mem_lock);
  3080. readv = (uint64)LOAD_U32(maddr);
  3081. if (readv == expect)
  3082. STORE_U32(maddr, (uint32)(sval));
  3083. os_mutex_unlock(&module->e->mem_lock);
  3084. }
  3085. else {
  3086. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  3087. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3088. os_mutex_lock(&module->e->mem_lock);
  3089. readv = (uint64)LOAD_I64(maddr);
  3090. if (readv == expect) {
  3091. STORE_I64(maddr, sval);
  3092. }
  3093. os_mutex_unlock(&module->e->mem_lock);
  3094. }
  3095. PUSH_I64(readv);
  3096. break;
  3097. }
  3098. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  3099. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  3100. DEF_ATOMIC_RMW_OPCODE(AND, &);
  3101. DEF_ATOMIC_RMW_OPCODE(OR, |);
  3102. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  3103. /* xchg, ignore the read value, and store the given
  3104. value: readv * 0 + sval */
  3105. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  3106. }
  3107. HANDLE_OP_END();
  3108. }
  3109. #endif
  3110. HANDLE_OP(WASM_OP_IMPDEP)
  3111. {
  3112. frame = prev_frame;
  3113. frame_ip = frame->ip;
  3114. goto call_func_from_entry;
  3115. }
  3116. HANDLE_OP(WASM_OP_CALL)
  3117. {
  3118. #if WASM_ENABLE_THREAD_MGR != 0
  3119. CHECK_SUSPEND_FLAGS();
  3120. #endif
  3121. fidx = read_uint32(frame_ip);
  3122. #if WASM_ENABLE_MULTI_MODULE != 0
  3123. if (fidx >= module->e->function_count) {
  3124. wasm_set_exception(module, "unknown function");
  3125. goto got_exception;
  3126. }
  3127. #endif
  3128. cur_func = module->e->functions + fidx;
  3129. goto call_func_from_interp;
  3130. }
  3131. #if WASM_ENABLE_TAIL_CALL != 0
  3132. HANDLE_OP(WASM_OP_RETURN_CALL)
  3133. {
  3134. #if WASM_ENABLE_THREAD_MGR != 0
  3135. CHECK_SUSPEND_FLAGS();
  3136. #endif
  3137. fidx = read_uint32(frame_ip);
  3138. #if WASM_ENABLE_MULTI_MODULE != 0
  3139. if (fidx >= module->e->function_count) {
  3140. wasm_set_exception(module, "unknown function");
  3141. goto got_exception;
  3142. }
  3143. #endif
  3144. cur_func = module->e->functions + fidx;
  3145. goto call_func_from_return_call;
  3146. }
  3147. #endif /* WASM_ENABLE_TAIL_CALL */
  3148. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3149. default:
  3150. wasm_set_exception(module, "unsupported opcode");
  3151. goto got_exception;
  3152. }
  3153. #endif
  3154. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3155. HANDLE_OP(WASM_OP_UNUSED_0x06)
  3156. HANDLE_OP(WASM_OP_UNUSED_0x07)
  3157. HANDLE_OP(WASM_OP_UNUSED_0x08)
  3158. HANDLE_OP(WASM_OP_UNUSED_0x09)
  3159. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  3160. #if WASM_ENABLE_TAIL_CALL == 0
  3161. HANDLE_OP(WASM_OP_RETURN_CALL)
  3162. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  3163. #endif
  3164. #if WASM_ENABLE_SHARED_MEMORY == 0
  3165. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  3166. #endif
  3167. #if WASM_ENABLE_REF_TYPES == 0
  3168. HANDLE_OP(WASM_OP_TABLE_GET)
  3169. HANDLE_OP(WASM_OP_TABLE_SET)
  3170. HANDLE_OP(WASM_OP_REF_NULL)
  3171. HANDLE_OP(WASM_OP_REF_IS_NULL)
  3172. HANDLE_OP(WASM_OP_REF_FUNC)
  3173. #endif
  3174. /* SELECT_T is converted to SELECT or SELECT_64 */
  3175. HANDLE_OP(WASM_OP_SELECT_T)
  3176. HANDLE_OP(WASM_OP_UNUSED_0x14)
  3177. HANDLE_OP(WASM_OP_UNUSED_0x15)
  3178. HANDLE_OP(WASM_OP_UNUSED_0x16)
  3179. HANDLE_OP(WASM_OP_UNUSED_0x17)
  3180. HANDLE_OP(WASM_OP_UNUSED_0x18)
  3181. HANDLE_OP(WASM_OP_UNUSED_0x19)
  3182. HANDLE_OP(WASM_OP_UNUSED_0x27)
  3183. /* optimized op code */
  3184. HANDLE_OP(WASM_OP_F32_STORE)
  3185. HANDLE_OP(WASM_OP_F64_STORE)
  3186. HANDLE_OP(WASM_OP_F32_LOAD)
  3187. HANDLE_OP(WASM_OP_F64_LOAD)
  3188. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  3189. HANDLE_OP(WASM_OP_GET_LOCAL)
  3190. HANDLE_OP(WASM_OP_DROP)
  3191. HANDLE_OP(WASM_OP_DROP_64)
  3192. HANDLE_OP(WASM_OP_BLOCK)
  3193. HANDLE_OP(WASM_OP_LOOP)
  3194. HANDLE_OP(WASM_OP_END)
  3195. HANDLE_OP(WASM_OP_NOP)
  3196. HANDLE_OP(EXT_OP_BLOCK)
  3197. HANDLE_OP(EXT_OP_LOOP)
  3198. HANDLE_OP(EXT_OP_IF)
  3199. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  3200. {
  3201. wasm_set_exception(module, "unsupported opcode");
  3202. goto got_exception;
  3203. }
  3204. #endif
  3205. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3206. continue;
  3207. #else
  3208. FETCH_OPCODE_AND_DISPATCH();
  3209. #endif
  3210. #if WASM_ENABLE_TAIL_CALL != 0
  3211. call_func_from_return_call:
  3212. {
  3213. uint32 *lp_base;
  3214. uint32 *lp;
  3215. int i;
  3216. if (!(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
  3217. * sizeof(uint32)))) {
  3218. wasm_set_exception(module, "allocate memory failed");
  3219. goto got_exception;
  3220. }
  3221. for (i = 0; i < cur_func->param_count; i++) {
  3222. if (cur_func->param_types[i] == VALUE_TYPE_I64
  3223. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  3224. PUT_I64_TO_ADDR(
  3225. lp, GET_OPERAND(uint64, I64,
  3226. 2 * (cur_func->param_count - i - 1)));
  3227. lp += 2;
  3228. }
  3229. else {
  3230. *lp = GET_OPERAND(uint32, I32,
  3231. (2 * (cur_func->param_count - i - 1)));
  3232. lp++;
  3233. }
  3234. }
  3235. frame->lp = frame->operand + cur_func->const_cell_num;
  3236. if (lp - lp_base > 0) {
  3237. word_copy(frame->lp, lp_base, lp - lp_base);
  3238. }
  3239. wasm_runtime_free(lp_base);
  3240. FREE_FRAME(exec_env, frame);
  3241. frame_ip += cur_func->param_count * sizeof(int16);
  3242. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3243. goto call_func_from_entry;
  3244. }
  3245. #endif /* WASM_ENABLE_TAIL_CALL */
  3246. call_func_from_interp:
  3247. {
  3248. /* Only do the copy when it's called from interpreter. */
  3249. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3250. int i;
  3251. #if WASM_ENABLE_MULTI_MODULE != 0
  3252. if (cur_func->is_import_func) {
  3253. outs_area->lp = outs_area->operand
  3254. + (cur_func->import_func_inst
  3255. ? cur_func->import_func_inst->const_cell_num
  3256. : 0);
  3257. }
  3258. else
  3259. #endif
  3260. {
  3261. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  3262. }
  3263. if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
  3264. > exec_env->wasm_stack.s.top_boundary) {
  3265. wasm_set_exception(module, "wasm operand stack overflow");
  3266. goto got_exception;
  3267. }
  3268. for (i = 0; i < cur_func->param_count; i++) {
  3269. if (cur_func->param_types[i] == VALUE_TYPE_I64
  3270. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  3271. PUT_I64_TO_ADDR(
  3272. outs_area->lp,
  3273. GET_OPERAND(uint64, I64,
  3274. 2 * (cur_func->param_count - i - 1)));
  3275. outs_area->lp += 2;
  3276. }
  3277. else {
  3278. *outs_area->lp = GET_OPERAND(
  3279. uint32, I32, (2 * (cur_func->param_count - i - 1)));
  3280. outs_area->lp++;
  3281. }
  3282. }
  3283. frame_ip += cur_func->param_count * sizeof(int16);
  3284. if (cur_func->ret_cell_num != 0) {
  3285. /* Get the first return value's offset. Since loader emit
  3286. * all return values' offset so we must skip remain return
  3287. * values' offsets.
  3288. */
  3289. WASMType *func_type;
  3290. if (cur_func->is_import_func)
  3291. func_type = cur_func->u.func_import->func_type;
  3292. else
  3293. func_type = cur_func->u.func->func_type;
  3294. frame->ret_offset = GET_OFFSET();
  3295. frame_ip += 2 * (func_type->result_count - 1);
  3296. }
  3297. SYNC_ALL_TO_FRAME();
  3298. prev_frame = frame;
  3299. }
  3300. call_func_from_entry:
  3301. {
  3302. if (cur_func->is_import_func) {
  3303. #if WASM_ENABLE_MULTI_MODULE != 0
  3304. if (cur_func->import_func_inst) {
  3305. wasm_interp_call_func_import(module, exec_env, cur_func,
  3306. prev_frame);
  3307. }
  3308. else
  3309. #endif
  3310. {
  3311. wasm_interp_call_func_native(module, exec_env, cur_func,
  3312. prev_frame);
  3313. }
  3314. prev_frame = frame->prev_frame;
  3315. cur_func = frame->function;
  3316. UPDATE_ALL_FROM_FRAME();
  3317. /* update memory size, no need to update memory ptr as
  3318. it isn't changed in wasm_enlarge_memory */
  3319. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3320. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3321. || WASM_ENABLE_BULK_MEMORY != 0
  3322. if (memory)
  3323. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  3324. #endif
  3325. if (wasm_get_exception(module))
  3326. goto got_exception;
  3327. }
  3328. else {
  3329. WASMFunction *cur_wasm_func = cur_func->u.func;
  3330. all_cell_num = (uint64)cur_func->param_cell_num
  3331. + (uint64)cur_func->local_cell_num
  3332. + (uint64)cur_func->const_cell_num
  3333. + (uint64)cur_wasm_func->max_stack_cell_num;
  3334. if (all_cell_num >= UINT32_MAX) {
  3335. wasm_set_exception(module, "wasm operand stack overflow");
  3336. goto got_exception;
  3337. }
  3338. frame_size = wasm_interp_interp_frame_size((uint32)all_cell_num);
  3339. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  3340. frame = prev_frame;
  3341. goto got_exception;
  3342. }
  3343. /* Initialize the interpreter context. */
  3344. frame->function = cur_func;
  3345. frame_ip = wasm_get_func_code(cur_func);
  3346. frame_ip_end = wasm_get_func_code_end(cur_func);
  3347. frame_lp = frame->lp =
  3348. frame->operand + cur_wasm_func->const_cell_num;
  3349. /* Initialize the consts */
  3350. if (cur_wasm_func->const_cell_num > 0) {
  3351. word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
  3352. cur_wasm_func->const_cell_num);
  3353. }
  3354. /* Initialize the local variables */
  3355. memset(frame_lp + cur_func->param_cell_num, 0,
  3356. (uint32)(cur_func->local_cell_num * 4));
  3357. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  3358. }
  3359. HANDLE_OP_END();
  3360. }
  3361. return_func:
  3362. {
  3363. FREE_FRAME(exec_env, frame);
  3364. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3365. if (!prev_frame->ip)
  3366. /* Called from native. */
  3367. return;
  3368. RECOVER_CONTEXT(prev_frame);
  3369. HANDLE_OP_END();
  3370. }
  3371. (void)frame_ip_end;
  3372. #if WASM_ENABLE_SHARED_MEMORY != 0
  3373. unaligned_atomic:
  3374. wasm_set_exception(module, "unaligned atomic");
  3375. goto got_exception;
  3376. #endif
  3377. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3378. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3379. || WASM_ENABLE_BULK_MEMORY != 0
  3380. out_of_bounds:
  3381. wasm_set_exception(module, "out of bounds memory access");
  3382. #endif
  3383. got_exception:
  3384. SYNC_ALL_TO_FRAME();
  3385. return;
  3386. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3387. }
  3388. #else
  3389. FETCH_OPCODE_AND_DISPATCH();
  3390. #endif
  3391. }
  3392. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3393. void **
  3394. wasm_interp_get_handle_table()
  3395. {
  3396. WASMModuleInstance module;
  3397. memset(&module, 0, sizeof(WASMModuleInstance));
  3398. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  3399. return global_handle_table;
  3400. }
  3401. #endif
  3402. void
  3403. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  3404. WASMFunctionInstance *function, uint32 argc,
  3405. uint32 argv[])
  3406. {
  3407. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  3408. WASMInterpFrame *frame, *outs_area;
  3409. /* Allocate sufficient cells for all kinds of return values. */
  3410. unsigned all_cell_num =
  3411. function->ret_cell_num > 2 ? function->ret_cell_num : 2,
  3412. i;
  3413. /* This frame won't be used by JITed code, so only allocate interp
  3414. frame here. */
  3415. unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
  3416. if (argc < function->param_cell_num) {
  3417. char buf[128];
  3418. snprintf(buf, sizeof(buf),
  3419. "invalid argument count %" PRIu32
  3420. ", must be no smaller than %" PRIu32,
  3421. argc, (uint32)function->param_cell_num);
  3422. wasm_set_exception(module_inst, buf);
  3423. return;
  3424. }
  3425. argc = function->param_cell_num;
  3426. #ifndef OS_ENABLE_HW_BOUND_CHECK
  3427. if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
  3428. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3429. "native stack overflow");
  3430. return;
  3431. }
  3432. #endif
  3433. if (!(frame =
  3434. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  3435. return;
  3436. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3437. frame->function = NULL;
  3438. frame->ip = NULL;
  3439. /* There is no local variable. */
  3440. frame->lp = frame->operand + 0;
  3441. frame->ret_offset = 0;
  3442. if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
  3443. > exec_env->wasm_stack.s.top_boundary) {
  3444. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3445. "wasm operand stack overflow");
  3446. return;
  3447. }
  3448. if (argc > 0)
  3449. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  3450. wasm_exec_env_set_cur_frame(exec_env, frame);
  3451. if (function->is_import_func) {
  3452. #if WASM_ENABLE_MULTI_MODULE != 0
  3453. if (function->import_module_inst) {
  3454. LOG_DEBUG("it is a function of a sub module");
  3455. wasm_interp_call_func_import(module_inst, exec_env, function,
  3456. frame);
  3457. }
  3458. else
  3459. #endif
  3460. {
  3461. LOG_DEBUG("it is an native function");
  3462. wasm_interp_call_func_native(module_inst, exec_env, function,
  3463. frame);
  3464. }
  3465. }
  3466. else {
  3467. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  3468. }
  3469. /* Output the return value to the caller */
  3470. if (!wasm_get_exception(module_inst)) {
  3471. for (i = 0; i < function->ret_cell_num; i++)
  3472. argv[i] = *(frame->lp + i);
  3473. }
  3474. else {
  3475. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3476. if (wasm_interp_create_call_stack(exec_env)) {
  3477. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  3478. }
  3479. #endif
  3480. LOG_DEBUG("meet an exception %s", wasm_get_exception(module_inst));
  3481. }
  3482. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3483. FREE_FRAME(exec_env, frame);
  3484. #if WASM_ENABLE_OPCODE_COUNTER != 0
  3485. wasm_interp_dump_op_count();
  3486. #endif
  3487. }