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

Skip to content

Commit 3bc6456

Browse files
author
AndreiDiaconu1
committed
Work on throw statements, bug fixes, small refactor.
Throw statements now give correct code, apart from the case of rethrows: need to make explicit the fact that a finally block is executed even if stack unwinding happens. Added 2 new classes to TranslatedStmt.qll, one for throws that have an exception, one for rethrows. Fixed a bug in TranslatedDeclarationEntry.qll where some local declaration would be missed. Changed toString into getQualifiedName for more clarity when generating the instructions in Instruction.qll. Some general refactoring in TranslatedExpr.qll and TranslatedStmt.qll.
1 parent b90bc96 commit 3bc6456

8 files changed

Lines changed: 718 additions & 564 deletions

File tree

csharp/ql/src/semmle/code/csharp/ir/implementation/internal/OperandTag.qll

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
<<<<<<< HEAD:csharp/ql/src/semmle/code/csharp/ir/implementation/internal/OperandTag.qll
21
private import OperandTagInternal
3-
=======
4-
import csharp
5-
6-
private int getMaxCallArgIndex() {
7-
result = max(int argIndex |
8-
exists(Call call |
9-
exists(call.getArgument(argIndex))
10-
)
11-
)
12-
}
13-
>>>>>>> 7c94037e5... Object creation and initialization + refactoring:csharp/ql/src/semmle/code/csharp/ir/internal/OperandTag.qll
142

153
private newtype TOperandTag =
164
TAddressOperand() or

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ class FieldInstruction extends Instruction {
618618
}
619619

620620
override final string getImmediateString() {
621-
result = field.toString()
621+
result = field.getQualifiedNameWithTypes()
622622
}
623623

624624
final Language::Field getField() {
@@ -634,7 +634,7 @@ class FunctionInstruction extends Instruction {
634634
}
635635

636636
override final string getImmediateString() {
637-
result = funcSymbol.toString()
637+
result = funcSymbol.getQualifiedNameWithTypes()
638638
}
639639

640640
final Language::Function getFunctionSymbol() {
@@ -1594,7 +1594,7 @@ class CatchByTypeInstruction extends CatchInstruction {
15941594
}
15951595

15961596
final override string getImmediateString() {
1597-
result = exceptionType.toString()
1597+
result = exceptionType.getQualifiedNameWithTypes()
15981598
}
15991599

16001600
/**

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ abstract class TranslatedDeclarationEntry extends TranslatedElement, TTranslated
3131
}
3232

3333
override final Callable getFunction() {
34-
exists(LocalVariableDeclStmt stmt |
35-
stmt.getAVariableDeclExpr().getVariable() = entry and
36-
result = stmt.getEnclosingCallable()
34+
exists(LocalVariableDeclExpr expr |
35+
expr.getVariable() = entry and
36+
result = expr.getEnclosingCallable()
3737
)
3838
}
3939

@@ -200,18 +200,18 @@ class TranslatedVariableDeclarationEntry extends TranslatedVariableDeclaration,
200200
}
201201
}
202202

203-
/**
204-
* Gets the `TranslatedRangeBasedForVariableDeclaration` that represents the declaration of
205-
* `var`.
206-
*/
203+
///**
204+
// * Gets the `TranslatedRangeBasedForVariableDeclaration` that represents the declaration of
205+
// * `var`.
206+
// */
207207
//TranslatedRangeBasedForVariableDeclaration getTranslatedRangeBasedForVariableDeclaration(
208208
// LocalVariable var) {
209209
// result.getVariable() = var
210210
//}
211211

212-
/**
213-
* Represents the IR translation of a compiler-generated variable in a range-based `for` loop.
214-
*/
212+
///**
213+
// * Represents the IR translation of a compiler-generated variable in a range-based `for` loop.
214+
// */
215215
//class TranslatedRangeBasedForVariableDeclaration extends TranslatedVariableDeclaration,
216216
// TTranslatedRangeBasedForVariableDeclaration {
217217
// RangeBasedForStmt forStmt;
@@ -242,14 +242,14 @@ class TranslatedVariableDeclarationEntry extends TranslatedVariableDeclaration,
242242
// result.getAST() = expr
243243
//}
244244

245-
/**
246-
* Represents the IR translation of the declaration portion of a `ConditionDeclExpr`, which
247-
* represents the variable declared in code such as:
248-
* ```
249-
* if (int* p = &x) {
250-
* }
251-
* ```
252-
*/
245+
///**
246+
// * Represents the IR translation of the declaration portion of a `ConditionDeclExpr`, which
247+
// * represents the variable declared in code such as:
248+
// * ```
249+
// * if (int* p = &x) {
250+
// * }
251+
// * ```
252+
// */
253253
//class TranslatedConditionDecl extends TranslatedVariableDeclaration, TTranslatedConditionDecl {
254254
// ConditionDeclExpr conditionDeclExpr;
255255
//

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

Lines changed: 123 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,130 +2417,129 @@ class TranslatedConditionalExpr extends TranslatedNonConstantExpr,
24172417
}
24182418
}
24192419

