| 1 | /* |
| 2 | * Copyright (C) 2014-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 | |
| 27 | #include "config.h" |
| 28 | #include "BuiltinExecutables.h" |
| 29 | |
| 30 | #include "BuiltinNames.h" |
| 31 | #include "JSCInlines.h" |
| 32 | #include "Parser.h" |
| 33 | #include <wtf/NeverDestroyed.h> |
| 34 | |
| 35 | namespace JSC { |
| 36 | |
| 37 | BuiltinExecutables::BuiltinExecutables(VM& vm) |
| 38 | : m_vm(vm) |
| 39 | , m_combinedSourceProvider(StringSourceProvider::create(StringImpl::createFromLiteral(s_JSCCombinedCode, s_JSCCombinedCodeLength), { }, URL())) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | SourceCode BuiltinExecutables::defaultConstructorSourceCode(ConstructorKind constructorKind) |
| 44 | { |
| 45 | switch (constructorKind) { |
| 46 | case ConstructorKind::None: |
| 47 | break; |
| 48 | case ConstructorKind::Base: { |
| 49 | static NeverDestroyed<const String> baseConstructorCode(MAKE_STATIC_STRING_IMPL("(function () { })" )); |
| 50 | return makeSource(baseConstructorCode, { }); |
| 51 | } |
| 52 | case ConstructorKind::Extends: { |
| 53 | static NeverDestroyed<const String> derivedConstructorCode(MAKE_STATIC_STRING_IMPL("(function (...args) { super(...args); })" )); |
| 54 | return makeSource(derivedConstructorCode, { }); |
| 55 | } |
| 56 | } |
| 57 | RELEASE_ASSERT_NOT_REACHED(); |
| 58 | return SourceCode(); |
| 59 | } |
| 60 | |
| 61 | UnlinkedFunctionExecutable* BuiltinExecutables::createDefaultConstructor(ConstructorKind constructorKind, const Identifier& name) |
| 62 | { |
| 63 | switch (constructorKind) { |
| 64 | case ConstructorKind::None: |
| 65 | break; |
| 66 | case ConstructorKind::Base: |
| 67 | case ConstructorKind::Extends: |
| 68 | return createExecutable(m_vm, defaultConstructorSourceCode(constructorKind), name, constructorKind, ConstructAbility::CanConstruct); |
| 69 | } |
| 70 | ASSERT_NOT_REACHED(); |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | UnlinkedFunctionExecutable* BuiltinExecutables::createBuiltinExecutable(const SourceCode& code, const Identifier& name, ConstructAbility constructAbility) |
| 75 | { |
| 76 | return createExecutable(m_vm, code, name, ConstructorKind::None, constructAbility); |
| 77 | } |
| 78 | |
| 79 | UnlinkedFunctionExecutable* createBuiltinExecutable(VM& vm, const SourceCode& code, const Identifier& name, ConstructAbility constructAbility) |
| 80 | { |
| 81 | return BuiltinExecutables::createExecutable(vm, code, name, ConstructorKind::None, constructAbility); |
| 82 | } |
| 83 | |
| 84 | UnlinkedFunctionExecutable* BuiltinExecutables::createExecutable(VM& vm, const SourceCode& source, const Identifier& name, ConstructorKind constructorKind, ConstructAbility constructAbility) |
| 85 | { |
| 86 | // FIXME: Can we just make MetaData computation be constexpr and have the compiler do this for us? |
| 87 | // https://bugs.webkit.org/show_bug.cgi?id=193272 |
| 88 | // Someone should get mad at me for writing this code. But, it prevents us from recursing into |
| 89 | // the parser, and hence, from throwing stack overflow when parsing a builtin. |
| 90 | StringView view = source.view(); |
| 91 | RELEASE_ASSERT(!view.isNull()); |
| 92 | RELEASE_ASSERT(view.is8Bit()); |
| 93 | auto* characters = view.characters8(); |
| 94 | const char* regularFunctionBegin = "(function (" ; |
| 95 | const char* asyncFunctionBegin = "(async function (" ; |
| 96 | RELEASE_ASSERT(view.length() >= strlen("(function (){})" )); |
| 97 | bool isAsyncFunction = view.length() >= strlen("(async function (){})" ) && !memcmp(characters, asyncFunctionBegin, strlen(asyncFunctionBegin)); |
| 98 | RELEASE_ASSERT(isAsyncFunction || !memcmp(characters, regularFunctionBegin, strlen(regularFunctionBegin))); |
| 99 | |
| 100 | unsigned asyncOffset = isAsyncFunction ? strlen("async " ) : 0; |
| 101 | unsigned = strlen("function (" ) + asyncOffset; |
| 102 | unsigned startColumn = parametersStart; |
| 103 | int functionKeywordStart = strlen("(" ) + asyncOffset; |
| 104 | int functionNameStart = parametersStart; |
| 105 | bool isInStrictContext = false; |
| 106 | bool isArrowFunctionBodyExpression = false; |
| 107 | |
| 108 | unsigned parameterCount; |
| 109 | { |
| 110 | unsigned i = parametersStart + 1; |
| 111 | unsigned commas = 0; |
| 112 | bool sawOneParam = false; |
| 113 | bool hasRestParam = false; |
| 114 | while (true) { |
| 115 | ASSERT(i < view.length()); |
| 116 | if (characters[i] == ')') |
| 117 | break; |
| 118 | |
| 119 | if (characters[i] == ',') |
| 120 | ++commas; |
| 121 | else if (!Lexer<LChar>::isWhiteSpace(characters[i])) |
| 122 | sawOneParam = true; |
| 123 | |
| 124 | if (i + 2 < view.length() && characters[i] == '.' && characters[i + 1] == '.' && characters[i + 2] == '.') { |
| 125 | hasRestParam = true; |
| 126 | i += 2; |
| 127 | } |
| 128 | |
| 129 | ++i; |
| 130 | } |
| 131 | |
| 132 | if (commas) |
| 133 | parameterCount = commas + 1; |
| 134 | else if (sawOneParam) |
| 135 | parameterCount = 1; |
| 136 | else |
| 137 | parameterCount = 0; |
| 138 | |
| 139 | if (hasRestParam) { |
| 140 | RELEASE_ASSERT(parameterCount); |
| 141 | --parameterCount; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | unsigned lineCount = 0; |
| 146 | unsigned endColumn = 0; |
| 147 | unsigned offsetOfLastNewline = 0; |
| 148 | Optional<unsigned> offsetOfSecondToLastNewline; |
| 149 | for (unsigned i = 0; i < view.length(); ++i) { |
| 150 | if (characters[i] == '\n') { |
| 151 | if (lineCount) |
| 152 | offsetOfSecondToLastNewline = offsetOfLastNewline; |
| 153 | ++lineCount; |
| 154 | endColumn = 0; |
| 155 | offsetOfLastNewline = i; |
| 156 | } else |
| 157 | ++endColumn; |
| 158 | |
| 159 | if (!isInStrictContext && (characters[i] == '"' || characters[i] == '\'')) { |
| 160 | const unsigned useStrictLength = strlen("use strict" ); |
| 161 | if (i + 1 + useStrictLength < view.length()) { |
| 162 | if (!memcmp(characters + i + 1, "use strict" , useStrictLength)) { |
| 163 | isInStrictContext = true; |
| 164 | i += 1 + useStrictLength; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | unsigned positionBeforeLastNewlineLineStartOffset = offsetOfSecondToLastNewline ? *offsetOfSecondToLastNewline + 1 : 0; |
| 171 | |
| 172 | int closeBraceOffsetFromEnd = 1; |
| 173 | while (true) { |
| 174 | if (characters[view.length() - closeBraceOffsetFromEnd] == '}') |
| 175 | break; |
| 176 | ++closeBraceOffsetFromEnd; |
| 177 | } |
| 178 | |
| 179 | JSTextPosition positionBeforeLastNewline; |
| 180 | positionBeforeLastNewline.line = lineCount; |
| 181 | positionBeforeLastNewline.offset = source.startOffset() + offsetOfLastNewline; |
| 182 | positionBeforeLastNewline.lineStartOffset = source.startOffset() + positionBeforeLastNewlineLineStartOffset; |
| 183 | |
| 184 | SourceCode newSource = source.subExpression(source.startOffset() + parametersStart, source.startOffset() + (view.length() - closeBraceOffsetFromEnd), 0, parametersStart); |
| 185 | bool isBuiltinDefaultClassConstructor = constructorKind != ConstructorKind::None; |
| 186 | UnlinkedFunctionKind kind = isBuiltinDefaultClassConstructor ? UnlinkedNormalFunction : UnlinkedBuiltinFunction; |
| 187 | |
| 188 | SourceParseMode parseMode = isAsyncFunction ? SourceParseMode::AsyncFunctionMode : SourceParseMode::NormalFunctionMode; |
| 189 | |
| 190 | JSTokenLocation start; |
| 191 | start.line = -1; |
| 192 | start.lineStartOffset = std::numeric_limits<unsigned>::max(); |
| 193 | start.startOffset = source.startOffset() + parametersStart; |
| 194 | start.endOffset = std::numeric_limits<unsigned>::max(); |
| 195 | |
| 196 | JSTokenLocation end; |
| 197 | end.line = 1; |
| 198 | end.lineStartOffset = source.startOffset(); |
| 199 | end.startOffset = source.startOffset() + strlen("(" ) + asyncOffset; |
| 200 | end.endOffset = std::numeric_limits<unsigned>::max(); |
| 201 | |
| 202 | FunctionMetadataNode metadata( |
| 203 | start, end, startColumn, endColumn, source.startOffset() + functionKeywordStart, source.startOffset() + functionNameStart, source.startOffset() + parametersStart, |
| 204 | isInStrictContext, constructorKind, constructorKind == ConstructorKind::Extends ? SuperBinding::Needed : SuperBinding::NotNeeded, |
| 205 | parameterCount, parseMode, isArrowFunctionBodyExpression); |
| 206 | |
| 207 | metadata.finishParsing(newSource, Identifier(), FunctionMode::FunctionExpression); |
| 208 | metadata.overrideName(name); |
| 209 | metadata.setEndPosition(positionBeforeLastNewline); |
| 210 | |
| 211 | if (!ASSERT_DISABLED || Options::validateBytecode()) { |
| 212 | JSTextPosition positionBeforeLastNewlineFromParser; |
| 213 | ParserError error; |
| 214 | JSParserBuiltinMode builtinMode = isBuiltinDefaultClassConstructor ? JSParserBuiltinMode::NotBuiltin : JSParserBuiltinMode::Builtin; |
| 215 | std::unique_ptr<ProgramNode> program = parse<ProgramNode>( |
| 216 | &vm, source, Identifier(), builtinMode, |
| 217 | JSParserStrictMode::NotStrict, JSParserScriptMode::Classic, SourceParseMode::ProgramMode, SuperBinding::NotNeeded, error, |
| 218 | &positionBeforeLastNewlineFromParser, constructorKind); |
| 219 | |
| 220 | if (program) { |
| 221 | StatementNode* exprStatement = program->singleStatement(); |
| 222 | RELEASE_ASSERT(exprStatement); |
| 223 | RELEASE_ASSERT(exprStatement->isExprStatement()); |
| 224 | ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr(); |
| 225 | RELEASE_ASSERT(funcExpr); |
| 226 | RELEASE_ASSERT(funcExpr->isFuncExprNode()); |
| 227 | FunctionMetadataNode* metadataFromParser = static_cast<FuncExprNode*>(funcExpr)->metadata(); |
| 228 | RELEASE_ASSERT(!program->hasCapturedVariables()); |
| 229 | |
| 230 | metadataFromParser->setEndPosition(positionBeforeLastNewlineFromParser); |
| 231 | RELEASE_ASSERT(metadataFromParser); |
| 232 | RELEASE_ASSERT(metadataFromParser->ident().isNull()); |
| 233 | |
| 234 | // This function assumes an input string that would result in a single anonymous function expression. |
| 235 | metadataFromParser->setEndPosition(positionBeforeLastNewlineFromParser); |
| 236 | RELEASE_ASSERT(metadataFromParser); |
| 237 | metadataFromParser->overrideName(name); |
| 238 | metadataFromParser->setEndPosition(positionBeforeLastNewlineFromParser); |
| 239 | if (metadata != *metadataFromParser || positionBeforeLastNewlineFromParser != positionBeforeLastNewline) { |
| 240 | dataLogLn("Expected Metadata:\n" , metadata); |
| 241 | dataLogLn("Metadata from parser:\n" , *metadataFromParser); |
| 242 | dataLogLn("positionBeforeLastNewlineFromParser.line " , positionBeforeLastNewlineFromParser.line); |
| 243 | dataLogLn("positionBeforeLastNewlineFromParser.offset " , positionBeforeLastNewlineFromParser.offset); |
| 244 | dataLogLn("positionBeforeLastNewlineFromParser.lineStartOffset " , positionBeforeLastNewlineFromParser.lineStartOffset); |
| 245 | dataLogLn("positionBeforeLastNewline.line " , positionBeforeLastNewline.line); |
| 246 | dataLogLn("positionBeforeLastNewline.offset " , positionBeforeLastNewline.offset); |
| 247 | dataLogLn("positionBeforeLastNewline.lineStartOffset " , positionBeforeLastNewline.lineStartOffset); |
| 248 | WTFLogAlways("Metadata of parser and hand rolled parser don't match\n" ); |
| 249 | CRASH(); |
| 250 | } |
| 251 | } else { |
| 252 | RELEASE_ASSERT(error.isValid()); |
| 253 | RELEASE_ASSERT(error.type() == ParserError::StackOverflow); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | UnlinkedFunctionExecutable* functionExecutable = UnlinkedFunctionExecutable::create(&vm, source, &metadata, kind, constructAbility, JSParserScriptMode::Classic, WTF::nullopt, DerivedContextType::None, isBuiltinDefaultClassConstructor); |
| 258 | return functionExecutable; |
| 259 | } |
| 260 | |
| 261 | void BuiltinExecutables::finalizeUnconditionally() |
| 262 | { |
| 263 | for (auto*& unlinkedExecutable : m_unlinkedExecutables) { |
| 264 | if (unlinkedExecutable && !m_vm.heap.isMarked(unlinkedExecutable)) |
| 265 | unlinkedExecutable = nullptr; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | #define DEFINE_BUILTIN_EXECUTABLES(name, functionName, overrideName, length) \ |
| 270 | SourceCode BuiltinExecutables::name##Source() \ |
| 271 | {\ |
| 272 | return SourceCode { m_combinedSourceProvider.copyRef(), static_cast<int>(s_##name - s_JSCCombinedCode), static_cast<int>((s_##name - s_JSCCombinedCode) + length), 1, 1 };\ |
| 273 | }\ |
| 274 | \ |
| 275 | UnlinkedFunctionExecutable* BuiltinExecutables::name##Executable() \ |
| 276 | {\ |
| 277 | unsigned index = static_cast<unsigned>(BuiltinCodeIndex::name);\ |
| 278 | if (!m_unlinkedExecutables[index]) {\ |
| 279 | Identifier executableName = m_vm.propertyNames->builtinNames().functionName##PublicName();\ |
| 280 | if (overrideName)\ |
| 281 | executableName = Identifier::fromString(&m_vm, overrideName);\ |
| 282 | m_unlinkedExecutables[index] = createBuiltinExecutable(name##Source(), executableName, s_##name##ConstructAbility);\ |
| 283 | }\ |
| 284 | return m_unlinkedExecutables[index];\ |
| 285 | } |
| 286 | JSC_FOREACH_BUILTIN_CODE(DEFINE_BUILTIN_EXECUTABLES) |
| 287 | #undef DEFINE_BUILTIN_EXECUTABLES |
| 288 | |
| 289 | } |
| 290 | |