llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang should warn when building with `-fexperimental-bounds-safety` and `-x assembler-with-cpp`

Open delcypher opened this issue 3 years ago • 8 comments

In ~~#70480~~ #92623 the -fexperimental-bounds-safety flag was added.

Currently Clang will emit a warning that -fexperimental-bounds-safety is ignored when building with -x assembler. However, it won't emit a warning when building with -x assembler-with-cpp.

This issue tracks adding a warning to detect when -fexperimental-bounds-safety is used with -x assembler-with-cpp.

We can either have a generic warning whenever this combination of flags are used. Or we could have a more targeted warning:

E.g.

#if __has_feature(bounds_safety) // Should warn here
mov x0, #42
#else
#error "oh no"
#endif

rdar://117917700

delcypher avatar Nov 03 '23 17:11 delcypher

We should have a targeted warning and in general follow how options are normally handled with -x assembler-with-cpp by default, in order to not introduce disruption for project builds that pass cflags to assembly.

rapidsna avatar Nov 03 '23 17:11 rapidsna

And this targeted warning should be disabled when we actually support the feature for assembler-with-cpp to allow #if __has_feature(bounds_safety).

rapidsna avatar Nov 03 '23 17:11 rapidsna

Hi, I'm interested in the GSoC project on upstreaming -fbounds-safety and wanted to start with a small fix. I looked into this issue and was able to reproduce it:

  $ ./build/bin/clang -Xclang -fexperimental-bounds-safety -x assembler -c test.S                                                                         
  clang: warning: argument unused during compilation: '-Xclang -fexperimental-bounds-safety'                                                                                
                                                                                                                                                                            
  $ ./build/bin/clang -Xclang -fexperimental-bounds-safety -x assembler-with-cpp -c test.S
  (no warning) and "oh no" error comes through

(The flag is CC1 only now since #70480 was closed and #92623 added it as CC1, so I used -Xclang to pass it through.)

For the targeted warning that @rapidsna suggested (warn when __has_feature(bounds_safety) is evaluated in assembler-with-cpp, instead of a blanket "argument unused" on every .S file), I think __has_feature(bounds_safety) would need to be wired up first. Right now there's no entry in Features.def so it returns false even in C mode with the flag on.

Would it make sense to first add bounds_safety to Features.def, then add the targeted warning in the preprocessor for assembler-with-cpp? Am I on the right track?

Happy to hear if there's a better first issue to work on too.

abhijeetsharma200 avatar Feb 12 '26 19:02 abhijeetsharma200

@abhijeetsharma200

Thanks for looking at this and sorry for slow reply. It's been a while since I've thought about this and I think there are more important issues that need attention. Seeing as you mentioned support __has_feature(bounds_safety) why don't you try to tackle that because its a lot more important. I've filed #182948 to track it.

delcypher avatar Feb 23 '26 22:02 delcypher

Thanks, will take a look!

abhijeetsharma200 avatar Feb 23 '26 22:02 abhijeetsharma200

For this particular issue

  • I don't think my original suggestion of a more targeted warning makes sense. In assembly you'd probably only ever write #if __has_feature(bounds_safety) if you wanted to write some kind assembly that's specific to -fbounds-safety's ABI changes and in that case you wouldn't want to see the warning.
  • The simpler thing about warning when combining -x assembler-with-cpp with -fexperimental-bounds-safety makes more sense.
  • There doesn't seem to much precedent for doing this because if I try with something like -fsanitize=address I don't get a warning with -x assembler-with-cpp but do with -x assembler. So we are already matching the existing behavior for another type of instrumentation in Clang.

Given the above I think this makes this issue low priority. We should probably fix it at some point but there are many other things that need attention.

delcypher avatar Feb 23 '26 22:02 delcypher

Sorry, its a bit late for me right now. I will probably have to wait till tomorrow to have any meaningful convo right now. I will try to have a PR for the issue you opened in the next couple of days. Thanks for the details though. Much appreciated!

abhijeetsharma200 avatar Feb 23 '26 22:02 abhijeetsharma200

  • don't think my original suggestion of a more targeted warning makes sense. In assembly you'd probably only ever write #if __has_feature(bounds_safety) if you wanted to write some kind assembly that's specific to -fbounds-safety's ABI changes and in that case you wouldn't want to see the warning.

Makes sense to not have a warning here.

  • The simpler thing about warning when combining -x assembler-with-cpp with -fexperimental-bounds-safety makes more sense.

Along the same lines as the first point, one possible approach instead of the full blanket would be (-x assembler-with-cpp) AND (-fexperimental-bounds-safety) AND (code does NOT call __has_feature(bounds_safety)). Though I am not sure of the feasibility of this

  • There doesn't seem to much precedent for doing this because if I try with something like -fsanitize=address I don't get a warning with -x assembler-with-cpp but do with -x assembler. So we are already matching the existing behavior for another type of instrumentation in Clang.

Given the above I think this makes this issue low priority. We should probably fix it at some point but there are many other things that need attention.

Makes sense. Thanks for the context and the -fsanitize=address comparison.

Quick procedural question on the “fix it at some point” part: since this behavior already matches -fsanitize=address, what would “fixing” look like in practice?

Is the expectation that we eventually introduce a more general policy for flags in assembler-with-cpp mode (and potentially update -fsanitize=address and any similar flags for consistency), or would it be acceptable to special-case bounds-safety only, even if other instrumentation flags keep the current behavior? I’m trying to understand whether this is meant to be a broader consistency change across flags, or just a bounds-safety-specific cleanup. Also, just want to learn how LLVM goes about decisions like this: grouping it by flag vs type of fix.

Also, on prioritization: what bounds-safety tasks do you see as higher priority right now? Is there anything I can work on?

abhijeetsharma200 avatar Feb 24 '26 06:02 abhijeetsharma200