/
PathFinder.kt
693 lines (634 loc) · 23.8 KB
/
PathFinder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package shark.internal
import shark.GcRoot
import shark.GcRoot.JavaFrame
import shark.GcRoot.JniGlobal
import shark.GcRoot.ThreadObject
import shark.HeapGraph
import shark.HeapObject
import shark.HeapObject.HeapClass
import shark.HeapObject.HeapInstance
import shark.HeapObject.HeapObjectArray
import shark.HeapObject.HeapPrimitiveArray
import shark.IgnoredReferenceMatcher
import shark.LeakReference
import shark.LeakTraceElement.Type.ARRAY_ENTRY
import shark.LeakTraceElement.Type.INSTANCE_FIELD
import shark.LeakTraceElement.Type.LOCAL
import shark.LeakTraceElement.Type.STATIC_FIELD
import shark.LibraryLeakReferenceMatcher
import shark.OnAnalysisProgressListener
import shark.OnAnalysisProgressListener.Step.FINDING_DOMINATORS
import shark.OnAnalysisProgressListener.Step.FINDING_PATHS_TO_LEAKING_OBJECTS
import shark.PrimitiveType.INT
import shark.ReferenceMatcher
import shark.ReferencePattern
import shark.ReferencePattern.InstanceFieldPattern
import shark.ReferencePattern.NativeGlobalVariablePattern
import shark.ReferencePattern.StaticFieldPattern
import shark.SharkLog
import shark.ValueHolder
import shark.internal.ReferencePathNode.ChildNode.LibraryLeakChildNode
import shark.internal.ReferencePathNode.ChildNode.NormalNode
import shark.internal.ReferencePathNode.LibraryLeakNode
import shark.internal.ReferencePathNode.RootNode
import shark.internal.ReferencePathNode.RootNode.LibraryLeakRootNode
import shark.internal.ReferencePathNode.RootNode.NormalRootNode
import shark.internal.hppc.LongLongScatterMap
import shark.internal.hppc.LongScatterSet
import java.util.ArrayDeque
import java.util.Deque
import java.util.LinkedHashMap
/**
* Not thread safe.
*
* Finds the shortest path from leaking references to a gc root, first ignoring references
* identified as "to visit last" and then visiting them as needed if no path is
* found.
*/
@Suppress("TooManyFunctions")
internal class PathFinder(
private val graph: HeapGraph,
private val listener: OnAnalysisProgressListener,
referenceMatchers: List<ReferenceMatcher>
) {
class PathFindingResults(
val pathsToLeakingObjects: List<ReferencePathNode>,
val dominatedObjectIds: LongLongScatterMap
)
private class State(
val leakingObjectIds: Set<Long>,
val sizeOfObjectInstances: Int,
val computeRetainedHeapSize: Boolean
) {
/** Set of objects to visit */
val toVisitQueue: Deque<ReferencePathNode> = ArrayDeque()
/**
* Objects to visit when [toVisitQueue] is empty. Should contain [JavaFrame] gc roots first,
* then [LibraryLeakNode].
*/
val toVisitLastQueue: Deque<ReferencePathNode> = ArrayDeque()
/**
* Enables fast checking of whether a node is already in the queue.
*/
val toVisitSet = HashSet<Long>()
val toVisitLastSet = HashSet<Long>()
val visitedSet = LongScatterSet()
/**
* Map of objects to their leaking dominator.
* If an object has been added to [toVisitSet] or [visitedSet] and is missing from
* [dominatedObjectIds] then it's considered "undomitable" ie it is dominated by gc roots
* and cannot be dominated by a leaking object.
*/
val dominatedObjectIds = LongLongScatterMap()
val queuesNotEmpty: Boolean
get() = toVisitQueue.isNotEmpty() || toVisitLastQueue.isNotEmpty()
}
private val fieldNameByClassName: Map<String, Map<String, ReferenceMatcher>>
private val staticFieldNameByClassName: Map<String, Map<String, ReferenceMatcher>>
private val threadNameReferenceMatchers: Map<String, ReferenceMatcher>
private val jniGlobalReferenceMatchers: Map<String, ReferenceMatcher>
init {
val fieldNameByClassName = mutableMapOf<String, MutableMap<String, ReferenceMatcher>>()
val staticFieldNameByClassName = mutableMapOf<String, MutableMap<String, ReferenceMatcher>>()
val threadNames = mutableMapOf<String, ReferenceMatcher>()
val jniGlobals = mutableMapOf<String, ReferenceMatcher>()
referenceMatchers.filter {
(it is IgnoredReferenceMatcher || (it is LibraryLeakReferenceMatcher && it.patternApplies(
graph
)))
}
.forEach { referenceMatcher ->
when (val pattern = referenceMatcher.pattern) {
is ReferencePattern.JavaLocalPattern -> {
threadNames[pattern.threadName] = referenceMatcher
}
is StaticFieldPattern -> {
val mapOrNull = staticFieldNameByClassName[pattern.className]
val map = if (mapOrNull != null) mapOrNull else {
val newMap = mutableMapOf<String, ReferenceMatcher>()
staticFieldNameByClassName[pattern.className] = newMap
newMap
}
map[pattern.fieldName] = referenceMatcher
}
is InstanceFieldPattern -> {
val mapOrNull = fieldNameByClassName[pattern.className]
val map = if (mapOrNull != null) mapOrNull else {
val newMap = mutableMapOf<String, ReferenceMatcher>()
fieldNameByClassName[pattern.className] = newMap
newMap
}
map[pattern.fieldName] = referenceMatcher
}
is NativeGlobalVariablePattern -> {
jniGlobals[pattern.className] = referenceMatcher
}
}
}
this.fieldNameByClassName = fieldNameByClassName
this.staticFieldNameByClassName = staticFieldNameByClassName
this.threadNameReferenceMatchers = threadNames
this.jniGlobalReferenceMatchers = jniGlobals
}
fun findPathsFromGcRoots(
leakingObjectIds: Set<Long>,
computeRetainedHeapSize: Boolean
): PathFindingResults {
listener.onAnalysisProgress(FINDING_PATHS_TO_LEAKING_OBJECTS)
val sizeOfObjectInstances = determineSizeOfObjectInstances(graph)
val state = State(leakingObjectIds, sizeOfObjectInstances, computeRetainedHeapSize)
return state.findPathsFromGcRoots()
}
private fun determineSizeOfObjectInstances(graph: HeapGraph): Int {
val objectClass = graph.findClassByName("java.lang.Object")
return if (objectClass != null) {
// In Android 16 ClassDumpRecord.instanceSize for java.lang.Object can be 8 yet there are 0
// fields. This is likely because there is extra per instance data that isn't coming from
// fields in the Object class. See #1374
val objectClassFieldSize = objectClass.readFieldsByteSize()
// shadow$_klass_ (object id) + shadow$_monitor_ (Int)
val sizeOfObjectOnArt = graph.identifierByteSize + INT.byteSize
if (objectClassFieldSize == sizeOfObjectOnArt) {
sizeOfObjectOnArt
} else {
0
}
} else {
0
}
}
private fun State.findPathsFromGcRoots(): PathFindingResults {
enqueueGcRoots()
val shortestPathsToLeakingObjects = mutableListOf<ReferencePathNode>()
visitingQueue@ while (queuesNotEmpty) {
val node = poll()
if (checkSeen(node)) {
throw IllegalStateException(
"Node $node objectId=${node.objectId} should not be enqueued when already visited or enqueued"
)
}
if (node.objectId in leakingObjectIds) {
shortestPathsToLeakingObjects.add(node)
// Found all refs, stop searching (unless computing retained size)
if (shortestPathsToLeakingObjects.size == leakingObjectIds.size) {
if (computeRetainedHeapSize) {
listener.onAnalysisProgress(FINDING_DOMINATORS)
} else {
break@visitingQueue
}
}
}
when (val heapObject = graph.findObjectById(node.objectId)) {
is HeapClass -> visitClassRecord(heapObject, node)
is HeapInstance -> visitInstance(heapObject, node)
is HeapObjectArray -> visitObjectArray(heapObject, node)
}
}
return PathFindingResults(shortestPathsToLeakingObjects, dominatedObjectIds)
}
private fun State.poll(): ReferencePathNode {
return if (!toVisitQueue.isEmpty()) {
val removedNode = toVisitQueue.poll()
toVisitSet.remove(removedNode.objectId)
removedNode
} else {
val removedNode = toVisitLastQueue.poll()
toVisitLastSet.remove(removedNode.objectId)
removedNode
}
}
private fun State.checkSeen(node: ReferencePathNode): Boolean {
val neverSeen = visitedSet.add(node.objectId)
return !neverSeen
}
private fun State.enqueueGcRoots() {
val gcRoots = sortedGcRoots()
val threadNames = mutableMapOf<HeapInstance, String>()
val threadsBySerialNumber = mutableMapOf<Int, Pair<HeapInstance, ThreadObject>>()
gcRoots.forEach { (objectRecord, gcRoot) ->
if (computeRetainedHeapSize) {
undominateWithSkips(gcRoot.id)
}
when (gcRoot) {
is ThreadObject -> {
threadsBySerialNumber[gcRoot.threadSerialNumber] = objectRecord.asInstance!! to gcRoot
enqueue(NormalRootNode(gcRoot.id, gcRoot))
}
is JavaFrame -> {
val (threadInstance, threadRoot) = threadsBySerialNumber.getValue(
gcRoot.threadSerialNumber
)
val threadName = threadNames[threadInstance] ?: {
val name = threadInstance[Thread::class, "name"]?.value?.readAsJavaString() ?: ""
threadNames[threadInstance] = name
name
}()
val referenceMatcher = threadNameReferenceMatchers[threadName]
if (referenceMatcher !is IgnoredReferenceMatcher) {
val rootNode = NormalRootNode(threadRoot.id, gcRoot)
// Unfortunately Android heap dumps do not include stack trace data, so
// JavaFrame.frameNumber is always -1 and we cannot know which method is causing the
// reference to be held.
val leakReference = LeakReference(LOCAL, "")
val childNode = if (referenceMatcher is LibraryLeakReferenceMatcher) {
LibraryLeakChildNode(gcRoot.id, rootNode, leakReference, referenceMatcher)
} else {
NormalNode(gcRoot.id, rootNode, leakReference)
}
enqueue(childNode)
}
}
is JniGlobal -> {
val referenceMatcher = when (objectRecord) {
is HeapClass -> jniGlobalReferenceMatchers[objectRecord.name]
is HeapInstance -> jniGlobalReferenceMatchers[objectRecord.instanceClassName]
is HeapObjectArray -> jniGlobalReferenceMatchers[objectRecord.arrayClassName]
is HeapPrimitiveArray -> jniGlobalReferenceMatchers[objectRecord.arrayClassName]
}
if (referenceMatcher !is IgnoredReferenceMatcher) {
if (referenceMatcher is LibraryLeakReferenceMatcher)
enqueue(LibraryLeakRootNode(gcRoot.id, gcRoot, referenceMatcher))
} else {
enqueue(NormalRootNode(gcRoot.id, gcRoot))
}
}
else -> enqueue(NormalRootNode(gcRoot.id, gcRoot))
}
}
}
/**
* Sorting GC roots to get stable shortest path
* Once sorted all ThreadObject Gc Roots are located before JavaLocalPattern Gc Roots.
* This ensures ThreadObjects are visited before JavaFrames, and threadsBySerialNumber can be
* built before JavaFrames.
*/
private fun sortedGcRoots(): List<Pair<HeapObject, GcRoot>> {
val rootClassName: (HeapObject) -> String = { graphObject ->
when (graphObject) {
is HeapClass -> {
graphObject.name
}
is HeapInstance -> {
graphObject.instanceClassName
}
is HeapObjectArray -> {
graphObject.arrayClassName
}
is HeapPrimitiveArray -> {
graphObject.arrayClassName
}
}
}
return graph.gcRoots
.filter { gcRoot ->
// GC roots sometimes reference objects that don't exist in the heap dump
// See https://github.com/square/leakcanary/issues/1516
val objectExists = graph.objectExists(gcRoot.id)
if (!objectExists) {
SharkLog.d {
"${gcRoot::class.java.simpleName} gc root ignored because it's pointing to unknown object @${gcRoot.id}"
}
}
objectExists
}
.map { graph.findObjectById(it.id) to it }
.sortedWith(Comparator { (graphObject1, root1), (graphObject2, root2) ->
// Sorting based on pattern name first. In reverse order so that ThreadObject is before JavaLocalPattern
val gcRootTypeComparison = root2::class.java.name.compareTo(root1::class.java.name)
if (gcRootTypeComparison != 0) {
gcRootTypeComparison
} else {
rootClassName(graphObject1).compareTo(rootClassName(graphObject2))
}
})
}
private fun State.visitClassRecord(
heapClass: HeapClass,
parent: ReferencePathNode
) {
val ignoredStaticFields = staticFieldNameByClassName[heapClass.name] ?: emptyMap()
for (staticField in heapClass.readStaticFields()) {
if (!staticField.value.isNonNullReference) {
continue
}
val fieldName = staticField.name
if (fieldName == "\$staticOverhead") {
continue
}
val objectId = staticField.value.asObjectId!!
if (computeRetainedHeapSize) {
undominateWithSkips(objectId)
}
val node = when (val referenceMatcher = ignoredStaticFields[fieldName]) {
null -> NormalNode(objectId, parent, LeakReference(STATIC_FIELD, fieldName))
is LibraryLeakReferenceMatcher -> LibraryLeakChildNode(
objectId, parent, LeakReference(STATIC_FIELD, fieldName), referenceMatcher
)
is IgnoredReferenceMatcher -> null
}
if (node != null) {
enqueue(node)
}
}
}
private fun State.visitInstance(
instance: HeapInstance,
parent: ReferencePathNode
) {
val fieldReferenceMatchers = LinkedHashMap<String, ReferenceMatcher>()
instance.instanceClass.classHierarchy.forEach {
val referenceMatcherByField = fieldNameByClassName[it.name]
if (referenceMatcherByField != null) {
for ((fieldName, referenceMatcher) in referenceMatcherByField) {
if (!fieldReferenceMatchers.containsKey(fieldName)) {
fieldReferenceMatchers[fieldName] = referenceMatcher
}
}
}
}
val threadLocalValuesMatcher = if (instance instanceOf "java.lang.Thread") {
val threadName = instance["java.lang.Thread", "name"]?.value?.readAsJavaString()
threadNameReferenceMatchers[threadName]
} else null
val fieldNamesAndValues = instance.readFields()
.filter { it.value.isNonNullReference }
.toMutableList()
fieldNamesAndValues.sortBy { it.name }
fieldNamesAndValues.forEach { field ->
val objectId = field.value.asObjectId!!
if (computeRetainedHeapSize) {
updateDominatorWithSkips(parent.objectId, objectId)
}
val node =
if (threadLocalValuesMatcher != null && field.declaringClass.name == "java.lang.Thread" && field.name == "localValues") {
// Earlier Android versions store local references in a Thread.localValues field.
if (threadLocalValuesMatcher is LibraryLeakReferenceMatcher) {
LibraryLeakChildNode(
objectId, parent, LeakReference(INSTANCE_FIELD, field.name),
threadLocalValuesMatcher
)
} else {
null
}
} else when (val referenceMatcher = fieldReferenceMatchers[field.name]) {
null -> NormalNode(objectId, parent, LeakReference(INSTANCE_FIELD, field.name))
is LibraryLeakReferenceMatcher ->
LibraryLeakChildNode(
objectId, parent, LeakReference(INSTANCE_FIELD, field.name), referenceMatcher
)
is IgnoredReferenceMatcher -> null
}
if (node != null) {
enqueue(node)
}
}
}
private fun State.visitObjectArray(
objectArray: HeapObjectArray,
parent: ReferencePathNode
) {
val record = objectArray.readRecord()
val nonNullElementIds = record.elementIds.filter { objectId ->
objectId != ValueHolder.NULL_REFERENCE && graph.objectExists(objectId).apply {
if (!this) {
// dalvik.system.PathClassLoader.runtimeInternalObjects references objects which don't
// otherwise exist in the heap dump.
SharkLog.d { "Invalid Hprof? Found unknown object id $objectId" }
}
}
}
nonNullElementIds.forEachIndexed { index, elementId ->
if (computeRetainedHeapSize) {
updateDominatorWithSkips(parent.objectId, elementId)
}
val name = index.toString()
enqueue(NormalNode(elementId, parent, LeakReference(ARRAY_ENTRY, name)))
}
}
@Suppress("ReturnCount")
private fun State.enqueue(
node: ReferencePathNode
) {
if (node.objectId == ValueHolder.NULL_REFERENCE) {
return
}
if (visitedSet.contains(node.objectId)) {
return
}
// Already enqueued => shorter or equal distance
if (toVisitSet.contains(node.objectId)) {
return
}
val visitLast =
node is LibraryLeakNode ||
// We deprioritize thread objects because on Lollipop the thread local values are stored
// as a field.
(node is RootNode && node.gcRoot is ThreadObject) ||
(node is NormalNode && node.parent is RootNode && node.parent.gcRoot is JavaFrame)
if (toVisitLastSet.contains(node.objectId)) {
// Already enqueued => shorter or equal distance amongst library leak ref patterns.
if (visitLast) {
return
} else {
toVisitQueue.add(node)
toVisitSet.add(node.objectId)
val nodeToRemove = toVisitLastQueue.first { it.objectId == node.objectId }
toVisitLastQueue.remove(nodeToRemove)
toVisitLastSet.remove(node.objectId)
return
}
}
val isLeakingObject = node.objectId in leakingObjectIds
if (!isLeakingObject) {
val skip = when (val graphObject = graph.findObjectById(node.objectId)) {
is HeapClass -> false
is HeapInstance ->
when {
graphObject.isPrimitiveWrapper -> true
graphObject.instanceClassName == "java.lang.String" -> true
graphObject.instanceClass.instanceByteSize <= sizeOfObjectInstances -> true
else -> false
}
is HeapObjectArray -> when {
graphObject.isPrimitiveWrapperArray -> true
else -> false
}
is HeapPrimitiveArray -> true
}
if (skip) {
return
}
}
if (visitLast) {
toVisitLastQueue.add(node)
toVisitLastSet.add(node.objectId)
} else {
toVisitQueue.add(node)
toVisitSet.add(node.objectId)
}
}
private fun State.updateDominatorWithSkips(
parentObjectId: Long,
objectId: Long
) {
when (val graphObject = graph.findObjectById(objectId)) {
is HeapClass -> {
undominate(objectId, false)
}
is HeapInstance -> {
// String internal array is never enqueued
if (graphObject.instanceClassName == "java.lang.String") {
updateDominator(parentObjectId, objectId, true)
val valueId = graphObject["java.lang.String", "value"]?.value?.asObjectId
if (valueId != null) {
updateDominator(parentObjectId, valueId, true)
}
} else {
updateDominator(parentObjectId, objectId, false)
}
}
is HeapObjectArray -> {
// Primitive wrapper array elements are never enqueued
if (graphObject.isPrimitiveWrapperArray) {
updateDominator(parentObjectId, objectId, true)
for (wrapperId in graphObject.readRecord().elementIds) {
updateDominator(parentObjectId, wrapperId, true)
}
} else {
updateDominator(parentObjectId, objectId, false)
}
}
else -> {
updateDominator(parentObjectId, objectId, false)
}
}
}
@Suppress("ComplexCondition")
private fun State.updateDominator(
parent: Long,
objectId: Long,
neverEnqueued: Boolean
) {
val currentDominatorSlot = dominatedObjectIds.getSlot(objectId)
if (currentDominatorSlot == -1 && (objectId in visitedSet || objectId in toVisitSet || objectId in toVisitLastSet)) {
return
}
val parentDominatorSlot = dominatedObjectIds.getSlot(parent)
val parentIsRetainedObject = parent in leakingObjectIds
if (!parentIsRetainedObject && parentDominatorSlot == -1) {
// parent is not a retained instance and parent has no dominator, but it must have been
// visited therefore we know parent belongs to undominated.
if (neverEnqueued) {
visitedSet.add(objectId)
}
if (currentDominatorSlot != -1) {
dominatedObjectIds.remove(objectId)
}
return
}
val nextDominator =
if (parentIsRetainedObject) parent else dominatedObjectIds.getSlotValue(parentDominatorSlot)
if (currentDominatorSlot == -1) {
dominatedObjectIds[objectId] = nextDominator
} else {
val parentDominators = mutableListOf<Long>()
val currentDominators = mutableListOf<Long>()
var stop = false
var dominator: Long = nextDominator
while (!stop) {
parentDominators.add(dominator)
val nextDominatorSlot = dominatedObjectIds.getSlot(dominator)
if (nextDominatorSlot == -1) {
stop = true
} else {
dominator = dominatedObjectIds.getSlotValue(nextDominatorSlot)
}
}
stop = false
dominator = dominatedObjectIds.getSlotValue(currentDominatorSlot)
while (!stop) {
currentDominators.add(dominator)
val nextDominatorSlot = dominatedObjectIds.getSlot(dominator)
if (nextDominatorSlot == -1) {
stop = true
} else {
dominator = dominatedObjectIds.getSlotValue(nextDominatorSlot)
}
}
var sharedDominator: Long? = null
exit@ for (parentD in parentDominators) {
for (currentD in currentDominators) {
if (currentD == parentD) {
sharedDominator = currentD
break@exit
}
}
}
if (sharedDominator == null) {
dominatedObjectIds.remove(objectId)
if (neverEnqueued) {
visitedSet.add(objectId)
}
} else {
dominatedObjectIds[objectId] = sharedDominator
}
}
}
private fun State.undominateWithSkips(objectId: Long) {
when (val graphObject = graph.findObjectById(objectId)) {
is HeapClass -> {
undominate(objectId, false)
}
is HeapInstance -> {
// String internal array is never enqueued
if (graphObject.instanceClassName == "java.lang.String") {
undominate(objectId, true)
val valueId = graphObject["java.lang.String", "value"]?.value?.asObjectId
if (valueId != null) {
undominate(valueId, true)
}
} else {
undominate(objectId, false)
}
}
is HeapObjectArray -> {
// Primitive wrapper array elements are never enqueued
if (graphObject.isPrimitiveWrapperArray) {
undominate(objectId, true)
for (wrapperId in graphObject.readRecord().elementIds) {
undominate(wrapperId, true)
}
} else {
undominate(objectId, false)
}
}
else -> {
undominate(objectId, false)
}
}
}
private fun State.undominate(
objectId: Long,
neverEnqueued: Boolean
) {
dominatedObjectIds.remove(objectId)
if (neverEnqueued) {
visitedSet.add(objectId)
}
}
}