f4d6ebe7375f7ae7ab3cde0a20be3027d7ed80b0
[WebKit-https.git] / Source / JavaScriptCore / runtime / CommonSlowPaths.h
1 /*
2  * Copyright (C) 2011-2017 Apple Inc. All rights reserved.
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
26 #pragma once
27
28 #include "CodeBlock.h"
29 #include "CodeSpecializationKind.h"
30 #include "ExceptionHelpers.h"
31 #include "FunctionCodeBlock.h"
32 #include "SlowPathReturnType.h"
33 #include "StackAlignment.h"
34 #include "VMInlines.h"
35 #include <wtf/StdLibExtras.h>
36
37 namespace JSC {
38
39 // The purpose of this namespace is to include slow paths that are shared
40 // between the interpreter and baseline JIT. They are written to be agnostic
41 // with respect to the slow-path calling convention, but they do rely on the
42 // JS code being executed more-or-less directly from bytecode (so the call
43 // frame layout is unmodified, making it potentially awkward to use these
44 // from any optimizing JIT, like the DFG).
45
46 namespace CommonSlowPaths {
47
48 struct ArityCheckData {
49     unsigned paddedStackSpace;
50     void* thunkToCall;
51 };
52
53 ALWAYS_INLINE int numberOfExtraSlots(int argumentCountIncludingThis)
54 {
55     int frameSize = argumentCountIncludingThis + CallFrame::headerSizeInRegisters;
56     int alignedFrameSize = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), frameSize);
57     return alignedFrameSize - frameSize;
58 }
59
60 ALWAYS_INLINE int numberOfStackPaddingSlots(CodeBlock* codeBlock, int argumentCountIncludingThis)
61 {
62     if (argumentCountIncludingThis >= codeBlock->numParameters())
63         return 0;
64     int alignedFrameSize = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), argumentCountIncludingThis + CallFrame::headerSizeInRegisters);
65     int alignedFrameSizeForParameters = WTF::roundUpToMultipleOf(stackAlignmentRegisters(), codeBlock->numParameters() + CallFrame::headerSizeInRegisters);
66     return alignedFrameSizeForParameters - alignedFrameSize;
67 }
68
69 ALWAYS_INLINE int numberOfStackPaddingSlotsWithExtraSlots(CodeBlock* codeBlock, int argumentCountIncludingThis)
70 {
71     if (argumentCountIncludingThis >= codeBlock->numParameters())
72         return 0;
73     return numberOfStackPaddingSlots(codeBlock, argumentCountIncludingThis) + numberOfExtraSlots(argumentCountIncludingThis);
74 }
75
76 ALWAYS_INLINE int arityCheckFor(ExecState* exec, VM& vm, CodeSpecializationKind kind)
77 {
78     JSFunction* callee = jsCast<JSFunction*>(exec->jsCallee());
79     ASSERT(!callee->isHostFunction());
80     CodeBlock* newCodeBlock = callee->jsExecutable()->codeBlockFor(kind);
81     ASSERT(exec->argumentCountIncludingThis() < static_cast<unsigned>(newCodeBlock->numParameters()));
82     int padding = numberOfStackPaddingSlotsWithExtraSlots(newCodeBlock, exec->argumentCountIncludingThis());
83     
84     Register* newStack = exec->registers() - WTF::roundUpToMultipleOf(stackAlignmentRegisters(), padding);
85
86     if (UNLIKELY(!vm.ensureStackCapacityFor(newStack)))
87         return -1;
88     return padding;
89 }
90
91 inline bool opIn(ExecState* exec, JSValue baseVal, JSValue propName, ArrayProfile* arrayProfile = nullptr)
92 {
93     VM& vm = exec->vm();
94     auto scope = DECLARE_THROW_SCOPE(vm);
95     if (!baseVal.isObject()) {
96         throwException(exec, scope, createInvalidInParameterError(exec, baseVal));
97         return false;
98     }
99
100     JSObject* baseObj = asObject(baseVal);
101     if (arrayProfile)
102         arrayProfile->observeStructure(baseObj->structure(vm));
103
104     uint32_t i;
105     if (propName.getUInt32(i)) {
106         scope.release();
107         return baseObj->hasProperty(exec, i);
108     }
109
110     auto property = propName.toPropertyKey(exec);
111     RETURN_IF_EXCEPTION(scope, false);
112     scope.release();
113     return baseObj->hasProperty(exec, property);
114 }
115
116 inline void tryCachePutToScopeGlobal(
117     ExecState* exec, CodeBlock* codeBlock, Instruction* pc, JSObject* scope,
118     GetPutInfo getPutInfo, PutPropertySlot& slot, const Identifier& ident)
119 {
120     // Covers implicit globals. Since they don't exist until they first execute, we didn't know how to cache them at compile time.
121     ResolveType resolveType = getPutInfo.resolveType();
122     if (resolveType != GlobalProperty && resolveType != GlobalPropertyWithVarInjectionChecks 
123         && resolveType != UnresolvedProperty && resolveType != UnresolvedPropertyWithVarInjectionChecks)
124         return;
125
126     if (resolveType == UnresolvedProperty || resolveType == UnresolvedPropertyWithVarInjectionChecks) {
127         if (scope->isGlobalObject()) {
128             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalProperty : GlobalPropertyWithVarInjectionChecks;
129             resolveType = newResolveType;
130             getPutInfo = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode());
131             ConcurrentJSLocker locker(codeBlock->m_lock);
132             pc[4].u.operand = getPutInfo.operand();
133         } else if (scope->isGlobalLexicalEnvironment()) {
134             JSGlobalLexicalEnvironment* globalLexicalEnvironment = jsCast<JSGlobalLexicalEnvironment*>(scope);
135             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalLexicalVar : GlobalLexicalVarWithVarInjectionChecks;
136             pc[4].u.operand = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode()).operand();
137             SymbolTableEntry entry = globalLexicalEnvironment->symbolTable()->get(ident.impl());
138             ASSERT(!entry.isNull());
139             ConcurrentJSLocker locker(codeBlock->m_lock);
140             pc[5].u.watchpointSet = entry.watchpointSet();
141             pc[6].u.pointer = static_cast<void*>(globalLexicalEnvironment->variableAt(entry.scopeOffset()).slot());
142         }
143     }
144     
145     if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) {
146         if (!slot.isCacheablePut()
147             || slot.base() != scope
148             || !scope->structure()->propertyAccessesAreCacheable())
149             return;
150         
151         if (slot.type() == PutPropertySlot::NewProperty) {
152             // Don't cache if we've done a transition. We want to detect the first replace so that we
153             // can invalidate the watchpoint.
154             return;
155         }
156         
157         VM& vm = exec->vm();
158         scope->structure()->didCachePropertyReplacement(vm, slot.cachedOffset());
159
160         ConcurrentJSLocker locker(codeBlock->m_lock);
161         pc[5].u.structure.set(vm, codeBlock, scope->structure());
162         pc[6].u.operand = slot.cachedOffset();
163     }
164 }
165
166 inline void tryCacheGetFromScopeGlobal(
167     ExecState* exec, VM& vm, Instruction* pc, JSObject* scope, PropertySlot& slot, const Identifier& ident)
168 {
169     GetPutInfo getPutInfo(pc[4].u.operand);
170     ResolveType resolveType = getPutInfo.resolveType();
171
172     if (resolveType == UnresolvedProperty || resolveType == UnresolvedPropertyWithVarInjectionChecks) {
173         if (scope->isGlobalObject()) {
174             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalProperty : GlobalPropertyWithVarInjectionChecks;
175             resolveType = newResolveType; // Allow below caching mechanism to kick in.
176             ConcurrentJSLocker locker(exec->codeBlock()->m_lock);
177             pc[4].u.operand = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode()).operand();
178         } else if (scope->isGlobalLexicalEnvironment()) {
179             JSGlobalLexicalEnvironment* globalLexicalEnvironment = jsCast<JSGlobalLexicalEnvironment*>(scope);
180             ResolveType newResolveType = resolveType == UnresolvedProperty ? GlobalLexicalVar : GlobalLexicalVarWithVarInjectionChecks;
181             SymbolTableEntry entry = globalLexicalEnvironment->symbolTable()->get(ident.impl());
182             ASSERT(!entry.isNull());
183             ConcurrentJSLocker locker(exec->codeBlock()->m_lock);
184             pc[4].u.operand = GetPutInfo(getPutInfo.resolveMode(), newResolveType, getPutInfo.initializationMode()).operand();
185             pc[5].u.watchpointSet = entry.watchpointSet();
186             pc[6].u.pointer = static_cast<void*>(globalLexicalEnvironment->variableAt(entry.scopeOffset()).slot());
187         }
188     }
189
190     // Covers implicit globals. Since they don't exist until they first execute, we didn't know how to cache them at compile time.
191     if (slot.isCacheableValue() && slot.slotBase() == scope && scope->structure()->propertyAccessesAreCacheable()) {
192         if (resolveType == GlobalProperty || resolveType == GlobalPropertyWithVarInjectionChecks) {
193             CodeBlock* codeBlock = exec->codeBlock();
194             Structure* structure = scope->structure(vm);
195             {
196                 ConcurrentJSLocker locker(codeBlock->m_lock);
197                 pc[5].u.structure.set(vm, codeBlock, structure);
198                 pc[6].u.operand = slot.cachedOffset();
199             }
200             structure->startWatchingPropertyForReplacements(vm, slot.cachedOffset());
201         }
202     }
203 }
204
205 } // namespace CommonSlowPaths
206
207 class ExecState;
208 struct Instruction;
209
210 #define SLOW_PATH
211     
212 #define SLOW_PATH_DECL(name) \
213 extern "C" SlowPathReturnType SLOW_PATH name(ExecState* exec, Instruction* pc)
214     
215 #define SLOW_PATH_HIDDEN_DECL(name) \
216 SLOW_PATH_DECL(name) WTF_INTERNAL
217     
218 SLOW_PATH_HIDDEN_DECL(slow_path_call_arityCheck);
219 SLOW_PATH_HIDDEN_DECL(slow_path_construct_arityCheck);
220 SLOW_PATH_HIDDEN_DECL(slow_path_create_direct_arguments);
221 SLOW_PATH_HIDDEN_DECL(slow_path_create_scoped_arguments);
222 SLOW_PATH_HIDDEN_DECL(slow_path_create_cloned_arguments);
223 SLOW_PATH_HIDDEN_DECL(slow_path_create_this);
224 SLOW_PATH_HIDDEN_DECL(slow_path_enter);
225 SLOW_PATH_HIDDEN_DECL(slow_path_get_callee);
226 SLOW_PATH_HIDDEN_DECL(slow_path_to_this);
227 SLOW_PATH_HIDDEN_DECL(slow_path_throw_tdz_error);
228 SLOW_PATH_HIDDEN_DECL(slow_path_throw_strict_mode_readonly_property_write_error);
229 SLOW_PATH_HIDDEN_DECL(slow_path_not);
230 SLOW_PATH_HIDDEN_DECL(slow_path_eq);
231 SLOW_PATH_HIDDEN_DECL(slow_path_neq);
232 SLOW_PATH_HIDDEN_DECL(slow_path_stricteq);
233 SLOW_PATH_HIDDEN_DECL(slow_path_nstricteq);
234 SLOW_PATH_HIDDEN_DECL(slow_path_less);
235 SLOW_PATH_HIDDEN_DECL(slow_path_lesseq);
236 SLOW_PATH_HIDDEN_DECL(slow_path_greater);
237 SLOW_PATH_HIDDEN_DECL(slow_path_greatereq);
238 SLOW_PATH_HIDDEN_DECL(slow_path_inc);
239 SLOW_PATH_HIDDEN_DECL(slow_path_dec);
240 SLOW_PATH_HIDDEN_DECL(slow_path_to_number);
241 SLOW_PATH_HIDDEN_DECL(slow_path_to_string);
242 SLOW_PATH_HIDDEN_DECL(slow_path_negate);
243 SLOW_PATH_HIDDEN_DECL(slow_path_add);
244 SLOW_PATH_HIDDEN_DECL(slow_path_mul);
245 SLOW_PATH_HIDDEN_DECL(slow_path_sub);
246 SLOW_PATH_HIDDEN_DECL(slow_path_div);
247 SLOW_PATH_HIDDEN_DECL(slow_path_mod);
248 SLOW_PATH_HIDDEN_DECL(slow_path_pow);
249 SLOW_PATH_HIDDEN_DECL(slow_path_lshift);
250 SLOW_PATH_HIDDEN_DECL(slow_path_rshift);
251 SLOW_PATH_HIDDEN_DECL(slow_path_urshift);
252 SLOW_PATH_HIDDEN_DECL(slow_path_unsigned);
253 SLOW_PATH_HIDDEN_DECL(slow_path_bitand);
254 SLOW_PATH_HIDDEN_DECL(slow_path_bitor);
255 SLOW_PATH_HIDDEN_DECL(slow_path_bitxor);
256 SLOW_PATH_HIDDEN_DECL(slow_path_typeof);
257 SLOW_PATH_HIDDEN_DECL(slow_path_is_object);
258 SLOW_PATH_HIDDEN_DECL(slow_path_is_object_or_null);
259 SLOW_PATH_HIDDEN_DECL(slow_path_is_function);
260 SLOW_PATH_HIDDEN_DECL(slow_path_in);
261 SLOW_PATH_HIDDEN_DECL(slow_path_del_by_val);
262 SLOW_PATH_HIDDEN_DECL(slow_path_strcat);
263 SLOW_PATH_HIDDEN_DECL(slow_path_to_primitive);
264 SLOW_PATH_HIDDEN_DECL(slow_path_get_enumerable_length);
265 SLOW_PATH_HIDDEN_DECL(slow_path_has_generic_property);
266 SLOW_PATH_HIDDEN_DECL(slow_path_has_structure_property);
267 SLOW_PATH_HIDDEN_DECL(slow_path_has_indexed_property);
268 SLOW_PATH_HIDDEN_DECL(slow_path_get_direct_pname);
269 SLOW_PATH_HIDDEN_DECL(slow_path_get_property_enumerator);
270 SLOW_PATH_HIDDEN_DECL(slow_path_next_structure_enumerator_pname);
271 SLOW_PATH_HIDDEN_DECL(slow_path_next_generic_enumerator_pname);
272 SLOW_PATH_HIDDEN_DECL(slow_path_to_index_string);
273 SLOW_PATH_HIDDEN_DECL(slow_path_profile_type_clear_log);
274 SLOW_PATH_HIDDEN_DECL(slow_path_assert);
275 SLOW_PATH_HIDDEN_DECL(slow_path_unreachable);
276 SLOW_PATH_HIDDEN_DECL(slow_path_create_lexical_environment);
277 SLOW_PATH_HIDDEN_DECL(slow_path_push_with_scope);
278 SLOW_PATH_HIDDEN_DECL(slow_path_resolve_scope);
279 SLOW_PATH_HIDDEN_DECL(slow_path_is_var_scope);
280 SLOW_PATH_HIDDEN_DECL(slow_path_resolve_scope_for_hoisting_func_decl_in_eval);
281 SLOW_PATH_HIDDEN_DECL(slow_path_create_rest);
282 SLOW_PATH_HIDDEN_DECL(slow_path_get_by_id_with_this);
283 SLOW_PATH_HIDDEN_DECL(slow_path_get_by_val_with_this);
284 SLOW_PATH_HIDDEN_DECL(slow_path_put_by_id_with_this);
285 SLOW_PATH_HIDDEN_DECL(slow_path_put_by_val_with_this);
286 SLOW_PATH_HIDDEN_DECL(slow_path_define_data_property);
287 SLOW_PATH_HIDDEN_DECL(slow_path_define_accessor_property);
288 SLOW_PATH_HIDDEN_DECL(slow_path_throw_static_error);
289 SLOW_PATH_HIDDEN_DECL(slow_path_new_array_with_spread);
290 SLOW_PATH_HIDDEN_DECL(slow_path_spread);
291
292 } // namespace JSC