Skip to content
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

More coccinelle fixes #24606

Merged
merged 3 commits into from Mar 30, 2023
Merged

More coccinelle fixes #24606

merged 3 commits into from Mar 30, 2023

Conversation

qmonnet
Copy link
Member

@qmonnet qmonnet commented Mar 28, 2023

  • bpf: Add semi-colons after __maybe_unused label attributes
  • bpf: Add some missing const qualifiers
  • cocci: Set COCCINELLE_HOME to fix path for isomorphisms and macros files

Please look at commit descriptions for details.

cocci: Work around a bug in coccinelle to better check files, add a few missing `const` qualifiers to BPF code

@qmonnet qmonnet added area/CI Continuous Integration testing issue or flake sig/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages. release-note/misc This PR makes changes that have no direct user impact. labels Mar 28, 2023
@qmonnet qmonnet requested review from a team as code owners March 28, 2023 12:51
@qmonnet qmonnet marked this pull request as draft March 28, 2023 13:05
@qmonnet
Copy link
Member Author

qmonnet commented Mar 28, 2023

CI failing because some of those consts are not valid (cilium_dbg() expects not const).

Copy link
Member

@kaworu kaworu left a comment

Choose a reason for hiding this comment

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

s/Good new,/Good news,/ in the first commit message, s/"__attribute__((unused))/"__attribute__((unused))"/ in the second commit message, other than that and the comment about the make variable that LGTM.

bpf/Makefile.bpf Outdated Show resolved Hide resolved
bpf/Makefile.bpf Outdated Show resolved Hide resolved
GCC and clang support label attributes, such as __attribute__((unused))
to indicate that under certain configurations, (goto) labels will not be
used at compilation time. We've been using this to silence warnings
about unused labels.

However, GCC's "Attribute Syntax" documentation [0] states that:

    GNU C++ only permits attributes on labels if the attribute specifier
    is immediately followed by a semicolon (i.e., the label applies to
    an empty statement). If the semicolon is missing, C++ label
    attributes are ambiguous, as it is permissible for a declaration,
    which could begin with an attribute list, to be labelled in C++.

We're using clang to compile the BPF programs and I couldn't find an
equivalent statement, but it should likely be the same. Let's add the
semi-colons.

The real motivation for this clean-up is to coccinelle parse the files
properly and to better apply checks. We've seen it struggle when the
semi-colons are missing.

[0] https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html#Label-Attributes-2

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
@qmonnet
Copy link
Member Author

qmonnet commented Mar 29, 2023

So I added a new commit at the beginning, to fix a parsing issue for coccinelle that was messing up with the reports for the const qualifiers added in the following commit.

I also switched to $COCCINELLE_HOME for setting the correct path to pick up the resources. But given that it's an environment variable and no longer a spatch option, I had to pass it to the container in the workflow file instead of bpf/Makefile.bpf where no container is involved.

All missing qualifiers detected by coccinelle.

The missing "const" on the "mac" argument in icmp6.h was found by
running the latest coccinelle (not 1.1.1 as in CI and Docker image) on
the bpf code base.

The missing qualifier in proxy.h was not detected so far because
coccinelle stumbles on parsing macro CTX_REDIRECT_FN in proxy.h, in
particular because of the CT_TUPLE_TYPE argument where we pass a
two-word long type (struct ipv(4|)_ct_tuple). The spatch tool would fail
with:

    EXN: Failure("bpf/lib/proxy.h: 164: try to delete an expanded token: struct") in bpf/lib/proxy.h

and then would fail to detect the missing "const". By commenting out
temporarily the calls to CTX_REDIRECT_FN we make it work again. Good
news, by adding the missing "const" to "CT_TUPLE_TYPE", we're also
fixing the parsing for coccinelle, so future mistakes in the file should
be reported as expected.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Trying to create a new coccinelle check, we realised that some patterns
that we expected to match in the bpf/ were in fact never caught.

Here is a minimal reproducer.

foo.cocci:

    @@
    @@
    (
    - foo = bar;
    + foo = baz;
    )

