summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/exceptions/bug-1744663-extended.js
blob: d6bf53df5d67b28fb9b9dc4a0c8a6fd16a2a8536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
load(libdir + "eqArrayHelper.js");

// These tests ensure that the gc related errors in bug 1744663 are resolved.

{
  let catchlessTry = `
            try
             (call $gc)
             (throw $exn)
            end`;
  let rethrow0 = `
            try
             (call $gc)
             (throw $exn)
            catch $exn
              (rethrow 0)
            end`;
  let rethrow1 = `
           try
             (throw $exn)
           catch_all
               try
                 (throw $exn)
               catch $exn
                 (rethrow 1)
               end
           end`;
  let delegate0 = `
           try
             (call $gc)
             (throw $exn)
           delegate 0`;
  let delegate1 = `
           (block
             try
               (call $gc)
               (throw $exn)
             delegate 1)`;
  let delegate0InCatch = `
            try
              (throw $exn)
            catch_all
                try
                  (call $gc)
                  (throw $exn)
                delegate 0
            end`;
  let delegate1InCatch = `
            try
              throw $exn
            catch_all
              try
                (call $gc)
                (throw $exn)
              delegate 1
            end`;

  let rethrowingBodies = [catchlessTry, rethrow0, delegate0, delegate1,
                          rethrow1, delegate0InCatch, delegate1InCatch];

  function rethrowingIndirectly(rethrowingBody) {
    let exports = wasmEvalText(
      `(module
         (tag $exn (export "exn"))
         (import "js" "gc" (func $gc))
         (func $throwExn (export "throwExn") ${rethrowingBody}))`,
      {js: {gc: () => { gc(); }}}
    ).exports;

    let mod =
        `(module
           (type $exnType (func))
           (type $indirectFunctype (func))
           (import "m" "exn" (tag $exn (type $exnType)))
           (import "m" "throwExn" (func $throwExn (type $indirectFunctype)))
           (table funcref (elem $throwExn))
           (func (export "testFunc") (result i32)
             try (result i32)
               (call_indirect (type $indirectFunctype) (i32.const 0))
                   (i32.const 0)
             catch $exn (i32.const 1)
             end
           )
         )`;

    let testFunction = wasmEvalText(mod, { m : exports}).exports.testFunc;
    assertEq(testFunction(), 1);
  };

  for (let rethrowingBody of rethrowingBodies) {
    //console.log("Calling rethrowingIndirectly with rethrowingBody = " + rethrowingBody);
    rethrowingIndirectly(rethrowingBody);
  }
}

// The full test case that caused the original failure.

