[LLVMbugs] [Bug 11315] New: Bug in clang with throwing exceptions with noreturn functions?

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Nov 4 16:56:51 PDT 2011


             Bug #: 11315
           Summary: Bug in clang with throwing exceptions with noreturn
           Product: clang
           Version: 2.9
          Platform: PC
        OS/Version: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: mamaich at pisem.net
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

The following program:
#include <stdio.h>

int main()
    try {
        throw "hello world";
    catch(const char *aaa)
        puts("unknown exception");
when compiled with GCC on MinGW outputs "hello world". While when compiled with
"clang++.exe prog.cpp" - it crashes with the error "terminate called after
throwing an instance of 'char const*'".
I've examined the code obtained with "llc -march=c prog.o" from a generated
LLVM code:

unsigned int main(void) {
  unsigned int llvm_cbe_tmp__1;    /* Address-exposed local */
  unsigned char *llvm_cbe_tmp__2;    /* Address-exposed local */
  unsigned char *llvm_cbe_aaa;    /* Address-exposed local */
  unsigned char *llvm_cbe_tmp__3;

  *(&llvm_cbe_tmp__1) = 0u;
  llvm_cbe_tmp__3 = __cxa_allocate_exception(4u);
  *(((unsigned char **)llvm_cbe_tmp__3)) = ((&_OC_str.array[((signed int
  __cxa_throw(llvm_cbe_tmp__3, ((unsigned char *)(&_ZTIPKc)), ((unsigned char

there is no "catch" block code at all.
LLVM code seems to be corect (i'm not familiar with it):
define i32 @main() {
  %1 = alloca i32, align 4
  %2 = alloca i8*
  %aaa = alloca i8*, align 4
  store i32 0, i32* %1
  %3 = call i8* @__cxa_allocate_exception(i32 4) nounwind
  %4 = bitcast i8* %3 to i8**
  store i8* getelementptr inbounds ([18 x i8]* @.str, i32 0, i32 0), i8** %4
  invoke void @__cxa_throw(i8* %3, i8* bitcast (i8** @_ZTIPKc to i8*), i8*
null) noreturn
          to label %22 unwind label %5

; <label>:5                                       ; preds = %0
  %6 = call i8* @llvm.eh.exception() nounwind
  store i8* %6, i8** %2
  %7 = call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* %6, i8* bitcast (i32
(...)* @__gxx_personality_v0 to i8*), i8* bitcast (i8** @_ZTIPKc to i8*), i8*
null) nounwind
  %8 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIPKc to i8*))
  %9 = icmp eq i32 %7, %8
  br i1 %9, label %11, label %10
... and so on ...

Probably compiler thinks that "__cxa_throw" has a noreturn flag and stops
generating further code in main() as it thinks that it would never be reached.
Though it is not true, as there are exception handlers after __cxa_throw that
may be executed. 
The same problem may arise also with other "noreturn" functions that can throw

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list