@@ -14,7 +14,6 @@ typealias JavaValueProvider = ValueProvider<FuzzedType, FuzzedValue, FuzzedDescr
1414class FuzzedDescription (
1515 val description : FuzzedMethodDescription ,
1616 val tracer : Trie <Instruction , * >,
17- val genThisInstance : ClassId ? ,
1817) : Description<FuzzedType>(
1918 description.parameters.mapIndexed { index, classId ->
2019 description.fuzzerType(index) ? : FuzzedType (classId)
@@ -47,7 +46,7 @@ suspend fun runJavaFuzzing(
4746 names : List <String >,
4847 mock : (ClassId ) -> Boolean = { false },
4948 providers : List <ValueProvider <FuzzedType , FuzzedValue , FuzzedDescription >> = defaultValueProviders(idGenerator),
50- exec : suspend (description: FuzzedDescription , values: List <FuzzedValue >) -> BaseFeedback <Trie .Node <Instruction >, FuzzedType , FuzzedValue >
49+ exec : suspend (thisInstance: FuzzedValue ? , description: FuzzedDescription , values: List <FuzzedValue >) -> BaseFeedback <Trie .Node <Instruction >, FuzzedType , FuzzedValue >
5150) {
5251 val classUnderTest = methodUnderTest.classId
5352 val thisInstance = with (methodUnderTest) {
@@ -61,17 +60,27 @@ suspend fun runJavaFuzzing(
6160 val returnType = methodUnderTest.returnType
6261 val parameters = listOfNotNull(thisInstance) + methodUnderTest.parameters
6362
64- val fmd = FuzzedMethodDescription (
63+ /* *
64+ * To fuzz this instance the class of it is added into head of parameters list.
65+ * Done for compatibility with old fuzzer logic and should be reworked more robust way.
66+ */
67+ fun createFuzzedMethodDescription (thisInstance : ClassId ? ) = FuzzedMethodDescription (
6568 name, returnType, parameters, constants
6669 ).apply {
6770 compilableName = if (! methodUnderTest.isConstructor) methodUnderTest.name else null
6871 className = classUnderTest.simpleName
6972 packageName = classUnderTest.packageName
70- parameterNameMap = { index -> names.getOrNull(index) }
73+ parameterNameMap = { index ->
74+ when {
75+ thisInstance != null && index == 0 -> " this"
76+ thisInstance != null -> names.getOrNull(index - 1 )
77+ else -> names.getOrNull(index)
78+ }
79+ }
7180 fuzzerType = {
7281 try {
7382 when {
74- thisInstance != null && it == 0 -> toFuzzerType(classUnderTest.jClass )
83+ thisInstance != null && it == 0 -> toFuzzerType(methodUnderTest.executable.declaringClass )
7584 thisInstance != null -> toFuzzerType(methodUnderTest.executable.genericParameterTypes[it - 1 ])
7685 else -> toFuzzerType(methodUnderTest.executable.genericParameterTypes[it])
7786 }
@@ -81,8 +90,17 @@ suspend fun runJavaFuzzing(
8190 }
8291 shouldMock = mock
8392 }
84- BaseFuzzing (providers, exec)
85- .fuzz(FuzzedDescription (fmd, Trie (Instruction ::id), thisInstance))
93+
94+ val tracer = Trie (Instruction ::id)
95+ val descriptionWithOptionalThisInstance = FuzzedDescription (createFuzzedMethodDescription(thisInstance), tracer)
96+ val descriptionWithOnlyParameters = FuzzedDescription (createFuzzedMethodDescription(null ), tracer)
97+ BaseFuzzing (providers) { d, t ->
98+ if (thisInstance == null ) {
99+ exec(null , d, t)
100+ } else {
101+ exec(t.first(), descriptionWithOnlyParameters, t.drop(1 ))
102+ }
103+ }.fuzz(descriptionWithOptionalThisInstance)
86104}
87105
88106private fun toFuzzerType (type : Type ): FuzzedType {
0 commit comments