[llvm-bugs] [Bug 41645] New: [ThinLTO] externally visible linkonce_odr object is incorrectly internalized

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Apr 29 01:55:47 PDT 2019


            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: unassignedbugs at nondot.org
          Reporter: eleviant at accesssoftek.com
                CC: htmldeveloper at gmail.com, llvm-bugs at 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() {
  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

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190429/404848fa/attachment.html>

More information about the llvm-bugs mailing list