Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Miscompilation: clang generates a weak symbol for a generic lambda that depends on a lambda in a static function #43338

Open Quuxplusone opened 4 years ago

Quuxplusone commented 4 years ago
Bugzilla Link PR44368
Status NEW
Importance P normal
Reported by Philippe Daouadi (blastrock@free.fr)
Reported on 2019-12-23 06:13:07 -0800
Last modified on 2020-01-30 06:18:09 -0800
Version trunk
Hardware PC Linux
CC dblaikie@gmail.com, htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments proj.tar.gz (495 bytes, application/gzip)
Blocks
Blocked by
See also
Created attachment 22961
complete project that shows the bug

Here is a code snippet that shows the bug:

    template <typename T> auto async() {
      return [](auto func) {
        [func] { func(); }();
      };
    }
    static void f() {
      async<int>()([] { });
    }
    void f1() { f(); }

Compile with:

    clang++ -c ll.cpp

Run objdump -t ll.o -C, and you'll see the following symbol:

    0000000000000000  w    F .text._ZZZ5asyncIiEDavENKUlT_E_clIZL1fvE3$_0EEDaS0_ENKUlvE_clEv    000000000000001b async<int>()::{lambda(auto:1)#1}::operator()<f()::$_0>(f()::$_0) const::{lambda()#1}::operator()() const

This is the call operator of the inner lambda on line 3 and this symbol is
*weak*, even though it depends on the func argument, which is a lambda from a
static function. I think this symbol should be *local* as a *weak* symbol might
be overridden by another translation unit.

The consequence is that if there are two static functions that have the same
name in two different translation units and that they both define a lambda,
calling async with that lambda will generate only one symbol in the final
binary and that usually leads to a crash in a more complex example.

I am attaching a more complete project which shows the miscompilation issue.
The program should print 1 and 2, but it prints 1 and 1 for the reason
described above. Renaming the static function in one of the translation units
fixes the issue.

GCC 9 compiles the program correctly, produces *local* symbols for that call
operator and the program prints 1 and 2.
Quuxplusone commented 4 years ago

Attached proj.tar.gz (495 bytes, application/gzip): complete project that shows the bug

Quuxplusone commented 4 years ago

Here's an attempt at fixing the bug: https://reviews.llvm.org/D73701