• 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} catch (e) {
28    if ((e instanceof RangeError)) {
29        print("stack overflow2!");
30    }
31}
32
33function foo2() {
34    foo2()
35}
36try {
37    foo2()
38} catch (e) {
39    if ((e instanceof RangeError)) {
40        print("stack overflow!");
41    }
42}
43
44const obj = {};
45const pro = new Proxy({}, obj);
46obj.__proto__ = pro;
47try {
48    obj[10];
49} catch (e) {
50    if (e instanceof RangeError) {
51        print("proxy stackoverflow!");
52    }
53}
54try {
55    function func() { }
56    Function.prototype.__proto__ = new Proxy(func, {})
57    Function.prototype.__proto__ = new Proxy(func, {})
58} catch (error) {
59    if (error instanceof RangeError) {
60        print("proxy stackoverflow2!");
61    }
62}
63
64// callarg0-3 callrange
65function callarg0() {
66    callarg0();
67}
68try {
69    callarg0();
70} catch (e) {
71    print("callarg0 stackoverflow");
72}
73
74function callarg1(a) {
75    callarg1(a);
76}
77try {
78    callarg1(1);
79} catch (e) {
80    print("callarg1 stackoverflow");
81}
82
83function callarg2(a, b) {
84    callarg2(a, b);
85}
86try {
87    callarg2(1, 2);
88} catch (e) {
89    print("callarg2 stackoverflow");
90}
91
92function callarg3(a, b, c) {
93    callarg3(a, b, c);
94}
95try {
96    callarg3(1, 2, 3);
97} catch (e) {
98    print("callarg3 stackoverflow");
99}
100
101function callrange(a, b, c, d) {
102    callrange(a, b, c, d);
103}
104try {
105    callrange(1, 2, 3, 4);
106} catch (e) {
107    print("callrange stackoverflow");
108}
109
110// callthis0-3 callthisrange
111var obj2 = {
112    callthis0: function () {
113        this.callthis0();
114    },
115    callthis1: function (a) {
116        this.callthis1(a);
117    },
118    callthis2: function (a, b) {
119        this.callthis2(a, b);
120    },
121    callthis3: function (a, b, c) {
122        this.callthis3(a, b, c);
123    },
124    callthisrange: function (a, b, c, d) {
125        this.callthisrange(a, b, c, d);
126    },
127}
128try {
129    obj2.callthis0();
130} catch (e) {
131    print("callthis0 stackoverflow");
132}
133try {
134    obj2.callthis1(1);
135} catch (e) {
136    print("callthis1 stackoverflow");
137}
138try {
139    obj2.callthis2(1, 2);
140} catch (e) {
141    print("callthis2 stackoverflow");
142}
143try {
144    obj2.callthis3(1, 2, 3);
145} catch (e) {
146    print("callthis3 stackoverflow");
147}
148try {
149    obj2.callthisrange(1, 2, 3, 4);
150} catch (e) {
151    print("callthisrange stackoverflow");
152}
153
154// callstatic
155class MyClass {
156    static callstatic0() {
157        MyClass.callstatic0();
158    }
159    static callstatic1(a) {
160        MyClass.callstatic1(a);
161    }
162    static callstatic2(a, b) {
163        MyClass.callstatic2(a, b);
164    }
165    static callstatic3(a, b, c) {
166        MyClass.callstatic3(a, b, c);
167    }
168    static callstaticrange(a, b, c, d) {
169        MyClass.callstaticrange(a, b, c, d);
170    }
171}
172try {
173    MyClass.callstatic0();
174} catch (e) {
175    print("callstatic0 stackoverflow");
176}
177try {
178    MyClass.callstatic1(1);
179} catch (e) {
180    print("callstatic1 stackoverflow");
181}
182try {
183    MyClass.callstatic2(1, 2);
184} catch (e) {
185    print("callstatic2 stackoverflow");
186}
187try {
188    MyClass.callstatic3(1, 2, 3);
189} catch (e) {
190    print("callstatic3 stackoverflow");
191}
192try {
193    MyClass.callstaticrange(1, 2, 3, 4);
194} catch (e) {
195    print("callstaticrange stackoverflow");
196}
197
198// callnew
199class MyClass2 {
200    constructor() {
201        new MyClass2();
202    }
203}
204try {
205    new MyClass2();
206} catch (e) {
207    print("callnew stackoverflow");
208}
209
210class MyClass3 {
211    constructor(a, b, c, d) {
212        new MyClass3(a, b, c, d);
213    }
214}
215try {
216    new MyClass3(1, 2, 3, 4);
217} catch (e) {
218    print("callnew stackoverflow2");
219}
220
221function callarg0_with_callthis() {
222    const arr = [1]
223    function bar() {
224        return 1;
225    }
226    arr.filter(bar);
227    return foo_arg0();
228}
229
230try {
231    foo_arg0();
232} catch (e) {
233    print("callarg0_with_callthis stack overflow!")
234}
235
236function callarg1_with_callthis(a) {
237    const arr = [1]
238    function bar() {
239        return 1;
240    }
241    arr.filter(bar);
242    return callarg1_with_callthis(a);
243}
244
245try {
246    callarg1_with_callthis(1);
247} catch (e) {
248    print("callarg1_with_callthis stack overflow!")
249}
250
251function callarg2_with_callthis(a, b) {
252    const arr = [1]
253    function bar() {
254        return 1;
255    }
256    arr.filter(bar);
257    return callarg2_with_callthis(a, b);
258}
259
260try {
261    callarg2_with_callthis(1, 2);
262} catch (e) {
263    print("callarg2_with_callthis stack overflow!")
264}
265
266function callarg3_with_callthis(a, b, c) {
267    const arr = [1]
268    function bar() {
269        return 1;
270    }
271    arr.filter(bar);
272    return callarg3_with_callthis(a, b, c);
273}
274
275try {
276    callarg3_with_callthis(1, 2, 3);
277} catch (e) {
278    print("callarg3_with_callthis stack overflow!")
279}
280
281function callrange_with_callthis(a, b, c, d) {
282    const arr = [1]
283    function bar() {
284        return 1;
285    }
286    arr.filter(bar);
287    return callrange_with_callthis(a, b, c, d);
288}
289
290try {
291    callrange_with_callthis(1, 2, 3, 4);
292} catch (e) {
293    print("callrange_with_callthis stack overflow!")
294}
295
296// PoC
297var source = Array(2500).join("(") + "a" + Array(2500).join(")");
298try {
299    var r = RegExp(source);
300    r.test("\x80");
301} catch (e) {
302    print(e.name)
303}
304
305// Poc
306try {
307    class MyClass4 {
308        set f(a) {
309            this.f = a;
310        }
311    }
312    const instance4 = new MyClass4();
313    instance4.f = false;
314} catch (e) {
315    print(e.name);
316}
317try {
318    var arr = [1];
319    arr.push(arr);
320    const v7 = arr.flat(65535);
321} catch (error) {
322    print(error.name)
323}
324
325function f0() {
326    return f0;
327}
328class C1 extends f0 {
329    constructor(a3) {
330        super();
331        const v6 = new Proxy(C1, Reflect);
332        new v6();
333    }
334}
335try {
336    new C1(f0);
337} catch(e) {
338    print(e.name);
339}
340
341const v3 = new Proxy([123], {});
342class C4{
343  constructor(a6, a7, a8) {
344    try {
345      new C4();
346    } catch (e) {
347
348    }
349    new Set(v3);
350  }
351}
352new C4();
353print("recursive stack overflow")
354
355function f(a5) {
356    ("ZU").matchAll(a5);
357}
358function runNearStackLimit(f) {
359    function t() {
360        try {
361            t();
362        } catch (e) {
363            f();
364        }
365    }
366    try {
367        t();
368    } catch (e) {
369    }
370}
371runNearStackLimit(f);
372print("matchAll success");
373