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