Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: clang AddressSanitizer does not work #8788

Closed
dvyukov opened this issue Sep 22, 2014 · 13 comments

Comments

@dvyukov
Copy link
Member

commented Sep 22, 2014

go version go1.3 linux/amd64
clang version 3.6.0 (217929)

The program is:
package main
// void foo() {}
import "C"
func main() {
    C.foo()
}

$ CC="clang -fsanitize=address" go run src.go

/var/tmp/go-link-FimrBB/000000.o:(.data._ZN11__sanitizer15IOCTL_KDGKBSENTE+0x0):
multiple definition of `__sanitizer::IOCTL_KDGKBSENT'
/ssd/src/llvm/build/bin/../lib/clang/3.6.0/lib/linux/libclang_rt.asan-x86_64.a(sanitizer_platform_limits_posix.cc.o):(.data._ZN11__sanitizer15IOCTL_KDGKBSENTE+0x0):
first defined here
/var/tmp/go-link-FimrBB/000000.o: In function `__interceptor_xdr_float':

While gcc 4.8.2 works fine.
@dvyukov

This comment has been minimized.

Copy link
Member Author

commented Sep 22, 2014

Comment 1:

gcc uses shared asan runtime, and so it tolerates some issues in build process:
$ ldd src
    libasan.so.0 => /usr/lib/x86_64-linux-gnu/libasan.so.0 (0x00007fae89644000)
while clang links asan runtime statically, and some issue in cgo build process causes
multiple symbol errors.
@dvyukov

This comment has been minimized.

Copy link
Member Author

commented Sep 22, 2014

Comment 2:

Kostya, the is in clang driver, it incorrectly handles -r linker flag. One of the
objects is build as:
clang -I . -fsanitize=address -fPIC -m64 -pthread -fno-caret-diagnostics
-Qunused-arguments -fmessage-length=0 -o $WORK/command-line-arguments/_obj/_all.o
$WORK/command-line-arguments/_obj/_cgo_export.o
$WORK/command-line-arguments/_obj/src.cgo2.o -g -O2 -Wl,-r -nostdlib
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a
It's kind of linking, but this object is later linked again:
pack r $WORK/command-line-arguments.a $WORK/command-line-arguments/_obj/_cgo_import.6
$WORK/command-line-arguments/_obj/_cgo_defun.6 $WORK/command-line-arguments/_obj/_all.o
# internal
cd .
/usr/lib/google-golang/pkg/tool/linux_amd64/6l -o
$WORK/command-line-arguments/_obj/exe/src -L $WORK -w -extld=clang
-extldflags=-fsanitize=address $WORK/command-line-arguments.a
So one asan runtime comes from _all.o (incorrectly) and another from implicitly added by
driver -lasan.
Driver must not link in asan runtime if -Wl,-r is present.
@dvyukov

This comment has been minimized.

Copy link
Member Author

commented Sep 22, 2014

Comment 3:

Moved to asan tracker:
https://code.google.com/p/address-sanitizer/issues/detail?id=344

Status changed to Invalid.

@dvyukov

This comment has been minimized.

Copy link
Member Author

commented Sep 23, 2014

Comment 4:

Ian, what do you think if we strip any -fsanitize= flags when we give -Wl,-r to linker?

Status changed to Thinking.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 23, 2014

Comment 5:

I don't see a reason to get into complex handling of the CC environment variable.  We
have enough flexibility without requiring that.
For example:
CC=clang CGO_CFLAGS=-fsanitize-address go run src.go
I don't know if it will work, but it should cause -fsanitize-address to be used when
compiling but not for the -Wl,-r step.
@dvyukov

This comment has been minimized.

Copy link
Member Author

commented Sep 23, 2014

Comment 6:

If I do:
CC=clang CGO_CFLAGS=-fsanitize=address
then it barks to undefined referenced to asan runtime.
If I do:
CC=clang CGO_CFLAGS=-fsanitize=address CGO_LDFLAGS=-fsanitize=address
then it barks at multiple definitions of asan runtime functions.
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 23, 2014

Comment 7:

Change the handling of bareLDFLAGS in cgo in cmd/go/build.go to strip -fsanitize options
(this is not the same as what you suggested in comment #4, or at least not the same as
what I understand you to be suggesting, because we already have to strip options from
CGO_LDFLAGS when using -r).
@dvyukov

This comment has been minimized.

Copy link
Member Author

commented Sep 23, 2014

Comment 8:

That's what I meant.
https://golang.org/cl/142470043
@gopherbot

This comment has been minimized.

Copy link

commented Sep 23, 2014

Comment 9:

CL https://golang.org/cl/142470043 mentions this issue.
@dvyukov

This comment has been minimized.

Copy link
Member Author

commented Sep 24, 2014

Comment 10:

This issue was closed by revision 39cd39b.

Status changed to Fixed.

@dvyukov dvyukov added fixed labels Sep 24, 2014

@bradfitz bradfitz modified the milestone: Go1.5 Dec 16, 2014

@bradfitz bradfitz removed the release-go1.5 label Dec 16, 2014

@vonosmas

This comment has been minimized.

Copy link

commented Mar 13, 2015

Just a quick question: do you strip -fsanitize=address from linker flags unconditionally, or only when you additionally pass "-r"? I don't think you should do the former.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 13, 2015

The -fsanitize options are only removed when the link is going to use -Wl,-r. This only happens when building a package that uses cgo (http://golang.org/cmd/cgo), as part of the steps of implementing the C/Go interface.

@vonosmas

This comment has been minimized.

Copy link

commented Mar 13, 2015

Right, thanks for clarifications!

@golang golang locked and limited conversation to collaborators Jun 25, 2016

wheatman added a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
wheatman added a commit to wheatman/go-akaros that referenced this issue Jun 26, 2018
wheatman added a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018
wheatman added a commit to wheatman/go-akaros that referenced this issue Jul 30, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants
You can’t perform that action at this time.