Address sanitizer in cgo code

643 views
Skip to first unread message

Ian Pye

unread,
Sep 20, 2014, 2:43:41 AM9/20/14
to golan...@googlegroups.com
I'm trying to use AddressSanitizer (https://code.google.com/p/address-sanitizer/wiki/AddressSanitizer) in my cgo code.

However, compiling with:

 LDFLAGS="-ldl" CC="clang -fsanitize=address" go get chfclient 

results in lots of errors, beginning with:

/var/tmp/go-link-nBMW4n/000001.o: In function `__asan_report_load16':
(.text+0x2de30): multiple definition of `__asan_report_load16'
/var/tmp/go-link-nBMW4n/000000.o:(.text+0x2e20): first defined here
/var/tmp/go-link-nBMW4n/000001.o: In function `__lll_robust_lock_wait':

Does anyone have any tips on using Address Sanitizer with go?

Thanks,

Ian

Dmitry Vyukov

unread,
Sep 21, 2014, 3:01:26 AM9/21/14
to Ian Pye, golang-nuts
Hi,

Please provide a reproducer program, and also versions of go and
clang/gcc. We use AddressSanitizer with swig/cgo code so it can be
made work.

Ian Pye

unread,
Sep 22, 2014, 1:22:34 AM9/22/14
to golan...@googlegroups.com, ian...@gmail.com
go version go1.3.1 linux/amd64
Ubuntu clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3

=== exec/exec.go ====

package exec

/*
#include <stdio.h>
#include <stdlib.h>

void myprint(char* s) {
        printf("%s", s);
}
*/
import "C"

import "unsafe"

func Example() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
C.free(unsafe.Pointer(cs))
}

=== run/run.go ===

package main

import "exec"

func main() {
exec.Example()
}

================

CC="clang -fsanitize=address" go get run

This errors for me, while

CC="clang" go get run

Runs fine.

Thanks for any advice!

Dmitry Vyukov

unread,
Sep 22, 2014, 2:00:44 AM9/22/14
to Ian Pye, golang-nuts
Clang indeed does not work with cgo. Filed:
https://code.google.com/p/go/issues/detail?id=8788

While gcc 4.8.2 (stock gcc on Ubuntu 14.04) works fine:
$ CC="gcc -fsanitize=address" go run src2.go

=================================================================

==24628== ERROR: AddressSanitizer: heap-buffer-overflow on address
0x60160000aff5 at pc 0x401066 bp 0x7fff5bf8e960 sp 0x7fff5bf8e958
WRITE of size 1 at 0x60160000aff5 thread T0
#0 0x401065
(/tmp/go-build529336755/command-line-arguments/_obj/exe/src2+0x401065)
#1 0x423990
(/tmp/go-build529336755/command-line-arguments/_obj/exe/src2+0x423990)
0x60160000aff5 is located 1 bytes to the right of 100-byte region
[0x60160000af90,0x60160000aff4)
allocated by thread T0 here:
#0 0x7f543595e41a (/usr/lib/x86_64-linux-gnu/libasan.so.0.0.0+0x1541a)
#1 0x401019
(/tmp/go-build529336755/command-line-arguments/_obj/exe/src2+0x401019)


Can you switch to a relatively fresh gcc (like 4.8.2)?
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ian Pye

unread,
Sep 22, 2014, 1:39:13 PM9/22/14
to golan...@googlegroups.com, ian...@gmail.com
Thanks a lot for your help -- really appreciate it!
Reply all
Reply to author
Forward
0 new messages