wasm_interp_classic.c 154 KB

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