| 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: 16 July 2002 |
| 9 | * SUMMARY: Testing that Array.sort() doesn't crash on very large arrays |
| 10 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=157652 |
| 11 | * |
| 12 | * How large can a JavaScript array be? |
| 13 | * ECMA-262 Ed.3 Final, Section 15.4.2.2 : new Array(len) |
| 14 | * |
| 15 | * This states that |len| must be a a uint32 (unsigned 32-bit integer). |
| 16 | * Note the UBound for uint32's is 2^32 -1 = 0xFFFFFFFF = 4,294,967,295. |
| 17 | * |
| 18 | * Check: |
| 19 | * js> var arr = new Array(0xFFFFFFFF) |
| 20 | * js> arr.length |
| 21 | * 4294967295 |
| 22 | * |
| 23 | * js> var arr = new Array(0x100000000) |
| 24 | * RangeError: invalid array length |
| 25 | * |
| 26 | * |
| 27 | * We'll try the largest possible array first, then a couple others. |
| 28 | * We're just testing that we don't crash on Array.sort(). |
| 29 | * |
| 30 | * Try to be good about memory by nulling each array variable after it is |
| 31 | * used. This will tell the garbage collector the memory is no longer needed. |
| 32 | * |
| 33 | * As of 2002-08-13, the JS shell runs out of memory no matter what we do, |
| 34 | * when trying to sort such large arrays. |
| 35 | * |
| 36 | * We only want to test that we don't CRASH on the sort. So it will be OK |
| 37 | * if we get the JS "out of memory" error. Note this terminates the test |
| 38 | * with exit code 3. Therefore we put |
| 39 | * |
| 40 | * |expectExitCode(3);| |
| 41 | * |
| 42 | * The only problem will arise if the JS shell ever DOES have enough memory |
| 43 | * to do the sort. Then this test will terminate with the normal exit code 0 |
| 44 | * and fail. |
| 45 | * |
| 46 | * Right now, I can't see any other way to do this, because "out of memory" |
| 47 | * is not a catchable error: it cannot be trapped with try...catch. |
| 48 | * |
| 49 | * |
| 50 | * FURTHER HEADACHE: Rhino can't seem to handle the largest array: it hangs. |
| 51 | * So we skip this case in Rhino. Here is correspondence with Igor Bukanov. |
| 52 | * He explains that Rhino isn't actually hanging; it's doing the huge sort: |
| 53 | * |
| 54 | * Philip Schwartau wrote: |
| 55 | * |
| 56 | * > Hi, |
| 57 | * > |
| 58 | * > I'm getting a graceful OOM message on trying to sort certain large |
| 59 | * > arrays. But if the array is too big, Rhino simply hangs. Note that ECMA |
| 60 | * > allows array lengths to be anything less than Math.pow(2,32), so the |
| 61 | * > arrays I'm sorting are legal. |
| 62 | * > |
| 63 | * > Note below, I'm getting an instantaneous OOM error on arr.sort() for LEN |
| 64 | * > = Math.pow(2, 30). So shouldn't I also get one for every LEN between |
| 65 | * > that and Math.pow(2, 32)? For some reason, I start to hang with 100% CPU |
| 66 | * > as LEN hits, say, Math.pow(2, 31) and higher. SpiderMonkey gives OOM |
| 67 | * > messages for all of these. Should I file a bug on this? |
| 68 | * |
| 69 | * Igor Bukanov wrote: |
| 70 | * |
| 71 | * This is due to different sorting algorithm Rhino uses when sorting |
| 72 | * arrays with length > Integer.MAX_VALUE. If length can fit Java int, |
| 73 | * Rhino first copies internal spare array to a temporary buffer, and then |
| 74 | * sorts it, otherwise it sorts array directly. In case of very spare |
| 75 | * arrays, that Array(big_number) generates, it is rather inefficient and |
| 76 | * generates OutOfMemory if length fits int. It may be worth in your case |
| 77 | * to optimize sorting to take into account array spareness, but then it |
| 78 | * would be a good idea to file a bug about ineficient sorting of spare |
| 79 | * arrays both in case of Rhino and SpiderMonkey as SM always uses a |
| 80 | * temporary buffer. |
| 81 | * |
| 82 | */ |
| 83 | //----------------------------------------------------------------------------- |
| 84 | var gTestfile = 'regress-157652.js'; |
| 85 | var BUGNUMBER = 157652; |
| 86 | var summary = "Testing that Array.sort() doesn't crash on very large arrays"; |
| 87 | var expect = 'No Crash'; |
| 88 | var actual = 'No Crash'; |
| 89 | |
| 90 | printBugNumber(BUGNUMBER); |
| 91 | printStatus(summary); |
| 92 | |
| 93 | expectExitCode(0); |
| 94 | expectExitCode(5); |
| 95 | |
| 96 | var IN_RHINO = inRhino(); |
| 97 | |
| 98 | try |
| 99 | { |
| 100 | if (!IN_RHINO) |
| 101 | { |
| 102 | var a1=Array(0xFFFFFFFF); |
| 103 | a1.sort(); |
| 104 | a1 = null; |
| 105 | } |
| 106 | |
| 107 | var a2 = Array(0x40000000); |
| 108 | a2.sort(); |
| 109 | a2=null; |
| 110 | |
| 111 | var a3=Array(0x10000000/4); |
| 112 | a3.sort(); |
| 113 | a3=null; |
| 114 | } |
| 115 | catch(ex) |
| 116 | { |
| 117 | // handle changed 1.9 branch behavior. see bug 422348 |
| 118 | expect = 'InternalError: allocation size overflow'; |
| 119 | actual = ex + ''; |
| 120 | } |
| 121 | |
| 122 | reportCompare(expect, actual, summary); |