Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d9b3ec

Browse files
committed
Allow annotating fnptr return types
1 parent 348fe8f commit 1d9b3ec

5 files changed

Lines changed: 22 additions & 17 deletions

File tree

csharp/ql/src/semmle/code/csharp/AnnotatedType.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ private module Annotations {
231231
type = element.(Expr).getType()
232232
or
233233
type = element.(DelegateType).getReturnType()
234+
or
235+
type = element.(FunctionPointerType).getReturnType()
234236
)
235237
}
236238
}

csharp/ql/src/semmle/code/csharp/Type.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,9 @@ class FunctionPointerType extends Type, Parameterizable, @function_pointer_type
880880
/** Gets an unmanaged calling convention. */
881881
Type getAnUnmanagedCallingConvention() { result = getUnmanagedCallingConvention(_) }
882882

883+
/** Gets the annotated return type of this function pointer type. */
884+
AnnotatedType getAnnotatedReturnType() { result.appliesTo(this) }
885+
883886
override string getAPrimaryQlClass() { result = "FunctionPointerType" }
884887
}
885888

csharp/ql/src/semmlecode.csharp.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ type_mention_location(
466466
unique int id: @type_mention ref,
467467
int loc: @location ref);
468468

469-
@has_type_annotation = @assignable | @type_parameter | @callable | @expr | @delegate_type | @generic;
469+
@has_type_annotation = @assignable | @type_parameter | @callable | @expr | @delegate_type | @generic | @function_pointer_type;
470470

471471
/**
472472
* A direct annotation on an entity, for example `string? x;`.

csharp/ql/test/library-tests/csharp9/FunctionPointer.expected

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
type
22
| file://:0:0:0:0 | delegate* default<A,B> | B | DefaultCallingConvention |
33
| file://:0:0:0:0 | delegate* default<B,A> | A | DefaultCallingConvention |
4-
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | Int32 | DefaultCallingConvention |
5-
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 ref readonly> | Int32 | DefaultCallingConvention |
4+
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | ref int | DefaultCallingConvention |
5+
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 ref readonly> | readonly int | DefaultCallingConvention |
66
| file://:0:0:0:0 | delegate* default<Int32*,Void*> | Void* | DefaultCallingConvention |
7-
| file://:0:0:0:0 | delegate* default<Int32> | Int32 | DefaultCallingConvention |
8-
| file://:0:0:0:0 | delegate* default<T,Int32> | Int32 | DefaultCallingConvention |
9-
| file://:0:0:0:0 | delegate* default<Void*,Int32*> | Int32* | DefaultCallingConvention |
7+
| file://:0:0:0:0 | delegate* default<Int32> | int | DefaultCallingConvention |
8+
| file://:0:0:0:0 | delegate* default<T,Int32> | int | DefaultCallingConvention |
9+
| file://:0:0:0:0 | delegate* default<Void*,Int32*> | int* | DefaultCallingConvention |
1010
| file://:0:0:0:0 | delegate* stdcall<Int32 ref,Object out,T,Void> | Void | StdCallCallingConvention |
1111
unmanagedCallingConvention
1212
parameter
1313
| file://:0:0:0:0 | delegate* default<A,B> | 0 | file://:0:0:0:0 | | A |
1414
| file://:0:0:0:0 | delegate* default<B,A> | 0 | file://:0:0:0:0 | | B |
15-
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | 0 | file://:0:0:0:0 | | Int32 |
16-
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | 1 | file://:0:0:0:0 | `1 | Object |
17-
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | 2 | file://:0:0:0:0 | `2 | Int32 |
18-
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 ref readonly> | 0 | file://:0:0:0:0 | | Int32 |
19-
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 ref readonly> | 1 | file://:0:0:0:0 | `1 | Object |
20-
| file://:0:0:0:0 | delegate* default<Int32*,Void*> | 0 | file://:0:0:0:0 | | Int32* |
15+
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | 0 | file://:0:0:0:0 | | ref int! |
16+
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | 1 | file://:0:0:0:0 | `1 | out object? |
17+
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 in,Int32 ref> | 2 | file://:0:0:0:0 | `2 | readonly int! |
18+
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 ref readonly> | 0 | file://:0:0:0:0 | | ref int! |
19+
| file://:0:0:0:0 | delegate* default<Int32 ref,Object out,Int32 ref readonly> | 1 | file://:0:0:0:0 | `1 | out object? |
20+
| file://:0:0:0:0 | delegate* default<Int32*,Void*> | 0 | file://:0:0:0:0 | | int*! |
2121
| file://:0:0:0:0 | delegate* default<T,Int32> | 0 | file://:0:0:0:0 | | T |
22-
| file://:0:0:0:0 | delegate* default<Void*,Int32*> | 0 | file://:0:0:0:0 | | Void* |
23-
| file://:0:0:0:0 | delegate* stdcall<Int32 ref,Object out,T,Void> | 0 | file://:0:0:0:0 | | Int32 |
24-
| file://:0:0:0:0 | delegate* stdcall<Int32 ref,Object out,T,Void> | 1 | file://:0:0:0:0 | `1 | Object |
22+
| file://:0:0:0:0 | delegate* default<Void*,Int32*> | 0 | file://:0:0:0:0 | | Void*! |
23+
| file://:0:0:0:0 | delegate* stdcall<Int32 ref,Object out,T,Void> | 0 | file://:0:0:0:0 | | ref int! |
24+
| file://:0:0:0:0 | delegate* stdcall<Int32 ref,Object out,T,Void> | 1 | file://:0:0:0:0 | `1 | out object? |
2525
| file://:0:0:0:0 | delegate* stdcall<Int32 ref,Object out,T,Void> | 2 | file://:0:0:0:0 | `2 | T |
2626
invocation
2727
| FunctionPointer.cs:19:21:19:43 | function pointer call |

csharp/ql/test/library-tests/csharp9/FunctionPointer.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import csharp
22

33
query predicate type(FunctionPointerType fpt, string returnType, string callingConvention) {
4-
fpt.getReturnType().toString() = returnType and
4+
fpt.getAnnotatedReturnType().toString() = returnType and
55
fpt.getCallingConvention().toString() = callingConvention
66
}
77

@@ -10,7 +10,7 @@ query predicate unmanagedCallingConvention(FunctionPointerType fpt, int i, strin
1010
}
1111

1212
query predicate parameter(FunctionPointerType fpt, int i, Parameter p, string t) {
13-
fpt.getParameter(i) = p and p.getType().toString() = t
13+
fpt.getParameter(i) = p and p.getAnnotatedType().toString() = t
1414
}
1515

1616
query predicate invocation(FunctionPointerCall fpc) { any() }

0 commit comments

Comments
 (0)