Skip to content

Commit 79a7164

Browse files
authored
Merge pull request #10003 from michaelnebel/csharp/stubsenum
C#: Stub generator improvements.
2 parents 51a7243 + af473db commit 79a7164

File tree

108 files changed

+5986
-5909
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+5986
-5909
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -882,37 +882,37 @@ private newtype TCallingConvention =
882882
MkCallingConvention(int i) { function_pointer_calling_conventions(_, i) }
883883

884884
/**
885-
* Represents a signature calling convention. Specifies how arguments in a given
885+
* A signature representing a calling convention. Specifies how arguments in a given
886886
* signature are passed from the caller to the callee.
887887
*/
888888
class CallingConvention extends TCallingConvention {
889889
/** Gets a textual representation of this calling convention. */
890890
string toString() { result = "CallingConvention" }
891891
}
892892

893-
/** Managed calling convention with fixed-length argument list. */
893+
/** A managed calling convention with fixed-length argument list. */
894894
class DefaultCallingConvention extends CallingConvention {
895895
DefaultCallingConvention() { this = MkCallingConvention(0) }
896896

897897
override string toString() { result = "DefaultCallingConvention" }
898898
}
899899

900-
/** Unmanaged C/C++-style calling convention where the call stack is cleaned by the caller. */
900+
/** An unmanaged C/C++-style calling convention where the call stack is cleaned by the caller. */
901901
class CDeclCallingConvention extends CallingConvention {
902902
CDeclCallingConvention() { this = MkCallingConvention(1) }
903903

904904
override string toString() { result = "CDeclCallingConvention" }
905905
}
906906

907-
/** Unmanaged calling convention where call stack is cleaned up by the callee. */
907+
/** An unmanaged calling convention where call stack is cleaned up by the callee. */
908908
class StdCallCallingConvention extends CallingConvention {
909909
StdCallCallingConvention() { this = MkCallingConvention(2) }
910910

911911
override string toString() { result = "StdCallCallingConvention" }
912912
}
913913

