From 3b8951c1dcea8dd3121c0003241370765a13fc77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 22 Feb 2019 11:20:18 +0100 Subject: [PATCH 1/5] meson: make sure preprocesor warnings are not treated as errors Clang includes -W#warning in -Werror, so the #warning used for msan would be an error. v2: - use -Wno-error=... so that the warning is still emitted, but not as an error. --- meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/meson.build b/meson.build index ed787d4749..c1f457bc7d 100644 --- a/meson.build +++ b/meson.build @@ -333,6 +333,7 @@ possible_cc_flags = [ '-Wno-missing-field-initializers', '-Wno-unused-result', '-Wno-format-signedness', + '-Wno-error=#warnings', # work-around for gcc 7.1 turning this on on its own. '-Wno-error=nonnull', From c322f379e6ca972f1c4d3409ac97828b1b838d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 22 Feb 2019 13:07:00 +0100 Subject: [PATCH 2/5] Add wrapper for __msan_unpoinson() to reduce #ifdeffery This isn't really necessary for the subsequent commit, but I expect that we'll need to unpoison more often once we turn on msan in CI, so I think think this change makes sense in the long run. --- src/basic/alloc-util.h | 10 ++++++++++ src/basic/random-util.c | 11 ++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 893a1238ff..78ee34bb71 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -8,6 +8,10 @@ #include "macro.h" +#if HAS_FEATURE_MEMORY_SANITIZER +# include +#endif + typedef void (*free_func_t)(void *p); /* If for some reason more than 4M are allocated on the stack, let's abort immediately. It's better than @@ -160,3 +164,9 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size); (ptr) = NULL; \ _ptr_; \ }) + +#if HAS_FEATURE_MEMORY_SANITIZER +# define msan_unpoison(r, s) __msan_unpoison(r, s) +#else +# define msan_unpoison(r, s) +#endif diff --git a/src/basic/random-util.c b/src/basic/random-util.c index f7decf60b6..ca25fd2420 100644 --- a/src/basic/random-util.c +++ b/src/basic/random-util.c @@ -23,16 +23,13 @@ # include #endif +#include "alloc-util.h" #include "fd-util.h" #include "io-util.h" #include "missing.h" #include "random-util.h" #include "time-util.h" -#if HAS_FEATURE_MEMORY_SANITIZER -#include -#endif - int rdrand(unsigned long *ret) { #if defined(__i386__) || defined(__x86_64__) @@ -58,11 +55,7 @@ int rdrand(unsigned long *ret) { "setc %1" : "=r" (*ret), "=qm" (err)); - -#if HAS_FEATURE_MEMORY_SANITIZER - __msan_unpoison(&err, sizeof(err)); -#endif - + msan_unpoison(&err, sizeof(err)); if (!err) return -EAGAIN; From 9003da29631b5348a4578d605e150321adbd3461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 22 Feb 2019 13:23:57 +0100 Subject: [PATCH 3/5] test-mountpoint-util: unpoison string allocated by sscanf %ms --- src/test/test-mountpoint-util.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index 8e45c0b1a7..be76429506 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -36,7 +36,7 @@ static void test_mount_propagation_flags(const char *name, int ret, unsigned lon static void test_mnt_id(void) { _cleanup_fclose_ FILE *f = NULL; - Hashmap *h; + _cleanup_hashmap_free_free_ Hashmap *h = NULL; Iterator i; char *p; void *k; @@ -57,7 +57,14 @@ static void test_mnt_id(void) { assert_se(r > 0); assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2); - +#if HAS_FEATURE_MEMORY_SANITIZER + /* We don't know the length of the string, so we need to unpoison it one char at a time */ + for (const char *c = path; ;c++) { + msan_unpoison(c, 1); + if (!*c) + break; + } +#endif log_debug("mountinfo: %s → %i", path, mnt_id); assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0); @@ -84,8 +91,6 @@ static void test_mnt_id(void) { log_debug("the other path for mnt id %i is %s\n", mnt_id2, t); assert_se(path_equal(p, t)); } - - hashmap_free_free(h); } static void test_path_is_mount_point(void) { From b6cda3ec4dd0c7a9d5c43cb001326f3c0ccad116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 22 Feb 2019 12:05:24 +0100 Subject: [PATCH 4/5] test-json: avoid deep stack recursion under msan --- src/test/test-json.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/test-json.c b/src/test/test-json.c index fdf1b4f40c..f55edfdaef 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -391,6 +391,13 @@ static void test_depth(void) { log_info("max depth at %u", i); break; } +#if HAS_FEATURE_MEMORY_SANITIZER + /* msan doesn't like the stack nesting to be too deep. Let's quit early. */ + if (i >= 128) { + log_info("quitting early at depth %u", i); + break; + } +#endif assert_se(r >= 0); From adbdcfbe6377439999a2fb6b31e74a57ace0baeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 22 Feb 2019 12:05:49 +0100 Subject: [PATCH 5/5] test-json: use standard test intro --- src/test/test-json.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/test-json.c b/src/test/test-json.c index f55edfdaef..9b8a2a9422 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -8,6 +8,7 @@ #include "json.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "util.h" static void test_tokenizer(const char *data, ...) { @@ -410,10 +411,7 @@ static void test_depth(void) { } int main(int argc, char *argv[]) { - - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_tokenizer("x", -EINVAL); test_tokenizer("", JSON_TOKEN_END);