From 3a37a8121d1d5e74abc1f1b9a5855c4c9d72d6a2 Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Tue, 12 Aug 2025 16:04:18 +0200 Subject: [PATCH 1/4] EDITME: cover title for enable-memory-sanitizer # Describe the purpose of this series. The information you put here # will be used by the project maintainer to make a decision whether # your patches should be reviewed, and in what priority order. Please be # very detailed and link to any relevant discussions or sites that the # maintainer can review to better understand your proposed changes. If you # only have a single patch in your series, the contents of the cover # letter will be appended to the "under-the-cut" portion of the patch. # Lines starting with # will be removed from the cover letter. You can # use them to add notes or reminders to yourself. If you want to use # markdown headers in your cover letter, start the line with ">#". # You can add trailers to the cover letter. Any email addresses found in # these trailers will be added to the addresses specified/generated # during the b4 send stage. You can also run "b4 prep --auto-to-cc" to # auto-populate the To: and Cc: trailers based on the code being # modified. Signed-off-by: Karthik Nayak --- b4-submit-tracking --- # This section is used internally by b4 prep for tracking purposes. { "series": { "revision": 1, "change-id": "20250812-enable-memory-sanitizer-363d1c07510c", "prefixes": [] } } -- GitLab From 954a6bae3e26ddd3abff29c8381dbe3462dd50f5 Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Tue, 12 Aug 2025 16:04:44 +0200 Subject: [PATCH 2/4] throwaway: use the memory sanitizer --- ci/lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/lib.sh b/ci/lib.sh index f561884d401..50d02adadeb 100755 --- a/ci/lib.sh +++ b/ci/lib.sh @@ -355,7 +355,7 @@ linux-musl-meson) MESONFLAGS="$MESONFLAGS -Dtest_utf8_locale=C.UTF-8" ;; linux-leaks|linux-reftable-leaks) - export SANITIZE=leak + export SANITIZE=memory ;; linux-asan-ubsan) export SANITIZE=address,undefined -- GitLab From f4fc02911469b2f3d88cc65fad5dab292b910a30 Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Tue, 12 Aug 2025 16:09:22 +0200 Subject: [PATCH 3/4] git: add memory unpoison --- Makefile | 3 +++ git-compat-util.h | 7 +++++++ meson.build | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/Makefile b/Makefile index e11340c1ae7..f75c7a8d21d 100644 --- a/Makefile +++ b/Makefile @@ -1517,6 +1517,9 @@ ifneq ($(filter address,$(SANITIZERS)),) NO_REGEX = NeededForASAN SANITIZE_ADDRESS = YesCompiledWithIt endif +ifneq ($(filter memory,$(SANITIZERS)),) +BASIC_CFLAGS += -DENABLE_MSAN_UNPOISON +endif endif ifndef sysconfdir diff --git a/git-compat-util.h b/git-compat-util.h index 9408f463e31..494b3809491 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1102,4 +1102,11 @@ extern int not_supposed_to_survive; #define assert(expr) ((void)(not_supposed_to_survive || (expr))) #endif /* CHECK_ASSERTION_SIDE_EFFECTS */ +#ifdef ENABLE_MSAN_UNPOISON +#include +#define msan_unpoison(ptr, len) __msan_unpoison(ptr, len) +#else +#define msan_unpoison(ptr, len) do {} while (0) +#endif + #endif diff --git a/meson.build b/meson.build index 5dd299b4962..e87c0aa3367 100644 --- a/meson.build +++ b/meson.build @@ -860,6 +860,10 @@ if get_option('b_sanitize').contains('leak') else build_options_config.set('SANITIZE_LEAK', '') endif +if get_option('b_sanitize').contains('memory') + libgit_c_args += '-DENABLE_MSAN_UNPOISON' + libgit_c_args += '-fsanitize-memory-track-origins' +endif if get_option('b_sanitize').contains('undefined') libgit_c_args += '-DSHA1DC_FORCE_ALIGNED_ACCESS' endif -- GitLab From 261ea5af6f9877706c05868528a3b7615c4b4b72 Mon Sep 17 00:00:00 2001 From: Karthik Nayak Date: Wed, 13 Aug 2025 13:18:13 +0200 Subject: [PATCH 4/4] wrapper: add msan_unpoison --- wrapper.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wrapper.c b/wrapper.c index 2f00d2ac876..f4b71f2a8af 100644 --- a/wrapper.c +++ b/wrapper.c @@ -482,6 +482,8 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode) if (csprng_bytes(&v, sizeof(v), 0) < 0) return error_errno("unable to get random bytes for temporary file"); + msan_unpoison(&v, sizeof(v)); + /* Fill in the random bits. */ for (i = 0; i < num_x; i++) { filename_template[i] = letters[v % num_letters]; @@ -846,6 +848,8 @@ uint32_t git_rand(unsigned flags) if (csprng_bytes(&result, sizeof(result), flags) < 0) die(_("unable to get random bytes")); + msan_unpoison(&result, sizeof(result)); + return result; } -- GitLab