[compiler-rt] [sanitizer_symbolizer] Use correct format strings for uptr (PR #89815)

Dimitry Andric via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 24 09:12:25 PDT 2024


https://github.com/DimitryAndric updated https://github.com/llvm/llvm-project/pull/89815

>From 409b4070c6adbdfbf851990c8b3d97715413369a Mon Sep 17 00:00:00 2001
From: Dimitry Andric <dimitry at andric.com>
Date: Wed, 24 Apr 2024 18:09:06 +0200
Subject: [PATCH] [sanitizer_symbolizer] Cast arguments for format strings in
 markup

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, cast the arguments to the expected type of the
format strings.
---
 .../sanitizer_symbolizer_markup.cpp              | 16 ++++++++++------
 .../sanitizer_symbolizer_markup_constants.h      |  2 +-
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp
index b2a1069a9a61cc..22633cb1f0af0d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup.cpp
@@ -28,7 +28,7 @@ void MarkupStackTracePrinter::RenderData(InternalScopedString *buffer,
                                          const char *format, const DataInfo *DI,
                                          const char *strip_path_prefix) {
   RenderContext(buffer);
-  buffer->AppendF(kFormatData, DI->start);
+  buffer->AppendF(kFormatData, reinterpret_cast<void *>(DI->start));
 }
 
 bool MarkupStackTracePrinter::RenderNeedsSymbolization(const char *format) {
@@ -43,12 +43,13 @@ void MarkupStackTracePrinter::RenderFrame(InternalScopedString *buffer,
                                           const char *strip_path_prefix) {
   CHECK(!RenderNeedsSymbolization(format));
   RenderContext(buffer);
-  buffer->AppendF(kFormatFrame, frame_no, address);
+  buffer->AppendF(kFormatFrame, frame_no, reinterpret_cast<void *>(address));
 }
 
 bool MarkupSymbolizerTool::SymbolizePC(uptr addr, SymbolizedStack *stack) {
   char buffer[kFormatFunctionMax];
-  internal_snprintf(buffer, sizeof(buffer), kFormatFunction, addr);
+  internal_snprintf(buffer, sizeof(buffer), kFormatFunction,
+                    reinterpret_cast<void *>(addr));
   stack->info.function = internal_strdup(buffer);
   return true;
 }
@@ -96,7 +97,7 @@ static void RenderModule(InternalScopedString *buffer,
   for (uptr i = 0; i < module.uuid_size(); i++)
     buildIdBuffer.AppendF("%02x", module.uuid()[i]);
 
-  buffer->AppendF(kFormatModule, moduleId, module.full_name(),
+  buffer->AppendF(kFormatModule, static_cast<int>(moduleId), module.full_name(),
                   buildIdBuffer.data());
   buffer->Append("\n");
 }
@@ -118,8 +119,11 @@ static void RenderMmaps(InternalScopedString *buffer,
     // module.base_address == dlpi_addr
     // range.beg == dlpi_addr + p_vaddr
     // relative address == p_vaddr == range.beg - module.base_address
-    buffer->AppendF(kFormatMmap, range.beg, range.end - range.beg, moduleId,
-                    accessBuffer.data(), range.beg - module.base_address());
+    buffer->AppendF(
+        kFormatMmap, reinterpret_cast<void *>(range.beg),
+        static_cast<unsigned int>(range.end - range.beg),
+        static_cast<int>(moduleId), accessBuffer.data(),
+        static_cast<unsigned int>(range.beg - module.base_address()));
 
     buffer->Append("\n");
     accessBuffer.clear();
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..4f309f38809091 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_markup_constants.h
@@ -33,7 +33,7 @@ constexpr uptr kFormatFunctionMax = 64;  // More than big enough for 64-bit hex.
 constexpr const char *kFormatData = "{{{data:%p}}}";
 
 // One frame in a backtrace (printed on a line by itself).
-constexpr const char *kFormatFrame = "{{{bt:%u:%p}}}";
+constexpr const char *kFormatFrame = "{{{bt:%d:%p}}}";
 
 // Module contextual element.
 constexpr const char *kFormatModule = "{{{module:%d:%s:elf:%s}}}";



More information about the llvm-commits mailing list