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"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.h"
|
2018-04-30 13:27:37 +00:00
|
|
|
#include "src/objects/api-callbacks.h"
|
2017-01-03 13:16:59 +00:00
|
|
|
#include "src/property.h"
|
2015-12-09 11:25:26 +00:00
|
|
|
#include "test/cctest/heap/heap-tester.h"
|
2016-05-20 13:30:22 +00:00
|
|
|
#include "test/cctest/heap/heap-utils.h"
|
2008-10-30 09:15:58 +00:00
|
|
|
|
2017-08-11 10:04:47 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace heap {
|
2008-10-30 09:15:58 +00:00
|
|
|
|
2018-04-09 19:11:22 +00:00
|
|
|
Handle<Object> HeapTester::TestAllocateAfterFailures() {
|
|
|
|
// Similar to what the factory's retrying logic does in the last-resort case,
|
|
|
|
// we wrap the allocator function in an AlwaysAllocateScope. Test that
|
|
|
|
// all allocations succeed immediately without any retry.
|
|
|
|
CcTest::CollectAllAvailableGarbage();
|
|
|
|
AlwaysAllocateScope scope(CcTest::i_isolate());
|
2015-08-21 12:40:22 +00:00
|
|
|
Heap* heap = CcTest::heap();
|
2018-04-09 19:11:22 +00:00
|
|
|
int size = FixedArray::SizeFor(100);
|
2008-10-30 09:15:58 +00:00
|
|
|
// New space.
|
2018-04-09 19:11:22 +00:00
|
|
|
HeapObject* obj = heap->AllocateRaw(size, NEW_SPACE).ToObjectChecked();
|
|
|
|
// In order to pass heap verification on Isolate teardown, mark the
|
|
|
|
// allocated area as a filler.
|
|
|
|
heap->CreateFillerObjectAt(obj->address(), size, ClearRecordedSlots::kNo);
|
2008-10-30 09:15:58 +00:00
|
|
|
|
2018-04-09 19:11:22 +00:00
|
|
|
// Old space.
|
2016-05-20 13:30:22 +00:00
|
|
|
heap::SimulateFullSpace(heap->old_space());
|
2018-04-09 19:11:22 +00:00
|
|
|
obj = heap->AllocateRaw(size, OLD_SPACE).ToObjectChecked();
|
|
|
|
heap->CreateFillerObjectAt(obj->address(), size, ClearRecordedSlots::kNo);
|
2011-09-19 18:36:47 +00:00
|
|
|
|
2008-10-30 09:15:58 +00:00
|
|
|
// Large object space.
|
2016-11-14 16:46:42 +00:00
|
|
|
static const size_t kLargeObjectSpaceFillerLength =
|
|
|
|
3 * (Page::kPageSize / 10);
|
|
|
|
static const size_t kLargeObjectSpaceFillerSize =
|
|
|
|
FixedArray::SizeFor(kLargeObjectSpaceFillerLength);
|
|
|
|
CHECK_GT(kLargeObjectSpaceFillerSize,
|
|
|
|
static_cast<size_t>(heap->old_space()->AreaSize()));
|
2011-09-19 18:36:47 +00:00
|
|
|
while (heap->OldGenerationSpaceAvailable() > kLargeObjectSpaceFillerSize) {
|
2018-04-09 19:11:22 +00:00
|
|
|
obj = heap->AllocateRaw(kLargeObjectSpaceFillerSize, OLD_SPACE)
|
|
|
|
.ToObjectChecked();
|
|
|
|
heap->CreateFillerObjectAt(obj->address(), size, ClearRecordedSlots::kNo);
|
2008-10-30 09:15:58 +00:00
|
|
|
}
|
2018-04-09 19:11:22 +00:00
|
|
|
obj = heap->AllocateRaw(kLargeObjectSpaceFillerSize, OLD_SPACE)
|
|
|
|
.ToObjectChecked();
|
|
|
|
heap->CreateFillerObjectAt(obj->address(), size, ClearRecordedSlots::kNo);
|
2008-10-30 09:15:58 +00:00
|
|
|
|
|
|
|
// Map space.
|
2016-05-20 13:30:22 +00:00
|
|
|
heap::SimulateFullSpace(heap->map_space());
|
2018-04-09 19:11:22 +00:00
|
|
|
obj = heap->AllocateRaw(Map::kSize, MAP_SPACE).ToObjectChecked();
|
|
|
|
heap->CreateFillerObjectAt(obj->address(), Map::kSize,
|
|
|
|
ClearRecordedSlots::kNo);
|
2008-10-30 09:15:58 +00:00
|
|
|
|
2018-04-09 19:11:22 +00:00
|
|
|
// Code space.
|
2016-05-20 13:30:22 +00:00
|
|
|
heap::SimulateFullSpace(heap->code_space());
|
2018-04-09 19:11:22 +00:00
|
|
|
size = CcTest::i_isolate()->builtins()->builtin(Builtins::kIllegal)->Size();
|
|
|
|
obj = heap->AllocateRaw(size, CODE_SPACE).ToObjectChecked();
|
|
|
|
heap->CreateFillerObjectAt(obj->address(), size, ClearRecordedSlots::kNo);
|
|
|
|
return CcTest::i_isolate()->factory()->true_value();
|
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());
|
2015-11-09 19:48:08 +00:00
|
|
|
v8::Local<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();
|
2016-06-14 10:08:44 +00:00
|
|
|
CHECK(o->IsTrue(CcTest::i_isolate()));
|
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);
|
2017-08-11 10:04:47 +00:00
|
|
|
info.GetReturnValue().Set(
|
|
|
|
v8::Utils::ToLocal(HeapTester::TestAllocateAfterFailures()));
|
2008-10-30 14:16:02 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 08:59:24 +00:00
|
|
|
void TestSetter(v8::Local<v8::Name> name, v8::Local<v8::Value> value,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Boolean>& info) {
|
2014-04-29 10:59:14 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<AccessorInfo> TestAccessorInfo(
|
|
|
|
Isolate* isolate, PropertyAttributes attributes) {
|
2014-09-10 12:38:12 +00:00
|
|
|
Handle<String> name = isolate->factory()->NewStringFromStaticChars("get");
|
2017-10-26 15:18:09 +00:00
|
|
|
return Accessors::MakeAccessor(isolate, name, &TestGetter, &TestSetter);
|
2014-04-29 10:59:14 +00:00
|
|
|
}
|
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());
|
2015-11-09 19:48:08 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(CcTest::isolate());
|
2008-10-30 14:16:02 +00:00
|
|
|
env->Enter();
|
2018-04-05 11:28:45 +00:00
|
|
|
|
|
|
|
NewFunctionArgs args = NewFunctionArgs::ForBuiltin(
|
|
|
|
factory->function_string(), isolate->sloppy_function_map(),
|
|
|
|
Builtins::kEmptyFunction);
|
|
|
|
Handle<JSFunction> function = factory->NewFunction(args);
|
|
|
|
CHECK(!function->shared()->construct_as_builtin());
|
|
|
|
|
|
|
|
// Force the creation of an initial map.
|
2013-06-04 10:30:05 +00:00
|
|
|
factory->NewJSObject(function);
|
2018-04-05 11:28:45 +00:00
|
|
|
|
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());
|
2017-08-30 04:21:29 +00:00
|
|
|
CHECK_EQ(0, instance_descriptors->number_of_descriptors());
|
2012-07-18 14:00:58 +00:00
|
|
|
|
2015-12-02 16:30:06 +00:00
|
|
|
PropertyAttributes attrs = NONE;
|
2014-04-29 10:59:14 +00:00
|
|
|
Handle<AccessorInfo> foreign = TestAccessorInfo(isolate, attrs);
|
2018-06-19 09:00:37 +00:00
|
|
|
Map::EnsureDescriptorSlack(isolate, map, 1);
|
2012-07-18 14:00:58 +00:00
|
|
|
|
2016-12-21 16:40:00 +00:00
|
|
|
Descriptor d = Descriptor::AccessorConstant(
|
|
|
|
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.
|
2015-11-09 19:48:08 +00:00
|
|
|
CHECK(env->Global()
|
|
|
|
->Set(env, v8::String::NewFromUtf8(CcTest::isolate(), "Foo",
|
|
|
|
v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
v8::Utils::CallableToLocal(function))
|
|
|
|
.FromJust());
|
2008-10-30 14:16:02 +00:00
|
|
|
// Call the accessor through JavaScript.
|
2015-11-09 19:48:08 +00:00
|
|
|
v8::Local<v8::Value> result =
|
|
|
|
v8::Script::Compile(
|
|
|
|
env, v8::String::NewFromUtf8(CcTest::isolate(), "(new Foo).get",
|
|
|
|
v8::NewStringType::kNormal)
|
|
|
|
.ToLocalChecked())
|
|
|
|
.ToLocalChecked()
|
|
|
|
->Run(env)
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK_EQ(true, result->BooleanValue(env).FromJust());
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-11 10:04:47 +00:00
|
|
|
namespace {
|
2009-10-05 11:16:25 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2017-08-11 10:04:47 +00:00
|
|
|
} // namespace
|
2009-10-05 11:16:25 +00:00
|
|
|
|
|
|
|
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();
|
2018-06-11 17:33:32 +00:00
|
|
|
CodeRange code_range(reinterpret_cast<Isolate*>(CcTest::isolate()),
|
|
|
|
code_range_size);
|
2014-06-13 11:06:42 +00:00
|
|
|
size_t current_allocated = 0;
|
|
|
|
size_t total_allocated = 0;
|
2017-08-29 12:40:31 +00:00
|
|
|
std::vector<Block> blocks;
|
|
|
|
blocks.reserve(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
|
2016-09-06 12:58:59 +00:00
|
|
|
// 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.
|
2016-09-06 12:58:59 +00:00
|
|
|
size_t requested = (kMaxRegularHeapObjectSize << (Pseudorandom() % 3)) +
|
|
|
|
Pseudorandom() % 5000 + 1;
|
2017-11-21 01:39:41 +00:00
|
|
|
requested = RoundUp(requested, MemoryAllocator::GetCommitPageSize());
|
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);
|
2018-04-13 22:28:05 +00:00
|
|
|
CHECK_NE(base, kNullAddress);
|
2017-08-29 12:40:31 +00:00
|
|
|
blocks.emplace_back(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.
|
2017-08-29 12:40:31 +00:00
|
|
|
size_t index = Pseudorandom() % blocks.size();
|
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;
|
2017-08-29 12:40:31 +00:00
|
|
|
if (index < blocks.size() - 1) {
|
|
|
|
blocks[index] = blocks.back();
|
2009-10-05 11:16:25 +00:00
|
|
|
}
|
2017-08-29 12:40:31 +00:00
|
|
|
blocks.pop_back();
|
2009-10-05 11:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-11 10:04:47 +00:00
|
|
|
|
|
|
|
} // namespace heap
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|