wasm_interp_classic.c 153 KB

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