Line data Source code
1 : // Copyright 2012 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 "src/v8.h"
29 : #include "test/cctest/cctest.h"
30 :
31 : #include "src/accessors.h"
32 : #include "src/api.h"
33 : #include "src/objects-inl.h"
34 : #include "src/property.h"
35 : #include "test/cctest/heap/heap-tester.h"
36 : #include "test/cctest/heap/heap-utils.h"
37 :
38 : namespace v8 {
39 : namespace internal {
40 : namespace heap {
41 :
42 12 : AllocationResult HeapTester::AllocateAfterFailures() {
43 84 : Heap* heap = CcTest::heap();
44 :
45 : // New space.
46 12 : heap->AllocateByteArray(100).ToObjectChecked();
47 12 : heap->AllocateFixedArray(100, NOT_TENURED).ToObjectChecked();
48 :
49 : // Make sure we can allocate through optimized allocation functions
50 : // for specific kinds.
51 12 : heap->AllocateFixedArray(100).ToObjectChecked();
52 12 : heap->AllocateHeapNumber().ToObjectChecked();
53 : Object* object = heap->AllocateJSObject(
54 24 : *CcTest::i_isolate()->object_function()).ToObjectChecked();
55 12 : heap->CopyJSObject(JSObject::cast(object)).ToObjectChecked();
56 :
57 : // Old data space.
58 12 : heap::SimulateFullSpace(heap->old_space());
59 12 : heap->AllocateByteArray(100, TENURED).ToObjectChecked();
60 :
61 : // Old pointer space.
62 12 : heap::SimulateFullSpace(heap->old_space());
63 12 : heap->AllocateFixedArray(10000, TENURED).ToObjectChecked();
64 :
65 : // Large object space.
66 : static const size_t kLargeObjectSpaceFillerLength =
67 : 3 * (Page::kPageSize / 10);
68 : static const size_t kLargeObjectSpaceFillerSize =
69 : FixedArray::SizeFor(kLargeObjectSpaceFillerLength);
70 24 : CHECK_GT(kLargeObjectSpaceFillerSize,
71 : static_cast<size_t>(heap->old_space()->AreaSize()));
72 96 : while (heap->OldGenerationSpaceAvailable() > kLargeObjectSpaceFillerSize) {
73 : heap->AllocateFixedArray(
74 84 : kLargeObjectSpaceFillerLength, TENURED).ToObjectChecked();
75 : }
76 : heap->AllocateFixedArray(
77 12 : kLargeObjectSpaceFillerLength, TENURED).ToObjectChecked();
78 :
79 : // Map space.
80 12 : heap::SimulateFullSpace(heap->map_space());
81 : int instance_size = JSObject::kHeaderSize;
82 12 : heap->AllocateMap(JS_OBJECT_TYPE, instance_size).ToObjectChecked();
83 :
84 : // Test that we can allocate in old pointer space and code space.
85 12 : heap::SimulateFullSpace(heap->code_space());
86 12 : heap->AllocateFixedArray(100, TENURED).ToObjectChecked();
87 : heap->CopyCode(CcTest::i_isolate()->builtins()->builtin(
88 12 : Builtins::kIllegal)).ToObjectChecked();
89 :
90 : // Return success.
91 12 : return heap->true_value();
92 : }
93 :
94 12 : Handle<Object> HeapTester::TestAllocateAfterFailures() {
95 : // Similar to what the CALL_AND_RETRY macro does in the last-resort case, we
96 : // are wrapping the allocator function in an AlwaysAllocateScope. Test that
97 : // all allocations succeed immediately without any retry.
98 12 : CcTest::CollectAllAvailableGarbage();
99 : AlwaysAllocateScope scope(CcTest::i_isolate());
100 36 : return handle(AllocateAfterFailures().ToObjectChecked(), CcTest::i_isolate());
101 : }
102 :
103 :
104 23724 : HEAP_TEST(StressHandles) {
105 6 : v8::HandleScope scope(CcTest::isolate());
106 6 : v8::Local<v8::Context> env = v8::Context::New(CcTest::isolate());
107 6 : env->Enter();
108 6 : Handle<Object> o = TestAllocateAfterFailures();
109 6 : CHECK(o->IsTrue(CcTest::i_isolate()));
110 6 : env->Exit();
111 6 : }
112 :
113 :
114 6 : void TestGetter(
115 : v8::Local<v8::Name> name,
116 : const v8::PropertyCallbackInfo<v8::Value>& info) {
117 : i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
118 : HandleScope scope(isolate);
119 : info.GetReturnValue().Set(
120 6 : v8::Utils::ToLocal(HeapTester::TestAllocateAfterFailures()));
121 6 : }
122 :
123 0 : void TestSetter(v8::Local<v8::Name> name, v8::Local<v8::Value> value,
124 : const v8::PropertyCallbackInfo<v8::Boolean>& info) {
125 0 : UNREACHABLE();
126 : }
127 :
128 :
129 6 : Handle<AccessorInfo> TestAccessorInfo(
130 : Isolate* isolate, PropertyAttributes attributes) {
131 6 : Handle<String> name = isolate->factory()->NewStringFromStaticChars("get");
132 : return Accessors::MakeAccessor(isolate, name, &TestGetter, &TestSetter,
133 6 : attributes);
134 : }
135 :
136 :
137 23724 : TEST(StressJS) {
138 : Isolate* isolate = CcTest::i_isolate();
139 : Factory* factory = isolate->factory();
140 6 : v8::HandleScope scope(CcTest::isolate());
141 6 : v8::Local<v8::Context> env = v8::Context::New(CcTest::isolate());
142 6 : env->Enter();
143 : Handle<JSFunction> function = factory->NewFunction(
144 6 : factory->function_string());
145 : // Force the creation of an initial map and set the code to
146 : // something empty.
147 6 : factory->NewJSObject(function);
148 : function->set_code(
149 6 : CcTest::i_isolate()->builtins()->builtin(Builtins::kEmptyFunction));
150 : // Patch the map to have an accessor for "get".
151 : Handle<Map> map(function->initial_map());
152 : Handle<DescriptorArray> instance_descriptors(map->instance_descriptors());
153 6 : CHECK_EQ(0, instance_descriptors->number_of_descriptors());
154 :
155 : PropertyAttributes attrs = NONE;
156 6 : Handle<AccessorInfo> foreign = TestAccessorInfo(isolate, attrs);
157 6 : Map::EnsureDescriptorSlack(map, 1);
158 :
159 : Descriptor d = Descriptor::AccessorConstant(
160 : Handle<Name>(Name::cast(foreign->name())), foreign, attrs);
161 6 : map->AppendDescriptor(&d);
162 :
163 : // Add the Foo constructor the global object.
164 24 : CHECK(env->Global()
165 : ->Set(env, v8::String::NewFromUtf8(CcTest::isolate(), "Foo",
166 : v8::NewStringType::kNormal)
167 : .ToLocalChecked(),
168 : v8::Utils::CallableToLocal(function))
169 : .FromJust());
170 : // Call the accessor through JavaScript.
171 : v8::Local<v8::Value> result =
172 : v8::Script::Compile(
173 : env, v8::String::NewFromUtf8(CcTest::isolate(), "(new Foo).get",
174 6 : v8::NewStringType::kNormal)
175 6 : .ToLocalChecked())
176 6 : .ToLocalChecked()
177 : ->Run(env)
178 6 : .ToLocalChecked();
179 12 : CHECK_EQ(true, result->BooleanValue(env).FromJust());
180 6 : env->Exit();
181 6 : }
182 :
183 :
184 : // CodeRange test.
185 : // Tests memory management in a CodeRange by allocating and freeing blocks,
186 : // using a pseudorandom generator to choose block sizes geometrically
187 : // distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize.
188 : // Ensure that the freed chunks are collected and reused by allocating (in
189 : // total) more than the size of the CodeRange.
190 :
191 : // This pseudorandom generator does not need to be particularly good.
192 : // Use the lower half of the V8::Random() generator.
193 0 : unsigned int Pseudorandom() {
194 : static uint32_t lo = 2345;
195 2232 : lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0.
196 2232 : return lo & 0xFFFF;
197 : }
198 :
199 : namespace {
200 :
201 : // Plain old data class. Represents a block of allocated memory.
202 : class Block {
203 : public:
204 : Block(Address base_arg, int size_arg)
205 750 : : base(base_arg), size(size_arg) {}
206 :
207 : Address base;
208 : int size;
209 : };
210 :
211 : } // namespace
212 :
213 23724 : TEST(CodeRange) {
214 : const size_t code_range_size = 32*MB;
215 6 : CcTest::InitializeVM();
216 6 : CodeRange code_range(reinterpret_cast<Isolate*>(CcTest::isolate()));
217 6 : code_range.SetUp(code_range_size);
218 : size_t current_allocated = 0;
219 : size_t total_allocated = 0;
220 : std::vector<Block> blocks;
221 6 : blocks.reserve(1000);
222 :
223 1494 : while (total_allocated < 5 * code_range_size) {
224 1482 : if (current_allocated < code_range_size / 10) {
225 : // Allocate a block.
226 : // Geometrically distributed sizes, greater than
227 : // kMaxRegularHeapObjectSize (which is greater than code page area).
228 : // TODO(gc): instead of using 3 use some contant based on code_range_size
229 : // kMaxRegularHeapObjectSize.
230 1500 : size_t requested = (kMaxRegularHeapObjectSize << (Pseudorandom() % 3)) +
231 1500 : Pseudorandom() % 5000 + 1;
232 750 : size_t allocated = 0;
233 :
234 : // The request size has to be at least 2 code guard pages larger than the
235 : // actual commit size.
236 : Address base = code_range.AllocateRawMemory(
237 750 : requested, requested - (2 * MemoryAllocator::CodePageGuardSize()),
238 750 : &allocated);
239 750 : CHECK_NOT_NULL(base);
240 750 : blocks.emplace_back(base, static_cast<int>(allocated));
241 750 : current_allocated += static_cast<int>(allocated);
242 750 : total_allocated += static_cast<int>(allocated);
243 : } else {
244 : // Free a block.
245 1464 : size_t index = Pseudorandom() % blocks.size();
246 732 : code_range.FreeRawMemory(blocks[index].base, blocks[index].size);
247 1464 : current_allocated -= blocks[index].size;
248 1464 : if (index < blocks.size() - 1) {
249 504 : blocks[index] = blocks.back();
250 : }
251 : blocks.pop_back();
252 : }
253 6 : }
254 6 : }
255 :
256 : } // namespace heap
257 : } // namespace internal
258 71154 : } // namespace v8
|