[Support] Fix LEB128 test when building with MSVC (#93184)

The VALUE expansion might be compiled in the different ways, because of
string pooling which isn't always enabled/guaranteed. When building with
MSVC, previously I was seeing for example empty strings `""` pointing to
different addresses, thus the negative offsets below in the log.

Previous test log:
```
Note: Google Test filter = LEB128Test.DecodeInvalidULEB128
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from LEB128Test
[ RUN      ] LEB128Test.DecodeInvalidULEB128
C:\src\git\llvm-project\llvm\unittests\Support\LEB128Test.cpp(167): error: Expected equality of these values:
  0u
    Which is: 0
  Value - reinterpret_cast<const uint8_t *>("")
    Which is: -5

C:\src\git\llvm-project\llvm\unittests\Support\LEB128Test.cpp(168): error: Expected equality of these values:
  1u
    Which is: 1
  Value - reinterpret_cast<const uint8_t *>("\x80")
    Which is: -167

C:\src\git\llvm-project\llvm\unittests\Support\LEB128Test.cpp(171): error: Expected equality of these values:
  9u
    Which is: 9
  Value - reinterpret_cast<const uint8_t *>("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x02")
    Which is: -167

C:\src\git\llvm-project\llvm\unittests\Support\LEB128Test.cpp(172): error: Expected equality of these values:
  10u
    Which is: 10
  Value - reinterpret_cast<const uint8_t *>("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x02")
    Which is: -166

[  FAILED  ] LEB128Test.DecodeInvalidULEB128 (2 ms)
[----------] 1 test from LEB128Test (2 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (4 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] LEB128Test.DecodeInvalidULEB128

 1 FAILED TEST
```
This commit is contained in:
Alexandre Ganea 2024-05-25 09:13:02 -04:00 committed by GitHub
parent 4ecbfacf9e
commit 56d319e7e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -147,7 +147,8 @@ TEST(LEB128Test, DecodeULEB128) {
TEST(LEB128Test, DecodeInvalidULEB128) {
#define EXPECT_INVALID_ULEB128(VALUE, ERROR_OFFSET) \
do { \
const uint8_t *Value = reinterpret_cast<const uint8_t *>(VALUE); \
const char *DefaultValue = VALUE; \
const uint8_t *Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
const char *Error = nullptr; \
unsigned ErrorOffset = 0; \
uint64_t Actual = \
@ -155,12 +156,13 @@ TEST(LEB128Test, DecodeInvalidULEB128) {
EXPECT_NE(Error, nullptr); \
EXPECT_EQ(0ul, Actual); \
EXPECT_EQ(ERROR_OFFSET, ErrorOffset); \
Value = reinterpret_cast<const uint8_t *>(VALUE); \
Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
Error = nullptr; \
Actual = decodeULEB128AndInc(Value, Value + strlen(VALUE), &Error); \
EXPECT_NE(Error, nullptr); \
EXPECT_EQ(0ul, Actual); \
EXPECT_EQ(ERROR_OFFSET, Value - reinterpret_cast<const uint8_t *>(VALUE)); \
EXPECT_EQ(ERROR_OFFSET, \
Value - reinterpret_cast<const uint8_t *>(DefaultValue)); \
} while (0)
// Buffer overflow.
@ -222,7 +224,8 @@ TEST(LEB128Test, DecodeSLEB128) {
TEST(LEB128Test, DecodeInvalidSLEB128) {
#define EXPECT_INVALID_SLEB128(VALUE, ERROR_OFFSET) \
do { \
const uint8_t *Value = reinterpret_cast<const uint8_t *>(VALUE); \
const char *DefaultValue = VALUE; \
const uint8_t *Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
const char *Error = nullptr; \
unsigned ErrorOffset = 0; \
uint64_t Actual = \
@ -230,12 +233,13 @@ TEST(LEB128Test, DecodeInvalidSLEB128) {
EXPECT_NE(Error, nullptr); \
EXPECT_EQ(0ul, Actual); \
EXPECT_EQ(ERROR_OFFSET, ErrorOffset); \
Value = reinterpret_cast<const uint8_t *>(VALUE); \
Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
Error = nullptr; \
Actual = decodeSLEB128AndInc(Value, Value + strlen(VALUE), &Error); \
EXPECT_NE(Error, nullptr); \
EXPECT_EQ(0ul, Actual); \
EXPECT_EQ(ERROR_OFFSET, Value - reinterpret_cast<const uint8_t *>(VALUE)); \
EXPECT_EQ(ERROR_OFFSET, \
Value - reinterpret_cast<const uint8_t *>(DefaultValue)); \
} while (0)
// Buffer overflow.
@ -257,7 +261,9 @@ TEST(LEB128Test, DecodeInvalidSLEB128) {
TEST(LEB128Test, DecodeAndInc) {
#define EXPECT_LEB128(FUN, VALUE, SIZE) \
do { \
const uint8_t *V = reinterpret_cast<const uint8_t *>(VALUE), *P = V; \
const char *DefaultValue = VALUE; \
const uint8_t *V = reinterpret_cast<const uint8_t *>(DefaultValue), \
*P = V; \
auto Expected = FUN(P), Actual = FUN##AndInc(P, P + strlen(VALUE)); \
EXPECT_EQ(Actual, Expected); \
EXPECT_EQ(P - V, SIZE); \