|
| 1 | +/** |
| 2 | + * INTERNAL: Do not use. |
| 3 | + * |
| 4 | + * Provides a simple analysis for identifying calls to callables that will |
| 5 | + * not return. |
| 6 | + */ |
| 7 | + |
| 8 | +import csharp |
| 9 | +private import semmle.code.csharp.ExprOrStmtParent |
| 10 | +private import semmle.code.csharp.commons.Assertions |
| 11 | +private import semmle.code.csharp.frameworks.System |
| 12 | +private import semmle.code.csharp.controlflow.internal.Completion |
| 13 | + |
| 14 | +/** A call that definitely does not return (conservative analysis). */ |
| 15 | +abstract class NonReturningCall extends Call { |
| 16 | + /** Gets a valid completion for this non-returning call. */ |
| 17 | + abstract Completion getACompletion(); |
| 18 | +} |
| 19 | + |
| 20 | +private class ExitingCall extends NonReturningCall { |
| 21 | + ExitingCall() { |
| 22 | + this.getTarget() instanceof ExitingCallable |
| 23 | + or |
| 24 | + exists(AssertMethod m | m = this.(FailingAssertion).getAssertMethod() | |
| 25 | + not exists(m.getExceptionClass()) |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + override ExitCompletion getACompletion() { any() } |
| 30 | +} |
| 31 | + |
| 32 | +private class ThrowingCall extends NonReturningCall { |
| 33 | + private ThrowCompletion c; |
| 34 | + |
| 35 | + ThrowingCall() { |
| 36 | + c = this.getTarget().(ThrowingCallable).getACallCompletion() |
| 37 | + or |
| 38 | + exists(AssertMethod m | m = this.(FailingAssertion).getAssertMethod() | |
| 39 | + c.getExceptionClass() = m.getExceptionClass() |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + override ThrowCompletion getACompletion() { result = c } |
| 44 | +} |
| 45 | + |
| 46 | +abstract private class NonReturningCallable extends Callable { |
| 47 | + NonReturningCallable() { |
| 48 | + not exists(ReturnStmt ret | ret.getEnclosingCallable() = this) and |
| 49 | + not hasAccessorAutoImplementation(this, _) and |
| 50 | + not exists(Virtualizable v | v.isOverridableOrImplementable() | |
| 51 | + v = this or |
| 52 | + v = this.(Accessor).getDeclaration() |
| 53 | + ) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +abstract private class ExitingCallable extends NonReturningCallable { } |
| 58 | + |
| 59 | +private class DirectlyExitingCallable extends ExitingCallable { |
| 60 | + DirectlyExitingCallable() { |
| 61 | + this = any(Method m | |
| 62 | + m.hasQualifiedName("System.Environment", "Exit") or |
| 63 | + m.hasQualifiedName("System.Windows.Forms.Application", "Exit") |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +private class IndirectlyExitingCallable extends ExitingCallable { |
| 69 | + IndirectlyExitingCallable() { |
| 70 | + forex(ControlFlowElement body | body = this.getABody() | body = getAnExitingElement()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +private ControlFlowElement getAnExitingElement() { |
| 75 | + result instanceof ExitingCall |
| 76 | + or |
| 77 | + result = getAnExitingStmt() |
| 78 | +} |
| 79 | + |
| 80 | +private Stmt getAnExitingStmt() { |
| 81 | + result.(ExprStmt).getExpr() = getAnExitingElement() |
| 82 | + or |
| 83 | + result.(BlockStmt).getFirstStmt() = getAnExitingElement() |
| 84 | + or |
| 85 | + exists(IfStmt ifStmt | |
| 86 | + result = ifStmt and |
| 87 | + ifStmt.getThen() = getAnExitingElement() and |
| 88 | + ifStmt.getElse() = getAnExitingElement() |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +private class ThrowingCallable extends NonReturningCallable { |
| 93 | + ThrowingCallable() { |
| 94 | + forex(ControlFlowElement body | body = this.getABody() | body = getAThrowingElement(_)) |
| 95 | + } |
| 96 | + |
| 97 | + /** Gets a valid completion for a call to this throwing callable. */ |
| 98 | + ThrowCompletion getACallCompletion() { this.getABody() = getAThrowingElement(result) } |
| 99 | +} |
| 100 | + |
| 101 | +private predicate directlyThrows(ThrowElement te, ThrowCompletion c) { |
| 102 | + c.getExceptionClass() = te.getThrownExceptionType() and |
| 103 | + // For stub implementations, there may exist proper implementations that are not seen |
| 104 | + // during compilation, so we conservatively rule those out |
| 105 | + not isStub(te) |
| 106 | +} |
| 107 | + |
| 108 | +private ControlFlowElement getAThrowingElement(ThrowCompletion c) { |
| 109 | + c = result.(ThrowingCall).getACompletion() |
| 110 | + or |
| 111 | + directlyThrows(result, c) |
| 112 | + or |
| 113 | + result = getAThrowingStmt(c) |
| 114 | +} |
| 115 | + |
| 116 | +private Stmt getAThrowingStmt(ThrowCompletion c) { |
| 117 | + directlyThrows(result, c) |
| 118 | + or |
| 119 | + result.(ExprStmt).getExpr() = getAThrowingElement(c) |
| 120 | + or |
| 121 | + result.(BlockStmt).getFirstStmt() = getAThrowingStmt(c) |
| 122 | + or |
| 123 | + exists(IfStmt ifStmt, ThrowCompletion c1, ThrowCompletion c2 | |
| 124 | + result = ifStmt and |
| 125 | + ifStmt.getThen() = getAThrowingStmt(c1) and |
| 126 | + ifStmt.getElse() = getAThrowingStmt(c2) |
| 127 | + | |
| 128 | + c = c1 |
| 129 | + or |
| 130 | + c = c2 |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +/** Holds if `throw` element `te` indicates a stub implementation. */ |
| 135 | +private predicate isStub(ThrowElement te) { |
| 136 | + exists(Expr e | e = te.getExpr() | |
| 137 | + e instanceof NullLiteral or |
| 138 | + e.getType() instanceof SystemNotImplementedExceptionClass |
| 139 | + ) |
| 140 | +} |
0 commit comments