Skip to content

Commit 9b2590e

Browse files
committed
Updating PR per review comments. Moving more towards a simplified model.
1 parent 6aa7412 commit 9b2590e

File tree

10 files changed

+39
-94
lines changed

10 files changed

+39
-94
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedCall.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@ class TranslatedFunctionCall extends TranslatedCallExpr, TranslatedDirectCall {
363363
}
364364

365365
final override predicate mayThrowException() {
366-
expr.getTarget().(ThrowingFunction).mayThrowException(_)
366+
expr.getTarget() instanceof AlwaysSehThrowingFunction
367367
}
368368

369369
final override predicate mustThrowException() {
370-
expr.getTarget().(ThrowingFunction).mayThrowException(true)
370+
expr.getTarget() instanceof AlwaysSehThrowingFunction
371371
}
372372
}
373373

cpp/ql/lib/semmle/code/cpp/models/implementations/Memcpy.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import semmle.code.cpp.models.interfaces.DataFlow
99
import semmle.code.cpp.models.interfaces.Alias
1010
import semmle.code.cpp.models.interfaces.SideEffect
1111
import semmle.code.cpp.models.interfaces.Taint
12-
import semmle.code.cpp.models.interfaces.Throwing
12+
import semmle.code.cpp.models.interfaces.NonThrowing
1313

1414
/**
1515
* The standard functions `memcpy`, `memmove` and `bcopy`; and the gcc variant
1616
* `__builtin___memcpy_chk`.
1717
*/
1818
private class MemcpyFunction extends ArrayFunction, DataFlowFunction, SideEffectFunction,
19-
AliasFunction, NonThrowingFunction
19+
AliasFunction, NonCppThrowingFunction
2020
{
2121
MemcpyFunction() {
2222
// memcpy(dest, src, num)
@@ -106,8 +106,6 @@ private class MemcpyFunction extends ArrayFunction, DataFlowFunction, SideEffect
106106
not this.hasGlobalName(["bcopy", mempcpy(), "memccpy"]) and
107107
index = this.getParamDest()
108108
}
109-
110-
override TCxxException getExceptionType() { any() }
111109
}
112110

113111
private string mempcpy() { result = ["mempcpy", "wmempcpy"] }

cpp/ql/lib/semmle/code/cpp/models/implementations/Memset.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
88
import semmle.code.cpp.models.interfaces.DataFlow
99
import semmle.code.cpp.models.interfaces.Alias
1010
import semmle.code.cpp.models.interfaces.SideEffect
11-
import semmle.code.cpp.models.interfaces.Throwing
11+
import semmle.code.cpp.models.interfaces.NonThrowing
1212

1313
private class MemsetFunctionModel extends ArrayFunction, DataFlowFunction, AliasFunction,
14-
SideEffectFunction, NonThrowingFunction
14+
SideEffectFunction, NonCppThrowingFunction
1515
{
1616
MemsetFunctionModel() {
1717
this.hasGlobalOrStdOrBslName("memset")
@@ -74,8 +74,6 @@ private class MemsetFunctionModel extends ArrayFunction, DataFlowFunction, Alias
7474
i = 0 and
7575
if this.hasGlobalName(bzero()) then result = 1 else result = 2
7676
}
77-
78-
override TCxxException getExceptionType() { any() }
7977
}
8078

8179
private string bzero() { result = ["bzero", "explicit_bzero"] }
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import semmle.code.cpp.models.interfaces.Throwing
1+
import semmle.code.cpp.models.interfaces.NonThrowing
22

