fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 2 | * Copyright (C) 2015-2018 Apple Inc. All rights reserved. |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 26 | #pragma once |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 27 | |
| 28 | #include "GenericArguments.h" |
| 29 | #include "JSLexicalEnvironment.h" |
| 30 | |
| 31 | namespace JSC { |
| 32 | |
| 33 | // This is an Arguments-class object that we create when you say "arguments" inside a function, |
| 34 | // and one or more of the arguments may be captured in the function's activation. The function |
| 35 | // will copy its formally declared arguments into the activation and then create this object. This |
| 36 | // object will store the overflow arguments, if there are any. This object will use the symbol |
| 37 | // table's ScopedArgumentsTable and the activation, or its overflow storage, to handle all indexed |
| 38 | // lookups. |
utatane.tea@gmail.com | 64e3d77 | 2018-03-07 17:18:49 +0000 | [diff] [blame] | 39 | class ScopedArguments final : public GenericArguments<ScopedArguments> { |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 40 | private: |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 41 | ScopedArguments(VM&, Structure*, WriteBarrier<Unknown>* storage); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 42 | void finishCreation(VM&, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*); |
keith_miller@apple.com | cafcf70 | 2018-06-22 18:26:36 +0000 | [diff] [blame] | 43 | using Base = GenericArguments<ScopedArguments>; |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 44 | |
| 45 | public: |
fpizlo@apple.com | 8b199c8 | 2017-08-12 18:40:07 +0000 | [diff] [blame] | 46 | template<typename CellType> |
fpizlo@apple.com | 5efcc49 | 2017-11-30 04:39:50 +0000 | [diff] [blame] | 47 | static CompleteSubspace* subspaceFor(VM& vm) |
fpizlo@apple.com | 8b199c8 | 2017-08-12 18:40:07 +0000 | [diff] [blame] | 48 | { |
| 49 | RELEASE_ASSERT(!CellType::needsDestruction); |
| 50 | return &vm.jsValueGigacageCellSpace; |
| 51 | } |
| 52 | |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 53 | // Creates an arguments object but leaves it uninitialized. This is dangerous if we GC right |
| 54 | // after allocation. |
| 55 | static ScopedArguments* createUninitialized(VM&, Structure*, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*, unsigned totalLength); |
| 56 | |
| 57 | // Creates an arguments object and initializes everything to the empty value. Use this if you |
| 58 | // cannot guarantee that you'll immediately initialize all of the elements. |
| 59 | static ScopedArguments* create(VM&, Structure*, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*, unsigned totalLength); |
| 60 | |
| 61 | // Creates an arguments object by copying the arguments from the stack. |
| 62 | static ScopedArguments* createByCopying(ExecState*, ScopedArgumentsTable*, JSLexicalEnvironment*); |
| 63 | |
| 64 | // Creates an arguments object by copying the arguments from a well-defined stack location. |
| 65 | static ScopedArguments* createByCopyingFrom(VM&, Structure*, Register* argumentsStart, unsigned totalLength, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*); |
| 66 | |
| 67 | static void visitChildren(JSCell*, SlotVisitor&); |
| 68 | |
| 69 | uint32_t internalLength() const |
| 70 | { |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 71 | return storageHeader().totalLength; |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | uint32_t length(ExecState* exec) const |
| 75 | { |
mark.lam@apple.com | 23e9624 | 2017-09-09 16:21:45 +0000 | [diff] [blame] | 76 | VM& vm = exec->vm(); |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 77 | if (UNLIKELY(storageHeader().overrodeThings)) |
mark.lam@apple.com | 23e9624 | 2017-09-09 16:21:45 +0000 | [diff] [blame] | 78 | return get(exec, vm.propertyNames->length).toUInt32(exec); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 79 | return internalLength(); |
| 80 | } |
| 81 | |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 82 | bool isMappedArgument(uint32_t i) const |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 83 | { |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 84 | WriteBarrier<Unknown>* storage = overflowStorage(); |
| 85 | if (i >= storageHeader(storage).totalLength) |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 86 | return false; |
| 87 | unsigned namedLength = m_table->length(); |
| 88 | if (i < namedLength) |
| 89 | return !!m_table->get(i); |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 90 | return !!storage[i - namedLength].get(); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 91 | } |
benjamin@webkit.org | 75cbd6a | 2015-06-05 05:20:45 +0000 | [diff] [blame] | 92 | |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 93 | bool isMappedArgumentInDFG(uint32_t i) const |
benjamin@webkit.org | 75cbd6a | 2015-06-05 05:20:45 +0000 | [diff] [blame] | 94 | { |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 95 | return isMappedArgument(i); |
benjamin@webkit.org | 75cbd6a | 2015-06-05 05:20:45 +0000 | [diff] [blame] | 96 | } |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 97 | |
| 98 | JSValue getIndexQuickly(uint32_t i) const |
| 99 | { |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 100 | ASSERT_WITH_SECURITY_IMPLICATION(isMappedArgument(i)); |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 101 | WriteBarrier<Unknown>* storage = overflowStorage(); |
| 102 | unsigned totalLength = storageHeader(storage).totalLength; |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 103 | unsigned namedLength = m_table->length(); |
| 104 | if (i < namedLength) |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 105 | return preciseIndexMaskPtr(i, totalLength, &m_scope->variableAt(m_table->get(i)))->get(); |
| 106 | return preciseIndexMaskPtr(i, totalLength, storage + (i - namedLength))->get(); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void setIndexQuickly(VM& vm, uint32_t i, JSValue value) |
| 110 | { |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 111 | ASSERT_WITH_SECURITY_IMPLICATION(isMappedArgument(i)); |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 112 | WriteBarrier<Unknown>* storage = overflowStorage(); |
| 113 | unsigned totalLength = storageHeader(storage).totalLength; |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 114 | unsigned namedLength = m_table->length(); |
| 115 | if (i < namedLength) |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 116 | preciseIndexMaskPtr(i, totalLength, &m_scope->variableAt(m_table->get(i)))->set(vm, m_scope.get(), value); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 117 | else |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 118 | preciseIndexMaskPtr(i, totalLength, storage + (i - namedLength))->set(vm, this, value); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 121 | JSFunction* callee() |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 122 | { |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 123 | return m_callee.get(); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 124 | } |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 125 | |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 126 | bool overrodeThings() const { return storageHeader().overrodeThings; } |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 127 | void overrideThings(VM&); |
| 128 | void overrideThingsIfNecessary(VM&); |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 129 | void unmapArgument(VM&, uint32_t index); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 130 | |
commit-queue@webkit.org | 812d82a | 2016-12-24 21:26:22 +0000 | [diff] [blame] | 131 | void initModifiedArgumentsDescriptorIfNecessary(VM& vm) |
| 132 | { |
| 133 | GenericArguments<ScopedArguments>::initModifiedArgumentsDescriptorIfNecessary(vm, m_table->length()); |
| 134 | } |
| 135 | |
| 136 | void setModifiedArgumentDescriptor(VM& vm, unsigned index) |
| 137 | { |
| 138 | GenericArguments<ScopedArguments>::setModifiedArgumentDescriptor(vm, index, m_table->length()); |
| 139 | } |
| 140 | |
| 141 | bool isModifiedArgumentDescriptor(unsigned index) |
| 142 | { |
| 143 | return GenericArguments<ScopedArguments>::isModifiedArgumentDescriptor(index, m_table->length()); |
| 144 | } |
| 145 | |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 146 | void copyToArguments(ExecState*, VirtualRegister firstElementDest, unsigned offset, unsigned length); |
| 147 | |
| 148 | DECLARE_INFO; |
| 149 | |
| 150 | static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype); |
| 151 | |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 152 | static ptrdiff_t offsetOfStorage() { return OBJECT_OFFSETOF(ScopedArguments, m_storage); } |
| 153 | static ptrdiff_t offsetOfOverrodeThingsInStorage() { return OBJECT_OFFSETOF(StorageHeader, overrodeThings) - sizeof(WriteBarrier<Unknown>); } |
| 154 | static ptrdiff_t offsetOfTotalLengthInStorage() { return OBJECT_OFFSETOF(StorageHeader, totalLength) - sizeof(WriteBarrier<Unknown>); } |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 155 | static ptrdiff_t offsetOfTable() { return OBJECT_OFFSETOF(ScopedArguments, m_table); } |
| 156 | static ptrdiff_t offsetOfScope() { return OBJECT_OFFSETOF(ScopedArguments, m_scope); } |
| 157 | |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 158 | static size_t allocationSize(size_t inlineSize) |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 159 | { |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 160 | RELEASE_ASSERT(!inlineSize); |
| 161 | return sizeof(ScopedArguments); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 164 | static size_t storageSize(Checked<size_t> capacity) |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 165 | { |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 166 | return (sizeof(WriteBarrier<Unknown>) * (capacity + static_cast<size_t>(1))).unsafeGet(); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 167 | } |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 168 | |
| 169 | static size_t storageHeaderSize() { return sizeof(WriteBarrier<Unknown>); } |
| 170 | |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 171 | private: |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 172 | struct StorageHeader { |
| 173 | unsigned totalLength; |
| 174 | bool overrodeThings; // True if length, callee, and caller are fully materialized in the object. |
| 175 | }; |
| 176 | |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 177 | WriteBarrier<Unknown>* overflowStorage() const |
| 178 | { |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 179 | return m_storage.get().unpoisoned(); |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
fpizlo@apple.com | 92acb5a | 2018-03-22 02:15:44 +0000 | [diff] [blame] | 182 | static StorageHeader& storageHeader(WriteBarrier<Unknown>* storage) |
| 183 | { |
| 184 | static_assert(sizeof(StorageHeader) <= sizeof(WriteBarrier<Unknown>), "StorageHeader needs to be no bigger than a JSValue"); |
| 185 | return *bitwise_cast<StorageHeader*>(storage - 1); |
| 186 | } |
| 187 | |
| 188 | StorageHeader& storageHeader() const |
| 189 | { |
| 190 | return storageHeader(overflowStorage()); |
| 191 | } |
| 192 | |
| 193 | template<typename T> |
| 194 | using PoisonedBarrier = PoisonedWriteBarrier<ScopedArgumentsPoison, T>; |
| 195 | |
| 196 | PoisonedBarrier<JSFunction> m_callee; |
| 197 | PoisonedBarrier<ScopedArgumentsTable> m_table; |
| 198 | PoisonedBarrier<JSLexicalEnvironment> m_scope; |
| 199 | |
| 200 | AuxiliaryBarrier<Poisoned<ScopedArgumentsPoison, WriteBarrier<Unknown>*>> m_storage; |
fpizlo@apple.com | da834ae | 2015-03-26 04:28:43 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } // namespace JSC |