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 "CodeOrigin.h"
28
29#include "CallFrame.h"
30#include "CodeBlock.h"
31#include "InlineCallFrame.h"
32#include "JSCInlines.h"
33
34namespace JSC {
35
36unsigned CodeOrigin::inlineDepth() const
37{
38 unsigned result = 1;
39 for (InlineCallFrame* current = inlineCallFrame(); current; current = current->directCaller.inlineCallFrame())
40 result++;
41 return result;
42}
43
44bool CodeOrigin::isApproximatelyEqualTo(const CodeOrigin& other, InlineCallFrame* terminal) const
45{
46 CodeOrigin a = *this;
47 CodeOrigin b = other;
48
49 if (!a.isSet())
50 return !b.isSet();
51 if (!b.isSet())
52 return false;
53
54 if (a.isHashTableDeletedValue())
55 return b.isHashTableDeletedValue();
56 if (b.isHashTableDeletedValue())
57 return false;
58
59 for (;;) {
60 ASSERT(a.isSet());
61 ASSERT(b.isSet());
62
63 if (a.bytecodeIndex() != b.bytecodeIndex())
64 return false;
65
66 auto* aInlineCallFrame = a.inlineCallFrame();
67 auto* bInlineCallFrame = b.inlineCallFrame();
68 bool aHasInlineCallFrame = !!aInlineCallFrame && aInlineCallFrame != terminal;
69 bool bHasInlineCallFrame = !!bInlineCallFrame;
70 if (aHasInlineCallFrame != bHasInlineCallFrame)
71 return false;
72
73 if (!aHasInlineCallFrame)
74 return true;
75
76 if (aInlineCallFrame->baselineCodeBlock.get() != bInlineCallFrame->baselineCodeBlock.get())
77 return false;
78
79 a = aInlineCallFrame->directCaller;
80 b = bInlineCallFrame->directCaller;
81 }
82}
83
84unsigned CodeOrigin::approximateHash(InlineCallFrame* terminal) const
85{
86 if (!isSet())
87 return 0;
88 if (isHashTableDeletedValue())
89 return 1;
90
91 unsigned result = 2;
92 CodeOrigin codeOrigin = *this;
93 for (;;) {
94 result += codeOrigin.bytecodeIndex();
95
96 auto* inlineCallFrame = codeOrigin.inlineCallFrame();
97
98 if (!inlineCallFrame)
99 return result;
100
101 if (inlineCallFrame == terminal)
102 return result;
103
104 result += WTF::PtrHash<JSCell*>::hash(inlineCallFrame->baselineCodeBlock.get());
105
106 codeOrigin = inlineCallFrame->directCaller;
107 }
108}
109
110Vector<CodeOrigin> CodeOrigin::inlineStack() const
111{
112 Vector<CodeOrigin> result(inlineDepth());
113 result.last() = *this;
114 unsigned index = result.size() - 2;
115 for (InlineCallFrame* current = inlineCallFrame(); current; current = current->directCaller.inlineCallFrame())
116 result[index--] = current->directCaller;
117 RELEASE_ASSERT(!result[0].inlineCallFrame());
118 return result;
119}
120
121CodeBlock* CodeOrigin::codeOriginOwner() const
122{
123 auto* inlineCallFrame = this->inlineCallFrame();
124 if (!inlineCallFrame)
125 return nullptr;
126 return inlineCallFrame->baselineCodeBlock.get();
127}
128
129int CodeOrigin::stackOffset() const
130{
131 auto* inlineCallFrame = this->inlineCallFrame();
132 if (!inlineCallFrame)
133 return 0;
134 return inlineCallFrame->stackOffset;
135}
136
137void CodeOrigin::dump(PrintStream& out) const
138{
139 if (!isSet()) {
140 out.print("<none>");
141 return;
142 }
143
144 Vector<CodeOrigin> stack = inlineStack();
145 for (unsigned i = 0; i < stack.size(); ++i) {
146 if (i)
147 out.print(" --> ");
148
149 if (InlineCallFrame* frame = stack[i].inlineCallFrame()) {
150 out.print(frame->briefFunctionInformation(), ":<", RawPointer(frame->baselineCodeBlock.get()), "> ");
151 if (frame->isClosureCall)
152 out.print("(closure) ");
153 }
154
155 out.print("bc#", stack[i].bytecodeIndex());
156 }
157}
158
159void CodeOrigin::dumpInContext(PrintStream& out, DumpContext*) const
160{
161 dump(out);
162}
163
164} // namespace JSC
165