The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / cctest / test-alloc.cc @ 40c0f755

History | View | Annotate | Download (5.43 KB)

1
// Copyright 2007-2008 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
#include "v8.h"
29
#include "accessors.h"
30
#include "top.h"
31

    
32
#include "cctest.h"
33

    
34

    
35
using namespace v8::internal;
36

    
37

    
38
static Object* AllocateAfterFailures() {
39
  static int attempts = 0;
40
  if (++attempts < 3) return Failure::RetryAfterGC(0);
41

    
42
  // New space.
43
  NewSpace* new_space = Heap::new_space();
44
  static const int kNewSpaceFillerSize = ByteArray::SizeFor(0);
45
  while (new_space->Available() > kNewSpaceFillerSize) {
46
    CHECK(!Heap::AllocateByteArray(0)->IsFailure());
47
  }
48
  CHECK(!Heap::AllocateByteArray(100)->IsFailure());
49
  CHECK(!Heap::AllocateFixedArray(100, NOT_TENURED)->IsFailure());
50

    
51
  // Make sure we can allocate through optimized allocation functions
52
  // for specific kinds.
53
  CHECK(!Heap::AllocateFixedArray(100)->IsFailure());
54
  CHECK(!Heap::AllocateHeapNumber(0.42)->IsFailure());
55
  CHECK(!Heap::AllocateArgumentsObject(Smi::FromInt(87), 10)->IsFailure());
56
  Object* object = Heap::AllocateJSObject(*Top::object_function());
57
  CHECK(!Heap::CopyJSObject(JSObject::cast(object))->IsFailure());
58

    
59
  // Old data space.
60
  OldSpace* old_data_space = Heap::old_data_space();
61
  static const int kOldDataSpaceFillerSize = SeqAsciiString::SizeFor(0);
62
  while (old_data_space->Available() > kOldDataSpaceFillerSize) {
63
    CHECK(!Heap::AllocateRawAsciiString(0, TENURED)->IsFailure());
64
  }
65
  CHECK(!Heap::AllocateRawAsciiString(100, TENURED)->IsFailure());
66

    
67
  // Large object space.
68
  while (!Heap::OldGenerationAllocationLimitReached()) {
69
    CHECK(!Heap::AllocateFixedArray(10000, TENURED)->IsFailure());
70
  }
71
  CHECK(!Heap::AllocateFixedArray(10000, TENURED)->IsFailure());
72

    
73
  // Map space.
74
  MapSpace* map_space = Heap::map_space();
75
  static const int kMapSpaceFillerSize = Map::kSize;
76
  InstanceType instance_type = JS_OBJECT_TYPE;
77
  int instance_size = JSObject::kHeaderSize;
78
  while (map_space->Available() > kMapSpaceFillerSize) {
79
    CHECK(!Heap::AllocateMap(instance_type, instance_size)->IsFailure());
80
  }
81
  CHECK(!Heap::AllocateMap(instance_type, instance_size)->IsFailure());
82

    
83
  // Test that we can allocate in old pointer space and code space.
84
  CHECK(!Heap::AllocateFixedArray(100, TENURED)->IsFailure());
85
  CHECK(!Heap::CopyCode(Builtins::builtin(Builtins::Illegal))->IsFailure());
86

    
87
  // Return success.
88
  return Smi::FromInt(42);
89
}
90

    
91

    
92
static Handle<Object> Test() {
93
  CALL_HEAP_FUNCTION(AllocateAfterFailures(), Object);
94
}
95

    
96

    
97
TEST(StressHandles) {
98
  v8::Persistent<v8::Context> env = v8::Context::New();
99
  v8::HandleScope scope;
100
  env->Enter();
101
  Handle<Object> o = Test();
102
  CHECK(o->IsSmi() && Smi::cast(*o)->value() == 42);
103
  env->Exit();
104
}
105

    
106

    
107
static Object* TestAccessorGet(Object* object, void*) {
108
  return AllocateAfterFailures();
109
}
110

    
111

    
112
const AccessorDescriptor kDescriptor = {
113
  TestAccessorGet,
114
  0,
115
  0
116
};
117

    
118

    
119
TEST(StressJS) {
120
  v8::Persistent<v8::Context> env = v8::Context::New();
121
  v8::HandleScope scope;
122
  env->Enter();
123
  Handle<JSFunction> function =
124
      Factory::NewFunction(Factory::function_symbol(), Factory::null_value());
125
  // Force the creation of an initial map and set the code to
126
  // something empty.
127
  Factory::NewJSObject(function);
128
  function->set_code(Builtins::builtin(Builtins::EmptyFunction));
129
  // Patch the map to have an accessor for "get".
130
  Handle<Map> map(function->initial_map());
131
  Handle<DescriptorArray> instance_descriptors(map->instance_descriptors());
132
  Handle<Proxy> proxy = Factory::NewProxy(&kDescriptor);
133
  instance_descriptors = Factory::CopyAppendProxyDescriptor(
134
      instance_descriptors,
135
      Factory::NewStringFromAscii(Vector<const char>("get", 3)),
136
      proxy,
137
      static_cast<PropertyAttributes>(0));
138
  map->set_instance_descriptors(*instance_descriptors);
139
  // Add the Foo constructor the global object.
140
  env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function));
141
  // Call the accessor through JavaScript.
142
  v8::Handle<v8::Value> result =
143
      v8::Script::Compile(v8::String::New("(new Foo).get"))->Run();
144
  CHECK_EQ(42, result->Int32Value());
145
  env->Exit();
146
}