wa-lang.org/wazero@v1.0.2/internal/integration_test/spectest/v2/testdata/elem.wast (about)

     1  ;; Test the element section
     2  
     3  ;; Syntax
     4  (module
     5    (table $t 10 funcref)
     6    (func $f)
     7    (func $g)
     8  
     9    ;; Passive
    10    (elem funcref)
    11    (elem funcref (ref.func $f) (item ref.func $f) (item (ref.null func)) (ref.func $g))
    12    (elem func)
    13    (elem func $f $f $g $g)
    14  
    15    (elem $p1 funcref)
    16    (elem $p2 funcref (ref.func $f) (ref.func $f) (ref.null func) (ref.func $g))
    17    (elem $p3 func)
    18    (elem $p4 func $f $f $g $g)
    19  
    20    ;; Active
    21    (elem (table $t) (i32.const 0) funcref)
    22    (elem (table $t) (i32.const 0) funcref (ref.func $f) (ref.null func))
    23    (elem (table $t) (i32.const 0) func)
    24    (elem (table $t) (i32.const 0) func $f $g)
    25    (elem (table $t) (offset (i32.const 0)) funcref)
    26    (elem (table $t) (offset (i32.const 0)) func $f $g)
    27    (elem (table 0) (i32.const 0) func)
    28    (elem (table 0x0) (i32.const 0) func $f $f)
    29    (elem (table 0x000) (offset (i32.const 0)) func)
    30    (elem (table 0) (offset (i32.const 0)) func $f $f)
    31    (elem (table $t) (i32.const 0) func)
    32    (elem (table $t) (i32.const 0) func $f $f)
    33    (elem (table $t) (offset (i32.const 0)) func)
    34    (elem (table $t) (offset (i32.const 0)) func $f $f)
    35    (elem (offset (i32.const 0)))
    36    (elem (offset (i32.const 0)) funcref (ref.func $f) (ref.null func))
    37    (elem (offset (i32.const 0)) func $f $f)
    38    (elem (offset (i32.const 0)) $f $f)
    39    (elem (i32.const 0))
    40    (elem (i32.const 0) funcref (ref.func $f) (ref.null func))
    41    (elem (i32.const 0) func $f $f)
    42    (elem (i32.const 0) $f $f)
    43    (elem (i32.const 0) funcref (item (ref.func $f)) (item (ref.null func)))
    44  
    45    (elem $a1 (table $t) (i32.const 0) funcref)
    46    (elem $a2 (table $t) (i32.const 0) funcref (ref.func $f) (ref.null func))
    47    (elem $a3 (table $t) (i32.const 0) func)
    48    (elem $a4 (table $t) (i32.const 0) func $f $g)
    49    (elem $a9 (table $t) (offset (i32.const 0)) funcref)
    50    (elem $a10 (table $t) (offset (i32.const 0)) func $f $g)
    51    (elem $a11 (table 0) (i32.const 0) func)
    52    (elem $a12 (table 0x0) (i32.const 0) func $f $f)
    53    (elem $a13 (table 0x000) (offset (i32.const 0)) func)
    54    (elem $a14 (table 0) (offset (i32.const 0)) func $f $f)
    55    (elem $a15 (table $t) (i32.const 0) func)
    56    (elem $a16 (table $t) (i32.const 0) func $f $f)
    57    (elem $a17 (table $t) (offset (i32.const 0)) func)
    58    (elem $a18 (table $t) (offset (i32.const 0)) func $f $f)
    59    (elem $a19 (offset (i32.const 0)))
    60    (elem $a20 (offset (i32.const 0)) funcref (ref.func $f) (ref.null func))
    61    (elem $a21 (offset (i32.const 0)) func $f $f)
    62    (elem $a22 (offset (i32.const 0)) $f $f)
    63    (elem $a23 (i32.const 0))
    64    (elem $a24 (i32.const 0) funcref (ref.func $f) (ref.null func))
    65    (elem $a25 (i32.const 0) func $f $f)
    66    (elem $a26 (i32.const 0) $f $f)
    67  
    68    ;; Declarative
    69    (elem declare funcref)
    70    (elem declare funcref (ref.func $f) (ref.func $f) (ref.null func) (ref.func $g))
    71    (elem declare func)
    72    (elem declare func $f $f $g $g)
    73  
    74    (elem $d1 declare funcref)
    75    (elem $d2 declare funcref (ref.func $f) (ref.func $f) (ref.null func) (ref.func $g))
    76    (elem $d3 declare func)
    77    (elem $d4 declare func $f $f $g $g)
    78  )
    79  
    80  (module
    81    (func $f)
    82    (func $g)
    83  
    84    (table $t funcref (elem (ref.func $f) (ref.null func) (ref.func $g)))
    85  )
    86  
    87  
    88  ;; Basic use
    89  
    90  (module
    91    (table 10 funcref)
    92    (func $f)
    93    (elem (i32.const 0) $f)
    94  )
    95  (module
    96    (import "spectest" "table" (table 10 funcref))
    97    (func $f)
    98    (elem (i32.const 0) $f)
    99  )
   100  
   101  (module
   102    (table 10 funcref)
   103    (func $f)
   104    (elem (i32.const 0) $f)
   105    (elem (i32.const 3) $f)
   106    (elem (i32.const 7) $f)
   107    (elem (i32.const 5) $f)
   108    (elem (i32.const 3) $f)
   109  )
   110  (module
   111    (import "spectest" "table" (table 10 funcref))
   112    (func $f)
   113    (elem (i32.const 9) $f)
   114    (elem (i32.const 3) $f)
   115    (elem (i32.const 7) $f)
   116    (elem (i32.const 3) $f)
   117    (elem (i32.const 5) $f)
   118  )
   119  
   120  (module
   121    (global (import "spectest" "global_i32") i32)
   122    (table 1000 funcref)
   123    (func $f)
   124    (elem (global.get 0) $f)
   125  )
   126  
   127  (module
   128    (global $g (import "spectest" "global_i32") i32)
   129    (table 1000 funcref)
   130    (func $f)
   131    (elem (global.get $g) $f)
   132  )
   133  
   134  (module
   135    (type $out-i32 (func (result i32)))
   136    (table 10 funcref)
   137    (elem (i32.const 7) $const-i32-a)
   138    (elem (i32.const 9) $const-i32-b)
   139    (func $const-i32-a (type $out-i32) (i32.const 65))
   140    (func $const-i32-b (type $out-i32) (i32.const 66))
   141    (func (export "call-7") (type $out-i32)
   142      (call_indirect (type $out-i32) (i32.const 7))
   143    )
   144    (func (export "call-9") (type $out-i32)
   145      (call_indirect (type $out-i32) (i32.const 9))
   146    )
   147  )
   148  (assert_return (invoke "call-7") (i32.const 65))
   149  (assert_return (invoke "call-9") (i32.const 66))
   150  
   151  (assert_invalid
   152    (module (table 1 funcref) (global i32 (i32.const 0)) (elem (global.get 0) $f) (func $f))
   153    "unknown global"
   154  )
   155  (assert_invalid
   156    (module (table 1 funcref) (global $g i32 (i32.const 0)) (elem (global.get $g) $f) (func $f))
   157    "unknown global"
   158  )
   159  
   160  
   161  ;; Corner cases
   162  
   163  (module
   164    (table 10 funcref)
   165    (func $f)
   166    (elem (i32.const 9) $f)
   167  )
   168  (module
   169    (import "spectest" "table" (table 10 funcref))
   170    (func $f)
   171    (elem (i32.const 9) $f)
   172  )
   173  
   174  (module
   175    (table 0 funcref)
   176    (elem (i32.const 0))
   177  )
   178  (module
   179    (import "spectest" "table" (table 0 funcref))
   180    (elem (i32.const 0))
   181  )
   182  
   183  (module
   184    (table 0 0 funcref)
   185    (elem (i32.const 0))
   186  )
   187  
   188  (module
   189    (table 20 funcref)
   190    (elem (i32.const 20))
   191  )
   192  
   193  (module
   194    (import "spectest" "table" (table 0 funcref))
   195    (func $f)
   196    (elem (i32.const 0) $f)
   197  )
   198  
   199  (module
   200    (import "spectest" "table" (table 0 100 funcref))
   201    (func $f)
   202    (elem (i32.const 0) $f)
   203  )
   204  
   205  (module
   206    (import "spectest" "table" (table 0 funcref))
   207    (func $f)
   208    (elem (i32.const 1) $f)
   209  )
   210  
   211  (module
   212    (import "spectest" "table" (table 0 30 funcref))
   213    (func $f)
   214    (elem (i32.const 1) $f)
   215  )
   216  
   217  ;; Invalid bounds for elements
   218  
   219  (assert_trap
   220    (module
   221      (table 0 funcref)
   222      (func $f)
   223      (elem (i32.const 0) $f)
   224    )
   225    "out of bounds table access"
   226  )
   227  
   228  (assert_trap
   229    (module
   230      (table 0 0 funcref)
   231      (func $f)
   232      (elem (i32.const 0) $f)
   233    )
   234    "out of bounds table access"
   235  )
   236  
   237  (assert_trap
   238    (module
   239      (table 0 1 funcref)
   240      (func $f)
   241      (elem (i32.const 0) $f)
   242    )
   243    "out of bounds table access"
   244  )
   245  
   246  (assert_trap
   247    (module
   248      (table 0 funcref)
   249      (elem (i32.const 1))
   250    )
   251    "out of bounds table access"
   252  )
   253  (assert_trap
   254    (module
   255      (table 10 funcref)
   256      (func $f)
   257      (elem (i32.const 10) $f)
   258    )
   259    "out of bounds table access"
   260  )
   261  (assert_trap
   262    (module
   263      (import "spectest" "table" (table 10 funcref))
   264      (func $f)
   265      (elem (i32.const 10) $f)
   266    )
   267    "out of bounds table access"
   268  )
   269  
   270  (assert_trap
   271    (module
   272      (table 10 20 funcref)
   273      (func $f)
   274      (elem (i32.const 10) $f)
   275    )
   276    "out of bounds table access"
   277  )
   278  (assert_trap
   279    (module
   280      (import "spectest" "table" (table 10 funcref))
   281      (func $f)
   282      (elem (i32.const 10) $f)
   283    )
   284    "out of bounds table access"
   285  )
   286  
   287  (assert_trap
   288    (module
   289      (table 10 funcref)
   290      (func $f)
   291      (elem (i32.const -1) $f)
   292    )
   293    "out of bounds table access"
   294  )
   295  (assert_trap
   296    (module
   297      (import "spectest" "table" (table 10 funcref))
   298      (func $f)
   299      (elem (i32.const -1) $f)
   300    )
   301    "out of bounds table access"
   302  )
   303  
   304  (assert_trap
   305    (module
   306      (table 10 funcref)
   307      (func $f)
   308      (elem (i32.const -10) $f)
   309    )
   310    "out of bounds table access"
   311  )
   312  (assert_trap
   313    (module
   314      (import "spectest" "table" (table 10 funcref))
   315      (func $f)
   316      (elem (i32.const -10) $f)
   317    )
   318    "out of bounds table access"
   319  )
   320  
   321  ;; Implicitly dropped elements
   322  
   323  (module
   324    (table 10 funcref)
   325    (elem $e (i32.const 0) func $f)
   326    (func $f)
   327    (func (export "init")
   328      (table.init $e (i32.const 0) (i32.const 0) (i32.const 1))
   329    )
   330  )
   331  (assert_trap (invoke "init") "out of bounds table access")
   332  
   333  (module
   334    (table 10 funcref)
   335    (elem $e declare func $f)
   336    (func $f)
   337    (func (export "init")
   338      (table.init $e (i32.const 0) (i32.const 0) (i32.const 1))
   339    )
   340  )
   341  (assert_trap (invoke "init") "out of bounds table access")
   342  
   343  ;; Element without table
   344  
   345  (assert_invalid
   346    (module
   347      (func $f)
   348      (elem (i32.const 0) $f)
   349    )
   350    "unknown table"
   351  )
   352  
   353  ;; Invalid offsets
   354  
   355  (assert_invalid
   356    (module
   357      (table 1 funcref)
   358      (elem (i64.const 0))
   359    )
   360    "type mismatch"
   361  )
   362  
   363  (assert_invalid
   364    (module
   365      (table 1 funcref)
   366      (elem (ref.null func))
   367    )
   368    "type mismatch"
   369  )
   370  
   371  (assert_invalid
   372    (module 
   373      (table 1 funcref)
   374      (elem (offset (;empty instruction sequence;)))
   375    )
   376    "type mismatch"
   377  )
   378  
   379  (assert_invalid
   380    (module
   381      (table 1 funcref)
   382      (elem (offset (i32.const 0) (i32.const 0)))
   383    )
   384    "type mismatch"
   385  )
   386  
   387  (assert_invalid
   388    (module
   389      (global (import "test" "global-i32") i32)
   390      (table 1 funcref)
   391      (elem (offset (global.get 0) (global.get 0)))
   392    )
   393    "type mismatch"
   394  )
   395  
   396  (assert_invalid
   397    (module
   398      (global (import "test" "global-i32") i32)
   399      (table 1 funcref)
   400      (elem (offset (global.get 0) (i32.const 0)))
   401    )
   402    "type mismatch"
   403  )
   404  
   405  
   406  (assert_invalid
   407    (module
   408      (table 1 funcref)
   409      (elem (i32.ctz (i32.const 0)))
   410    )
   411    "constant expression required"
   412  )
   413  
   414  (assert_invalid
   415    (module
   416      (table 1 funcref)
   417      (elem (nop))
   418    )
   419    "constant expression required"
   420  )
   421  
   422  (assert_invalid
   423    (module
   424      (table 1 funcref)
   425      (elem (offset (nop) (i32.const 0)))
   426    )
   427    "constant expression required"
   428  )
   429  
   430  (assert_invalid
   431    (module
   432      (table 1 funcref)
   433      (elem (offset (i32.const 0) (nop)))
   434    )
   435    "constant expression required"
   436  )
   437  
   438  (assert_invalid
   439    (module
   440      (global $g (import "test" "g") (mut i32))
   441      (table 1 funcref)
   442      (elem (global.get $g))
   443    )
   444    "constant expression required"
   445  )
   446  
   447  (assert_invalid
   448     (module 
   449       (table 1 funcref)
   450       (elem (global.get 0))
   451     )
   452     "unknown global 0"
   453  )
   454  
   455  (assert_invalid
   456     (module
   457       (global (import "test" "global-i32") i32)
   458       (table 1 funcref)
   459       (elem (global.get 1))
   460     )
   461     "unknown global 1"
   462  )
   463  
   464  (assert_invalid
   465     (module 
   466       (global (import "test" "global-mut-i32") (mut i32))
   467       (table 1 funcref)
   468       (elem (global.get 0))
   469     )
   470     "constant expression required"
   471  )
   472  
   473  ;; Invalid elements
   474  
   475  (assert_invalid
   476    (module
   477      (table 1 funcref)
   478      (elem (i32.const 0) funcref (ref.null extern))
   479    )
   480    "type mismatch"
   481  )
   482  
   483  (assert_invalid
   484    (module
   485      (table 1 funcref)
   486      (elem (i32.const 0) funcref (item (ref.null func) (ref.null func)))
   487    )
   488    "type mismatch"
   489  )
   490  
   491  (assert_invalid
   492    (module
   493      (table 1 funcref)
   494      (elem (i32.const 0) funcref (i32.const 0))
   495    )
   496    "type mismatch"
   497  )
   498  
   499  (assert_invalid
   500    (module
   501      (table 1 funcref)
   502      (elem (i32.const 0) funcref (item (i32.const 0)))
   503    )
   504    "type mismatch"
   505  )
   506  
   507  (assert_invalid
   508    (module
   509      (table 1 funcref)
   510      (elem (i32.const 0) funcref (item (call $f)))
   511      (func $f (result funcref) (ref.null func))
   512    )
   513    "constant expression required"
   514  )
   515  
   516  (assert_invalid
   517    (module
   518      (table 1 funcref)
   519      (elem (i32.const 0) funcref (item (i32.add (i32.const 0) (i32.const 1))))
   520    )
   521    "constant expression required"
   522  )
   523  
   524  ;; Two elements target the same slot
   525  
   526  (module
   527    (type $out-i32 (func (result i32)))
   528    (table 10 funcref)
   529    (elem (i32.const 9) $const-i32-a)
   530    (elem (i32.const 9) $const-i32-b)
   531    (func $const-i32-a (type $out-i32) (i32.const 65))
   532    (func $const-i32-b (type $out-i32) (i32.const 66))
   533    (func (export "call-overwritten") (type $out-i32)
   534      (call_indirect (type $out-i32) (i32.const 9))
   535    )
   536  )
   537  (assert_return (invoke "call-overwritten") (i32.const 66))
   538  
   539  (module
   540    (type $out-i32 (func (result i32)))
   541    (import "spectest" "table" (table 10 funcref))
   542    (elem (i32.const 9) $const-i32-a)
   543    (elem (i32.const 9) $const-i32-b)
   544    (func $const-i32-a (type $out-i32) (i32.const 65))
   545    (func $const-i32-b (type $out-i32) (i32.const 66))
   546    (func (export "call-overwritten-element") (type $out-i32)
   547      (call_indirect (type $out-i32) (i32.const 9))
   548    )
   549  )
   550  (assert_return (invoke "call-overwritten-element") (i32.const 66))
   551  
   552  ;; Element sections across multiple modules change the same table
   553  
   554  (module $module1
   555    (type $out-i32 (func (result i32)))
   556    (table (export "shared-table") 10 funcref)
   557    (elem (i32.const 8) $const-i32-a)
   558    (elem (i32.const 9) $const-i32-b)
   559    (func $const-i32-a (type $out-i32) (i32.const 65))
   560    (func $const-i32-b (type $out-i32) (i32.const 66))
   561    (func (export "call-7") (type $out-i32)
   562      (call_indirect (type $out-i32) (i32.const 7))
   563    )
   564    (func (export "call-8") (type $out-i32)
   565      (call_indirect (type $out-i32) (i32.const 8))
   566    )
   567    (func (export "call-9") (type $out-i32)
   568      (call_indirect (type $out-i32) (i32.const 9))
   569    )
   570  )
   571  
   572  (register "module1" $module1)
   573  
   574  (assert_trap (invoke $module1 "call-7") "uninitialized element")
   575  (assert_return (invoke $module1 "call-8") (i32.const 65))
   576  (assert_return (invoke $module1 "call-9") (i32.const 66))
   577  
   578  (module $module2
   579    (type $out-i32 (func (result i32)))
   580    (import "module1" "shared-table" (table 10 funcref))
   581    (elem (i32.const 7) $const-i32-c)
   582    (elem (i32.const 8) $const-i32-d)
   583    (func $const-i32-c (type $out-i32) (i32.const 67))
   584    (func $const-i32-d (type $out-i32) (i32.const 68))
   585  )
   586  
   587  (assert_return (invoke $module1 "call-7") (i32.const 67))
   588  (assert_return (invoke $module1 "call-8") (i32.const 68))
   589  (assert_return (invoke $module1 "call-9") (i32.const 66))
   590  
   591  (module $module3
   592    (type $out-i32 (func (result i32)))
   593    (import "module1" "shared-table" (table 10 funcref))
   594    (elem (i32.const 8) $const-i32-e)
   595    (elem (i32.const 9) $const-i32-f)
   596    (func $const-i32-e (type $out-i32) (i32.const 69))
   597    (func $const-i32-f (type $out-i32) (i32.const 70))
   598  )
   599  
   600  (assert_return (invoke $module1 "call-7") (i32.const 67))
   601  (assert_return (invoke $module1 "call-8") (i32.const 69))
   602  (assert_return (invoke $module1 "call-9") (i32.const 70))
   603  
   604  ;; Element segments must match element type of table
   605  
   606  (assert_invalid
   607    (module (func $f) (table 1 externref) (elem (i32.const 0) $f))
   608    "type mismatch"
   609  )
   610  
   611  (assert_invalid
   612    (module (table 1 funcref) (elem (i32.const 0) externref (ref.null extern)))
   613    "type mismatch"
   614  )
   615  
   616  (assert_invalid
   617    (module
   618      (func $f)
   619      (table $t 1 externref)
   620      (elem $e funcref (ref.func $f))
   621      (func (table.init $t $e (i32.const 0) (i32.const 0) (i32.const 1))))
   622    "type mismatch"
   623  )
   624  
   625  (assert_invalid
   626    (module
   627      (table $t 1 funcref)
   628      (elem $e externref (ref.null extern))
   629      (func (table.init $t $e (i32.const 0) (i32.const 0) (i32.const 1))))
   630    "type mismatch"
   631  )
   632  
   633  ;; Initializing a table with an externref-type element segment
   634  
   635  (module $m
   636  	(table $t (export "table") 2 externref)
   637  	(func (export "get") (param $i i32) (result externref)
   638  	      (table.get $t (local.get $i)))
   639  	(func (export "set") (param $i i32) (param $x externref)
   640  	      (table.set $t (local.get $i) (local.get $x))))
   641  
   642  (register "exporter" $m)
   643  
   644  (assert_return (invoke $m "get" (i32.const 0)) (ref.null extern))
   645  (assert_return (invoke $m "get" (i32.const 1)) (ref.null extern))
   646  
   647  (assert_return (invoke $m "set" (i32.const 0) (ref.extern 42)))
   648  (assert_return (invoke $m "set" (i32.const 1) (ref.extern 137)))
   649  
   650  (assert_return (invoke $m "get" (i32.const 0)) (ref.extern 42))
   651  (assert_return (invoke $m "get" (i32.const 1)) (ref.extern 137))
   652  
   653  (module
   654    (import "exporter" "table" (table $t 2 externref))
   655    (elem (i32.const 0) externref (ref.null extern)))
   656  
   657  (assert_return (invoke $m "get" (i32.const 0)) (ref.null extern))
   658  (assert_return (invoke $m "get" (i32.const 1)) (ref.extern 137))