[compiler-rt] [sanitizer_symbolizer] Use correct format strings for uptr (PR #89815)
Dimitry Andric via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 23 12:53:38 PDT 2024
https://github.com/DimitryAndric updated https://github.com/llvm/llvm-project/pull/89815
>From 79fa752ff1df4ff3aa48493cf1abecf3fc3c7d94 Mon Sep 17 00:00:00 2001
From: Dimitry Andric <dimitry at andric.com>
Date: Tue, 23 Apr 2024 21:41:24 +0200
Subject: [PATCH 1/2] [sanitizer_symbolizer] Use correct format strings for
uptr
When compiling the common sanitizer libraries, there are many warnings
about format specifiers, similar to:
compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp:31:32: warning: format specifies type 'void *' but the argument has type 'uptr' (aka 'unsigned long') [-Wformat]
31 | buffer->AppendF(kFormatData, DI->start);
| ~~~~~~~~~~~ ^~~~~~~~~
compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h:33:46: note: format string is defined here
33 | constexpr const char *kFormatData = "{{{data:%p}}}";
| ^~
| %lu
compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp:46:43: warning: format specifies type 'void *' but the argument has type 'uptr' (aka 'unsigned long') [-Wformat]
46 | buffer->AppendF(kFormatFrame, frame_no, address);
| ~~~~~~~~~~~~ ^~~~~~~
compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h:36:48: note: format string is defined here
36 | constexpr const char *kFormatFrame = "{{{bt:%u:%p}}}";
| ^~
| %lu
...
This is because `uptr` is dependent on the platform, and can be either
`unsigned long long`, `unsigned long`, or `unsigned int`.
To fix the warnings, define the correct printf modifier for `uptr` and
`sptr` as `SANITIZER_PTR_MOD`, and use it in the various format strings
in `sanitizer_symbolizer_markup_constants.h`.
---
.../lib/sanitizer_common/sanitizer_internal_defs.h | 3 +++
.../sanitizer_symbolizer_markup_constants.h | 13 ++++++++-----
2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
index 294e330c4d5611..a977ff8ff7ea7e 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
@@ -142,13 +142,16 @@ namespace __sanitizer {
// 64-bit Windows uses LLP64 data model.
typedef unsigned long long uptr;
typedef signed long long sptr;
+# define SANITIZER_PTR_MOD "ll"
#else
# if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE || SANITIZER_WINDOWS
typedef unsigned long uptr;
typedef signed long sptr;
+# define SANITIZER_PTR_MOD "l"
# else
typedef unsigned int uptr;
typedef signed int sptr;
+# define SANITIZER_PTR_MOD ""
# endif
#endif // defined(_WIN64)
#if defined(__x86_64__)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h
index 83643504e1289e..5a465a34718324 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h
@@ -26,20 +26,23 @@ constexpr const char *kFormatDemangle = "{{{symbol:%s}}}";
constexpr uptr kFormatDemangleMax = 1024; // Arbitrary.
// Function name or equivalent from PC location.
-constexpr const char *kFormatFunction = "{{{pc:%p}}}";
+constexpr const char *kFormatFunction = "{{{pc:0x%" SANITIZER_PTR_MOD "x}}}";
constexpr uptr kFormatFunctionMax = 64; // More than big enough for 64-bit hex.
// Global variable name or equivalent from data memory address.
-constexpr const char *kFormatData = "{{{data:%p}}}";
+constexpr const char *kFormatData = "{{{data:0x%" SANITIZER_PTR_MOD "x}}}";
// One frame in a backtrace (printed on a line by itself).
-constexpr const char *kFormatFrame = "{{{bt:%u:%p}}}";
+constexpr const char *kFormatFrame = "{{{bt:%d:0x%" SANITIZER_PTR_MOD "x}}}";
// Module contextual element.
-constexpr const char *kFormatModule = "{{{module:%d:%s:elf:%s}}}";
+constexpr const char *kFormatModule =
+ "{{{module:%" SANITIZER_PTR_MOD "d:%s:elf:%s}}}";
// mmap for a module segment.
-constexpr const char *kFormatMmap = "{{{mmap:%p:0x%x:load:%d:%s:0x%x}}}";
+constexpr const char *kFormatMmap =
+ "{{{mmap:0x%" SANITIZER_PTR_MOD "x:0x%" SANITIZER_PTR_MOD
+ "x:load:%" SANITIZER_PTR_MOD "d:%s:0x%" SANITIZER_PTR_MOD "x}}}";
// Dump trigger element.
#define FORMAT_DUMPFILE "{{{dumpfile:%s:%s}}}"
>From 8c45d7d78526e4953ee66a48cd8114218486d750 Mon Sep 17 00:00:00 2001
From: Dimitry Andric <dimitry at andric.com>
Date: Tue, 23 Apr 2024 21:53:23 +0200
Subject: [PATCH 2/2] Fix formatting.
---
compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
index a977ff8ff7ea7e..eb26f6e0b49693 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
@@ -142,7 +142,7 @@ namespace __sanitizer {
// 64-bit Windows uses LLP64 data model.
typedef unsigned long long uptr;
typedef signed long long sptr;
-# define SANITIZER_PTR_MOD "ll"
+# define SANITIZER_PTR_MOD "ll"
#else
# if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE || SANITIZER_WINDOWS
typedef unsigned long uptr;
More information about the llvm-commits
mailing list