1/*
2 * Copyright (C) 2011, 2016 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ConservativeRoots.h"
28
29#include "CodeBlock.h"
30#include "CodeBlockSetInlines.h"
31#include "HeapInlines.h"
32#include "HeapUtil.h"
33#include "JITStubRoutineSet.h"
34#include "JSCast.h"
35#include "JSObject.h"
36#include "JSCInlines.h"
37#include "MarkedBlockInlines.h"
38#include "Structure.h"
39#include <wtf/OSAllocator.h>
40
41namespace JSC {
42
43ConservativeRoots::ConservativeRoots(Heap& heap)
44 : m_roots(m_inlineRoots)
45 , m_size(0)
46 , m_capacity(inlineCapacity)
47 , m_heap(heap)
48{
49}
50
51ConservativeRoots::~ConservativeRoots()
52{
53 if (m_roots != m_inlineRoots)
54 OSAllocator::decommitAndRelease(m_roots, m_capacity * sizeof(HeapCell*));
55}
56
57void ConservativeRoots::grow()
58{
59 size_t newCapacity = m_capacity == inlineCapacity ? nonInlineCapacity : m_capacity * 2;
60 HeapCell** newRoots = static_cast<HeapCell**>(OSAllocator::reserveAndCommit(newCapacity * sizeof(HeapCell*)));
61 memcpy(newRoots, m_roots, m_size * sizeof(HeapCell*));
62 if (m_roots != m_inlineRoots)
63 OSAllocator::decommitAndRelease(m_roots, m_capacity * sizeof(HeapCell*));
64 m_capacity = newCapacity;
65 m_roots = newRoots;
66}
67
68template<typename MarkHook>
69inline void ConservativeRoots::genericAddPointer(void* p, HeapVersion markingVersion, HeapVersion newlyAllocatedVersion, TinyBloomFilter filter, MarkHook& markHook)
70{
71 p = removeArrayPtrTag(p);
72 markHook.mark(p);
73
74 HeapUtil::findGCObjectPointersForMarking(
75 m_heap, markingVersion, newlyAllocatedVersion, filter, p,
76 [&] (void* p, HeapCell::Kind cellKind) {
77 if (isJSCellKind(cellKind))
78 markHook.markKnownJSCell(static_cast<JSCell*>(p));
79
80 if (m_size == m_capacity)
81 grow();
82
83 m_roots[m_size++] = bitwise_cast<HeapCell*>(p);
84 });
85}
86
87template<typename MarkHook>
88SUPPRESS_ASAN
89void ConservativeRoots::genericAddSpan(void* begin, void* end, MarkHook& markHook)
90{
91 if (begin > end) {
92 void* swapTemp = begin;
93 begin = end;
94 end = swapTemp;
95 }
96
97 RELEASE_ASSERT(isPointerAligned(begin));
98 RELEASE_ASSERT(isPointerAligned(end));
99
100 TinyBloomFilter filter = m_heap.objectSpace().blocks().filter(); // Make a local copy of filter to show the compiler it won't alias, and can be register-allocated.
101 HeapVersion markingVersion = m_heap.objectSpace().markingVersion();
102 HeapVersion newlyAllocatedVersion = m_heap.objectSpace().newlyAllocatedVersion();
103 for (char** it = static_cast<char**>(begin); it != static_cast<char**>(end); ++it)
104 genericAddPointer(*it, markingVersion, newlyAllocatedVersion, filter, markHook);
105}
106
107class DummyMarkHook {
108public:
109 void mark(void*) { }
110 void markKnownJSCell(JSCell*) { }
111};
112
113void ConservativeRoots::add(void* begin, void* end)
114{
115 DummyMarkHook dummy;
116 genericAddSpan(begin, end, dummy);
117}
118
119class CompositeMarkHook {
120public:
121 CompositeMarkHook(JITStubRoutineSet& stubRoutines, CodeBlockSet& codeBlocks, const AbstractLocker& locker)
122 : m_stubRoutines(stubRoutines)
123 , m_codeBlocks(codeBlocks)
124 , m_codeBlocksLocker(locker)
125 {
126 }
127
128 void mark(void* address)
129 {
130 m_stubRoutines.mark(address);
131 }
132
133 void markKnownJSCell(JSCell* cell)
134 {
135 if (cell->type() == CodeBlockType)
136 m_codeBlocks.mark(m_codeBlocksLocker, jsCast<CodeBlock*>(cell));
137 }
138
139private:
140 JITStubRoutineSet& m_stubRoutines;
141 CodeBlockSet& m_codeBlocks;
142 const AbstractLocker& m_codeBlocksLocker;
143};
144
145void ConservativeRoots::add(
146 void* begin, void* end, JITStubRoutineSet& jitStubRoutines, CodeBlockSet& codeBlocks)
147{
148 LockHolder locker(codeBlocks.getLock());
149 CompositeMarkHook markHook(jitStubRoutines, codeBlocks, locker);
150 genericAddSpan(begin, end, markHook);
151}
152
153} // namespace JSC
154