diff --git a/cpp/ql/lib/semmle/code/cpp/ir/internal/IRUtilities.qll b/cpp/ql/lib/semmle/code/cpp/ir/internal/IRUtilities.qll index c42f734a62ac..40d3f3400358 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/internal/IRUtilities.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/internal/IRUtilities.qll @@ -20,31 +20,43 @@ predicate isNonReferenceStructuredBinding(Variable v) { not v.getUnspecifiedType() instanceof ReferenceType } +/** + * Gets the type that `v` is declared with. + */ +private Type getDeclaredType(Variable v) { + result = v.(Parameter).getDefinition().getType() + or + not v.(Parameter).hasDefinition() and + result = v.getType() +} + /** * Get the actual type of the specified variable, as opposed to the declared type. * This returns the type of the variable after any pointer decay is applied, and * after any unsized array type has its size inferred from the initializer. */ Type getVariableType(Variable v) { - exists(Type declaredType | - declaredType = v.getUnspecifiedType() and + exists(Type declaredType, Type unspecifiedType | + declaredType = getDeclaredType(v) and + unspecifiedType = declaredType.getUnspecifiedType() + | if v instanceof Parameter then - result = getDecayedType(declaredType) + result = getDecayedType(unspecifiedType) or - not exists(getDecayedType(declaredType)) and result = v.getType() + not exists(getDecayedType(unspecifiedType)) and result = declaredType else - if declaredType instanceof ArrayType and not declaredType.(ArrayType).hasArraySize() + if unspecifiedType instanceof ArrayType and not unspecifiedType.(ArrayType).hasArraySize() then result = v.getInitializer().getExpr().getType() or - not exists(v.getInitializer()) and result = v.getType() + not exists(v.getInitializer()) and result = declaredType else if isNonReferenceStructuredBinding(v) then // The extractor ensures `r` exists when `isNonReferenceStructuredBinding(v)` holds. - exists(LValueReferenceType r | r.getBaseType() = v.getUnspecifiedType() | result = r) - else result = v.getType() + exists(LValueReferenceType r | r.getBaseType() = unspecifiedType | result = r) + else result = declaredType ) } diff --git a/cpp/ql/test/library-tests/ir/parameter-types/declarations.cpp b/cpp/ql/test/library-tests/ir/parameter-types/declarations.cpp new file mode 100644 index 000000000000..590304b4abc9 --- /dev/null +++ b/cpp/ql/test/library-tests/ir/parameter-types/declarations.cpp @@ -0,0 +1,14 @@ +// The parameter is declared as `char *argv[]` in `test.cpp`. +void array_param_with_pointer_declaration(char **argv); + +// The parameter is declared as `char **argv` in `test.cpp`. +void pointer_param_with_array_declaration(char *argv[]); + +// The parameter is declared as `void f()` in `test.cpp`. +void function_param_with_pointer_declaration(void (*f)()); + +// The parameter is declared as `const int x` in `test.cpp`. +void const_param_with_non_const_declaration(int x); + +// The parameter is declared as `size_t n` in `test.cpp`. +void typedef_param_with_underlying_type_declaration(unsigned long n); diff --git a/cpp/ql/test/library-tests/ir/parameter-types/parameter-types.expected b/cpp/ql/test/library-tests/ir/parameter-types/parameter-types.expected new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/cpp/ql/test/library-tests/ir/parameter-types/parameter-types.ql b/cpp/ql/test/library-tests/ir/parameter-types/parameter-types.ql new file mode 100644 index 000000000000..4bb873bc990d --- /dev/null +++ b/cpp/ql/test/library-tests/ir/parameter-types/parameter-types.ql @@ -0,0 +1,19 @@ +private import cpp +private import semmle.code.cpp.ir.IR +import utils.test.InlineExpectationsTest + +module ParameterTypesTest implements TestSig { + string getARelevantTag() { result = "type" } + + predicate hasActualResult(Location location, string element, string tag, string value) { + exists(VariableAddressInstruction va, InitializeParameterInstruction init | + init.getAnOperand().getDef() = va and + location = va.getLocation() and + element = va.toString() and + tag = "type" and + value = "\"" + va.getResultLanguageType().toString() + "\"" + ) + } +} + +import MakeTest diff --git a/cpp/ql/test/library-tests/ir/parameter-types/test.cpp b/cpp/ql/test/library-tests/ir/parameter-types/test.cpp new file mode 100644 index 000000000000..c9c116535f57 --- /dev/null +++ b/cpp/ql/test/library-tests/ir/parameter-types/test.cpp @@ -0,0 +1,27 @@ +typedef unsigned long size_t; + +void pointer_param(char **argv) { // $ type="glval" +} + +void array_param(char *argv[]) { // $ type="glval" +} + +// The parameter is declared as `char **argv` in `declarations.cpp`. +void array_param_with_pointer_declaration(char *argv[]) { // $ type="glval" +} + +// The parameter is declared as `char *argv[]` in `declarations.cpp`. +void pointer_param_with_array_declaration(char **argv) { // $ type="glval" +} + +// The parameter is declared as `void (*f)()` in `declarations.cpp`. +void function_param_with_pointer_declaration(void f()) { // $ type="glval<..(*)(..)>" +} + +// The parameter is declared as `int x` in `declarations.cpp`. +void const_param_with_non_const_declaration(const int x) { // $ type="glval" +} + +// The parameter is declared as `unsigned long n` in `declarations.cpp`. +void typedef_param_with_underlying_type_declaration(size_t n) { // $ type="glval" +} diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/UnboundedWrite.expected b/cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/UnboundedWrite.expected index 5c10f6e059d1..d9285b3286b5 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/UnboundedWrite.expected +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/UnboundedWrite.expected @@ -4,36 +4,20 @@ edges | main.cpp:6:27:6:30 | **argv | main.cpp:9:29:9:32 | **argv | provenance | | | main.cpp:6:27:6:30 | **argv | main.cpp:10:20:10:23 | **argv | provenance | | | main.cpp:7:33:7:36 | **argv | main.cpp:7:33:7:36 | overflowdesination_main output argument | provenance | | -| main.cpp:7:33:7:36 | **argv | main.cpp:7:33:7:36 | overflowdesination_main output argument | provenance | | | main.cpp:7:33:7:36 | **argv | overflowdestination.cpp:23:45:23:48 | **argv | provenance | | | main.cpp:7:33:7:36 | overflowdesination_main output argument | main.cpp:8:34:8:37 | **argv | provenance | | -| main.cpp:7:33:7:36 | overflowdesination_main output argument | main.cpp:8:34:8:37 | *argv | provenance | | | main.cpp:7:33:7:36 | overflowdesination_main output argument | main.cpp:9:29:9:32 | **argv | provenance | | -| main.cpp:7:33:7:36 | overflowdesination_main output argument | main.cpp:9:29:9:32 | *argv | provenance | | | main.cpp:7:33:7:36 | overflowdesination_main output argument | main.cpp:10:20:10:23 | **argv | provenance | | -| main.cpp:7:33:7:36 | overflowdesination_main output argument | main.cpp:10:20:10:23 | *argv | provenance | | -| main.cpp:8:34:8:37 | **argv | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | provenance | | | main.cpp:8:34:8:37 | **argv | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | provenance | | | main.cpp:8:34:8:37 | **argv | test_buffer_overrun.cpp:32:46:32:49 | **argv | provenance | | -| main.cpp:8:34:8:37 | *argv | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | provenance | | -| main.cpp:8:34:8:37 | *argv | test_buffer_overrun.cpp:32:46:32:49 | *argv | provenance | | | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | main.cpp:9:29:9:32 | **argv | provenance | | -| main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | main.cpp:9:29:9:32 | *argv | provenance | | | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | main.cpp:10:20:10:23 | **argv | provenance | | -| main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | main.cpp:10:20:10:23 | *argv | provenance | | | main.cpp:9:29:9:32 | **argv | main.cpp:9:29:9:32 | tests_restrict_main output argument | provenance | | | main.cpp:9:29:9:32 | **argv | tests_restrict.c:15:41:15:44 | **argv | provenance | | -| main.cpp:9:29:9:32 | *argv | main.cpp:9:29:9:32 | tests_restrict_main output argument | provenance | | -| main.cpp:9:29:9:32 | *argv | tests_restrict.c:15:41:15:44 | *argv | provenance | | | main.cpp:9:29:9:32 | tests_restrict_main output argument | main.cpp:10:20:10:23 | **argv | provenance | | -| main.cpp:9:29:9:32 | tests_restrict_main output argument | main.cpp:10:20:10:23 | *argv | provenance | | | main.cpp:10:20:10:23 | **argv | tests.cpp:1074:32:1074:35 | **argv | provenance | | -| main.cpp:10:20:10:23 | *argv | tests.cpp:1074:32:1074:35 | *argv | provenance | | | overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | **argv | provenance | | -| overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | *argv | provenance | | | test_buffer_overrun.cpp:32:46:32:49 | **argv | test_buffer_overrun.cpp:32:46:32:49 | **argv | provenance | | -| test_buffer_overrun.cpp:32:46:32:49 | **argv | test_buffer_overrun.cpp:32:46:32:49 | *argv | provenance | | -| test_buffer_overrun.cpp:32:46:32:49 | *argv | test_buffer_overrun.cpp:32:46:32:49 | *argv | provenance | | | tests.cpp:634:19:634:24 | *source | tests.cpp:636:17:636:22 | *source | provenance | | | tests.cpp:643:19:643:24 | *source | tests.cpp:646:2:646:16 | *... = ... | provenance | | | tests.cpp:646:2:646:2 | *s [post update] [*home] | tests.cpp:649:14:649:14 | *s [*home] | provenance | | @@ -43,35 +27,22 @@ edges | tests.cpp:649:16:649:19 | *home | tests.cpp:649:14:649:19 | *home | provenance | | | tests.cpp:1074:32:1074:35 | **argv | tests.cpp:1099:9:1099:15 | *access to array | provenance | | | tests.cpp:1074:32:1074:35 | **argv | tests.cpp:1100:9:1100:15 | *access to array | provenance | | -| tests.cpp:1074:32:1074:35 | *argv | tests.cpp:1099:9:1099:15 | *access to array | provenance | | -| tests.cpp:1074:32:1074:35 | *argv | tests.cpp:1100:9:1100:15 | *access to array | provenance | | | tests.cpp:1099:9:1099:15 | *access to array | tests.cpp:634:19:634:24 | *source | provenance | | | tests.cpp:1100:9:1100:15 | *access to array | tests.cpp:643:19:643:24 | *source | provenance | | | tests_restrict.c:15:41:15:44 | **argv | tests_restrict.c:15:41:15:44 | **argv | provenance | | -| tests_restrict.c:15:41:15:44 | *argv | tests_restrict.c:15:41:15:44 | *argv | provenance | | nodes | main.cpp:6:27:6:30 | **argv | semmle.label | **argv | | main.cpp:7:33:7:36 | **argv | semmle.label | **argv | | main.cpp:7:33:7:36 | overflowdesination_main output argument | semmle.label | overflowdesination_main output argument | -| main.cpp:7:33:7:36 | overflowdesination_main output argument | semmle.label | overflowdesination_main output argument | | main.cpp:8:34:8:37 | **argv | semmle.label | **argv | -| main.cpp:8:34:8:37 | *argv | semmle.label | *argv | -| main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | semmle.label | test_buffer_overrun_main output argument | | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | semmle.label | test_buffer_overrun_main output argument | | main.cpp:9:29:9:32 | **argv | semmle.label | **argv | -| main.cpp:9:29:9:32 | *argv | semmle.label | *argv | -| main.cpp:9:29:9:32 | tests_restrict_main output argument | semmle.label | tests_restrict_main output argument | | main.cpp:9:29:9:32 | tests_restrict_main output argument | semmle.label | tests_restrict_main output argument | | main.cpp:10:20:10:23 | **argv | semmle.label | **argv | -| main.cpp:10:20:10:23 | *argv | semmle.label | *argv | | overflowdestination.cpp:23:45:23:48 | **argv | semmle.label | **argv | | overflowdestination.cpp:23:45:23:48 | **argv | semmle.label | **argv | -| overflowdestination.cpp:23:45:23:48 | *argv | semmle.label | *argv | | test_buffer_overrun.cpp:32:46:32:49 | **argv | semmle.label | **argv | | test_buffer_overrun.cpp:32:46:32:49 | **argv | semmle.label | **argv | -| test_buffer_overrun.cpp:32:46:32:49 | *argv | semmle.label | *argv | -| test_buffer_overrun.cpp:32:46:32:49 | *argv | semmle.label | *argv | -| test_buffer_overrun.cpp:32:46:32:49 | *argv | semmle.label | *argv | | tests.cpp:634:19:634:24 | *source | semmle.label | *source | | tests.cpp:636:17:636:22 | *source | semmle.label | *source | | tests.cpp:643:19:643:24 | *source | semmle.label | *source | @@ -81,21 +52,14 @@ nodes | tests.cpp:649:14:649:19 | *home | semmle.label | *home | | tests.cpp:649:16:649:19 | *home | semmle.label | *home | | tests.cpp:1074:32:1074:35 | **argv | semmle.label | **argv | -| tests.cpp:1074:32:1074:35 | *argv | semmle.label | *argv | | tests.cpp:1099:9:1099:15 | *access to array | semmle.label | *access to array | | tests.cpp:1100:9:1100:15 | *access to array | semmle.label | *access to array | | tests_restrict.c:15:41:15:44 | **argv | semmle.label | **argv | | tests_restrict.c:15:41:15:44 | **argv | semmle.label | **argv | -| tests_restrict.c:15:41:15:44 | *argv | semmle.label | *argv | -| tests_restrict.c:15:41:15:44 | *argv | semmle.label | *argv | subpaths | main.cpp:7:33:7:36 | **argv | overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | **argv | main.cpp:7:33:7:36 | overflowdesination_main output argument | -| main.cpp:7:33:7:36 | **argv | overflowdestination.cpp:23:45:23:48 | **argv | overflowdestination.cpp:23:45:23:48 | *argv | main.cpp:7:33:7:36 | overflowdesination_main output argument | | main.cpp:8:34:8:37 | **argv | test_buffer_overrun.cpp:32:46:32:49 | **argv | test_buffer_overrun.cpp:32:46:32:49 | **argv | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | -| main.cpp:8:34:8:37 | **argv | test_buffer_overrun.cpp:32:46:32:49 | **argv | test_buffer_overrun.cpp:32:46:32:49 | *argv | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | -| main.cpp:8:34:8:37 | *argv | test_buffer_overrun.cpp:32:46:32:49 | *argv | test_buffer_overrun.cpp:32:46:32:49 | *argv | main.cpp:8:34:8:37 | test_buffer_overrun_main output argument | | main.cpp:9:29:9:32 | **argv | tests_restrict.c:15:41:15:44 | **argv | tests_restrict.c:15:41:15:44 | **argv | main.cpp:9:29:9:32 | tests_restrict_main output argument | -| main.cpp:9:29:9:32 | *argv | tests_restrict.c:15:41:15:44 | *argv | tests_restrict.c:15:41:15:44 | *argv | main.cpp:9:29:9:32 | tests_restrict_main output argument | #select | tests.cpp:636:2:636:7 | call to strcpy | main.cpp:6:27:6:30 | **argv | tests.cpp:636:17:636:22 | *source | This 'call to strcpy' with input from $@ may overflow the destination. | main.cpp:6:27:6:30 | **argv | a command-line argument | | tests.cpp:649:2:649:7 | call to strcpy | main.cpp:6:27:6:30 | **argv | tests.cpp:649:14:649:19 | *home | This 'call to strcpy' with input from $@ may overflow the destination. | main.cpp:6:27:6:30 | **argv | a command-line argument |