33
/**
44
* A function that is annotated with a `noexcept` specifier (or the equivalent
55
* `throw()` specifier) guaranteeing that the function can not throw exceptions.
66
*
77
* Note: The `throw` specifier was deprecated in C++11 and removed in C++17.
88
*/
9-
class NoexceptFunction extends NonThrowingFunction {
9+
class NoexceptFunction extends NonCppThrowingFunction {
1010
NoexceptFunction() { this.isNoExcept() or this.isNoThrow() }
11-
12-
override TCxxException getExceptionType() { any() }
1311
}

cpp/ql/lib/semmle/code/cpp/models/implementations/Printf.qll

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import semmle.code.cpp.models.interfaces.FormattingFunction
99
import semmle.code.cpp.models.interfaces.Alias
1010
import semmle.code.cpp.models.interfaces.SideEffect
11-
import semmle.code.cpp.models.interfaces.Throwing
11+
import semmle.code.cpp.models.interfaces.NonThrowing
1212

1313
/**
1414
* The standard functions `printf`, `wprintf` and their glib variants.
1515
*/
16-
private class Printf extends FormattingFunction, AliasFunction, NonThrowingFunction {
16+
private class Printf extends FormattingFunction, AliasFunction, NonCppThrowingFunction {
1717
Printf() {
1818
this instanceof TopLevelFunction and
1919
(
@@ -32,8 +32,6 @@ private class Printf extends FormattingFunction, AliasFunction, NonThrowingFunct
3232
override predicate parameterEscapesOnlyViaReturn(int n) { none() }
3333

3434
override predicate parameterIsAlwaysReturned(int n) { none() }
35-
36-
override TCxxException getExceptionType() { any() }
3735
}
3836

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

5452
override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = true }
55-
56-
override TCxxException getExceptionType() { any() }
5753
}
5854

5955
/**
@@ -97,8 +93,6 @@ private class Sprintf extends FormattingFunction, NonThrowingFunction {
9793
then result = 4
9894
else result = super.getFirstFormatArgumentIndex()
9995
}
100-
101-
override TCxxException getExceptionType() { any() }
10296
}
10397

10498
/**
@@ -171,8 +165,6 @@ private class SnprintfImpl extends Snprintf, AliasFunction, SideEffectFunction,
171165
// We don't know how many parameters are passed to the function since it's varargs, but they also have read side effects.
172166
i = this.getFormatParameterIndex() and buffer = true
173167
}
174-
175-
override TCxxException getExceptionType() { any() }
176168
}
177169

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

225217
override predicate isOutputGlobal() { any() }
226-
227-
override TCxxException getExceptionType() { any() }
228218
}

cpp/ql/lib/semmle/code/cpp/models/implementations/Strcat.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
77
import semmle.code.cpp.models.interfaces.DataFlow
88
import semmle.code.cpp.models.interfaces.Taint
99
import semmle.code.cpp.models.interfaces.SideEffect
10-
import semmle.code.cpp.models.interfaces.Throwing
10+
import semmle.code.cpp.models.interfaces.NonThrowing
1111

1212
/**
1313
* The standard function `strcat` and its wide, sized, and Microsoft variants.
1414
*
1515
* Does not include `strlcat`, which is covered by `StrlcatFunction`
1616
*/
1717
class StrcatFunction extends TaintFunction, DataFlowFunction, ArrayFunction, SideEffectFunction,
18-
NonThrowingFunction
18+
NonCppThrowingFunction
1919
{
2020
StrcatFunction() {
2121
this.hasGlobalOrStdOrBslName([
@@ -94,8 +94,6 @@ class StrcatFunction extends TaintFunction, DataFlowFunction, ArrayFunction, Sid
9494
(i = 0 or i = 1) and
9595
buffer = true
9696
}
97-
98-
override TCxxException getExceptionType() { any() }
9997
}
10098

10199
/**

cpp/ql/lib/semmle/code/cpp/models/implementations/Strcpy.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import semmle.code.cpp.models.interfaces.ArrayFunction
77
import semmle.code.cpp.models.interfaces.DataFlow
88
import semmle.code.cpp.models.interfaces.Taint
99
import semmle.code.cpp.models.interfaces.SideEffect
10-
import semmle.code.cpp.models.interfaces.Throwing
10+
import semmle.code.cpp.models.interfaces.NonThrowing
1111

1212
/**
1313
* The standard function `strcpy` and its wide, sized, and Microsoft variants.
1414
*/
1515
class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction, SideEffectFunction,
16-
NonThrowingFunction
16+
NonCppThrowingFunction
1717
{
1818
StrcpyFunction() {
1919
this.hasGlobalOrStdOrBslName([
@@ -145,6 +145,4 @@ class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction, Sid
145145
i = this.getParamDest() and
146146
result = this.getParamSize()
147147
}
148-
149-
override TCxxException getExceptionType() { any() }
150148
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import semmle.code.cpp.models.interfaces.Throwing
22

3-
class WindowsDriverExceptionAnnotation extends ThrowingFunction {
3+
class WindowsDriverExceptionAnnotation extends AlwaysSehThrowingFunction {
44
WindowsDriverExceptionAnnotation() {
55
this.hasGlobalName(["RaiseException", "ExRaiseAccessViolation", "ExRaiseDatatypeMisalignment"])
66
}
7-
8-
override predicate mayThrowException(boolean unconditional) { unconditional = true }
9-
10-
override TSehException getExceptionType() { any() }
117
}

cpp/ql/lib/semmle/code/cpp/models/interfaces/NonThrowing.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
import semmle.code.cpp.Function
66
import semmle.code.cpp.models.Models
77

8+
/**
9+
* A function that is guaranteed to never throw a C++ exception
10+
* (distinct from a structured exception handling, SEH, exception).
11+
*/
12+
abstract class NonCppThrowingFunction extends Function { }
13+
814
/**
915
* A function that is guaranteed to never throw.
1016
*
11-
* DEPRECATED: use `NonThrowingFunction` in `semmle.code.cpp.models.Models.Interfaces.Throwing` instead.
17+
* DEPRECATED: use `NonCppThrowingFunction` instead.
1218
*/
13-
abstract deprecated class NonThrowingFunction extends Function { }
19+
deprecated class NonThrowingFunction = NonCppThrowingFunction;

cpp/ql/lib/semmle/code/cpp/models/interfaces/Throwing.qll

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,65 +11,28 @@ import semmle.code.cpp.models.Models
1111
import semmle.code.cpp.models.interfaces.FunctionInputsAndOutputs
1212

1313
/**
14-
* Represents a type of exception,
15-
* either Structured Exception Handling (SEH) or C++ exceptions.
16-
*/
17-
newtype TException =
18-
/** Structured Exception Handling (SEH) exception */
19-
TSehException() or
20-
/** C++ exception */
21-
TCxxException()
22-
23-
/**
24-
* Functions with information about how an exception is thrown or if one is thrown at all.
25-
* If throwing details conflict for the same function, IR is assumed
26-
* to use the most restricted interpretation, meaning taking options
27-
* that stipulate no exception is raised, before the exception is always raised,
28-
* before conditional exceptions.
14+
* A function that is known to raise an exception.
2915
*
30-
* Annotations must specify if the exception is from SEH (structured exception handling)
31-
* or ordinary c++ exceptions.
16+
* DEPRECATED: use `AlwaysSehThrowingFunction` instead if a function unconditionally throws.
17+
* These are assumed the only case where functions throw/raise exceptions unconditionally.
18+
* For functions that may throw, this will be the default behavior in the IR.
3219
*/
33-
abstract private class ExceptionAnnotation extends Function {
34-
/**
35-
* Returns the type of exception this annotation is for,
36-
* either a CPP exception or a STructured Exception Handling (SEH) exception.
37-
*/
38-
abstract TException getExceptionType();
39-
40-
/**
41-
* Holds if the exception type of this annotation is for a Structured Exception Handling (SEH) exception.
42-
*/
43-
final predicate isSeh() { this.getExceptionType() = TSehException() }
20+
abstract deprecated class ThrowingFunction extends Function {
21+
ThrowingFunction() { any() }
4422

4523
/**
46-
* Holds if the exception type of this annotation is for a CPP exception.
24+
* Holds if this function may throw an exception during evaluation.
25+
* If `unconditional` is `true` the function always throws an exception.
26+
*
27+
* DPERECATED: for always throwing functions use `AlwaysSehThrowingFunction` instead.
28+
* For functions that may throw, this will be the default behavior in the IR.
4729
*/
48-
final predicate isCxx() { this.getExceptionType() = TCxxException() }
30+
abstract deprecated predicate mayThrowException(boolean unconditional);
4931
}
5032

5133
/**
52-
* A Function that is known to not throw an exception.
53-
*/
54-
abstract class NonThrowingFunction extends ExceptionAnnotation { }
55-
56-
/**
57-
* A function this is known to raise an exception.
34+
* A function that is known to raise an exception unconditionally.
35+
* The only cases known where this happens is for SEH
36+
* (structured exception handling) exceptions.
5837
*/
59-
abstract class ThrowingFunction extends ExceptionAnnotation {
60-
ThrowingFunction() { any() }
61-
62-
/**
63-
* Holds if this function may raise an exception during evaluation.
64-
* If `unconditional` is `false` the function may raise, and if `true` the function
65-
* will always raise an exception.
66-
* Do not specify `none()` if no exception is raised, instead use the
67-
* `NonThrowingFunction` class instead.
68-
*/
69-
abstract predicate mayThrowException(boolean unconditional);
70-
71-
/**
72-
* Holds if this function will always raise an exception if called
73-
*/
74-
final predicate alwaysThrowsException() { this.mayThrowException(true) }
75-
}
38+
abstract class AlwaysSehThrowingFunction extends Function { }

0 commit comments

Comments
 (0)