From 8cfd19cf2fea5e084058048795f75c85f576a061 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 31 Oct 2024 10:52:25 +0100 Subject: [PATCH 1/3] C++: Add multiple entry point test --- .../ir/multiple-entry-points/raw_ir.expected | 67 +++++++++++++++++++ .../ir/multiple-entry-points/raw_ir.ql | 6 ++ .../ir/multiple-entry-points/test1.cpp | 7 ++ .../ir/multiple-entry-points/test2.cpp | 3 + 4 files changed, 83 insertions(+) create mode 100644 cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected create mode 100644 cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.ql create mode 100644 cpp/ql/test/library-tests/ir/multiple-entry-points/test1.cpp create mode 100644 cpp/ql/test/library-tests/ir/multiple-entry-points/test2.cpp diff --git a/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected b/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected new file mode 100644 index 000000000000..2ded282e8d35 --- /dev/null +++ b/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected @@ -0,0 +1,67 @@ +test1.cpp: +# 3| int foo(int) +# 3| Block 0 +# 3| v3_1(void) = EnterFunction : +test2.cpp: +# 1| v3_1(void) = EnterFunction : +test1.cpp: +# 3| mu3_2(unknown) = AliasedDefinition : +test2.cpp: +# 1| mu3_2(unknown) = AliasedDefinition : +test1.cpp: +# 3| mu3_3(unknown) = InitializeNonLocal : +test2.cpp: +# 1| mu3_3(unknown) = InitializeNonLocal : +test1.cpp: +# 3| r3_4(glval) = VariableAddress[i] : +test2.cpp: +# 1| r3_4(glval) = VariableAddress[i] : +test1.cpp: +# 3| mu3_5(int) = InitializeParameter[i] : &:r1_4, &:r3_4 +test2.cpp: +# 1| mu3_5(int) = InitializeParameter[i] : &:r1_4, &:r3_4 +#-----| Goto -> Block 2 +#-----| Goto -> Block 2 + +# 1| Block 0 +#-----| Goto -> Block 2 +#-----| Goto -> Block 2 + +test1.cpp: +# 3| Block 1 +# 3| r3_6(glval) = VariableAddress[#return] : +test2.cpp: +# 1| r3_6(glval) = VariableAddress[#return] : +test1.cpp: +# 3| v3_7(void) = ReturnValue : &:r1_6, &:r3_6, ~m? +test2.cpp: +# 1| v3_7(void) = ReturnValue : &:r1_6, &:r3_6, ~m? +test1.cpp: +# 3| v3_8(void) = AliasedUse : ~m? +test2.cpp: +# 1| v3_8(void) = AliasedUse : ~m? +test1.cpp: +# 3| v3_9(void) = ExitFunction : +test2.cpp: +# 1| v3_9(void) = ExitFunction : + +# 1| Block 1 + +test1.cpp: +# 4| Block 2 +# 4| r4_1(glval) = VariableAddress[#return] : +# 4| r4_2(int) = Constant[42] : +# 4| mu4_3(int) = Store[#return] : &:r4_1, r4_2 +#-----| Goto -> Block 1 +#-----| Goto -> Block 1 + +test2.cpp: +# 2| Block 2 +# 2| r2_1(glval) = VariableAddress[#return] : +# 2| r2_2(glval) = VariableAddress[i] : +# 2| r2_3(int) = Load[i] : &:r2_2, ~m? +# 2| mu2_4(int) = Store[#return] : &:r2_1, r2_3 +#-----| Goto -> Block 1 +#-----| Goto -> Block 1 + +# 1| int foo(int) diff --git a/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.ql b/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.ql new file mode 100644 index 000000000000..cfc9da598a12 --- /dev/null +++ b/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.ql @@ -0,0 +1,6 @@ +/** + * @kind graph + */ + +private import cpp +private import semmle.code.cpp.ir.implementation.raw.PrintIR diff --git a/cpp/ql/test/library-tests/ir/multiple-entry-points/test1.cpp b/cpp/ql/test/library-tests/ir/multiple-entry-points/test1.cpp new file mode 100644 index 000000000000..463e9d34a868 --- /dev/null +++ b/cpp/ql/test/library-tests/ir/multiple-entry-points/test1.cpp @@ -0,0 +1,7 @@ +int foo(int i); + +int foo(int i) { + return 42; +} + +int bar(); diff --git a/cpp/ql/test/library-tests/ir/multiple-entry-points/test2.cpp b/cpp/ql/test/library-tests/ir/multiple-entry-points/test2.cpp new file mode 100644 index 000000000000..b61562ce2c3e --- /dev/null +++ b/cpp/ql/test/library-tests/ir/multiple-entry-points/test2.cpp @@ -0,0 +1,3 @@ +int foo(int i) { + return i; +} From 1a830cdf14ceb9fe46916b6495056dfdbe2eb26c Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Tue, 8 Oct 2024 16:11:27 +0200 Subject: [PATCH 2/3] C++: Do not generate IR for functions with multiple entry points in different locations --- .../cpp/ir/implementation/raw/internal/TranslatedElement.qll | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll index 0f9bc370f7a5..8e7e46c94c62 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll @@ -196,6 +196,8 @@ private predicate isInvalidFunction(Function func) { expr.getEnclosingFunction() = func and not exists(expr.getType()) ) + or + count(func.getEntryPoint().getLocation()) > 1 } /** From 5e3748ac128ce12649c66f7eb60bdbd66c447f0e Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Thu, 31 Oct 2024 10:57:12 +0100 Subject: [PATCH 3/3] C++: Update expected test results --- .../ir/multiple-entry-points/raw_ir.expected | 67 ------------------- 1 file changed, 67 deletions(-) diff --git a/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected b/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected index 2ded282e8d35..e69de29bb2d1 100644 --- a/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected +++ b/cpp/ql/test/library-tests/ir/multiple-entry-points/raw_ir.expected @@ -1,67 +0,0 @@ -test1.cpp: -# 3| int foo(int) -# 3| Block 0 -# 3| v3_1(void) = EnterFunction : -test2.cpp: -# 1| v3_1(void) = EnterFunction : -test1.cpp: -# 3| mu3_2(unknown) = AliasedDefinition : -test2.cpp: -# 1| mu3_2(unknown) = AliasedDefinition : -test1.cpp: -# 3| mu3_3(unknown) = InitializeNonLocal : -test2.cpp: -# 1| mu3_3(unknown) = InitializeNonLocal : -test1.cpp: -# 3| r3_4(glval) = VariableAddress[i] : -test2.cpp: -# 1| r3_4(glval) = VariableAddress[i] : -test1.cpp: -# 3| mu3_5(int) = InitializeParameter[i] : &:r1_4, &:r3_4 -test2.cpp: -# 1| mu3_5(int) = InitializeParameter[i] : &:r1_4, &:r3_4 -#-----| Goto -> Block 2 -#-----| Goto -> Block 2 - -# 1| Block 0 -#-----| Goto -> Block 2 -#-----| Goto -> Block 2 - -test1.cpp: -# 3| Block 1 -# 3| r3_6(glval) = VariableAddress[#return] : -test2.cpp: -# 1| r3_6(glval) = VariableAddress[#return] : -test1.cpp: -# 3| v3_7(void) = ReturnValue : &:r1_6, &:r3_6, ~m? -test2.cpp: -# 1| v3_7(void) = ReturnValue : &:r1_6, &:r3_6, ~m? -test1.cpp: -# 3| v3_8(void) = AliasedUse : ~m? -test2.cpp: -# 1| v3_8(void) = AliasedUse : ~m? -test1.cpp: -# 3| v3_9(void) = ExitFunction : -test2.cpp: -# 1| v3_9(void) = ExitFunction : - -# 1| Block 1 - -test1.cpp: -# 4| Block 2 -# 4| r4_1(glval) = VariableAddress[#return] : -# 4| r4_2(int) = Constant[42] : -# 4| mu4_3(int) = Store[#return] : &:r4_1, r4_2 -#-----| Goto -> Block 1 -#-----| Goto -> Block 1 - -test2.cpp: -# 2| Block 2 -# 2| r2_1(glval) = VariableAddress[#return] : -# 2| r2_2(glval) = VariableAddress[i] : -# 2| r2_3(int) = Load[i] : &:r2_2, ~m? -# 2| mu2_4(int) = Store[#return] : &:r2_1, r2_3 -#-----| Goto -> Block 1 -#-----| Goto -> Block 1 - -# 1| int foo(int)