wasm_interp_fast.c 140 KB

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