2012-07-19 14:45:19 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-10-30 09:15:58 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/accessors.h"
|
|
|
|
#include "src/api.h"
|
2015-08-21 12:40:22 +00:00
|
|
|
#include "test/cctest/heap-tester.h"
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
|
|
|
|
2015-08-21 12:40:22 +00:00
|
|
|
AllocationResult v8::internal::HeapTester::AllocateAfterFailures() {
|
|
|
|
Heap* heap = CcTest::heap();
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
// New space.
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateByteArray(100).ToObjectChecked();
|
|
|
|
heap->AllocateFixedArray(100, NOT_TENURED).ToObjectChecked();
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
// Make sure we can allocate through optimized allocation functions
|
|
|
|
// for specific kinds.
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateFixedArray(100).ToObjectChecked();
|
|
|
|
heap->AllocateHeapNumber(0.42).ToObjectChecked();
|
2011-03-18 20:35:07 +00:00
|
|
|
Object* object = heap->AllocateJSObject(
|
2014-04-30 12:25:18 +00:00
|
|
|
*CcTest::i_isolate()->object_function()).ToObjectChecked();
|
|
|
|
heap->CopyJSObject(JSObject::cast(object)).ToObjectChecked();
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
// Old data space.
|
2015-04-07 11:31:57 +00:00
|
|
|
SimulateFullSpace(heap->old_space());
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateByteArray(100, TENURED).ToObjectChecked();
|
2008-10-30 09:15:58 +00:00
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
// Old pointer space.
|
2015-04-07 11:31:57 +00:00
|
|
|
SimulateFullSpace(heap->old_space());
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateFixedArray(10000, TENURED).ToObjectChecked();
|
2011-09-19 18:36:47 +00:00
|
|
|
|
2008-10-30 09:15:58 +00:00
|
|
|
// Large object space.
|
2015-02-24 19:05:21 +00:00
|
|
|
static const int kLargeObjectSpaceFillerLength = 3 * (Page::kPageSize / 10);
|
2011-09-19 18:36:47 +00:00
|
|
|
static const int kLargeObjectSpaceFillerSize = FixedArray::SizeFor(
|
|
|
|
kLargeObjectSpaceFillerLength);
|
2015-04-07 11:31:57 +00:00
|
|
|
DCHECK(kLargeObjectSpaceFillerSize > heap->old_space()->AreaSize());
|
2011-09-19 18:36:47 +00:00
|
|
|
while (heap->OldGenerationSpaceAvailable() > kLargeObjectSpaceFillerSize) {
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateFixedArray(
|
|
|
|
kLargeObjectSpaceFillerLength, TENURED).ToObjectChecked();
|
2008-10-30 09:15:58 +00:00
|
|
|
}
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateFixedArray(
|
|
|
|
kLargeObjectSpaceFillerLength, TENURED).ToObjectChecked();
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
// Map space.
|
2012-04-12 10:06:32 +00:00
|
|
|
SimulateFullSpace(heap->map_space());
|
2008-10-30 09:15:58 +00:00
|
|
|
int instance_size = JSObject::kHeaderSize;
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateMap(JS_OBJECT_TYPE, instance_size).ToObjectChecked();
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
// Test that we can allocate in old pointer space and code space.
|
2012-12-10 15:14:20 +00:00
|
|
|
SimulateFullSpace(heap->code_space());
|
2014-04-30 12:25:18 +00:00
|
|
|
heap->AllocateFixedArray(100, TENURED).ToObjectChecked();
|
|
|
|
heap->CopyCode(CcTest::i_isolate()->builtins()->builtin(
|
|
|
|
Builtins::kIllegal)).ToObjectChecked();
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
// Return success.
|
2014-11-03 08:43:20 +00:00
|
|
|
return heap->true_value();
|
2008-10-30 09:15:58 +00:00
|
|
|
}
|
|
|
|
|
2008-10-30 14:16:02 +00:00
|
|
|
|
2015-08-21 12:40:22 +00:00
|
|
|
Handle<Object> v8::internal::HeapTester::TestAllocateAfterFailures() {
|
2015-09-28 18:08:17 +00:00
|
|
|
// Similar to what the CALL_AND_RETRY macro does in the last-resort case, we
|
|
|
|
// are wrapping the allocator function in an AlwaysAllocateScope. Test that
|
|
|
|
// all allocations succeed immediately without any retry.
|
|
|
|
CcTest::heap()->CollectAllAvailableGarbage("panic");
|
|
|
|
AlwaysAllocateScope scope(CcTest::i_isolate());
|
|
|
|
return handle(AllocateAfterFailures().ToObjectChecked(), CcTest::i_isolate());
|
2008-10-30 09:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-21 12:40:22 +00:00
|
|
|
HEAP_TEST(StressHandles) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Handle<v8::Context> env = v8::Context::New(CcTest::isolate());
|
2008-10-30 09:15:58 +00:00
|
|
|
env->Enter();
|
2015-08-21 12:40:22 +00:00
|
|
|
Handle<Object> o = TestAllocateAfterFailures();
|
2014-11-03 08:43:20 +00:00
|
|
|
CHECK(o->IsTrue());
|
2008-10-30 09:15:58 +00:00
|
|
|
env->Exit();
|
|
|
|
}
|
2008-10-30 14:16:02 +00:00
|
|
|
|
|
|
|
|
2014-04-29 10:59:14 +00:00
|
|
|
void TestGetter(
|
2014-08-20 15:25:13 +00:00
|
|
|
v8::Local<v8::Name> name,
|
2014-04-29 10:59:14 +00:00
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
|
|
|
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
|
|
|
|
HandleScope scope(isolate);
|
2015-08-21 12:40:22 +00:00
|
|
|
info.GetReturnValue().Set(v8::Utils::ToLocal(
|
|
|
|
v8::internal::HeapTester::TestAllocateAfterFailures()));
|
2008-10-30 14:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-29 10:59:14 +00:00
|
|
|
void TestSetter(
|
2014-08-20 15:25:13 +00:00
|
|
|
v8::Local<v8::Name> name,
|
2014-04-29 10:59:14 +00:00
|
|
|
v8::Local<v8::Value> value,
|
|
|
|
const v8::PropertyCallbackInfo<void>& info) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<AccessorInfo> TestAccessorInfo(
|
|
|
|
Isolate* isolate, PropertyAttributes attributes) {
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> name = isolate->factory()->NewStringFromStaticChars("get");
|
2014-04-29 10:59:14 +00:00
|
|
|
return Accessors::MakeAccessor(isolate, name, &TestGetter, &TestSetter,
|
|
|
|
attributes);
|
|
|
|
}
|
2008-10-30 14:16:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(StressJS) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
|
|
|
v8::Handle<v8::Context> env = v8::Context::New(CcTest::isolate());
|
2008-10-30 14:16:02 +00:00
|
|
|
env->Enter();
|
2014-05-09 16:39:33 +00:00
|
|
|
Handle<JSFunction> function = factory->NewFunction(
|
|
|
|
factory->function_string());
|
2008-10-30 14:16:02 +00:00
|
|
|
// Force the creation of an initial map and set the code to
|
|
|
|
// something empty.
|
2013-06-04 10:30:05 +00:00
|
|
|
factory->NewJSObject(function);
|
2013-09-19 09:17:13 +00:00
|
|
|
function->ReplaceCode(CcTest::i_isolate()->builtins()->builtin(
|
2011-03-23 13:40:07 +00:00
|
|
|
Builtins::kEmptyFunction));
|
2008-10-30 14:16:02 +00:00
|
|
|
// Patch the map to have an accessor for "get".
|
|
|
|
Handle<Map> map(function->initial_map());
|
|
|
|
Handle<DescriptorArray> instance_descriptors(map->instance_descriptors());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(instance_descriptors->IsEmpty());
|
2012-07-18 14:00:58 +00:00
|
|
|
|
2014-04-29 10:59:14 +00:00
|
|
|
PropertyAttributes attrs = static_cast<PropertyAttributes>(0);
|
|
|
|
Handle<AccessorInfo> foreign = TestAccessorInfo(isolate, attrs);
|
2014-04-11 12:13:53 +00:00
|
|
|
Map::EnsureDescriptorSlack(map, 1);
|
2012-07-18 14:00:58 +00:00
|
|
|
|
2015-01-19 17:49:13 +00:00
|
|
|
AccessorConstantDescriptor d(Handle<Name>(Name::cast(foreign->name())),
|
|
|
|
foreign, attrs);
|
2014-04-11 12:13:53 +00:00
|
|
|
map->AppendDescriptor(&d);
|
2012-07-18 14:00:58 +00:00
|
|
|
|
2008-10-30 14:16:02 +00:00
|
|
|
// Add the Foo constructor the global object.
|
2013-11-22 12:43:17 +00:00
|
|
|
env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "Foo"),
|
2015-10-26 13:36:36 +00:00
|
|
|
v8::Utils::CallableToLocal(function));
|
2008-10-30 14:16:02 +00:00
|
|
|
// Call the accessor through JavaScript.
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Handle<v8::Value> result = v8::Script::Compile(
|
|
|
|
v8::String::NewFromUtf8(CcTest::isolate(), "(new Foo).get"))->Run();
|
2014-11-03 08:43:20 +00:00
|
|
|
CHECK_EQ(true, result->BooleanValue());
|
2008-10-30 14:16:02 +00:00
|
|
|
env->Exit();
|
|
|
|
}
|
2009-10-05 11:16:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
// CodeRange test.
|
|
|
|
// Tests memory management in a CodeRange by allocating and freeing blocks,
|
|
|
|
// using a pseudorandom generator to choose block sizes geometrically
|
|
|
|
// distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize.
|
|
|
|
// Ensure that the freed chunks are collected and reused by allocating (in
|
|
|
|
// total) more than the size of the CodeRange.
|
|
|
|
|
|
|
|
// This pseudorandom generator does not need to be particularly good.
|
|
|
|
// Use the lower half of the V8::Random() generator.
|
|
|
|
unsigned int Pseudorandom() {
|
|
|
|
static uint32_t lo = 2345;
|
|
|
|
lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0.
|
|
|
|
return lo & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Plain old data class. Represents a block of allocated memory.
|
|
|
|
class Block {
|
|
|
|
public:
|
2011-09-19 18:36:47 +00:00
|
|
|
Block(Address base_arg, int size_arg)
|
2009-10-05 11:16:25 +00:00
|
|
|
: base(base_arg), size(size_arg) {}
|
|
|
|
|
2011-09-19 18:36:47 +00:00
|
|
|
Address base;
|
2009-10-05 11:16:25 +00:00
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(CodeRange) {
|
2014-06-13 11:06:42 +00:00
|
|
|
const size_t code_range_size = 32*MB;
|
2013-09-05 08:17:57 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
CodeRange code_range(reinterpret_cast<Isolate*>(CcTest::isolate()));
|
2015-01-09 13:07:29 +00:00
|
|
|
code_range.SetUp(code_range_size +
|
|
|
|
kReservedCodeRangePages * v8::base::OS::CommitPageSize());
|
2014-06-13 11:06:42 +00:00
|
|
|
size_t current_allocated = 0;
|
|
|
|
size_t total_allocated = 0;
|
2014-07-30 13:54:45 +00:00
|
|
|
List< ::Block> blocks(1000);
|
2009-10-05 11:16:25 +00:00
|
|
|
|
|
|
|
while (total_allocated < 5 * code_range_size) {
|
|
|
|
if (current_allocated < code_range_size / 10) {
|
|
|
|
// Allocate a block.
|
2012-02-23 12:11:24 +00:00
|
|
|
// Geometrically distributed sizes, greater than
|
2014-01-23 13:02:27 +00:00
|
|
|
// Page::kMaxRegularHeapObjectSize (which is greater than code page area).
|
2011-09-19 18:36:47 +00:00
|
|
|
// TODO(gc): instead of using 3 use some contant based on code_range_size
|
2015-02-24 19:05:21 +00:00
|
|
|
// kMaxRegularHeapObjectSize.
|
2012-02-23 12:11:24 +00:00
|
|
|
size_t requested =
|
2014-01-23 13:02:27 +00:00
|
|
|
(Page::kMaxRegularHeapObjectSize << (Pseudorandom() % 3)) +
|
2012-02-23 12:11:24 +00:00
|
|
|
Pseudorandom() % 5000 + 1;
|
2009-10-05 11:16:25 +00:00
|
|
|
size_t allocated = 0;
|
2015-09-01 18:39:13 +00:00
|
|
|
|
|
|
|
// The request size has to be at least 2 code guard pages larger than the
|
|
|
|
// actual commit size.
|
|
|
|
Address base = code_range.AllocateRawMemory(
|
|
|
|
requested, requested - (2 * MemoryAllocator::CodePageGuardSize()),
|
|
|
|
&allocated);
|
2011-03-30 17:16:36 +00:00
|
|
|
CHECK(base != NULL);
|
2014-07-30 13:54:45 +00:00
|
|
|
blocks.Add(::Block(base, static_cast<int>(allocated)));
|
2009-11-11 09:50:06 +00:00
|
|
|
current_allocated += static_cast<int>(allocated);
|
|
|
|
total_allocated += static_cast<int>(allocated);
|
2009-10-05 11:16:25 +00:00
|
|
|
} else {
|
|
|
|
// Free a block.
|
|
|
|
int index = Pseudorandom() % blocks.length();
|
2013-09-05 08:17:57 +00:00
|
|
|
code_range.FreeRawMemory(blocks[index].base, blocks[index].size);
|
2009-10-05 11:16:25 +00:00
|
|
|
current_allocated -= blocks[index].size;
|
|
|
|
if (index < blocks.length() - 1) {
|
|
|
|
blocks[index] = blocks.RemoveLast();
|
|
|
|
} else {
|
|
|
|
blocks.RemoveLast();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|