1/*
2 * Copyright (C) 2015-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#pragma once
27
28#include "InternalFunctionAllocationProfile.h"
29#include "JSCast.h"
30#include "ObjectAllocationProfile.h"
31#include "PackedCellPtr.h"
32#include "Watchpoint.h"
33
34namespace JSC {
35
36class JSGlobalObject;
37class LLIntOffsetsExtractor;
38namespace DFG {
39class SpeculativeJIT;
40class JITCompiler;
41}
42
43class FunctionRareData final : public JSCell {
44 friend class JIT;
45 friend class DFG::SpeculativeJIT;
46 friend class DFG::JITCompiler;
47 friend class VM;
48
49public:
50 typedef JSCell Base;
51 static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
52
53 static FunctionRareData* create(VM&);
54
55 static const bool needsDestruction = true;
56 static void destroy(JSCell*);
57
58 static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype);
59
60 static void visitChildren(JSCell*, SlotVisitor&);
61
62 DECLARE_INFO;
63
64 static inline ptrdiff_t offsetOfObjectAllocationProfile()
65 {
66 return OBJECT_OFFSETOF(FunctionRareData, m_objectAllocationProfile);
67 }
68
69 ObjectAllocationProfileWithPrototype* objectAllocationProfile()
70 {
71 return &m_objectAllocationProfile;
72 }
73
74 Structure* objectAllocationStructure() { return m_objectAllocationProfile.structure(); }
75 JSObject* objectAllocationPrototype() { return m_objectAllocationProfile.prototype(); }
76
77 InlineWatchpointSet& allocationProfileWatchpointSet()
78 {
79 return m_objectAllocationProfileWatchpoint;
80 }
81
82 void clear(const char* reason);
83
84 void initializeObjectAllocationProfile(VM&, JSGlobalObject*, JSObject* prototype, size_t inlineCapacity, JSFunction* constructor);
85
86 bool isObjectAllocationProfileInitialized() { return !m_objectAllocationProfile.isNull(); }
87
88 Structure* internalFunctionAllocationStructure() { return m_internalFunctionAllocationProfile.structure(); }
89 Structure* createInternalFunctionAllocationStructureFromBase(VM& vm, JSGlobalObject* globalObject, JSObject* prototype, Structure* baseStructure)
90 {
91 return m_internalFunctionAllocationProfile.createAllocationStructureFromBase(vm, globalObject, this, prototype, baseStructure);
92 }
93 void clearInternalFunctionAllocationProfile()
94 {
95 m_internalFunctionAllocationProfile.clear();
96 }
97
98 Structure* getBoundFunctionStructure() { return m_boundFunctionStructure.get(); }
99 void setBoundFunctionStructure(VM& vm, Structure* structure) { m_boundFunctionStructure.set(vm, this, structure); }
100
101 bool hasReifiedLength() const { return m_hasReifiedLength; }
102 void setHasReifiedLength() { m_hasReifiedLength = true; }
103 bool hasReifiedName() const { return m_hasReifiedName; }
104 void setHasReifiedName() { m_hasReifiedName = true; }
105
106 bool hasAllocationProfileClearingWatchpoint() const { return !!m_allocationProfileClearingWatchpoint; }
107 Watchpoint* createAllocationProfileClearingWatchpoint()
108 {
109 RELEASE_ASSERT(!hasAllocationProfileClearingWatchpoint());
110 m_allocationProfileClearingWatchpoint = std::make_unique<AllocationProfileClearingWatchpoint>(this);
111 return m_allocationProfileClearingWatchpoint.get();
112 }
113
114 class AllocationProfileClearingWatchpoint final : public Watchpoint {
115 public:
116 AllocationProfileClearingWatchpoint(FunctionRareData* rareData)
117 : Watchpoint(Watchpoint::Type::FunctionRareDataAllocationProfileClearing)
118 , m_rareData(rareData)
119 { }
120
121 void fireInternal(VM&, const FireDetail&);
122
123 private:
124 // Own destructor may not be called. Keep members trivially destructible.
125 JSC_WATCHPOINT_FIELD(PackedCellPtr<FunctionRareData>, m_rareData);
126 };
127
128protected:
129 FunctionRareData(VM&);
130 ~FunctionRareData();
131
132private:
133 friend class LLIntOffsetsExtractor;
134
135 // Ideally, there would only be one allocation profile for subclassing but due to Reflect.construct we
136 // have two. There are some pros and cons in comparison to our current system to using the same profile
137 // for both JS constructors and subclasses of builtin constructors:
138 //
139 // 1) + Uses less memory.
140 // 2) + Conceptually simplier as there is only one profile.
141 // 3) - We would need a check in all JSFunction object creations (both with classes and without) that the
142 // new.target's profiled structure has a JSFinalObject ClassInfo. This is needed, for example, if we have
143 // `Reflect.construct(Array, args, myConstructor)` since myConstructor will be the new.target of Array
144 // the Array constructor will set the allocation profile of myConstructor to hold an Array structure
145 //
146 // We don't really care about 1) since this memory is rare and small in total. 2) is unfortunate but is
147 // probably outweighed by the cost of 3).
148 ObjectAllocationProfileWithPrototype m_objectAllocationProfile;
149 InlineWatchpointSet m_objectAllocationProfileWatchpoint;
150 InternalFunctionAllocationProfile m_internalFunctionAllocationProfile;
151 WriteBarrier<Structure> m_boundFunctionStructure;
152 std::unique_ptr<AllocationProfileClearingWatchpoint> m_allocationProfileClearingWatchpoint;
153 bool m_hasReifiedLength { false };
154 bool m_hasReifiedName { false };
155};
156
157} // namespace JSC
158