Skip to content

Commit 320cd6b

Browse files
author
AndreiDiaconu1
committed
More PR fixes
1 parent 7654144 commit 320cd6b

File tree

10 files changed

+38
-42
lines changed

10 files changed

+38
-42
lines changed

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ private import TranslatedStmt
1111
private import IRConstruction
1212
private import semmle.code.csharp.ir.Util
1313
private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
14+
private import desugar.Foreach
15+
private import desugar.Delegate
16+
private import desugar.Lock
1417

1518
/**
1619
* Gets the built-in `int` type.
@@ -23,14 +26,14 @@ ArrayType getArrayOfDim(int dim, Type type) {
2326
}
2427

2528
private predicate canCreateCompilerGeneratedElement(Element generatedBy, int nth) {
26-
(
27-
generatedBy instanceof ForeachStmt or
28-
generatedBy instanceof LockStmt or
29-
generatedBy instanceof DelegateCreation or
30-
generatedBy instanceof DelegateCall
31-
) and
32-
// For now we allow a max of 15 compiler generated elements
33-
nth in [0 .. 14]
29+
generatedBy instanceof ForeachStmt and nth in [0 .. ForeachElements::noGeneratedElements()]
30+
or
31+
generatedBy instanceof LockStmt and nth in [0 .. LockElements::noGeneratedElements()]
32+
or
33+
generatedBy instanceof DelegateCreation and
34+
nth in [0 .. DelegateElements::noGeneratedElements(generatedBy)]
35+
or
36+
generatedBy instanceof DelegateCall and nth in [0 .. DelegateElements::noGeneratedElements(generatedBy)]
3437
}
3538

3639
/**

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedFunction.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class TranslatedFunction extends TranslatedElement, TTranslatedFunction {
165165
isLValue = false and
166166
(
167167
// Only generate the `Unwind` instruction if there is any exception
168-
// handling present in the function.
168+
// handling present in the function (compiler generated or not).
169169
exists(TryStmt try | try.getEnclosingCallable() = callable) or
170170
exists(ThrowStmt throw | throw.getEnclosingCallable() = callable)
171171
)

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/common/TranslatedDeclarationBase.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ abstract class LocalVariableDeclarationBase extends TranslatedElement {
113113
abstract TranslatedElement getInitialization();
114114

115115
/**
116-
* Predicate that holds if a declaration is not explicitly initialized,
116+
* Holds if a declaration is not explicitly initialized,
117117
* but will be implicitly initialized by an element.
118118
*/
119119
abstract predicate isInitializedByElement();

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/desugar/Delegate.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ module DelegateElements {
3131
}
3232

3333
TranslatedDelegateInvokeCall getInvoke(DelegateCall generatedBy) { result.getAST() = generatedBy }
34+
35+
int noGeneratedElements(Element generatedBy) {
36+
generatedBy instanceof DelegateCreation and result = 1
37+
or
38+
generatedBy instanceof DelegateCall and result = 1
39+
}
3440
}
3541

3642
/**

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/desugar/Foreach.qll

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ module ForeachElements {
5858
TranslatedForeachTry getTry(ForeachStmt generatedBy) { result.getAST() = generatedBy }
5959

6060
TranslatedForeachEnumerator getEnumDecl(ForeachStmt generatedBy) { result.getAST() = generatedBy }
61+
62+
int noGeneratedElements() { result = 12 }
6163
}
6264

6365
private class TranslatedForeachTry extends TranslatedCompilerGeneratedTry,
@@ -206,17 +208,14 @@ private class TranslatedForeachGetEnumerator extends TranslatedCompilerGenerated
206208

207209
override Callable getInstructionFunction(InstructionTag tag) {
208210
tag = CallTargetTag() and
209-
exists(Callable internal |
210-
internal.getName() = "GetEnumerator" and
211-
// TODO: For now ignore the possibility that the
212-
// foreach variable can have a generic type.
213-
// The type of the callable will need to be fabricated,
214-
// since we might not find the correct callable in the DB.
215-
// Probably will have change the way the immediate
216-
// operand of `FunctionAddress` is calculated.
217-
internal.getReturnType().getName() = "IEnumerator" and
218-
result = internal
219-
)
211+
result.getName() = "GetEnumerator" and
212+
// TODO: For now ignore the possibility that the
213+
// foreach variable can have a generic type.
214+
// The type of the callable will need to be fabricated,
215+
// since we might not find the correct callable in the DB.
216+
// Probably will have change the way the immediate
217+
// operand of `FunctionAddress` is calculated.
218+
result.getReturnType().getName() = "IEnumerator"
220219
}
221220

222221
override TranslatedExpr getArgument(int id) { none() }

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/desugar/Lock.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ module LockElements {
4848
TranslatedLockWasTakenDecl getLockWasTakenDecl(LockStmt generatedBy) {
4949
result.getAST() = generatedBy
5050
}
51+
52+
int noGeneratedElements() { result = 13 }
5153
}
5254

5355
/**

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/desugar/internal/TranslatedCompilerGeneratedElement.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* which represents the element that generated the compiler generated element.
44
*/
55