{
  gczeal(2,1); // Collect after every allocation.

  let v128Type = " i32";
  let wrongV128 = "(i32.const 0)";
  let correctV128 = "(i32.const 1)";
  let checkV128Value = "";

  if (wasmSimdEnabled()) {
    v128Type = " v128";
    wrongV128 = "(v128.const i32x4 11 22 33 44)";
    correctV128 = "(v128.const i32x4 55 66 77 88)";
    checkV128Value = `;; Check the V128 value
               (v128.const i32x4 55 66 77 88)
               (i32x4.eq)
               (i32x4.all_true)`;
  }

  let exports = wasmEvalText(
    `(module
       (type $exnType (func (param i32 i64 f32 f64 externref ${v128Type})))
       (type $indirectFunctype (func (param i32 i64 f32 f64 externref ${v128Type})
                                     (result i32 i64 f32 f64 externref ${v128Type})))
       (tag $exn (export "exn") (type $exnType))
       (tag $emptyExn (export "emptyExn"))
       (func $throwExn (export "throwExn") (param i32 i64 f32 f64 externref ${v128Type})
                                           (result i32 i64 f32 f64 externref ${v128Type})
                                           (local $ifPredicate i32)
         (local.get 0) ;; i32
         (local.get 1) ;; i64
         (local.get 2) ;; f32
         (local.get 3) ;; f64
         (local.get 4) ;; ref
         (local.get 5) ;; v128 or i32
          try (param i32 i64 f32 f64 externref ${v128Type})
            (if (param i32 i64 f32 f64 externref ${v128Type})
              (local.get $ifPredicate)
              (then (throw $exn))
              (else (throw $exn)))
          catch $exn
            try (param i32 i64 f32 f64 externref ${v128Type})
              (throw $exn)
            catch_all (rethrow 1)
            end
          catch_all
          end
          unreachable)
       (func $throwEmptyExn (export "throwEmptyExn")
                              (param i32 i64 f32 f64 externref ${v128Type})
                              (result i32 i64 f32 f64 externref ${v128Type})
           (throw $emptyExn)
           unreachable)
         (func $returnArgs (export "returnArgs")
                           (param i32 i64 f32 f64 externref ${v128Type})
                           (result i32 i64 f32 f64 externref ${v128Type})
           (local.get 0)  ;; i32
           (local.get 1)  ;; i64
           (local.get 2)  ;; f32
           (local.get 3)  ;; f64
           (local.get 4)  ;; ref
           (local.get 5))
         (table (export "tab") funcref (elem $throwExn       ;; 0
                                             $throwEmptyExn  ;; 1
                                             $returnArgs))   ;; 2
         )`).exports;

  var mod =
      `(module
         (type $exnType (func (param i32 i64 f32 f64 externref ${v128Type})))
         (type $indirectFunctype (func (param i32 i64 f32 f64 externref ${v128Type})
                                 (result i32 i64 f32 f64 externref ${v128Type})))
         (import "m" "exn" (tag $exn (type $exnType)))
         (import "m" "emptyExn" (tag $emptyExn))
         (import "m" "throwExn" (func $throwExn (type $indirectFunctype)))
         (import "m" "throwEmptyExn"
                 (func $throwEmptyExn (type $indirectFunctype)))
         (import "m" "returnArgs"
                 (func $returnArgs (type $indirectFunctype)))
         (import "m" "tab" (table 3 funcref))
         (func (export "testFunc") (param $correctRef externref)
                                   (param $wrongRef externref)
                                   ;; The last i32 result is the v128 check.
                                   (result i32 i64 f32 f64 externref i32)
                                   (local $ifPredicate i32)
           try (result i32 i64 f32 f64 externref i32)
             ;; Wrong values
             (i32.const 5)
             (i64.const 6)
             (f32.const 0.1)
             (f64.const 0.6437)
             (local.get $wrongRef)
             ${wrongV128}
             ;; throwEmptyExn
             (call_indirect (type $indirectFunctype) (i32.const 1))
             drop ;; Drop the last v128 value.
             (i32.const 0)
           catch_all
              try (result i32 i64 f32 f64 externref ${v128Type})
                 ;; Values to throw.
                 (i32.const 2)
                 (i64.const 3)
                 (f32.const 4)
                 (f64.const 13.37)
                 (local.get $correctRef)
                 ${correctV128}
                 (call_indirect (type $indirectFunctype) (i32.const 2)) ;; returnArgs
                 (call_indirect (type $indirectFunctype) (i32.const 0)) ;; throwExn
                 drop drop ;; Drop v128 and externref to do trivial and irrelevant ops.
                 (f64.const 5)
                 (f64.add)
                 (local.get $wrongRef)
                 ${wrongV128}
                 ;; throwEmptyExn
                 (call_indirect (type $indirectFunctype) (i32.const 1))
                 unreachable
              catch $emptyExn
                  ;; Wrong values
                  (i32.const 5)
                  (i64.const 6)
                  (f32.const 0.1)
                  (f64.const 0.6437)
                  (local.get $wrongRef)
                  ${wrongV128}
              catch $exn
              catch_all
                 ;; Wrong values
                 (i32.const 5)
                 (i64.const 6)
                 (f32.const 0.1)
                 (f64.const 0.6437)
                 (local.get $wrongRef)
                 ${wrongV128}
              end
              ${checkV128Value}
            end))`;

  let testAllValtypes = wasmEvalText(mod, { m : exports}).exports.testFunc;
  assertEqArray(testAllValtypes("foo", "bar"),
                [2, 3n, 4, 13.37, "foo", 1]);
}