wasm_interp_fast.c 141 KB

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