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

Skip to content

Commit 35b028e

Browse files
Andrei DiaconuAndreiDiaconu1
authored andcommitted
Initial work for objects and statements
Objects now work, although I will refactor the code quite a bit for clarity If and while statements now produce good code Began work on try statements
1 parent 4462bab commit 35b028e

8 files changed

Lines changed: 219 additions & 149 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ private newtype TOpcode =
4242
TDynamicCastToVoid() or
4343
TVariableAddress() or
4444
TFieldAddress() or
45-
TIndexedElementAddress() or
4645
TFunctionAddress() or
46+
TIndexedElementAddress() or
4747
TConstant() or
4848
TStringConstant() or
4949
TConditionalBranch() or
@@ -72,7 +72,8 @@ private newtype TOpcode =
7272
TBufferMayWriteSideEffect() or
7373
TChi() or
7474
TInlineAsm() or
75-
TUnreached()
75+
TUnreached() or
76+
TNewObj()
7677

7778
class Opcode extends TOpcode {
7879
string toString() {
@@ -221,4 +222,5 @@ module Opcode {
221222
class Chi extends Opcode, TChi { override final string toString() { result = "Chi" } }
222223
class InlineAsm extends Opcode, TInlineAsm { override final string toString() { result = "InlineAsm" } }
223224
class Unreached extends Opcode, TUnreached { override final string toString() { result = "Unreached" } }
225+
class NewObj extends Opcode, TNewObj { override final string toString() { result = "NewObj" } }
224226
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ newtype TInstructionTag =
5959
LoadTag() or // Implicit load due to lvalue-to-rvalue conversion
6060
CatchTag() or
6161
ThrowTag() or
62+
NewObjTag() or
6263
UnwindTag() or
6364
InitializerUninitializedTag() or
6465
InitializerFieldAddressTag(Field field) {
@@ -141,6 +142,7 @@ string getInstructionTagId(TInstructionTag tag) {
141142
tag = CatchTag() and result = "Catch" or
142143
tag = ThrowTag() and result = "Throw" or
143144
tag = UnwindTag() and result = "Unwind" or
145+
tag = NewObjTag() and result = "NewObj" or
144146
// TODO: Reread
145147
// exists(Field field, Class cls, int index, string tagName |
146148
// field = cls.getCanonicalMember(index) and

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ class TranslatedFunctionCall extends TranslatedCallExpr, TranslatedDirectCall {
314314
/**
315315
* Represents the IR translation of a call to a constructor (can't call destructor explicitly in C#.
316316
*/
317-
class TranslatedStructorCall extends TranslatedFunctionCall {
318-
TranslatedStructorCall() {
317+
class TranslatedConstructorCall extends TranslatedFunctionCall {
318+
TranslatedConstructorCall() {
319319
expr instanceof ObjectCreation
320320
}
321321

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ abstract class TranslatedVariableDeclaration extends TranslatedElement, Initiali
173173
if (getVariable().getInitializer() instanceof ArrayCreation) then
174174
result = getTranslatedInitialization(getVariable().getInitializer().(ArrayCreation).getInitializer())
175175
else if (getVariable().getInitializer() instanceof ObjectCreation) then
176-
result = getTranslatedInitialization(getVariable().getInitializer().(ObjectCreation).getInitializer())
176+
result = getTranslatedInitialization(getVariable().getInitializer())
177177
else // then the simple variable initialization
178178
result = getTranslatedInitialization(getVariable().getInitializer())
179179
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ newtype TTranslatedElement =
229229
not expr instanceof ObjectCreation) or
230230

231231
// Then treat complex ones
232+
exists(ObjectCreation objCreation | objCreation = expr) or
232233
exists(ArrayInitializer arrInit | arrInit = expr) or
233234
exists(ObjectInitializer objInit | objInit = expr) or
234235

@@ -316,15 +317,12 @@ newtype TTranslatedElement =
316317
} or
317318
// A local declaration
318319
TTranslatedDeclarationEntry(Declaration entry) {
319-
exists(LocalVariableDeclStmt declStmt |
320-
translateStmt(declStmt) and
321-
declStmt.getAVariableDeclExpr().getVariable() = entry
320+
exists(LocalVariableDeclExpr declExpr |
321+
declExpr.getVariable() = entry
322322
)
323323
} or
324-
// An allocator call in a `new` or `new[]` expression
325-
TTranslatedAllocatorCall(ObjectCreation newExpr) { not ignoreExpr(newExpr) } or
326-
// An allocation size for a `new` or `new[]` expression
327-
TTranslatedAllocationSize(ObjectCreation newExpr) { not ignoreExpr(newExpr) }
324+
// An allocator call generated by an ObjectCreation expression
325+
TTranslatedAllocatorCall(ObjectCreation newExpr) { not ignoreExpr(newExpr) }
328326

329327
/**
330328
* Gets the index of the first explicitly initialized element in `initList`

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

Lines changed: 99 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,18 +1853,17 @@ class TranslatedAssignOperation extends TranslatedAssignment {
18531853
// }
18541854
//}
18551855

1856-
///**
1857-
// * The IR translation of a call to `operator new` as part of a `new` or `new[]`
1858-
// * expression.
1859-
// */
1856+
/**
1857+
* The IR translation of a call to `operator new` as part of a `new` expression.
1858+
*/
18601859
//class TranslatedAllocatorCall extends TTranslatedAllocatorCall,
1861-
// TranslatedDirectCall {
1862-
// override NewOrNewArrayExpr expr;
1860+
// TranslatedExpr {
1861+
// override ObjectCreation expr;
18631862
//
18641863
// TranslatedAllocatorCall() {
18651864
// this = TTranslatedAllocatorCall(expr)
18661865
// }
1867-
//
1866+
//
18681867
// override final string toString() {
18691868
// result = "Allocator call for " + expr.toString()
18701869
// }
@@ -1873,42 +1872,98 @@ class TranslatedAssignOperation extends TranslatedAssignment {
18731872
// none()
18741873
// }
18751874
//
1876-
// override Function getInstructionFunction(InstructionTag tag) {
1877-
// tag = CallTargetTag() and result = expr.getAllocator()
1875+
// override final TranslatedElement getChild(int id) {
1876+
// none()
1877+
// }
1878+
//
1879+
// // TODO: Will probably need a side effect instruction
1880+
// override predicate hasInstruction(Opcode opcode, InstructionTag tag,
1881+
// Type resultType, boolean isLValue) {
1882+
// tag = NewObjTag() and
1883+
// opcode instanceof Opcode::NewObj and
1884+
// resultType = expr.getType() and
1885+
// isLValue = false
1886+
// }
1887+
//
1888+
// override final Instruction getFirstInstruction() {
1889+
// result = getInstruction(NewObjTag())
1890+
// }
1891+
//
1892+
// override final Instruction getChildSuccessor(TranslatedElement element) {
1893+
// none()
1894+
// }
1895+
//
1896+
// override final Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
1897+
// tag = NewObjTag() and
1898+
// kind instanceof GotoEdge and
1899+
// result = getParent().getChildSuccessor(this)
1900+
// }
1901+
//
1902+
// override final Instruction getResult() {
1903+
// none()
1904+
// }
1905+
//}
1906+
1907+
//TranslatedAllocatorCall getTranslatedAllocatorCall(ObjectCreation newExpr) {
1908+
// result.getAST() = newExpr
1909+
//}
1910+
1911+
//class TranslatedObjectCreation extends TranslatedNonConstantExpr,
1912+
// InitializationContext {
1913+
// override ObjectCreation expr;
1914+
//
1915+
// override final TranslatedElement getChild(int id) {
1916+
// id = 0 and result = getInitialization()
18781917
// }
18791918
//
1880-
// override final Type getCallResultType() {
1881-
// result = expr.getAllocator().getUnspecifiedType()
1919+
// override final predicate hasInstruction(Opcode opcode, InstructionTag tag,
1920+
// Type resultType, boolean isLValue) {
1921+
// tag = NewObjTag() and
1922+
// opcode instanceof Opcode::NewObj and
1923+
// resultType = expr.getType() and
1924+
// isLValue = false
18821925
// }
18831926
//
1884-
// override final TranslatedExpr getQualifier() {
1885-
// none()
1927+
// override final Instruction getFirstInstruction() {
1928+
// result = getInstruction(NewObjTag())
18861929
// }
18871930
//
1888-
// override final predicate hasArguments() {
1889-
// // All allocator calls have at least one argument.
1890-
// any()
1931+
// override final Instruction getResult() {
1932+
// result = getInstruction(NewObjTag())
18911933
// }
18921934
//
1893-
// override final TranslatedExpr getArgument(int index) {
1894-
// // If the allocator is the default operator new(void*), there will be no
1895-
// // allocator call in the AST. Otherwise, there will be an allocator call
1896-
// // that includes all arguments to the allocator, including the size,
1897-
// // alignment (if any), and placement args. However, the size argument is
1898-
// // an error node, so we need to provide the correct size argument in any
1899-
// // case.
1900-
// if index = 0 then
1901-
// result = getTranslatedAllocationSize(expr)
1902-
// else if(index = 1 and expr.hasAlignedAllocation()) then
1903-
// result = getTranslatedExpr(expr.getAlignmentArgument())
1935+
// override final Instruction getInstructionSuccessor(InstructionTag tag,
1936+
// EdgeKind kind) {
1937+
// kind instanceof GotoEdge and
1938+
// tag = NewObjTag() and
1939+
// if exists(getInitialization()) then
1940+
// result = getInitialization().getFirstInstruction()
19041941
// else
1905-
// result = getTranslatedExpr(expr.getAllocatorCall().getArgument(index))
1942+
// result = getParent().getChildSuccessor(this)
1943+
// }
1944+
//
1945+
// override final Instruction getChildSuccessor(TranslatedElement child) {
1946+
// child = getInitialization() and result = getParent().getChildSuccessor(this)
1947+
// }
1948+
//
1949+
// override final Instruction getInstructionOperand(InstructionTag tag,
1950+
// OperandTag operandTag) {
1951+
// none()
1952+
// }
1953+
//
1954+
// override final Instruction getTargetAddress() {
1955+
// result = getInstruction(NewObjTag())
1956+
// }
1957+
//
1958+
// override final Type getTargetType() {
1959+
// result = expr.getType()
1960+
// }
1961+
//
1962+
// final TranslatedInitialization getInitialization() {
1963+
// result = getTranslatedInitialization(expr)
19061964
// }
19071965
//}
19081966

1909-
//TranslatedAllocatorCall getTranslatedAllocatorCall(NewOrNewArrayExpr newExpr) {
1910-
// result.getAST() = newExpr
1911-
//}
19121967
/**
19131968
* Abstract class implemented by any `TranslatedElement` that has a child
19141969
* expression that is a call to a constructor or destructor, in order to
@@ -2454,21 +2509,22 @@ class TranslatedThrowValueExpr extends TranslatedThrowExpr,
24542509
///**
24552510
// * The IR translation of a `new` or `new[]` expression.
24562511
// */
2457-
//abstract class TranslatedNewOrNewArrayExpr extends TranslatedNonConstantExpr,
2512+
//abstract class TranslatedNewExpr extends TranslatedNonConstantExpr,
24582513
// InitializationContext {
2459-
// override NewOrNewArrayExpr expr;
2514+
// override ObjectCreation expr;
24602515
//
24612516
// override final TranslatedElement getChild(int id) {
24622517
// id = 0 and result = getAllocatorCall() or
24632518
// id = 1 and result = getInitialization()
24642519
// }
24652520
//
2521+
// final TranslatedInitialization getInitialization() {
2522+
// result = getTranslatedInitialization(expr.getInitializer())
2523+
// }
2524+
//
24662525
// override final predicate hasInstruction(Opcode opcode, InstructionTag tag,
2467-
// Type resultType, boolean ) {
2468-
// tag = OnlyInstructionTag() and
2469-
// opcode instanceof Opcode::Convert and
2470-
// resultType = getResultType() and
2471-
// = false
2526+
// Type resultType, boolean isLValue) {
2527+
// none()
24722528
// }
24732529
//
24742530
// override final Instruction getFirstInstruction() {
@@ -2489,6 +2545,10 @@ class TranslatedThrowValueExpr extends TranslatedThrowExpr,
24892545
// result = getParent().getChildSuccessor(this)
24902546
// }
24912547
//
2548+
// override final Type getTargetType() {
2549+
// result = expr.getType()
2550+
// }
2551+
//
24922552
// override final Instruction getChildSuccessor(TranslatedElement child) {
24932553
// child = getAllocatorCall() and result = getInstruction(OnlyInstructionTag()) or
24942554
// child = getInitialization() and result = getParent().getChildSuccessor(this)
@@ -2508,41 +2568,9 @@ class TranslatedThrowValueExpr extends TranslatedThrowExpr,
25082568
// private TranslatedAllocatorCall getAllocatorCall() {
25092569
// result = getTranslatedAllocatorCall(expr)
25102570
// }
2511-
//
2512-
// abstract TranslatedInitialization getInitialization();
2513-
//}
2514-
//
2515-
///**
2516-
// * The IR translation of a `new` expression.
2517-
// */
2518-
//class TranslatedNewExpr extends TranslatedNewOrNewArrayExpr {
2519-
// override NewExpr expr;
2520-
//
2521-
// override final Type getTargetType() {
2522-
// result = expr.getAllocatedType().getUnspecifiedType()
2523-
// }
2524-
//
2525-
// override final TranslatedInitialization getInitialization() {
2526-
// result = getTranslatedInitialization(expr.getInitializer())
2527-
// }
2528-
//}
2529-
//
2530-
///**
2531-
// * The IR translation of a `new[]` expression.
2532-
// */
2533-
//class TranslatedNewArrayExpr extends TranslatedNewOrNewArrayExpr {
2534-
// override NewArrayExpr expr;
2535-
//
2536-
// override final Type getTargetType() {
2537-
// result = expr.getAllocatedType().getUnspecifiedType()
2538-
// }
2539-
//
2540-
// override final TranslatedInitialization getInitialization() {
2541-
// // REVIEW: Figure out how we want to model array initialization in the IR.
2542-
// none()
2543-
// }
25442571
//}
25452572

2573+
25462574
/**
25472575
* The IR translation of a `ConditionDeclExpr`, which represents the value of the declared variable
25482576
* after conversion to `bool` in code such as:

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,27 +216,63 @@ class TranslatedSimpleDirectInitialization extends TranslatedDirectInitializatio
216216
}
217217
}
218218

219-
class TranslatedConstructorInitialization extends TranslatedDirectInitialization, StructorCallContext {
219+
class TranslatedObjectCreationInitialization extends TranslatedDirectInitialization, StructorCallContext {
220220
override ObjectCreation expr;
221-
221+
222222
override predicate hasInstruction(Opcode opcode, InstructionTag tag, Type resultType, boolean isLValue) {
223-
none()
223+
(
224+
tag = InitializerStoreTag() and
225+
opcode instanceof Opcode::Store and
226+
resultType = getContext().getTargetType() and
227+
isLValue = false
228+
)
229+
or
230+
(
231+
tag = NewObjTag() and
232+
opcode instanceof Opcode::NewObj and
233+
resultType = expr.getType() and
234+
isLValue = false
235+
)
224236
}
225237

226238
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
227-
none()
239+
(
240+
tag = NewObjTag() and
241+
kind instanceof GotoEdge and
242+
result = getInitializer().getFirstInstruction()
243+
)
244+
or
245+
(
246+
tag = InitializerStoreTag() and
247+
result = getParent().getChildSuccessor(this) and
248+
kind instanceof GotoEdge
249+
)
228250
}
229251

252+
override Instruction getFirstInstruction() {
253+
result = getInstruction(NewObjTag())
254+
}
255+
230256
override Instruction getChildSuccessor(TranslatedElement child) {
231-
child = getInitializer() and result = getParent().getChildSuccessor(this)
257+
child = getInitializer() and result = getInstruction(InitializerStoreTag())
232258
}
233259

234260
override Instruction getInstructionOperand(InstructionTag tag, OperandTag operandTag) {
235-
none()
261+
tag = InitializerStoreTag() and
262+
(
263+
(
264+
operandTag instanceof AddressOperandTag and
265+
result = getContext().getTargetAddress()
266+
) or
267+
(
268+
operandTag instanceof StoreValueOperandTag and
269+
result = getInstruction(NewObjTag())
270+
)
271+
)
236272
}
237273

238274
override Instruction getReceiver() {
239-
result = getContext().getTargetAddress()
275+
result = getInstruction(NewObjTag())
240276
}
241277
}
242278

0 commit comments

Comments
 (0)