Skip to content

Run static analysis on OCaml C stubs in the CI #6338

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

Merged
merged 5 commits into from
Mar 5, 2025

Conversation

edwintorok
Copy link
Contributor

@edwintorok edwintorok commented Mar 4, 2025

Similar to the static analyzer introduced in xha, but for OCaml C stubs.

This uses my https://ocaml.org/p/dune-compiledb/latest to product a compile_commands.json for the C stubs out of the dune rules (which can also be useful if you want to use clangd and get some editor integration about compiler warnings). This requires installing enough of the build dependencies to be able to run dune successfully (perhaps in the future that restriction can be removed). Caching is used though, and we only need to install the build deps when dune files change, otherwise we can reuse a cached compile_commands.json.

Static analyzers, like CodeChecker support reading compile_commands.json and invoking static analyzers with the appropriate flags to preprocess, and analyze the C source code. We use clang, clang-tidy and cppcheck as the default analyzers, although more analyzers could be added in the future (CodeChecker supports converting gcc -fanalyzer output for example. GCC also supports emiting SARIF format directly, but github cannot parse it, because it doesn't implement the full SARIF spec).

At the end we convert the results back to the standard SARIF format that Github also supports for its code scanning results, which will make it automatically add comments on PRs that introduce new bugs, without necessarily gating on them.

I fixed some of the most obvious warnings, and suppressed some minor ones that we cannot fix (where the warning is caused by a Xen or OCaml header). More warnings can be skipped by adding to .codechecker.json if needed.

So far it seems to have found a file descriptor leak in unixpwd.c on an error path, but I haven't gone through all the reports in detail yet.

There is also a 'make analyze' to run this locally.

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Macros beginning with `_` are reserved by the language.

Fixes a CodeChecker warning.

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Fixes a warning reported by codechecker

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Signed-off-by: Edwin Török <edwin.torok@cloud.com>
Analyzer can't figure out that 'rc' is set on 'fd < 0',
and we never try to use 'size_in_bytes'.

Appears to be a limitation in the analyzer, it doesn't realize that `fd < 0` implies that `rc` is set to `NOT_IMPLEMENTED`,
which raises an exception, so `caml_copy_int64` is never reached.

But the logic there is quite hard to follow, so better be explicit, and ensure `size_in_bytes` is always initialized.

Signed-off-by: Edwin Török <edwin.torok@cloud.com>
@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@edwintorok edwintorok added this pull request to the merge queue Mar 5, 2025
Merged via the queue into xapi-project:master with commit f5cd21d Mar 5, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants