-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JacoDB produces incorrect 3-address code for IINC instruction (#158)
JacoDB produces incorrect 3-address code for IINC instruction #146 Co-authored-by: Sergey Pospelov <[email protected]>
- Loading branch information
1 parent
c0616ca
commit 07d3ffc
Showing
7 changed files
with
320 additions
and
252 deletions.
There are no files selected for viewing
271 changes: 101 additions & 170 deletions
271
jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/RawInstListBuilder.kt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
jacodb-core/src/test/kotlin/org/jacodb/testing/cfg/BaseInstructionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2022 UnitTestBot contributors (utbot.org) | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 org.jacodb.testing.cfg | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import org.jacodb.api.JcClassOrInterface | ||
import org.jacodb.api.NoClassInClasspathException | ||
import org.jacodb.api.cfg.applyAndGet | ||
import org.jacodb.api.ext.isKotlin | ||
import org.jacodb.api.ext.packageName | ||
import org.jacodb.impl.bytecode.JcDatabaseClassWriter | ||
import org.jacodb.impl.cfg.MethodNodeBuilder | ||
import org.jacodb.impl.features.hierarchyExt | ||
import org.jacodb.testing.BaseTest | ||
import org.junit.jupiter.api.Assertions | ||
import org.objectweb.asm.ClassWriter | ||
import org.objectweb.asm.util.CheckClassAdapter | ||
import java.io.File | ||
import java.net.URLClassLoader | ||
import java.nio.file.Files | ||
import java.nio.file.Paths | ||
|
||
abstract class BaseInstructionsTest : BaseTest() { | ||
|
||
private val target = Files.createTempDirectory("jcdb-temp") | ||
|
||
val ext = runBlocking { cp.hierarchyExt() } | ||
|
||
protected fun testClass(klass: JcClassOrInterface) { | ||
testAndLoadClass(klass, false) | ||
} | ||
|
||
protected fun testAndLoadClass(klass: JcClassOrInterface): Class<*> { | ||
return testAndLoadClass(klass, true)!! | ||
} | ||
|
||
private fun testAndLoadClass(klass: JcClassOrInterface, loadClass: Boolean): Class<*>? { | ||
try { | ||
val classNode = klass.asmNode() | ||
classNode.methods = klass.declaredMethods.filter { it.enclosingClass == klass }.map { | ||
if (it.isAbstract || it.name.contains("$\$forInline")) { | ||
it.asmNode() | ||
} else { | ||
try { | ||
// val oldBody = it.body() | ||
// println() | ||
// println("Old body: ${oldBody.print()}") | ||
val instructionList = it.rawInstList | ||
it.instList.forEachIndexed { index, inst -> | ||
Assertions.assertEquals(index, inst.location.index, "indexes not matched for $it at $index") | ||
} | ||
// println("Instruction list: $instructionList") | ||
val graph = it.flowGraph() | ||
if (!it.enclosingClass.isKotlin) { | ||
val methodMsg = "$it should have line number" | ||
graph.instructions.forEach { | ||
Assertions.assertTrue(it.lineNumber > 0, methodMsg) | ||
} | ||
} | ||
graph.applyAndGet(OverridesResolver(ext)) {} | ||
JcGraphChecker(it, graph).check() | ||
// println("Graph: $graph") | ||
// graph.view("/usr/bin/dot", "/usr/bin/firefox", false) | ||
// graph.blockGraph().view("/usr/bin/dot", "/usr/bin/firefox") | ||
val newBody = MethodNodeBuilder(it, instructionList).build() | ||
// println("New body: ${newBody.print()}") | ||
// println() | ||
newBody | ||
} catch (e: Exception) { | ||
throw IllegalStateException("error handling $it", e) | ||
} | ||
|
||
} | ||
} | ||
val cw = JcDatabaseClassWriter(cp, ClassWriter.COMPUTE_FRAMES) | ||
val checker = CheckClassAdapter(cw) | ||
try { | ||
classNode.accept(checker) | ||
} catch (ex: Throwable) { | ||
println(ex) | ||
} | ||
val targetDir = target.resolve(klass.packageName.replace('.', '/')) | ||
val targetFile = targetDir.resolve("${klass.simpleName}.class").toFile().also { | ||
it.parentFile?.mkdirs() | ||
} | ||
targetFile.writeBytes(cw.toByteArray()) | ||
if (loadClass) { | ||
|
||
val cp = listOf(target.toUri().toURL()) + System.getProperty("java.class.path") | ||
.split(File.pathSeparatorChar) | ||
.map { Paths.get(it).toUri().toURL() } | ||
val allClassLoader = URLClassLoader(cp.toTypedArray(), null) | ||
return allClassLoader.loadClass(klass.name) | ||
} | ||
} catch (e: NoClassInClasspathException) { | ||
System.err.println(e.localizedMessage) | ||
} | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
jacodb-core/src/testFixtures/java/org/jacodb/testing/cfg/Incrementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2022 UnitTestBot contributors (utbot.org) | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 org.jacodb.testing.cfg; | ||
|
||
public class Incrementation { | ||
|
||
static public int iinc(int x) { | ||
return x++; | ||
} | ||
|
||
static public int[] iincArrayIntIdx() { | ||
int[] arr = new int[3]; | ||
int idx = 0; | ||
arr[idx++] = 1; | ||
arr[++idx] = 2; | ||
return arr; | ||
} | ||
|
||
static public int[] iincArrayByteIdx() { | ||
int[] arr = new int[3]; | ||
byte idx = 0; | ||
arr[idx++] = 1; | ||
arr[++idx] = 2; | ||
return arr; | ||
} | ||
|
||
static public int[] iincFor() { | ||
int[] result = new int[5]; | ||
for (int i = 0; i < 5; i++) { | ||
result[i] = i; | ||
} | ||
return result; | ||
} | ||
|
||
} |