types-fuzz.h 8.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Copyright 2014 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_TEST_CCTEST_TYPES_H_
#define V8_TEST_CCTEST_TYPES_H_

31
#include "src/base/utils/random-number-generator.h"
32
#include "src/compiler/js-heap-broker.h"
33
#include "src/execution/isolate.h"
34
#include "src/heap/factory.h"
35
#include "src/init/v8.h"
36 37 38

namespace v8 {
namespace internal {
39
namespace compiler {
40 41 42

class Types {
 public:
43
  Types(Zone* zone, Isolate* isolate, v8::base::RandomNumberGenerator* rng)
44
      : zone_(zone), js_heap_broker_(isolate, zone), rng_(rng) {
45 46 47
#define DECLARE_TYPE(name, value) \
  name = Type::name();            \
  types.push_back(name);
48
    PROPER_BITSET_TYPE_LIST(DECLARE_TYPE)
49
#undef DECLARE_TYPE
50

51 52
    SignedSmall = Type::SignedSmall();
    UnsignedSmall = Type::UnsignedSmall();
53

54
    Handle<i::Map> object_map =
55
        isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
56 57 58 59 60 61
    Handle<i::Smi> smi = handle(Smi::FromInt(666), isolate);
    Handle<i::HeapNumber> boxed_smi = isolate->factory()->NewHeapNumber(666);
    Handle<i::HeapNumber> signed32 =
        isolate->factory()->NewHeapNumber(0x40000000);
    Handle<i::HeapNumber> float1 = isolate->factory()->NewHeapNumber(1.53);
    Handle<i::HeapNumber> float2 = isolate->factory()->NewHeapNumber(0.53);
62 63
    // float3 is identical to float1 in order to test that OtherNumberConstant
    // types are equal by double value and not by handle pointer value.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    Handle<i::HeapNumber> float3 = isolate->factory()->NewHeapNumber(1.53);
    Handle<i::JSObject> object1 =
        isolate->factory()->NewJSObjectFromMap(object_map);
    Handle<i::JSObject> object2 =
        isolate->factory()->NewJSObjectFromMap(object_map);
    Handle<i::JSArray> array = isolate->factory()->NewJSArray(20);
    Handle<i::Oddball> uninitialized =
        isolate->factory()->uninitialized_value();
    Handle<i::Oddball> undefined = isolate->factory()->undefined_value();
    Handle<i::HeapNumber> nan = isolate->factory()->nan_value();

    SmiConstant = Type::Constant(js_heap_broker(), smi, zone);
    Signed32Constant = Type::Constant(js_heap_broker(), signed32, zone);
    ObjectConstant1 = Type::Constant(js_heap_broker(), object1, zone);
    ObjectConstant2 = Type::Constant(js_heap_broker(), object2, zone);
    ArrayConstant = Type::Constant(js_heap_broker(), array, zone);
80
    UninitializedConstant =
81
        Type::Constant(js_heap_broker(), uninitialized, zone);
82 83

    values.push_back(smi);
84
    values.push_back(boxed_smi);
85 86 87 88 89
    values.push_back(signed32);
    values.push_back(object1);
    values.push_back(object2);
    values.push_back(array);
    values.push_back(uninitialized);
90 91
    values.push_back(undefined);
    values.push_back(nan);
92 93 94
    values.push_back(float1);
    values.push_back(float2);
    values.push_back(float3);
95
    for (ValueVector::iterator it = values.begin(); it != values.end(); ++it) {
96
      types.push_back(Type::Constant(js_heap_broker(), *it, zone));
97 98
    }

99 100
    integers.push_back(isolate->factory()->NewNumber(-V8_INFINITY));
    integers.push_back(isolate->factory()->NewNumber(+V8_INFINITY));
101 102 103 104 105 106 107 108 109
    integers.push_back(isolate->factory()->NewNumber(-rng_->NextInt(10)));
    integers.push_back(isolate->factory()->NewNumber(+rng_->NextInt(10)));
    for (int i = 0; i < 10; ++i) {
      double x = rng_->NextInt();
      integers.push_back(isolate->factory()->NewNumber(x));
      x *= rng_->NextInt();
      if (!IsMinusZero(x)) integers.push_back(isolate->factory()->NewNumber(x));
    }

110
    Integer = Type::Range(-V8_INFINITY, +V8_INFINITY, zone);
111

112 113 114 115 116
    for (int i = 0; i < 30; ++i) {
      types.push_back(Fuzz());
    }
  }

117
#define DECLARE_TYPE(name, value) Type name;
118
  PROPER_BITSET_TYPE_LIST(DECLARE_TYPE)
119
#undef DECLARE_TYPE
120

121 122
  Type SignedSmall;
  Type UnsignedSmall;
123

124 125 126 127 128 129
  Type SmiConstant;
  Type Signed32Constant;
  Type ObjectConstant1;
  Type ObjectConstant2;
  Type ArrayConstant;
  Type UninitializedConstant;
130

131
  Type Integer;
132

133 134
  using TypeVector = std::vector<Type>;
  using ValueVector = std::vector<Handle<i::Object> >;
135 136 137 138 139

