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