Skip to content

Commit

Permalink
Merge pull request #23 from WebAssembly/add-spec-tests
Browse files Browse the repository at this point in the history
Add spec tests for the custom-page-sizes proposal
  • Loading branch information
fitzgen authored Jun 12, 2024
2 parents a05c4dc + aab9e48 commit 1a81043
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 2 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci-interpreter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ jobs:
node-version: 19.x
- name: Build interpreter
run: cd interpreter && opam exec make
- name: Run tests
run: cd interpreter && opam exec make JS=node ci

# XXX: Disable the tests until the spec interpreter has been updated for
# the custom-page-sizes proposal.
#
# - name: Run tests
# run: cd interpreter && opam exec make JS=node ci
110 changes: 110 additions & 0 deletions test/core/custom-page-sizes/custom-page-sizes-invalid.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
;; Page size that is not a power of two.
(assert_malformed
(module quote "(memory 0 (pagesize 3))")
"invalid custom page size"
)

;; Power-of-two page sizes that are not 1 or 64KiB.
(assert_invalid
(module (memory 0 (pagesize 2)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 4)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 8)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 16)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 32)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 64)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 128)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 256)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 512)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 1024)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 2048)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 4096)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 8192)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 16384)))
"invalid custom page size"
)
(assert_invalid
(module (memory 0 (pagesize 32768)))
"invalid custom page size"
)

;; Power-of-two page size that is larger than 64KiB.
(assert_invalid
(module (memory 0 (pagesize 0x20000)))
"invalid custom page size"
)

;; Power of two page size that cannot fit in a u64 to exercise checks against
;; shift overflow.
(assert_malformed
(module binary
"\00asm" "\01\00\00\00"
"\05\04\01" ;; Memory section

;; memory 0
"\08" ;; flags w/ custom page size
"\00" ;; minimum = 0
"\41" ;; pagesize = 2**65
)
"invalid custom page size"
)

;; Importing a memory with the wrong page size.

(module $m
(memory (export "small-pages-memory") 0 (pagesize 1))
(memory (export "large-pages-memory") 0 (pagesize 65536))
)
(register "m" $m)

(assert_unlinkable
(module
(memory (import "m" "small-pages-memory") 0 (pagesize 65536))
)
"memory types incompatible"
)

(assert_unlinkable
(module
(memory (import "m" "large-pages-memory") 0 (pagesize 1))
)
"memory types incompatible"
)
108 changes: 108 additions & 0 deletions test/core/custom-page-sizes/custom-page-sizes.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
;; Check all the valid custom page sizes.
(module (memory 1 (pagesize 1)))
(module (memory 1 (pagesize 65536)))

;; Check them all again with maximums specified.
(module (memory 1 2 (pagesize 1)))
(module (memory 1 2 (pagesize 65536)))

;; Check the behavior of memories with page size 1.
(module
(memory 0 (pagesize 1))
(func (export "size") (result i32)
memory.size
)
(func (export "grow") (param i32) (result i32)
(memory.grow (local.get 0))
)
(func (export "load") (param i32) (result i32)
(i32.load8_u (local.get 0))
)
(func (export "store") (param i32 i32)
(i32.store8 (local.get 0) (local.get 1))
)
)

(assert_return (invoke "size") (i32.const 0))
(assert_trap (invoke "load" (i32.const 0)) "out of bounds memory access")

(assert_return (invoke "grow" (i32.const 65536)) (i32.const 0))
(assert_return (invoke "size") (i32.const 65536))
(assert_return (invoke "load" (i32.const 65535)) (i32.const 0))
(assert_return (invoke "store" (i32.const 65535) (i32.const 1)))
(assert_return (invoke "load" (i32.const 65535)) (i32.const 1))
(assert_trap (invoke "load" (i32.const 65536)) "out of bounds memory access")

(assert_return (invoke "grow" (i32.const 65536)) (i32.const 65536))
(assert_return (invoke "size") (i32.const 131072))
(assert_return (invoke "load" (i32.const 131071)) (i32.const 0))
(assert_return (invoke "store" (i32.const 131071) (i32.const 1)))
(assert_return (invoke "load" (i32.const 131071)) (i32.const 1))
(assert_trap (invoke "load" (i32.const 131072)) "out of bounds memory access")

;; Although smaller page sizes let us get to memories larger than 2**16 pages,
;; we can't do that with the default page size, even if we explicitly state it
;; as a custom page size.
(module
(memory 0 (pagesize 65536))
(func (export "size") (result i32)
memory.size
)
(func (export "grow") (param i32) (result i32)
(memory.grow (local.get 0))
)
)
(assert_return (invoke "size") (i32.const 0))
(assert_return (invoke "grow" (i32.const 65537)) (i32.const -1))
(assert_return (invoke "size") (i32.const 0))

;; Can copy between memories of different page sizes.
(module
(memory $small 10 (pagesize 1))
(memory $large 1 (pagesize 65536))

(data (memory $small) (i32.const 0) "\11\22\33\44")
(data (memory $large) (i32.const 0) "\55\66\77\88")

(func (export "copy-small-to-large") (param i32 i32 i32)
(memory.copy $large $small (local.get 0) (local.get 1) (local.get 2))
)

(func (export "copy-large-to-small") (param i32 i32 i32)
(memory.copy $small $large (local.get 0) (local.get 1) (local.get 2))
)

(func (export "load8-small") (param i32) (result i32)
(i32.load8_u $small (local.get 0))
)

(func (export "load8-large") (param i32) (result i32)
(i32.load8_u $large (local.get 0))
)
)

(assert_return (invoke "copy-small-to-large" (i32.const 6) (i32.const 0) (i32.const 2)))
(assert_return (invoke "load8-large" (i32.const 6)) (i32.const 0x11))
(assert_return (invoke "load8-large" (i32.const 7)) (i32.const 0x22))

(assert_return (invoke "copy-large-to-small" (i32.const 4) (i32.const 1) (i32.const 3)))
(assert_return (invoke "load8-small" (i32.const 4)) (i32.const 0x66))
(assert_return (invoke "load8-small" (i32.const 5)) (i32.const 0x77))
(assert_return (invoke "load8-small" (i32.const 6)) (i32.const 0x88))

;; Can link together modules that export and import memories with custom page
;; sizes.

(module $m
(memory (export "small-pages-memory") 0 (pagesize 1))
(memory (export "large-pages-memory") 0 (pagesize 65536))
)
(register "m" $m)

(module
(memory (import "m" "small-pages-memory") 0 (pagesize 1))
)

(module
(memory (import "m" "large-pages-memory") 0 (pagesize 65536))
)

0 comments on commit 1a81043

Please sign in to comment.