NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
16.67k stars 13.12k forks source link

clang 32 bit with address sanitizer missing libclang_rt.lsan-i386.a #201199

Open Mrogovp opened 1 year ago

Mrogovp commented 1 year ago

Describe the bug

clang 10 for 32bit doesn't support address sanitizer.

Steps To Reproduce

Steps to reproduce the behavior:

  1. using pkgsi686Linux.clang_10 in shell.nix
  2. clang a.c -m32 -fsanitize=leak in nix-shell
  3. got error: /nix/store/4c4fy3hs558qm0ykif24f200d8mvcrkb-binutils-2.35.1/bin/ld: cannot find /nix/store/h4h6h98m32xcp18vcbd2mx5rfd34bmw1-clang-wrapper-10.0.1/resource-root/lib/linux/libclang_rt.lsan-i386.a: No such file or directory clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

Expected behavior

Build should work

Metadata

tobiasBora commented 1 year ago

In my case I can make it work with a recent enough clang (tried with 13 and 14… do you actually mean that it should work even with clang 10?), and for the 32 bits I just use pkgs.pkgsi686Linux instead of pkgs, without even using -m32. However, the leaks are not detected (nothing happens when I run a program with a leak): a.c

#include <stdlib.h>
void *p;
int main() {
  p = malloc(7);
  p = 0; // The memory is leaked here.
  return 0;
}

derivation.nix

{ lib
, stdenv
, clang_14 # Fails with clang, you need a recent enough version
, ...
}:

stdenv.mkDerivation rec {
  pname = "";
  version = "";

  src = ./.;

  nativeBuildInputs = [ clang_14 ];
  # Same issue with -fsanitize=leak
  # just to make it look like https://clang.llvm.org/docs/LeakSanitizer.html
  buildPhase = ''
    clang -fsanitize=address -g a.c
  '';
  installPhase = ''
    mkdir -p $out/bin
    cp a.out $out/bin/
  '';
}

default.nix

{ pkgs ? import <nixpkgs> {} }:
pkgs.callPackage ./derivation.nix {}
# pkgs.pkgsi686Linux.callPackage ./derivation.nix {}

Commands:

$ nix-build
$ ASAN_OPTIONS=detect_leaks=1 ./result/bin/a.out
tobiasBora commented 1 year ago

@lovek323 @7c6f434c @dtzWill @primeos

tobiasBora commented 1 year ago

Argh, in fact I should have disabled the optimization first. With clang -O0 -fsanitize=address -g a.c it does work as expected:

$ ASAN_OPTIONS=detect_leaks=1 ./result/bin/a.out                                                                                                                                                             
=================================================================
==511906==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x4cf85f  (/nix/store/cjl6zbvw62q3vqz4pb59pf9fzfsvb676--/bin/a.out+0x4cf85f)
    #1 0x512fb8  (/nix/store/cjl6zbvw62q3vqz4pb59pf9fzfsvb676--/bin/a.out+0x512fb8)
    #2 0x7fed0b08e24d  (/nix/store/fz54faknl123dimzz6jsppw193lx2mip-glibc-2.35-163/lib/libc.so.6+0x2924d) (BuildId: a51f395ddf9cd82c373cb8ede4d2ad6bb2212ff8)

SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).

To maximize reproducibility I made a flake here (see readme for instructions).

@Mrogovp Is the bug related to the fact that clang 10 does not work, or is it fine to use clang 12/13/14… (on clang 11 we get an error when starting the binary)

Mrogovp commented 1 year ago

Thanks for your reply, Yes, the bug is related to clang10.

tobiasBora commented 1 year ago

Ok makes sense then. Note that the 64bit version does not suffer from the same issue: it does compile but then it's impossible to run the binary:

