Skip to content

Commit b858962

Browse files
authored
Merge pull request github#2000 from AndreiDiaconu1/ircsharp-fixes
C# IR: Minor fixes and changes
2 parents 22e57a6 + 7f76947 commit b858962

File tree

4 files changed

+91
-79
lines changed

4 files changed

+91
-79
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ predicate needsLoad(Expr expr) {
194194
* Holds if we should ignore the `Load` instruction for `expr` when generating IR.
195195
*/
196196
private predicate ignoreLoad(Expr expr) {
197-
// No load needed for the qualifier
198-
// in an array access
197+
// No load needed for the qualifier of an array access,
198+
// since we use the instruction `ElementsAddress`
199+
// to get the address of the first element in an array
199200
expr = any(ArrayAccess aa).getQualifier()
200201
or
201202
// No load is needed for the lvalue in an assignment such as:
@@ -211,6 +212,9 @@ private predicate ignoreLoad(Expr expr) {
211212
// address is the final value of the expression
212213
expr.getParent() instanceof AddressOfExpr
213214
or
215+
// A property access does not need a load since it is a call
216+
expr instanceof PropertyAccess
217+
or
214218
// If expr is a variable access used as the qualifier for a field access and
215219
// its target variable is a value type variable,
216220
// ignore the load since the address of a variable that is a value type is

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ abstract class TranslatedCrementOperation extends TranslatedNonConstantExpr {
414414
this.getOpcode() instanceof Opcode::PointerAdd or
415415
this.getOpcode() instanceof Opcode::PointerSub
416416
) and
417-
result = 4 //max(getResultType().(PointerType).getSize())
417+
result = Language::getTypeSize(this.getResultType().(PointerType).getReferentType())
418418
}
419419

420420
final TranslatedExpr getOperand() { result = getTranslatedExpr(expr.getOperand()) }
@@ -578,7 +578,6 @@ class TranslatedArrayAccess extends TranslatedNonConstantExpr {
578578
(
579579
// The successor of a `PointerAdd` is an `ElementsAddress` if
580580
// that `PointerAdd` is not the last `PointerAdd` instruction.
581-
index < getRank() - 1 and
582581
tag = PointerAddTag(index) and
583582
result = this.getInstruction(ElementsAddressTag(index + 1))
584583
or
@@ -607,10 +606,7 @@ class TranslatedArrayAccess extends TranslatedNonConstantExpr {
607606
result = this.getInstruction(PointerAddTag(child.getAST().getIndex()))
608607
}
609608

610-
override Instruction getResult() {
611-
result = this.getInstruction(PointerAddTag(getRank() - 1)) //and
612-
//result.getResultType() = expr.getType()
613-
}
609+
override Instruction getResult() { result = this.getInstruction(PointerAddTag(getRank() - 1)) }
614610

615611
override predicate hasInstruction(
616612
Opcode opcode, InstructionTag tag, Type resultType, boolean isLValue
@@ -663,12 +659,14 @@ class TranslatedArrayAccess extends TranslatedNonConstantExpr {
663659
}
664660

665661
override int getInstructionElementSize(InstructionTag tag) {
666-
tag = PointerAddTag(_) and
667-
// TODO: Fix sizes once we have type sizes
668-
result = 4
662+
exists(int index |
663+
inBounds(index) and
664+
tag = PointerAddTag(index) and
665+
result = Language::getTypeSize(expr.getQualifier().getType().(ArrayType).getElementType())
666+
)
669667
}
670668

671-
private TranslatedExpr getBaseOperand() { result = getTranslatedExpr(expr.getChild(-1)) }
669+
private TranslatedExpr getBaseOperand() { result = getTranslatedExpr(expr.getQualifier()) }
672670

673671
private TranslatedExpr getOffsetOperand(int index) {
674672
this.inBounds(index) and
@@ -1248,16 +1246,14 @@ class TranslatedBinaryOperation extends TranslatedSingleInstructionExpr {
12481246
opcode instanceof Opcode::PointerSub or
12491247
opcode instanceof Opcode::PointerDiff
12501248
) and
1251-
result = 8 //max(getPointerOperand().getResultType().(PointerType).getReferentType().getSize()) TODO: SIZE AGAIN
1249+
result = Language::getTypeSize(this.getPointerOperand().getResultType())
12521250
)
12531251
}
12541252

1255-
// private TranslatedExpr getPointerOperand() {
1256-
// if swapOperandsOnOp() then
1257-
// result = this.getRightOperand()
1258-
// else
1259-
// result = this.getLeftOperand()
1260-
// }
1253+
private TranslatedExpr getPointerOperand() {
1254+
if swapOperandsOnOp() then result = this.getRightOperand() else result = this.getLeftOperand()
1255+
}
1256+
12611257
private predicate swapOperandsOnOp() {
12621258
// Swap the operands on a pointer add 'i + p', so that the pointer operand
12631259
// always comes first. Note that we still evaluate the operands
@@ -1444,9 +1440,19 @@ class TranslatedAssignOperation extends TranslatedAssignment {
14441440
}
14451441

14461442
private Opcode getOpcode() {
1447-
expr instanceof AssignAddExpr and result instanceof Opcode::Add
1443+
expr instanceof AssignAddExpr and
1444+
(
1445+
if expr.getRValue().getType() instanceof PointerType
1446+
then result instanceof Opcode::PointerAdd
1447+
else result instanceof Opcode::Add
1448+
)
14481449
or
1449-
expr instanceof AssignSubExpr and result instanceof Opcode::Sub
1450+
expr instanceof AssignSubExpr and
1451+
(
1452+
if expr.getRValue().getType() instanceof PointerType
1453+
then result instanceof Opcode::PointerSub
1454+
else result instanceof Opcode::Sub
1455+
)
14501456
or
14511457
expr instanceof AssignMulExpr and result instanceof Opcode::Mul
14521458
or
@@ -1462,10 +1468,7 @@ class TranslatedAssignOperation extends TranslatedAssignment {
14621468
or
14631469
expr instanceof AssignLShiftExpr and result instanceof Opcode::ShiftLeft
14641470
or
1465-
expr instanceof AssignRShiftExpr and result instanceof Opcode::ShiftRight // or
1466-
// TODO: THE CASES ABOVE DEAL WITH POINTERS
1467-
// expr instanceof AssignPointerAddExpr and result instanceof Opcode::PointerAdd or
1468-
// expr instanceof AssignPointerSubExpr and result instanceof Opcode::PointerSub
1471+
expr instanceof AssignRShiftExpr and result instanceof Opcode::ShiftRight
14691472
}
14701473

14711474
override predicate hasInstruction(
@@ -1500,11 +1503,13 @@ class TranslatedAssignOperation extends TranslatedAssignment {
15001503
override int getInstructionElementSize(InstructionTag tag) {
15011504
tag = AssignOperationOpTag() and
15021505
exists(Opcode opcode |
1503-
opcode = getOpcode() and
1504-
// TODO: ADD AND SUB FOR POITNER ARITH (WAS POINTERADD AND POINTERSUB)
1505-
(opcode instanceof Opcode::Add or opcode instanceof Opcode::Sub)
1506+
opcode = this.getOpcode() and
1507+
(
1508+
opcode instanceof Opcode::PointerAdd or
1509+
opcode instanceof Opcode::PointerSub
1510+
)
15061511
) and
1507-
result = 8 //max(getResultType().(PointerType).getReferentType().getSize()) TODO: DEAL WITH SIZE
1512+
result = Language::getTypeSize(getResultType().(PointerType).getReferentType())
15081513
}
15091514

15101515
override Instruction getInstructionOperand(InstructionTag tag, OperandTag operandTag) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ abstract class TranslatedElementInitialization extends TranslatedElement {
249249
)
250250
}
251251

252+
override int getInstructionElementSize(InstructionTag tag) {
253+
tag = getElementAddressTag() and
254+
result = Language::getTypeSize(getElementType())
255+
}
256+
252257
override string getInstructionConstantValue(InstructionTag tag) {
253258
tag = getElementIndexTag() and
254259
result = getElementIndex().toString()

0 commit comments

Comments
 (0)