-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sergey Pospelov <[email protected]>
- Loading branch information
1 parent
4499986
commit bf3f6b4
Showing
110 changed files
with
7,667 additions
and
4,096 deletions.
There are no files selected for viewing
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
74 changes: 0 additions & 74 deletions
74
jacodb-analysis/src/main/kotlin/org/jacodb/analysis/AnalysisMain.kt
This file was deleted.
Oops, something went wrong.
185 changes: 185 additions & 0 deletions
185
jacodb-analysis/src/main/kotlin/org/jacodb/analysis/config/Condition.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,185 @@ | ||
/* | ||
* 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.analysis.config | ||
|
||
import org.jacodb.analysis.ifds.AccessPath | ||
import org.jacodb.analysis.ifds.ElementAccessor | ||
import org.jacodb.analysis.ifds.Maybe | ||
import org.jacodb.analysis.ifds.onSome | ||
import org.jacodb.analysis.ifds.toPath | ||
import org.jacodb.analysis.taint.Tainted | ||
import org.jacodb.api.cfg.JcBool | ||
import org.jacodb.api.cfg.JcConstant | ||
import org.jacodb.api.cfg.JcInt | ||
import org.jacodb.api.cfg.JcStringConstant | ||
import org.jacodb.api.cfg.JcValue | ||
import org.jacodb.api.ext.isAssignable | ||
import org.jacodb.taint.configuration.And | ||
import org.jacodb.taint.configuration.AnnotationType | ||
import org.jacodb.taint.configuration.ConditionVisitor | ||
import org.jacodb.taint.configuration.ConstantBooleanValue | ||
import org.jacodb.taint.configuration.ConstantEq | ||
import org.jacodb.taint.configuration.ConstantGt | ||
import org.jacodb.taint.configuration.ConstantIntValue | ||
import org.jacodb.taint.configuration.ConstantLt | ||
import org.jacodb.taint.configuration.ConstantMatches | ||
import org.jacodb.taint.configuration.ConstantStringValue | ||
import org.jacodb.taint.configuration.ConstantTrue | ||
import org.jacodb.taint.configuration.ContainsMark | ||
import org.jacodb.taint.configuration.IsConstant | ||
import org.jacodb.taint.configuration.IsType | ||
import org.jacodb.taint.configuration.Not | ||
import org.jacodb.taint.configuration.Or | ||
import org.jacodb.taint.configuration.PositionResolver | ||
import org.jacodb.taint.configuration.SourceFunctionMatches | ||
import org.jacodb.taint.configuration.TypeMatches | ||
|
||
open class BasicConditionEvaluator( | ||
internal val positionResolver: PositionResolver<Maybe<JcValue>>, | ||
) : ConditionVisitor<Boolean> { | ||
|
||
override fun visit(condition: ConstantTrue): Boolean { | ||
return true | ||
} | ||
|
||
override fun visit(condition: Not): Boolean { | ||
return !condition.arg.accept(this) | ||
} | ||
|
||
override fun visit(condition: And): Boolean { | ||
return condition.args.all { it.accept(this) } | ||
} | ||
|
||
override fun visit(condition: Or): Boolean { | ||
return condition.args.any { it.accept(this) } | ||
} | ||
|
||
override fun visit(condition: IsConstant): Boolean { | ||
positionResolver.resolve(condition.position).onSome { return it is JcConstant } | ||
return false | ||
} | ||
|
||
override fun visit(condition: IsType): Boolean { | ||
// Note: TaintConfigurationFeature.ConditionSpecializer is responsible for | ||
// expanding IsType condition upon parsing the taint configuration. | ||
error("Unexpected condition: $condition") | ||
} | ||
|
||
override fun visit(condition: AnnotationType): Boolean { | ||
// Note: TaintConfigurationFeature.ConditionSpecializer is responsible for | ||
// expanding AnnotationType condition upon parsing the taint configuration. | ||
error("Unexpected condition: $condition") | ||
} | ||
|
||
override fun visit(condition: ConstantEq): Boolean { | ||
positionResolver.resolve(condition.position).onSome { value -> | ||
return when (val constant = condition.value) { | ||
is ConstantBooleanValue -> { | ||
value is JcBool && value.value == constant.value | ||
} | ||
|
||
is ConstantIntValue -> { | ||
value is JcInt && value.value == constant.value | ||
} | ||
|
||
is ConstantStringValue -> { | ||
// TODO: if 'value' is not string, convert it to string and compare with 'constant.value' | ||
value is JcStringConstant && value.value == constant.value | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
override fun visit(condition: ConstantLt): Boolean { | ||
positionResolver.resolve(condition.position).onSome { value -> | ||
return when (val constant = condition.value) { | ||
is ConstantIntValue -> { | ||
value is JcInt && value.value < constant.value | ||
} | ||
|
||
else -> error("Unexpected constant: $constant") | ||
} | ||
} | ||
return false | ||
} | ||
|
||
override fun visit(condition: ConstantGt): Boolean { | ||
positionResolver.resolve(condition.position).onSome { value -> | ||
return when (val constant = condition.value) { | ||
is ConstantIntValue -> { | ||
value is JcInt && value.value > constant.value | ||
} | ||
|
||
else -> error("Unexpected constant: $constant") | ||
} | ||
} | ||
return false | ||
} | ||
|
||
override fun visit(condition: ConstantMatches): Boolean { | ||
positionResolver.resolve(condition.position).onSome { value -> | ||
val re = condition.pattern.toRegex() | ||
return re.matches(value.toString()) | ||
} | ||
return false | ||
} | ||
|
||
override fun visit(condition: SourceFunctionMatches): Boolean { | ||
TODO("Not implemented yet") | ||
} | ||
|
||
override fun visit(condition: ContainsMark): Boolean { | ||
error("This visitor does not support condition $condition. Use FactAwareConditionEvaluator instead") | ||
} | ||
|
||
override fun visit(condition: TypeMatches): Boolean { | ||
positionResolver.resolve(condition.position).onSome { value -> | ||
return value.type.isAssignable(condition.type) | ||
} | ||
return false | ||
} | ||
} | ||
|
||
class FactAwareConditionEvaluator( | ||
private val fact: Tainted, | ||
positionResolver: PositionResolver<Maybe<JcValue>>, | ||
) : BasicConditionEvaluator(positionResolver) { | ||
|
||
override fun visit(condition: ContainsMark): Boolean { | ||
if (fact.mark != condition.mark) return false | ||
positionResolver.resolve(condition.position).onSome { value -> | ||
val variable = value.toPath() | ||
|
||
// FIXME: Adhoc for arrays | ||
val variableWithoutStars = variable.removeTrailingElementAccessors() | ||
val factWithoutStars = fact.variable.removeTrailingElementAccessors() | ||
if (variableWithoutStars == factWithoutStars) return true | ||
|
||
return variable == fact.variable | ||
} | ||
return false | ||
} | ||
|
||
private fun AccessPath.removeTrailingElementAccessors(): AccessPath { | ||
val accesses = accesses.toMutableList() | ||
while (accesses.lastOrNull() is ElementAccessor) { | ||
accesses.removeLast() | ||
} | ||
return AccessPath(value, accesses) | ||
} | ||
} |
Oops, something went wrong.