foo.c (not working):

    int main (void)
    {
    	int foo, bar, baz = 0, coccibreaker __maybe_unused;

    	foo = bar;
    	return foo;
    }

Invocation:

    $ docker run --rm --user 1000 \
        --workdir /workspace -v `pwd`:/workspace -it
        docker.io/cilium/coccicheck:2.4@sha256:24abe3fbb8e829fa41a68a3b76cb4df84fd5a87a7d1d6254c1c1fe5effb5bd1b \
        spatch --include-headers --very-quiet --in-place \
        --sp-file contrib/coccinelle/foo.cocci bpf/foo.c

This does not catch the "foo = bar;" statement in the C file.
However, the pattern is caught in the following file instead:

foo.c (working):

    int main (void)
    {
    	int foo, bar, baz = 0;
    	int coccifine __maybe_unused;

    	foo = bar;
    	return foo;
    }

The __maybe_unused attribute seems to prevent spatch to parse correctly
the function. This happens independently of whether __maybe_unused is
defined in the C code. The behaviour remains the same if we replace
__maybe_unused with some other random word (although replacing it with
"__attribute__((unused))" works as expected).

Running from coccinelle's "master" branch works fine, and when running
spatch locally (no Docker image) we can also observe the following
warnings:

    warning: Can't find macro file: /usr/local/bin/lib/coccinelle/standard.h
    warning: Can't find default iso file: /usr/local/bin/lib/coccinelle/standard.iso

This error was fixed with commit coccinelle/coccinelle@540888ff426e
("Fix 263: wrong default path for COCCINELLE_HOME") [0], which is not
part of a release yet.

Until it is, we still want to have our code covered by coccinelle. As a
workaround to the issue, we could pass manually the paths to the files
containing the definitions for the isomorphisms and built-in macros for
coccinelle, with spatch options "--macro-file-builtin" and "--iso-file".
Even simpler, we can set $(COCCINELLE_HOME) to the correct path so that
both files are found.

No camels were harmed during this investigation.

[0] https://gitlab.inria.fr/coccinelle/coccinelle/-/commit/540888ff426e

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
@qmonnet qmonnet marked this pull request as ready for review March 29, 2023 09:38
@qmonnet qmonnet requested review from a team as code owners March 29, 2023 09:38
@qmonnet
Copy link
Member Author

qmonnet commented Mar 29, 2023

/test

Copy link
Member

@pchaigno pchaigno left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the very clear commit messages!

@pchaigno
Copy link
Member

In terms of tests, I don't think we need anything other than coccicheck and build datapath to pass. The first should cover the coccicheck changes and the second should be enough to cover the small changes to the C code. Marking ready to merge.

@pchaigno pchaigno added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Mar 29, 2023
@julianwiedmann
Copy link
Member

There's an outstanding change request by @kaworu. Please either dismiss it, or give him the time to resolve it.

Please also triage the test-runtime fail.

@julianwiedmann julianwiedmann removed the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Mar 29, 2023
Copy link
Member

@kaworu kaworu left a comment

Choose a reason for hiding this comment

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

Thanks @qmonnet! TIL about semi-colons after __maybe_unused for goto labels, but that make sense.

@qmonnet
Copy link
Member Author

qmonnet commented Mar 29, 2023

/test-runtime

@qmonnet
Copy link
Member Author

qmonnet commented Mar 29, 2023

Review is in and I filed #24633 for the failed test, which looks like a new flake.

@ciliumbot
Copy link

Build finished.

@qmonnet
Copy link
Member Author

qmonnet commented Mar 29, 2023

runtime test succeeded, but instead of GitHub picking up the result, we have ciliumbot commenting 🤔

Anyway, this time we should be good to go.

@qmonnet qmonnet added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Mar 29, 2023
@borkmann borkmann merged commit 89dd226 into cilium:master Mar 30, 2023
42 checks passed
@qmonnet qmonnet deleted the pr/coccicheck-home-path branch March 30, 2023 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/CI Continuous Integration testing issue or flake ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact. sig/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants