What package do I have to install to get address sanitizer?

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; [

I reported the issue here: https://github.com/NixOS/nixpkgs/issues/185141

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


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

$ 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:

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.

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.

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).

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

Hmm, on my x86 system it seems like it’s shipped with the gcc11 package:

$ find /nix | grep asan\.so

The same command yield no output on my aarch64 (M1) machine

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 :slight_smile:

Looked into this more, apparently I was being fooled:

[nix-shell:~]$ realpath $(which 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


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.

Hosted by Flying Circus.