[llvm-bugs] [Bug 52229] New: CNTTP materialisation exported even for unnamed-namespace content

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 19 14:11:34 PDT 2021


            Bug ID: 52229
           Summary: CNTTP materialisation exported even for
                    unnamed-namespace content
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: thiago at kde.org
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

When a non-type class template parameter is materialised because its address is
taken, clang emits it as weak but always global. That content does not obey
either -fvisibility=hidden nor the fact that the type may be in an unnamed
namespace. The latter produces broken code.

$ cat test1.cpp
#include <stdio.h>

[[gnu::noinline]] void dump(const void *data, size_t len)
    const char *ptr = reinterpret_cast<const char *>(data);
    const char *end = ptr + len;
    printf("%p(%zd) = ", data, len);
    for ( ; ptr != end; ++ptr)
        printf("%02x", *ptr);

namespace { struct Foo { short i; }; }
template <Foo f> void test1()
    dump(&f, sizeof(f));
    printf("expected: %d\n", f.i);

void test2();
int main()

$ cat test2.cpp
#include <stdio.h>

void dump(const void *data, size_t len);

namespace { struct Foo { int Foo::* i; }; }
template <Foo f> void test2()
    dump(&f, sizeof(f));
    printf("expected: %td\n", f.i);

void test2()

$ g++ -O2 -std=c++20 test1.cpp test2.cpp  
$ ./a.out                               
0x402022(2) = 0000
expected: 0
0x402038(8) = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
expected: -1
$ clang -O2 -std=c++20 test1.cpp test2.cpp
tjmaciei at tjmaciei-mobl5 /tmp $ ./a.out                                    
0x402028(2) = 0000
expected: 0
0x402028(8) = 0000657870656374
expected: -1

The assembly for test2.cpp ends in:
        .type   _ZTAXtlN12_GLOBAL__N_13FooEEE, at object #
.rodata._ZTAXtlN12_GLOBAL__N_13FooEEE,"aG", at progbits,_ZTAXtlN12_GLOBAL__N_13FooEEE,comdat
        .weak   _ZTAXtlN12_GLOBAL__N_13FooEEE
        .p2align        3
        .quad   -1                              # 0xffffffffffffffff

The _ZTA symbol is .weak but exported. Therefore, the <unnamed
namespace>::Foo{} expression in it matches the other Foo expression, thus
making the two symbols get merged by the linker, even though they don't have
the same size or contents.

$ clang --version
clang version 13.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

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/20211019/244b22ab/attachment-0001.html>

More information about the llvm-bugs mailing list