Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brodes/seh flow phase2 splitting seh edges #18049

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
de05aee
Adding model transition to using Throwing.qll.
bdrodes Nov 18, 2024
4b83a45
Change log
bdrodes Nov 18, 2024
1c7b5ae
Merge branch 'main' into brodes/seh_flow_phase1_throwing_models
bdrodes Nov 18, 2024
792231c
Removing SEH default case for function calls as the logic to handle S…
bdrodes Nov 18, 2024
1c874d3
Fixed usage raisesException
bdrodes Nov 19, 2024
5bb765d
Merge branch 'main' into brodes/seh_flow_phase1_throwing_models
bdrodes Nov 19, 2024
26d590a
Putting back deleted file, and deprecating instead. Deprecating mayTh…
bdrodes Nov 19, 2024
63ddd81
Merge branch 'brodes/seh_flow_phase1_throwing_models' of https://gith…
bdrodes Nov 19, 2024
0784776
bringing back mayThrowException to make it cleaner/easier for backwar…
bdrodes Nov 19, 2024
ae1ed38
Merge branch 'main' into brodes/seh_flow_phase1_throwing_models
bdrodes Nov 19, 2024
a69daa0
Missing change to 'mayThrowException' in StructuredExceptionHandling.qll
bdrodes Nov 19, 2024
23485f1
Merge branch 'brodes/seh_flow_phase1_throwing_models' of https://gith…
bdrodes Nov 19, 2024
4e77756
Changing terminology back to "throws" vs "rasis" for alwaysThrowsExce…
bdrodes Nov 19, 2024
69df07e
Update cpp/ql/lib/change-notes/2024-11-18-throwing-functions.md
bdrodes Nov 20, 2024
6aa7412
Merge branch 'main' into brodes/seh_flow_phase1_throwing_models
bdrodes Nov 20, 2024
4078d79
Adds SEH exception edge types, disjoint from normal C++ edges. Does n…
bdrodes Nov 20, 2024
007dd83
Updating ir test expected files.
bdrodes Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/ql/lib/change-notes/2024-11-18-throwing-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: deprecated
---
* The `NonThrowing` class (`semmle.code.cpp.models.interfaces.NonThrowing`) has been deprecated. Please use the `NonThrowing` class from `semmle.code.cpp.models.interfaces.Throwing` instead.
20 changes: 17 additions & 3 deletions cpp/ql/lib/semmle/code/cpp/ir/implementation/EdgeKind.qll
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*/

private import internal.EdgeKindInternal
private import codeql.util.Boolean

