Skip to content

Commit

Permalink
Fixes w.r.t PR request
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniilStepanov committed Nov 1, 2023
1 parent 5847035 commit 4d86a74
Show file tree
Hide file tree
Showing 27 changed files with 376 additions and 366 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public class MockCollector {

private static final int DEFAULT_MOCKINFO_ARRAY_CAPACITY = 10;
public static MockInfoArrayWrapper mocks = new MockInfoArrayWrapper();

public static boolean inExecution;

public static boolean isInExecution() {
Expand All @@ -14,30 +14,31 @@ public static Object getMockValue(long mockedId, Object mockedObject) {
return mocks.find(mockedId, mockedObject).getMockValue();
}
public static boolean getBooleanMockValue(long mockedId, Object mockedObject) {
return (boolean) mocks.find(mockedId, mockedObject).getMockValue();
return (boolean) getMockValue(mockedId, mockedObject);
}
public static byte getByteMockValue(long mockedId, Object mockedObject) {
return (byte) mocks.find(mockedId, mockedObject).getMockValue();
return (byte) getMockValue(mockedId, mockedObject);
}
public static short getShortMockValue(long mockedId, Object mockedObject) {
return (short) mocks.find(mockedId, mockedObject).getMockValue();
return (short) getMockValue(mockedId, mockedObject);
}
public static char getCharMockValue(long mockedId, Object mockedObject) {
return (char) mocks.find(mockedId, mockedObject).getMockValue();
return (char) getMockValue(mockedId, mockedObject);
}
public static int getIntMockValue(long mockedId, Object mockedObject) {
return (int) mocks.find(mockedId, mockedObject).getMockValue();
return (int) getMockValue(mockedId, mockedObject);
}
public static float getFloatMockValue(long mockedId, Object mockedObject) {
return (float) mocks.find(mockedId, mockedObject).getMockValue();
return (float) getMockValue(mockedId, mockedObject);
}
public static double getDoubleMockValue(long mockedId, Object mockedObject) {
return (double) mocks.find(mockedId, mockedObject).getMockValue();
return (double) getMockValue(mockedId, mockedObject);
}
public static long getLongMockValue(long mockedId, Object mockedObject) {
return (long) mocks.find(mockedId, mockedObject).getMockValue();
return (long) getMockValue(mockedId, mockedObject);
}


public static boolean isMocked(long mockedId, Object mockedObject) {
return mocks.find(mockedId, mockedObject) != null;
}
Expand Down Expand Up @@ -73,7 +74,7 @@ public static class MockInfoArrayWrapper {
public int size;

MockInfoArrayWrapper() {
arr = new MockInfo[10];
arr = new MockInfo[DEFAULT_MOCKINFO_ARRAY_CAPACITY];
}

public MockInfo find(long mockId, Object mockedObject) {
Expand Down Expand Up @@ -104,8 +105,8 @@ public void add(MockInfo el) {
}

public void removeLast() {
arr[size] = null;
if (size == 0) return;
arr[size] = null;
size--;
}

Expand All @@ -131,7 +132,7 @@ public static class MockValueArrayWrapper {
public int size;

public MockValueArrayWrapper() {
arr = new Object[10];
arr = new Object[DEFAULT_MOCKINFO_ARRAY_CAPACITY];
}

public MockValueArrayWrapper(int size) {
Expand All @@ -147,8 +148,8 @@ public void add(Object el) {
}

public void removeLast() {
arr[size] = 0;
if (size == 0) return;
arr[size] = 0;
size--;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.jacodb.api.JcClasspath
import org.jacodb.api.JcField
import org.jacodb.api.ext.findClass
import org.objectweb.asm.tree.ClassNode
import org.usvm.instrumentation.instrumentation.JcRuntimeTraceInstrumenter
import org.usvm.instrumentation.testcase.descriptor.StaticDescriptorsBuilder
import org.usvm.instrumentation.util.*
import java.lang.instrument.ClassDefinition
Expand Down Expand Up @@ -42,8 +43,8 @@ class WorkerClassLoader(
}

//Invoking clinit method for loaded classes for statics reset between executions
fun reset(accessedStatic: List<JcField>) {
val jcClassesToReinit = accessedStatic.map { it.enclosingClass }.toSet()
fun reset(accessedStatics: List<JcField>) {
val jcClassesToReinit = accessedStatics.map { it.enclosingClass }.toSet()
val classesToReinit = foundClasses.values
.filter { it.second in jcClassesToReinit }
.map { it.first }
Expand All @@ -61,7 +62,9 @@ class WorkerClassLoader(
}
classesToReinit.forEach { cl ->
try {
cl.declaredMethods.find { it.name == "generatedClinit0" }?.invokeWithAccessibility(null, listOf())
cl.declaredMethods
.find { it.name == JcRuntimeTraceInstrumenter.GENERATED_CLINIT_NAME }
?.invokeWithAccessibility(null, listOf())
} catch (e: Throwable) {
//cannot access some classes, for example, enums
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@ package org.usvm.instrumentation.executor

import com.jetbrains.rd.util.lifetime.LifetimeDefinition
import com.jetbrains.rd.util.lifetime.isAlive
import com.jetbrains.rd.util.reactive.RdFault
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.withTimeout
import org.jacodb.api.JcClasspath
import org.jacodb.api.ext.findClass
import org.jacodb.api.ext.toType
import org.usvm.instrumentation.instrumentation.JcInstrumenterFactory
import org.usvm.instrumentation.testcase.UTest
import org.usvm.instrumentation.testcase.api.UTestExecutionFailedResult
import org.usvm.instrumentation.testcase.api.UTestExecutionResult
import org.usvm.instrumentation.testcase.api.UTestExecutionTimedOutResult
import org.usvm.instrumentation.testcase.descriptor.UTestExceptionDescriptor
import org.usvm.instrumentation.testcase.descriptor.UTestUnexpectedExecutionBuilder
import org.usvm.instrumentation.util.InstrumentationModuleConstants
import org.usvm.instrumentation.util.UTestExecutorInitException
import java.util.concurrent.CancellationException
import java.util.concurrent.TimeoutException
import kotlin.reflect.KClass
import kotlin.time.Duration

Expand All @@ -39,6 +31,7 @@ class UTestConcreteExecutor(

private val instrumentationProcessRunner =
InstrumentationProcessRunner(testingProjectClasspath, jcClasspath, instrumentationClassFactory)
private val uTestUnexpectedExecutionBuilder = UTestUnexpectedExecutionBuilder(jcClasspath)

suspend fun ensureRunnerAlive() {
check(lifetime.isAlive) { "Executor already closed" }
Expand All @@ -49,8 +42,7 @@ class UTestConcreteExecutor(
try {
instrumentationProcessRunner.init(lifetime)
} catch (e: Throwable) {
//TODO replace to logger
println("Cant init rdProcess:(")
println("Cant init rdProcess")
}
}
if (!instrumentationProcessRunner.isAlive()) {
Expand All @@ -61,24 +53,8 @@ class UTestConcreteExecutor(
fun executeSync(uTest: UTest): UTestExecutionResult {
return try {
instrumentationProcessRunner.executeUTestSync(uTest, timeout)
} catch (e: TimeoutException) {
instrumentationProcessRunner.destroy()
val descriptor = UTestExceptionDescriptor(
type = jcClasspath.findClass<Exception>().toType(),
message = e.message ?: "timeout",
stackTrace = listOf(),
raisedByUserCode = false
)
UTestExecutionTimedOutResult(descriptor)
} catch (e: RdFault) {
instrumentationProcessRunner.destroy()
val descriptor = UTestExceptionDescriptor(
type = jcClasspath.findClass<Exception>().toType(),
message = e.reasonAsText,
stackTrace = listOf(),
raisedByUserCode = false
)
UTestExecutionFailedResult(descriptor)
} catch (e: Exception) {
uTestUnexpectedExecutionBuilder.build(e)
}
}

Expand All @@ -88,30 +64,8 @@ class UTestConcreteExecutor(
withTimeout(timeout) {
instrumentationProcessRunner.executeUTestAsync(uTest)
}
} catch (e: TimeoutCancellationException) {
val descriptor = UTestExceptionDescriptor(
type = jcClasspath.findClass<Exception>().toType(),
message = e.message ?: "timeout",
stackTrace = listOf(),
raisedByUserCode = false
)
UTestExecutionTimedOutResult(descriptor)
} catch (e: RdFault) {
val descriptor = UTestExceptionDescriptor(
type = jcClasspath.findClass<Exception>().toType(),
message = e.reasonAsText,
stackTrace = listOf(),
raisedByUserCode = false
)
UTestExecutionFailedResult(descriptor)
} catch (e: CancellationException) {
val descriptor = UTestExceptionDescriptor(
type = jcClasspath.findClass<Exception>().toType(),
message = "CancellationException",
stackTrace = listOf(),
raisedByUserCode = false
)
UTestExecutionFailedResult(descriptor)
} catch (e: Exception) {
uTestUnexpectedExecutionBuilder.build(e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class JcRuntimeTraceInstrumenter(
//Copy of clinit method to be able to rollback statics between executions!
//We are not able to call <clinit> method directly with reflection
asmMethods.find { it.name == "<clinit>" }?.let { clinitNode ->
val clinitCopy = MethodNode(9, "generatedClinit0", "()V", null, emptyArray())
val clinitCopy = MethodNode(9, GENERATED_CLINIT_NAME, "()V", null, emptyArray())
clinitNode.instructions.forEach { clinitCopy.instructions.add(it) }
asmMethods.add(0, clinitCopy)
}
Expand All @@ -97,4 +97,8 @@ class JcRuntimeTraceInstrumenter(
return classNode
}

companion object {
const val GENERATED_CLINIT_NAME = "generatedClinit0"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ class TraceHelper(
) {

private val jcVirtualGlobalObjectClass =
JcVirtualClassImpl(globalObjectJClass.name,
globalObjectJClass.modifiers,
listOf(),
globalObjectJClass.declaredMethods.map { createJcVirtualMethod(it) }).also {
it.classpath = jcClasspath
}
JcVirtualClassImpl(
name = globalObjectJClass.name,
access = globalObjectJClass.modifiers,
initialFields = listOf(),
initialMethods = globalObjectJClass.declaredMethods.map { createJcVirtualMethod(it) }
).also { it.classpath = jcClasspath }

//We need virtual method to insert it invocation in instrumented instruction list
private fun createJcVirtualMethod(jMethod: Method): JcVirtualMethod = JcVirtualMethodImpl(
jMethod.name, jMethod.modifiers, TypeNameImpl(jMethod.returnType.name), createJcVirtualMethodParams(jMethod), ""
name = jMethod.name,
access = jMethod.modifiers,
returnType = TypeNameImpl(jMethod.returnType.name),
parameters = createJcVirtualMethodParams(jMethod),
description = ""
)

private fun createJcVirtualMethodParams(jMethod: Method): List<JcVirtualParameter> =
Expand All @@ -51,33 +55,33 @@ class TraceHelper(
val jcTraceMethod = jcVirtualGlobalObjectClass.declaredMethods.find { it.name == traceMethodName }!!
val jcRawLong = JcRawLong(id)
return JcRawStaticCallExpr(
jcVirtualGlobalObjectClass.typename,
jcTraceMethod.name,
listOf(jcClasspath.long.getTypename(), jcClasspath.objectType.getTypename()),
jcTraceMethod.returnType,
listOf(jcRawLong, jcThisReference)
declaringClass = jcVirtualGlobalObjectClass.typename,
methodName = jcTraceMethod.name,
argumentTypes = listOf(jcClasspath.long.getTypename(), jcClasspath.objectType.getTypename()),
returnType = jcTraceMethod.returnType,
args = listOf(jcRawLong, jcThisReference)
)
}

fun createMockCollectorIsInExecutionCall(): JcRawStaticCallExpr {
val jcTraceMethod = jcVirtualGlobalObjectClass.declaredMethods.find { it.name == "isInExecution" }!!
return JcRawStaticCallExpr(
jcVirtualGlobalObjectClass.typename,
jcTraceMethod.name,
listOf(),
jcTraceMethod.returnType,
listOf()
declaringClass = jcVirtualGlobalObjectClass.typename,
methodName = jcTraceMethod.name,
argumentTypes = listOf(),
returnType = jcTraceMethod.returnType,
args = listOf()
)
}

fun createStaticExprWithLongArg(arg: Long, jcTraceMethod: JcVirtualMethod): JcRawStaticCallExpr {
val argAsJcConst = JcRawLong(arg)
return JcRawStaticCallExpr(
jcVirtualGlobalObjectClass.typename,
jcTraceMethod.name,
listOf(jcClasspath.long.getTypename()),
jcTraceMethod.returnType,
listOf(argAsJcConst)
declaringClass = jcVirtualGlobalObjectClass.typename,
methodName = jcTraceMethod.name,
argumentTypes = listOf(jcClasspath.long.getTypename()),
returnType = jcTraceMethod.returnType,
args = listOf(argAsJcConst)
)
}

Expand Down
Loading

0 comments on commit 4d86a74

Please sign in to comment.