Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix C style function resolution #1860

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
source.scope,
)
val language = source.language
val sourceCall = source as? CallExpression

if (language == null) {
result.success = CallResolutionResult.SuccessKind.PROBLEMATIC
Expand All @@ -527,29 +528,43 @@ open class SymbolResolver(ctx: TranslationContext) : ComponentPass(ctx) {
val (scope, _) = ctx.scopeManager.extractScope(source, source.scope)
result.actualStartScope = scope ?: source.scope

// If the function does not allow function overloading, and we have multiple candidate
// symbols, the result is "problematic"
if (source.language !is HasFunctionOverloading && result.candidateFunctions.size > 1) {
result.success = PROBLEMATIC
}

// Filter functions that match the signature of our call, either directly or with casts;
// those functions are "viable". Take default arguments into account if the language has
// them.
result.signatureResults =
result.candidateFunctions
.map {
Pair(
it,
it.matchesSignature(
arguments.map(Expression::type),
arguments,
source.language is HasDefaultArguments,
// Resolution depends on language features
if (source.language !is HasFunctionOverloading) {
// If the function does not allow function overloading, and we have multiple candidate
// symbols, the result is "problematic"
if (result.candidateFunctions.size > 1) {
result.success = CallResolutionResult.SuccessKind.PROBLEMATIC
} else
// If we have only one candidate function and the number of arguments match, we can take
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has multiple issues

  1. This does not only affect C but other languages as well.
  2. It does not take into account if languages have default arguments

At the very least I think we need to make this configurable. Depending on the use case you MAY or MAY NOT want to do this. For example if you actually want to check if something is resolving similar to a compiler (e.g., also inference turned off), you want to have an empty list of invokes edges here.

If you want to be very optimistic about matching types, this may be another (maybe even global?) configuration option. You can always of course override the tryCast in a language that depends on the C language if you want to have a "matches-everything-C-language`.

So I am very skeptical about this change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true, default arguments are a problem, haven't thought about that.
Also the go var args arguments fail.
Do we have a language feature for that to match on?
Affecting other languages is not a problem per se I would say, but other languages bring other combinations of features, which must be taken into account during the resolution.

Override the tryCast might be an alternative for the C language.

Not sure if we want to make this configurable, as long as it is conform with the language design.
Configuration options tend to be only used by the person who implemented them and others are wondering why it's not working, not knowing about the option.

// a shortcut and stop here
if (
result.candidateFunctions.size == 1 &&
result.candidateFunctions.first().parameters.size == sourceCall?.arguments?.size
) {
result.signatureResults =
result.candidateFunctions.associateWith {
SignatureMatches(mutableListOf(DirectMatch))
}
}
} else {
// Filter functions that match the signature of our call, either directly or with
// casts; those functions are "viable". Take default arguments into account if the
// language has them.
result.signatureResults =
result.candidateFunctions
.map {
Pair(
it,
it.matchesSignature(
arguments.map(Expression::type),
arguments,
source.language is HasDefaultArguments,
)
)
)
}
.filter { it.second is SignatureMatches }
.associate { it }
}
.filter { it.second is SignatureMatches }
.associate { it }
}
result.viableFunctions = result.signatureResults.keys

// If we have a "problematic" result, we can stop here. In this case we cannot really
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ class CXXDeclarationTest {

declarations.forEach { assertEquals(definition, it.definition) }

// without the "std" lib, int will not match with size_t and we will infer a new function;
// and this will actually result in a problematic resolution, since C does not allow
// function overloading.
// As C does not support function overload, we can resolve the call to foo even if we do not
// know the argument type (due to missing includes), only by the name of the function and
// the number of arguments.
val inferredDefinition =
result.functions[{ it.name.localName == "foo" && !it.isDefinition && it.isInferred }]
assertNotNull(inferredDefinition)
assertNull(inferredDefinition)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package de.fraunhofer.aisec.cpg.passes

import de.fraunhofer.aisec.cpg.*
import de.fraunhofer.aisec.cpg.frontends.cxx.CLanguage
import de.fraunhofer.aisec.cpg.frontends.cxx.CPPLanguage
import de.fraunhofer.aisec.cpg.graph.*
import de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration
Expand Down Expand Up @@ -749,6 +750,26 @@ class CallResolverTest : BaseTest() {
assertEquals(2, declarations.size)
}

@Test
@Throws(Exception::class)
fun testCFunctionResolution() {
val file = File("src/test/resources/calls/c-function-resolution.c")
val tu =
analyzeAndGetFirstTU(listOf(file), file.parentFile.toPath(), true) {
it.registerLanguage<CLanguage>()
}

val funcFoo = tu.functions["foo"]
assertNotNull(funcFoo)
assertFalse(funcFoo.isInferred)

val fooCalls = tu.calls("foo")
fooCalls.forEach { assertContains(it.invokes, funcFoo) }

val barCalls = tu.calls("bar")
barCalls.forEach { assertTrue { it.invokes.first().isInferred } }
}

companion object {
private val topLevel = Path.of("src", "test", "resources", "calls")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//#define MY_CONST_INT 1

void foo(int i) {
}
void bar(int i) {
}

int main() {
foo(MY_CONST_INT);
foo(1);
bar(1,2);
return 0;
}

Loading