| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | |
| 6 | /* |
| 7 | * |
| 8 | * Date: 24 Nov 2003 |
| 9 | * SUMMARY: Testing for recursion check in js_EmitTree |
| 10 | * |
| 11 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=226507 |
| 12 | * Igor's comments: |
| 13 | * |
| 14 | * "For example, with N in the test set to 35, I got on my RedHat |
| 15 | * Linux 10 box a segmentation fault from js after setting the stack limit |
| 16 | * to 100K. When I set the stack limit to 20K I still got the segmentation fault. |
| 17 | * Only after -s was changed to 15K, too-deep recursion was detected: |
| 18 | * |
| 19 | |
| 20 | ~/w/js/x> ulimit -s |
| 21 | 100 |
| 22 | ~/w/js/x> js fintest.js |
| 23 | Segmentation fault |
| 24 | ~/w/js/x> js -S $((20*1024)) fintest.js |
| 25 | Segmentation fault |
| 26 | ~/w/js/x> js -S $((15*1024)) fintest.js |
| 27 | fintest.js:19: InternalError: too much recursion |
| 28 | |
| 29 | * |
| 30 | * After playing with numbers it seems that while processing try/finally the |
| 31 | * recursion in js_Emit takes 10 times more space the corresponding recursion |
| 32 | * in the parser." |
| 33 | * |
| 34 | * |
| 35 | * Note the use of the new -S option to the JS shell to limit stack size. |
| 36 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=225061. This in turn |
| 37 | * can be passed to the JS shell by the test driver's -o option, as in: |
| 38 | * |
| 39 | * perl jsDriver.pl -e smdebug -fTEST.html -o "-S 100" -l js1_5/Regress |
| 40 | * |
| 41 | */ |
| 42 | //----------------------------------------------------------------------------- |
| 43 | var gTestfile = 'regress-226507.js'; |
| 44 | var UBound = 0; |
| 45 | var BUGNUMBER = 226507; |
| 46 | var summary = 'Testing for recursion check in js_EmitTree'; |
| 47 | var status = ''; |
| 48 | var statusitems = []; |
| 49 | var actual = ''; |
| 50 | var actualvalues = []; |
| 51 | var expect= ''; |
| 52 | var expectedvalues = []; |
| 53 | |
| 54 | |
| 55 | /* |
| 56 | * With stack limit 100K on Linux debug build even N=30 already can cause |
| 57 | * stack overflow; use 35 to trigger it for sure. |
| 58 | * But we need to trigger this consistently but not a Java exception, so back |
| 59 | * to 35. |
| 60 | */ |
| 61 | var N = 35; |
| 62 | |
| 63 | var counter = 0; |
| 64 | function f() |
| 65 | { |
| 66 | ++counter; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /* |
| 71 | * Example: if N were 3, this is what |source| |
| 72 | * would end up looking like: |
| 73 | * |
| 74 | * try { f(); } finally { |
| 75 | * try { f(); } finally { |
| 76 | * try { f(); } finally { |
| 77 | * f(1,1,1,1); |
| 78 | * }}} |
| 79 | * |
| 80 | */ |
| 81 | var source = "".concat( |
| 82 | repeat_str("try { f(); } finally {\n", N), |
| 83 | "f(", |
| 84 | repeat_str("1,", N), |
| 85 | "1);\n", |
| 86 | repeat_str("}", N)); |
| 87 | |
| 88 | // Repeat it for additional stress testing |
| 89 | source += source; |
| 90 | |
| 91 | /* |
| 92 | * In Rhino, eval() always uses interpreted mode. |
| 93 | * To use compiled mode, use Script.exec() instead. |
| 94 | */ |
| 95 | if (typeof Script == 'undefined') |
| 96 | { |
| 97 | print('Test skipped. Script not defined.'); |
| 98 | expect = actual = 0; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | try |
| 103 | { |
| 104 | var script = Script(source); |
| 105 | script(); |
| 106 | |
| 107 | |
| 108 | status = inSection(1); |
| 109 | actual = counter; |
| 110 | expect = (N + 1) * 2; |
| 111 | } |
| 112 | catch(ex) |
| 113 | { |
| 114 | actual = ex + ''; |
| 115 | } |
| 116 | } |
| 117 | addThis(); |
| 118 | |
| 119 | |
| 120 | //----------------------------------------------------------------------------- |
| 121 | test(); |
| 122 | //----------------------------------------------------------------------------- |
| 123 | |
| 124 | |
| 125 | |
| 126 | function repeat_str(str, repeat_count) |
| 127 | { |
| 128 | var arr = new Array(--repeat_count); |
| 129 | while (repeat_count != 0) |
| 130 | arr[--repeat_count] = str; |
| 131 | return str.concat.apply(str, arr); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | function addThis() |
| 136 | { |
| 137 | statusitems[UBound] = status; |
| 138 | actualvalues[UBound] = actual; |
| 139 | expectedvalues[UBound] = expect; |
| 140 | UBound++; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | function test() |
| 145 | { |
| 146 | enterFunc('test'); |
| 147 | printBugNumber(BUGNUMBER); |
| 148 | printStatus(summary); |
| 149 | |
| 150 | for (var i=0; i<UBound; i++) |
| 151 | { |
| 152 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 153 | } |
| 154 | |
| 155 | exitFunc ('test'); |
| 156 | } |