From 751bee9c5b05c6d08e58a5d1c6ed70ec10dede8e Mon Sep 17 00:00:00 2001 From: Jonathan Roelofs Date: Wed, 18 Jan 2017 15:31:11 +0000 Subject: [PATCH] Warn when calling a non interrupt function from an interrupt on ARM The idea for this originated from a really tricky bug: ISRs on ARM don't automatically save off the VFP regs, so if say, memcpy gets interrupted and the ISR itself calls memcpy, the regs are left clobbered when the ISR is done. https://reviews.llvm.org/D28820 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@292375 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 3 +++ lib/Sema/SemaExpr.cpp | 9 +++++++++ test/Sema/arm-interrupt-attr.c | 11 +++++++++++ 3 files changed, 23 insertions(+) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 03789d59dd..e0a5073483 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -259,6 +259,9 @@ def err_anyx86_interrupt_attribute : Error< "a pointer as the first parameter|a %2 type as the second parameter}1">; def err_anyx86_interrupt_called : Error< "interrupt service routine cannot be called directly">; +def warn_arm_interrupt_calling_convention : Warning< + "call to function without interrupt attribute could clobber interruptee's VFP registers">, + InGroup; def warn_mips_interrupt_attribute : Warning< "MIPS 'interrupt' attribute only applies to functions that have " "%select{no parameters|a 'void' return type}0">, diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 7b404f40a7..7d72b7ede8 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -5395,6 +5395,15 @@ Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, return ExprError(); } + // Interrupt handlers don't save off the VFP regs automatically on ARM, + // so there's some risk when calling out to non-interrupt handler functions + // that the callee might not preserve them. This is easy to diagnose here, + // but can be very challenging to debug. + if (auto *Caller = getCurFunctionDecl()) + if (Caller->hasAttr()) + if (!FDecl->hasAttr()) + Diag(Fn->getExprLoc(), diag::warn_arm_interrupt_calling_convention); + // Promote the function operand. // We special-case function promotion here because we only allow promoting // builtin functions to function pointers in the callee of a call. diff --git a/test/Sema/arm-interrupt-attr.c b/test/Sema/arm-interrupt-attr.c index b9684f0b46..cd67278d94 100644 --- a/test/Sema/arm-interrupt-attr.c +++ b/test/Sema/arm-interrupt-attr.c @@ -17,3 +17,14 @@ __attribute__((interrupt("UNDEF"))) void foo7() {} __attribute__((interrupt)) void foo8() {} __attribute__((interrupt())) void foo9() {} __attribute__((interrupt(""))) void foo10() {} + +void callee1(); +__attribute__((interrupt("IRQ"))) void callee2(); +void caller1() { + callee1(); + callee2(); +} +__attribute__((interrupt("IRQ"))) void caller2() { + callee1(); // expected-warning {{call to function without interrupt attribute could clobber interruptee's VFP registers}} + callee2(); +} -- 2.40.0