https://bugs.llvm.org/show_bug.cgi?id=41645

            Bug ID: 41645
           Summary: [ThinLTO] externally visible linkonce_odr object is
                    incorrectly internalized
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedb...@nondot.org
          Reporter: elevi...@accesssoftek.com
                CC: htmldevelo...@gmail.com, llvm-bugs@lists.llvm.org

Symbol with linkonce_odr linkage whose prevailing definition is in regular
object file can be incorrectly internalized by ThinLTO.

Steps to reproduce:
1) Create following C++ source files (struct.h, main.cpp, init.cpp)

// File: struct.h
template <class T>
struct Singleton {
  static __attribute__((noinline)) T& getInstance() throw() {
    static T instance;
    return instance;
  }
};

struct S : public Singleton<S> {
  long a = 0;
};

static inline S* getS() {
  S& os = S::getInstance();
  return os.a ? &os : nullptr;
}

// File: main.cpp
#include "struct.h"

void init();

int main() {
  init();
  return getS()->a;
}

// File: init.cpp
#include "struct.h"

void init() {
  S::getInstance().a = 1;
}

2) Compile init.cpp to regular object

clang -c init.cpp

3) Compile main.cpp in thin LTO mode

clang -c -flto=thin main.cpp

4) Link program. It is important that init.o goes before main.o in command line
params

clang -flto=thin -fuse-ld=lld init.o main.o 

5) Run a.out. Program will crash because getS() returns zero in main function.

This happens because static instance variable is incorrectly internalized by
thin LTO, so finally init and main functions access two different copies of it.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to