Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: test/cctest/test-alloc.cc

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

Powered by Google App Engine
This is Rietveld 408576698