Home | History | Annotate | Download | only in fuzz-natives
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // TODO(jkummerow): There are many ways to improve these tests, e.g.:
      6 // - more variance in randomized inputs
      7 // - better time complexity management
      8 // - better code readability and documentation of intentions.
      9 
     10 var RUN_WITH_ALL_ARGUMENT_ENTRIES = false;
     11 var kOnManyArgumentsRemove = 5;
     12 
     13 function makeArguments() {
     14   var result = [ ];
     15   result.push(17);
     16   result.push(-31);
     17   result.push(new Array(100));
     18   result.push(new Array(100003));
     19   result.push(Number.MIN_VALUE);
     20   result.push("whoops");
     21   result.push("x");
     22   result.push({"x": 1, "y": 2});
     23   var slowCaseObj = {"a": 3, "b": 4, "c": 5};
     24   delete slowCaseObj.c;
     25   result.push(slowCaseObj);
     26   result.push(function () { return 8; });
     27   return result;
     28 }
     29 
     30 var kArgObjects = makeArguments().length;
     31 
     32 function makeFunction(name, argc) {
     33   var args = [];
     34   for (var i = 0; i < argc; i++)
     35     args.push("x" + i);
     36   var argsStr = args.join(", ");
     37   return new Function(argsStr,
     38                       "return %" + name + "(" + argsStr + ");");
     39 }
     40 
     41 function testArgumentCount(name, argc) {
     42   for (var i = 0; i < 10; i++) {
     43     var func = null;
     44     try {
     45       func = makeFunction(name, i);
     46     } catch (e) {
     47       if (e != "SyntaxError: Illegal access") throw e;
     48     }
     49     if (func === null && i == argc) {
     50       throw "unexpected exception";
     51     }
     52     var args = [ ];
     53     for (var j = 0; j < i; j++)
     54       args.push(0);
     55     try {
     56       func.apply(void 0, args);
     57     } catch (e) {
     58       // we don't care what happens as long as we don't crash
     59     }
     60   }
     61 }
     62 
     63 function testArgumentTypes(name, argc) {
     64   var type = 0;
     65   var hasMore = true;
     66   var func = makeFunction(name, argc);
     67   while (hasMore) {
     68     var argPool = makeArguments();
     69     // When we have 5 or more arguments we lower the amount of tests cases
     70     // by randomly removing kOnManyArgumentsRemove entries
     71     var numArguments = RUN_WITH_ALL_ARGUMENT_ENTRIES ?
     72       kArgObjects : kArgObjects - kOnManyArgumentsRemove;
     73     if (kArgObjects >= 5 && !RUN_WITH_ALL_ARGUMENT_ENTRIES) {
     74       for (var i = 0; i < kOnManyArgumentsRemove; i++) {
     75         var rand = Math.floor(Math.random() * (kArgObjects - i));
     76         argPool.splice(rand, 1);
     77       }
     78     }
     79     var current = type;
     80     hasMore = false;
     81     var argList = [ ];
     82     for (var i = 0; i < argc; i++) {
     83       var index = current % numArguments;
     84       current = (current / numArguments) << 0;
     85       if (index != (numArguments - 1))
     86         hasMore = true;
     87       argList.push(argPool[index]);
     88     }
     89     try {
     90       func.apply(void 0, argList);
     91     } catch (e) {
     92       // we don't care what happens as long as we don't crash
     93     }
     94     type++;
     95   }
     96 }
     97 
     98 testArgumentCount(NAME, ARGC);
     99 testArgumentTypes(NAME, ARGC);
    100