private newtype TEdgeKind =
TGotoEdge() or // Single successor (including fall-through)
TTrueEdge() or // 'true' edge of conditional branch
TFalseEdge() or // 'false' edge of conditional branch
TExceptionEdge() or // Thrown exception
TExceptionEdge(Boolean isSeh) or // Thrown exception, true for SEH exceptions, false otherwise
TDefaultEdge() or // 'default' label of switch
TCaseEdge(string minValue, string maxValue) {
// Case label of switch
Expand Down Expand Up @@ -54,7 +55,18 @@ class FalseEdge extends EdgeKind, TFalseEdge {
* instruction's evaluation throws an exception.
*/
class ExceptionEdge extends EdgeKind, TExceptionEdge {
final override string toString() { result = "Exception" }
Boolean isSeh; //true for Structured Exception Handling, false for C++ exceptions

ExceptionEdge() { this = TExceptionEdge(isSeh) }

/**
* Holds if the exception is a Structured Exception Handling (SEH) exception.
*/
final predicate isSeh() { isSeh = true }

final override string toString() {
if isSeh = true then result = "SEH Exception" else result = "C++ Exception"
}
}

/**
Expand Down Expand Up @@ -122,8 +134,10 @@ module EdgeKind {

/**
* Gets the single instance of the `ExceptionEdge` class.
* Gets the instance of the `ExceptionEdge` class.
* `isSeh` is true if the exception is an SEH exception, and false for a C++ edge.
*/
ExceptionEdge exceptionEdge() { result = TExceptionEdge() }
ExceptionEdge exceptionEdge(Boolean isSeh) { result = TExceptionEdge(isSeh) }

/**
* Gets the single instance of the `DefaultEdge` class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ abstract class TranslatedCall extends TranslatedExpr {
result = this.getParent().getChildSuccessor(this, kind)
or
this.mayThrowException() and
kind instanceof ExceptionEdge and
kind = EdgeKind::exceptionEdge(false) and
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3039,7 +3039,7 @@ class TranslatedDestructorsAfterThrow extends TranslatedElement, TTranslatedDest
or
// And otherwise, exit this element with an exceptional edge
not exists(this.getChild(id + 1)) and
kind instanceof ExceptionEdge and
kind = EdgeKind::exceptionEdge(false) and
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
)
}
Expand Down Expand Up @@ -3078,7 +3078,7 @@ abstract class TranslatedThrowExpr extends TranslatedNonConstantExpr {
result = this.getDestructors().getFirstInstruction(kind)
or
not exists(this.getDestructors()) and
kind instanceof ExceptionEdge and
kind = EdgeKind::exceptionEdge(false) and
result = this.getParent().getExceptionSuccessorInstruction(any(GotoEdge edge))
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ class TranslatedCatchByTypeHandler extends TranslatedHandler {
kind instanceof GotoEdge and
result = this.getParameter().getFirstInstruction(kind)
or
kind instanceof ExceptionEdge and
kind = EdgeKind::exceptionEdge(false) and
if exists(this.getDestructors())
then result = this.getDestructors().getFirstInstruction(any(GotoEdge edge))
else result = this.getParent().(TranslatedTryStmt).getNextHandler(this, any(GotoEdge edge))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* functions. See `semmle.code.cpp.models.Models` for usage information.
*/

import semmle.code.cpp.Function

Check warning

Code scanning / CodeQL

Redundant import Warning

Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.ArrayFunction
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.DataFlow
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.Alias
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.SideEffect
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.Taint
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.NonThrowing
.
import semmle.code.cpp.models.interfaces.ArrayFunction
import semmle.code.cpp.models.interfaces.DataFlow
import semmle.code.cpp.models.interfaces.Alias
import semmle.code.cpp.models.interfaces.SideEffect
import semmle.code.cpp.models.interfaces.Taint
import semmle.code.cpp.models.interfaces.NonThrowing
import semmle.code.cpp.models.interfaces.Throwing

/**
* The standard functions `memcpy`, `memmove` and `bcopy`; and the gcc variant
Expand Down Expand Up @@ -106,6 +106,8 @@
not this.hasGlobalName(["bcopy", mempcpy(), "memccpy"]) and
index = this.getParamDest()
}

override TCxxException getExceptionType() { any() }
}

private string mempcpy() { result = ["mempcpy", "wmempcpy"] }
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* functions. See `semmle.code.cpp.models.Models` for usage information.
*/

import semmle.code.cpp.Function

Check warning

Code scanning / CodeQL

Redundant import Warning

Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.ArrayFunction
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.DataFlow
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.Alias
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.SideEffect
.
Redundant import, the module is already imported inside
semmle.code.cpp.models.interfaces.NonThrowing
.
import semmle.code.cpp.models.interfaces.ArrayFunction
import semmle.code.cpp.models.interfaces.DataFlow
import semmle.code.cpp.models.interfaces.Alias
import semmle.code.cpp.models.interfaces.SideEffect
import semmle.code.cpp.models.interfaces.NonThrowing
import semmle.code.cpp.models.interfaces.Throwing

private class MemsetFunctionModel extends ArrayFunction, DataFlowFunction, AliasFunction,
SideEffectFunction, NonThrowingFunction
Expand Down Expand Up @@ -74,6 +74,8 @@
i = 0 and
if this.hasGlobalName(bzero()) then result = 1 else result = 2
}

override TCxxException getExceptionType() { any() }
}

