wasm_interp_classic.c 178 KB

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