wasm_interp_classic.c 178 KB

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