GC constraint solving should be parallel
[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, HeapVersion newlyAllocatedVersion, TinyBloomFilter filter,
50         void* passedPointer, const Func& func)
51     {
52         const HashSet<MarkedBlock*>& set = heap.objectSpace().blocks().set();
53         
54         ASSERT(heap.objectSpace().isMarking());
55         static const bool isMarking = true;
56         
57         char* pointer = static_cast<char*>(passedPointer);
58         
59         // It could point to a large allocation.
60         if (heap.objectSpace().largeAllocationsForThisCollectionSize()) {
61             if (heap.objectSpace().largeAllocationsForThisCollectionBegin()[0]->aboveLowerBound(pointer)
62                 && heap.objectSpace().largeAllocationsForThisCollectionEnd()[-1]->belowUpperBound(pointer)) {
63                 LargeAllocation** result = approximateBinarySearch<LargeAllocation*>(
64                     heap.objectSpace().largeAllocationsForThisCollectionBegin(),
65                     heap.objectSpace().largeAllocationsForThisCollectionSize(),
66                     LargeAllocation::fromCell(pointer),
67                     [] (LargeAllocation** ptr) -> LargeAllocation* { return *ptr; });
68                 if (result) {
69                     if (result > heap.objectSpace().largeAllocationsForThisCollectionBegin()
70                         && result[-1]->contains(pointer))
71                         func(result[-1]->cell());
72                     if (result[0]->contains(pointer))
73                         func(result[0]->cell());
74                     if (result + 1 < heap.objectSpace().largeAllocationsForThisCollectionEnd()
75                         && result[1]->contains(pointer))
76                         func(result[1]->cell());
77                 }
78             }
79         }
80     
81         MarkedBlock* candidate = MarkedBlock::blockFor(pointer);
82         // It's possible for a butterfly pointer to point past the end of a butterfly. Check this now.
83         if (pointer <= bitwise_cast<char*>(candidate) + sizeof(IndexingHeader)) {
84             // We may be interested in the last cell of the previous MarkedBlock.
85             char* previousPointer = pointer - sizeof(IndexingHeader) - 1;
86             MarkedBlock* previousCandidate = MarkedBlock::blockFor(previousPointer);
87             if (!filter.ruleOut(bitwise_cast<Bits>(previousCandidate))
88                 && set.contains(previousCandidate)
89                 && previousCandidate->handle().cellKind() == HeapCell::Auxiliary) {
90                 previousPointer = static_cast<char*>(previousCandidate->handle().cellAlign(previousPointer));
91                 if (previousCandidate->handle().isLiveCell(markingVersion, newlyAllocatedVersion, isMarking, previousPointer))
92                     func(previousPointer);
93             }
94         }
95     
96         if (filter.ruleOut(bitwise_cast<Bits>(candidate))) {
97             ASSERT(!candidate || !set.contains(candidate));
98             return;
99         }
100     
101         if (!set.contains(candidate))
102             return;
103         
104         auto tryPointer = [&] (void* pointer) {
105             if (candidate->handle().isLiveCell(markingVersion, newlyAllocatedVersion, isMarking, pointer))
106                 func(pointer);
107         };
108     
109         if (candidate->handle().cellKind() == HeapCell::JSCell) {
110             if (!MarkedBlock::isAtomAligned(pointer))
111                 return;
112         
113             tryPointer(pointer);
114             return;
115         }
116     
117         // A butterfly could point into the middle of an object.
118         char* alignedPointer = static_cast<char*>(candidate->handle().cellAlign(pointer));
119         tryPointer(alignedPointer);
120     
121         // Also, a butterfly could point at the end of an object plus sizeof(IndexingHeader). In that
122         // case, this is pointing to the object to the right of the one we should be marking.
123         if (candidate->atomNumber(alignedPointer) > MarkedBlock::firstAtom()
124             && pointer <= alignedPointer + sizeof(IndexingHeader))
125             tryPointer(alignedPointer - candidate->cellSize());
126     }
127     
128     static bool isPointerGCObjectJSCell(
129         Heap& heap, TinyBloomFilter filter, const void* pointer)
130     {
131         // It could point to a large allocation.
132         const Vector<LargeAllocation*>& largeAllocations = heap.objectSpace().largeAllocations();
133         if (!largeAllocations.isEmpty()) {
134             if (largeAllocations[0]->aboveLowerBound(pointer)
135                 && largeAllocations.last()->belowUpperBound(pointer)) {
136                 LargeAllocation*const* result = approximateBinarySearch<LargeAllocation*const>(
137                     largeAllocations.begin(), largeAllocations.size(),
138                     LargeAllocation::fromCell(pointer),
139                     [] (LargeAllocation*const* ptr) -> LargeAllocation* { return *ptr; });
140                 if (result) {
141                     if (result > largeAllocations.begin()
142                         && result[-1]->cell() == pointer
143                         && result[-1]->attributes().cellKind == HeapCell::JSCell)
144                         return true;
145                     if (result[0]->cell() == pointer
146                         && result[0]->attributes().cellKind == HeapCell::JSCell)
147                         return true;
148                     if (result + 1 < largeAllocations.end()
149                         && result[1]->cell() == pointer
150                         && result[1]->attributes().cellKind == HeapCell::JSCell)
151                         return true;
152                 }
153             }
154         }
155     
156         const HashSet<MarkedBlock*>& set = heap.objectSpace().blocks().set();
157         
158         MarkedBlock* candidate = MarkedBlock::blockFor(pointer);
159         if (filter.ruleOut(bitwise_cast<Bits>(candidate))) {
160             ASSERT(!candidate || !set.contains(candidate));
161             return false;
162         }
163         
164         if (!MarkedBlock::isAtomAligned(pointer))
165             return false;
166         
167         if (!set.contains(candidate))
168             return false;
169         
170         if (candidate->handle().cellKind() != HeapCell::JSCell)
171             return false;
172         
173         if (!candidate->handle().isLiveCell(pointer))
174             return false;
175         
176         return true;
177     }
178     
179     static bool isValueGCObject(
180         Heap& heap, TinyBloomFilter filter, JSValue value)
181     {
182         if (!value.isCell())
183             return false;
184         return isPointerGCObjectJSCell(heap, filter, static_cast<void*>(value.asCell()));
185     }
186 };
187
188 } // namespace JSC
189