2420-
/**
2421-
* IR translation of a `throw` expression.
2422-
*/
2423-
abstract class TranslatedThrowExpr extends TranslatedNonConstantExpr {
2424-
override ThrowExpr expr;
2425-
2426-
2427-
override predicate hasInstruction(Opcode opcode, InstructionTag tag,
2428-
Type resultType, boolean isLValue) {
2429-
tag = ThrowTag() and
2430-
opcode = getThrowOpcode() and
2431-
resultType instanceof VoidType and
2432-
isLValue = false
2433-
}
2434-
2435-
override Instruction getInstructionSuccessor(InstructionTag tag,
2436-
EdgeKind kind) {
2437-
tag = ThrowTag() and
2438-
kind instanceof ExceptionEdge and
2439-
result = getParent().getExceptionSuccessorInstruction()
2440-
}
2441-
2442-
override Instruction getResult() {
2443-
none()
2444-
}
2445-
2446-
abstract Opcode getThrowOpcode();
2447-
}
2448-
2449-
/**
2450-
* IR translation of a `throw` expression with an argument
2451-
* (e.g. `throw std::bad_alloc()`).
2452-
*/
2453-
class TranslatedThrowValueExpr extends TranslatedThrowExpr,
2454-
InitializationContext {
2455-
TranslatedThrowValueExpr() {
2456-
not expr instanceof ThrowExpr
2457-
}
2458-
2459-
override TranslatedElement getChild(int id) {
2460-
id = 0 and result = getInitialization()
2461-
}
2462-
2463-
override Instruction getFirstInstruction() {
2464-
result = getInstruction(InitializerVariableAddressTag())
2465-
}
2466-
2467-
override predicate hasInstruction(Opcode opcode, InstructionTag tag,
2468-
Type resultType, boolean isLValue) {
2469-
TranslatedThrowExpr.super.hasInstruction(opcode, tag, resultType, isLValue) or
2470-
tag = InitializerVariableAddressTag() and
2471-
opcode instanceof Opcode::VariableAddress and
2472-
resultType = getExceptionType() and
2473-
isLValue = true
2474-
}
2475-
2476-
override Instruction getInstructionSuccessor(InstructionTag tag,
2477-
EdgeKind kind) {
2478-
result = TranslatedThrowExpr.super.getInstructionSuccessor(tag, kind) or
2479-
(
2480-
tag = InitializerVariableAddressTag() and
2481-
result = getInitialization().getFirstInstruction() and
2482-
kind instanceof GotoEdge
2483-
)
2484-
}
2485-
2486-
override Instruction getChildSuccessor(TranslatedElement child) {
2487-
child = getInitialization() and
2488-
result = getInstruction(ThrowTag())
2489-
}
2490-
2491-
override IRVariable getInstructionVariable(InstructionTag tag) {
2492-
tag = InitializerVariableAddressTag() and
2493-
result = getIRTempVariable(expr, ThrowTempVar())
2494-
}
2495-
2496-
override final predicate hasTempVariable(TempVariableTag tag, Type type) {
2497-
tag = ThrowTempVar() and
2498-
type = getExceptionType()
2499-
}
2500-
2501-
override final Instruction getInstructionOperand(InstructionTag tag,
2502-
OperandTag operandTag) {
2503-
tag = ThrowTag() and
2504-
(
2505-
(
2506-
operandTag instanceof AddressOperandTag and
2507-
result = getInstruction(InitializerVariableAddressTag())
2508-
) or
2509-
(
2510-
operandTag instanceof LoadOperandTag and
2511-
result = getEnclosingFunction().getUnmodeledDefinitionInstruction()
2512-
)
2513-
)
2514-
}
2515-
2516-
override final Type getInstructionOperandType(InstructionTag tag,
2517-
TypedOperandTag operandTag) {
2518-
tag = ThrowTag() and
2519-
operandTag instanceof LoadOperandTag and
2520-
result = getExceptionType()
2521-
}
2522-
2523-
override Instruction getTargetAddress() {
2524-
result = getInstruction(InitializerVariableAddressTag())
2525-
}
2526-
2527-
override Type getTargetType() {
2528-
result = getExceptionType()
2529-
}
2530-
2531-
TranslatedInitialization getInitialization() {
2532-
result = getTranslatedInitialization(
2533-
expr.getExpr())
2534-
}
2535-
2536-
override final Opcode getThrowOpcode() {
2537-
result instanceof Opcode::ThrowValue
2538-
}
2539-
2540-
private Type getExceptionType() {
2541-
result = expr.getType()
2542-
}
2543-
}
2420+
///**
2421+
// * IR translation of a `throw` expression.
2422+
// */
2423+
//abstract class TranslatedThrowExpr extends TranslatedNonConstantExpr {
2424+
// override ThrowExpr expr;
2425+
//
2426+
// override predicate hasInstruction(Opcode opcode, InstructionTag tag,
2427+
// Type resultType, boolean isLValue) {
2428+
// tag = ThrowTag() and
2429+
// opcode = getThrowOpcode() and
2430+
// resultType instanceof VoidType and
2431+
// isLValue = false
2432+
// }
2433+
//
2434+
// override Instruction getInstructionSuccessor(InstructionTag tag,
2435+
// EdgeKind kind) {
2436+
// tag = ThrowTag() and
2437+
// kind instanceof ExceptionEdge and
2438+
// result = getParent().getExceptionSuccessorInstruction()
2439+
// }
2440+
//
2441+
// override Instruction getResult() {
2442+
// none()
2443+
// }
2444+
//
2445+
// abstract Opcode getThrowOpcode();
2446+
//}
2447+
//
2448+
///**
2449+
// * IR translation of a `throw` expression with an argument
2450+
// * (e.g. `throw std::bad_alloc()`).
2451+
// */
2452+
//class TranslatedThrowValueExpr extends TranslatedThrowExpr,
2453+
// InitializationContext {
2454+
// TranslatedThrowValueExpr() {
2455+
// not expr instanceof ThrowExpr
2456+
// }
2457+
//
2458+
// override TranslatedElement getChild(int id) {
2459+
// id = 0 and result = getInitialization()
2460+
// }
2461+
//
2462+
// override Instruction getFirstInstruction() {
2463+
// result = getInstruction(InitializerVariableAddressTag())
2464+
// }
2465+
//
2466+
// override predicate hasInstruction(Opcode opcode, InstructionTag tag,
2467+
// Type resultType, boolean isLValue) {
2468+
// TranslatedThrowExpr.super.hasInstruction(opcode, tag, resultType, isLValue) or
2469+
// tag = InitializerVariableAddressTag() and
2470+
// opcode instanceof Opcode::VariableAddress and
2471+
// resultType = getExceptionType() and
2472+
// isLValue = true
2473+
// }
2474+
//
2475+
// override Instruction getInstructionSuccessor(InstructionTag tag,
2476+
// EdgeKind kind) {
2477+
// result = TranslatedThrowExpr.super.getInstructionSuccessor(tag, kind) or
2478+
// (
2479+
// tag = InitializerVariableAddressTag() and
2480+
// result = getInitialization().getFirstInstruction() and
2481+
// kind instanceof GotoEdge
2482+
// )
2483+
// }
2484+
//
2485+
// override Instruction getChildSuccessor(TranslatedElement child) {
2486+
// child = getInitialization() and
2487+
// result = getInstruction(ThrowTag())
2488+
// }
2489+
//
2490+
// override IRVariable getInstructionVariable(InstructionTag tag) {
2491+
// tag = InitializerVariableAddressTag() and
2492+
// result = getIRTempVariable(expr, ThrowTempVar())
2493+
// }
2494+
//
2495+
// override final predicate hasTempVariable(TempVariableTag tag, Type type) {
2496+
// tag = ThrowTempVar() and
2497+
// type = getExceptionType()
2498+
// }
2499+
//
2500+
// override final Instruction getInstructionOperand(InstructionTag tag,
2501+
// OperandTag operandTag) {
2502+
// tag = ThrowTag() and
2503+
// (
2504+
// (
2505+
// operandTag instanceof AddressOperandTag and
2506+
// result = getInstruction(InitializerVariableAddressTag())
2507+
// ) or
2508+
// (
2509+
// operandTag instanceof LoadOperandTag and
2510+
// result = getEnclosingFunction().getUnmodeledDefinitionInstruction()
2511+
// )
2512+
// )
2513+
// }
2514+
//
2515+
// override final Type getInstructionOperandType(InstructionTag tag,
2516+
// TypedOperandTag operandTag) {
2517+
// tag = ThrowTag() and
2518+
// operandTag instanceof LoadOperandTag and
2519+
// result = getExceptionType()
2520+
// }
2521+
//
2522+
// override Instruction getTargetAddress() {
2523+
// result = getInstruction(InitializerVariableAddressTag())
2524+
// }
2525+
//
2526+
// override Type getTargetType() {
2527+
// result = getExceptionType()
2528+
// }
2529+
//
2530+
// TranslatedInitialization getInitialization() {
2531+
// result = getTranslatedInitialization(
2532+
// expr.getExpr())
2533+
// }
2534+
//
2535+
// override final Opcode getThrowOpcode() {
2536+
// result instanceof Opcode::ThrowValue
2537+
// }
2538+
//
2539+
// private Type getExceptionType() {
2540+
// result = expr.getType()
2541+
// }
2542+
//}
25442543

25452544
// TODO: Should be handeled by the normal throw in C#
25462545
///**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class TranslatedFunction extends TranslatedElement,
197197
exists(TryStmt try |
198198
try.getEnclosingCallable() = callable
199199
) or
200-
exists(ThrowExpr throw |
200+
exists(ThrowStmt throw |
201201
throw.getEnclosingCallable() = callable
202202
)
203203
)

0 commit comments

Comments
 (0)