Repository navigation
[refactor] Rework analyzer labeling - #5127
Conversation
dc342dd to
57bad77
Compare
barnabasdomozi
left a comment
There was a problem hiding this comment.
Please fix the failing test cases.
89eeb14 to
d309a0b
Compare
There was a problem hiding this comment.
Please mention all the breaking changes in the commit message.
As an example:
CodeChecker analyze ./compile_commands.json -e sei-cert-c:str34-c -o ./report_dir
worked before this patch but now it throws an error:
The sei-cert-c namespace is not known. Please selectone of these existing namespace options: checker, prefix, label-tool-skip, severity, guideline, doc_url, blacklist, rule, profile, description.
Additionally, update docs/analyzer/user_guide.md which still references the old way of working.
It would also be useful to add documentation about how label, guideline and rule files relate to each other, and also what each file contains.
| return dict(self.__rule_details.get( | ||
| rule_id, {"title": "", "rule_url": ""})) | ||
|
|
||
| def guideline_titles(self) -> dict[str, str]: |
There was a problem hiding this comment.
It seems this guideline_titles function is not used anywhere. Why was it added?
There was a problem hiding this comment.
It has to do something with my git, I've accidentally left in functions and parts that are not needed. Thanks for the heads up, removed them.
| return {name: meta.get("title", "") | ||
| for name, meta in self.__guideline_meta.items()} | ||
|
|
||
| def guideline_urls(self) -> dict[str, str]: |
There was a problem hiding this comment.
This guideline_urls function is also not used anywhere. Why was it implemented?
There was a problem hiding this comment.
Same issue as with guideline_titles.
| for name, meta in self.__guideline_meta.items()} | ||
|
|
||
| return guideline_rules | ||
| def all_guidelines(self) -> Iterable[str]: |
There was a problem hiding this comment.
If this function returns a list, why does it have a return type of Iterable? Several places where this function is called, the return value is converted to a list anyway e.g.:
analyzer/codechecker_analyzer/cli/checkers.py, line 206codechecker_common/checker_labels.pyline 409
Seems like unnecessary type conversions to me.
There was a problem hiding this comment.
Good point.
| """ | ||
| guideline_files = map( | ||
| lambda f: os.path.join(guidelines_dir, f), | ||
| filter(lambda f: f.endswith('.yaml') or f.endswith('.yml'), |
There was a problem hiding this comment.
All YAML files in the repository have extension .yaml. I think we should have a standard file extension for guidelines and not allow ones that have a .yml extension for some odd reason.
There was a problem hiding this comment.
I think there is no difference between the two extensions. If we agree on using one over the other, we can make it, so it only accepts that.
| if rule_id in self.__rule_details: | ||
| return dict(self.__rule_details[rule_id]) | ||
|
|
||
| return { |
There was a problem hiding this comment.
Using a raw dictionary when returning structured data is prone to errors and harder to reason about. Consider using a dataclass instead.
| Return the rules of a guideline as a mapping from rule id to its | ||
| details (title, url). | ||
| """ | ||
| return self.__all_rules[guideline_name] |
There was a problem hiding this comment.
This function does not check whether guideline_name is in the dictionary. Since self.__all_rules is a defaultdict, non-existing keys could be inserted to it.
This is especially concerning because the server calls this from getGuidelineRules, therefore, clients can insert arbitrary keys to this dictionary using the Thrift API.
My suggestion is to NOT use defaultdict for self.__all_rules to avoid bugs like this in the future.
| guideline_files: Iterable[str] | ||
| ) -> defaultdict[str, dict[str, dict[str, str]]]: | ||
| guidelines_dir: str | ||
| ) -> tuple[defaultdict[str, dict[str, dict[str, str]]], |
There was a problem hiding this comment.
Please use typed dataclasses as I mentioned before for structured data.
The return type actually shows how hard this is to follow right now: tuple[defaultdict[str, dict[str, dict[str, str]]], dict[str, dict[str, str]]]
| return args.guideline | ||
| # "<guideline>:<rule>" form -> query by the rule only, since the guideline | ||
| # is derived from the rule in memory. | ||
| _, rule = args.guideline.split(':', 1) |
There was a problem hiding this comment.
To me, dropping the guideline part means two things:
- we are NOT checking whether the specified guideline exists
- we are NOT checking if the rule is actually part of the guideline
Is that intentional?
There was a problem hiding this comment.
It was not intentional; I forgot to add the checks to check either the guideline existence or if the rule actually in the given one.
6f00765 to
6b6fc34
Compare
barnabasdomozi
left a comment
There was a problem hiding this comment.
Unfortunately, it seems there are still backward compatibility issues:
-
checkers --label <guideline:rule>not working
Example:
CodeChecker checkers --label sei-cert-c:str34-cIn previous version:
bugprone-signed-char-misuse Status: enabled Analyzer: clang-tidy ...Now:
[INFO 2026-10-26 16:40:26] - No checkers with the given label found. -
checkers --detailsmissing<guideline:rule>labels and also some profiles
Example:
CodeChecker checkers --details | grep -FA 25 "unix.cstring.NullArg"Previously:
unix.cstring.NullArg Status: enabled Analyzer: clangsa Description: Check for null pointers being passed as arguments to C string functions Labels: cwe-top-25-2024:cwe-476 cwe-top-25-2025:cwe-476 cwe-vulnerabilities:cwe-476 doc_url:https://clang.llvm.org/docs/analyzer/checkers.html#unix-cstring-nullarg-c guideline:cwe-top-25-2024 guideline:cwe-top-25-2025 guideline:cwe-vulnerabilities guideline:memory-safety guideline:sei-cert-c guideline:sei-cert-cpp memory-safety:cwe-476 profile:default profile:extreme profile:security profile:sensitive sei-cert-c:exp34-c sei-cert-cpp:exp34-c severity:HIGHNow:
unix.cstring.NullArg Status: enabled Analyzer: clangsa Description: Check for null pointers being passed as arguments to C string functions Labels: doc_url:https://clang.llvm.org/docs/analyzer/checkers.html#unix-cstring-nullarg-c guideline:cwe-top-25-2024 guideline:cwe-top-25-2025 guideline:cwe-vulnerabilities guideline:memory-safety guideline:sei-cert-c guideline:sei-cert-cpp profile:default profile:security rule:cwe-476 rule:exp34-c severity:HIGHNow, it no longer lists labels:
sei-cert-c:exp34-csei-cert-cpp:exp34-cmemory-safety:cwe-476- etc.
profile:extremeprofile:sensitive
| url=rules[rule].rule_url, | ||
| checkers=checkers, | ||
| level=rules[rule].get("level", "") | ||
| level="" |
There was a problem hiding this comment.
Is there a reason why level is now always returned as an empty string?
There was a problem hiding this comment.
Level is a not used field currently for rules. The real question here is are we going to use it in the future? If not then the whole level field could be removed.
There was a problem hiding this comment.
It seems the level field is still used in web GUI: src/components/Statistics/Guideline/GuidelineStatistics.vue, lines 247 and 318 but I'm not sure why.
It can be worth a look why it is still used there.
There was a problem hiding this comment.
I'm not sure what the idea was behind this level parameter. How about this: I restore the mentioned line above to get the actual variable instead of setting it to an empty string and then we could go after it to check what the original intention was with it.
| # may belong to multiple guidelines. | ||
| guideline_rule_map: dict = {} | ||
| if all_guideline_names: | ||
| guideline_rules = client.getGuidelineRules([ |
There was a problem hiding this comment.
If the server now also sends the <guideline>:<rule> labels, modifying the client-side code should not be necessary. Moreover, this new implementation now has an extra API call: client.getGuidelineRules().
| # was not part of the released API. It is excluded here so the | ||
| # returned set stays byte-for-byte compatible with older clients; | ||
| # it is still used below to derive the legacy guideline labels. | ||
| checker_labels_set = {f'{key}:{value}' for key, value in pairs |
There was a problem hiding this comment.
I think these pieces of backward incompatible fixes should belong to the CheckerLabels class, not to the report server implementation.
| # getCheckerLabels keeps its output compatible with older clients: the | ||
| # internal "rule" labels are excluded, while the legacy transitive | ||
| # profile tiers and "<guideline>:<rule>" labels are reconstructed. | ||
| div_zero_labels = {f'{key}:{value}' for key, value in label_pairs |
There was a problem hiding this comment.
It seems this code is the copy of what is written in the report server class. In test files, the original implementation being tested should be imported and not copy pasted.
This also relates to my other point, that this code should be moved out from the report server class.
|
|
||
| return expanded | ||
|
|
||
| def __derived_guidelines( |
There was a problem hiding this comment.
It seems only __augment_labels calls this __derived_guidelines function.
I think these two could be merged, that would make the code more easier to read.
dkrupp
left a comment
There was a problem hiding this comment.
In general, this change looks very good.
I have just a few minor comments.
I cross-tested this PR with the 6.29.0 version by querying the checkers for the default, sensitive profiles, sei-cert-c guidelines and various label queries and all seem to return similar results. Also tested "CodeChecker analyze" with profile/guideline selection. Looks like that it selects the correct checkers.
| title: '' | ||
| rule_url: https://cmu-sei.github.io/secure-coding-standards/sei-cert-cpp-coding-standard/rules/miscellaneous-msc/msc53-cpp/ | ||
| - rule_id: msc54-cpp | ||
| title: A signal handler must be a plain old function |
There was a problem hiding this comment.
Please remove the title from sei-cert rules for consistency and for copyright reasons.
|
|
||
| # Collect the guidelines that appear for the queried checkers. A checker's | ||
| # guidelines are derived on the server from its "rule:<rule_id>" labels. | ||
| all_guideline_names = set() |
There was a problem hiding this comment.
Does the checker coverage command line query work wit the new server and old clients. In other words, is the change backward compatible with old clients?
There was a problem hiding this comment.
It is yet to be tested but since the output has not changed, it should be. I've tested the new server with old client (released 6.29.0) and it worked.
e48ac25 to
8b8d029
Compare
bruntib
left a comment
There was a problem hiding this comment.
Thanks for the improvement, it really makes the "database" of checker labels much more maintainable.
However, I have a few comments. Almost all of these comments want to express that the public interface of CheckerLabels class shouldn't change, and this should be the single source of all label information. I don't think, it's a good approach that implicit information is introduced, like the invisible "rule" label. The goal of the CheckerLabels class is that the client code doesn't have to have any extra knowledge about the implementation details of guideline handling. How should I decide whether I need to use labels_of_checker() or visible_labels_of_checker()?
| @dataclass | ||
| class Guideline: | ||
| """A named grouping of rules, mapping rule id to its Rule descriptor.""" | ||
| guideline_id: str |
There was a problem hiding this comment.
I would follow a naming convention where the member names don't contain the class name:
guideline = Guideline()
guideline.guideline_id
# vs
guideline.idThis comment applies to Rule.rule_id and Rule.rule_url, too.
There was a problem hiding this comment.
I can accept this, renamed them to only be id, url.
| guidelines = analyzer_context.get_context().guideline | ||
| if identifier_namespace in guidelines.all_guidelines(): | ||
| identifier_namespace = "rule" |
There was a problem hiding this comment.
I assume, this section is here because of --guideline <guideline>:<rule> flag. But how can this format reach this point? The --guideline flag should be transformed to the equivalent --label <guideline>:<rule> at an earlier stage.
|
|
||
| A guideline is derived in memory from the "rule:<rule_id>" labels of a | ||
| checker, so a whole-guideline query is expressed as a "guideline:<name>" | ||
| label and a single-rule query as a "rule:<rule_id>" label. |
There was a problem hiding this comment.
Earlier we didn't have a rule:<rule_id> label. Why do we introduce it instead of <guideline>:<rule_id>? I think, this PR shouldn't change the current behavior.
|
|
||
| return result | ||
|
|
||
| def profiles_containing(self, profile: str) -> list[str]: |
There was a problem hiding this comment.
Should be a private function.
Note: Neither this function nor __expand_query_profiles() handle transitivity of the "contains" relation. We need to remember that if a new profile is added that contains a previous profile, then its transitively contained profiles have to be added to the config file, too. Do we want to enforce it with a test case? Or should it be possible to add a new profile that contains "sensitive", but doesn't contain "default"?
There was a problem hiding this comment.
I'm open to create a test case. This current solution makes it easier in terms of code simplicity. And yes, it is also possible this way to add a profile which only contains parts of the current ones.
There was a problem hiding this comment.
Neither this function nor __expand_query_profiles() handle transitivity of the "contains" relation. We need to remember that if a new profile is added that contains a previous profile, then its transitively contained profiles have to be added to the config file, too.
I think we should prefer simpler implementation instead of resolving the transitivity in code.
Also, we rarely introduce new profiles and it's as easy as editing a few fields in description.json:
"profile-containment": {
"extreme": [
"sensitive",
"default"
],
"sensitive": [
"default"
]
}
So the current design looks fine to me.
|
|
||
| return expanded | ||
|
|
||
| def __augment_labels( |
There was a problem hiding this comment.
I still think that rule shouldn't be introduced as a key. If we have labels in the form <guideline>:<rule_id>, then this function is not necessary.
| """ | ||
| # Guidelines are derived from rules, so their values come from the | ||
| # Guidelines object rather than from raw labels. | ||
| if label == 'guideline' and self.__guidelines is not None: |
There was a problem hiding this comment.
self.__guidelines shouldn't be optional.
| rows = list(map(lambda x: (x,), cl.labels())) | ||
| # Present each guideline name as a label and hide the internal "rule" | ||
| # label, matching the released output. | ||
| guidelines = analyzer_context.get_context().guideline |
There was a problem hiding this comment.
I think, the CheckerLabels should be the single source of truth.
|
|
||
| rows = list(map(lambda x: (x,), cl.occurring_values(args.label))) | ||
| # A guideline name as the label lists its rule ids. | ||
| guidelines = analyzer_context.get_context().guideline |
There was a problem hiding this comment.
I think, the CheckerLabels should be the single source of truth.
| checker.checkerId, analyzer_name)))) | ||
| labels.append(sorted( | ||
| f'{key}:{value}' for key, value in | ||
| checker_labels.visible_labels_of_checker( |
There was a problem hiding this comment.
Unnecessary interface change in CheckerLabels.
| self.checker_labels_dir, guidelines) | ||
| div_zero_labels = { | ||
| f'{key}:{value}' for key, value in | ||
| local_checker_labels.visible_labels_of_checker( |
There was a problem hiding this comment.
Unnecessary interface change in CheckerLabels.
barnabasdomozi
left a comment
There was a problem hiding this comment.
Looks mostly fine to me, but I left a few comments.
Also, as mentioned before, it would nice to have some markdown documentation about the guideline, rule files, what they contain, how they relate to each other, that the profile containment is derived from descriptions.json etc.
73f6363 to
bd4fc39
Compare
barnabasdomozi
left a comment
There was a problem hiding this comment.
I left one comment regarding the level field.
barnabasdomozi
left a comment
There was a problem hiding this comment.
It seems some checkers are missing the rule:exp34-c label which was added by this commit: c771c5c1a
Regarding the level field, we should add a FIXME comment to the Rule dataclass that it's currently unused.
This change reworks how checker labels are working inside CodeChecker. The guideline files are now separated from the rules. Each type of rule (CWE, Sei-Cert, OWASP, etc.) have a separate file collecting all the rules the given type has. Every rule has its ID and optionally its title and URL. The guideline files are now only serves as a collection of rule IDs. Label files also got reworked: containing less data for any given checker, guideline definitions are discarded and now derived from the rule ID based on which guideline contains the given rule. Every rule is now added by `rule:<rule_name>`. Profiles are also reworked, the generic profiles (default, sensitive and extreme) are now contained in eachother meaning only the smallest set must be defined and the checker will automatically be in every broader set. E.g. if a checker must be in sensitive and extreme, only sensitive has to be defined, it will be in extreme automatically. This change was made to prevent user mistakes when editing labelling, guidelines or rules. Every component (rules, guidelines, labels) has its own source of truth now which makes easier the further development.
Rework the docs for labels to describe the actual state of the labels.
e6fb9f1 to
bcb1980
Compare
| else: | ||
| normalized.append(label) | ||
|
|
||
| return list(dict.fromkeys(normalized)) |
There was a problem hiding this comment.
What is the use of transforming normalized to dict keys and back to a list again?
Suggested changes have been implemented.
fixes CODECHKR-7068
This change reworks how checker labels are working inside CodeChecker.
The guideline files are now separated from the rules. Each type of rule (CWE, Sei-Cert, OWASP, etc.) have a separate file collecting all the rules the given type has. Every rule has its ID and optionally its title and URL. The guideline files are now only serves as a collection of rule IDs.
Label files also got reworked: containing less data for any given checker, guideline definitions are discarded and now derived from the rule ID based on which guideline contains the given rule. Every rule is now added by
rule:<rule_name>. Profiles are also reworked, the generic profiles (default, sensitive and extreme) are now contained in eachother meaning only the smallest set must be defined and the checker will automatically be in every broader set. E.g. if a checker must be in sensitive and extreme, only sensitive has to be defined, it will be in extreme automatically.This change was made to prevent user mistakes when editing labelling, guidelines or rules. Every component (rules, guidelines, labels) has its own source of truth now which makes easier the further development.