9280c9f448e4be66cd09d6b1ce11328e6e81121a
[WebKit-https.git] / Source / JavaScriptCore / heap / HeapUtil.h
1 /*
2  * Copyright (C) 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 #pragma once
27
28 namespace JSC {
29
30 // Are you tired of waiting for all of WebKit to build because you changed the implementation of a
31 // function in HeapInlines.h?  Does it bother you that you're waiting on rebuilding the JS DOM
32 // bindings even though your change is in a function called from only 2 .cpp files?  Then HeapUtil.h
33 // is for you!  Everything in this class should be a static method that takes a Heap& if needed.
34 // This is a friend of Heap, so you can access all of Heap's privates.
35 //
36 // This ends up being an issue because Heap exposes a lot of methods that ought to be inline for
37 // performance or that must be inline because they are templates.  This class ought to contain
38 // methods that are used for the implementation of the collector, or for unusual clients that need
39 // to reach deep into the collector for some reason.  Don't put things in here that would cause you
40 // to have to include it from more than a handful of places, since that would defeat the purpose.
41 // This class isn't here to look pretty.  It's to let us hack the GC more easily!
42
43 class HeapUtil {
44 public:
45     // This function must be run after stopAllocation() is called and 
46     // before liveness data is cleared to be accurate.
47     template<typename Func>
48     static void findGCObjectPointersForMarking(
49         Heap& heap, HeapVersion markingVersion, TinyBloomFilter filter, void* passedPointer,
50         const Func& func)
51     {
52         const HashSet<MarkedBlock*>& set = heap.objectSpace().blocks().set();
53         
54         char* pointer = static_cast<char*>(passedPointer);
55         
56         // It could point to a large allocation.
57         if (heap.objectSpace().largeAllocationsForThisCollectionSize()) {
58             if (heap.objectSpace().largeAllocationsForThisCollectionBegin()[0]->aboveLowerBound(pointer)
59                 && heap.objectSpace().largeAllocationsForThisCollectionEnd()[-1]->belowUpperBound(pointer)) {
60                 LargeAllocation** result = approximateBinarySearch<LargeAllocation*>(
61                     heap.objectSpace().largeAllocationsForThisCollectionBegin(),
62                     heap.objectSpace().largeAllocationsForThisCollectionSize(),
63                     LargeAllocation::fromCell(pointer),
64                     [] (LargeAllocation** ptr) -> LargeAllocation* { return *ptr; });
65                 if (result) {
66                     if (result > heap.objectSpace().largeAllocationsForThisCollectionBegin()
67                         && result[-1]->contains(pointer))
68                         func(result[-1]->cell());
69                     if (result[0]->contains(pointer))
70                         func(result[0]->cell());
71                     if (result + 1 < heap.objectSpace().largeAllocationsForThisCollectionEnd()
72                         && result[1]->contains(pointer))
73                         func(result[1]->cell());
74                 }
75             }
76         }
77     
78         MarkedBlock* candidate = MarkedBlock::blockFor(pointer);
79         // It's possible for a butterfly pointer to point past the end of a butterfly. Check this now.
80         if (pointer <= bitwise_cast<char*>(candidate) + sizeof(IndexingHeader)) {
81             // We may be interested in the last cell of the previous MarkedBlock.
82             char* previousPointer = pointer - sizeof(IndexingHeader) - 1;
83             MarkedBlock* previousCandidate = MarkedBlock::blockFor(previousPointer);
84             if (!filter.ruleOut(bitwise_cast<Bits>(previousCandidate))
85                 && set.contains(previousCandidate)
86                 && previousCandidate->handle().cellKind() == HeapCell::Auxiliary) {
87                 previousPointer = static_cast<char*>(previousCandidate->handle().cellAlign(previousPointer));
88                 if (previousCandidate->handle().isLiveCell(markingVersion, previousPointer))
89                     func(previousPointer);
90             }
91         }
92     
93         if (filter.ruleOut(bitwise_cast<Bits>(candidate))) {
94             ASSERT(!candidate || !set.contains(candidate));
95             return;
96         }
97     
98         if (!set.contains(candidate))
99             return;
100         
101         auto tryPointer = [&] (void* pointer) {
102             if (candidate->handle().isLiveCell(markingVersion, pointer))
103                 func(pointer);
104         };
105     
106         if (candidate->handle().cellKind() == HeapCell::JSCell) {
107             if (!MarkedBlock::isAtomAligned(pointer))
108                 return;
109         
110             tryPointer(pointer);
111             return;
112         }
113     
114         // A butterfly could point into the middle of an object.
115         char* alignedPointer = static_cast<char*>(candidate->handle().cellAlign(pointer));
116         tryPointer(alignedPointer);
117     
118         // Also, a butterfly could point at the end of an object plus sizeof(IndexingHeader). In that
119         // case, this is pointing to the object to the right of the one we should be marking.
120         if (candidate->atomNumber(alignedPointer) > MarkedBlock::firstAtom()
121             && pointer <= alignedPointer + sizeof(IndexingHeader))
122             tryPointer(alignedPointer - candidate->cellSize());
123     }
124     
125     static bool isPointerGCObjectJSCell(
126         Heap& heap, TinyBloomFilter filter, const void* pointer)
127     {
128         // It could point to a large allocation.
129         const Vector<LargeAllocation*>& largeAllocations = heap.objectSpace().largeAllocations();
130         if (!largeAllocations.isEmpty()) {
131             if (largeAllocations[0]->aboveLowerBound(pointer)
132                 && largeAllocations.last()->belowUpperBound(pointer)) {
133                 LargeAllocation*const* result = approximateBinarySearch<LargeAllocation*const>(
134                     largeAllocations.begin(), largeAllocations.size(),
135                     LargeAllocation::fromCell(pointer),
136                     [] (LargeAllocation*const* ptr) -> LargeAllocation* { return *ptr; });
137                 if (result) {
138                     if (result > largeAllocations.begin()
139                         && result[-1]->cell() == pointer
140                         && result[-1]->attributes().cellKind == HeapCell::JSCell)
141                         return true;
142                     if (result[0]->cell() == pointer
143                         && result[0]->attributes().cellKind == HeapCell::JSCell)
144                         return true;
145                     if (result + 1 < largeAllocations.end()
146                         && result[1]->cell() == pointer
147                         && result[1]->attributes().cellKind == HeapCell::JSCell)
148                         return true;
149                 }
150             }
151         }
152     
153         const HashSet<MarkedBlock*>& set = heap.objectSpace().blocks().set();
154         
155         MarkedBlock* candidate = MarkedBlock::blockFor(pointer);
156         if (filter.ruleOut(bitwise_cast<Bits>(candidate))) {
157             ASSERT(!candidate || !set.contains(candidate));
158             return false;
159         }
160         
161         if (!MarkedBlock::isAtomAligned(pointer))
162             return false;
163         
164         if (!set.contains(candidate))
165             return false;
166         
167         if (candidate->handle().cellKind() != HeapCell::JSCell)
168             return false;
169         
170         if (!candidate->handle().isLiveCell(pointer))
171             return false;
172         
173         return true;
174     }
175     
176     static bool isValueGCObject(
177         Heap& heap, TinyBloomFilter filter, JSValue value)
178     {
179         if (!value.isCell())
180             return false;
181         return isPointerGCObjectJSCell(heap, filter, static_cast<void*>(value.asCell()));
182     }
183 };
184
185 } // namespace JSC
186