github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/integration_test/spectest/v2/testdata/global.wast (about)

     1  ;; Test globals
     2  
     3  (module
     4    (global (import "spectest" "global_i32") i32)
     5    (global (import "spectest" "global_i64") i64)
     6  
     7    (global $a i32 (i32.const -2))
     8    (global (;3;) f32 (f32.const -3))
     9    (global (;4;) f64 (f64.const -4))
    10    (global $b i64 (i64.const -5))
    11  
    12    (global $x (mut i32) (i32.const -12))
    13    (global (;7;) (mut f32) (f32.const -13))
    14    (global (;8;) (mut f64) (f64.const -14))
    15    (global $y (mut i64) (i64.const -15))
    16  
    17    (global $z1 i32 (global.get 0))
    18    (global $z2 i64 (global.get 1))
    19  
    20    (global $r externref (ref.null extern))
    21    (global $mr (mut externref) (ref.null extern))
    22    (global funcref (ref.null func))
    23  
    24    (func (export "get-a") (result i32) (global.get $a))
    25    (func (export "get-b") (result i64) (global.get $b))
    26    (func (export "get-r") (result externref) (global.get $r))
    27    (func (export "get-mr") (result externref) (global.get $mr))
    28    (func (export "get-x") (result i32) (global.get $x))
    29    (func (export "get-y") (result i64) (global.get $y))
    30    (func (export "get-z1") (result i32) (global.get $z1))
    31    (func (export "get-z2") (result i64) (global.get $z2))
    32    (func (export "set-x") (param i32) (global.set $x (local.get 0)))
    33    (func (export "set-y") (param i64) (global.set $y (local.get 0)))
    34    (func (export "set-mr") (param externref) (global.set $mr (local.get 0)))
    35  
    36    (func (export "get-3") (result f32) (global.get 3))
    37    (func (export "get-4") (result f64) (global.get 4))
    38    (func (export "get-7") (result f32) (global.get 7))
    39    (func (export "get-8") (result f64) (global.get 8))
    40    (func (export "set-7") (param f32) (global.set 7 (local.get 0)))
    41    (func (export "set-8") (param f64) (global.set 8 (local.get 0)))
    42  
    43    ;; As the argument of control constructs and instructions
    44  
    45    (memory 1)
    46  
    47    (func $dummy)
    48  
    49    (func (export "as-select-first") (result i32)
    50      (select (global.get $x) (i32.const 2) (i32.const 3))
    51    )
    52    (func (export "as-select-mid") (result i32)
    53      (select (i32.const 2) (global.get $x) (i32.const 3))
    54    )
    55    (func (export "as-select-last") (result i32)
    56      (select (i32.const 2) (i32.const 3) (global.get $x))
    57    )
    58  
    59    (func (export "as-loop-first") (result i32)
    60      (loop (result i32)
    61        (global.get $x) (call $dummy) (call $dummy)
    62      )
    63    )
    64    (func (export "as-loop-mid") (result i32)
    65      (loop (result i32)
    66        (call $dummy) (global.get $x) (call $dummy)
    67      )
    68    )
    69    (func (export "as-loop-last") (result i32)
    70      (loop (result i32)
    71        (call $dummy) (call $dummy) (global.get $x)
    72      )
    73    )
    74  
    75    (func (export "as-if-condition") (result i32)
    76      (if (result i32) (global.get $x)
    77        (then (call $dummy) (i32.const 2))
    78        (else (call $dummy) (i32.const 3))
    79      )
    80    )
    81    (func (export "as-if-then") (result i32)
    82      (if (result i32) (i32.const 1)
    83        (then (global.get $x)) (else (i32.const 2))
    84      )
    85    )
    86    (func (export "as-if-else") (result i32)
    87      (if (result i32) (i32.const 0)
    88        (then (i32.const 2)) (else (global.get $x))
    89      )
    90    )
    91  
    92    (func (export "as-br_if-first") (result i32)
    93      (block (result i32)
    94        (br_if 0 (global.get $x) (i32.const 2))
    95        (return (i32.const 3))
    96      )
    97    )
    98    (func (export "as-br_if-last") (result i32)
    99      (block (result i32)
   100        (br_if 0 (i32.const 2) (global.get $x))
   101        (return (i32.const 3))
   102      )
   103    )
   104  
   105    (func (export "as-br_table-first") (result i32)
   106      (block (result i32)
   107        (global.get $x) (i32.const 2) (br_table 0 0)
   108      )
   109    )
   110    (func (export "as-br_table-last") (result i32)
   111      (block (result i32)
   112        (i32.const 2) (global.get $x) (br_table 0 0)
   113      )
   114    )
   115  
   116    (func $func (param i32 i32) (result i32) (local.get 0))
   117    (type $check (func (param i32 i32) (result i32)))
   118    (table funcref (elem $func))
   119    (func (export "as-call_indirect-first") (result i32)
   120      (block (result i32)
   121        (call_indirect (type $check)
   122          (global.get $x) (i32.const 2) (i32.const 0)
   123        )
   124      )
   125    )
   126    (func (export "as-call_indirect-mid") (result i32)
   127      (block (result i32)
   128        (call_indirect (type $check)
   129          (i32.const 2) (global.get $x) (i32.const 0)
   130        )
   131      )
   132    )
   133   (func (export "as-call_indirect-last") (result i32)
   134      (block (result i32)
   135        (call_indirect (type $check)
   136          (i32.const 2) (i32.const 0) (global.get $x)
   137        )
   138      )
   139    )
   140  
   141    (func (export "as-store-first")
   142      (global.get $x) (i32.const 1) (i32.store)
   143    )
   144    (func (export "as-store-last")
   145      (i32.const 0) (global.get $x) (i32.store)
   146    )
   147    (func (export "as-load-operand") (result i32)
   148      (i32.load (global.get $x))
   149    )
   150    (func (export "as-memory.grow-value") (result i32)
   151      (memory.grow (global.get $x))
   152    )
   153  
   154    (func $f (param i32) (result i32) (local.get 0))
   155    (func (export "as-call-value") (result i32)
   156      (call $f (global.get $x))
   157    )
   158  
   159    (func (export "as-return-value") (result i32)
   160      (global.get $x) (return)
   161    )
   162    (func (export "as-drop-operand")
   163      (drop (global.get $x))
   164    )
   165    (func (export "as-br-value") (result i32)
   166      (block (result i32) (br 0 (global.get $x)))
   167    )
   168  
   169    (func (export "as-local.set-value") (param i32) (result i32)
   170      (local.set 0 (global.get $x))
   171      (local.get 0)
   172    )
   173    (func (export "as-local.tee-value") (param i32) (result i32)
   174      (local.tee 0 (global.get $x))
   175    )
   176    (func (export "as-global.set-value") (result i32)
   177      (global.set $x (global.get $x))
   178      (global.get $x)
   179    )
   180  
   181    (func (export "as-unary-operand") (result i32)
   182      (i32.eqz (global.get $x))
   183    )
   184    (func (export "as-binary-operand") (result i32)
   185      (i32.mul
   186        (global.get $x) (global.get $x)
   187      )
   188    )
   189    (func (export "as-compare-operand") (result i32)
   190      (i32.gt_u
   191        (global.get 0) (i32.const 1)
   192      )
   193    )
   194  )
   195  
   196  (assert_return (invoke "get-a") (i32.const -2))
   197  (assert_return (invoke "get-b") (i64.const -5))
   198  (assert_return (invoke "get-r") (ref.null extern))
   199  (assert_return (invoke "get-mr") (ref.null extern))
   200  (assert_return (invoke "get-x") (i32.const -12))
   201  (assert_return (invoke "get-y") (i64.const -15))
   202  (assert_return (invoke "get-z1") (i32.const 666))
   203  (assert_return (invoke "get-z2") (i64.const 666))
   204  
   205  (assert_return (invoke "get-3") (f32.const -3))
   206  (assert_return (invoke "get-4") (f64.const -4))
   207  (assert_return (invoke "get-7") (f32.const -13))
   208  (assert_return (invoke "get-8") (f64.const -14))
   209  
   210  (assert_return (invoke "set-x" (i32.const 6)))
   211  (assert_return (invoke "set-y" (i64.const 7)))
   212  
   213  (assert_return (invoke "set-7" (f32.const 8)))
   214  (assert_return (invoke "set-8" (f64.const 9)))
   215  
   216  (assert_return (invoke "get-x") (i32.const 6))
   217  (assert_return (invoke "get-y") (i64.const 7))
   218  (assert_return (invoke "get-7") (f32.const 8))
   219  (assert_return (invoke "get-8") (f64.const 9))
   220  
   221  (assert_return (invoke "set-7" (f32.const 8)))
   222  (assert_return (invoke "set-8" (f64.const 9)))
   223  (assert_return (invoke "set-mr" (ref.extern 10)))
   224  
   225  (assert_return (invoke "get-x") (i32.const 6))
   226  (assert_return (invoke "get-y") (i64.const 7))
   227  (assert_return (invoke "get-7") (f32.const 8))
   228  (assert_return (invoke "get-8") (f64.const 9))
   229  (assert_return (invoke "get-mr") (ref.extern 10))
   230  
   231  (assert_return (invoke "as-select-first") (i32.const 6))
   232  (assert_return (invoke "as-select-mid") (i32.const 2))
   233  (assert_return (invoke "as-select-last") (i32.const 2))
   234  
   235  (assert_return (invoke "as-loop-first") (i32.const 6))
   236  (assert_return (invoke "as-loop-mid") (i32.const 6))
   237  (assert_return (invoke "as-loop-last") (i32.const 6))
   238  
   239  (assert_return (invoke "as-if-condition") (i32.const 2))
   240  (assert_return (invoke "as-if-then") (i32.const 6))
   241  (assert_return (invoke "as-if-else") (i32.const 6))
   242  
   243  (assert_return (invoke "as-br_if-first") (i32.const 6))
   244  (assert_return (invoke "as-br_if-last") (i32.const 2))
   245  
   246  (assert_return (invoke "as-br_table-first") (i32.const 6))
   247  (assert_return (invoke "as-br_table-last") (i32.const 2))
   248  
   249  (assert_return (invoke "as-call_indirect-first") (i32.const 6))
   250  (assert_return (invoke "as-call_indirect-mid") (i32.const 2))
   251  (assert_trap (invoke "as-call_indirect-last") "undefined element")
   252  
   253  (assert_return (invoke "as-store-first"))
   254  (assert_return (invoke "as-store-last"))
   255  (assert_return (invoke "as-load-operand") (i32.const 1))
   256  (assert_return (invoke "as-memory.grow-value") (i32.const 1))
   257  
   258  (assert_return (invoke "as-call-value") (i32.const 6))
   259  
   260  (assert_return (invoke "as-return-value") (i32.const 6))
   261  (assert_return (invoke "as-drop-operand"))
   262  (assert_return (invoke "as-br-value") (i32.const 6))
   263  
   264  (assert_return (invoke "as-local.set-value" (i32.const 1)) (i32.const 6))
   265  (assert_return (invoke "as-local.tee-value" (i32.const 1)) (i32.const 6))
   266  (assert_return (invoke "as-global.set-value") (i32.const 6))
   267  
   268  (assert_return (invoke "as-unary-operand") (i32.const 0))
   269  (assert_return (invoke "as-binary-operand") (i32.const 36))
   270  (assert_return (invoke "as-compare-operand") (i32.const 1))
   271  
   272  (assert_invalid
   273    (module (global f32 (f32.const 0)) (func (global.set 0 (f32.const 1))))
   274    "global is immutable"
   275  )
   276  
   277  (assert_invalid
   278    (module (import "spectest" "global_i32" (global i32)) (func (global.set 0 (i32.const 1))))
   279    "global is immutable"
   280  )
   281  
   282  ;; mutable globals can be exported
   283  (module (global (mut f32) (f32.const 0)) (export "a" (global 0)))
   284  (module (global (export "a") (mut f32) (f32.const 0)))
   285  
   286  (assert_invalid
   287    (module (global f32 (f32.neg (f32.const 0))))
   288    "constant expression required"
   289  )
   290  
   291  (assert_invalid
   292    (module (global f32 (local.get 0)))
   293    "constant expression required"
   294  )
   295  
   296  (assert_invalid
   297    (module (global f32 (f32.neg (f32.const 1))))
   298    "constant expression required"
   299  )
   300  
   301  (assert_invalid
   302    (module (global i32 (i32.const 0) (nop)))
   303    "constant expression required"
   304  )
   305  
   306  (assert_invalid
   307    (module (global i32 (i32.ctz (i32.const 0))))
   308    "constant expression required"
   309  )
   310  
   311  (assert_invalid
   312    (module (global i32 (nop)))
   313    "constant expression required"
   314  )
   315  
   316  (assert_invalid
   317    (module (global i32 (f32.const 0)))
   318    "type mismatch"
   319  )
   320  
   321  (assert_invalid
   322    (module (global i32 (i32.const 0) (i32.const 0)))
   323    "type mismatch"
   324  )
   325  
   326  (assert_invalid
   327    (module (global i32 (;empty instruction sequence;)))
   328    "type mismatch"
   329  )
   330  
   331  (assert_invalid
   332    (module (global (import "" "") externref) (global funcref (global.get 0)))
   333    "type mismatch"
   334  )
   335  
   336  (assert_invalid
   337    (module (global (import "test" "global-i32") i32) (global i32 (global.get 0) (global.get 0)))
   338    "type mismatch"
   339  )
   340  
   341  (assert_invalid
   342    (module (global (import "test" "global-i32") i32) (global i32 (i32.const 0) (global.get 0)))
   343    "type mismatch"
   344  )
   345  
   346  (assert_invalid
   347    (module (global i32 (global.get 0)))
   348    "unknown global"
   349  )
   350  
   351  (assert_invalid
   352    (module (global i32 (i32.const 0)) (global i32 (global.get 0)))
   353    "unknown global"
   354  )
   355  (assert_invalid
   356    (module (global $g i32 (i32.const 0)) (global i32 (global.get $g)))
   357    "unknown global"
   358  )
   359  
   360  (assert_invalid
   361    (module (global i32 (global.get 1)) (global i32 (i32.const 0)))
   362    "unknown global"
   363  )
   364  
   365  (assert_invalid
   366    (module (global (import "test" "global-i32") i32) (global i32 (global.get 2)))
   367    "unknown global"
   368  )
   369  
   370  (assert_invalid
   371    (module (global (import "test" "global-mut-i32") (mut i32)) (global i32 (global.get 0)))
   372    "constant expression required"
   373  )
   374  
   375  (module
   376    (import "spectest" "global_i32" (global i32))
   377  )
   378  (assert_malformed
   379    (module binary
   380      "\00asm" "\01\00\00\00"
   381      "\02\98\80\80\80\00"             ;; import section
   382        "\01"                          ;; length 1
   383        "\08\73\70\65\63\74\65\73\74"  ;; "spectest"
   384        "\0a\67\6c\6f\62\61\6c\5f\69\33\32" ;; "global_i32"
   385        "\03"                          ;; GlobalImport
   386        "\7f"                          ;; i32
   387        "\02"                          ;; malformed mutability
   388    )
   389    "malformed mutability"
   390  )
   391  (assert_malformed
   392    (module binary
   393      "\00asm" "\01\00\00\00"
   394      "\02\98\80\80\80\00"             ;; import section
   395        "\01"                          ;; length 1
   396        "\08\73\70\65\63\74\65\73\74"  ;; "spectest"
   397        "\0a\67\6c\6f\62\61\6c\5f\69\33\32" ;; "global_i32"
   398        "\03"                          ;; GlobalImport
   399        "\7f"                          ;; i32
   400        "\ff"                          ;; malformed mutability
   401    )
   402    "malformed mutability"
   403  )
   404  
   405  (module
   406    (global i32 (i32.const 0))
   407  )
   408  (assert_malformed
   409    (module binary
   410      "\00asm" "\01\00\00\00"
   411      "\06\86\80\80\80\00"  ;; global section
   412        "\01"               ;; length 1
   413        "\7f"               ;; i32
   414        "\02"               ;; malformed mutability
   415        "\41\00"            ;; i32.const 0
   416        "\0b"               ;; end
   417    )
   418    "malformed mutability"
   419  )
   420  (assert_malformed
   421    (module binary
   422      "\00asm" "\01\00\00\00"
   423      "\06\86\80\80\80\00"  ;; global section
   424        "\01"               ;; length 1
   425        "\7f"               ;; i32
   426        "\ff"               ;; malformed mutability
   427        "\41\00"            ;; i32.const 0
   428        "\0b"               ;; end
   429    )
   430    "malformed mutability"
   431  )
   432  
   433  ;; global.get with invalid index
   434  (assert_invalid
   435    (module (func (result i32) (global.get 0)))
   436    "unknown global"
   437  )
   438  
   439  (assert_invalid
   440    (module
   441      (global i32 (i32.const 0))
   442      (func (result i32) (global.get 1))
   443    )
   444    "unknown global"
   445  )
   446  
   447  (assert_invalid
   448    (module
   449      (import "spectest" "global_i32" (global i32))
   450      (func (result i32) (global.get 1))
   451    )
   452    "unknown global"
   453  )
   454  
   455  (assert_invalid
   456    (module
   457      (import "spectest" "global_i32" (global i32))
   458      (global i32 (i32.const 0))
   459      (func (result i32) (global.get 2))
   460    )
   461    "unknown global"
   462  )
   463  
   464  ;; global.set with invalid index
   465  (assert_invalid
   466    (module (func (i32.const 0) (global.set 0)))
   467    "unknown global"
   468  )
   469  
   470  (assert_invalid
   471    (module
   472      (global i32 (i32.const 0))
   473      (func (i32.const 0) (global.set 1))
   474    )
   475    "unknown global"
   476  )
   477  
   478  (assert_invalid
   479    (module
   480      (import "spectest" "global_i32" (global i32))
   481      (func (i32.const 0) (global.set 1))
   482    )
   483    "unknown global"
   484  )
   485  
   486  (assert_invalid
   487    (module
   488      (import "spectest" "global_i32" (global i32))
   489      (global i32 (i32.const 0))
   490      (func (i32.const 0) (global.set 2))
   491    )
   492    "unknown global"
   493  )
   494  
   495  
   496  (assert_invalid
   497    (module
   498      (global $x (mut i32) (i32.const 0))
   499      (func $type-global.set-value-empty
   500        (global.set $x)
   501      )
   502    )
   503    "type mismatch"
   504  )
   505  (assert_invalid
   506    (module
   507      (global $x (mut i32) (i32.const 0))
   508      (func $type-global.set-value-empty-in-block
   509        (i32.const 0)
   510        (block (global.set $x))
   511      )
   512    )
   513    "type mismatch"
   514  )
   515  (assert_invalid
   516    (module
   517      (global $x (mut i32) (i32.const 0))
   518      (func $type-global.set-value-empty-in-loop
   519        (i32.const 0)
   520        (loop (global.set $x))
   521      )
   522    )
   523    "type mismatch"
   524  )
   525  (assert_invalid
   526    (module
   527      (global $x (mut i32) (i32.const 0))
   528      (func $type-global.set-value-empty-in-then
   529        (i32.const 0) (i32.const 0)
   530        (if (then (global.set $x)))
   531      )
   532    )
   533    "type mismatch"
   534  )
   535  (assert_invalid
   536    (module
   537      (global $x (mut i32) (i32.const 0))
   538      (func $type-global.set-value-empty-in-else
   539        (i32.const 0) (i32.const 0)
   540        (if (result i32) (then (i32.const 0)) (else (global.set $x)))
   541      )
   542    )
   543    "type mismatch"
   544  )
   545  (assert_invalid
   546    (module
   547      (global $x (mut i32) (i32.const 0))
   548      (func $type-global.set-value-empty-in-br
   549        (i32.const 0)
   550        (block (br 0 (global.set $x)))
   551      )
   552    )
   553    "type mismatch"
   554  )
   555  (assert_invalid
   556    (module
   557      (global $x (mut i32) (i32.const 0))
   558      (func $type-global.set-value-empty-in-br_if
   559        (i32.const 0)
   560        (block (br_if 0 (global.set $x)))
   561      )
   562    )
   563    "type mismatch"
   564  )
   565  (assert_invalid
   566    (module
   567      (global $x (mut i32) (i32.const 0))
   568      (func $type-global.set-value-empty-in-br_table
   569        (i32.const 0)
   570        (block (br_table 0 (global.set $x)))
   571      )
   572    )
   573    "type mismatch"
   574  )
   575  (assert_invalid
   576    (module
   577      (global $x (mut i32) (i32.const 0))
   578      (func $type-global.set-value-empty-in-return
   579        (return (global.set $x))
   580      )
   581    )
   582    "type mismatch"
   583  )
   584  (assert_invalid
   585    (module
   586      (global $x (mut i32) (i32.const 0))
   587      (func $type-global.set-value-empty-in-select
   588        (select (global.set $x) (i32.const 1) (i32.const 2))
   589      )
   590    )
   591    "type mismatch"
   592  )
   593  (assert_invalid
   594    (module
   595      (global $x (mut i32) (i32.const 0))
   596      (func $type-global.set-value-empty-in-call
   597        (call 1 (global.set $x))
   598      )
   599      (func (param i32) (result i32) (local.get 0))
   600    )
   601    "type mismatch"
   602  )
   603  (assert_invalid
   604    (module
   605      (global $x (mut i32) (i32.const 0))
   606      (func $f (param i32) (result i32) (local.get 0))
   607      (type $sig (func (param i32) (result i32)))
   608      (table funcref (elem $f))
   609      (func $type-global.set-value-empty-in-call_indirect
   610        (block (result i32)
   611          (call_indirect (type $sig)
   612            (global.set $x) (i32.const 0)
   613          )
   614        )
   615      )
   616    )
   617    "type mismatch"
   618  )
   619  
   620  ;; Duplicate identifier errors
   621  
   622  (assert_malformed (module quote
   623    "(global $foo i32 (i32.const 0))"
   624    "(global $foo i32 (i32.const 0))")
   625    "duplicate global")
   626  (assert_malformed (module quote
   627    "(import \"\" \"\" (global $foo i32))"
   628    "(global $foo i32 (i32.const 0))")
   629    "duplicate global")
   630  (assert_malformed (module quote
   631    "(import \"\" \"\" (global $foo i32))"
   632    "(import \"\" \"\" (global $foo i32))")
   633    "duplicate global")