1/*
2 * Copyright (C) 2013-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#include "config.h"
27#include "FTLOSREntry.h"
28
29#include "CallFrame.h"
30#include "CodeBlock.h"
31#include "DFGJITCode.h"
32#include "FTLForOSREntryJITCode.h"
33#include "OperandsInlines.h"
34#include "JSCInlines.h"
35#include "VMInlines.h"
36
37#if ENABLE(FTL_JIT)
38
39namespace JSC { namespace FTL {
40
41SUPPRESS_ASAN
42void* prepareOSREntry(
43 VM& vm, CallFrame* callFrame, CodeBlock* dfgCodeBlock, CodeBlock* entryCodeBlock,
44 BytecodeIndex bytecodeIndex, unsigned streamIndex)
45{
46 CodeBlock* baseline = dfgCodeBlock->baselineVersion();
47 ExecutableBase* executable = dfgCodeBlock->ownerExecutable();
48 DFG::JITCode* dfgCode = dfgCodeBlock->jitCode()->dfg();
49 ForOSREntryJITCode* entryCode = entryCodeBlock->jitCode()->ftlForOSREntry();
50
51 if (!entryCode->dfgCommon()->isStillValid) {
52 dfgCode->clearOSREntryBlockAndResetThresholds(dfgCodeBlock);
53 return 0;
54 }
55
56 if (Options::verboseOSR()) {
57 dataLog(
58 "FTL OSR from ", *dfgCodeBlock, " to ", *entryCodeBlock, " at ",
59 bytecodeIndex, ".\n");
60 }
61
62 if (bytecodeIndex)
63 jsCast<ScriptExecutable*>(executable)->setDidTryToEnterInLoop(true);
64
65 if (bytecodeIndex != entryCode->bytecodeIndex()) {
66 if (Options::verboseOSR())
67 dataLog(" OSR failed because we don't have an entrypoint for ", bytecodeIndex, "; ours is for ", entryCode->bytecodeIndex(), "\n");
68 return 0;
69 }
70
71 Operands<Optional<JSValue>> values;
72 dfgCode->reconstruct(callFrame, dfgCodeBlock, CodeOrigin(bytecodeIndex), streamIndex, values);
73
74 if (Options::verboseOSR())
75 dataLog(" Values at entry: ", values, "\n");
76
77 for (int argument = values.numberOfArguments(); argument--;) {
78 JSValue valueOnStack = callFrame->r(virtualRegisterForArgument(argument).offset()).asanUnsafeJSValue();
79 Optional<JSValue> reconstructedValue = values.argument(argument);
80 if ((reconstructedValue && valueOnStack == reconstructedValue.value()) || !argument)
81 continue;
82 dataLog("Mismatch between reconstructed values and the value on the stack for argument arg", argument, " for ", *entryCodeBlock, " at ", bytecodeIndex, ":\n");
83 dataLog(" Value on stack: ", valueOnStack, "\n");
84 dataLog(" Reconstructed value: ", reconstructedValue, "\n");
85 RELEASE_ASSERT_NOT_REACHED();
86 }
87
88 RELEASE_ASSERT(
89 static_cast<int>(values.numberOfLocals()) == baseline->numCalleeLocals());
90
91 EncodedJSValue* scratch = static_cast<EncodedJSValue*>(
92 entryCode->entryBuffer()->dataBuffer());
93
94 for (int local = values.numberOfLocals(); local--;) {
95 Optional<JSValue> value = values.local(local);
96 if (value)
97 scratch[local] = JSValue::encode(value.value());
98 else
99 scratch[local] = JSValue::encode(JSValue());
100 }
101
102 int stackFrameSize = entryCode->common.requiredRegisterCountForExecutionAndExit();
103 if (UNLIKELY(!vm.ensureStackCapacityFor(&callFrame->registers()[virtualRegisterForLocal(stackFrameSize - 1).offset()]))) {
104 if (Options::verboseOSR())
105 dataLog(" OSR failed because stack growth failed.\n");
106 return 0;
107 }
108
109 callFrame->setCodeBlock(entryCodeBlock);
110
111 void* result = entryCode->addressForCall(ArityCheckNotRequired).executableAddress();
112 if (Options::verboseOSR())
113 dataLog(" Entry will succeed, going to address ", RawPointer(result), "\n");
114
115 return result;
116}
117
118} } // namespace JSC::FTL
119
120#endif // ENABLE(FTL_JIT)
121
122
123