Lazily decode cached bytecode
[WebKit-https.git] / Source / JavaScriptCore / bytecode / UnlinkedFunctionExecutable.cpp
1 /*
2  * Copyright (C) 2012-2018 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 #include "config.h"
27 #include "UnlinkedFunctionExecutable.h"
28
29 #include "BuiltinExecutables.h"
30 #include "BytecodeGenerator.h"
31 #include "CachedTypes.h"
32 #include "ClassInfo.h"
33 #include "CodeCache.h"
34 #include "Debugger.h"
35 #include "ExecutableInfo.h"
36 #include "FunctionOverrides.h"
37 #include "IsoCellSetInlines.h"
38 #include "JSCInlines.h"
39 #include "Parser.h"
40 #include "SourceProvider.h"
41 #include "Structure.h"
42 #include "UnlinkedFunctionCodeBlock.h"
43
44 namespace JSC {
45
46 static_assert(sizeof(UnlinkedFunctionExecutable) <= 128, "UnlinkedFunctionExecutable should fit in a 128-byte cell to keep allocated blocks count to only one after initializing JSGlobalObject.");
47
48 const ClassInfo UnlinkedFunctionExecutable::s_info = { "UnlinkedFunctionExecutable", nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(UnlinkedFunctionExecutable) };
49
50 static UnlinkedFunctionCodeBlock* generateUnlinkedFunctionCodeBlock(
51     VM& vm, UnlinkedFunctionExecutable* executable, const SourceCode& source,
52     CodeSpecializationKind kind, DebuggerMode debuggerMode,
53     UnlinkedFunctionKind functionKind, ParserError& error, SourceParseMode parseMode)
54 {
55     JSParserBuiltinMode builtinMode = executable->isBuiltinFunction() ? JSParserBuiltinMode::Builtin : JSParserBuiltinMode::NotBuiltin;
56     JSParserStrictMode strictMode = executable->isInStrictContext() ? JSParserStrictMode::Strict : JSParserStrictMode::NotStrict;
57     JSParserScriptMode scriptMode = executable->scriptMode();
58     ASSERT(isFunctionParseMode(executable->parseMode()));
59     std::unique_ptr<FunctionNode> function = parse<FunctionNode>(
60         &vm, source, executable->name(), builtinMode, strictMode, scriptMode, executable->parseMode(), executable->superBinding(), error, nullptr);
61
62     if (!function) {
63         ASSERT(error.isValid());
64         return nullptr;
65     }
66
67     function->finishParsing(executable->name(), executable->functionMode());
68     executable->recordParse(function->features(), function->hasCapturedVariables());
69
70     bool isClassContext = executable->superBinding() == SuperBinding::Needed;
71
72     UnlinkedFunctionCodeBlock* result = UnlinkedFunctionCodeBlock::create(&vm, FunctionCode, ExecutableInfo(function->usesEval(), function->isStrictMode(), kind == CodeForConstruct, functionKind == UnlinkedBuiltinFunction, executable->constructorKind(), scriptMode, executable->superBinding(), parseMode, executable->derivedContextType(), false, isClassContext, EvalContextType::FunctionEvalContext), debuggerMode);
73
74     VariableEnvironment parentScopeTDZVariables = executable->parentScopeTDZVariables();
75     error = BytecodeGenerator::generate(vm, function.get(), source, result, debuggerMode, &parentScopeTDZVariables);
76
77     if (error.isValid())
78         return nullptr;
79     return result;
80 }
81
82 UnlinkedFunctionExecutable::UnlinkedFunctionExecutable(VM* vm, Structure* structure, const SourceCode& parentSource, FunctionMetadataNode* node, UnlinkedFunctionKind kind, ConstructAbility constructAbility, JSParserScriptMode scriptMode, CompactVariableMap::Handle parentScopeTDZVariables, DerivedContextType derivedContextType, bool isBuiltinDefaultClassConstructor)
83     : Base(*vm, structure)
84     , m_firstLineOffset(node->firstLine() - parentSource.firstLine().oneBasedInt())
85     , m_lineCount(node->lastLine() - node->firstLine())
86     , m_unlinkedFunctionNameStart(node->functionNameStart() - parentSource.startOffset())
87     , m_unlinkedBodyStartColumn(node->startColumn())
88     , m_unlinkedBodyEndColumn(m_lineCount ? node->endColumn() : node->endColumn() - node->startColumn())
89     , m_startOffset(node->source().startOffset() - parentSource.startOffset())
90     , m_sourceLength(node->source().length())
91     , m_parametersStartOffset(node->parametersStart())
92     , m_typeProfilingStartOffset(node->functionKeywordStart())
93     , m_typeProfilingEndOffset(node->startStartOffset() + node->source().length() - 1)
94     , m_parameterCount(node->parameterCount())
95     , m_features(0)
96     , m_sourceParseMode(node->parseMode())
97     , m_isInStrictContext(node->isInStrictContext())
98     , m_hasCapturedVariables(false)
99     , m_isBuiltinFunction(kind == UnlinkedBuiltinFunction)
100     , m_isBuiltinDefaultClassConstructor(isBuiltinDefaultClassConstructor)
101     , m_constructAbility(static_cast<unsigned>(constructAbility))
102     , m_constructorKind(static_cast<unsigned>(node->constructorKind()))
103     , m_functionMode(static_cast<unsigned>(node->functionMode()))
104     , m_scriptMode(static_cast<unsigned>(scriptMode))
105     , m_superBinding(static_cast<unsigned>(node->superBinding()))
106     , m_derivedContextType(static_cast<unsigned>(derivedContextType))
107     , m_isCached(false)
108     , m_unlinkedCodeBlockForCall()
109     , m_unlinkedCodeBlockForConstruct()
110     , m_name(node->ident())
111     , m_ecmaName(node->ecmaName())
112     , m_inferredName(node->inferredName())
113     , m_parentScopeTDZVariables(WTFMove(parentScopeTDZVariables))
114 {
115     // Make sure these bitfields are adequately wide.
116     ASSERT(m_constructAbility == static_cast<unsigned>(constructAbility));
117     ASSERT(m_constructorKind == static_cast<unsigned>(node->constructorKind()));
118     ASSERT(m_functionMode == static_cast<unsigned>(node->functionMode()));
119     ASSERT(m_scriptMode == static_cast<unsigned>(scriptMode));
120     ASSERT(m_superBinding == static_cast<unsigned>(node->superBinding()));
121     ASSERT(m_derivedContextType == static_cast<unsigned>(derivedContextType));
122     ASSERT(!(m_isBuiltinDefaultClassConstructor && constructorKind() == ConstructorKind::None));
123     if (!node->classSource().isNull())
124         setClassSource(node->classSource());
125 }
126
127 UnlinkedFunctionExecutable::~UnlinkedFunctionExecutable()
128 {
129     if (m_isCached)
130         m_decoder.~RefPtr();
131 }
132
133 void UnlinkedFunctionExecutable::destroy(JSCell* cell)
134 {
135     static_cast<UnlinkedFunctionExecutable*>(cell)->~UnlinkedFunctionExecutable();
136 }
137
138 void UnlinkedFunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
139 {
140     UnlinkedFunctionExecutable* thisObject = jsCast<UnlinkedFunctionExecutable*>(cell);
141     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
142     Base::visitChildren(thisObject, visitor);
143     if (!thisObject->m_isCached) {
144         visitor.append(thisObject->m_unlinkedCodeBlockForCall);
145         visitor.append(thisObject->m_unlinkedCodeBlockForConstruct);
146     }
147 }
148
149 SourceCode UnlinkedFunctionExecutable::linkedSourceCode(const SourceCode& passedParentSource) const
150 {
151     const SourceCode& parentSource = !m_isBuiltinDefaultClassConstructor ? passedParentSource : BuiltinExecutables::defaultConstructorSourceCode(constructorKind());
152     unsigned startColumn = linkedStartColumn(parentSource.startColumn().oneBasedInt());
153     unsigned startOffset = parentSource.startOffset() + m_startOffset;
154     unsigned firstLine = parentSource.firstLine().oneBasedInt() + m_firstLineOffset;
155     return SourceCode(parentSource.provider(), startOffset, startOffset + m_sourceLength, firstLine, startColumn);
156 }
157
158 FunctionExecutable* UnlinkedFunctionExecutable::link(VM& vm, const SourceCode& passedParentSource, Optional<int> overrideLineNumber, Intrinsic intrinsic)
159 {
160     SourceCode source = linkedSourceCode(passedParentSource);
161     unsigned firstLine = source.firstLine().oneBasedInt();
162     unsigned lineCount = m_lineCount;
163     unsigned endColumn = linkedEndColumn(source.startColumn().oneBasedInt());
164     FunctionOverrides::OverrideInfo overrideInfo;
165     bool hasFunctionOverride = false;
166     if (UNLIKELY(Options::functionOverrides())) {
167         hasFunctionOverride = FunctionOverrides::initializeOverrideFor(source, overrideInfo);
168         if (UNLIKELY(hasFunctionOverride)) {
169             firstLine = overrideInfo.firstLine;
170             lineCount = overrideInfo.lineCount;
171             endColumn = overrideInfo.endColumn;
172             source = overrideInfo.sourceCode;
173         }
174     }
175
176     FunctionExecutable* result = FunctionExecutable::create(vm, source, this, firstLine + lineCount, endColumn, intrinsic);
177     if (overrideLineNumber)
178         result->setOverrideLineNumber(*overrideLineNumber);
179
180     if (UNLIKELY(hasFunctionOverride)) {
181         result->overrideParameterAndTypeProfilingStartEndOffsets(
182             overrideInfo.parametersStartOffset,
183             overrideInfo.typeProfilingStartOffset,
184             overrideInfo.typeProfilingEndOffset);
185     }
186
187     return result;
188 }
189
190 UnlinkedFunctionExecutable* UnlinkedFunctionExecutable::fromGlobalCode(
191     const Identifier& name, ExecState& exec, const SourceCode& source, 
192     JSObject*& exception, int overrideLineNumber, Optional<int> functionConstructorParametersEndPosition)
193 {
194     ParserError error;
195     VM& vm = exec.vm();
196     auto& globalObject = *exec.lexicalGlobalObject();
197     CodeCache* codeCache = vm.codeCache();
198     DebuggerMode debuggerMode = globalObject.hasInteractiveDebugger() ? DebuggerOn : DebuggerOff;
199     UnlinkedFunctionExecutable* executable = codeCache->getUnlinkedGlobalFunctionExecutable(vm, name, source, debuggerMode, functionConstructorParametersEndPosition, error);
200
201     if (globalObject.hasDebugger())
202         globalObject.debugger()->sourceParsed(&exec, source.provider(), error.line(), error.message());
203
204     if (error.isValid()) {
205         exception = error.toErrorObject(&globalObject, source, overrideLineNumber);
206         return nullptr;
207     }
208
209     return executable;
210 }
211
212 UnlinkedFunctionCodeBlock* UnlinkedFunctionExecutable::unlinkedCodeBlockFor(CodeSpecializationKind specializationKind)
213 {
214     switch (specializationKind) {
215     case CodeForCall:
216         return m_unlinkedCodeBlockForCall.get();
217     case CodeForConstruct:
218         return m_unlinkedCodeBlockForConstruct.get();
219     }
220     ASSERT_NOT_REACHED();
221     return nullptr;
222 }
223
224 UnlinkedFunctionCodeBlock* UnlinkedFunctionExecutable::unlinkedCodeBlockFor(
225     VM& vm, const SourceCode& source, CodeSpecializationKind specializationKind, 
226     DebuggerMode debuggerMode, ParserError& error, SourceParseMode parseMode)
227 {
228     if (m_isCached)
229         decodeCachedCodeBlocks();
230     switch (specializationKind) {
231     case CodeForCall:
232         if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForCall.get())
233             return codeBlock;
234         break;
235     case CodeForConstruct:
236         if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForConstruct.get())
237             return codeBlock;
238         break;
239     }
240
241     UnlinkedFunctionCodeBlock* result = generateUnlinkedFunctionCodeBlock(
242         vm, this, source, specializationKind, debuggerMode, 
243         isBuiltinFunction() ? UnlinkedBuiltinFunction : UnlinkedNormalFunction, 
244         error, parseMode);
245     
246     if (error.isValid())
247         return nullptr;
248
249     switch (specializationKind) {
250     case CodeForCall:
251         m_unlinkedCodeBlockForCall.set(vm, this, result);
252         break;
253     case CodeForConstruct:
254         m_unlinkedCodeBlockForConstruct.set(vm, this, result);
255         break;
256     }
257     vm.unlinkedFunctionExecutableSpace.set.add(this);
258     return result;
259 }
260
261 void UnlinkedFunctionExecutable::decodeCachedCodeBlocks()
262 {
263     ASSERT(m_isCached);
264     ASSERT(m_decoder);
265     ASSERT(m_cachedCodeBlockForCallOffset || m_cachedCodeBlockForConstructOffset);
266
267     RefPtr<Decoder> decoder = WTFMove(m_decoder);
268     int32_t cachedCodeBlockForCallOffset = m_cachedCodeBlockForCallOffset;
269     int32_t cachedCodeBlockForConstructOffset = m_cachedCodeBlockForConstructOffset;
270
271     DeferGC deferGC(decoder->vm().heap);
272
273     // No need to clear m_unlinkedCodeBlockForCall here, since we moved the decoder out of the same slot
274     if (cachedCodeBlockForCallOffset)
275         decodeFunctionCodeBlock(*decoder, cachedCodeBlockForCallOffset, m_unlinkedCodeBlockForCall, this);
276     if (cachedCodeBlockForConstructOffset)
277         decodeFunctionCodeBlock(*decoder, cachedCodeBlockForConstructOffset, m_unlinkedCodeBlockForConstruct, this);
278     else
279         m_unlinkedCodeBlockForConstruct.clear();
280
281     WTF::storeStoreFence();
282     m_isCached = false;
283     decoder->vm().heap.writeBarrier(this);
284 }
285
286 UnlinkedFunctionExecutable::RareData& UnlinkedFunctionExecutable::ensureRareDataSlow()
287 {
288     ASSERT(!m_rareData);
289     m_rareData = std::make_unique<RareData>();
290     return *m_rareData;
291 }
292
293 void UnlinkedFunctionExecutable::setInvalidTypeProfilingOffsets()
294 {
295     m_typeProfilingStartOffset = std::numeric_limits<unsigned>::max();
296     m_typeProfilingEndOffset = std::numeric_limits<unsigned>::max();
297 }
298
299 } // namespace JSC