erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 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" |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 29 | #include "accessors.h" |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 30 | |
| 31 | #include "cctest.h" |
| 32 | |
| 33 | |
| 34 | using namespace v8::internal; |
| 35 | |
| 36 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 37 | static inline void SimulateFullSpace(PagedSpace* space) { |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 38 | int old_linear_size = static_cast<int>(space->limit() - space->top()); |
| 39 | space->Free(space->top(), old_linear_size); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 40 | space->SetTop(space->limit(), space->limit()); |
| 41 | space->ResetFreeList(); |
| 42 | space->ClearStats(); |
| 43 | } |
| 44 | |
| 45 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 46 | static MaybeObject* AllocateAfterFailures() { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 47 | static int attempts = 0; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 48 | if (++attempts < 3) return Failure::RetryAfterGC(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 49 | Heap* heap = Isolate::Current()->heap(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 50 | |
| 51 | // New space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 52 | NewSpace* new_space = heap->new_space(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 53 | static const int kNewSpaceFillerSize = ByteArray::SizeFor(0); |
| 54 | while (new_space->Available() > kNewSpaceFillerSize) { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 55 | int available_before = static_cast<int>(new_space->Available()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 56 | CHECK(!heap->AllocateByteArray(0)->IsFailure()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 57 | if (available_before == new_space->Available()) { |
| 58 | // It seems that we are avoiding new space allocations when |
| 59 | // allocation is forced, so no need to fill up new space |
| 60 | // in order to make the test harder. |
| 61 | break; |
| 62 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 63 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 64 | CHECK(!heap->AllocateByteArray(100)->IsFailure()); |
| 65 | CHECK(!heap->AllocateFixedArray(100, NOT_TENURED)->IsFailure()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 66 | |
| 67 | // Make sure we can allocate through optimized allocation functions |
| 68 | // for specific kinds. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 69 | CHECK(!heap->AllocateFixedArray(100)->IsFailure()); |
| 70 | CHECK(!heap->AllocateHeapNumber(0.42)->IsFailure()); |
| 71 | CHECK(!heap->AllocateArgumentsObject(Smi::FromInt(87), 10)->IsFailure()); |
| 72 | Object* object = heap->AllocateJSObject( |
| 73 | *Isolate::Current()->object_function())->ToObjectChecked(); |
| 74 | CHECK(!heap->CopyJSObject(JSObject::cast(object))->IsFailure()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 75 | |
| 76 | // Old data space. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 77 | SimulateFullSpace(heap->old_data_space()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 78 | CHECK(!heap->AllocateRawAsciiString(100, TENURED)->IsFailure()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 79 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 80 | // Old pointer space. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 81 | SimulateFullSpace(heap->old_pointer_space()); |
| 82 | CHECK(!heap->AllocateFixedArray(10000, TENURED)->IsFailure()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 83 | |
| 84 | // Large object space. |
| 85 | static const int kLargeObjectSpaceFillerLength = 300000; |
| 86 | static const int kLargeObjectSpaceFillerSize = FixedArray::SizeFor( |
| 87 | kLargeObjectSpaceFillerLength); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 88 | ASSERT(kLargeObjectSpaceFillerSize > heap->old_pointer_space()->AreaSize()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 89 | while (heap->OldGenerationSpaceAvailable() > kLargeObjectSpaceFillerSize) { |
| 90 | CHECK(!heap->AllocateFixedArray(kLargeObjectSpaceFillerLength, TENURED)-> |
| 91 | IsFailure()); |
| 92 | } |
| 93 | CHECK(!heap->AllocateFixedArray(kLargeObjectSpaceFillerLength, TENURED)-> |
| 94 | IsFailure()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 95 | |
| 96 | // Map space. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 97 | SimulateFullSpace(heap->map_space()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 98 | int instance_size = JSObject::kHeaderSize; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 99 | CHECK(!heap->AllocateMap(JS_OBJECT_TYPE, instance_size)->IsFailure()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 100 | |
| 101 | // Test that we can allocate in old pointer space and code space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 102 | CHECK(!heap->AllocateFixedArray(100, TENURED)->IsFailure()); |
| 103 | CHECK(!heap->CopyCode(Isolate::Current()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 104 | Builtins::kIllegal))->IsFailure()); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 105 | |
| 106 | // Return success. |
| 107 | return Smi::FromInt(42); |
| 108 | } |
| 109 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 110 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 111 | static Handle<Object> Test() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 112 | CALL_HEAP_FUNCTION(ISOLATE, AllocateAfterFailures(), Object); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 116 | TEST(StressHandles) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 117 | v8::Persistent<v8::Context> env = v8::Context::New(); |
| 118 | v8::HandleScope scope; |
| 119 | env->Enter(); |
| 120 | Handle<Object> o = Test(); |
| 121 | CHECK(o->IsSmi() && Smi::cast(*o)->value() == 42); |
| 122 | env->Exit(); |
| 123 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 124 | |
| 125 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 126 | static MaybeObject* TestAccessorGet(Object* object, void*) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 127 | return AllocateAfterFailures(); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | const AccessorDescriptor kDescriptor = { |
| 132 | TestAccessorGet, |
| 133 | 0, |
| 134 | 0 |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | TEST(StressJS) { |
| 139 | v8::Persistent<v8::Context> env = v8::Context::New(); |
| 140 | v8::HandleScope scope; |
| 141 | env->Enter(); |
| 142 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 143 | FACTORY->NewFunction(FACTORY->function_symbol(), FACTORY->null_value()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 144 | // Force the creation of an initial map and set the code to |
| 145 | // something empty. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 146 | FACTORY->NewJSObject(function); |
| 147 | function->ReplaceCode(Isolate::Current()->builtins()->builtin( |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 148 | Builtins::kEmptyFunction)); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 149 | // Patch the map to have an accessor for "get". |
| 150 | Handle<Map> map(function->initial_map()); |
| 151 | Handle<DescriptorArray> instance_descriptors(map->instance_descriptors()); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 152 | Handle<Foreign> foreign = FACTORY->NewForeign(&kDescriptor); |
| 153 | instance_descriptors = FACTORY->CopyAppendForeignDescriptor( |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 154 | instance_descriptors, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 155 | FACTORY->NewStringFromAscii(Vector<const char>("get", 3)), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 156 | foreign, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 157 | static_cast<PropertyAttributes>(0)); |
| 158 | map->set_instance_descriptors(*instance_descriptors); |
| 159 | // Add the Foo constructor the global object. |
| 160 | env->Global()->Set(v8::String::New("Foo"), v8::Utils::ToLocal(function)); |
| 161 | // Call the accessor through JavaScript. |
| 162 | v8::Handle<v8::Value> result = |
| 163 | v8::Script::Compile(v8::String::New("(new Foo).get"))->Run(); |
| 164 | CHECK_EQ(42, result->Int32Value()); |
| 165 | env->Exit(); |
| 166 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 167 | |
| 168 | |
| 169 | // CodeRange test. |
| 170 | // Tests memory management in a CodeRange by allocating and freeing blocks, |
| 171 | // using a pseudorandom generator to choose block sizes geometrically |
| 172 | // distributed between 2 * Page::kPageSize and 2^5 + 1 * Page::kPageSize. |
| 173 | // Ensure that the freed chunks are collected and reused by allocating (in |
| 174 | // total) more than the size of the CodeRange. |
| 175 | |
| 176 | // This pseudorandom generator does not need to be particularly good. |
| 177 | // Use the lower half of the V8::Random() generator. |
| 178 | unsigned int Pseudorandom() { |
| 179 | static uint32_t lo = 2345; |
| 180 | lo = 18273 * (lo & 0xFFFF) + (lo >> 16); // Provably not 0. |
| 181 | return lo & 0xFFFF; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | // Plain old data class. Represents a block of allocated memory. |
| 186 | class Block { |
| 187 | public: |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 188 | Block(Address base_arg, int size_arg) |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 189 | : base(base_arg), size(size_arg) {} |
| 190 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 191 | Address base; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 192 | int size; |
| 193 | }; |
| 194 | |
| 195 | |
| 196 | TEST(CodeRange) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 197 | const int code_range_size = 32*MB; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 198 | OS::SetUp(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 199 | Isolate::Current()->InitializeLoggingAndCounters(); |
| 200 | CodeRange* code_range = new CodeRange(Isolate::Current()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 201 | code_range->SetUp(code_range_size); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 202 | int current_allocated = 0; |
| 203 | int total_allocated = 0; |
| 204 | List<Block> blocks(1000); |
| 205 | |
| 206 | while (total_allocated < 5 * code_range_size) { |
| 207 | if (current_allocated < code_range_size / 10) { |
| 208 | // Allocate a block. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 209 | // Geometrically distributed sizes, greater than |
| 210 | // Page::kMaxNonCodeHeapObjectSize (which is greater than code page area). |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 211 | // TODO(gc): instead of using 3 use some contant based on code_range_size |
| 212 | // kMaxHeapObjectSize. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 213 | size_t requested = |
| 214 | (Page::kMaxNonCodeHeapObjectSize << (Pseudorandom() % 3)) + |
| 215 | Pseudorandom() % 5000 + 1; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 216 | size_t allocated = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 217 | Address base = code_range->AllocateRawMemory(requested, &allocated); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 218 | CHECK(base != NULL); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 219 | blocks.Add(Block(base, static_cast<int>(allocated))); |
| 220 | current_allocated += static_cast<int>(allocated); |
| 221 | total_allocated += static_cast<int>(allocated); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 222 | } else { |
| 223 | // Free a block. |
| 224 | int index = Pseudorandom() % blocks.length(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 225 | code_range->FreeRawMemory(blocks[index].base, blocks[index].size); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 226 | current_allocated -= blocks[index].size; |
| 227 | if (index < blocks.length() - 1) { |
| 228 | blocks[index] = blocks.RemoveLast(); |
| 229 | } else { |
| 230 | blocks.RemoveLast(); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 235 | code_range->TearDown(); |
| 236 | delete code_range; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 237 | } |