Skip to content

fno-semantic-interposition breaks code relying on address uniqueness of a function #58295

Open
@aelovikov-intel

Description

@aelovikov-intel
// a.cpp
#include <functional>
#include <cassert>
int foo(int x) { return x + 42; }

bool bar(std::function<int(int)> F) {
  auto f = F.target<int(*)(int)>();
  return f && *f == foo;
}
// b.cpp
#include <functional>
int foo(int x);

bool bar(std::function<int(int)> F);

int main() {
  return bar(foo);
}
$ /usr/bin/clang++ -std=c++17 -fPIC -fno-semantic-interposition a.cpp -shared -o liba.so
$ /usr/bin/clang++ -std=c++17 b.cpp -L. -la
$ LD_LIBRARY_PATH=. ./a.out ; echo $?
0
$ /usr/bin/clang++ -std=c++17 -fPIC a.cpp -shared -o liba.so
$ LD_LIBRARY_PATH=. ./a.out ; echo $?
1

GCC is consistent with/without the option.

I'm not sure if that is a bug or desired behavior, but https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100483#c2 suggests that the case above doesn't use semantic interposition and so should work with the option. I'm not unlikely to misread/misunderstand it though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions