wasm_interp_fast.c 141 KB

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