private string bzero() { result = ["bzero", "explicit_bzero"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import semmle.code.cpp.models.interfaces.NonThrowing
import semmle.code.cpp.models.interfaces.Throwing

/**
* A function that is annotated with a `noexcept` specifier (or the equivalent
Expand All @@ -8,4 +8,6 @@ import semmle.code.cpp.models.interfaces.NonThrowing
*/
class NoexceptFunction extends NonThrowingFunction {
NoexceptFunction() { this.isNoExcept() or this.isNoThrow() }

override TCxxException getExceptionType() { any() }
}
12 changes: 11 additions & 1 deletion cpp/ql/lib/semmle/code/cpp/models/implementations/Printf.qll
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import semmle.code.cpp.models.interfaces.FormattingFunction
import semmle.code.cpp.models.interfaces.Alias
import semmle.code.cpp.models.interfaces.SideEffect
import semmle.code.cpp.models.interfaces.NonThrowing
import semmle.code.cpp.models.interfaces.Throwing

/**
* The standard functions `printf`, `wprintf` and their glib variants.
Expand All @@ -32,6 +32,8 @@ private class Printf extends FormattingFunction, AliasFunction, NonThrowingFunct
override predicate parameterEscapesOnlyViaReturn(int n) { none() }

override predicate parameterIsAlwaysReturned(int n) { none() }

override TCxxException getExceptionType() { any() }
}

/**
Expand All @@ -50,6 +52,8 @@ private class Fprintf extends FormattingFunction, NonThrowingFunction {
override int getFormatParameterIndex() { result = 1 }

override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = true }

override TCxxException getExceptionType() { any() }
}

/**
Expand Down Expand Up @@ -93,6 +97,8 @@ private class Sprintf extends FormattingFunction, NonThrowingFunction {
then result = 4
else result = super.getFirstFormatArgumentIndex()
}

override TCxxException getExceptionType() { any() }
}

/**
Expand Down Expand Up @@ -165,6 +171,8 @@ private class SnprintfImpl extends Snprintf, AliasFunction, SideEffectFunction,
// We don't know how many parameters are passed to the function since it's varargs, but they also have read side effects.
i = this.getFormatParameterIndex() and buffer = true
}

override TCxxException getExceptionType() { any() }
}

/**
Expand Down Expand Up @@ -215,4 +223,6 @@ private class Syslog extends FormattingFunction, NonThrowingFunction {
override int getFormatParameterIndex() { result = 1 }

override predicate isOutputGlobal() { any() }

override TCxxException getExceptionType() { any() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
import semmle.code.cpp.models.interfaces.DataFlow
import semmle.code.cpp.models.interfaces.Taint
import semmle.code.cpp.models.interfaces.SideEffect
import semmle.code.cpp.models.interfaces.NonThrowing
import semmle.code.cpp.models.interfaces.Throwing

/**
* The standard function `strcat` and its wide, sized, and Microsoft variants.
Expand Down Expand Up @@ -94,6 +94,8 @@ class StrcatFunction extends TaintFunction, DataFlowFunction, ArrayFunction, Sid
(i = 0 or i = 1) and
buffer = true
}

override TCxxException getExceptionType() { any() }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
import semmle.code.cpp.models.interfaces.DataFlow
import semmle.code.cpp.models.interfaces.Taint
import semmle.code.cpp.models.interfaces.SideEffect
import semmle.code.cpp.models.interfaces.NonThrowing
import semmle.code.cpp.models.interfaces.Throwing

/**
* The standard function `strcpy` and its wide, sized, and Microsoft variants.
Expand Down Expand Up @@ -145,4 +145,6 @@ class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction, Sid
i = this.getParamDest() and
result = this.getParamSize()
}

override TCxxException getExceptionType() { any() }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import semmle.code.cpp.models.interfaces.Throwing

class WindowsDriverFunction extends ThrowingFunction {
WindowsDriverFunction() {
class WindowsDriverExceptionAnnotation extends ThrowingFunction {
WindowsDriverExceptionAnnotation() {
this.hasGlobalName(["RaiseException", "ExRaiseAccessViolation", "ExRaiseDatatypeMisalignment"])
}

final override predicate mayThrowException(boolean unconditional) { unconditional = true }
override predicate mayThrowException(boolean unconditional) { unconditional = true }

override TSehException getExceptionType() { any() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ import semmle.code.cpp.models.Models

/**
* A function that is guaranteed to never throw.
*
* DEPRECATED: use `NonThrowingFunction` in `semmle.code.cpp.models.Models.Interfaces.Throwing` instead.
*/
abstract class NonThrowingFunction extends Function { }
abstract deprecated class NonThrowingFunction extends Function { }
61 changes: 57 additions & 4 deletions cpp/ql/lib/semmle/code/cpp/models/interfaces/Throwing.qll
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,65 @@ import semmle.code.cpp.models.Models
import semmle.code.cpp.models.interfaces.FunctionInputsAndOutputs

/**
* A class that models the exceptional behavior of a function.
* Represents a type of exception,
* either Structured Exception Handling (SEH) or C++ exceptions.
*/
abstract class ThrowingFunction extends Function {
newtype TException =
/** Structured Exception Handling (SEH) exception */
TSehException() or
/** C++ exception */
TCxxException()

/**
* Functions with information about how an exception is thrown or if one is thrown at all.
* If throwing details conflict for the same function, IR is assumed
* to use the most restricted interpretation, meaning taking options
* that stipulate no exception is raised, before the exception is always raised,
* before conditional exceptions.
*
* Annotations must specify if the exception is from SEH (structured exception handling)
* or ordinary c++ exceptions.
*/
abstract private class ExceptionAnnotation extends Function {
/**
* Returns the type of exception this annotation is for,
* either a CPP exception or a STructured Exception Handling (SEH) exception.
*/
abstract TException getExceptionType();

/**
* Holds if the exception type of this annotation is for a Structured Exception Handling (SEH) exception.
*/
final predicate isSeh() { this.getExceptionType() = TSehException() }

/**
* Holds if the exception type of this annotation is for a CPP exception.
*/
final predicate isCxx() { this.getExceptionType() = TCxxException() }
}

/**
* A Function that is known to not throw an exception.
*/
abstract class NonThrowingFunction extends ExceptionAnnotation { }

/**
* A function this is known to raise an exception.
*/
abstract class ThrowingFunction extends ExceptionAnnotation {
ThrowingFunction() { any() }

/**
* Holds if this function may throw an exception during evaluation.
* If `unconditional` is `true` the function always throws an exception.
* Holds if this function may raise an exception during evaluation.
* If `unconditional` is `false` the function may raise, and if `true` the function
* will always raise an exception.
* Do not specify `none()` if no exception is raised, instead use the
* `NonThrowingFunction` class instead.
*/
abstract predicate mayThrowException(boolean unconditional);

/**
* Holds if this function will always raise an exception if called
*/
final predicate alwaysThrowsException() { this.mayThrowException(true) }
}
Loading