Skip to content

Commit 70477e4

Browse files
Recognize FW_ASSERT in JPL CodeQL Rule 16 assertion queries
Co-Authored-By: michael.d.starch <michael.d.starch@jpl.nasa.gov>
1 parent 3067220 commit 70477e4

9 files changed

Lines changed: 151 additions & 0 deletions

.github/actions/codeql/jpl-standard-pack-1.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ packs:
66
# Source of the query pack is https://github.com/github/codeql/tree/main/cpp/ql/src/JPL_C
77
- codeql/cpp-queries:JPL_C
88

9+
queries:
10+
# FW_ASSERT-aware replacements for the Rule 16 assertion queries
11+
- uses: ./.github/codeql/fprime-queries
12+
913
query-filters:
1014
- exclude:
1115
problem.severity:
1216
- recommendation
17+
# Excluded in favor of the FW_ASSERT-aware replacements above, which
18+
# recognize F Prime's FW_ASSERT macro as an assertion
19+
- exclude:
20+
id:
21+
- cpp/jpl-c/use-of-assertions-constant
22+
- cpp/jpl-c/use-of-assertions-non-boolean
23+
- cpp/jpl-c/use-of-assertions-side-effect

.github/actions/codeql/jpl-standard-pack-2.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ packs:
66
# Source of the query pack is https://github.com/github/codeql/tree/main/cpp/ql/src/JPL_C
77
- codeql/cpp-queries:JPL_C
88

9+
queries:
10+
# FW_ASSERT-aware replacements for the Rule 16 assertion queries
11+
- uses: ./.github/codeql/fprime-queries
12+
913
query-filters:
1014
- exclude:
1115
problem.severity:
@@ -18,3 +22,8 @@ query-filters:
1822
- exclude:
1923
id:
2024
- cpp/jpl-c/basic-int-types
25+
# Excluded in favor of the FW_ASSERT-aware replacement above, which
26+
# recognizes F Prime's FW_ASSERT macro as an assertion
27+
- exclude:
28+
id:
29+
- cpp/jpl-c/use-of-assertions-density
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Extends the standard CodeQL assertion library with support for the F Prime
3+
* `FW_ASSERT` macro family, so that the JPL Coding Standard Rule 16 queries
4+
* recognize F Prime assertions.
5+
*/
6+
7+
import cpp
8+
import semmle.code.cpp.commons.Assertions
9+
10+
/**
11+
* An F Prime assertion: an invocation of an `FW_ASSERT` macro, which expands
12+
* to a conditional expression of the form
13+
* `(condition ? (void)0 : Fw::SwAssert(...))`.
14+
*/
15+
class FwAssert extends MacroInvocation, Assertion {
16+
FwAssert() { this.getMacro().getName() = "FW_ASSERT" }
17+
18+
override Expr getAsserted() {
19+
exists(ConditionalExpr ce |
20+
ce = this.getAGeneratedElement() and
21+
// Restrict to the outermost conditional generated by this macro
22+
// invocation (nested FW_ASSERTs are separate macro invocations).
23+
not exists(ConditionalExpr outer |
24+
outer = this.getAGeneratedElement() and
25+
ce.getParent+() = outer
26+
)
27+
|
28+
result = ce.getCondition()
29+
)
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @name Constant assertion
3+
* @description Assertions should check dynamic properties of pre-/post-conditions and invariants. Assertions that either always succeed or always fail are an error.
4+
* @kind problem
5+
* @id cpp/fprime/jpl-c/use-of-assertions-constant
6+
* @problem.severity warning
7+
* @tags maintainability
8+
* reliability
9+
* external/jpl
10+
*/
11+
12+
import FprimeAssertions
13+
14+
from Assertion a, string value, string msg
15+
where
16+
value = a.getAsserted().getValue() and
17+
if value.toInt() = 0
18+
then msg = "This assertion is always false."
19+
else msg = "This assertion is always true."
20+
select a.getAsserted(), msg
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Long function without assertion
3+
* @description All functions of more than 10 lines should have at least one assertion.
4+
* @kind problem
5+
* @id cpp/fprime/jpl-c/use-of-assertions-density
6+
* @problem.severity recommendation
7+
* @tags maintainability
8+
* reliability
9+
* external/jpl
10+
*/
11+
12+
import FprimeAssertions
13+
14+
from Function f
15+
where
16+
f.getMetrics().getNumberOfLinesOfCode() > 10 and
17+
not exists(Assertion a | a.getAsserted().getEnclosingFunction() = f)
18+
select f, "All functions of more than 10 lines should have at least one assertion."
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Non-boolean assertion
3+
* @description Assertions should be defined as Boolean tests, meaning "assert(p != NULL)" rather than "assert(p)".
4+
* @kind problem
5+
* @id cpp/fprime/jpl-c/use-of-assertions-non-boolean
6+
* @problem.severity warning
7+
* @tags correctness
8+
* external/jpl
9+
*/
10+
11+
import FprimeAssertions
12+
13+
from Assertion a
14+
where a.getAsserted().getType() instanceof PointerType
15+
select a.getAsserted(), "Assertions should be defined as Boolean tests."
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Assertion with side effects
3+
* @description Assertions should not have side-effects -- they may be disabled completely, changing program behavior.
4+
* @kind problem
5+
* @id cpp/fprime/jpl-c/use-of-assertions-side-effect
6+
* @problem.severity warning
7+
* @tags correctness
8+
* external/jpl
9+
*/
10+
11+
import FprimeAssertions
12+
13+
from Assertion a
14+
where not a.getAsserted().isPure()
15+
select a.getAsserted(), "Assertions should not have side effects."
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
lockVersion: 1.0.0
3+
dependencies:
4+
codeql/controlflow:
5+
version: 2.0.35
6+
codeql/cpp-all:
7+
version: 10.2.0
8+
codeql/dataflow:
9+
version: 2.1.7
10+
codeql/mad:
11+
version: 1.0.51
12+
codeql/quantum:
13+
version: 0.0.29
14+
codeql/rangeanalysis:
15+
version: 1.0.51
16+
codeql/ssa:
17+
version: 2.0.27
18+
codeql/tutorial:
19+
version: 1.0.51
20+
codeql/typeflow:
21+
version: 1.0.51
22+
codeql/typetracking:
23+
version: 2.0.35
24+
codeql/util:
25+
version: 2.0.38
26+
codeql/xml:
27+
version: 1.0.51
28+
compiled: false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: fprime/jpl-c-queries
2+
version: 1.0.0
3+
dependencies:
4+
codeql/cpp-all: "*"

0 commit comments

Comments
 (0)