• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * @tc.name:asmstackoverflow
18 * @tc.desc:test stack overflow in asm
19 * @tc.type: FUNC
20 * @tc.require: issueI5NO8G
21 */
22function foo(x, y, z, a, b) {
23    foo(x, y, z, a, b)
24}
25try {
26    foo(1, 2, 3, 4, 5)
27	assert_unreachable();
28} catch (e) {
29    assert_equal(e instanceof RangeError, true);
30}
31
32function foo2() {
33    foo2()
34}
35try {
36    foo2()
37	assert_unreachable();
38} catch (e) {
39    assert_equal(e instanceof RangeError, true);
40}
41
42const obj = {};
43const pro = new Proxy({}, obj);
44obj.__proto__ = pro;
45try {
46    obj[10];
47	assert_unreachable();
48} catch (e) {
49    assert_equal(e instanceof RangeError, true);
50}
51try {
52    function func() { }
53    Function.prototype.__proto__ = new Proxy(func, {})
54    Function.prototype.__proto__ = new Proxy(func, {})
55	assert_unreachable();
56} catch (error) {
57    assert_equal(error instanceof RangeError, true);
58}
59
60// callarg0-3 callrange
61function callarg0() {
62    callarg0();
63}
64try {
65    callarg0();
66	assert_unreachable();
67} catch (e) {
68    assert_equal(e instanceof RangeError, true);
69}
70
71function callarg1(a) {
72    callarg1(a);
73}
74try {
75    callarg1(1);
76	assert_unreachable();
77} catch (e) {
78    assert_equal(e instanceof RangeError, true);
79}
80
81function callarg2(a, b) {
82    callarg2(a, b);
83}
84try {
85    callarg2(1, 2);
86	assert_unreachable();
87} catch (e) {
88    assert_equal(e instanceof RangeError, true);
89}
90
91function callarg3(a, b, c) {
92    callarg3(a, b, c);
93}
94try {
95    callarg3(1, 2, 3);
96	assert_unreachable();
97} catch (e) {
98    assert_equal(e instanceof RangeError, true);
99}
100
101function callrange(a, b, c, d) {
102    callrange(a, b, c, d);
103}
104try {
105    callrange(1, 2, 3, 4);
106	assert_unreachable();
107} catch (e) {
108    assert_equal(e instanceof RangeError, true);
109}
110
111// callthis0-3 callthisrange
112var obj2 = {
113    callthis0: function () {
114        this.callthis0();
115    },
116    callthis1: function (a) {
117        this.callthis1(a);
118    },
119    callthis2: function (a, b) {
120        this.callthis2(a, b);
121    },
122    callthis3: function (a, b, c) {
123        this.callthis3(a, b, c);
124    },
125    callthisrange: function (a, b, c, d) {
126        this.callthisrange(a, b, c, d);
127    },
128}
129try {
130    obj2.callthis0();
131	assert_unreachable();
132} catch (e) {
133    assert_equal(e instanceof RangeError, true);
134}
135try {
136    obj2.callthis1(1);
137	assert_unreachable();
138} catch (e) {
139    assert_equal(e instanceof RangeError, true);
140}
141try {
142    obj2.callthis2(1, 2);
143	assert_unreachable();
144} catch (e) {
145    assert_equal(e instanceof RangeError, true);
146}
147try {
148    obj2.callthis3(1, 2, 3);
149	assert_unreachable();
150} catch (e) {
151    assert_equal(e instanceof RangeError, true);
152}
153try {
154    obj2.callthisrange(1, 2, 3, 4);
155	assert_unreachable();
156} catch (e) {
157    assert_equal(e instanceof RangeError, true);
158}
159
160// callstatic
161class MyClass {
162    static callstatic0() {
163        MyClass.callstatic0();
164    }
165    static callstatic1(a) {
166        MyClass.callstatic1(a);
167    }
168    static callstatic2(a, b) {
169        MyClass.callstatic2(a, b);
170    }
171    static callstatic3(a, b, c) {
172        MyClass.callstatic3(a, b, c);
173    }
174    static callstaticrange(a, b, c, d) {
175        MyClass.callstaticrange(a, b, c, d);
176    }
177}
178try {
179    MyClass.callstatic0();
180	assert_unreachable();
181} catch (e) {
182    assert_equal(e instanceof RangeError, true);
183}
184try {
185    MyClass.callstatic1(1);
186	assert_unreachable();
187} catch (e) {
188    assert_equal(e instanceof RangeError, true);
189}
190try {
191    MyClass.callstatic2(1, 2);
192	assert_unreachable();
193} catch (e) {
194    assert_equal(e instanceof RangeError, true);
195}
196try {
197    MyClass.callstatic3(1, 2, 3);
198	assert_unreachable();
199} catch (e) {
200    assert_equal(e instanceof RangeError, true);
201}
202try {
203    MyClass.callstaticrange(1, 2, 3, 4);
204	assert_unreachable();
205} catch (e) {
206    assert_equal(e instanceof RangeError, true);
207}
208
209// callnew
210class MyClass2 {
211    constructor() {
212        new MyClass2();
213    }
214}
215try {
216    new MyClass2();
217	assert_unreachable();
218} catch (e) {
219    assert_equal(e instanceof RangeError, true);
220}
221
222class MyClass3 {
223    constructor(a, b, c, d) {
224        new MyClass3(a, b, c, d);
225    }
226}
227try {
228    new MyClass3(1, 2, 3, 4);
229	assert_unreachable();
230} catch (e) {
231    assert_equal(e instanceof RangeError, true);
232}
233
234function callarg0_with_callthis() {
235    const arr = [1]
236    function bar() {
237        return 1;
238    }
239    arr.filter(bar);
240    return foo_arg0();
241}
242
243try {
244    foo_arg0();
245	assert_unreachable();
246} catch (e) {
247    assert_equal(e instanceof ReferenceError, true);
248}
249
250function callarg1_with_callthis(a) {
251    const arr = [1]
252    function bar() {
253        return 1;
254    }
255    arr.filter(bar);
256    return callarg1_with_callthis(a);
257}
258
259try {
260    callarg1_with_callthis(1);
261	assert_unreachable();
262} catch (e) {
263    assert_equal(e instanceof RangeError, true);
264}
265
266function callarg2_with_callthis(a, b) {
267    const arr = [1]
268    function bar() {
269        return 1;
270    }
271    arr.filter(bar);
272    return callarg2_with_callthis(a, b);
273}
274
275try {
276    callarg2_with_callthis(1, 2);
277	assert_unreachable();
278} catch (e) {
279    assert_equal(e instanceof RangeError, true);
280}
281
282function callarg3_with_callthis(a, b, c) {
283    const arr = [1]
284    function bar() {
285        return 1;
286    }
287    arr.filter(bar);
288    return callarg3_with_callthis(a, b, c);
289}
290
291try {
292    callarg3_with_callthis(1, 2, 3);
293	assert_unreachable();
294} catch (e) {
295    assert_equal(e instanceof RangeError, true);
296}
297
298function callrange_with_callthis(a, b, c, d) {
299    const arr = [1]
300    function bar() {
301        return 1;
302    }
303    arr.filter(bar);
304    return callrange_with_callthis(a, b, c, d);
305}
306
307try {
308    callrange_with_callthis(1, 2, 3, 4);
309	assert_unreachable();
310} catch (e) {
311    assert_equal(e instanceof RangeError, true);
312}
313
314// PoC
315var source = Array(2500).join("(") + "a" + Array(2500).join(")");
316try {
317    var r = RegExp(source);
318    r.test("\x80");
319	assert_unreachable();
320} catch (e) {
321    assert_equal(e.name, "SyntaxError");
322}
323
324// Poc
325try {
326    class MyClass4 {
327        set f(a) {
328            this.f = a;
329        }
330    }
331    const instance4 = new MyClass4();
332    instance4.f = false;
333} catch (e) {
334    print(e.name);
335}
336try {
337    var arr = [1];
338    arr.push(arr);
339    const v7 = arr.flat(65535);
340	assert_unreachable();
341} catch (error) {
342    assert_equal(error.name, "RangeError");
343}
344
345function f0() {
346    return f0;
347}
348class C1 extends f0 {
349    constructor(a3) {
350        super();
351        const v6 = new Proxy(C1, Reflect);
352        new v6();
353    }
354}
355try {
356    new C1(f0);
357    assert_unreachable();
358} catch(e) {
359    assert_equal(e.name, "RangeError");
360}
361
362const v3 = new Proxy([123], {});
363class C4{
364  constructor(a6, a7, a8) {
365    try {
366      new C4();
367    } catch (e) {
368
369    }
370    new Set(v3);
371  }
372}
373new C4();
374const result1 = "recursive stack overflow"
375assert_equal(result1, "recursive stack overflow")
376
377function f(a5) {
378    ("ZU").matchAll(a5);
379}
380function runNearStackLimit(f) {
381    function t() {
382        try {
383            t();
384        } catch (e) {
385            f();
386        }
387    }
388    try {
389        t();
390    } catch (e) {
391    }
392}
393runNearStackLimit(f);
394const result2 = "matchAll success";
395assert_equal(result2, "matchAll success");
396
397test_end();