6-
import csharp
76
private import semmle.code.csharp.ir.implementation.raw.internal.TranslatedElement
87
private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
98

csharp/ql/src/semmle/code/csharp/ir/internal/IRUtilities.qll

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
private import csharp
22

3-
/**
4-
* Given a type, get the type that would result by applying "pointer decay".
5-
* A function type becomes a pointer to that function type, and an array type
6-
* becomes a pointer to the element type of the array. If the specified type
7-
* is not subject to pointer decay, this predicate does not hold.
8-
*/
9-
// TODO: Only pointer to array decay in C#?
10-
private Type getDecayedType(Type type) {
11-
result.(PointerType).getReferentType() = type.(ArrayType).getElementType()
12-
}
13-
143
/**
154
* Get the actual type of the specified variable, as opposed to the declared type.
165
* This returns the type of the variable after any pointer decay is applied, and
@@ -21,9 +10,7 @@ Type getVariableType(Variable v) {
2110
declaredType = v.getType() and
2211
if v instanceof Parameter
2312
then
24-
result = getDecayedType(declaredType)
25-
or
26-
not exists(getDecayedType(declaredType)) and result = declaredType
13+
result = declaredType
2714
else
2815
if declaredType instanceof ArrayType
2916
then

csharp/ql/test/library-tests/ir/ir/lock.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ class LockTest
44
{
55
static void A()
66
{
7-
object _object = new object();
8-
lock (_object)
7+
object @object = new object();
8+
lock (@object)
99
{
10-
Console.WriteLine(_object.ToString());
10+
Console.WriteLine(@object.ToString());
1111
}
1212
}
1313
}

csharp/ql/test/library-tests/ir/ir/raw_ir.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,14 +609,14 @@ lock.cs:
609609
# 5| v0_0(Void) = EnterFunction :
610610
# 5| mu0_1(null) = AliasedDefinition :
611611
# 5| mu0_2(null) = UnmodeledDefinition :
612-
# 7| r0_3(glval<Object>) = VariableAddress[_object] :
612+
# 7| r0_3(glval<Object>) = VariableAddress[object] :
613613
# 7| r0_4(Object) = NewObj :
614614
# 7| r0_5(glval<null>) = FunctionAddress[Object] :
615615
# 7| v0_6(Void) = Call : func:r0_5, this:r0_4
616616
# 7| mu0_7(null) = ^CallSideEffect : ~mu0_2
617617
# 7| mu0_8(Object) = Store : &:r0_3, r0_4
618618
# 8| r0_9(glval<Object>) = VariableAddress[#temp8:9] :
619-
# 8| r0_10(glval<Object>) = VariableAddress[_object] :
619+
# 8| r0_10(glval<Object>) = VariableAddress[object] :
620620
# 8| r0_11(Object) = Load : &:r0_10, ~mu0_2
621621
# 8| mu0_12(Object) = Store : &:r0_9, r0_11
622622
# 8| r0_13(glval<Boolean>) = VariableAddress[#temp8:9] :
@@ -629,7 +629,7 @@ lock.cs:
629629
# 8| v0_20(Void) = Call : func:r0_16, 0:r0_18, 1:r0_19
630630
# 8| mu0_21(null) = ^CallSideEffect : ~mu0_2
631631
# 10| r0_22(glval<null>) = FunctionAddress[WriteLine] :
632-
# 10| r0_23(glval<Object>) = VariableAddress[_object] :
632+
# 10| r0_23(glval<Object>) = VariableAddress[object] :
633633
# 10| r0_24(Object) = Load : &:r0_23, ~mu0_2
634634
# 10| r0_25(glval<null>) = FunctionAddress[ToString] :
635635
# 10| r0_26(String) = Call : func:r0_25, this:r0_24

0 commit comments

Comments
 (0)