914914
/**
915-
* Unmanaged C++-style calling convention for calling instance member functions
915+
* An unmanaged C++-style calling convention for calling instance member functions
916916
* with a fixed argument list.
917917
*/
918918
class ThisCallCallingConvention extends CallingConvention {
@@ -921,20 +921,30 @@ class ThisCallCallingConvention extends CallingConvention {
921921
override string toString() { result = "ThisCallCallingConvention" }
922922
}
923923

924-
/** Unmanaged calling convention where arguments are passed in registers when possible. */
924+
/** An unmanaged calling convention where arguments are passed in registers when possible. */
925925
class FastCallCallingConvention extends CallingConvention {
926926
FastCallCallingConvention() { this = MkCallingConvention(4) }
927927

928928
override string toString() { result = "FastCallCallingConvention" }
929929
}
930930

931-
/** Managed calling convention for passing extra arguments. */
931+
/** A managed calling convention for passing extra arguments. */
932932
class VarArgsCallingConvention extends CallingConvention {
933933
VarArgsCallingConvention() { this = MkCallingConvention(5) }
934934

935935
override string toString() { result = "VarArgsCallingConvention" }
936936
}
937937

938+
/**
939+
* An unmanaged calling convention that indicates that the specifics
940+
* are encoded as modopts.
941+
*/
942+
class UnmanagedCallingConvention extends CallingConvention {
943+
UnmanagedCallingConvention() { this = MkCallingConvention(9) }
944+
945+
override string toString() { result = "UnmanagedCallingConvention" }
946+
}
947+
938948
/**
939949
* A function pointer type, for example
940950
*

csharp/ql/src/Stubs/Stubs.qll

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ abstract private class GeneratedType extends Type, GeneratedElement {
160160

161161
private string stubBaseTypesString() {
162162
if this instanceof Enum
163-
then result = ""
163+
then result = " : " + this.(Enum).getUnderlyingType().toStringWithTypes()
164164
else
165165
if exists(this.getAnInterestingBaseType())
166166
then
@@ -502,23 +502,48 @@ private string stubClassName(Type t) {
502502
else
503503
if t instanceof TupleType
504504
then
505-
if t.(TupleType).getArity() < 2
506-
then result = stubClassName(t.(TupleType).getUnderlyingType())
507-
else
508-
result =
509-
"(" +
510-
concat(int i, Type element |
511-
element = t.(TupleType).getElementType(i)
512-
|
513-
stubClassName(element), "," order by i
514-
) + ")"
505+
exists(TupleType tt | tt = t |
506+
if tt.getArity() < 2
507+
then result = stubClassName(tt.getUnderlyingType())
508+
else
509+
result =
510+
"(" +
511+
concat(int i, Type element |
512+
element = tt.getElementType(i)
513+
|
514+
stubClassName(element), "," order by i
515+
) + ")"
516+
)
515517
else
516-
if t instanceof ValueOrRefType
518+
if t instanceof FunctionPointerType
517519
then
518-
result =
519-
stubQualifiedNamePrefix(t) + t.getUndecoratedName() +
520-
stubGenericArguments(t)
521-
else result = "<error>"
520+
exists(
521+
FunctionPointerType fpt, CallingConvention callconvention,
522+
string calltext
523+
|
524+
fpt = t
525+
|
526+
callconvention = fpt.getCallingConvention() and
527+
(
528+
if callconvention instanceof UnmanagedCallingConvention
529+
then calltext = "unmanaged"
530+
else calltext = ""
531+
) and
532+
result =
533+
"delegate* " + calltext + "<" +
534+
concat(int i, Parameter p |
535+
p = fpt.getParameter(i)
536+
|
537+
stubClassName(p.getType()) + "," order by i
538+
) + stubClassName(fpt.getReturnType()) + ">"
539+
)
540+
else
541+
if t instanceof ValueOrRefType
542+
then
543+
result =
544+
stubQualifiedNamePrefix(t) + t.getUndecoratedName() +
545+
stubGenericArguments(t)
546+
else result = "<error>"
522547
}
523548

524549
language[monotonicAggregates]
@@ -726,7 +751,7 @@ pragma[noinline]
726751
private string stubEnumConstant(EnumConstant ec, Assembly assembly) {
727752
ec instanceof GeneratedMember and
728753
ec.getALocation() = assembly and
729-
result = " " + escapeIfKeyword(ec.getName()) + ",\n"
754+
result = " " + escapeIfKeyword(ec.getName()) + " = " + ec.getValue() + ",\n"
730755
}
731756

732757
pragma[noinline]

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ type
1111
| file://:0:0:0:0 | delegate* default<Void*,Int32*> | int* | DefaultCallingConvention |
1212
| file://:0:0:0:0 | delegate* default<Void*,Object> | object | DefaultCallingConvention |
1313
| file://:0:0:0:0 | delegate* stdcall<Int32 ref,Object out,T,Void> | Void | StdCallCallingConvention |
14-
| file://:0:0:0:0 | delegate* unmanaged<Char*,IntPtr,Void> | Void | CallingConvention |
15-
| file://:0:0:0:0 | delegate* unmanaged<Int32,PosixSignal,Int32> | int | CallingConvention |
16-
| file://:0:0:0:0 | delegate* unmanaged<IntPtr,Int32> | int | CallingConvention |
17-
| file://:0:0:0:0 | delegate* unmanaged<IntPtr,Void> | Void | CallingConvention |
18-
| file://:0:0:0:0 | delegate* unmanaged<Void> | Void | CallingConvention |
14+
| file://:0:0:0:0 | delegate* unmanaged<Char*,IntPtr,Void> | Void | UnmanagedCallingConvention |
15+
| file://:0:0:0:0 | delegate* unmanaged<Int32,PosixSignal,Int32> | int | UnmanagedCallingConvention |
16+
| file://:0:0:0:0 | delegate* unmanaged<IntPtr,Int32> | int | UnmanagedCallingConvention |
17+
| file://:0:0:0:0 | delegate* unmanaged<IntPtr,Void> | Void | UnmanagedCallingConvention |
18+
| file://:0:0:0:0 | delegate* unmanaged<Void> | Void | UnmanagedCallingConvention |
1919
unmanagedCallingConvention
2020
parameter
2121
| file://:0:0:0:0 | delegate* default<A,B> | 0 | file://:0:0:0:0 | | A |

0 commit comments

Comments
 (0)