blob: bc469aa5201f9e08d2163f1e3c03967180bb63d8 [file] [log] [blame]
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001// Copyright 2012 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
lrn@chromium.org303ada72010-10-27 09:33:13 +000037static MaybeObject* AllocateAfterFailures() {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000038 static int attempts = 0;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +000039 if (++attempts < 3) return Failure::RetryAfterGC();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000040 Heap* heap = Isolate::Current()->heap();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000041
42 // New space.
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000043 SimulateFullSpace(heap->new_space());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000044 CHECK(!heap->AllocateByteArray(100)->IsFailure());
45 CHECK(!heap->AllocateFixedArray(100, NOT_TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000046
47 // Make sure we can allocate through optimized allocation functions
48 // for specific kinds.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049 CHECK(!heap->AllocateFixedArray(100)->IsFailure());
50 CHECK(!heap->AllocateHeapNumber(0.42)->IsFailure());
51 CHECK(!heap->AllocateArgumentsObject(Smi::FromInt(87), 10)->IsFailure());
52 Object* object = heap->AllocateJSObject(
53 *Isolate::Current()->object_function())->ToObjectChecked();
54 CHECK(!heap->CopyJSObject(JSObject::cast(object))->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000055
56 // Old data space.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000057 SimulateFullSpace(heap->old_data_space());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +000058 CHECK(!heap->AllocateRawOneByteString(100, TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000059
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000060 // Old pointer space.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000061 SimulateFullSpace(heap->old_pointer_space());
62 CHECK(!heap->AllocateFixedArray(10000, TENURED)->IsFailure());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000063
64 // Large object space.
65 static const int kLargeObjectSpaceFillerLength = 300000;
66 static const int kLargeObjectSpaceFillerSize = FixedArray::SizeFor(
67 kLargeObjectSpaceFillerLength);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +000068 ASSERT(kLargeObjectSpaceFillerSize > heap->old_pointer_space()->AreaSize());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000069 while (heap->OldGenerationSpaceAvailable() > kLargeObjectSpaceFillerSize) {
70 CHECK(!heap->AllocateFixedArray(kLargeObjectSpaceFillerLength, TENURED)->
71 IsFailure());
72 }
73 CHECK(!heap->AllocateFixedArray(kLargeObjectSpaceFillerLength, TENURED)->
74 IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000075
76 // Map space.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000077 SimulateFullSpace(heap->map_space());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000078 int instance_size = JSObject::kHeaderSize;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000079 CHECK(!heap->AllocateMap(JS_OBJECT_TYPE, instance_size)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000080
81 // Test that we can allocate in old pointer space and code space.
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000082 SimulateFullSpace(heap->code_space());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000083 CHECK(!heap->AllocateFixedArray(100, TENURED)->IsFailure());
84 CHECK(!heap->CopyCode(Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000085 Builtins::kIllegal))->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000086
87 // Return success.
88 return Smi::FromInt(42);
89}
90
ager@chromium.org870a0b62008-11-04 11:43:05 +000091
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000092static Handle<Object> Test() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000093 CALL_HEAP_FUNCTION(ISOLATE, AllocateAfterFailures(), Object);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000094}
95
96
ager@chromium.org870a0b62008-11-04 11:43:05 +000097TEST(StressHandles) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +000098 v8::HandleScope scope(v8::Isolate::GetCurrent());
99 v8::Handle<v8::Context> env = v8::Context::New(v8::Isolate::GetCurrent());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000100 env->Enter();
101 Handle<Object> o = Test();
102 CHECK(o->IsSmi() && Smi::cast(*o)->value() == 42);
103 env->Exit();
104}
ager@chromium.org870a0b62008-11-04 11:43:05 +0000105
106
lrn@chromium.org303ada72010-10-27 09:33:13 +0000107static MaybeObject* TestAccessorGet(Object* object, void*) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000108 return AllocateAfterFailures();
109}
110
111
112const AccessorDescriptor kDescriptor = {
113 TestAccessorGet,
114 0,
115 0
116};
117
118
119TEST(StressJS) {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000120 v8::HandleScope scope(v8::Isolate::GetCurrent());
121 v8::Handle<v8::Context> env = v8::Context::New(v8::Isolate::GetCurrent());
ager@chromium.org870a0b62008-11-04 11:43:05 +0000122 env->Enter();
123 Handle<JSFunction> function =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000124 FACTORY->NewFunction(FACTORY->function_string(), FACTORY->null_value());
ager@chromium.org870a0b62008-11-04 11:43:05 +0000125 // Force the creation of an initial map and set the code to
126 // something empty.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000127 FACTORY->NewJSObject(function);
128 function->ReplaceCode(Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000129 Builtins::kEmptyFunction));
ager@chromium.org870a0b62008-11-04 11:43:05 +0000130 // Patch the map to have an accessor for "get".
131 Handle<Map> map(function->initial_map());
132 Handle<DescriptorArray> instance_descriptors(map->instance_descriptors());
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000133 Handle<Foreign> foreign = FACTORY->NewForeign(&kDescriptor);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000134 Handle<String> name =
135 FACTORY->NewStringFromAscii(Vector<const char>("get", 3));
136 ASSERT(instance_descriptors->IsEmpty());
137
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000138 Handle<DescriptorArray> new_descriptors = FACTORY->NewDescriptorArray(0, 1);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000139
140 v8::internal::DescriptorArray::WhitenessWitness witness(*new_descriptors);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000141 map->set_instance_descriptors(*new_descriptors);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000142
143 CallbacksDescriptor d(*name,
144 *foreign,
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000145 static_cast<PropertyAttributes>(0));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000146 map->AppendDescriptor(&d, witness);
147
ager@chromium.org870a0b62008-11-04 11:43:05 +0000148 // Add the Foo constructor the global object.
149 env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function));
150 // Call the accessor through JavaScript.
151 v8::Handle<v8::Value> result =
152 v8::Script::Compile(v8::String::New("(new Foo).get"))->Run();
153 CHECK_EQ(42, result->Int32Value());
154 env->Exit();
155}
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000156
157
158// CodeRange test.
159// Tests memory management in a CodeRange by allocating and freeing blocks,
160// using a pseudorandom generator to choose block sizes geometrically
161// distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize.
162// Ensure that the freed chunks are collected and reused by allocating (in
163// total) more than the size of the CodeRange.
164
165// This pseudorandom generator does not need to be particularly good.
166// Use the lower half of the V8::Random() generator.
167unsigned int Pseudorandom() {
168 static uint32_t lo = 2345;
169 lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0.
170 return lo & 0xFFFF;
171}
172
173
174// Plain old data class. Represents a block of allocated memory.
175class Block {
176 public:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000177 Block(Address base_arg, int size_arg)
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000178 : base(base_arg), size(size_arg) {}
179
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000180 Address base;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000181 int size;
182};
183
184
185TEST(CodeRange) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000186 const int code_range_size = 32*MB;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000187 OS::SetUp();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000188 Isolate::Current()->InitializeLoggingAndCounters();
189 CodeRange* code_range = new CodeRange(Isolate::Current());
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000190 code_range->SetUp(code_range_size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000191 int current_allocated = 0;
192 int total_allocated = 0;
193 List<Block> blocks(1000);
194
195 while (total_allocated < 5 * code_range_size) {
196 if (current_allocated < code_range_size / 10) {
197 // Allocate a block.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000198 // Geometrically distributed sizes, greater than
199 // Page::kMaxNonCodeHeapObjectSize (which is greater than code page area).
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000200 // TODO(gc): instead of using 3 use some contant based on code_range_size
201 // kMaxHeapObjectSize.
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000202 size_t requested =
203 (Page::kMaxNonCodeHeapObjectSize << (Pseudorandom() % 3)) +
204 Pseudorandom() % 5000 + 1;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000205 size_t allocated = 0;
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +0000206 Address base = code_range->AllocateRawMemory(requested,
207 requested,
208 &allocated);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000209 CHECK(base != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000210 blocks.Add(Block(base, static_cast<int>(allocated)));
211 current_allocated += static_cast<int>(allocated);
212 total_allocated += static_cast<int>(allocated);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000213 } else {
214 // Free a block.
215 int index = Pseudorandom() % blocks.length();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000216 code_range->FreeRawMemory(blocks[index].base, blocks[index].size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000217 current_allocated -= blocks[index].size;
218 if (index < blocks.length() - 1) {
219 blocks[index] = blocks.RemoveLast();
220 } else {
221 blocks.RemoveLast();
222 }
223 }
224 }
225
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000226 code_range->TearDown();
227 delete code_range;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000228}