Hi, I’m trying to create a development environment and I want to enable the address sanitizer as part of my debug builds. However, I seem to miss the asan library:
$ g++ -fsanitize=address test.cpp
ld: library not found for -lasan
collect2: error: ld returned 1 exit status
With test.cpp
simply containing int main() {}
I tried to look around in nixpkgs for address sanitizer but I can’t find anything relevant.
Thanks for the help!
You shouldn’t have to add anything. It does work for me.
Perhaps you added something to the environment that broke it like gcc.lib
?
Hey! Thanks for the reply.
I think it may be an issue with nix on ARM instruction set
My nix shell that reproduce the issue is the following:
{
pkgs ? import <nixpkgs> {},
stdenv ? pkgs.stdenv
}:
pkgs.mkShell {
buildInputs = with pkgs; [
gcc11
];
}
I reported the issue here: Address sanitizer is missing a library on Darwin arm64 · Issue #185141 · NixOS/nixpkgs · GitHub
gcc11
should be available through your stdenv (on linux). Also mkShell
just uses stdenv.mkDerivation
but adds some more dev shell conveniences. Just remove all buildInputs.
[09:49:54] jon@jon-desktop /home/jon/projects/nixpkgs (master)
$ echo 'int main() {}' > test.cpp
[09:50:05] jon@jon-desktop /home/jon/projects/nixpkgs (master)
$ nix-shell -p stdenv
[nix-shell:/home/jon/projects/nixpkgs]$ g++ -fsanitize=address test.cpp
[nix-shell:/home/jon/projects/nixpkgs]$
I’m on a Darwin system for that project and you have to ask for GCC explicitly
Then you want gccStdenv.mkDerivation
instead of mkShell.
pkgs.gccStdenv.mkDerivation {
name = "dev-env";
src = null;
}
You may also be able to do something like (overrideCC mkShell buildPackages.gcc11)
if you want to keep some of mkShell's
QoL features.
1 Like
[11:16:58] jon@Jonathans-Mac-mini ~/projects/nixpkgs (master)
$ echo 'int main() {}' > test.cpp
[11:17:05] jon@Jonathans-Mac-mini ~/projects/nixpkgs (master)
$ nix-shell -p gccStdenv
[nix-shell:~/projects/nixpkgs]$ g++ -fsanitize=address test.cpp
[nix-shell:~/projects/nixpkgs]$ uname -a
Darwin Jonathans-Mac-mini.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:29:10 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T8101 arm64 arm Darwin
Okay I tried this shell file as you described:
{
pkgs ? import <nixpkgs> {},
stdenv ? pkgs.stdenv
}:
pkgs.gccStdenv.mkDerivation {
name = "shell";
src = null;
}
Then I tried again:
$ gcc --version
gcc (GCC) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -fsanitize=address test.cpp
ld: library not found for -lasan
collect2: error: ld returned 1 exit status
$ type gcc
gcc is hashed (/nix/store/2rdryix3sh7pcvkpf6lvswp7zaqv6rbv-gcc-wrapper-11.2.0/bin/gcc
Someone pointed out that address sanitizer may not be built on Darwin aarch64:
https://github.com/NixOS/nixpkgs/issues/185141#issuecomment-1205395157
Oh wait, you’re in a M1 too? Hmm… Then I’m not sure what’s going on with my system then.
Yea, I have an m1 for testing and review purposes.
Most likely my version of nixpkgs points to something quite a bit more modern.
trofi
August 7, 2022, 11:08am
12
Do you know where your -lasan
comes from? Adding -Wl,-t
should show linked libraries used in final binary.
Looking at a random gcc-11.2.0
build in staging-next
https://hydra.nixos.org/log/j5las0g2lmnm2z6jipi5r99ncmnnxisk-gcc-11.2.0.drv shows expected checking for libsanitizer support... no
.
I think asan
is not supported on aarch64-darwin
by gcc
so far. I only see x86_64-darwin
and aarch64-linux
in the default target selector: gcc.gnu.org Git - gcc.git/blob - libsanitizer/configure.tgt
I think -lasan
is automatically added when you have -fsanitize=address
.
trofi
August 7, 2022, 1:22pm
14
Sure. I mean where is the actual file resides to make lasan
resolvable? If the libsanitizer
is not built it probably means it comes from somewhere else like compiler-rt
(likely unexpectedly).
NobbZ
August 7, 2022, 3:00pm
15
Seems as ift was provided by the following attributes:
$ nix shell nixpkgs#nix-index -c nix-locate asan.so | rg -v '\(' | cut -d' ' -f1,1 remarkable2-toolchain.out remarkable2-toolchain.out
remarkable-toolchain.out
remarkable-toolchain.out
libgccjit.out
libgccjit.out
libgccjit.out
gcc-unwrapped.lib
gcc-unwrapped.lib
gcc-unwrapped.lib
Hmm, on my x86 system it seems like it’s shipped with the gcc11 package:
$ find /nix | grep asan\.so
/nix/store/b5jpilrsa93fj4fpcx90hx05giinkjdz-gfortran-11.3.0-lib/lib/libasan.so.6.0.0
/nix/store/b5jpilrsa93fj4fpcx90hx05giinkjdz-gfortran-11.3.0-lib/lib/libasan.so.6
/nix/store/b5jpilrsa93fj4fpcx90hx05giinkjdz-gfortran-11.3.0-lib/lib/libasan.so
/nix/store/8dn12i3d7harw8g7dzk6dy7c5diz5ibp-gcc-11.3.0-lib/lib/libasan.so.6.0.0
/nix/store/8dn12i3d7harw8g7dzk6dy7c5diz5ibp-gcc-11.3.0-lib/lib/libasan.so.6
/nix/store/8dn12i3d7harw8g7dzk6dy7c5diz5ibp-gcc-11.3.0-lib/lib/libasan.so
/nix/store/mw4m75mrcyibvmkflrlfp1na5lw5l90q-gfortran-11.3.0-lib/lib/libasan.so.6.0.0
/nix/store/mw4m75mrcyibvmkflrlfp1na5lw5l90q-gfortran-11.3.0-lib/lib/libasan.so.6
/nix/store/mw4m75mrcyibvmkflrlfp1na5lw5l90q-gfortran-11.3.0-lib/lib/libasan.so
/nix/store/q9gfnkp0zaafccpgj4mqngmp9f9y93xh-gcc-11.3.0-lib/lib/libasan.so.6.0.0
/nix/store/q9gfnkp0zaafccpgj4mqngmp9f9y93xh-gcc-11.3.0-lib/lib/libasan.so.6
/nix/store/q9gfnkp0zaafccpgj4mqngmp9f9y93xh-gcc-11.3.0-lib/lib/libasan.so
/nix/store/paqfl70z4zxip8lvpsijbspi0y2wzg4i-gcc-10.3.0-lib/lib/libasan.so.6.0.0
/nix/store/paqfl70z4zxip8lvpsijbspi0y2wzg4i-gcc-10.3.0-lib/lib/libasan.so.6
/nix/store/paqfl70z4zxip8lvpsijbspi0y2wzg4i-gcc-10.3.0-lib/lib/libasan.so
/nix/store/zj5wpxw6z32rqwidcq7ii6pgg1i3d209-gfortran-11.3.0-lib/lib/libasan.so.6.0.0
/nix/store/zj5wpxw6z32rqwidcq7ii6pgg1i3d209-gfortran-11.3.0-lib/lib/libasan.so.6
/nix/store/zj5wpxw6z32rqwidcq7ii6pgg1i3d209-gfortran-11.3.0-lib/lib/libasan.so
/nix/store/gka59hya7l7qp26s0rydcgq8hj0d7v7k-gcc-11.3.0-lib/lib/libasan.so.6.0.0
/nix/store/gka59hya7l7qp26s0rydcgq8hj0d7v7k-gcc-11.3.0-lib/lib/libasan.so.6
/nix/store/gka59hya7l7qp26s0rydcgq8hj0d7v7k-gcc-11.3.0-lib/lib/libasan.so
The same command yield no output on my aarch64 (M1) machine
trofi
August 12, 2022, 4:49pm
17
Yeah, I wonder where valid libasan.so
comes from for @jonringer . In my understanding it should not work on aarch64-darwin
(unless it’s an emulated x86_64
environment on aarch64
machine): What package do I have to install to get address sanitizer? - #12 by trofi
1 Like
I have no idea, it just worked
EDIT:
Looked into this more, apparently I was being fooled:
[nix-shell:~]$ realpath $(which gcc)
/usr/bin/gcc
[nix-shell:~]$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.27.3)
Target: arm64-apple-darwin21.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
So I’m guessing clang just dropped the -fsanitize=address
Sorry for unintentionally gas lighting
3 Likes
So I’m guessing clang just dropped the -fsanitize=address
Oh, that’s quite sad. The address sanitizer is the best tool to identify problems and is a must for unit tests. Right now I must run my tests using a build from outside of nix to ensure everything is fine.
1 Like
I noticed that the address sanitizer dylibs are included in compiler-rt
(compiler-rt-libc-14.0.6/lib/libclang_rt.asan_osx_dynamic.dylib
)
Maybe there is some way to tell clang to link to it?
Actually, it looks like using llvmPackages_14.stdenv
works, if you’re okay with using clang.