From 2bc07083a258fdbbafc9c0381e936f441f93af70 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Tue, 13 Jul 2021 10:44:53 -0700 Subject: [PATCH] [sanitizer] Fix VSNPrintf %V on Windows --- compiler-rt/lib/sanitizer_common/sanitizer_common.h | 9 ++++++++- compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp | 13 +++++-------- .../lib/sanitizer_common/tests/sanitizer_printf_test.cpp | 6 +++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 64c15f1..4336fd6 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -990,7 +990,7 @@ struct SignalContext { }; void InitializePlatformEarly(); -void MaybeReexec(); +void MaybeReexec(); template class RunOnDestruction { @@ -1057,6 +1057,13 @@ class ArrayRef { T *end_ = nullptr; }; +#define PRINTF_128(v) \ + (*((u8 *)&v + 0)), (*((u8 *)&v + 1)), (*((u8 *)&v + 2)), (*((u8 *)&v + 3)), \ + (*((u8 *)&v + 4)), (*((u8 *)&v + 5)), (*((u8 *)&v + 6)), \ + (*((u8 *)&v + 7)), (*((u8 *)&v + 8)), (*((u8 *)&v + 9)), \ + (*((u8 *)&v + 10)), (*((u8 *)&v + 11)), (*((u8 *)&v + 12)), \ + (*((u8 *)&v + 13)), (*((u8 *)&v + 14)), (*((u8 *)&v + 15)) + } // namespace __sanitizer inline void *operator new(__sanitizer::operator_new_size_type size, diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp index cf45481..b913c92 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp @@ -132,7 +132,7 @@ static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) { int VSNPrintf(char *buff, int buff_length, const char *format, va_list args) { static const char *kPrintfFormatsHelp = - "Supported Printf formats: %([0-9]*)?(z|ll)?{d,u,x,X}; %p; " + "Supported Printf formats: %([0-9]*)?(z|ll)?{d,u,x,X,V}; %p; " "%[-]([0-9]*)?(\\.\\*)?s; %c\n"; RAW_CHECK(format); RAW_CHECK(buff_length > 0); @@ -190,16 +190,13 @@ int VSNPrintf(char *buff, int buff_length, width, pad_with_zero, uppercase); break; } -#if defined(__x86_64__) case 'V': { - __m128i v = va_arg(args, __m128i); - u8 x[sizeof(v)]; - internal_memcpy(x, &v, sizeof(x)); - for (uptr i = 0; i < sizeof(x); i++) - result += AppendUnsigned(&buff, buff_end, x[i], 16, 2, true, false); + for (uptr i = 0; i < 16; i++) { + unsigned x = va_arg(args, unsigned); + result += AppendUnsigned(&buff, buff_end, x, 16, 2, true, false); + } break; } -#endif case 'p': { RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp); result += AppendPointer(&buff, buff_end, va_arg(args, uptr)); diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_printf_test.cpp b/compiler-rt/lib/sanitizer_common/tests/sanitizer_printf_test.cpp index c47dff1..7a3724c 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_printf_test.cpp +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_printf_test.cpp @@ -162,12 +162,12 @@ TEST(Printf, Precision) { TEST(Printf, M128) { __m128i v = _mm_set_epi32(0x12345678, 0x0a0a0a0a, 0xb0b0b0b0, 0xaabbccdd); char buf[128]; - internal_snprintf(buf, sizeof(buf), "%V", v); + internal_snprintf(buf, sizeof(buf), "%V", PRINTF_128(v)); EXPECT_STREQ("ddccbbaab0b0b0b00a0a0a0a78563412", buf); v = _mm_cvtsi32_si128(0x12345678); - internal_snprintf(buf, sizeof(buf), "%V", v); + internal_snprintf(buf, sizeof(buf), "%V", PRINTF_128(v)); EXPECT_STREQ("78563412000000000000000000000000", buf); - internal_snprintf(buf, sizeof(buf), "%d %V", 0, v); + internal_snprintf(buf, sizeof(buf), "%d %V", 0, PRINTF_128(v)); EXPECT_STREQ("0 78563412000000000000000000000000", buf); } #endif -- 2.7.4