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"
|
2018-07-23 11:42:37 +00:00
|
|
|
#include "src/api-inl.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".
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<Map> map(function->initial_map(), isolate);
|
|
|
|
Handle<DescriptorArray> instance_descriptors(map->instance_descriptors(),
|
|
|
|
isolate);
|
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(
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<Name>(Name::cast(foreign->name()), isolate), 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();
|
2018-09-24 11:05:31 +00:00
|
|
|
CHECK_EQ(true, result->BooleanValue(CcTest::isolate()));
|
2008-10-30 14:16:02 +00:00
|
|
|
env->Exit();
|
|
|
|
}
|
2009-10-05 11:16:25 +00:00
|
|
|
|
2017-08-11 10:04:47 +00:00
|
|
|
} // namespace heap
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|