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

     1  (module
     2      (memory 0)
     3  
     4      (func (export "load_at_zero") (result i32) (i32.load (i32.const 0)))
     5      (func (export "store_at_zero") (i32.store (i32.const 0) (i32.const 2)))
     6  
     7      (func (export "load_at_page_size") (result i32) (i32.load (i32.const 0x10000)))
     8      (func (export "store_at_page_size") (i32.store (i32.const 0x10000) (i32.const 3)))
     9  
    10      (func (export "grow") (param $sz i32) (result i32) (memory.grow (local.get $sz)))
    11      (func (export "size") (result i32) (memory.size))
    12  )
    13  
    14  (assert_return (invoke "size") (i32.const 0))
    15  (assert_trap (invoke "store_at_zero") "out of bounds memory access")
    16  (assert_trap (invoke "load_at_zero") "out of bounds memory access")
    17  (assert_trap (invoke "store_at_page_size") "out of bounds memory access")
    18  (assert_trap (invoke "load_at_page_size") "out of bounds memory access")
    19  (assert_return (invoke "grow" (i32.const 1)) (i32.const 0))
    20  (assert_return (invoke "size") (i32.const 1))
    21  (assert_return (invoke "load_at_zero") (i32.const 0))
    22  (assert_return (invoke "store_at_zero"))
    23  (assert_return (invoke "load_at_zero") (i32.const 2))
    24  (assert_trap (invoke "store_at_page_size") "out of bounds memory access")
    25  (assert_trap (invoke "load_at_page_size") "out of bounds memory access")
    26  (assert_return (invoke "grow" (i32.const 4)) (i32.const 1))
    27  (assert_return (invoke "size") (i32.const 5))
    28  (assert_return (invoke "load_at_zero") (i32.const 2))
    29  (assert_return (invoke "store_at_zero"))
    30  (assert_return (invoke "load_at_zero") (i32.const 2))
    31  (assert_return (invoke "load_at_page_size") (i32.const 0))
    32  (assert_return (invoke "store_at_page_size"))
    33  (assert_return (invoke "load_at_page_size") (i32.const 3))
    34  
    35  
    36  (module
    37    (memory 0)
    38    (func (export "grow") (param i32) (result i32) (memory.grow (local.get 0)))
    39  )
    40  
    41  (assert_return (invoke "grow" (i32.const 0)) (i32.const 0))
    42  (assert_return (invoke "grow" (i32.const 1)) (i32.const 0))
    43  (assert_return (invoke "grow" (i32.const 0)) (i32.const 1))
    44  (assert_return (invoke "grow" (i32.const 2)) (i32.const 1))
    45  (assert_return (invoke "grow" (i32.const 800)) (i32.const 3))
    46  (assert_return (invoke "grow" (i32.const 0x10000)) (i32.const -1))
    47  (assert_return (invoke "grow" (i32.const 64736)) (i32.const -1))
    48  (assert_return (invoke "grow" (i32.const 1)) (i32.const 803))
    49  
    50  (module
    51    (memory 0 10)
    52    (func (export "grow") (param i32) (result i32) (memory.grow (local.get 0)))
    53  )
    54  
    55  (assert_return (invoke "grow" (i32.const 0)) (i32.const 0))
    56  (assert_return (invoke "grow" (i32.const 1)) (i32.const 0))
    57  (assert_return (invoke "grow" (i32.const 1)) (i32.const 1))
    58  (assert_return (invoke "grow" (i32.const 2)) (i32.const 2))
    59  (assert_return (invoke "grow" (i32.const 6)) (i32.const 4))
    60  (assert_return (invoke "grow" (i32.const 0)) (i32.const 10))
    61  (assert_return (invoke "grow" (i32.const 1)) (i32.const -1))
    62  (assert_return (invoke "grow" (i32.const 0x10000)) (i32.const -1))
    63  
    64  ;; Test that newly allocated memory (program start and memory.grow) is zeroed
    65  
    66  (module
    67    (memory 1)
    68    (func (export "grow") (param i32) (result i32)
    69      (memory.grow (local.get 0))
    70    )
    71    (func (export "check-memory-zero") (param i32 i32) (result i32)
    72      (local i32)
    73      (local.set 2 (i32.const 1))
    74      (block
    75        (loop
    76          (local.set 2 (i32.load8_u (local.get 0)))
    77          (br_if 1 (i32.ne (local.get 2) (i32.const 0)))
    78          (br_if 1 (i32.ge_u (local.get 0) (local.get 1)))
    79          (local.set 0 (i32.add (local.get 0) (i32.const 1)))
    80          (br_if 0 (i32.le_u (local.get 0) (local.get 1)))
    81        )
    82      )
    83      (local.get 2)
    84    )
    85  )
    86  
    87  (assert_return (invoke "check-memory-zero" (i32.const 0) (i32.const 0xffff)) (i32.const 0))
    88  (assert_return (invoke "grow" (i32.const 1)) (i32.const 1))
    89  (assert_return (invoke "check-memory-zero" (i32.const 0x10000) (i32.const 0x1_ffff)) (i32.const 0))
    90  (assert_return (invoke "grow" (i32.const 1)) (i32.const 2))
    91  (assert_return (invoke "check-memory-zero" (i32.const 0x20000) (i32.const 0x2_ffff)) (i32.const 0))
    92  (assert_return (invoke "grow" (i32.const 1)) (i32.const 3))
    93  (assert_return (invoke "check-memory-zero" (i32.const 0x30000) (i32.const 0x3_ffff)) (i32.const 0))
    94  (assert_return (invoke "grow" (i32.const 1)) (i32.const 4))
    95  (assert_return (invoke "check-memory-zero" (i32.const 0x40000) (i32.const 0x4_ffff)) (i32.const 0))
    96  (assert_return (invoke "grow" (i32.const 1)) (i32.const 5))
    97  (assert_return (invoke "check-memory-zero" (i32.const 0x50000) (i32.const 0x5_ffff)) (i32.const 0))
    98  
    99  ;; As the argument of control constructs and instructions
   100  
   101  (module
   102    (memory 1)
   103  
   104    (func (export "as-br-value") (result i32)
   105      (block (result i32) (br 0 (memory.grow (i32.const 0))))
   106    )
   107  
   108    (func (export "as-br_if-cond")
   109      (block (br_if 0 (memory.grow (i32.const 0))))
   110    )
   111    (func (export "as-br_if-value") (result i32)
   112      (block (result i32)
   113        (drop (br_if 0 (memory.grow (i32.const 0)) (i32.const 1))) (i32.const 7)
   114      )
   115    )
   116    (func (export "as-br_if-value-cond") (result i32)
   117      (block (result i32)
   118        (drop (br_if 0 (i32.const 6) (memory.grow (i32.const 0)))) (i32.const 7)
   119      )
   120    )
   121  
   122    (func (export "as-br_table-index")
   123      (block (br_table 0 0 0 (memory.grow (i32.const 0))))
   124    )
   125    (func (export "as-br_table-value") (result i32)
   126      (block (result i32)
   127        (br_table 0 0 0 (memory.grow (i32.const 0)) (i32.const 1)) (i32.const 7)
   128      )
   129    )
   130    (func (export "as-br_table-value-index") (result i32)
   131      (block (result i32)
   132        (br_table 0 0 (i32.const 6) (memory.grow (i32.const 0))) (i32.const 7)
   133      )
   134    )
   135  
   136    (func (export "as-return-value") (result i32)
   137      (return (memory.grow (i32.const 0)))
   138    )
   139  
   140    (func (export "as-if-cond") (result i32)
   141      (if (result i32) (memory.grow (i32.const 0))
   142        (then (i32.const 0)) (else (i32.const 1))
   143      )
   144    )
   145    (func (export "as-if-then") (result i32)
   146      (if (result i32) (i32.const 1)
   147        (then (memory.grow (i32.const 0))) (else (i32.const 0))
   148      )
   149    )
   150    (func (export "as-if-else") (result i32)
   151      (if (result i32) (i32.const 0)
   152        (then (i32.const 0)) (else (memory.grow (i32.const 0)))
   153      )
   154    )
   155  
   156    (func (export "as-select-first") (param i32 i32) (result i32)
   157      (select (memory.grow (i32.const 0)) (local.get 0) (local.get 1))
   158    )
   159    (func (export "as-select-second") (param i32 i32) (result i32)
   160      (select (local.get 0) (memory.grow (i32.const 0)) (local.get 1))
   161    )
   162    (func (export "as-select-cond") (result i32)
   163      (select (i32.const 0) (i32.const 1) (memory.grow (i32.const 0)))
   164    )
   165  
   166    (func $f (param i32 i32 i32) (result i32) (i32.const -1))
   167    (func (export "as-call-first") (result i32)
   168      (call $f (memory.grow (i32.const 0)) (i32.const 2) (i32.const 3))
   169    )
   170    (func (export "as-call-mid") (result i32)
   171      (call $f (i32.const 1) (memory.grow (i32.const 0)) (i32.const 3))
   172    )
   173    (func (export "as-call-last") (result i32)
   174      (call $f (i32.const 1) (i32.const 2) (memory.grow (i32.const 0)))
   175    )
   176  
   177    (type $sig (func (param i32 i32 i32) (result i32)))
   178    (table funcref (elem $f))
   179    (func (export "as-call_indirect-first") (result i32)
   180      (call_indirect (type $sig)
   181        (memory.grow (i32.const 0)) (i32.const 2) (i32.const 3) (i32.const 0)
   182      )
   183    )
   184    (func (export "as-call_indirect-mid") (result i32)
   185      (call_indirect (type $sig)
   186        (i32.const 1) (memory.grow (i32.const 0)) (i32.const 3) (i32.const 0)
   187      )
   188    )
   189    (func (export "as-call_indirect-last") (result i32)
   190      (call_indirect (type $sig)
   191        (i32.const 1) (i32.const 2) (memory.grow (i32.const 0)) (i32.const 0)
   192      )
   193    )
   194    (func (export "as-call_indirect-index") (result i32)
   195      (call_indirect (type $sig)
   196        (i32.const 1) (i32.const 2) (i32.const 3) (memory.grow (i32.const 0))
   197      )
   198    )
   199  
   200    (func (export "as-local.set-value") (local i32)
   201      (local.set 0 (memory.grow (i32.const 0)))
   202    )
   203    (func (export "as-local.tee-value") (result i32) (local i32)
   204      (local.tee 0 (memory.grow (i32.const 0)))
   205    )
   206    (global $g (mut i32) (i32.const 0))
   207    (func (export "as-global.set-value") (local i32)
   208      (global.set $g (memory.grow (i32.const 0)))
   209    )
   210  
   211    (func (export "as-load-address") (result i32)
   212      (i32.load (memory.grow (i32.const 0)))
   213    )
   214    (func (export "as-loadN-address") (result i32)
   215      (i32.load8_s (memory.grow (i32.const 0)))
   216    )
   217  
   218    (func (export "as-store-address")
   219      (i32.store (memory.grow (i32.const 0)) (i32.const 7))
   220    )
   221    (func (export "as-store-value")
   222      (i32.store (i32.const 2) (memory.grow (i32.const 0)))
   223    )
   224  
   225    (func (export "as-storeN-address")
   226      (i32.store8 (memory.grow (i32.const 0)) (i32.const 7))
   227    )
   228    (func (export "as-storeN-value")
   229      (i32.store16 (i32.const 2) (memory.grow (i32.const 0)))
   230    )
   231  
   232    (func (export "as-unary-operand") (result i32)
   233      (i32.clz (memory.grow (i32.const 0)))
   234    )
   235  
   236    (func (export "as-binary-left") (result i32)
   237      (i32.add (memory.grow (i32.const 0)) (i32.const 10))
   238    )
   239    (func (export "as-binary-right") (result i32)
   240      (i32.sub (i32.const 10) (memory.grow (i32.const 0)))
   241    )
   242  
   243    (func (export "as-test-operand") (result i32)
   244      (i32.eqz (memory.grow (i32.const 0)))
   245    )
   246  
   247    (func (export "as-compare-left") (result i32)
   248      (i32.le_s (memory.grow (i32.const 0)) (i32.const 10))
   249    )
   250    (func (export "as-compare-right") (result i32)
   251      (i32.ne (i32.const 10) (memory.grow (i32.const 0)))
   252    )
   253  
   254    (func (export "as-memory.grow-size") (result i32)
   255      (memory.grow (memory.grow (i32.const 0)))
   256    )
   257  )
   258  
   259  (assert_return (invoke "as-br-value") (i32.const 1))
   260  
   261  (assert_return (invoke "as-br_if-cond"))
   262  (assert_return (invoke "as-br_if-value") (i32.const 1))
   263  (assert_return (invoke "as-br_if-value-cond") (i32.const 6))
   264  
   265  (assert_return (invoke "as-br_table-index"))
   266  (assert_return (invoke "as-br_table-value") (i32.const 1))
   267  (assert_return (invoke "as-br_table-value-index") (i32.const 6))
   268  
   269  (assert_return (invoke "as-return-value") (i32.const 1))
   270  
   271  (assert_return (invoke "as-if-cond") (i32.const 0))
   272  (assert_return (invoke "as-if-then") (i32.const 1))
   273  (assert_return (invoke "as-if-else") (i32.const 1))
   274  
   275  (assert_return (invoke "as-select-first" (i32.const 0) (i32.const 1)) (i32.const 1))
   276  (assert_return (invoke "as-select-second" (i32.const 0) (i32.const 0)) (i32.const 1))
   277  (assert_return (invoke "as-select-cond") (i32.const 0))
   278  
   279  (assert_return (invoke "as-call-first") (i32.const -1))
   280  (assert_return (invoke "as-call-mid") (i32.const -1))
   281  (assert_return (invoke "as-call-last") (i32.const -1))
   282  
   283  (assert_return (invoke "as-call_indirect-first") (i32.const -1))
   284  (assert_return (invoke "as-call_indirect-mid") (i32.const -1))
   285  (assert_return (invoke "as-call_indirect-last") (i32.const -1))
   286  (assert_trap (invoke "as-call_indirect-index") "undefined element")
   287  
   288  (assert_return (invoke "as-local.set-value"))
   289  (assert_return (invoke "as-local.tee-value") (i32.const 1))
   290  (assert_return (invoke "as-global.set-value"))
   291  
   292  (assert_return (invoke "as-load-address") (i32.const 0))
   293  (assert_return (invoke "as-loadN-address") (i32.const 0))
   294  (assert_return (invoke "as-store-address"))
   295  (assert_return (invoke "as-store-value"))
   296  (assert_return (invoke "as-storeN-address"))
   297  (assert_return (invoke "as-storeN-value"))
   298  
   299  (assert_return (invoke "as-unary-operand") (i32.const 31))
   300  
   301  (assert_return (invoke "as-binary-left") (i32.const 11))
   302  (assert_return (invoke "as-binary-right") (i32.const 9))
   303  
   304  (assert_return (invoke "as-test-operand") (i32.const 0))
   305  
   306  (assert_return (invoke "as-compare-left") (i32.const 1))
   307  (assert_return (invoke "as-compare-right") (i32.const 1))
   308  
   309  (assert_return (invoke "as-memory.grow-size") (i32.const 1))
   310  
   311  
   312  (assert_invalid
   313    (module
   314      (memory 0)
   315      (func $type-size-empty-vs-i32 (result i32)
   316        (memory.grow)
   317      )
   318    )
   319    "type mismatch"
   320  )
   321  (assert_invalid
   322    (module
   323      (memory 0)
   324      (func $type-size-empty-vs-i32-in-block (result i32)
   325        (i32.const 0)
   326        (block (result i32) (memory.grow))
   327      )
   328    )
   329    "type mismatch"
   330  )
   331  (assert_invalid
   332    (module
   333      (memory 0)
   334      (func $type-size-empty-vs-i32-in-loop (result i32)
   335        (i32.const 0)
   336        (loop (result i32) (memory.grow))
   337      )
   338    )
   339    "type mismatch"
   340  )
   341  (assert_invalid
   342    (module
   343      (memory 0)
   344      (func $type-size-empty-vs-i32-in-then (result i32)
   345        (i32.const 0) (i32.const 0)
   346        (if (result i32) (then (memory.grow)))
   347      )
   348    )
   349    "type mismatch"
   350  )
   351  
   352  (assert_invalid
   353    (module
   354      (memory 1)
   355      (func $type-size-f32-vs-i32 (result i32)
   356        (memory.grow (f32.const 0))
   357      )
   358    )
   359    "type mismatch"
   360  )
   361  
   362  (assert_invalid
   363    (module
   364      (memory 1)
   365      (func $type-result-i32-vs-empty
   366        (memory.grow (i32.const 0))
   367      )
   368    )
   369    "type mismatch"
   370  )
   371  (assert_invalid
   372    (module
   373      (memory 1)
   374      (func $type-result-i32-vs-f32 (result f32)
   375        (memory.grow (i32.const 0))
   376      )
   377    )
   378    "type mismatch"
   379  )