Skip to content

Commit 187390a

Browse files
committed
gcc: use SARIF output with --gcc-analyzer-bin
With https://copr.fedorainfracloud.org/coprs/dmalcolm/gcc-latest/ added to the `fedora-41-x86_64` mock config, this can be tested using the following commands: ``` koji download-build -a src units-2.23-3.fc41 csmock -f units-2.23-3.fc41.src.rpm -r fedora-41-x86_64 --install gcc-latest --gcc-analyzer-bin /opt/gcc-latest/bin/gcc ``` Depends-on: csutils/csdiff#210 Depends-on: csutils/csdiff#212 Related: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116613 Closes: #187
1 parent 014012a commit 187390a

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

make-srpm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Tool for plugging static analyzers into the build process, free of mock.
125125
126126
%package common
127127
Summary: Core of csmock (a mock wrapper for Static Analysis tools)
128-
Requires: csdiff > 3.1.0
128+
Requires: csdiff > 3.5.1
129129
Requires: csgcca
130130
Requires: cswrap
131131
Requires: mock >= 5.7

py/plugins/gcc.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# along with csmock. If not, see <http://www.gnu.org/licenses/>.
1717

1818
# standard imports
19+
import os
1920
import subprocess
2021

2122
# local imports
@@ -25,10 +26,20 @@
2526

2627
CSGCCA_BIN = "/usr/bin/csgcca"
2728

29+
# directory for GCC results (currently used only with `--gcc-analyzer-bin`)
30+
GCC_RESULTS_DIR = "/builddir/gcc-results"
31+
2832
CSMOCK_GCC_WRAPPER_NAME = 'csmock-gcc-wrapper'
2933
CSMOCK_GCC_WRAPPER_PATH = '/usr/bin/%s' % CSMOCK_GCC_WRAPPER_NAME
30-
CSMOCK_GCC_WRAPPER_TEMPLATE = '#!/bin/bash\n' \
31-
'exec %s "$@"'
34+
35+
# script to run gcc analyzer and dump its output to a seaprate SARIF file in GCC_RESULTS_DIR
36+
CSMOCK_GCC_WRAPPER_TEMPLATE = f"""#!/bin/bash
37+
fn=$(flock {GCC_RESULTS_DIR} mktemp "{GCC_RESULTS_DIR}/$$-XXXX.sarif")
38+
exec %s "$@" -fdiagnostics-set-output="sarif:file=$fn"
39+
"""
40+
41+
# command to read and join all captured SARIF files
42+
FILTER_CMD = "csgrep --mode=json --remove-duplicates"
3243

3344
SANITIZER_CAPTURE_DIR = "/builddir/gcc-sanitizer-capture"
3445

@@ -289,6 +300,23 @@ def csgcca_hook(results, mock):
289300
# tell csgcca to use the wrapped script rather than system gcc analyzer
290301
props.env["CSGCCA_ANALYZER_BIN"] = CSMOCK_GCC_WRAPPER_NAME
291302

303+
# create directory for gcc results
304+
def create_gcc_results_dir_hook(_, mock):
305+
cmd = f"mkdir -pv '{GCC_RESULTS_DIR}' && touch '{GCC_RESULTS_DIR}/empty.sarif'"
306+
return mock.exec_mockbuild_cmd(cmd)
307+
props.post_depinst_hooks += [create_gcc_results_dir_hook]
308+
309+
# copy gcc results out of the chroot
310+
props.copy_out_files += [GCC_RESULTS_DIR]
311+
312+
# process all captured SARIF files
313+
def filter_hook(results):
314+
src = os.path.join(results.dbgdir_raw, GCC_RESULTS_DIR[1:])
315+
dst = os.path.join(results.dbgdir_uni, "gcc-results.json")
316+
cmd = f'{FILTER_CMD} --file-glob "{src}/*.sarif" > "{dst}"'
317+
return results.exec_cmd(cmd, shell=True)
318+
props.post_process_hooks += [filter_hook]
319+
292320
# XXX: changing props this way is extremely fragile
293321
# insert csgcca right before cswrap to avoid chaining
294322
# csclng/cscppc while invoking `gcc -fanalyzer`

0 commit comments

Comments
 (0)