  TypeVector types;
  ValueVector values;
  ValueVector integers;  // "Integer" values used for range limits.

140 141
  Type Constant(Handle<i::Object> value) {
    return Type::Constant(js_heap_broker(), value, zone_);
142 143
  }

144
  Type HeapConstant(Handle<i::HeapObject> value) {
145
    return Type::Constant(js_heap_broker(), value, zone_);
146 147
  }

148
  Type Range(double min, double max) { return Type::Range(min, max, zone_); }
149

150
  Type Union(Type t1, Type t2) { return Type::Union(t1, t2, zone_); }
151

152
  Type Intersect(Type t1, Type t2) { return Type::Intersect(t1, t2, zone_); }
153

154
  Type Random() { return types[rng_->NextInt(static_cast<int>(types.size()))]; }
155

156
  Type Fuzz(int depth = 4) {
157 158
    switch (rng_->NextInt(depth == 0 ? 3 : 20)) {
      case 0: {  // bitset
159
#define COUNT_BITSET_TYPES(type, value) +1
160
        int n = 0 PROPER_BITSET_TYPE_LIST(COUNT_BITSET_TYPES);
161
#undef COUNT_BITSET_TYPES
162
        // Pick a bunch of named bitsets and return their intersection.
163
        Type result = Type::Any();
164 165
        for (int i = 0, m = 1 + rng_->NextInt(3); i < m; ++i) {
          int j = rng_->NextInt(n);
166 167 168
#define PICK_BITSET_TYPE(type, value)                        \
  if (j-- == 0) {                                            \
    Type tmp = Type::Intersect(result, Type::type(), zone_); \
169
    if (tmp.Is(Type::None()) && i != 0) {                    \
170 171 172 173 174
      break;                                                 \
    } else {                                                 \
      result = tmp;                                          \
      continue;                                              \
    }                                                        \
175
  }
176
          PROPER_BITSET_TYPE_LIST(PICK_BITSET_TYPE)
177
#undef PICK_BITSET_TYPE
178 179 180
        }
        return result;
      }
bmeurer's avatar
bmeurer committed
181
      case 1: {  // constant
182
        int i = rng_->NextInt(static_cast<int>(values.size()));
183
        return Type::Constant(js_heap_broker(), values[i], zone_);
184
      }
bmeurer's avatar
bmeurer committed
185
      case 2: {  // range
186 187
        int i = rng_->NextInt(static_cast<int>(integers.size()));
        int j = rng_->NextInt(static_cast<int>(integers.size()));
188 189 190
        double min = integers[i]->Number();
        double max = integers[j]->Number();
        if (min > max) std::swap(min, max);
191
        return Type::Range(min, max, zone_);
192 193 194
      }
      default: {  // union
        int n = rng_->NextInt(10);
195
        Type type = None;
196
        for (int i = 0; i < n; ++i) {
197
          Type operand = Fuzz(depth - 1);
198
          type = Type::Union(type, operand, zone_);
199 200 201 202 203 204 205
        }
        return type;
      }
    }
    UNREACHABLE();
  }

206
  Zone* zone() { return zone_; }
207
  JSHeapBroker* js_heap_broker() { return &js_heap_broker_; }
208 209

 private:
210
  Zone* zone_;
211
  JSHeapBroker js_heap_broker_;
212 213 214
  v8::base::RandomNumberGenerator* rng_;
};

215
}  // namespace compiler
216 217
}  // namespace internal
}  // namespace v8
218 219

#endif