How do I run go test with the -msan option on Ubuntu 18.04?

Viewed 885

When I try to run the go unit test system with the memory sanity checking I get an error message that I am sure I use to solve by setting my compiler to CLANG.

The Go Command Documenation is a little brief on the matter.

-msan
    enable interoperation with memory sanitizer.
    Supported only on linux/amd64, linux/arm64
    and only with Clang/LLVM as the host C compiler.
    On linux/arm64, pie build mode will be used.

In the past I was use I got this to work by calling:

CC=clang go test -msan ./..

However when I do that now I get errors such as:

g++: error: unrecognized argument to -fsanitize= option: ‘memory’

What do I need to do in order to run my golang tests with the memory sanitizer under Ubuntu 18:04 ?


I am using the following versions of tools at the moment:

$ go version
go version go1.14 linux/amd64
$ clang --version
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
1 Answers

It requires a version of LLVM that supports -fsanitize=memory on your version of Ubuntu 18.04 that the program runs on. Please try updating and then try it again.

Also do note, on Linux you need at least LLVM 3.8 to get the -fsanitize flag.

The Go tool automatically adds the -fsanitize=memory option to CGO_CPPFLAGS flag as required by clang for linking and that's where it's resulting in an error for you.

Also, make sure to add both CC and CXX (for clang++) flags so to enable compilation using Clang when you're interop your program with C/C++ i.e.,

CC=clang CXX=clang++ go build -msan

Also do refer this link:

https://go.googlesource.com/go/+/go1.7/misc/cgo/testsanitizers/test.bash (bash script)

(or)

https://github.com/golang/go/tree/master/misc/cgo/testsanitizers (*.go files)

It will help you test the sanitizers if they would work on your setup or not.

Related