maishac 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- maishac/__init__.py +16 -0
- maishac/analyzers/__init__.py +60 -0
- maishac/analyzers/base.py +66 -0
- maishac/analyzers/clang_tidy.py +90 -0
- maishac/analyzers/cppcheck.py +136 -0
- maishac/analyzers/native.py +269 -0
- maishac/cli.py +317 -0
- maishac/engine/__init__.py +414 -0
- maishac/mcp_server.py +428 -0
- maishac/memory/__init__.py +466 -0
- maishac/model.py +157 -0
- maishac/patterns.py +481 -0
- maishac/report.py +660 -0
- maishac/rules/__init__.py +113 -0
- maishac/rules/barr_c_2018.json +26 -0
- maishac/rules/cert_c.json +36 -0
- maishac/rules/misra_c_2012.json +37 -0
- maishac-0.3.1.dist-info/METADATA +410 -0
- maishac-0.3.1.dist-info/RECORD +23 -0
- maishac-0.3.1.dist-info/WHEEL +5 -0
- maishac-0.3.1.dist-info/entry_points.txt +2 -0
- maishac-0.3.1.dist-info/licenses/LICENSE +28 -0
- maishac-0.3.1.dist-info/top_level.txt +1 -0
maishac/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Maisha: agent harness for MISRA C, BARR-C and CERT C compliance.
|
|
2
|
+
|
|
3
|
+
Architecture (all IDE-agnostic, exposed over MCP or CLI):
|
|
4
|
+
|
|
5
|
+
analyzers/ pluggable evidence sources (native lexer checks, cppcheck,
|
|
6
|
+
clang-tidy, gcc) that all emit normalized Finding objects
|
|
7
|
+
rules/ standards knowledge base + cross-standard rule mapping
|
|
8
|
+
memory/ SQLite-backed persistent memory: finding fingerprints,
|
|
9
|
+
fix history, deviations, suppressions, project conventions
|
|
10
|
+
engine/ the engineered agent loop: baseline -> prioritize -> fix ->
|
|
11
|
+
verify -> converge, with budgets and oscillation detection
|
|
12
|
+
mcp_server FastMCP stdio server exposing the harness to any agentic IDE
|
|
13
|
+
cli standalone command line interface (scan/fix-session/report)
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
__version__ = "0.3.1"
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Analyzer orchestration: run every available evidence source and merge."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .base import Analyzer, collect_c_files
|
|
8
|
+
from .native import NativeAnalyzer
|
|
9
|
+
from .cppcheck import CppcheckAnalyzer
|
|
10
|
+
from .clang_tidy import ClangTidyAnalyzer
|
|
11
|
+
from ..model import Finding
|
|
12
|
+
|
|
13
|
+
ALL_ANALYZERS: list[type[Analyzer]] = [NativeAnalyzer, CppcheckAnalyzer, ClangTidyAnalyzer]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def available_analyzers(only: list[str] | None = None) -> list[Analyzer]:
|
|
17
|
+
out = []
|
|
18
|
+
for cls in ALL_ANALYZERS:
|
|
19
|
+
inst = cls()
|
|
20
|
+
if only and inst.name not in only:
|
|
21
|
+
continue
|
|
22
|
+
if inst.available():
|
|
23
|
+
out.append(inst)
|
|
24
|
+
return out
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def run_scan(paths: list[str], root: Path,
|
|
28
|
+
analyzers: list[str] | None = None,
|
|
29
|
+
include_paths: list[str] | None = None) -> tuple[list[Finding], list[str]]:
|
|
30
|
+
"""Run all (or the named) available analyzers over paths.
|
|
31
|
+
|
|
32
|
+
include_paths are forwarded to analyzers that compile/parse (cppcheck,
|
|
33
|
+
clang-tidy) as -I flags — without them, headers outside the scanned paths
|
|
34
|
+
(e.g. a project's FreeRTOSConfig.h) are invisible and those analyzers
|
|
35
|
+
misreport "undefined"/"file not found" false positives (see BENCHMARKS.md).
|
|
36
|
+
|
|
37
|
+
Returns (deduped findings sorted by severity/file/line, analyzer names used).
|
|
38
|
+
Dedup rule: fingerprint collision keeps the first occurrence but records the
|
|
39
|
+
extra analyzer as reinforcing evidence in the message.
|
|
40
|
+
"""
|
|
41
|
+
files = collect_c_files(paths, root)
|
|
42
|
+
active = available_analyzers(analyzers)
|
|
43
|
+
merged: dict[str, Finding] = {}
|
|
44
|
+
used = []
|
|
45
|
+
for an in active:
|
|
46
|
+
used.append(an.name)
|
|
47
|
+
for fnd in an.analyze(files, root, include_paths):
|
|
48
|
+
key = fnd.fingerprint
|
|
49
|
+
if key in merged:
|
|
50
|
+
prior = merged[key]
|
|
51
|
+
if an.name not in prior.analyzer:
|
|
52
|
+
prior.analyzer = f"{prior.analyzer}+{an.name}"
|
|
53
|
+
else:
|
|
54
|
+
merged[key] = fnd
|
|
55
|
+
findings = sorted(
|
|
56
|
+
merged.values(),
|
|
57
|
+
key=lambda f: ({"blocker": 0, "critical": 1, "major": 2, "minor": 3, "info": 4}
|
|
58
|
+
.get(f.severity, 5), f.file, f.line),
|
|
59
|
+
)
|
|
60
|
+
return findings, used
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Analyzer plugin interface.
|
|
2
|
+
|
|
3
|
+
Every evidence source (native lexer, cppcheck, clang-tidy, compiler) subclasses
|
|
4
|
+
Analyzer and yields normalized Finding objects. The harness merges and
|
|
5
|
+
deduplicates them by fingerprint so overlapping tools reinforce rather than
|
|
6
|
+
duplicate each other.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import shutil
|
|
12
|
+
import subprocess
|
|
13
|
+
from abc import ABC, abstractmethod
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from ..model import Finding
|
|
17
|
+
|
|
18
|
+
C_EXTENSIONS = {".c", ".h"}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def collect_c_files(paths: list[str], root: Path) -> list[Path]:
|
|
22
|
+
out: list[Path] = []
|
|
23
|
+
for p in paths:
|
|
24
|
+
pth = (root / p) if not Path(p).is_absolute() else Path(p)
|
|
25
|
+
if pth.is_dir():
|
|
26
|
+
out.extend(sorted(f for f in pth.rglob("*") if f.suffix in C_EXTENSIONS))
|
|
27
|
+
elif pth.suffix in C_EXTENSIONS and pth.exists():
|
|
28
|
+
out.append(pth)
|
|
29
|
+
# dedupe preserving order
|
|
30
|
+
seen, uniq = set(), []
|
|
31
|
+
for f in out:
|
|
32
|
+
r = f.resolve()
|
|
33
|
+
if r not in seen:
|
|
34
|
+
seen.add(r)
|
|
35
|
+
uniq.append(f)
|
|
36
|
+
return uniq
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Analyzer(ABC):
|
|
40
|
+
name: str = "base"
|
|
41
|
+
requires: str | None = None # executable dependency, if any
|
|
42
|
+
options: str = "" # invocation options Maisha uses (for the GEP tool record)
|
|
43
|
+
|
|
44
|
+
def available(self) -> bool:
|
|
45
|
+
return self.requires is None or shutil.which(self.requires) is not None
|
|
46
|
+
|
|
47
|
+
def version(self) -> str:
|
|
48
|
+
"""Version string of the underlying tool, for the Guideline Enforcement
|
|
49
|
+
Plan's tool record. Best-effort; '<name> --version' first line."""
|
|
50
|
+
if self.requires is None:
|
|
51
|
+
return "built-in"
|
|
52
|
+
try:
|
|
53
|
+
proc = self._run([self.requires, "--version"], timeout=10)
|
|
54
|
+
first = (proc.stdout or proc.stderr).strip().splitlines()
|
|
55
|
+
return first[0].strip() if first else "unknown"
|
|
56
|
+
except Exception: # noqa: BLE001 — a missing/odd tool must not break the GEP
|
|
57
|
+
return "unknown"
|
|
58
|
+
|
|
59
|
+
@abstractmethod
|
|
60
|
+
def analyze(self, files: list[Path], root: Path,
|
|
61
|
+
include_paths: list[str] | None = None) -> list[Finding]:
|
|
62
|
+
...
|
|
63
|
+
|
|
64
|
+
@staticmethod
|
|
65
|
+
def _run(cmd: list[str], timeout: int = 300) -> subprocess.CompletedProcess:
|
|
66
|
+
return subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"""clang-tidy adapter.
|
|
2
|
+
|
|
3
|
+
Enables the cert-* check group (plus bugprone/clang-analyzer evidence) and
|
|
4
|
+
maps diagnostics back onto CERT C rules by their check name, e.g.
|
|
5
|
+
`cert-err34-c` -> `CERT ERR34-C`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from .base import Analyzer
|
|
14
|
+
from ..model import Finding, enclosing_function, relpath
|
|
15
|
+
from ..rules import REGISTRY
|
|
16
|
+
|
|
17
|
+
_DIAG = re.compile(
|
|
18
|
+
# `file` is non-greedy .+? rather than [^:\n]+ so a Windows drive-letter
|
|
19
|
+
# colon ("D:\path\file.c") doesn't get mistaken for the line:col
|
|
20
|
+
# separator — clang-tidy always emits absolute (drive-letter) paths in
|
|
21
|
+
# its diagnostics on Windows regardless of how the path was passed in,
|
|
22
|
+
# and the old pattern silently matched zero diagnostics as a result.
|
|
23
|
+
r"^(?P<file>.+?):(?P<line>\d+):(?P<col>\d+):\s+(?P<sev>warning|error):\s+"
|
|
24
|
+
r"(?P<msg>.*?)\s+\[(?P<check>[\w\-.,]+)\]\s*$", re.MULTILINE)
|
|
25
|
+
_CERT_CHECK = re.compile(r"cert-([a-z]{3}\d{2})-c")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class ClangTidyAnalyzer(Analyzer):
|
|
29
|
+
name = "clang-tidy"
|
|
30
|
+
requires = "clang-tidy"
|
|
31
|
+
options = "--checks=-*,cert-*,bugprone-*,clang-analyzer-* (CERT C checks)"
|
|
32
|
+
|
|
33
|
+
def analyze(self, files: list[Path], root: Path,
|
|
34
|
+
include_paths: list[str] | None = None) -> list[Finding]:
|
|
35
|
+
if not files:
|
|
36
|
+
return []
|
|
37
|
+
c_files = [f for f in files if f.suffix == ".c"]
|
|
38
|
+
if not c_files:
|
|
39
|
+
return []
|
|
40
|
+
# Without include dirs, clang-tidy can't find project headers ("file not
|
|
41
|
+
# found") and never parses the file at all — see BENCHMARKS.md.
|
|
42
|
+
include_flags = [f"-I{p}" for p in (include_paths or [])]
|
|
43
|
+
cmd = ["clang-tidy", "--quiet",
|
|
44
|
+
"--checks=-*,cert-*,bugprone-*,clang-analyzer-*"] \
|
|
45
|
+
+ [str(f) for f in c_files] + ["--", "-std=c11"] + include_flags
|
|
46
|
+
try:
|
|
47
|
+
proc = self._run(cmd, timeout=600)
|
|
48
|
+
except Exception:
|
|
49
|
+
return []
|
|
50
|
+
findings = []
|
|
51
|
+
for m in _DIAG.finditer(proc.stdout + "\n" + proc.stderr):
|
|
52
|
+
check = m.group("check")
|
|
53
|
+
cm = _CERT_CHECK.search(check)
|
|
54
|
+
fpath = m.group("file")
|
|
55
|
+
rel = relpath(fpath, root)
|
|
56
|
+
line = int(m.group("line"))
|
|
57
|
+
line_content, ctx_symbol = self._line_of(root, fpath, line)
|
|
58
|
+
if cm:
|
|
59
|
+
meta = REGISTRY.resolve(f"CERT {cm.group(1).upper()}-C") or {}
|
|
60
|
+
rid = meta.get("id", f"CERT {cm.group(1).upper()}-C")
|
|
61
|
+
findings.append(Finding(
|
|
62
|
+
rule_id=rid, standard="CERT-C",
|
|
63
|
+
severity=meta.get("severity", "critical"),
|
|
64
|
+
file=rel, line=line, column=int(m.group("col")),
|
|
65
|
+
message=m.group("msg"), analyzer=self.name,
|
|
66
|
+
line_content=line_content, context_symbol=ctx_symbol,
|
|
67
|
+
fix_hint=meta.get("fix", ""),
|
|
68
|
+
))
|
|
69
|
+
else:
|
|
70
|
+
findings.append(Finding(
|
|
71
|
+
rule_id=f"clang-tidy:{check}", standard="generic",
|
|
72
|
+
severity="major" if m.group("sev") == "warning" else "critical",
|
|
73
|
+
file=rel, line=line, column=int(m.group("col")),
|
|
74
|
+
message=m.group("msg"), analyzer=self.name,
|
|
75
|
+
line_content=line_content, context_symbol=ctx_symbol,
|
|
76
|
+
))
|
|
77
|
+
return findings
|
|
78
|
+
|
|
79
|
+
@staticmethod
|
|
80
|
+
def _line_of(root: Path, fpath: str, line: int) -> tuple[str, str]:
|
|
81
|
+
try:
|
|
82
|
+
p = Path(fpath)
|
|
83
|
+
if not p.is_absolute():
|
|
84
|
+
p = root / fpath
|
|
85
|
+
lines = p.read_text("utf-8", errors="replace").splitlines()
|
|
86
|
+
if 0 < line <= len(lines):
|
|
87
|
+
return lines[line - 1], enclosing_function(lines, line - 1)
|
|
88
|
+
return "", ""
|
|
89
|
+
except OSError:
|
|
90
|
+
return "", ""
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""cppcheck adapter.
|
|
2
|
+
|
|
3
|
+
Runs cppcheck with its MISRA addon (when the addon is available in the
|
|
4
|
+
cppcheck install) plus its general semantic checks, and normalizes output
|
|
5
|
+
into Findings. Semantic cppcheck IDs are mapped onto CERT C rules where a
|
|
6
|
+
faithful mapping exists; everything else is surfaced as supporting evidence
|
|
7
|
+
under a `cppcheck:<id>` rule so nothing is silently dropped.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
import xml.etree.ElementTree as ET
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from .base import Analyzer
|
|
17
|
+
from ..model import Finding, enclosing_function, relpath
|
|
18
|
+
from ..rules import REGISTRY
|
|
19
|
+
|
|
20
|
+
# cppcheck error id -> CERT rule
|
|
21
|
+
CPPCHECK_TO_CERT = {
|
|
22
|
+
"nullPointer": "EXP34-C", "nullPointerRedundantCheck": "EXP34-C",
|
|
23
|
+
"uninitvar": "EXP33-C", "uninitStructMember": "EXP33-C",
|
|
24
|
+
"arrayIndexOutOfBounds": "ARR30-C", "bufferAccessOutOfBounds": "ARR30-C",
|
|
25
|
+
"outOfBounds": "ARR30-C", "negativeIndex": "ARR30-C",
|
|
26
|
+
"doubleFree": "MEM31-C", "deallocuse": "MEM30-C", "useAfterFree": "MEM30-C",
|
|
27
|
+
"memleak": "MEM31-C", "mismatchAllocDealloc": "MEM34-C", "autovarInvalidDeallocation": "MEM34-C",
|
|
28
|
+
"zerodiv": "INT33-C", "zerodivcond": "INT33-C",
|
|
29
|
+
"integerOverflow": "INT32-C", "signConversion": "INT31-C", "truncLongCastAssignment": "INT31-C",
|
|
30
|
+
"returnDanglingLifetime": "DCL30-C", "danglingLifetime": "DCL30-C", "returnTempAddress": "DCL30-C",
|
|
31
|
+
"ignoredReturnValue": "ERR33-C", "leakReturnValNotUsed": "ERR33-C",
|
|
32
|
+
"invalidscanf": "STR31-C", "bufferNotZeroTerminated": "STR32-C",
|
|
33
|
+
"va_start_wrongParameter": "MSC39-C",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
_SEV_MAP = {"error": "critical", "warning": "major", "style": "minor",
|
|
37
|
+
"performance": "minor", "portability": "minor", "information": "info"}
|
|
38
|
+
|
|
39
|
+
_MISRA_ID = re.compile(r"misra-c2012-(\d+\.\d+)")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class CppcheckAnalyzer(Analyzer):
|
|
43
|
+
name = "cppcheck"
|
|
44
|
+
requires = "cppcheck"
|
|
45
|
+
options = "--addon=misra --enable=all (MISRA C:2012 addon)"
|
|
46
|
+
|
|
47
|
+
def analyze(self, files: list[Path], root: Path,
|
|
48
|
+
include_paths: list[str] | None = None) -> list[Finding]:
|
|
49
|
+
if not files:
|
|
50
|
+
return []
|
|
51
|
+
# Without the project's own include dirs, cppcheck can't see headers like
|
|
52
|
+
# FreeRTOSConfig.h, so it misreports "undefined identifier"/"unresolved
|
|
53
|
+
# declaration" on macros and functions that are perfectly visible to a
|
|
54
|
+
# real build — see BENCHMARKS.md for the measured false-positive cost.
|
|
55
|
+
include_flags = [f"-I{p}" for p in (include_paths or [])]
|
|
56
|
+
cmd = ["cppcheck", "--enable=all", "--inline-suppr", "--xml",
|
|
57
|
+
"--suppress=missingIncludeSystem", "--suppress=unusedFunction",
|
|
58
|
+
"--suppress=checkersReport", "--addon=misra"] \
|
|
59
|
+
+ include_flags + [str(f) for f in files]
|
|
60
|
+
try:
|
|
61
|
+
proc = self._run(cmd)
|
|
62
|
+
except Exception:
|
|
63
|
+
return []
|
|
64
|
+
xml_text = proc.stderr
|
|
65
|
+
if "<results" not in xml_text:
|
|
66
|
+
# addon may be missing; retry without it
|
|
67
|
+
cmd = [c for c in cmd if not c.startswith("--addon")]
|
|
68
|
+
try:
|
|
69
|
+
proc = self._run(cmd)
|
|
70
|
+
xml_text = proc.stderr
|
|
71
|
+
except Exception:
|
|
72
|
+
return []
|
|
73
|
+
return self._parse(xml_text, root)
|
|
74
|
+
|
|
75
|
+
def _parse(self, xml_text: str, root: Path) -> list[Finding]:
|
|
76
|
+
findings: list[Finding] = []
|
|
77
|
+
try:
|
|
78
|
+
tree = ET.fromstring(xml_text)
|
|
79
|
+
except ET.ParseError:
|
|
80
|
+
return findings
|
|
81
|
+
for err in tree.iter("error"):
|
|
82
|
+
eid = err.get("id", "")
|
|
83
|
+
msg = err.get("msg", "") or err.get("verbose", "")
|
|
84
|
+
sev = _SEV_MAP.get(err.get("severity", "warning"), "major")
|
|
85
|
+
loc = err.find("location")
|
|
86
|
+
if loc is None:
|
|
87
|
+
continue
|
|
88
|
+
fpath = loc.get("file", "")
|
|
89
|
+
line = int(loc.get("line", "0") or 0)
|
|
90
|
+
col = int(loc.get("column", "0") or 0)
|
|
91
|
+
rel = relpath(fpath, root)
|
|
92
|
+
line_content, ctx_symbol = self._line_of(root, fpath, line)
|
|
93
|
+
|
|
94
|
+
m = _MISRA_ID.match(eid)
|
|
95
|
+
if m:
|
|
96
|
+
meta = REGISTRY.resolve(f"MISRA {m.group(1)}") or {}
|
|
97
|
+
rid = meta.get("id", f"MISRA-C:2012 Rule {m.group(1)}")
|
|
98
|
+
summary = meta.get("summary", "")
|
|
99
|
+
if not msg or msg.startswith("misra violation"):
|
|
100
|
+
msg = summary or f"MISRA C:2012 Rule {m.group(1)} violation"
|
|
101
|
+
findings.append(Finding(
|
|
102
|
+
rule_id=rid, standard="MISRA-C:2012",
|
|
103
|
+
severity=meta.get("severity", sev), file=rel, line=line, column=col,
|
|
104
|
+
message=msg or summary, analyzer=self.name,
|
|
105
|
+
line_content=line_content, context_symbol=ctx_symbol, fix_hint=meta.get("fix", ""),
|
|
106
|
+
cross_refs=REGISTRY.cross_refs(rid) if meta else [],
|
|
107
|
+
))
|
|
108
|
+
elif eid in CPPCHECK_TO_CERT:
|
|
109
|
+
meta = REGISTRY.resolve(f"CERT {CPPCHECK_TO_CERT[eid]}") or {}
|
|
110
|
+
rid = meta.get("id", f"CERT {CPPCHECK_TO_CERT[eid]}")
|
|
111
|
+
findings.append(Finding(
|
|
112
|
+
rule_id=rid, standard="CERT-C",
|
|
113
|
+
severity=meta.get("severity", sev), file=rel, line=line, column=col,
|
|
114
|
+
message=msg, analyzer=self.name, line_content=line_content,
|
|
115
|
+
context_symbol=ctx_symbol, fix_hint=meta.get("fix", ""), cross_refs=REGISTRY.cross_refs(rid) if meta else [],
|
|
116
|
+
))
|
|
117
|
+
elif err.get("severity") in ("error", "warning"):
|
|
118
|
+
findings.append(Finding(
|
|
119
|
+
rule_id=f"cppcheck:{eid}", standard="generic", severity=sev,
|
|
120
|
+
file=rel, line=line, column=col, message=msg,
|
|
121
|
+
analyzer=self.name, line_content=line_content, context_symbol=ctx_symbol,
|
|
122
|
+
))
|
|
123
|
+
return findings
|
|
124
|
+
|
|
125
|
+
@staticmethod
|
|
126
|
+
def _line_of(root: Path, fpath: str, line: int) -> tuple[str, str]:
|
|
127
|
+
try:
|
|
128
|
+
p = Path(fpath)
|
|
129
|
+
if not p.is_absolute():
|
|
130
|
+
p = root / fpath
|
|
131
|
+
lines = p.read_text("utf-8", errors="replace").splitlines()
|
|
132
|
+
if 0 < line <= len(lines):
|
|
133
|
+
return lines[line - 1], enclosing_function(lines, line - 1)
|
|
134
|
+
return "", ""
|
|
135
|
+
except OSError:
|
|
136
|
+
return "", ""
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""Native analyzer: zero-dependency lexical + light structural checks.
|
|
2
|
+
|
|
3
|
+
This guarantees Maisha produces useful findings even on machines without
|
|
4
|
+
cppcheck or clang-tidy. It runs on a comment/string-stripped view of each file
|
|
5
|
+
so patterns never fire inside literals, and reports against the *original*
|
|
6
|
+
source line for correct fingerprints.
|
|
7
|
+
|
|
8
|
+
These are deliberately high-precision checks (banned functions/headers,
|
|
9
|
+
mechanical style rules). Deep semantic analysis (dataflow, lifetimes) is
|
|
10
|
+
delegated to the cppcheck/clang-tidy adapters when present.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import re
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
from .base import Analyzer
|
|
19
|
+
from ..model import Finding, enclosing_function, relpath
|
|
20
|
+
from ..rules import REGISTRY
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def strip_comments_strings(src: str) -> str:
|
|
24
|
+
"""Replace comment/string contents with spaces, preserving line structure
|
|
25
|
+
and length so (line, col) positions remain valid."""
|
|
26
|
+
out = []
|
|
27
|
+
i, n = 0, len(src)
|
|
28
|
+
mode = "code" # code | line | block | str | chr
|
|
29
|
+
while i < n:
|
|
30
|
+
c = src[i]
|
|
31
|
+
nxt = src[i + 1] if i + 1 < n else ""
|
|
32
|
+
if mode == "code":
|
|
33
|
+
if c == "/" and nxt == "/":
|
|
34
|
+
mode = "line"; out.append(" "); i += 2; continue
|
|
35
|
+
if c == "/" and nxt == "*":
|
|
36
|
+
mode = "block"; out.append(" "); i += 2; continue
|
|
37
|
+
if c == '"':
|
|
38
|
+
mode = "str"; out.append('"'); i += 1; continue
|
|
39
|
+
if c == "'":
|
|
40
|
+
mode = "chr"; out.append("'"); i += 1; continue
|
|
41
|
+
out.append(c); i += 1
|
|
42
|
+
elif mode == "line":
|
|
43
|
+
if c == "\n":
|
|
44
|
+
mode = "code"; out.append(c)
|
|
45
|
+
else:
|
|
46
|
+
out.append(" ")
|
|
47
|
+
i += 1
|
|
48
|
+
elif mode == "block":
|
|
49
|
+
if c == "*" and nxt == "/":
|
|
50
|
+
mode = "code"; out.append(" "); i += 2; continue
|
|
51
|
+
out.append(c if c == "\n" else " "); i += 1
|
|
52
|
+
elif mode in ("str", "chr"):
|
|
53
|
+
quote = '"' if mode == "str" else "'"
|
|
54
|
+
if c == "\\":
|
|
55
|
+
out.append(" "); i += 2; continue
|
|
56
|
+
if c == quote:
|
|
57
|
+
mode = "code"; out.append(quote); i += 1; continue
|
|
58
|
+
out.append(c if c == "\n" else " "); i += 1
|
|
59
|
+
return "".join(out)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
BANNED_CALLS = {
|
|
63
|
+
# func -> (rule query, extra message)
|
|
64
|
+
"malloc": ("MISRA 21.3", "dynamic allocation"), "calloc": ("MISRA 21.3", "dynamic allocation"),
|
|
65
|
+
"realloc": ("MISRA 21.3", "dynamic allocation"), "free": ("MISRA 21.3", "dynamic allocation"),
|
|
66
|
+
"setjmp": ("MISRA 21.4", "non-local jump"), "longjmp": ("MISRA 21.4", "non-local jump"),
|
|
67
|
+
"signal": ("MISRA 21.5", "signal handling"), "raise": ("MISRA 21.5", "signal handling"),
|
|
68
|
+
"printf": ("MISRA 21.6", "stdio in production"),"fprintf": ("MISRA 21.6", "stdio in production"),
|
|
69
|
+
"scanf": ("MISRA 21.6", "stdio in production"),"fscanf": ("MISRA 21.6", "stdio in production"),
|
|
70
|
+
"puts": ("MISRA 21.6", "stdio in production"),
|
|
71
|
+
"atoi": ("CERT ERR34-C", "no error detection possible"),
|
|
72
|
+
"atol": ("CERT ERR34-C", "no error detection possible"),
|
|
73
|
+
"atof": ("CERT ERR34-C", "no error detection possible"),
|
|
74
|
+
"abort": ("MISRA 21.8", "termination function"), "exit": ("MISRA 21.8", "termination function"),
|
|
75
|
+
"getenv": ("MISRA 21.8", "environment access"),
|
|
76
|
+
"system": ("CERT ENV33-C", "command processor invocation"),
|
|
77
|
+
"qsort": ("MISRA 21.9", "library sort (possibly recursive)"),
|
|
78
|
+
"bsearch": ("MISRA 21.9", "library search"),
|
|
79
|
+
"gets": ("CERT STR31-C", "unbounded read, removed from C11"),
|
|
80
|
+
"strcpy": ("CERT STR31-C", "unbounded copy"), "strcat": ("CERT STR31-C", "unbounded concat"),
|
|
81
|
+
"sprintf": ("CERT STR31-C", "unbounded format"),
|
|
82
|
+
"strtok": ("CERT CON33-C", "static internal state"),
|
|
83
|
+
"rand": ("CERT MSC32-C", "weak PRNG"),
|
|
84
|
+
"asctime": ("CERT MSC33-C", "fixed static buffer"),
|
|
85
|
+
"vsprintf": ("CERT STR31-C", "unbounded format"),
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_CALL_RE = re.compile(r"\b(" + "|".join(BANNED_CALLS) + r")\s*\(")
|
|
89
|
+
_OCTAL_RE = re.compile(r"(?<![\w.])0[0-7]+(?![\dxXbB.eEuUlL'])")
|
|
90
|
+
_LSUFFIX_RE = re.compile(r"\b\d+[uU]?l\b")
|
|
91
|
+
_ASSIGN_COND_RE = re.compile(r"\b(if|while)\s*\(([^()]|\([^()]*\))*[^=!<>+\-*/%&|^]=(?!=)")
|
|
92
|
+
_NOBRACE_RE = re.compile(r"^\s*(if|else\s+if|for|while)\s*\(.*\)\s*[^{;\s].*;\s*$")
|
|
93
|
+
_ELSE_NOBRACE_RE = re.compile(r"^\s*else\s+(?!if\b)(?!\{)[^{;\s].*;\s*$")
|
|
94
|
+
_CTRL_HDR_RE = re.compile(r"^\s*(if|else\s+if|for|while)\s*\(.*\)\s*$")
|
|
95
|
+
_ELSE_HDR_RE = re.compile(r"^\s*else\s*$")
|
|
96
|
+
_GOTO_RE = re.compile(r"\bgoto\s+\w+\s*;")
|
|
97
|
+
_UNION_RE = re.compile(r"\bunion\b\s*(\w+)?\s*\{")
|
|
98
|
+
_UNDEF_RE = re.compile(r"^\s*#\s*undef\b")
|
|
99
|
+
_TRIGRAPH_RE = re.compile(r"\?\?[=/'()!<>\-]")
|
|
100
|
+
_VLA_RE = re.compile(r"\b(?:int|char|float|double|long|short|uint\d+_t|int\d+_t|size_t)\s+\w+\s*\[\s*([a-zA-Z_]\w*)\s*\]")
|
|
101
|
+
_MACRO_CONST_RE = re.compile(r"^[A-Z_][A-Z0-9_]*$")
|
|
102
|
+
_FLOAT_EQ_RE = re.compile(r"\b(float|double)\b")
|
|
103
|
+
_FUNC_DEF_RE = re.compile(r"^\s*(?:static\s+|inline\s+|extern\s+)*[A-Za-z_][\w\s\*]*?\b([A-Za-z_]\w*)\s*\([^;{]*\)\s*\{?\s*$")
|
|
104
|
+
_BASIC_TYPE_DECL_RE = re.compile(r"^\s*(?:static\s+|const\s+|volatile\s+|extern\s+)*(?:unsigned\s+|signed\s+)?(int|short|long)\b(?!\s*\()")
|
|
105
|
+
_COMMENTED_CODE_RE = re.compile(r"(;|\{|\}|\breturn\b|=)")
|
|
106
|
+
_PREPROC_RE = re.compile(r"^\s*#")
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _next_significant(lines: list[str], start_idx: int) -> str | None:
|
|
110
|
+
"""First line at/after start_idx (0-based) that isn't blank or a
|
|
111
|
+
preprocessor directive. A #if/#ifdef/#else/#endif conditional-compilation
|
|
112
|
+
block sitting between a control header and its body is not a missing
|
|
113
|
+
brace — it's the body wrapped in a compile-time choice, and the real
|
|
114
|
+
brace is on the far side of it (see BENCHMARKS.md: 16/16 confirmed cases
|
|
115
|
+
of this exact pattern in FreeRTOS)."""
|
|
116
|
+
j = start_idx
|
|
117
|
+
while j < len(lines) and (not lines[j].strip() or _PREPROC_RE.match(lines[j])):
|
|
118
|
+
j += 1
|
|
119
|
+
return lines[j] if j < len(lines) else None
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class NativeAnalyzer(Analyzer):
|
|
123
|
+
name = "native"
|
|
124
|
+
requires = None
|
|
125
|
+
options = "zero-dependency lexical checks (MISRA/BARR-C/CERT subset)"
|
|
126
|
+
|
|
127
|
+
def version(self) -> str:
|
|
128
|
+
from .. import __version__
|
|
129
|
+
return f"maishac {__version__}"
|
|
130
|
+
|
|
131
|
+
def analyze(self, files: list[Path], root: Path,
|
|
132
|
+
include_paths: list[str] | None = None) -> list[Finding]:
|
|
133
|
+
# lexical checks need no compilation model, so include paths don't apply here
|
|
134
|
+
findings: list[Finding] = []
|
|
135
|
+
for f in files:
|
|
136
|
+
try:
|
|
137
|
+
src = f.read_text("utf-8", errors="replace")
|
|
138
|
+
except OSError:
|
|
139
|
+
continue
|
|
140
|
+
findings.extend(self._analyze_file(f, src, root))
|
|
141
|
+
return findings
|
|
142
|
+
|
|
143
|
+
def analyze_source(self, code: str, filename: str, root: Path) -> list[Finding]:
|
|
144
|
+
"""Analyze an in-memory source string (never touches disk) — the entry
|
|
145
|
+
point for proactively linting a draft before it is written to a file."""
|
|
146
|
+
return self._analyze_file(root / filename, code, root)
|
|
147
|
+
|
|
148
|
+
# ---------------------------------------------------------------- helpers
|
|
149
|
+
def _mk(self, rule_query: str, f: Path, root: Path, lineno: int,
|
|
150
|
+
lines: list[str], msg: str) -> Finding | None:
|
|
151
|
+
meta = REGISTRY.resolve(rule_query)
|
|
152
|
+
if not meta:
|
|
153
|
+
return None
|
|
154
|
+
return Finding(
|
|
155
|
+
rule_id=meta["id"], standard=meta["standard"], severity=meta["severity"],
|
|
156
|
+
file=relpath(f, root), line=lineno, message=msg, analyzer=self.name,
|
|
157
|
+
line_content=lines[lineno - 1] if 0 < lineno <= len(lines) else "",
|
|
158
|
+
context_symbol=enclosing_function(lines, lineno - 1),
|
|
159
|
+
cross_refs=REGISTRY.cross_refs(meta["id"]),
|
|
160
|
+
fix_hint=meta.get("fix", ""),
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
def _analyze_file(self, f: Path, src: str, root: Path) -> list[Finding]:
|
|
164
|
+
out: list[Finding] = []
|
|
165
|
+
raw_lines = src.splitlines()
|
|
166
|
+
clean = strip_comments_strings(src)
|
|
167
|
+
clean_lines = clean.splitlines()
|
|
168
|
+
add = lambda rule, ln, msg: out.append(x) if (x := self._mk(rule, f, root, ln, raw_lines, msg)) else None
|
|
169
|
+
|
|
170
|
+
func_stack: list[list] = [] # [name, depth_at_open], current function is func_stack[-1]
|
|
171
|
+
pending_func_name: str | None = None
|
|
172
|
+
switch_stack: list[tuple[int, int, bool]] = [] # (start_line, depth_at_open, saw_default)
|
|
173
|
+
depth = 0
|
|
174
|
+
|
|
175
|
+
for i, line in enumerate(clean_lines, start=1):
|
|
176
|
+
raw = raw_lines[i - 1] if i <= len(raw_lines) else ""
|
|
177
|
+
|
|
178
|
+
for m in _CALL_RE.finditer(line):
|
|
179
|
+
fn = m.group(1)
|
|
180
|
+
rule, why = BANNED_CALLS[fn]
|
|
181
|
+
add(rule, i, f"call to banned function '{fn}' ({why})")
|
|
182
|
+
if _OCTAL_RE.search(line):
|
|
183
|
+
add("MISRA 7.1", i, "octal integer constant")
|
|
184
|
+
if _LSUFFIX_RE.search(line):
|
|
185
|
+
add("MISRA 7.3", i, "lowercase 'l' literal suffix is easily misread as '1'")
|
|
186
|
+
if _ASSIGN_COND_RE.search(line):
|
|
187
|
+
add("MISRA 13.4", i, "assignment used inside a condition expression")
|
|
188
|
+
if _NOBRACE_RE.match(line) or _ELSE_NOBRACE_RE.match(line):
|
|
189
|
+
add("MISRA 15.6", i, "control statement body is not a compound (braced) block")
|
|
190
|
+
# header on one line, braceless body on the next — skip over any
|
|
191
|
+
# #if/#else/#endif between the header and its real body first.
|
|
192
|
+
if _CTRL_HDR_RE.match(line) or _ELSE_HDR_RE.match(line):
|
|
193
|
+
nxt = _next_significant(clean_lines, i) # clean_lines[i] is the line after `line` (0-based)
|
|
194
|
+
if nxt is not None and not nxt.lstrip().startswith("{") \
|
|
195
|
+
and not _CTRL_HDR_RE.match(nxt) and not _ELSE_HDR_RE.match(nxt):
|
|
196
|
+
add("MISRA 15.6", i,
|
|
197
|
+
"control statement body is not a compound (braced) block")
|
|
198
|
+
if _GOTO_RE.search(line):
|
|
199
|
+
add("MISRA 15.1", i, "goto statement")
|
|
200
|
+
if _UNION_RE.search(line):
|
|
201
|
+
add("MISRA 19.2", i, "union declaration")
|
|
202
|
+
if _UNDEF_RE.match(line):
|
|
203
|
+
add("MISRA 20.5", i, "#undef directive")
|
|
204
|
+
if _TRIGRAPH_RE.search(raw):
|
|
205
|
+
add("MISRA 4.2", i, "trigraph sequence")
|
|
206
|
+
vla_m = _VLA_RE.search(line)
|
|
207
|
+
# An ALL_CAPS array-size identifier is, by nearly universal C
|
|
208
|
+
# convention, a #define'd compile-time constant, not a runtime
|
|
209
|
+
# variable — without preprocessing, this heuristic can't resolve
|
|
210
|
+
# the macro's value, but it can at least stop mistaking
|
|
211
|
+
# "uint8_t buf[BUF_SIZE]" (a fixed array) for a VLA (see
|
|
212
|
+
# BENCHMARK-SUITE-REPORT.md: confirmed false positive, was firing
|
|
213
|
+
# on every macro-sized array in a struct).
|
|
214
|
+
if vla_m and "(" not in line.split("[")[0] and not _MACRO_CONST_RE.match(vla_m.group(1)):
|
|
215
|
+
add("MISRA 18.8", i, "possible variable-length array")
|
|
216
|
+
if _BASIC_TYPE_DECL_RE.match(line) and "main" not in line:
|
|
217
|
+
add("MISRA Dir 4.6", i, "basic numeric type instead of fixed-width <stdint.h> type")
|
|
218
|
+
if len(raw) > 80:
|
|
219
|
+
add("BARR 3.1a", i, f"line is {len(raw)} characters (limit 80)")
|
|
220
|
+
if "\t" in raw:
|
|
221
|
+
add("BARR 3.2a", i, "tab character used for indentation/alignment")
|
|
222
|
+
|
|
223
|
+
# commented-out code heuristic (uses raw line, since clean has comments blanked)
|
|
224
|
+
stripped = raw.strip()
|
|
225
|
+
if stripped.startswith("//") and _COMMENTED_CODE_RE.search(stripped[2:]) \
|
|
226
|
+
and len(stripped) > 8 and not stripped[2:].strip().startswith(("!", "TODO", "NOTE", "FIXME")):
|
|
227
|
+
add("MISRA Dir 4.4", i, "possible commented-out code")
|
|
228
|
+
|
|
229
|
+
# recursion (direct): function calls itself. Checked against only
|
|
230
|
+
# the CURRENT enclosing function (tracked incrementally via brace
|
|
231
|
+
# depth below) rather than every function name seen so far in the
|
|
232
|
+
# file — the previous every-name-times-every-line approach was
|
|
233
|
+
# O(functions x lines), which made large files (thousands of
|
|
234
|
+
# functions) unusably slow (see BENCHMARK-SUITE-REPORT.md).
|
|
235
|
+
if func_stack:
|
|
236
|
+
cur_name = func_stack[-1][0]
|
|
237
|
+
if re.search(rf"(?<![\w.]){re.escape(cur_name)}\s*\(", line) and not _FUNC_DEF_RE.match(line):
|
|
238
|
+
add("MISRA 17.2", i, f"direct recursion: '{cur_name}' calls itself")
|
|
239
|
+
|
|
240
|
+
fm = _FUNC_DEF_RE.match(line)
|
|
241
|
+
if fm and not line.strip().startswith(("if", "for", "while", "switch", "return", "else")):
|
|
242
|
+
pending_func_name = fm.group(1)
|
|
243
|
+
|
|
244
|
+
# float equality: crude but effective — == or != on a line mentioning float vars is
|
|
245
|
+
# too noisy; instead flag literal float comparisons like `x == 0.1`
|
|
246
|
+
if re.search(r"[!=]=\s*-?\d+\.\d+[fF]?\b", line) or re.search(r"-?\d+\.\d+[fF]?\s*[!=]=", line):
|
|
247
|
+
add("CERT FLP37-C", i, "floating-point equality comparison")
|
|
248
|
+
|
|
249
|
+
# switch/default tracking
|
|
250
|
+
depth += line.count("{") - line.count("}")
|
|
251
|
+
if pending_func_name is not None and "{" in line:
|
|
252
|
+
func_stack.append([pending_func_name, depth])
|
|
253
|
+
pending_func_name = None
|
|
254
|
+
while func_stack and func_stack[-1][1] > depth:
|
|
255
|
+
func_stack.pop()
|
|
256
|
+
if re.search(r"\bswitch\s*\(", line):
|
|
257
|
+
switch_stack.append([i, depth, False]) # type: ignore[list-item]
|
|
258
|
+
if switch_stack and re.search(r"\bdefault\s*:", line):
|
|
259
|
+
switch_stack[-1][2] = True # type: ignore[index]
|
|
260
|
+
while switch_stack and depth < switch_stack[-1][1]:
|
|
261
|
+
start, _, saw_default = switch_stack.pop()
|
|
262
|
+
if not saw_default:
|
|
263
|
+
add("MISRA 16.4", start, "switch statement has no default label")
|
|
264
|
+
|
|
265
|
+
for start, _, saw_default in switch_stack: # unterminated at EOF
|
|
266
|
+
if not saw_default:
|
|
267
|
+
add("MISRA 16.4", start, "switch statement has no default label")
|
|
268
|
+
|
|
269
|
+
return [f for f in out if f is not None]
|