Skip to content

[scripts] introduce static analysis tool - coccicheck#519

Open
TzengKyle wants to merge 1 commit into
littlekernel:masterfrom
TzengKyle:add-cocci
Open

[scripts] introduce static analysis tool - coccicheck#519
TzengKyle wants to merge 1 commit into
littlekernel:masterfrom
TzengKyle:add-cocci

Conversation

@TzengKyle

Copy link
Copy Markdown
Contributor

Introdution

Currently, the Little Kernel ecosystem is devoid of robust static analysis utilities for the efficient identification of prospective defects.

Considering that the Linux kernel utilizes various static analysis infrastructures to assist contributors in validating source code against contemporary C standards and neutralizing typical anti-patterns linked to system instability, I suggest the incorporation of coccicheck into the Little Kernel codebase.

Usage

Note: Before running this script, ensure that the Coccinelle tool (spatch) is installed on your system and you have already sucessfully buit the project you would like to check.

$ ./do-coccicheck <project> [M=<dir>] [COCCI=<file|dir>] [MODE=<report|context|patch>] [V=1] [S=<severity>]
$ ./scripts/do-coccicheck qemu-virt-arm64-test COCCI=countof
==================================================
Running Coccinelle (coccicheck)
Target Project   : qemu-virt-arm64-test
Target Directory : Whole Project
Cocci Script     : countof (1 files)
Analysis Mode    : report
Concurrency      : 64 jobs
Severity Filter  : All
==================================================
[misc/countof.cocci] external/lib/fdt/fdt_strerror.c:44:49-50: WARNING: Use countof
[misc/countof.cocci] lib/bio/disktest/disktest.c:755:32-33: WARNING: Use countof
[misc/countof.cocci] lib/heap/test/heap_tests.c:42:39-40: WARNING: Use countof
[misc/countof.cocci] lib/heap/test/heap_tests.c:195:36-37: WARNING: Use countof
[misc/countof.cocci] lib/heap/test/heap_tests.c:196:27-28: WARNING: Use countof
==================================================
Coccinelle analysis complete.
==================================================

New Files Introduction

This update introduces scripts/do-coccicheck, a script that enables developers to execute coccicheck via the spatch command, with terminal output filtered for improved readability. Additionally, a new directory, scripts/coccinelle, has been added to the codebase. This directory includes subfolders such as misc, free, and api, which categorize different .cocci files, mirroring the structure used in the Linux kernel.

Rationale

Coccicheck provides powerful static analysis capabilities. Below is an overview of the issues it effectively identifies:

  1. Replace Open-handed Code. For instance, countof.cocci identifies patterns like sizeof(x) / sizeof(x[0]), which should be replaced with the countof(x) macro. Similarly, strdup.cocci refactors manual string duplication (e.g., malloc(strlen(s) + 1) followed by strcpy/memcpy) to use strdup().
  2. Better C Coding Style.
    • ifnullfree.cocci: Removes redundant null checks, as free() safely accepts NULL pointers.
    • alloc_cast.cocci: Removes unnecessary pointer casts on memory allocation return values (common in C++ but redundant in C).
    • flexible_array.cocci: Replaces deprecated zero-length (arr[0]) or one-element (arr[1]) trailing arrays with standard C99 flexible-array members (arr[]).
  3. Potential Severe Error Pattern. For example, noderef.cocci identifies errors where sizeof(x) is applied to a pointer variable x. (This fix has been merged: #517)
  4. Trivial Code Smell semicolon.cocci cleans up code by removing redundant double semicolons (;;) and unnecessary empty statement semicolons following code blocks or switch cases.

Copilot AI review requested due to automatic review settings July 7, 2026 10:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a Coccinelle-based static analysis workflow (“coccicheck”) to LK by introducing a runner script and a small initial set of .cocci rules, aiming to help detect and optionally refactor common C pitfalls and style issues across LK sources.

Changes:

  • Add scripts/do-coccicheck to run spatch against a built project’s srcfiles.txt/include_paths.txt with optional filtering and parallelism.
  • Introduce initial Coccinelle rules under scripts/coccinelle/ (API usage, misc cleanups, free-related patterns).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
scripts/do-coccicheck Adds the coccicheck runner script (argument parsing, script selection, spatch invocation, output filtering).
scripts/coccinelle/misc/semicolon.cocci Rule to detect/remove redundant semicolons and ;; patterns.
scripts/coccinelle/misc/noderef.cocci Rule to report/patch sizeof(ptr)-style mistakes.
scripts/coccinelle/misc/flexible_array.cocci Rule to report (and partially patch) deprecated trailing array patterns.
scripts/coccinelle/misc/countof.cocci Rule to report/patch opencoded array-length division into countof(...).
scripts/coccinelle/free/ifnullfree.cocci Rule to remove redundant NULL checks before free().
scripts/coccinelle/api/strdup.cocci Rule to report/patch manual malloc+copy into strdup().
scripts/coccinelle/api/alloc_cast.cocci Rule to remove redundant allocation return casts in C code.

Comment thread scripts/do-coccicheck Outdated
Comment thread scripts/do-coccicheck Outdated
Comment thread scripts/do-coccicheck
Comment thread scripts/do-coccicheck
Comment thread scripts/do-coccicheck Outdated
Comment thread scripts/coccinelle/free/ifnullfree.cocci Outdated
Comment thread scripts/coccinelle/misc/semicolon.cocci Outdated
Comment thread scripts/coccinelle/misc/flexible_array.cocci
@TzengKyle

Copy link
Copy Markdown
Contributor Author

@copilot review

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.

2 participants