Search in sources :

Example 1 with FunctionDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.

the class CXXLiteralTest method testDecimalIntegerLiterals.

@Test
void testDecimalIntegerLiterals() throws Exception {
    File file = new File("src/test/resources/integer_literals.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    Set<FunctionDeclaration> decimal = tu.getDeclarationsByName("decimal", FunctionDeclaration.class);
    assertFalse(decimal.isEmpty());
    FunctionDeclaration funcDecl = decimal.iterator().next();
    assertEquals("decimal", funcDecl.getName());
    assertLiteral(42, TypeParser.createFrom("int", true), funcDecl, "i");
    assertLiteral(9223372036854775807L, TypeParser.createFrom("long", true), funcDecl, "l");
    assertLiteral(9223372036854775807L, TypeParser.createFrom("long", true), funcDecl, "l_with_suffix");
    assertLiteral(9223372036854775807L, TypeParser.createFrom("long long", true), funcDecl, "l_long_long_with_suffix");
    assertLiteral(new BigInteger("9223372036854775809"), TypeParser.createFrom("unsigned long", true), funcDecl, "l_unsigned_long_with_suffix");
    assertLiteral(new BigInteger("9223372036854775808"), TypeParser.createFrom("unsigned long long", true), funcDecl, "l_long_long_implicit");
    assertLiteral(new BigInteger("9223372036854775809"), TypeParser.createFrom("unsigned long long", true), funcDecl, "l_unsigned_long_long_with_suffix");
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) BigInteger(java.math.BigInteger) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with FunctionDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.

the class CXXLiteralTest method testLargeNegativeNumber.

@Test
void testLargeNegativeNumber() throws Exception {
    File file = new File("src/test/resources/largenegativenumber.cpp");
    TranslationUnitDeclaration tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);
    Set<FunctionDeclaration> main = tu.getDeclarationsByName("main", FunctionDeclaration.class);
    assertFalse(main.isEmpty());
    FunctionDeclaration funcDecl = main.iterator().next();
    VariableDeclaration a = funcDecl.getVariableDeclarationByName("a").orElse(null);
    assertNotNull(a);
    assertEquals(1, ((Literal) Objects.requireNonNull(a.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
    // there are no negative literals, so the construct "-2147483648" is
    // a unary expression and the literal "2147483648". Since "2147483648" is too large to fit
    // in an integer, it should be automatically converted to a long. The resulting value
    // -2147483648 however is small enough to fit into an int, so it is ok for the variable a to
    // have an int type
    VariableDeclaration b = funcDecl.getVariableDeclarationByName("b").orElse(null);
    assertNotNull(b);
    assertEquals(2147483648L, ((Literal) Objects.requireNonNull(b.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
    VariableDeclaration c = funcDecl.getVariableDeclarationByName("c").orElse(null);
    assertNotNull(c);
    assertEquals(2147483649L, ((Literal) Objects.requireNonNull(c.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
    VariableDeclaration d = funcDecl.getVariableDeclarationByName("d").orElse(null);
    assertNotNull(d);
    assertEquals(new BigInteger("9223372036854775808"), ((Literal) Objects.requireNonNull(d.getInitializerAs(UnaryOperator.class)).getInput()).getValue());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) BigInteger(java.math.BigInteger) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) BaseTest(de.fraunhofer.aisec.cpg.BaseTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with FunctionDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.

the class CallExpression method typeChanged.

@Override
public void typeChanged(HasType src, HasType root, Type oldType) {
    if (!TypeManager.isTypeSystemActive()) {
        return;
    }
    if (src == base) {
        setFqn(src.getType().getRoot().getTypeName() + "." + this.getName());
    } else {
        Type previous = this.type;
        List<Type> types = invokes.stream().map(PropertyEdge::getEnd).map(FunctionDeclaration::getType).filter(Objects::nonNull).collect(Collectors.toList());
        Type alternative = !types.isEmpty() ? types.get(0) : null;
        Type commonType = TypeManager.getInstance().getCommonType(types).orElse(alternative);
        Set<Type> subTypes = new HashSet<>(getPossibleSubTypes());
        subTypes.remove(oldType);
        subTypes.addAll(types);
        setType(commonType, root);
        setPossibleSubTypes(subTypes, root);
        if (!previous.equals(this.type)) {
            this.type.setTypeOrigin(Type.Origin.DATAFLOW);
        }
    }
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) Type(de.fraunhofer.aisec.cpg.graph.types.Type) HasType(de.fraunhofer.aisec.cpg.graph.HasType)

Example 4 with FunctionDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.

the class BotanExampleTest method testExample.

@Test
void testExample() throws Exception {
    File file = new File("src/test/resources/botan/symm_block_cipher.cpp");
    TranslationUnitDeclaration declaration = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), false);
    assertNotNull(declaration);
    List<Declaration> declarations = declaration.getDeclarations();
    assertEquals(5, declarations.size());
    Declaration doCrypt = declarations.stream().filter(decl -> decl.getName().equals("do_crypt")).findFirst().get();
    assertTrue(doCrypt instanceof FunctionDeclaration);
    assertEquals("do_crypt", doCrypt.getName());
    Declaration encrypt = declarations.stream().filter(decl -> decl.getName().equals("encrypt")).findFirst().get();
    assertTrue(encrypt instanceof FunctionDeclaration);
    assertEquals("encrypt", encrypt.getName());
    Declaration decrypt = declarations.stream().filter(decl -> decl.getName().equals("decrypt")).findFirst().get();
    assertTrue(decrypt instanceof FunctionDeclaration);
    assertEquals("decrypt", decrypt.getName());
    Declaration main = declarations.stream().filter(decl -> decl.getName().equals("main")).findFirst().get();
    assertTrue(main instanceof FunctionDeclaration);
    assertEquals("main", main.getName());
}
Also used : FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) Declaration(de.fraunhofer.aisec.cpg.graph.declarations.Declaration) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) File(java.io.File) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) Test(org.junit.jupiter.api.Test) BaseTest(de.fraunhofer.aisec.cpg.BaseTest)

Example 5 with FunctionDeclaration

use of de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration in project cpg by Fraunhofer-AISEC.

the class FunctionPointerTest method test.

void test(String language) throws Exception {
    List<TranslationUnitDeclaration> result = analyze(language);
    List<FunctionDeclaration> functions = TestUtils.subnodesOfType(result, FunctionDeclaration.class);
    FunctionDeclaration main = TestUtils.findByUniqueName(functions, "main");
    List<CallExpression> calls = TestUtils.subnodesOfType(main, CallExpression.class);
    FunctionDeclaration noParam = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("target") && f.getParameters().isEmpty());
    FunctionDeclaration singleParam = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("target") && f.getParameters().size() == 1);
    FunctionDeclaration noParamUnknown = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("fun") && f.getParameters().isEmpty());
    FunctionDeclaration singleParamUnknown = TestUtils.findByUniquePredicate(functions, f -> f.getName().equals("fun") && f.getParameters().size() == 1);
    Pattern pattern = Pattern.compile("\\((?<member>.+)?\\*(?<obj>.+\\.)?(?<func>.+)\\)");
    for (CallExpression call : calls) {
        if (call instanceof ConstructExpression) {
            continue;
        }
        String func;
        if (!call.getName().contains("(")) {
            func = call.getName();
            assertNotEquals("", func, "Unexpected call " + func);
        } else {
            Matcher matcher = pattern.matcher(call.getName());
            assertTrue(matcher.matches(), "Unexpected call " + call.getName());
            func = matcher.group("func");
        }
        switch(func) {
            case "no_param":
            case "no_param_uninitialized":
            case "no_param_field":
            case "no_param_field_uninitialized":
                assertEquals(List.of(noParam), call.getInvokes());
                break;
            case "single_param":
            case "single_param_uninitialized":
            case "single_param_field":
            case "single_param_field_uninitialized":
                assertEquals(List.of(singleParam), call.getInvokes());
                break;
            case "no_param_unknown":
            case "no_param_unknown_uninitialized":
            case "no_param_unknown_field":
            case "no_param_unknown_field_uninitialized":
                assertEquals(List.of(noParamUnknown), call.getInvokes());
                assertTrue(noParamUnknown.isInferred());
                break;
            case "single_param_unknown":
            case "single_param_unknown_uninitialized":
            case "single_param_unknown_field":
            case "single_param_unknown_field_uninitialized":
                assertEquals(List.of(singleParamUnknown), call.getInvokes());
                assertTrue(singleParamUnknown.isInferred());
                break;
            default:
                fail("Unexpected call " + call.getName());
        }
        List<VariableDeclaration> variables = TestUtils.subnodesOfType(result, VariableDeclaration.class);
        for (VariableDeclaration variable : variables) {
            switch(variable.getName()) {
                case "no_param_unused":
                case "no_param_unused_field":
                case "no_param_unused_uninitialized":
                    assertEquals(noParam, getSourceFunction(variable));
                    break;
                case "single_param_unused":
                case "single_param_unused_field":
                case "single_param_unused_field_uninitialized":
                    assertEquals(singleParam, getSourceFunction(variable));
                    break;
            }
        }
    }
}
Also used : ConstructExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.ConstructExpression) FunctionDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) VariableDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration) TranslationUnitDeclaration(de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration) CallExpression(de.fraunhofer.aisec.cpg.graph.statements.expressions.CallExpression)

