From 7aeaad5c86174f61b084d72d89fb02d7fc64391c Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 4 Aug 2021 20:55:28 -0700 Subject: runtime/cgo: when using msan explicitly unpoison cgoCallers This avoids an incorrect msan uninitialized memory report when using runtime.SetCgoTraceback when a signal occurs while the fifth argument register is undefined. See the issue for more details. Fixes #47543 Change-Id: I3d1b673e2c93471ccdae0171a99b88b5a6062840 Reviewed-on: https://go-review.googlesource.com/c/go/+/339902 Trust: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Go Bot Reviewed-by: Austin Clements --- src/runtime/cgo/gcc_traceback.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/runtime/cgo/gcc_traceback.c') diff --git a/src/runtime/cgo/gcc_traceback.c b/src/runtime/cgo/gcc_traceback.c index d86331c583..6e9470c43c 100644 --- a/src/runtime/cgo/gcc_traceback.c +++ b/src/runtime/cgo/gcc_traceback.c @@ -7,6 +7,14 @@ #include #include "libcgo.h" +#ifndef __has_feature +#define __has_feature(x) 0 +#endif + +#if __has_feature(memory_sanitizer) +#include +#endif + // Call the user's traceback function and then call sigtramp. // The runtime signal handler will jump to this code. // We do it this way so that the user's traceback function will be called @@ -19,6 +27,18 @@ x_cgo_callers(uintptr_t sig, void *info, void *context, void (*cgoTraceback)(str arg.SigContext = (uintptr_t)(context); arg.Buf = cgoCallers; arg.Max = 32; // must match len(runtime.cgoCallers) + +#if __has_feature(memory_sanitizer) + // This function is called directly from the signal handler. + // The arguments are passed in registers, so whether msan + // considers cgoCallers to be initialized depends on whether + // it considers the appropriate register to be initialized. + // That can cause false reports in rare cases. + // Explicitly unpoison the memory to avoid that. + // See issue #47543 for more details. + __msan_unpoison(&arg, sizeof arg); +#endif + (*cgoTraceback)(&arg); sigtramp(sig, info, context); } -- cgit v1.2.3-54-g00ecf