Skip to content

Commit 9475269

Browse files
committed
[clang][ARM] Fix warning for using VFP from interrupts.
This warning has three issues: - The interrupt attribute causes the function to return using an exception return instruction. This warning allows calls from one function with the interrupt attribute to another, and the diagnostic text suggests that not having the attribute on the callee is a problem. Actually making such a call will lead to a double exception return, which is unpredictable according to the ARM architecture manual section B9.1.1, "Restrictions on exception return instructions". Even on machines where an exception return from user/system mode is tolerated, if the callee's interrupt type is anything other than a supervisor call or secure monitor call, it will also return to a different address than a normal function would. For example, returning from an "IRQ" handler will return to lr - 4, which will generally result in calling the same function again. - The interrupt attribute currently does not cause caller-saved VFP registers to be saved and restored if they are used, so putting __attribute__((interrupt)) on a called function doesn't prevent it from clobbering VFP state. - It is part of the -Wextra diagnostic group and can't be individually disabled when using -Wextra, which also means the diagnostic text of this specific warning appears in the documentation of -Wextra. This change addresses all three issues by instead generating a warning for any interrupt handler where the vfp feature is enabled. The warning is also given its own diagnostic group. Closes #34876.
1 parent 87d58ab commit 9475269

File tree

5 files changed

+26
-55
lines changed

5 files changed

+26
-55
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ New Compiler Flags
442442
- ``-Wc++2c-compat`` group was added to help migrating existing codebases
443443
to upcoming C++26.
444444

445+
- For the ARM target, added ``-Warm-interrupt-vfp-clobber`` that will emit a
446+
diagnostic when an interrupt handler is declared and VFP is enabled.
447+
445448
Deprecated Compiler Flags
446449
-------------------------
447450

@@ -484,6 +487,13 @@ Modified Compiler Flags
484487
now include dianostics about C++26 features that are not present in older
485488
versions.
486489

490+
- Removed the "arm interrupt calling convention" warning that was included in
491+
``-Wextra`` without its own flag. This warning suggested adding
492+
``__attribute__((interrupt))`` to functions that are called from interrupt
493+
handlers to prevent clobbering VFP registers. Following this suggestion leads
494+
to unpredictable behavior by causing multiple exception returns from one
495+
exception. Fixes #GH34876.
496+
487497
Removed Compiler Flags
488498
-------------------------
489499

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,10 @@ def warn_anyx86_excessive_regsave : Warning<
336336
" with attribute 'no_caller_saved_registers'"
337337
" or be compiled with '-mgeneral-regs-only'">,
338338
InGroup<DiagGroup<"excessive-regsave">>;
339-
def warn_arm_interrupt_calling_convention : Warning<
340-
"call to function without interrupt attribute could clobber interruptee's VFP registers">,
341-
InGroup<Extra>;
339+
def warn_arm_interrupt_vfp_clobber : Warning<
340+
"interrupt service routine with vfp enabled may clobber the "
341+
"interruptee's vfp state">,
342+
InGroup<DiagGroup<"arm-interrupt-vfp-clobber">>;
342343
def warn_interrupt_attribute_invalid : Warning<
343344
"%select{MIPS|MSP430|RISC-V}0 'interrupt' attribute only applies to "
344345
"functions that have %select{no parameters|a 'void' return type}1">,

clang/lib/Sema/SemaARM.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,11 @@ void SemaARM::handleInterruptAttr(Decl *D, const ParsedAttr &AL) {
13291329
return;
13301330
}
13311331

1332+
const TargetInfo &TI = getASTContext().getTargetInfo();
1333+
if (TI.hasFeature("vfp")) {
1334+
Diag(D->getLocation(), diag::warn_arm_interrupt_vfp_clobber);
1335+
}
1336+
13321337
D->addAttr(::new (getASTContext())
13331338
ARMInterruptAttr(getASTContext(), AL, Kind));
13341339
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6631,22 +6631,10 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl,
66316631
return ExprError();
66326632
}
66336633

