Skip to content

Commit 5597097

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 llvm#34876.
1 parent 015526b commit 5597097

File tree

5 files changed

+25
-55
lines changed

5 files changed

+25
-55
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ New Compiler Flags
450450
that relies on it. Users should carefully consider this possibiilty when using
451451
the flag.
452452

453+
- For the ARM target, added ``-Warm-interrupt-vfp-clobber`` that will emit a
454+
diagnostic when an interrupt handler is declared and VFP is enabled.
455+
453456
Deprecated Compiler Flags
454457
-------------------------
455458

@@ -492,6 +495,13 @@ Modified Compiler Flags
492495
now include dianostics about C++26 features that are not present in older
493496
versions.
494497

498+
- Removed the "arm interrupt calling convention" warning that was included in
499+
``-Wextra`` without its own flag. This warning suggested adding
500+
``__attribute__((interrupt))`` to functions that are called from interrupt
501+
handlers to prevent clobbering VFP registers. Following this suggestion leads
502+
to unpredictable behavior by causing multiple exception returns from one
503+
exception. Fixes #GH34876.
504+
495505
Removed Compiler Flags
496506
-------------------------
497507

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,10 @@ 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+
13321336
D->addAttr(::new (getASTContext())
13331337
ARMInterruptAttr(getASTContext(), AL, Kind));
13341338
}

clang/lib/Sema/SemaExpr.cpp

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

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