Binary: Extract field references. · microsoft/codeql@79817dc · GitHub
Skip to content

Commit 79817dc

Browse files
committed
Binary: Extract field references.
1 parent 4378da4 commit 79817dc

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎binary/extractor/cil/Semmle.Extraction.CSharp.IL/ILExtractor.cs‎

Lines changed: 14 additions & 0 deletions

‎binary/ql/lib/semmle/code/binary/ast/internal/CilInstructions.qll‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
private import binary
22

3+
class CilField extends @field {
4+
string toString() { result = this.getName() }
5+
6+
string getName() { fields(this, result, _) }
7+
8+
CilType getDeclaringType() { fields(this, _, result) }
9+
10+
Location getLocation() { none() } // TODO: Extract
11+
}
12+
313
/**
414
* A CIL type (class, struct, interface, etc.).
515
*/
@@ -18,6 +28,9 @@ class CilType extends @type {
1828
/** Gets a method declared in this type. */
1929
CilMethod getAMethod() { result.getDeclaringType() = this }
2030

31+
/** Gets a field declared by this type. */
32+
CilField getAField() { result.getDeclaringType() = this }
33+
2134
Location getLocation() { none() } // TODO: Extract
2235
}
2336

@@ -625,14 +638,33 @@ class CilLdsfld extends @il_ldsfld, CilLoadFieldInstruction { }
625638
/** An instruction that loads the address of a static field. */
626639
class CilLdsflda extends @il_ldsflda, CilLoadFieldInstruction { }
627640

641+
pragma[nomagic]
642+
private predicate fieldHasDeclaringTypeNameAndName(
643+
string declaringTypeName, string fieldName, CilField f
644+
) {
645+
f.getDeclaringType().getFullName() = declaringTypeName and
646+
f.getName() = fieldName
647+
}
648+
628649
/** An instruction that stores a value to a field. */
629-
abstract class CilStoreFieldInstruction extends CilInstruction { }
650+
abstract class CilStoreFieldInstruction extends CilInstruction {
651+
CilField getField() {
652+
exists(string declaringTypeName, string fieldName |
653+
il_field_operand(this, declaringTypeName, fieldName) and
654+
fieldHasDeclaringTypeNameAndName(declaringTypeName, fieldName, result)
655+
)
656+
}
657+
658+
predicate isStatic() { none() }
659+
}
630660

631661
/** An instruction that stores a value to an instance field. */
632662
class CilStfld extends @il_stfld, CilStoreFieldInstruction { }
633663

634664
/** An instruction that stores a value to a static field. */
635-
class CilStsfld extends @il_stsfld, CilStoreFieldInstruction { }
665+
class CilStsfld extends @il_stsfld, CilStoreFieldInstruction {
666+
final override predicate isStatic() { any() }
667+
}
636668

637669
class CilStobj extends @il_stobj, CilInstruction { }
638670

‎binary/ql/lib/semmlecode.binary.dbscheme‎

Lines changed: 12 additions & 0 deletions

0 commit comments

Comments
 (0)