-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement initial PoC for checking return values (#846)
Co-authored-by: Selina Lin <[email protected]> Co-authored-by: Robert Haimerl <[email protected]>
- Loading branch information
1 parent
ad301db
commit b7043cf
Showing
29 changed files
with
2,109 additions
and
56 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
77 changes: 77 additions & 0 deletions
77
...ds/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/DataItemCpgDsl.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,77 @@ | ||
/* | ||
* Copyright (c) 2022, Fraunhofer AISEC. All rights reserved. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 de.fraunhofer.aisec.codyze.backends.cpg.coko.dsl | ||
|
||
import de.fraunhofer.aisec.codyze.backends.cpg.coko.Nodes | ||
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.CokoBackend | ||
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling.ArgumentItem | ||
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling.DataItem | ||
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling.ReturnValueItem | ||
import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.modelling.Value | ||
import de.fraunhofer.aisec.cpg.graph.* | ||
import de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration | ||
import de.fraunhofer.aisec.cpg.graph.statements.expressions.Reference | ||
|
||
/** | ||
* Get all [Nodes] that are associated with this [DataItem]. | ||
*/ | ||
context(CokoBackend) | ||
fun DataItem<*>.cpgGetAllNodes(): Nodes = | ||
when (this@DataItem) { | ||
is ReturnValueItem -> op.cpgGetAllNodes().flatMap { it.getVariableInNextDFGOrThis() } | ||
is Value -> this@DataItem.getNodes() | ||
is ArgumentItem -> op.cpgGetAllNodes().map { it.arguments[index] } // TODO: Do we count starting at 0 or 1? | ||
} | ||
|
||
/** | ||
* Get all [Nodes] that are associated with this [DataItem]. | ||
*/ | ||
context(CokoBackend) | ||
fun DataItem<*>.cpgGetNodes(): Nodes { | ||
return when (this@DataItem) { | ||
is ReturnValueItem -> op.cpgGetNodes().flatMap { it.getVariableInNextDFGOrThis() } | ||
is Value -> this@DataItem.getNodes() | ||
is ArgumentItem -> op.cpgGetNodes().map { it.arguments[index] } // TODO: Do we count starting at 0 or 1? | ||
} | ||
} | ||
|
||
/** | ||
* Get all [Nodes] that evaluate to the same value as [Value.value]. | ||
*/ | ||
context(CokoBackend) | ||
private fun Value<*>.getNodes(): Nodes { | ||
val value = this.value | ||
return if (value is Node) { | ||
listOf(value) | ||
} else { | ||
cpg.literals.filter { node -> | ||
node.value == value | ||
} + cpg.variables.filter { node -> | ||
node.evaluate() == value | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns all [VariableDeclaration]s and [DeclaredReferenceExpression]s that have a DFG edge from [this]. | ||
* If there are none, returns [this]. | ||
*/ | ||
private fun Node.getVariableInNextDFGOrThis(): Nodes = | ||
this.nextDFG | ||
.filter { | ||
next -> | ||
next is Reference || next is VariableDeclaration | ||
}.ifEmpty { listOf(this) } |
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
Oops, something went wrong.