-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 phase1 throwing models #18014
base: main
Are you sure you want to change the base?
Brodes/seh flow phase1 throwing models #18014
Conversation
…EH is not yet part of the IR generation to make this logic work.
*/ | ||
abstract predicate mayThrowException(boolean unconditional); | ||
final predicate mayRaiseException() { this.raisesException(false) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, the mayThrowException
predicate will need to be kept for now and marked as deprecated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to rethink this generally for backwards compatibility given the original thing being deprecated is abstract. Stand by...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I decided to move back to the old mechanic. I switched because SEH doesn't really throw an exception per se. But it's fine, and easier to just keep it the way it was. The changes have been made, just waiting for the checks to pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm seeing failures with some security/CWE tests, but I cannot recreate that locally. @MathiasVP is that a common discrepancy? can you maybe run the test locally as well? Thye all passed for me.
…rowException as well.
…ub.com/microsoft/codeql into brodes/seh_flow_phase1_throwing_models
…ds compatibility.
…ub.com/microsoft/codeql into brodes/seh_flow_phase1_throwing_models
…ption to be consistent with other backward compatibility changes.
Why do we need to deprecate Perhaps the stuff in |
This is because we want to distinguish between the kinds of exceptions that cannot be thrown. For example, I don't care specifically about how we distinguish between those two concepts. I agree that it's a bit unintuitive that you implement a Other API suggestions could be:
|
Co-authored-by: Mathias Vorreiter Pedersen <[email protected]>
@@ -106,6 +106,8 @@ private class MemcpyFunction extends ArrayFunction, DataFlowFunction, SideEffect | |||
not this.hasGlobalName(["bcopy", mempcpy(), "memccpy"]) and | |||
index = this.getParamDest() | |||
} | |||
|
|||
override TCxxException getExceptionType() { any() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this confusing. Together with the NonThrowing
this seems to say that:
This function is non-throwing, but only in the case of C++ exceptions. So it may still throw a SEH exception.
Is that the correct reading?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. That's what it is saying. It is no longer sufficient to say a function doesn't throw, you have to say how it doesn't throw (which kind of exception doesn't it throw). If they want to say it doesn't throw any you can just return the parent exception type.
The issue we got into with making memcpy nonthrowing is that it is true that it doesn't throw a C++ exception, but it absolutely throws a SEH exception. The mechanics in this PR force users to think about what it is they really want when they say a function throws or doesn't throw.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
I wonder if this is all somewhat overly complicated. In my understanding the following cases are interesting:
- C++ exceptions:
- Functions that may throw (this is the default, but are modelling of this in the IR is limited)
- Functions that do never throw a C++ exception, i.e., functions marked either as `noexcept or that are C functions (that we model)
- SEH exceptions:
- Functions that may throw (this is the default, and we want to improve the modelling in the IR here)
- Functions that always throw
Is this correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a brief meeting. The conclusion was that my assessment above is correct. The proposal is do something simpler:
- Rename,
NonThrowing
to something likeNonCppThrowingFunction
and introduce a deprecatedNonThrowing
alias. - Introduce a new class
AlwaysSehThrowingFunction
which is used to model functions that always throw an SEH exception. - Deprecate the
Throwing
class.
We should also remove the use of the Throwing
class in the models, but we can only do that in the next phase when we update the IR (otherwise the IR breaks).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overhauled the PR, let me know if that works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce a new class AlwaysSehThrowingFunction which is used to model functions that always throw an SEH exception.
@jketema When you say "always throw an SEH exception" does that mean that the function will always throw an SEH exception when called or that the function can only throw an SEH exception but doesn't necessarily always do so? To me the name sounds like the former but I guess it's actually the latter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The former: the function will always throw an SEH exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I see. So I guess it's for functions whose purpose is to raise exceptions? Like something along the lines of throw_my_SEH_exception
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes: RaiseException
, ExRaiseAccessViolation
, ExRaiseDatatypeMisalignment
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small comments, otherwise LGTM.
* A function that is guaranteed to never throw a C++ exception | ||
* (distinct from a structured exception handling, SEH, exception). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* A function that is guaranteed to never throw a C++ exception | |
* (distinct from a structured exception handling, SEH, exception). | |
* A function that is guaranteed to never throw a C++ exception. | |
* | |
* The function may still raise a structured exception handling (SEH) exception. |
* A class that models the exceptional behavior of a function. | ||
* A function that is known to raise an exception. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave this as it was, as we're going to deprecate this anyway.
ThrowingFunction() { any() } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem necessary?
/** | ||
* A function that is known to raise an exception unconditionally. | ||
* The only cases known where this happens is for SEH | ||
* (structured exception handling) exceptions. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since "SEH" is in the name of the class, I would change this to just:
/** | |
* A function that is known to raise an exception unconditionally. | |
* The only cases known where this happens is for SEH | |
* (structured exception handling) exceptions. | |
*/ | |
/** | |
* A function that unconditionally raises a structured exception handling (SEH) exception. | |
*/ |
final override predicate mayThrowException() { | ||
expr.getTarget().(ThrowingFunction).mayThrowException(_) | ||
or | ||
expr.getTarget() instanceof AlwaysSehThrowingFunction | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be able to remove this predicate completely, but let's leave that as a follow up.
Part of a phased approach to an overhaul of how exceptions are modeled and handled in IR, in order to handle both SEH (structured exception handling) and ordinary C++ exceptions. These two types of exceptions have subtly different mechanics and uses. Consequently, users cannot treat all exceptions as the same. E.g., a call to memset can raise an SEH exception but it would not raise an exception with C++ exceptions.
In this PR, I start by creating a new Throwing.qll library to provide metadata for any kind of exception, and a predicate to indicate if it is SEH or CXX. This change impacts models for NonThrowingFunctions, such as (as previously mentioned) memset. This PR, however, does not fully provide support to differentiate SEH and CXX exception handling in the IR. This will be addressed in future PRs.