blob: 0ccf4b8b664d338aced867d635d1b7ba52318cec [file] [log] [blame]
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001// 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"
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.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000043 NewSpace* new_space = heap->new_space();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000044 static const int kNewSpaceFillerSize = ByteArray::SizeFor(0);
45 while (new_space->Available() > kNewSpaceFillerSize) {
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +000046 int available_before = static_cast<int>(new_space->Available());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000047 CHECK(!heap->AllocateByteArray(0)->IsFailure());
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000048 if (available_before == new_space->Available()) {
49 // It seems that we are avoiding new space allocations when
50 // allocation is forced, so no need to fill up new space
51 // in order to make the test harder.
52 break;
53 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000054 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055 CHECK(!heap->AllocateByteArray(100)->IsFailure());
56 CHECK(!heap->AllocateFixedArray(100, NOT_TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000057
58 // Make sure we can allocate through optimized allocation functions
59 // for specific kinds.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000060 CHECK(!heap->AllocateFixedArray(100)->IsFailure());
61 CHECK(!heap->AllocateHeapNumber(0.42)->IsFailure());
62 CHECK(!heap->AllocateArgumentsObject(Smi::FromInt(87), 10)->IsFailure());
63 Object* object = heap->AllocateJSObject(
64 *Isolate::Current()->object_function())->ToObjectChecked();
65 CHECK(!heap->CopyJSObject(JSObject::cast(object))->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000066
67 // Old data space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000068 OldSpace* old_data_space = heap->old_data_space();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000069 static const int kOldDataSpaceFillerSize = ByteArray::SizeFor(0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000070 while (old_data_space->Available() > kOldDataSpaceFillerSize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000071 CHECK(!heap->AllocateByteArray(0, TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000072 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 CHECK(!heap->AllocateRawAsciiString(100, TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000074
75 // Large object space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000076 while (!heap->OldGenerationAllocationLimitReached()) {
77 CHECK(!heap->AllocateFixedArray(10000, TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000078 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000079 CHECK(!heap->AllocateFixedArray(10000, TENURED)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000080
81 // Map space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 MapSpace* map_space = heap->map_space();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000083 static const int kMapSpaceFillerSize = Map::kSize;
84 InstanceType instance_type = JS_OBJECT_TYPE;
85 int instance_size = JSObject::kHeaderSize;
86 while (map_space->Available() > kMapSpaceFillerSize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000087 CHECK(!heap->AllocateMap(instance_type, instance_size)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000088 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000089 CHECK(!heap->AllocateMap(instance_type, instance_size)->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000090
91 // Test that we can allocate in old pointer space and code space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 CHECK(!heap->AllocateFixedArray(100, TENURED)->IsFailure());
93 CHECK(!heap->CopyCode(Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000094 Builtins::kIllegal))->IsFailure());
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000095
96 // Return success.
97 return Smi::FromInt(42);
98}
99
ager@chromium.org870a0b62008-11-04 11:43:05 +0000100
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000101static Handle<Object> Test() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000102 CALL_HEAP_FUNCTION(ISOLATE, AllocateAfterFailures(), Object);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000103}
104
105
ager@chromium.org870a0b62008-11-04 11:43:05 +0000106TEST(StressHandles) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000107 v8::Persistent<v8::Context> env = v8::Context::New();
108 v8::HandleScope scope;
109 env->Enter();
110 Handle<Object> o = Test();
111 CHECK(o->IsSmi() && Smi::cast(*o)->value() == 42);
112 env->Exit();
113}
ager@chromium.org870a0b62008-11-04 11:43:05 +0000114
115
lrn@chromium.org303ada72010-10-27 09:33:13 +0000116static MaybeObject* TestAccessorGet(Object* object, void*) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000117 return AllocateAfterFailures();
118}
119
120
121const AccessorDescriptor kDescriptor = {
122 TestAccessorGet,
123 0,
124 0
125};
126
127
128TEST(StressJS) {
129 v8::Persistent<v8::Context> env = v8::Context::New();
130 v8::HandleScope scope;
131 env->Enter();
132 Handle<JSFunction> function =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000133 FACTORY->NewFunction(FACTORY->function_symbol(), FACTORY->null_value());
ager@chromium.org870a0b62008-11-04 11:43:05 +0000134 // Force the creation of an initial map and set the code to
135 // something empty.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000136 FACTORY->NewJSObject(function);
137 function->ReplaceCode(Isolate::Current()->builtins()->builtin(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000138 Builtins::kEmptyFunction));
ager@chromium.org870a0b62008-11-04 11:43:05 +0000139 // Patch the map to have an accessor for "get".
140 Handle<Map> map(function->initial_map());
141 Handle<DescriptorArray> instance_descriptors(map->instance_descriptors());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142 Handle<Proxy> proxy = FACTORY->NewProxy(&kDescriptor);
143 instance_descriptors = FACTORY->CopyAppendProxyDescriptor(
ager@chromium.org870a0b62008-11-04 11:43:05 +0000144 instance_descriptors,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000145 FACTORY->NewStringFromAscii(Vector<const char>("get", 3)),
ager@chromium.org870a0b62008-11-04 11:43:05 +0000146 proxy,
147 static_cast<PropertyAttributes>(0));
148 map->set_instance_descriptors(*instance_descriptors);
149 // Add the Foo constructor the global object.
150 env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function));
151 // Call the accessor through JavaScript.
152 v8::Handle<v8::Value> result =
153 v8::Script::Compile(v8::String::New("(new Foo).get"))->Run();
154 CHECK_EQ(42, result->Int32Value());
155 env->Exit();
156}
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000157
158
159// CodeRange test.
160// Tests memory management in a CodeRange by allocating and freeing blocks,
161// using a pseudorandom generator to choose block sizes geometrically
162// distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize.
163// Ensure that the freed chunks are collected and reused by allocating (in
164// total) more than the size of the CodeRange.
165
166// This pseudorandom generator does not need to be particularly good.
167// Use the lower half of the V8::Random() generator.
168unsigned int Pseudorandom() {
169 static uint32_t lo = 2345;
170 lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0.
171 return lo & 0xFFFF;
172}
173
174
175// Plain old data class. Represents a block of allocated memory.
176class Block {
177 public:
178 Block(void* base_arg, int size_arg)
179 : base(base_arg), size(size_arg) {}
180
181 void *base;
182 int size;
183};
184
185
186TEST(CodeRange) {
187 const int code_range_size = 16*MB;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000188 OS::Setup();
189 Isolate::Current()->code_range()->Setup(code_range_size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000190 int current_allocated = 0;
191 int total_allocated = 0;
192 List<Block> blocks(1000);
193
194 while (total_allocated < 5 * code_range_size) {
195 if (current_allocated < code_range_size / 10) {
196 // Allocate a block.
197 // Geometrically distributed sizes, greater than Page::kPageSize.
198 size_t requested = (Page::kPageSize << (Pseudorandom() % 6)) +
199 Pseudorandom() % 5000 + 1;
200 size_t allocated = 0;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201 void* base = Isolate::Current()->code_range()->
202 AllocateRawMemory(requested, &allocated);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000203 CHECK(base != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000204 blocks.Add(Block(base, static_cast<int>(allocated)));
205 current_allocated += static_cast<int>(allocated);
206 total_allocated += static_cast<int>(allocated);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000207 } else {
208 // Free a block.
209 int index = Pseudorandom() % blocks.length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000210 Isolate::Current()->code_range()->FreeRawMemory(
211 blocks[index].base, blocks[index].size);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000212 current_allocated -= blocks[index].size;
213 if (index < blocks.length() - 1) {
214 blocks[index] = blocks.RemoveLast();
215 } else {
216 blocks.RemoveLast();
217 }
218 }
219 }
220
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000221 Isolate::Current()->code_range()->TearDown();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000222}