6634-
// Interrupt handlers don't save off the VFP regs automatically on ARM,
6635-
// so there's some risk when calling out to non-interrupt handler functions
6636-
// that the callee might not preserve them. This is easy to diagnose here,
6637-
// but can be very challenging to debug.
6638-
// Likewise, X86 interrupt handlers may only call routines with attribute
6634+
// X86 interrupt handlers may only call routines with attribute
66396635
// no_caller_saved_registers since there is no efficient way to
66406636
// save and restore the non-GPR state.
66416637
if (auto *Caller = getCurFunctionDecl()) {
6642-
if (Caller->hasAttr<ARMInterruptAttr>()) {
6643-
bool VFP = Context.getTargetInfo().hasFeature("vfp");
6644-
if (VFP && (!FDecl || !FDecl->hasAttr<ARMInterruptAttr>())) {
6645-
Diag(Fn->getExprLoc(), diag::warn_arm_interrupt_calling_convention);
6646-
if (FDecl)
6647-
Diag(FDecl->getLocation(), diag::note_callee_decl) << FDecl;
6648-
}
6649-
}
66506638
if (Caller->hasAttr<AnyX86InterruptAttr>() ||
66516639
Caller->hasAttr<AnyX86NoCallerSavedRegistersAttr>()) {
66526640
const TargetInfo &TI = Context.getTargetInfo();

clang/test/Sema/arm-interrupt-attr.c

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,19 @@
1-
// RUN: %clang_cc1 %s -triple arm-apple-darwin -target-feature +vfp2 -verify -fsyntax-only
2-
// RUN: %clang_cc1 %s -triple thumb-apple-darwin -target-feature +vfp3 -verify -fsyntax-only
3-
// RUN: %clang_cc1 %s -triple armeb-none-eabi -target-feature +vfp4 -verify -fsyntax-only
4-
// RUN: %clang_cc1 %s -triple thumbeb-none-eabi -target-feature +neon -verify -fsyntax-only
5-
// RUN: %clang_cc1 %s -triple thumbeb-none-eabi -target-feature +neon -target-feature +soft-float -DSOFT -verify -fsyntax-only
1+
// RUN: %clang_cc1 %s -triple arm-none-eabi -DSOFT -verify -fsyntax-only
2+
// RUN: %clang_cc1 %s -triple arm-none-eabi -target-feature +vfp2 -verify -fsyntax-only
63

7-
__attribute__((interrupt(IRQ))) void foo(void) {} // expected-error {{'interrupt' attribute requires a string}}
8-
__attribute__((interrupt("irq"))) void foo1(void) {} // expected-warning {{'interrupt' attribute argument not supported: irq}}
94

5+
#ifdef SOFT
6+
__attribute__((interrupt("irq"))) void foo1(void) {} // expected-warning {{'interrupt' attribute argument not supported: irq}}
7+
__attribute__((interrupt(IRQ))) void foo(void) {} // expected-error {{'interrupt' attribute requires a string}}
108
__attribute__((interrupt("IRQ", 1))) void foo2(void) {} // expected-error {{'interrupt' attribute takes no more than 1 argument}}
11-
129
__attribute__((interrupt("IRQ"))) void foo3(void) {}
1310
__attribute__((interrupt("FIQ"))) void foo4(void) {}
1411
__attribute__((interrupt("SWI"))) void foo5(void) {}
1512
__attribute__((interrupt("ABORT"))) void foo6(void) {}
1613
__attribute__((interrupt("UNDEF"))) void foo7(void) {}
17-
1814
__attribute__((interrupt)) void foo8(void) {}
1915
__attribute__((interrupt())) void foo9(void) {}
2016
__attribute__((interrupt(""))) void foo10(void) {}
21-
22-
#ifndef SOFT
23-
// expected-note@+2 {{'callee1' declared here}}
24-
#endif
25-
void callee1(void);
26-
__attribute__((interrupt("IRQ"))) void callee2(void);
27-
void caller1(void) {
28-
callee1();
29-
callee2();
30-
}
31-
32-
#ifndef SOFT
33-
__attribute__((interrupt("IRQ"))) void caller2(void) {
34-
callee1(); // expected-warning {{call to function without interrupt attribute could clobber interruptee's VFP registers}}
35-
callee2();
36-
}
37-
38-
void (*callee3)(void);
39-
__attribute__((interrupt("IRQ"))) void caller3(void) {
40-
callee3(); // expected-warning {{call to function without interrupt attribute could clobber interruptee's VFP registers}}
41-
}
4217
#else
43-
__attribute__((interrupt("IRQ"))) void caller2(void) {
44-
callee1();
45-
callee2();
46-
}
47-
48-
void (*callee3)(void);
49-
__attribute__((interrupt("IRQ"))) void caller3(void) {
50-
callee3();
51-
}
18+
__attribute__((interrupt("IRQ"))) void float_irq(void); // expected-warning {{interrupt service routine with vfp enabled may clobber the interruptee's vfp state}}
5219
#endif

0 commit comments

Comments
 (0)