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