Skip to content

Commit f5b31ae

Browse files
author
AndreiDiaconu1
committed
Static fields
The translation of static fields now uses `VariableAddress` instead of `FieldAddress`. This fixes the logic as well as the "field address without qualifier address" sanity check.
1 parent 2151310 commit f5b31ae

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -833,16 +833,21 @@ abstract class TranslatedVariableAccess extends TranslatedNonConstantExpr {
833833

834834
class TranslatedNonFieldVariableAccess extends TranslatedVariableAccess {
835835
TranslatedNonFieldVariableAccess() {
836-
not expr instanceof FieldAccess and
837-
// If the parent expression is a `LocalVariableDeclAndInitExpr`,
838-
// then translate only the variables that are initializers (on the RHS)
839-
// and not the LHS (the address of the LHS is generated during
840-
// the translation of the initialization).
841836
(
842-
expr.getParent() instanceof LocalVariableDeclAndInitExpr
843-
implies
844-
expr = expr.getParent().(LocalVariableDeclAndInitExpr).getInitializer()
837+
not expr instanceof FieldAccess and
838+
// If the parent expression is a `LocalVariableDeclAndInitExpr`,
839+
// then translate only the variables that are initializers (on the RHS)
840+
// and not the LHS (the address of the LHS is generated during
841+
// the translation of the initialization).
842+
(
843+
expr.getParent() instanceof LocalVariableDeclAndInitExpr
844+
implies
845+
expr = expr.getParent().(LocalVariableDeclAndInitExpr).getInitializer()
846+
)
845847
)
848+
or
849+
// Static field accesses should be modeled as `TranslatedNonFieldAccess`
850+
expr.(FieldAccess).getTarget().isStatic()
846851
}
847852

848853
override Instruction getFirstInstruction() {
@@ -874,6 +879,11 @@ class TranslatedNonFieldVariableAccess extends TranslatedVariableAccess {
874879

875880
class TranslatedFieldAccess extends TranslatedVariableAccess {
876881
override FieldAccess expr;
882+
883+
TranslatedFieldAccess() {
884+
// Static field accesses should be modeled as `TranslatedNonFieldAccess`
885+
not expr.getTarget().isStatic()
886+
}
877887

878888
override Instruction getFirstInstruction() {
879889
// If there is a qualifier

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class PropClass
22
{
3-
private int prop;
3+
private static int prop;
44

55
public int Prop
66
{

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,12 +1505,11 @@ prop.cs:
15051505
# 12| mu0_5(Int32) = InitializeParameter[value] : &:r0_4
15061506
# 14| r0_6(glval<Int32>) = VariableAddress[value] :
15071507
# 14| r0_7(Int32) = Load : &:r0_6, ~mu0_2
1508-
# 14| r0_8(PropClass) = CopyValue : r0_3
1509-
# 14| r0_9(glval<Int32>) = FieldAddress[prop] : r0_8
1510-
# 14| mu0_10(Int32) = Store : &:r0_9, r0_7
1511-
# 12| v0_11(Void) = ReturnVoid :
1512-
# 12| v0_12(Void) = UnmodeledUse : mu*
1513-
# 12| v0_13(Void) = ExitFunction :
1508+
# 14| r0_8(glval<Int32>) = VariableAddress[prop] :
1509+
# 14| mu0_9(Int32) = Store : &:r0_8, r0_7
1510+
# 12| v0_10(Void) = ReturnVoid :
1511+
# 12| v0_11(Void) = UnmodeledUse : mu*
1512+
# 12| v0_12(Void) = ExitFunction :
15141513

15151514
# 18| System.Int32 PropClass.func()
15161515
# 18| Block 0

0 commit comments

Comments
 (0)