1 // Copyright 2012 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 "include/v8-function.h"
29 #include "src/api/api-inl.h"
30 #include "src/builtins/accessors.h"
31 #include "src/heap/heap-inl.h"
32 #include "src/init/v8.h"
33 #include "src/objects/api-callbacks.h"
34 #include "src/objects/objects-inl.h"
35 #include "src/objects/property.h"
36 #include "test/cctest/cctest.h"
37 #include "test/cctest/heap/heap-tester.h"
38 #include "test/cctest/heap/heap-utils.h"
39 
40 namespace v8 {
41 namespace internal {
42 namespace heap {
43 
TestAllocateAfterFailures()44 Handle<Object> HeapTester::TestAllocateAfterFailures() {
45   // Similar to what the factory's retrying logic does in the last-resort case,
46   // we wrap the allocator function in an AlwaysAllocateScope.  Test that
47   // all allocations succeed immediately without any retry.
48   CcTest::CollectAllAvailableGarbage();
49   Heap* heap = CcTest::heap();
50   AlwaysAllocateScopeForTesting scope(heap);
51   int size = FixedArray::SizeFor(100);
52   // Young generation.
53   HeapObject obj =
54       heap->AllocateRaw(size, AllocationType::kYoung).ToObjectChecked();
55   // In order to pass heap verification on Isolate teardown, mark the
56   // allocated area as a filler.
57   heap->CreateFillerObjectAt(obj.address(), size, ClearRecordedSlots::kNo);
58 
59   // Old generation.
60   heap::SimulateFullSpace(heap->old_space());
61   obj = heap->AllocateRaw(size, AllocationType::kOld).ToObjectChecked();
62   heap->CreateFillerObjectAt(obj.address(), size, ClearRecordedSlots::kNo);
63 
64   // Large object space.
65   static const size_t kLargeObjectSpaceFillerLength =
66       3 * (Page::kPageSize / 10);
67   static const size_t kLargeObjectSpaceFillerSize =
68       FixedArray::SizeFor(kLargeObjectSpaceFillerLength);
69   CHECK_GT(kLargeObjectSpaceFillerSize,
70            static_cast<size_t>(heap->old_space()->AreaSize()));
71   while (heap->OldGenerationSpaceAvailable() > kLargeObjectSpaceFillerSize) {
72     obj = heap->AllocateRaw(kLargeObjectSpaceFillerSize, AllocationType::kOld)
73               .ToObjectChecked();
74     heap->CreateFillerObjectAt(obj.address(), size, ClearRecordedSlots::kNo);
75   }
76   obj = heap->AllocateRaw(kLargeObjectSpaceFillerSize, AllocationType::kOld)
77             .ToObjectChecked();
78   heap->CreateFillerObjectAt(obj.address(), size, ClearRecordedSlots::kNo);
79 
80   // Map space.
81   heap::SimulateFullSpace(heap->map_space());
82   obj = heap->AllocateRaw(Map::kSize, AllocationType::kMap).ToObjectChecked();
83   heap->CreateFillerObjectAt(obj.address(), Map::kSize,
84                              ClearRecordedSlots::kNo);
85 
86   // Code space.
87   heap::SimulateFullSpace(heap->code_space());
88   size = CcTest::i_isolate()->builtins()->code(Builtin::kIllegal).Size();
89   obj =
90       heap->AllocateRaw(size, AllocationType::kCode, AllocationOrigin::kRuntime)
91           .ToObjectChecked();
92   heap->CreateFillerObjectAt(obj.address(), size, ClearRecordedSlots::kNo);
93   return CcTest::i_isolate()->factory()->true_value();
94 }
95 
96 
HEAP_TEST(StressHandles)97 HEAP_TEST(StressHandles) {
98   // For TestAllocateAfterFailures.
99   FLAG_stress_concurrent_allocation = false;
100   v8::HandleScope scope(CcTest::isolate());
101   v8::Local<v8::Context> env = v8::Context::New(CcTest::isolate());
102   env->Enter();
103   Handle<Object> o = TestAllocateAfterFailures();
104   CHECK(o->IsTrue(CcTest::i_isolate()));
105   env->Exit();
106 }
107 
108 
TestGetter(v8::Local<v8::Name> name,const v8::PropertyCallbackInfo<v8::Value> & info)109 void TestGetter(
110     v8::Local<v8::Name> name,
111     const v8::PropertyCallbackInfo<v8::Value>& info) {
112   i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
113   HandleScope scope(isolate);
114   info.GetReturnValue().Set(
115       v8::Utils::ToLocal(HeapTester::TestAllocateAfterFailures()));
116 }
117 
TestSetter(v8::Local<v8::Name> name,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Boolean> & info)118 void TestSetter(v8::Local<v8::Name> name, v8::Local<v8::Value> value,
119                 const v8::PropertyCallbackInfo<v8::Boolean>& info) {
120   UNREACHABLE();
121 }
122 
123 
TestAccessorInfo(Isolate * isolate,PropertyAttributes attributes)124 Handle<AccessorInfo> TestAccessorInfo(
125       Isolate* isolate, PropertyAttributes attributes) {
126   Handle<String> name = isolate->factory()->NewStringFromStaticChars("get");
127   return Accessors::MakeAccessor(isolate, name, &TestGetter, &TestSetter);
128 }
129 
130 
TEST(StressJS)131 TEST(StressJS) {
132   // For TestAllocateAfterFailures in TestGetter.
133   FLAG_stress_concurrent_allocation = false;
134   Isolate* isolate = CcTest::i_isolate();
135   Factory* factory = isolate->factory();
136   v8::HandleScope scope(CcTest::isolate());
137   v8::Local<v8::Context> env = v8::Context::New(CcTest::isolate());
138   env->Enter();
139 
140   Handle<NativeContext> context(isolate->native_context());
141   Handle<SharedFunctionInfo> info = factory->NewSharedFunctionInfoForBuiltin(
142       factory->function_string(), Builtin::kEmptyFunction);
143   info->set_language_mode(LanguageMode::kStrict);
144   Handle<JSFunction> function =
145       Factory::JSFunctionBuilder{isolate, info, context}.Build();
146   CHECK(!function->shared().construct_as_builtin());
147 
148   // Force the creation of an initial map.
149   factory->NewJSObject(function);
150 
151   // Patch the map to have an accessor for "get".
152   Handle<Map> map(function->initial_map(), isolate);
153   Handle<DescriptorArray> instance_descriptors(
154       map->instance_descriptors(isolate), isolate);
155   CHECK_EQ(0, instance_descriptors->number_of_descriptors());
156 
157   PropertyAttributes attrs = NONE;
158   Handle<AccessorInfo> foreign = TestAccessorInfo(isolate, attrs);
159   Map::EnsureDescriptorSlack(isolate, map, 1);
160 
161   Descriptor d = Descriptor::AccessorConstant(
162       Handle<Name>(Name::cast(foreign->name()), isolate), foreign, attrs);
163   map->AppendDescriptor(isolate, &d);
164 
165   // Add the Foo constructor the global object.
166   CHECK(env->Global()
167             ->Set(env, v8::String::NewFromUtf8Literal(CcTest::isolate(), "Foo"),
168                   v8::Utils::CallableToLocal(function))
169             .FromJust());
170   // Call the accessor through JavaScript.
171   v8::Local<v8::Value> result =
172       v8::Script::Compile(env, v8::String::NewFromUtf8Literal(CcTest::isolate(),
173                                                               "(new Foo).get"))
174           .ToLocalChecked()
175           ->Run(env)
176           .ToLocalChecked();
177   CHECK_EQ(true, result->BooleanValue(CcTest::isolate()));
178   env->Exit();
179 }
180 
181 }  // namespace heap
182 }  // namespace internal
183 }  // namespace v8
184