test-wasm-assembler.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; WebAssembly binary parser
  2. ;;; Copyright (C) 2023 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Parser for WebAssembly binary format
  18. ;;;
  19. ;;; Code:
  20. (use-modules (wasm assemble)
  21. (wasm lower)
  22. (wasm wat)
  23. (wasm parse)
  24. (ice-9 binary-ports)
  25. (srfi srfi-64))
  26. (define (scope-file file-name)
  27. (string-append (getenv "SRCDIR") "/test/" file-name))
  28. (test-begin "test-wasm-assembler")
  29. (define-syntax-rule (test-wat->wasm expected wat)
  30. (begin
  31. (test-equal expected (assemble-wasm (lower-wasm (wat->wasm 'wat))))
  32. (test-equal expected
  33. (assemble-wasm (call-with-input-bytevector expected parse-wasm)))))
  34. (test-wat->wasm
  35. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 127 3 2 1 0 10 6 1 4 0 65 42 11)
  36. (module
  37. (func (param) (result i32)
  38. (i32.const 42))))
  39. (define basic-types.wasm
  40. (call-with-input-file (scope-file "basic-types.wasm") get-bytevector-all))
  41. (define basic-types/1
  42. (call-with-input-bytevector basic-types.wasm parse-wasm))
  43. (define basic-types.wasm/2
  44. (assemble-wasm basic-types/1))
  45. (define basic-types/2
  46. (call-with-input-bytevector basic-types.wasm/2 parse-wasm))
  47. (define basic-types.wasm/3
  48. (assemble-wasm basic-types/2))
  49. ;; For wasm files like basic-types.wasm that are produced by external
  50. ;; tools, we don't aim for byte-for-byte parsing and re-serialization,
  51. ;; notably because binaryen emits deprecated GC opcodes which we rewrite
  52. ;; to the updated ones. But if we parse a file that we generate and
  53. ;; then re-serialize it, they should be the same.
  54. (test-equal "basic types reassembly"
  55. basic-types.wasm/2 basic-types.wasm/3)
  56. ;; Test type resolution for functions with similar signatures
  57. (test-wat->wasm
  58. #vu8(0 97 115 109 1 0 0 0 1 6 1 96 1 127 1 127 3 3 2 0 0 10 11 2 4 0 32
  59. 0 11 4 0 32 0 11)
  60. (module
  61. (func $a (param $x i32) (result i32)
  62. (local.get $x))
  63. (func $b (param $y i32) (result i32)
  64. (local.get $y))))
  65. ;; Test vector instructions
  66. (test-wat->wasm
  67. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 123 3 2 1 0 10 8 1 6 0 65 23 253
  68. 15 11)
  69. (module (func (result v128) (i8x16.splat (i32.const 23)))))
  70. (test-wat->wasm
  71. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 123 3 2 1 0 10 8 1 6 0 65 23 253
  72. 16 11)
  73. (module (func (result v128) (i16x8.splat (i32.const 23)))))
  74. (test-wat->wasm
  75. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 123 3 2 1 0 10 8 1 6 0 65 23 253
  76. 17 11)
  77. (module (func (result v128) (i32x4.splat (i32.const 23)))))
  78. (test-wat->wasm
  79. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 123 3 2 1 0 10 8 1 6 0 66 23 253
  80. 18 11)
  81. (module (func (result v128) (i64x2.splat (i64.const 23)))))
  82. (test-wat->wasm
  83. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 123 3 2 1 0 10 11 1 9 0 67 208
  84. 15 73 64 253 19 11)
  85. (module (func (result v128) (f32x4.splat (f32.const 3.14159)))))
  86. (test-wat->wasm
  87. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 123 3 2 1 0 10 15 1 13 0 68 110
  88. 134 27 240 249 33 9 64 253 20 11)
  89. (module (func (result v128) (f64x2.splat (f64.const 3.14159)))))
  90. (test-wat->wasm
  91. #vu8(0 97 115 109 1 0 0 0 1 4 1 94 127 0)
  92. (module (type $foo (array i32))))
  93. (test-wat->wasm
  94. #vu8(0 97 115 109 1 0 0 0 1 4 1 94 127 1)
  95. (module (type $foo (array (mut i32)))))
  96. (test-wat->wasm
  97. #vu8(0 97 115 109 1 0 0 0 1 7 1 95 2 127 0 127 0)
  98. (module (type $foo (struct (field $foo i32) (field $bar i32)))))
  99. (test-wat->wasm
  100. #vu8(0 97 115 109 1 0 0 0 1 7 1 95 2 127 1 127 0)
  101. (module (type $foo (struct (field $a (mut i32)) (field $b i32)))))
  102. (test-wat->wasm
  103. #vu8(0 97 115 109 1 0 0 0 1 7 1 95 2 127 0 127 1)
  104. (module (type $foo (struct (field $a i32) (field $b (mut i32))))))
  105. (test-wat->wasm
  106. #vu8(0 97 115 109 1 0 0 0 1 7 1 95 2 127 1 127 1)
  107. (module (type $foo (struct (field $a (mut i32)) (field $b (mut i32))))))
  108. (test-wat->wasm
  109. #vu8(0 97 115 109 1 0 0 0 1 7 1 78 1 95 1 127 1)
  110. (module
  111. (rec
  112. (type $heap-object
  113. (struct (field $hash (mut i32)))))))
  114. (test-wat->wasm
  115. #vu8(0 97 115 109 1 0 0 0 1 17 1 78 2 95 1 127 1 80 1 0 95 2 127 1
  116. 100 111 0)
  117. (module
  118. (rec
  119. (type $heap-object
  120. (struct (field $hash (mut i32))))
  121. (type $extern-ref
  122. (sub $heap-object
  123. (struct
  124. (field $hash (mut i32))
  125. (field $val (ref extern))))))))
  126. (test-wat->wasm
  127. #vu8(0 97 115 109 1 0 0 0 1 33 1 78 3 95 1 127 1 80 1 0 95 3 127 1
  128. 100 109 1 100 109 1 80 1 1 95 3 127 1 100 109 1 100 109 1)
  129. (module
  130. (rec
  131. (type $heap-object
  132. (struct (field $hash (mut i32))))
  133. (type $pair
  134. (sub $heap-object
  135. (struct
  136. (field $hash (mut i32))
  137. (field $car (mut (ref eq)))
  138. (field $cdr (mut (ref eq))))))
  139. (type $mutable-pair
  140. (sub $pair
  141. (struct
  142. (field $hash (mut i32))
  143. (field $car (mut (ref eq)))
  144. (field $cdr (mut (ref eq)))))))))
  145. (test-wat->wasm
  146. #vu8(0 97 115 109 1 0 0 0 1 4 1 96 0 0 3 2 1 0 10 9 1 7 0 65 0 14 0 0 11)
  147. (module (func (i32.const 0) (br_table 0))))
  148. (test-wat->wasm
  149. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 1 127 0 3 2 1 0 10 20 1 18 0 2 64 2
  150. 64 2 64 32 0 14 2 2 1 0 11 11 11 11)
  151. (module
  152. (func (param $i i32)
  153. (block $l1
  154. (block $l2
  155. (block $l3
  156. (br_table $l1 $l2 $l3 (local.get $i))))))))
  157. (test-wat->wasm
  158. #vu8(0 97 115 109 1 0 0 0 1 5 1 96 0 1 127 3 2 1 0 10 14 1 12 0 65 0 4
  159. 127 65 1 5 65 2 11 11)
  160. (module
  161. (func (result i32)
  162. (if i32
  163. (i32.const 0)
  164. (then (i32.const 1))
  165. (else (i32.const 2))))))
  166. (when (and (batch-mode?)
  167. (or (not (zero? (test-runner-fail-count (test-runner-get))))
  168. (not (zero? (test-runner-xpass-count (test-runner-get))))))
  169. (exit 1))
  170. (test-end "test-wasm-assembler")