-
Notifications
You must be signed in to change notification settings - Fork 286
Add SARIF output support via --sarif-result option #8835
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
Open
tautschnig
wants to merge
1
commit into
diffblue:develop
Choose a base branch
from
tautschnig:sarif-ui
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| [CPROVER Manual TOC](../) | ||
|
|
||
| # SARIF Output | ||
|
|
||
| CBMC and JBMC support [SARIF](https://sarifweb.azurewebsites.net/) (Static | ||
| Analysis Results Interchange Format) output, which is widely supported by IDEs | ||
| and CI systems. Use `--sarif-result` to write SARIF output to a file: | ||
|
|
||
| ``` | ||
| cbmc file1.c --bounds-check --pointer-check --sarif-result results.sarif | ||
| ``` | ||
|
|
||
| Use `-` as the filename to write to standard output. The `--sarif-result` | ||
| option can be combined with other UI modes (e.g., `--json-ui`, `--xml-ui`), | ||
| producing both the normal output and a SARIF file. | ||
|
|
||
| This produces a JSON document conforming to the SARIF 2.1.0 schema, with each | ||
| property reported as a SARIF result entry including rule identifier, severity | ||
| level, message, and source location. For example: | ||
|
|
||
| ```json | ||
| { | ||
| "$schema": "https://json.schemastore.org/sarif-2.1.0.json", | ||
| "version": "2.1.0", | ||
| "runs": [{ | ||
| "tool": { | ||
| "driver": { | ||
| "name": "CBMC ...", | ||
| "informationUri": "https://www.cprover.org/cbmc/" | ||
| } | ||
| }, | ||
| "results": [{ | ||
| "ruleId": "main.assertion.1", | ||
| "level": "error", | ||
| "message": { "text": "assertion x > 0 (FAILURE)" }, | ||
| "locations": [{ | ||
| "physicalLocation": { | ||
| "artifactLocation": { "uri": "example.c" }, | ||
| "region": { "startLine": 5 } | ||
| } | ||
| }] | ||
| }] | ||
| }] | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| add_test_pl_tests("$<TARGET_FILE:cbmc>") | ||
|
|
||
| add_test( | ||
| NAME cbmc-sarif-validation | ||
| COMMAND bash "${CMAKE_CURRENT_SOURCE_DIR}/test_sarif_valid.sh" | ||
| "$<TARGET_FILE:cbmc>" | ||
| WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" | ||
| ) | ||
| set_tests_properties(cbmc-sarif-validation PROPERTIES LABELS "CORE;CBMC") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| default: test | ||
|
|
||
| include ../../src/config.inc | ||
| include ../../src/common | ||
|
|
||
| test: | ||
| @../test.pl -e -p -c ../../../src/cbmc/cbmc | ||
| @bash test_sarif_valid.sh ../../src/cbmc/cbmc | ||
|
|
||
| tests.log: ../test.pl test | ||
|
|
||
| clean: | ||
| find . -name '*.out' -execdir $(RM) '{}' \; | ||
| $(RM) tests*.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| int main() | ||
| { | ||
| int a[5]; | ||
| int i; | ||
| a[i] = 1; | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| CORE | ||
| bounds.c | ||
| --sarif-result - --bounds-check | ||
| activate-multi-line-match | ||
| ^EXIT=10$ | ||
| ^SIGNAL=0$ | ||
| "\$schema": "https://json\.schemastore\.org/sarif-2\.1\.0\.json" | ||
| "version": "2\.1\.0" | ||
| "ruleId": "main\.array_bounds\.1" | ||
| "ruleId": "main\.array_bounds\.2" | ||
| "level": "error" | ||
| "startLine": 5 | ||
| -- | ||
| -- | ||
| Checks that --sarif-result - produces multiple SARIF results for bounds-check failures. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| int main() | ||
| { | ||
| unsigned x; | ||
| assert(x == 0); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| CORE | ||
| failure.c | ||
| --sarif-result - | ||
| activate-multi-line-match | ||
| ^EXIT=10$ | ||
| ^SIGNAL=0$ | ||
| "\$schema": "https://json\.schemastore\.org/sarif-2\.1\.0\.json" | ||
| "version": "2\.1\.0" | ||
| "ruleId": "main\.assertion\.1" | ||
| "level": "error" | ||
| "text": "assertion x == \(unsigned int\)0 \(FAILURE\)" | ||
| "startLine": 4 | ||
| -- | ||
| "level": "note" | ||
| -- | ||
| Checks that --sarif-result - produces error-level SARIF results for failed assertions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| int main() | ||
| { | ||
| int x = 1; | ||
| __CPROVER_assert(x == 0, "expected fail"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| CORE | ||
| test.c | ||
| --json-ui --sarif-result sarif.out | ||
| activate-multi-line-match | ||
| ^EXIT=10$ | ||
| ^SIGNAL=0$ | ||
| "cProverStatus": "failure" | ||
| "property": "main\.assertion\.1" | ||
| "status": "FAILURE" | ||
| -- | ||
| -- | ||
| Checks that --sarif-result combined with --json-ui produces JSON UI on stdout. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| int main() | ||
| { | ||
| int x; | ||
| assert(x >= 0); | ||
| int y = 1; | ||
| assert(y == 1); | ||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| CORE | ||
| mixed.c | ||
| --sarif-result - | ||
| activate-multi-line-match | ||
| ^EXIT=10$ | ||
| ^SIGNAL=0$ | ||
| "ruleId": "main\.assertion\.1" | ||
| "ruleId": "main\.assertion\.2" | ||
| "level": "error" | ||
| "level": "note" | ||
| "text": "assertion x >= 0 \(FAILURE\)" | ||
| "text": "assertion y == 1 \(SUCCESS\)" | ||
| -- | ||
| -- | ||
| Checks that --sarif-result - correctly reports mixed pass/fail with error and note levels. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #include <assert.h> | ||
|
|
||
| int main() | ||
| { | ||
| int x = 1; | ||
| assert(x == 1); | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| CORE | ||
| success.c | ||
| --sarif-result - | ||
| activate-multi-line-match | ||
| ^EXIT=0$ | ||
| ^SIGNAL=0$ | ||
| "\$schema": "https://json\.schemastore\.org/sarif-2\.1\.0\.json" | ||
| "version": "2\.1\.0" | ||
| "name": "cbmc" | ||
| "informationUri": "https://www\.cprover\.org/cbmc/" | ||
| "ruleId": "main\.assertion\.1" | ||
| "level": "note" | ||
| "text": "assertion x == 1 \(SUCCESS\)" | ||
| "startLine": 6 | ||
| -- | ||
| "level": "error" | ||
| -- | ||
| Checks that --sarif-result - produces valid SARIF output for successful verification. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #!/bin/bash | ||
| # Runs cbmc --sarif-result on test inputs and validates the SARIF output. | ||
| # Usage: test_sarif_valid.sh <cbmc-binary> | ||
| # Exit 0 if all validations pass, 1 otherwise. | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" | ||
| CBMC="$1" | ||
| FAILED=0 | ||
| TMPFILE=$(mktemp) | ||
| trap 'rm -f "$TMPFILE"' EXIT | ||
|
|
||
| for test_case in success failure bounds mixed; do | ||
| case "$test_case" in | ||
| bounds) OPTS="--bounds-check" ;; | ||
| *) OPTS="" ;; | ||
| esac | ||
|
|
||
| "$CBMC" --sarif-result "$TMPFILE" $OPTS "$SCRIPT_DIR/${test_case}/${test_case}.c" >/dev/null 2>&1 || true | ||
| if ! python3 "$SCRIPT_DIR/validate_sarif.py" "$TMPFILE"; then | ||
| echo "FAILED: ${test_case}.c" | ||
| cat "$TMPFILE" | ||
| FAILED=1 | ||
| fi | ||
| done | ||
|
|
||
| # Test combinations with other UI modes | ||
| for ui_mode in "--json-ui" "--xml-ui"; do | ||
| "$CBMC" $ui_mode --sarif-result "$TMPFILE" "$SCRIPT_DIR/failure/failure.c" >/dev/null 2>&1 || true | ||
| if ! python3 "$SCRIPT_DIR/validate_sarif.py" "$TMPFILE"; then | ||
| echo "FAILED: failure.c with $ui_mode" | ||
| cat "$TMPFILE" | ||
| FAILED=1 | ||
| fi | ||
| done | ||
|
|
||
| if [ $FAILED -eq 0 ]; then | ||
| echo "All SARIF validation tests passed" | ||
| fi | ||
| exit $FAILED |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.