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