# Replace clang_14 with clang_10 in the above flake
$ nix build .#hello
$ ./result/bin/a.out
==637860==ERROR: LeakSanitizer failed to allocate 0x0 (0) bytes of SetAlternateSignalStack (error code: 22)
==637860==Process memory map follows:
        0x000000400000-0x000000404000   /nix/store/vrrg6lrqr50ibrhb1a9gw66nn5qhc6b0--/bin/a.out
        0x000000404000-0x00000042f000   /nix/store/vrrg6lrqr50ibrhb1a9gw66nn5qhc6b0--/bin/a.out
        0x00000042f000-0x000000441000   /nix/store/vrrg6lrqr50ibrhb1a9gw66nn5qhc6b0--/bin/a.out
        0x000000441000-0x000000442000   /nix/store/vrrg6lrqr50ibrhb1a9gw66nn5qhc6b0--/bin/a.out
        0x000000442000-0x000000445000   /nix/store/vrrg6lrqr50ibrhb1a9gw66nn5qhc6b0--/bin/a.out
        0x000000445000-0x000000cd9000
        0x600000000000-0x640000000000
        0x640000000000-0x640000003000
        0x7f6851900000-0x7f6852100000
        0x7f6852100000-0x7f6852200000
        0x7f6852300000-0x7f6852400000
        0x7f68524e0000-0x7f6852700000
        0x7f6852800000-0x7f6852900000
        0x7f68529ad000-0x7f68529dd000
        0x7f68529dd000-0x7f6852a05000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libc.so.6
        0x7f6852a05000-0x7f6852b7b000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libc.so.6
        0x7f6852b7b000-0x7f6852bd3000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libc.so.6
        0x7f6852bd3000-0x7f6852bd7000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libc.so.6
        0x7f6852bd7000-0x7f6852bd9000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libc.so.6
        0x7f6852bd9000-0x7f6852be8000
        0x7f6852be8000-0x7f6852beb000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libgcc_s.so.1
        0x7f6852beb000-0x7f6852bfc000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libgcc_s.so.1
        0x7f6852bfc000-0x7f6852bff000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libgcc_s.so.1
        0x7f6852bff000-0x7f6852c00000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libgcc_s.so.1
        0x7f6852c00000-0x7f6852c01000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libgcc_s.so.1
        0x7f6852c01000-0x7f6852c02000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libgcc_s.so.1
        0x7f6852c02000-0x7f6852c03000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libdl.so.2
        0x7f6852c03000-0x7f6852c04000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libdl.so.2
        0x7f6852c04000-0x7f6852c05000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libdl.so.2
        0x7f6852c05000-0x7f6852c06000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libdl.so.2
        0x7f6852c06000-0x7f6852c07000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libdl.so.2
        0x7f6852c07000-0x7f6852c15000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libm.so.6
        0x7f6852c15000-0x7f6852c88000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libm.so.6
        0x7f6852c88000-0x7f6852ce5000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libm.so.6
        0x7f6852ce5000-0x7f6852ce6000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libm.so.6
        0x7f6852ce6000-0x7f6852ce7000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libm.so.6
        0x7f6852ce7000-0x7f6852ce8000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/librt.so.1
        0x7f6852ce8000-0x7f6852ce9000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/librt.so.1
        0x7f6852ce9000-0x7f6852cea000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/librt.so.1
        0x7f6852cea000-0x7f6852ceb000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/librt.so.1
        0x7f6852ceb000-0x7f6852cec000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/librt.so.1
        0x7f6852cec000-0x7f6852ced000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libpthread.so.0
        0x7f6852ced000-0x7f6852cee000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libpthread.so.0
        0x7f6852cee000-0x7f6852cef000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libpthread.so.0
        0x7f6852cef000-0x7f6852cf0000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libpthread.so.0
        0x7f6852cf0000-0x7f6852cf1000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libpthread.so.0
        0x7f6852cf1000-0x7f6852cf3000
        0x7f6852cf3000-0x7f6852cf5000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/ld-linux-x86-64.so.2
        0x7f6852cf5000-0x7f6852d1c000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/ld-linux-x86-64.so.2
        0x7f6852d1c000-0x7f6852d28000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/ld-linux-x86-64.so.2
        0x7f6852d28000-0x7f6852d2a000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/ld-linux-x86-64.so.2
        0x7f6852d2a000-0x7f6852d2c000   /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/ld-linux-x86-64.so.2
        0x7fffc84d7000-0x7fffc84fe000   [stack]
        0x7fffc858d000-0x7fffc8591000   [vvar]
        0x7fffc8591000-0x7fffc8593000   [vdso]
        0xffffffffff600000-0xffffffffff601000   [vsyscall]
==637860==End of process memory map.
==637860==Sanitizer CHECK failed: /build/compiler-rt-10.0.1.src/lib/sanitizer_common/sanitizer_common.cpp:54 ((0 && "unable to mmap")) != (0) (0, 0)