-
Notifications
You must be signed in to change notification settings - Fork 18k
runtime: enable leak sanitizer in Go #67833
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
Comments
I don't think this has to be a proposal, taking it out of the proposal process. We should just do this as part of our ordinary sanitizer support. It sounds like this will be pretty loose, unless there is some way to tell the sanitizer which pointers are live. But a conservative leak detector is still better than no leak detector at all. |
I took another look into LSAN, to understand how conservative it would be. https://maskray.me/blog/2023-02-12-all-about-leak-sanitizer was quite helpful. What that article doesn't document is the |
It's a completely redundant feature, the example code doesn't detect the array size at all, and of course it's also your freedom, so why should you be reminded of it |
Enabling the leak sanitizer would be a nice improvement. Note that some (see list) Go projects are already sidestepping this limitation by manually calling |
Does anybody happen to know which versions of clang and GCC added support for |
LLVM introduced it in version 3.4 (source), which was released in 2015. GCC followed suite in version 5 (source), released also in 2015. I've been using package main
// void __lsan_do_leak_check(void);
import "C"
func main() {
defer C.__lsan_do_leak_check()
C.CString("foo")
} Which on my machine outputs: =================================================================
==13248==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 4 byte(s) in 1 object(s) allocated from:
#0 0x7f127460a24f in malloc (/usr/lib/libasan.so.8+0xe124f) (BuildId: ccf997b37481b6e7b757519a36b110bee0a850cd)
#1 0x472c7b (/tmp/go-build1729442831/b001/exe/test+0x472c7b) (BuildId: 2adb13e01facb758d13955e5fc42872f5ec67f1f)
SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s).
exit status 1 Note that the memory leak sanitizer is enabled by default on Linux when using the address sanitizer, so Go apps built with |
Change https://go.dev/cl/651755 mentions this issue: |
Change https://go.dev/cl/659135 mentions this issue: |
CL 651755 introduced registration of root regions when allocating memory. We also need to unregister that memory to avoid the leak sanitizer accessing unmapped memory. Issue #67833 Change-Id: I5d403d66e65a8a003492f4d79dad22d416fd8574 Reviewed-on: https://go-review.googlesource.com/c/go/+/659135 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Proposal Details
This is follow-up to #44853 which proposed to enable ASAN for Go.
One type of error in Go / C interop are memory leaks. These can be detected by ASAN using
ASAN_OPTIONS=detect_leaks=1
. Unfortunately, this doesn't fully work with Go's ASAN integration. For example, allocating a C object and storing it in a global variable in Go is detected as a leak.I believe to make LSAN work, all we need to do is to tell it about memory regions that Go manages to consider as roots using
__lsan_register_root_region
.The text was updated successfully, but these errors were encountered: