Skip to content

Instantly share code, notes, and snippets.

@tobiasBora
Last active November 23, 2022 03:42
Show Gist options
  • Save tobiasBora/0952c923d3cd892e41fbc3ef025ac020 to your computer and use it in GitHub Desktop.
Save tobiasBora/0952c923d3cd892e41fbc3ef025ac020 to your computer and use it in GitHub Desktop.
Demo sanitizer clang nix

To answer https://stackoverflow.com/questions/74434312/clang-32bit-with-address-sanitizer-with-nix and NixOS/nixpkgs#201199

$ nix build 'git+https://gist.github.com/tobiasBora/0952c923d3cd892e41fbc3ef025ac020?ref=main#hello'

$ file ./result/bin/a.out
./result/bin/a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, not stripped

$ ./result/bin/a.out
Hello world

=================================================================
==548464==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x40c4f2  (/nix/store/3i0ipj0zwpjm8g1fslvfjb5dng4bd9s0--/bin/a.out+0x40c4f2)
    #1 0x43b858  (/nix/store/3i0ipj0zwpjm8g1fslvfjb5dng4bd9s0--/bin/a.out+0x43b858)
    #2 0x7f381db5624d  (/nix/store/4nlgxhb09sdr51nc9hdm8az5b08vzkgx-glibc-2.35-163/lib/libc.so.6+0x2924d) (BuildId: 2bb226bc600b443958c7566207d0d02f8345e6ea)

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

$ nix build 'git+https://gist.github.com/tobiasBora/0952c923d3cd892e41fbc3ef025ac020?ref=main#hello-32'

$ file ./result/bin/a.out
./result/bin/a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /nix/store/bzw74a7dla8iqk2h8wshwi03fbgj2c5h-glibc-2.35-163/lib/ld-linux.so.2, for GNU/Linux 2.6.32, not stripped

$ ./result/bin/a.out

Hello world

=================================================================
==554039==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x8055779  (/nix/store/yh4klla6n8c9flgcxd7r05zj705kycdl--/bin/a.out+0x8055779)
    #1 0x8094238  (/nix/store/yh4klla6n8c9flgcxd7r05zj705kycdl--/bin/a.out+0x8094238)
    #2 0xf7c102f9  (/nix/store/bzw74a7dla8iqk2h8wshwi03fbgj2c5h-glibc-2.35-163/lib/libc.so.6+0x212f9) (BuildId: 0fc69f6f66391d65b1e33788a951319bda8ba1d4)

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

#include <stdlib.h>
#include <stdio.h>
void *p;
int main() {
p = malloc(7);
p = 0; // The memory is leaked here.
printf("Hello world\n");
return 0;
}
{ lib
, stdenv
, clang_14 # Fails with clang, you need a recent enough version
, ...
}:
stdenv.mkDerivation rec {
pname = "";
version = "";
src = ./.;
nativeBuildInputs = [ clang_14 ];
buildPhase = ''
clang -O0 -fsanitize=leak -g a.c
'';
installPhase = ''
mkdir -p $out/bin
cp a.out $out/bin/
'';
}
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1669076005,
"narHash": "sha256-uzMji2q9Pk3jUH+e5nEFtoOZCP4VV1PDRJRLVmriY0M=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "69335c46c48a73f291d5c6f332fb9fe8b8e22b30",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}
{
description = "A very basic flake";
outputs = { self, nixpkgs }: let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
pkgs32 = pkgs.pkgsi686Linux;
in {
packages.x86_64-linux.hello = pkgs.callPackage ./derivation.nix {};
packages.x86_64-linux.hello-32 = pkgs32.callPackage ./derivation.nix {};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment