| 1 | /* |
| 2 | * Copyright (C) 2012-2019 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 | |
| 45 | namespace JSC { |
| 46 | |
| 47 | static_assert(sizeof(UnlinkedFunctionExecutable) <= 128, "UnlinkedFunctionExecutable should fit in a 128-byte cell to keep allocated blocks count to only one after initializing JSGlobalObject." ); |
| 48 | |
| 49 | const ClassInfo UnlinkedFunctionExecutable::s_info = { "UnlinkedFunctionExecutable" , nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(UnlinkedFunctionExecutable) }; |
| 50 | |
| 51 | static 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 | |
| 84 | UnlinkedFunctionExecutable::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 | |
| 130 | UnlinkedFunctionExecutable::~UnlinkedFunctionExecutable() |
| 131 | { |
| 132 | if (m_isCached) |
| 133 | m_decoder.~RefPtr(); |
| 134 | } |
| 135 | |
| 136 | void UnlinkedFunctionExecutable::destroy(JSCell* cell) |
| 137 | { |
| 138 | static_cast<UnlinkedFunctionExecutable*>(cell)->~UnlinkedFunctionExecutable(); |
| 139 | } |
| 140 | |
| 141 | void 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 | |
| 164 | SourceCode 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 | |
| 173 | FunctionExecutable* 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 | |
| 191 | UnlinkedFunctionExecutable* UnlinkedFunctionExecutable::fromGlobalCode( |
| 192 | const Identifier& name, JSGlobalObject* globalObject, const SourceCode& source, |
| 193 | JSObject*& exception, int overrideLineNumber, Optional<int> functionConstructorParametersEndPosition) |
| 194 | { |
| 195 | ParserError error; |
| 196 | VM& vm = globalObject->vm(); |
| 197 | CodeCache* codeCache = vm.codeCache(); |
| 198 | OptionSet<CodeGenerationMode> codeGenerationMode = globalObject->defaultCodeGenerationMode(); |
| 199 | UnlinkedFunctionExecutable* executable = codeCache->getUnlinkedGlobalFunctionExecutable(vm, name, source, codeGenerationMode, functionConstructorParametersEndPosition, error); |
| 200 | |
| 201 | if (globalObject->hasDebugger()) |
| 202 | globalObject->debugger()->sourceParsed(globalObject, 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( |
| 213 | VM& vm, const SourceCode& source, CodeSpecializationKind specializationKind, |
| 214 | OptionSet<CodeGenerationMode> codeGenerationMode, ParserError& error, SourceParseMode parseMode) |
| 215 | { |
| 216 | if (m_isCached) |
| 217 | decodeCachedCodeBlocks(vm); |
| 218 | switch (specializationKind) { |
| 219 | case CodeForCall: |
| 220 | if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForCall.get()) |
| 221 | return codeBlock; |
| 222 | break; |
| 223 | case CodeForConstruct: |
| 224 | if (UnlinkedFunctionCodeBlock* codeBlock = m_unlinkedCodeBlockForConstruct.get()) |
| 225 | return codeBlock; |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | UnlinkedFunctionCodeBlock* result = generateUnlinkedFunctionCodeBlock( |
| 230 | vm, this, source, specializationKind, codeGenerationMode, |
| 231 | isBuiltinFunction() ? UnlinkedBuiltinFunction : UnlinkedNormalFunction, |
| 232 | error, parseMode); |
| 233 | |
| 234 | if (error.isValid()) |
| 235 | return nullptr; |
| 236 | |
| 237 | switch (specializationKind) { |
| 238 | case CodeForCall: |
| 239 | m_unlinkedCodeBlockForCall.set(vm, this, result); |
| 240 | break; |
| 241 | case CodeForConstruct: |
| 242 | m_unlinkedCodeBlockForConstruct.set(vm, this, result); |
| 243 | break; |
| 244 | } |
| 245 | vm.unlinkedFunctionExecutableSpace.set.add(this); |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | void UnlinkedFunctionExecutable::decodeCachedCodeBlocks(VM& vm) |
| 250 | { |
| 251 | ASSERT(m_isCached); |
| 252 | ASSERT(m_decoder); |
| 253 | ASSERT(m_cachedCodeBlockForCallOffset || m_cachedCodeBlockForConstructOffset); |
| 254 | |
| 255 | RefPtr<Decoder> decoder = WTFMove(m_decoder); |
| 256 | int32_t cachedCodeBlockForCallOffset = m_cachedCodeBlockForCallOffset; |
| 257 | int32_t cachedCodeBlockForConstructOffset = m_cachedCodeBlockForConstructOffset; |
| 258 | |
| 259 | DeferGC deferGC(vm.heap); |
| 260 | |
| 261 | // No need to clear m_unlinkedCodeBlockForCall here, since we moved the decoder out of the same slot |
| 262 | if (cachedCodeBlockForCallOffset) |
| 263 | decodeFunctionCodeBlock(*decoder, cachedCodeBlockForCallOffset, m_unlinkedCodeBlockForCall, this); |
| 264 | if (cachedCodeBlockForConstructOffset) |
| 265 | decodeFunctionCodeBlock(*decoder, cachedCodeBlockForConstructOffset, m_unlinkedCodeBlockForConstruct, this); |
| 266 | else |
| 267 | m_unlinkedCodeBlockForConstruct.clear(); |
| 268 | |
| 269 | WTF::storeStoreFence(); |
| 270 | m_isCached = false; |
| 271 | vm.heap.writeBarrier(this); |
| 272 | } |
| 273 | |
| 274 | UnlinkedFunctionExecutable::RareData& UnlinkedFunctionExecutable::ensureRareDataSlow() |
| 275 | { |
| 276 | ASSERT(!m_rareData); |
| 277 | m_rareData = makeUnique<RareData>(); |
| 278 | return *m_rareData; |
| 279 | } |
| 280 | |
| 281 | void UnlinkedFunctionExecutable::setInvalidTypeProfilingOffsets() |
| 282 | { |
| 283 | m_typeProfilingStartOffset = std::numeric_limits<unsigned>::max(); |
| 284 | m_typeProfilingEndOffset = std::numeric_limits<unsigned>::max(); |
| 285 | } |
| 286 | |
| 287 | void UnlinkedFunctionExecutable::finalizeUnconditionally(VM& vm) |
| 288 | { |
| 289 | if (codeBlockEdgeMayBeWeak()) { |
| 290 | bool isCleared = false; |
| 291 | bool isStillValid = false; |
| 292 | auto clearIfDead = [&] (WriteBarrier<UnlinkedFunctionCodeBlock>& unlinkedCodeBlock) { |
| 293 | if (!unlinkedCodeBlock) |
| 294 | return; |
| 295 | if (!vm.heap.isMarked(unlinkedCodeBlock.get())) { |
| 296 | unlinkedCodeBlock.clear(); |
| 297 | isCleared = true; |
| 298 | } else |
| 299 | isStillValid = true; |
| 300 | }; |
| 301 | clearIfDead(m_unlinkedCodeBlockForCall); |
| 302 | clearIfDead(m_unlinkedCodeBlockForConstruct); |
| 303 | if (isCleared && !isStillValid) |
| 304 | vm.unlinkedFunctionExecutableSpace.set.remove(this); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | } // namespace JSC |
| 309 | |