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

Skip to content

Commit be01b03

Browse files
Andrei DiaconuAndreiDiaconu1
authored andcommitted
Fixed and refactored code for arrays
Introduced 2 new tags to support multidimensional arrays Multidimensional arrays produce correct code All types of initializations for arrays work correctly
1 parent 35b028e commit be01b03

9 files changed

Lines changed: 1387 additions & 405 deletions

File tree

csharp/ql/src/semmle/code/csharp/ir/implementation/Opcode.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private newtype TOpcode =
4343
TVariableAddress() or
4444
TFieldAddress() or
4545
TFunctionAddress() or
46-
TIndexedElementAddress() or
46+
TElementsAddress() or
4747
TConstant() or
4848
TStringConstant() or
4949
TConditionalBranch() or
@@ -191,7 +191,7 @@ module Opcode {
191191
class DynamicCastToVoid extends UnaryOpcode, TDynamicCastToVoid { override final string toString() { result = "DynamicCastToVoid" } }
192192
class VariableAddress extends Opcode, TVariableAddress { override final string toString() { result = "VariableAddress" } }
193193
class FieldAddress extends UnaryOpcode, TFieldAddress { override final string toString() { result = "FieldAddress" } }
194-
class IndexedElementAddress extends BinaryOpcode, TIndexedElementAddress { override final string toString() { result = "IndexedElementAddress" } }
194+
class ElementsAddress extends BinaryOpcode, TElementsAddress { override final string toString() { result = "ElementsAddress" } }
195195
class FunctionAddress extends Opcode, TFunctionAddress { override final string toString() { result = "FunctionAddress" } }
196196
class Constant extends Opcode, TConstant { override final string toString() { result = "Constant" } }
197197
class StringConstant extends Opcode, TStringConstant { override final string toString() { result = "StringConstant" } }

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ newtype TInstructionTag =
5959
LoadTag() or // Implicit load due to lvalue-to-rvalue conversion
6060
CatchTag() or
6161
ThrowTag() or
62-
NewObjTag() or
6362
UnwindTag() or
6463
InitializerUninitializedTag() or
6564
InitializerFieldAddressTag(Field field) {
@@ -82,6 +81,15 @@ newtype TInstructionTag =
8281
} or
8382
InitializerElementDefaultValueStoreTag(int elementIndex) {
8483
elementIsInitialized(elementIndex)
84+
} or
85+
// Added for C#
86+
NewObjTag() or
87+
// TODO: 255 provisory 9max dim of array)
88+
PointerAddTag(int index) {
89+
index in [0 .. 255]
90+
} or
91+
ElementsAddressTag(int index) {
92+
index in [0 .. 255]
8593
}
8694

8795
class InstructionTag extends TInstructionTag {
@@ -142,7 +150,12 @@ string getInstructionTagId(TInstructionTag tag) {
142150
tag = CatchTag() and result = "Catch" or
143151
tag = ThrowTag() and result = "Throw" or
144152
tag = UnwindTag() and result = "Unwind" or
153+
154+
// Added for C#
145155
tag = NewObjTag() and result = "NewObj" or
156+
tag = ElementsAddressTag(_) and result = "ElementsAddress" or
157+
tag = PointerAddTag(_) and result = "PointerAdd" or
158+
146159
// TODO: Reread
147160
// exists(Field field, Class cls, int index, string tagName |
148161
// field = cls.getCanonicalMember(index) and

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
1717
*/
1818
IntType getIntType() { any() }
1919

20+
ArrayType getArrayOfDim(int dim, Type type) {
21+
result.getRank() = dim and
22+
result.getElementType() = type
23+
}
24+
2025
/**
2126
* Gets the "real" parent of `expr`. This predicate treats conversions as if
2227
* they were explicit nodes in the expression tree, rather than as implicit
@@ -68,14 +73,15 @@ private predicate ignoreExprAndDescendants(Expr expr) {
6873
* Holds if `expr` (not including its descendants) should be ignored for the
6974
* purposes of IR generation.
7075
*/
71-
// TODO: See what exprs should be ignored for C# IR generation
7276
private predicate ignoreExprOnly(Expr expr) {
7377
// exists(NewOrNewArrayExpr newExpr |
7478
// // Ignore the allocator call, because we always synthesize it. Don't ignore
7579
// // its arguments, though, because we use them as part of the synthesis.
7680
// newExpr.getAllocatorCall() = expr
7781
// ) or
78-
not translateFunction(expr.getEnclosingCallable())
82+
not translateFunction(expr.getEnclosingCallable()) or
83+
// Ignore size of arrays when translating
84+
(expr.getParent() instanceof ArrayCreation and expr.hasValue())
7985
}
8086

8187
/**
@@ -185,7 +191,8 @@ newtype TTranslatedElement =
185191
TTranslatedLoad(Expr expr) {
186192
// TODO: Revisit and make sure Loads are only used when needed
187193
expr instanceof AssignableRead and
188-
not (expr.getParent() instanceof ArrayAccess)
194+
not (expr.getParent() instanceof ArrayAccess) and
195+
not (expr.getType() instanceof RefType)
189196
} or
190197
// An expression most naturally translated as control flow.
191198
TTranslatedNativeCondition(Expr expr) {

0 commit comments

Comments
 (0)