Aggregations

FunctionDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.FunctionDeclaration)28 TranslationUnitDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.TranslationUnitDeclaration)26 File (java.io.File)25 BaseTest (de.fraunhofer.aisec.cpg.BaseTest)24 Test (org.junit.jupiter.api.Test)24 VariableDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.VariableDeclaration)12 CompoundStatement (de.fraunhofer.aisec.cpg.graph.statements.CompoundStatement)10 DeclarationStatement (de.fraunhofer.aisec.cpg.graph.statements.DeclarationStatement)8 TryStatement (de.fraunhofer.aisec.cpg.graph.statements.TryStatement)8 CaseStatement (de.fraunhofer.aisec.cpg.graph.statements.CaseStatement)7 DefaultStatement (de.fraunhofer.aisec.cpg.graph.statements.DefaultStatement)7 ForEachStatement (de.fraunhofer.aisec.cpg.graph.statements.ForEachStatement)7 IfStatement (de.fraunhofer.aisec.cpg.graph.statements.IfStatement)7 ReturnStatement (de.fraunhofer.aisec.cpg.graph.statements.ReturnStatement)7 Statement (de.fraunhofer.aisec.cpg.graph.statements.Statement)7 SwitchStatement (de.fraunhofer.aisec.cpg.graph.statements.SwitchStatement)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 RecordDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.RecordDeclaration)4 ConstructorDeclaration (de.fraunhofer.aisec.cpg.graph.declarations.ConstructorDeclaration)3 Declaration (de.fraunhofer.aisec.cpg.graph.declarations.Declaration)3