4

If I compile a simple program (sample.cpp):

#include <cstdio>

int main() {
  printf("Hello, World");
  return 0;
}

with a shared sanitizer library, i.e.

clang++-12 -fsanitize=address -shared-libsan sample.cpp -o sample

I am getting the following error when running ./sample:

./sample: error while loading shared libraries: libclang_rt.asan-x86_64.so: cannot open shared object file: No such file or directory

I am getting this error for the sample code on my local machine (Ubuntu 20.04 and clang-12), as well as our build runner (Ubuntu 18.04 and clang-10).

Am I missing something, or shall I submit a bug and to whom? (The options I see are Ubuntu or LLVM/Clang teams)

Please note that this question is distinct from the one that was suggested as duplicate in close votes (this was confirmed by the linked question author in comments).

yugr
  • 19,769
  • 3
  • 51
  • 96
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • 2
    -Wl,-rpath,/usr/lib/llvm-12/lib/clang/12.0.1/lib/linux (adapt the exact path) or use LD_LIBRARY_PATH. If you want to submit a bug, it should be to ubuntu. – Marc Glisse Jul 29 '21 at 07:01
  • 2
    The linked question doesn't have the answer to my question. I saw that question earlier and it discusses a different topic. – Serge Rogatch Jul 29 '21 at 07:10
  • 2
    Did you see the LD_PRELOAD part in the answer? – Marc Glisse Jul 29 '21 at 07:13
  • 2
    @MarcGlisse, I sanitize the whole program, not just separate shared libs. That answer is for a different problem (conflicting or duplicate ASAN runtimes), and I think that forcing me to use `LD_PRELOAD` is not good: why can't I just run the sanitized program? – Serge Rogatch Jul 29 '21 at 07:19
  • 1
    @S.M. I'm the author of the linked question and I confirm that this issue is distinct. I request to reopen this question. – yugr Jul 30 '21 at 17:29
  • 1
    @SergeRogatch the issue here is that clang installs `libclang_rt.asan-x86_64.so` in directory which is not present in `LD_LIBRARY_PATH` so you have to run your code with `LD_LIBRARY_PATH=/usr/lib/llvm-10/lib/clang/10.0.0/lib/linux${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}` . I agree that this is hardly usable so I suggest this needs to be raised with Debian/Ubuntu maintainers (this library should be accessible by default). – yugr Jul 30 '21 at 17:32
  • 1
    @MarcGlisse please check my comment, I believe this is a distinct issue. If you agree, please vote to reopen (as this seems to be a serious issue in current Debian). – yugr Jul 30 '21 at 17:32
  • 1
    @yugr Although you solved another problem, your solution is also suitable to the question above. The answer clearly states about the missing path to libclang_rt.asan-x86_64.so. – 273K Jul 30 '21 at 21:34
  • 1
    @yugr This is not an issue in Debian. rt libs are very specific to each clang version. How would they be differed in one lib location. – 273K Jul 30 '21 at 21:37
  • 1
    @S.M. I still believe that neither the symptom, nor the suggested remedy have anything to do with the question posted here and suggest to reopen the question so that other people who run into this problem can benefit from a proper answer rather than a comment. – yugr Jul 30 '21 at 23:55
  • 1
    @S.M. "How would they be differed in one lib location" - that is a valid point and a good reason to start properly versioning them in the upstream then. – yugr Jul 30 '21 at 23:56

1 Answers1

4

This is a deficiency of the clang front-end -- when given -shared-libsan flag, it should automatically add -Wl,-rpath=/usr/lib/llvm-NN/lib/clang/MM.M.M/lib/linux to the link line, but it doesn't.

You could do that yourself by using e.g.

CXX=clang++-12
$CXX -fsanitize=address -shared-libsan sample.cpp -o sample \
   -Wl,-rpath=$(dirname $($CXX --print-file-name libclang_rt.asan-x86_64.so))
Employed Russian
  • 199,314
  • 34
  • 295
  • 362