Re: [PATCH] kcov: add __no_sanitize_coverage to fix noinstr for all architectures

From: Marco Elver
Date: Tue May 25 2021 - 15:13:16 EST


On Tue, 25 May 2021 at 20:25, Miguel Ojeda
<miguel.ojeda.sandonis@xxxxxxxxx> wrote:
> On Tue, May 25, 2021 at 7:59 PM Marco Elver <elver@xxxxxxxxxx> wrote:
> >
> > +#if defined(CONFIG_KCOV) && CONFIG_CLANG_VERSION >= 130000
>
> Is there any reason why Clang does not implement
> `__has_attribute(__no_sanitize_coverage__)` like GCC? That way we can
> merge both (perhaps even in `compiler_attributes.h`).

It's complicated. Clang implements all no_sanitize options via
no_sanitize(<string_literal>), except for 3 which are there for
backwards-compatibility reasons (no_sanitize_{address,memory,thread}).
But otherwise, no_sanitize_sanitizer is deprecated in Clang in favor
of no_sanitize("sanitizer") per comment at
https://github.com/llvm/llvm-project/blob/main/clang/include/clang/Basic/Attr.td#L2907.
(That being said, there's already inconsistency due to coverage
instrumentation requiring "-fsanitize-coverage=..." and not
"-fsanitize=coverage-...". The implementation vs other no_sanitize is
also a bit special, see LLVM commit.)

This means we only have __has_attribute(no_sanitize). Which is also
the reason why the other __no_sanitize_* defines in compiler-clang.h
first check the feature, as feature existence implies attribute
existence. But, sadly, this is not the case for coverage
instrumentation (where in fact, __has_feature(coverage_sanitizer)
doesn't work either...)

>From a UX perspective, having Clang only give us no_sanitize("...")
without the corresponding __has_attribute() support is not great, but
passable due to __has_feature() working for other sanitizers. From
Clang's perspective, it kept things simpler because we've gotten quite
a number of sanitizers recently. The big ones are manageable [1], but
UBSan is just too much [2].
[1] https://clang.llvm.org/docs/UsersManual.html#controlling-code-generation
[2] https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#ubsan-checks

This is probably a longer answer to your question, but is a summary of
the frustrations I encountered as I looked deeper into Clang's
no_sanitize attribute.

Long story short: this is not fixable without more Clang changes. The
only way to do it without a version check would be to introduce
no_sanitize_coverage attr to Clang, which we probably shouldn't do,
and I didn't want to fight it. ;-)

Thanks,
-- Marco