blob: 83ab1a9cb14e7e6e2d1e755c7df3bab2d542aad9 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +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"
29#include "accessors.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000030
31#include "cctest.h"
32
33
34using namespace v8::internal;
35
36
John Reck59135872010-11-02 12:39:01 -070037static MaybeObject* AllocateAfterFailures() {
Steve Blocka7e24c12009-10-30 11:49:00 +000038 static int attempts = 0;
Ben Murdochf87a2032010-10-22 12:50:53 +010039 if (++attempts < 3) return Failure::RetryAfterGC();
Steve Block44f0eee2011-05-26 01:26:41 +010040 Heap* heap = Isolate::Current()->heap();
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42 // New space.
Steve Block44f0eee2011-05-26 01:26:41 +010043 NewSpace* new_space = heap->new_space();
Steve Blocka7e24c12009-10-30 11:49:00 +000044 static const int kNewSpaceFillerSize = ByteArray::SizeFor(0);
45 while (new_space->Available() > kNewSpaceFillerSize) {
Ben Murdochf87a2032010-10-22 12:50:53 +010046 int available_before = static_cast<int>(new_space->Available());
Steve Block44f0eee2011-05-26 01:26:41 +010047 CHECK(!heap->AllocateByteArray(0)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +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 }
54 }
Steve Block44f0eee2011-05-26 01:26:41 +010055 CHECK(!heap->AllocateByteArray(100)->IsFailure());
56 CHECK(!heap->AllocateFixedArray(100, NOT_TENURED)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000057
58 // Make sure we can allocate through optimized allocation functions
59 // for specific kinds.
Steve Block44f0eee2011-05-26 01:26:41 +010060 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());
Steve Blocka7e24c12009-10-30 11:49:00 +000066
67 // Old data space.
Steve Block44f0eee2011-05-26 01:26:41 +010068 OldSpace* old_data_space = heap->old_data_space();
Steve Blockd0582a62009-12-15 09:54:21 +000069 static const int kOldDataSpaceFillerSize = ByteArray::SizeFor(0);
Steve Blocka7e24c12009-10-30 11:49:00 +000070 while (old_data_space->Available() > kOldDataSpaceFillerSize) {
Steve Block44f0eee2011-05-26 01:26:41 +010071 CHECK(!heap->AllocateByteArray(0, TENURED)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000072 }
Steve Block44f0eee2011-05-26 01:26:41 +010073 CHECK(!heap->AllocateRawAsciiString(100, TENURED)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000074
75 // Large object space.
Steve Block44f0eee2011-05-26 01:26:41 +010076 while (!heap->OldGenerationAllocationLimitReached()) {
77 CHECK(!heap->AllocateFixedArray(10000, TENURED)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000078 }
Steve Block44f0eee2011-05-26 01:26:41 +010079 CHECK(!heap->AllocateFixedArray(10000, TENURED)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81 // Map space.
Steve Block44f0eee2011-05-26 01:26:41 +010082 MapSpace* map_space = heap->map_space();
Steve Blocka7e24c12009-10-30 11:49:00 +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) {
Steve Block44f0eee2011-05-26 01:26:41 +010087 CHECK(!heap->AllocateMap(instance_type, instance_size)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000088 }
Steve Block44f0eee2011-05-26 01:26:41 +010089 CHECK(!heap->AllocateMap(instance_type, instance_size)->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000090
91 // Test that we can allocate in old pointer space and code space.
Steve Block44f0eee2011-05-26 01:26:41 +010092 CHECK(!heap->AllocateFixedArray(100, TENURED)->IsFailure());
93 CHECK(!heap->CopyCode(Isolate::Current()->builtins()->builtin(
94 Builtins::kIllegal))->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +000095
96 // Return success.
97 return Smi::FromInt(42);
98}
99
100
101static Handle<Object> Test() {
Steve Block44f0eee2011-05-26 01:26:41 +0100102 CALL_HEAP_FUNCTION(ISOLATE, AllocateAfterFailures(), Object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000103}
104
105
106TEST(StressHandles) {
107 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}
114
115
John Reck59135872010-11-02 12:39:01 -0700116static MaybeObject* TestAccessorGet(Object* object, void*) {
Steve Blocka7e24c12009-10-30 11:49:00 +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 =
Steve Block44f0eee2011-05-26 01:26:41 +0100133 FACTORY->NewFunction(FACTORY->function_symbol(), FACTORY->null_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000134 // Force the creation of an initial map and set the code to
135 // something empty.
Steve Block44f0eee2011-05-26 01:26:41 +0100136 FACTORY->NewJSObject(function);
137 function->ReplaceCode(Isolate::Current()->builtins()->builtin(
138 Builtins::kEmptyFunction));
Steve Blocka7e24c12009-10-30 11:49:00 +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());
Steve Block44f0eee2011-05-26 01:26:41 +0100142 Handle<Proxy> proxy = FACTORY->NewProxy(&kDescriptor);
143 instance_descriptors = FACTORY->CopyAppendProxyDescriptor(
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 instance_descriptors,
Steve Block44f0eee2011-05-26 01:26:41 +0100145 FACTORY->NewStringFromAscii(Vector<const char>("get", 3)),
Steve Blocka7e24c12009-10-30 11:49:00 +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}
157
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;
Steve Block44f0eee2011-05-26 01:26:41 +0100188 OS::Setup();
Ben Murdoch6d7cb002011-08-04 19:25:22 +0100189 Isolate::Current()->InitializeLoggingAndCounters();
190 CodeRange* code_range = new CodeRange(Isolate::Current());
191 code_range->Setup(code_range_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 int current_allocated = 0;
193 int total_allocated = 0;
194 List<Block> blocks(1000);
195
196 while (total_allocated < 5 * code_range_size) {
197 if (current_allocated < code_range_size / 10) {
198 // Allocate a block.
199 // Geometrically distributed sizes, greater than Page::kPageSize.
200 size_t requested = (Page::kPageSize << (Pseudorandom() % 6)) +
201 Pseudorandom() % 5000 + 1;
202 size_t allocated = 0;
Ben Murdoch6d7cb002011-08-04 19:25:22 +0100203 void* base = code_range->AllocateRawMemory(requested, &allocated);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100204 CHECK(base != NULL);
Steve Blockd0582a62009-12-15 09:54:21 +0000205 blocks.Add(Block(base, static_cast<int>(allocated)));
206 current_allocated += static_cast<int>(allocated);
207 total_allocated += static_cast<int>(allocated);
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 } else {
209 // Free a block.
210 int index = Pseudorandom() % blocks.length();
Ben Murdoch6d7cb002011-08-04 19:25:22 +0100211 code_range->FreeRawMemory(blocks[index].base, blocks[index].size);
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdoch6d7cb002011-08-04 19:25:22 +0100221 code_range->TearDown();
222 delete code_range;
Steve Blocka7e24c12009-10-30 11:49:00 +0000223}