Skip to content

Avoid recursive enumeration of class and classloader instances when plugin is enabled #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal fun enumerateObjects(obj: Any): Map<Any, Int> {
* @param objectNumberMap result enumeration map
*/
private fun enumerateObjects(obj: Any, processedObjects: MutableSet<Any>, objectNumberMap: MutableMap<Any, Int>) {
if (obj is Class<*> || obj is ClassLoader) return
if (!processedObjects.add(obj)) return
val objectNumber = getObjectNumber(obj.javaClass, obj)
objectNumberMap[obj] = objectNumber
Expand Down
33 changes: 33 additions & 0 deletions src/jvm/test/org/jetbrains/kotlinx/lincheck/ObjectTraverserTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Lincheck
*
* Copyright (C) 2019 - 2024 JetBrains s.r.o.
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.jetbrains.kotlinx.lincheck

import org.junit.Assert
import org.junit.Test

/**
* Checks invariants and restrictions on [enumerateObjects] method.
*/
class ObjectTraverserTest {

@Test
fun `should not traverse class and classLoader recursively while enumerating objects`() {
val myObject = @Suppress("unused") object : Any() {
var clazz: Class<*>? = this::class.java
var classLoader: ClassLoader? = this::class.java.classLoader
var integer: Int = 10
}
val objectEnumeration = enumerateObjects(myObject)

Assert.assertTrue(objectEnumeration.keys.none { it is Class<*> || it is ClassLoader })
}

}