Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // Flags: --allow-natives-syntax
     29 
     30 var RUN_WITH_ALL_ARGUMENT_ENTRIES = false;
     31 var kOnManyArgumentsRemove = 5;
     32 
     33 function makeArguments() {
     34   var result = [ ];
     35   result.push(17);
     36   result.push(-31);
     37   result.push(new Array(100));
     38   result.push(new Array(100003));
     39   result.push(Number.MIN_VALUE);
     40   result.push("whoops");
     41   result.push("x");
     42   result.push({"x": 1, "y": 2});
     43   var slowCaseObj = {"a": 3, "b": 4, "c": 5};
     44   delete slowCaseObj.c;
     45   result.push(slowCaseObj);
     46   result.push(function () { return 8; });
     47   return result;
     48 }
     49 
     50 var kArgObjects = makeArguments().length;
     51 
     52 function makeFunction(name, argc) {
     53   var args = [];
     54   for (var i = 0; i < argc; i++)
     55     args.push("x" + i);
     56   var argsStr = args.join(", ");
     57   return new Function(args.join(", "), "return %" + name + "(" + argsStr + ");");
     58 }
     59 
     60 function testArgumentCount(name, argc) {
     61   for (var i = 0; i < 10; i++) {
     62     var func = null;
     63     try {
     64       func = makeFunction(name, i);
     65     } catch (e) {
     66       if (e != "SyntaxError: Illegal access") throw e;
     67     }
     68     if (func === null && i == argc) {
     69       throw "unexpected exception";
     70     }
     71     var args = [ ];
     72     for (var j = 0; j < i; j++)
     73       args.push(0);
     74     try {
     75       func.apply(void 0, args);
     76     } catch (e) {
     77       // we don't care what happens as long as we don't crash
     78     }
     79   }
     80 }
     81 
     82 function testArgumentTypes(name, argc) {
     83   var type = 0;
     84   var hasMore = true;
     85   var func = makeFunction(name, argc);
     86   while (hasMore) {
     87     var argPool = makeArguments();
     88     // When we have 5 or more arguments we lower the amount of tests cases
     89     // by randomly removing kOnManyArgumentsRemove entries
     90     var numArguments = RUN_WITH_ALL_ARGUMENT_ENTRIES ?
     91       kArgObjects : kArgObjects-kOnManyArgumentsRemove;
     92     if (argc >= 5 && !RUN_WITH_ALL_ARGUMENT_ENTRIES) {
     93       for (var i = 0; i < kOnManyArgumentsRemove; i++) {
     94         var rand = Math.floor(Math.random() * (kArgObjects - i));
     95         argPool.splice(rand,1);
     96       }
     97     }
     98     var current = type;
     99     var hasMore = false;
    100     var argList = [ ];
    101     for (var i = 0; i < argc; i++) {
    102       var index = current % numArguments;
    103       current = (current / numArguments) << 0;
    104       if (index != (numArguments - 1))
    105         hasMore = true;
    106       argList.push(argPool[index]);
    107     }
    108     try {
    109       func.apply(void 0, argList);
    110     } catch (e) {
    111       // we don't care what happens as long as we don't crash
    112     }
    113     type++;
    114   }
    115 }
    116 
    117 var knownProblems = {
    118   "Abort": true,
    119   "ThrowMessage": true,
    120 
    121   // Avoid calling the concat operation, because weird lengths
    122   // may lead to out-of-memory.  Ditto for StringBuilderJoin.
    123   "StringBuilderConcat": true,
    124   "StringBuilderJoin": true,
    125 
    126   // These functions use pseudo-stack-pointers and are not robust
    127   // to unexpected integer values.
    128   "DebugEvaluate": true,
    129 
    130   // These functions do nontrivial error checking in recursive calls,
    131   // which means that we have to propagate errors back.
    132   "SetFunctionBreakPoint": true,
    133   "SetScriptBreakPoint": true,
    134   "PrepareStep": true,
    135 
    136   // Too slow.
    137   "DebugReferencedBy": true,
    138 
    139   // Calling disable/enable access checks may interfere with the
    140   // the rest of the tests.
    141   "DisableAccessChecks": true,
    142   "EnableAccessChecks": true,
    143 
    144   // These functions should not be callable as runtime functions.
    145   "NewFunctionContext": true,
    146   "NewArgumentsFast": true,
    147   "NewStrictArgumentsFast": true,
    148   "PushWithContext": true,
    149   "PushCatchContext": true,
    150   "PushBlockContext": true,
    151   "PushModuleContext": true,
    152   "LazyCompile": true,
    153   "LazyRecompile": true,
    154   "ConcurrentRecompile": true,
    155   "NotifyDeoptimized": true,
    156   "NotifyStubFailure": true,
    157   "NotifyOSR": true,
    158   "CreateObjectLiteralBoilerplate": true,
    159   "CloneLiteralBoilerplate": true,
    160   "CloneShallowLiteralBoilerplate": true,
    161   "CreateArrayLiteralBoilerplate": true,
    162   "IS_VAR": true,
    163   "ResolvePossiblyDirectEval": true,
    164   "Log": true,
    165   "DeclareGlobals": true,
    166   "ArrayConstructor": true,
    167   "InternalArrayConstructor": true,
    168   "SetAccessorProperty": true,
    169 
    170   "PromoteScheduledException": true,
    171   "DeleteHandleScopeExtensions": true,
    172 
    173   // Vararg with minimum number > 0.
    174   "Call": true,
    175 
    176   // Requires integer arguments to be non-negative.
    177   "Apply": true,
    178 
    179   // That can only be invoked on Array.prototype.
    180   "FinishArrayPrototypeSetup": true,
    181 
    182   "_SwapElements": true,
    183 
    184   // Performance critical functions which cannot afford type checks.
    185   "_IsNativeOrStrictMode": true,
    186   "_CallFunction": true,
    187 
    188   // Tries to allocate based on argument, and (correctly) throws
    189   // out-of-memory if the request is too large. In practice, the
    190   // size will be the number of captures of a RegExp.
    191   "RegExpConstructResult": true,
    192   "_RegExpConstructResult": true,
    193 
    194   // This functions perform some checks compile time (they require one of their
    195   // arguments to be a compile time smi).
    196   "_DateField": true,
    197   "_GetFromCache": true,
    198 
    199   // This function expects its first argument to be a non-smi.
    200   "_IsStringWrapperSafeForDefaultValueOf" : true,
    201 
    202   // Only applicable to strings.
    203   "_HasCachedArrayIndex": true,
    204   "_GetCachedArrayIndex": true,
    205   "_OneByteSeqStringSetChar": true,
    206   "_TwoByteSeqStringSetChar": true,
    207 
    208   // Only applicable to TypedArrays.
    209   "TypedArrayInitialize": true,
    210 
    211   // Only applicable to generators.
    212   "_GeneratorNext": true,
    213   "_GeneratorThrow": true,
    214 
    215   // Only applicable to DataViews.
    216   "DataViewInitialize": true,
    217   "DataViewGetBuffer": true,
    218   "DataViewGetByteLength": true,
    219   "DataViewGetByteOffset": true
    220 };
    221 
    222 var currentlyUncallable = {
    223   // We need to find a way to test this without breaking the system.
    224   "SystemBreak": true,
    225   // Inserts an int3/stop instruction when run with --always-opt.
    226   "_DebugBreakInOptimizedCode": true
    227 };
    228 
    229 function testNatives() {
    230   var allNatives = %ListNatives();
    231   var start = allNatives.length >> 2;
    232   var stop = (allNatives.length >> 2)*2;
    233   for (var i = start; i < stop; i++) {
    234     var nativeInfo = allNatives[i];
    235     var name = nativeInfo[0];
    236     if (name in knownProblems || name in currentlyUncallable)
    237       continue;
    238     print(name);
    239     var argc = nativeInfo[1];
    240     testArgumentCount(name, argc);
    241     testArgumentTypes(name, argc);
    242   }
    243 }
    244 
    245 testNatives();
    246