blob: b2e80ff4d926a128a2939f9b598deb6c2e3d5893 [file] [log] [blame]
fpizlo@apple.comda834ae2015-03-26 04:28:43 +00001/*
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +00002 * Copyright (C) 2015-2018 Apple Inc. All rights reserved.
fpizlo@apple.comda834ae2015-03-26 04:28:43 +00003 *
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.com22104f52016-09-28 17:08:17 +000026#pragma once
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000027
28#include "GenericArguments.h"
29#include "JSLexicalEnvironment.h"
30
31namespace 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.com64e3d772018-03-07 17:18:49 +000039class ScopedArguments final : public GenericArguments<ScopedArguments> {
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000040private:
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +000041 ScopedArguments(VM&, Structure*, WriteBarrier<Unknown>* storage);
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000042 void finishCreation(VM&, JSFunction* callee, ScopedArgumentsTable*, JSLexicalEnvironment*);
keith_miller@apple.comcafcf702018-06-22 18:26:36 +000043 using Base = GenericArguments<ScopedArguments>;
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000044
45public:
fpizlo@apple.com8b199c82017-08-12 18:40:07 +000046 template<typename CellType>
fpizlo@apple.com5efcc492017-11-30 04:39:50 +000047 static CompleteSubspace* subspaceFor(VM& vm)
fpizlo@apple.com8b199c82017-08-12 18:40:07 +000048 {
49 RELEASE_ASSERT(!CellType::needsDestruction);
50 return &vm.jsValueGigacageCellSpace;
51 }
52
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000053 // 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.com92acb5a2018-03-22 02:15:44 +000071 return storageHeader().totalLength;
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000072 }
73
74 uint32_t length(ExecState* exec) const
75 {
mark.lam@apple.com23e96242017-09-09 16:21:45 +000076 VM& vm = exec->vm();
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +000077 if (UNLIKELY(storageHeader().overrodeThings))
mark.lam@apple.com23e96242017-09-09 16:21:45 +000078 return get(exec, vm.propertyNames->length).toUInt32(exec);
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000079 return internalLength();
80 }
81
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +000082 bool isMappedArgument(uint32_t i) const
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000083 {
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +000084 WriteBarrier<Unknown>* storage = overflowStorage();
85 if (i >= storageHeader(storage).totalLength)
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000086 return false;
87 unsigned namedLength = m_table->length();
88 if (i < namedLength)
89 return !!m_table->get(i);
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +000090 return !!storage[i - namedLength].get();
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000091 }
benjamin@webkit.org75cbd6a2015-06-05 05:20:45 +000092
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +000093 bool isMappedArgumentInDFG(uint32_t i) const
benjamin@webkit.org75cbd6a2015-06-05 05:20:45 +000094 {
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +000095 return isMappedArgument(i);
benjamin@webkit.org75cbd6a2015-06-05 05:20:45 +000096 }
fpizlo@apple.comda834ae2015-03-26 04:28:43 +000097
98 JSValue getIndexQuickly(uint32_t i) const
99 {
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +0000100 ASSERT_WITH_SECURITY_IMPLICATION(isMappedArgument(i));
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000101 WriteBarrier<Unknown>* storage = overflowStorage();
102 unsigned totalLength = storageHeader(storage).totalLength;
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000103 unsigned namedLength = m_table->length();
104 if (i < namedLength)
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000105 return preciseIndexMaskPtr(i, totalLength, &m_scope->variableAt(m_table->get(i)))->get();
106 return preciseIndexMaskPtr(i, totalLength, storage + (i - namedLength))->get();
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000107 }
108
109 void setIndexQuickly(VM& vm, uint32_t i, JSValue value)
110 {
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +0000111 ASSERT_WITH_SECURITY_IMPLICATION(isMappedArgument(i));
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000112 WriteBarrier<Unknown>* storage = overflowStorage();
113 unsigned totalLength = storageHeader(storage).totalLength;
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000114 unsigned namedLength = m_table->length();
115 if (i < namedLength)
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000116 preciseIndexMaskPtr(i, totalLength, &m_scope->variableAt(m_table->get(i)))->set(vm, m_scope.get(), value);
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000117 else
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000118 preciseIndexMaskPtr(i, totalLength, storage + (i - namedLength))->set(vm, this, value);
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000119 }
120
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000121 JSFunction* callee()
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000122 {
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000123 return m_callee.get();
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000124 }
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +0000125
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000126 bool overrodeThings() const { return storageHeader().overrodeThings; }
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000127 void overrideThings(VM&);
128 void overrideThingsIfNecessary(VM&);
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +0000129 void unmapArgument(VM&, uint32_t index);
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000130
commit-queue@webkit.org812d82a2016-12-24 21:26:22 +0000131 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.comda834ae2015-03-26 04:28:43 +0000146 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.com92acb5a2018-03-22 02:15:44 +0000152 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.comda834ae2015-03-26 04:28:43 +0000155 static ptrdiff_t offsetOfTable() { return OBJECT_OFFSETOF(ScopedArguments, m_table); }
156 static ptrdiff_t offsetOfScope() { return OBJECT_OFFSETOF(ScopedArguments, m_scope); }
157
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000158 static size_t allocationSize(size_t inlineSize)
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000159 {
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000160 RELEASE_ASSERT(!inlineSize);
161 return sizeof(ScopedArguments);
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000162 }
163
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000164 static size_t storageSize(Checked<size_t> capacity)
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000165 {
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000166 return (sizeof(WriteBarrier<Unknown>) * (capacity + static_cast<size_t>(1))).unsafeGet();
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000167 }
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000168
169 static size_t storageHeaderSize() { return sizeof(WriteBarrier<Unknown>); }
170
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000171private:
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000172 struct StorageHeader {
173 unsigned totalLength;
174 bool overrodeThings; // True if length, callee, and caller are fully materialized in the object.
175 };
176
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000177 WriteBarrier<Unknown>* overflowStorage() const
178 {
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000179 return m_storage.get().unpoisoned();
fpizlo@apple.comda834ae2015-03-26 04:28:43 +0000180 }
181
fpizlo@apple.com92acb5a2018-03-22 02:15:44 +0000182 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.comda834ae2015-03-26 04:28:43 +0000201};
202
203} // namespace JSC