blob: e195d14923ef324dea976cd94aa2ec3d52594196 [file] [log] [blame]
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002// 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"
ager@chromium.org870a0b62008-11-04 11:43:05 +000029#include "accessors.h"
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000030
31#include "cctest.h"
32
33
34using namespace v8::internal;
35
36
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000037static inline void SimulateFullSpace(PagedSpace* space) {
danno@chromium.org1044a4d2012-04-30 12:34:39 +000038 int old_linear_size = static_cast<int>(space->limit() - space->top());
39 space->Free(space->top(), old_linear_size);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000040 space->SetTop(space->limit(), space->limit());
41 space->ResetFreeList();
42 space->ClearStats();
43}
44
45
lrn@chromium.org303ada72010-10-27 09:33:13 +000046static MaybeObject* AllocateAfterFailures() {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000047 static int attempts = 0;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +000048 if (++attempts < 3) return Failure::RetryAfterGC();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049 Heap* heap = Isolate::Current()->heap();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000050
51 // New space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000052 NewSpace* new_space = heap->new_space();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000053 static const int kNewSpaceFillerSize = ByteArray::SizeFor(0);
54 while (new_space->Available() > kNewSpaceFillerSize) {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +000055 int available_before = static_cast<int>(new_space->Available());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056 CHECK(!heap->AllocateByteArray(0)->IsFailure());
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000057 if (available_before == new_space->Available()) {
58 // It seems that we are avoiding new space allocations when
59 // allocation is forced, so no need to fill up new space
60 // in order to make the test harder.
61 break;
62 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000063 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000064 CHECK(!heap->AllocateByteArray(100)->IsFailure());
65 CHECK(!heap->AllocateFixedArray(100, NOT_TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000066
67 // Make sure we can allocate through optimized allocation functions
68 // for specific kinds.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000069 CHECK(!heap->AllocateFixedArray(100)->IsFailure());
70 CHECK(!heap->AllocateHeapNumber(0.42)->IsFailure());
71 CHECK(!heap->AllocateArgumentsObject(Smi::FromInt(87), 10)->IsFailure());
72 Object* object = heap->AllocateJSObject(
73 *Isolate::Current()->object_function())->ToObjectChecked();
74 CHECK(!heap->CopyJSObject(JSObject::cast(object))->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000075
76 // Old data space.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000077 SimulateFullSpace(heap->old_data_space());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000078 CHECK(!heap->AllocateRawAsciiString(100, TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000079
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000080 // Old pointer space.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000081 SimulateFullSpace(heap->old_pointer_space());
82 CHECK(!heap->AllocateFixedArray(10000, TENURED)->IsFailure());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000083
84 // Large object space.
85 static const int kLargeObjectSpaceFillerLength = 300000;
86 static const int kLargeObjectSpaceFillerSize = FixedArray::SizeFor(
87 kLargeObjectSpaceFillerLength);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000088 ASSERT(kLargeObjectSpaceFillerSize > heap->old_pointer_space()->AreaSize());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000089 while (heap->OldGenerationSpaceAvailable() > kLargeObjectSpaceFillerSize) {
90 CHECK(!heap->AllocateFixedArray(kLargeObjectSpaceFillerLength, TENURED)->
91 IsFailure());
92 }
93 CHECK(!heap->AllocateFixedArray(kLargeObjectSpaceFillerLength, TENURED)->
94 IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000095
96 // Map space.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000097 SimulateFullSpace(heap->map_space());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000098 int instance_size = JSObject::kHeaderSize;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000099 CHECK(!heap->AllocateMap(JS_OBJECT_TYPE, instance_size)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000100
101 // Test that we can allocate in old pointer space and code space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000102 CHECK(!heap->AllocateFixedArray(100, TENURED)->IsFailure());
103 CHECK(!heap->CopyCode(Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000104 Builtins::kIllegal))->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000105
106 // Return success.
107 return Smi::FromInt(42);
108}
109
ager@chromium.org870a0b62008-11-04 11:43:05 +0000110
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000111static Handle<Object> Test() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000112 CALL_HEAP_FUNCTION(ISOLATE, AllocateAfterFailures(), Object);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000113}
114
115
ager@chromium.org870a0b62008-11-04 11:43:05 +0000116TEST(StressHandles) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000117 v8::Persistent<v8::Context> env = v8::Context::New();
118 v8::HandleScope scope;
119 env->Enter();
120 Handle<Object> o = Test();
121 CHECK(o->IsSmi() && Smi::cast(*o)->value() == 42);
122 env->Exit();
123}
ager@chromium.org870a0b62008-11-04 11:43:05 +0000124
125
lrn@chromium.org303ada72010-10-27 09:33:13 +0000126static MaybeObject* TestAccessorGet(Object* object, void*) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000127 return AllocateAfterFailures();
128}
129
130
131const AccessorDescriptor kDescriptor = {
132 TestAccessorGet,
133 0,
134 0
135};
136
137
138TEST(StressJS) {
139 v8::Persistent<v8::Context> env = v8::Context::New();
140 v8::HandleScope scope;
141 env->Enter();
142 Handle<JSFunction> function =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000143 FACTORY->NewFunction(FACTORY->function_symbol(), FACTORY->null_value());
ager@chromium.org870a0b62008-11-04 11:43:05 +0000144 // Force the creation of an initial map and set the code to
145 // something empty.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000146 FACTORY->NewJSObject(function);
147 function->ReplaceCode(Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000148 Builtins::kEmptyFunction));
ager@chromium.org870a0b62008-11-04 11:43:05 +0000149 // Patch the map to have an accessor for "get".
150 Handle<Map> map(function->initial_map());
151 Handle<DescriptorArray> instance_descriptors(map->instance_descriptors());
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000152 Handle<Foreign> foreign = FACTORY->NewForeign(&kDescriptor);
153 instance_descriptors = FACTORY->CopyAppendForeignDescriptor(
ager@chromium.org870a0b62008-11-04 11:43:05 +0000154 instance_descriptors,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000155 FACTORY->NewStringFromAscii(Vector<const char>("get", 3)),
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000156 foreign,
ager@chromium.org870a0b62008-11-04 11:43:05 +0000157 static_cast<PropertyAttributes>(0));
158 map->set_instance_descriptors(*instance_descriptors);
159 // Add the Foo constructor the global object.
160 env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function));
161 // Call the accessor through JavaScript.
162 v8::Handle<v8::Value> result =
163 v8::Script::Compile(v8::String::New("(new Foo).get"))->Run();
164 CHECK_EQ(42, result->Int32Value());
165 env->Exit();
166}
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000167
168
169// CodeRange test.
170// Tests memory management in a CodeRange by allocating and freeing blocks,
171// using a pseudorandom generator to choose block sizes geometrically
172// distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize.
173// Ensure that the freed chunks are collected and reused by allocating (in
174// total) more than the size of the CodeRange.
175
176// This pseudorandom generator does not need to be particularly good.
177// Use the lower half of the V8::Random() generator.
178unsigned int Pseudorandom() {
179 static uint32_t lo = 2345;
180 lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0.
181 return lo & 0xFFFF;
182}
183
184
185// Plain old data class. Represents a block of allocated memory.
186class Block {
187 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000188 Block(Address base_arg, int size_arg)
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000189 : base(base_arg), size(size_arg) {}
190
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000191 Address base;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000192 int size;
193};
194
195
196TEST(CodeRange) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000197 const int code_range_size = 32*MB;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000198 OS::SetUp();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000199 Isolate::Current()->InitializeLoggingAndCounters();
200 CodeRange* code_range = new CodeRange(Isolate::Current());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000201 code_range->SetUp(code_range_size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000202 int current_allocated = 0;
203 int total_allocated = 0;
204 List<Block> blocks(1000);
205
206 while (total_allocated < 5 * code_range_size) {
207 if (current_allocated < code_range_size / 10) {
208 // Allocate a block.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000209 // Geometrically distributed sizes, greater than
210 // Page::kMaxNonCodeHeapObjectSize (which is greater than code page area).
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000211 // TODO(gc): instead of using 3 use some contant based on code_range_size
212 // kMaxHeapObjectSize.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000213 size_t requested =
214 (Page::kMaxNonCodeHeapObjectSize << (Pseudorandom() % 3)) +
215 Pseudorandom() % 5000 + 1;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000216 size_t allocated = 0;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000217 Address base = code_range->AllocateRawMemory(requested, &allocated);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000218 CHECK(base != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000219 blocks.Add(Block(base, static_cast<int>(allocated)));
220 current_allocated += static_cast<int>(allocated);
221 total_allocated += static_cast<int>(allocated);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000222 } else {
223 // Free a block.
224 int index = Pseudorandom() % blocks.length();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000225 code_range->FreeRawMemory(blocks[index].base, blocks[index].size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000226 current_allocated -= blocks[index].size;
227 if (index < blocks.length() - 1) {
228 blocks[index] = blocks.RemoveLast();
229 } else {
230 blocks.RemoveLast();
231 }
232 }
233 }
234
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000235 code_range->TearDown();
236 delete code_range;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000237}