Skip to content

Commit 56d319e

Browse files
authored
[Support] Fix LEB128 test when building with MSVC (llvm#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 ```
1 parent 4ecbfac commit 56d319e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

llvm/unittests/Support/LEB128Test.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,22 @@ TEST(LEB128Test, DecodeULEB128) {
147147
TEST(LEB128Test, DecodeInvalidULEB128) {
148148
#define EXPECT_INVALID_ULEB128(VALUE, ERROR_OFFSET) \
149149
do { \
150-
const uint8_t *Value = reinterpret_cast<const uint8_t *>(VALUE); \
150+
const char *DefaultValue = VALUE; \
151+
const uint8_t *Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
151152
const char *Error = nullptr; \
152153
unsigned ErrorOffset = 0; \
153154
uint64_t Actual = \
154155
decodeULEB128(Value, &ErrorOffset, Value + strlen(VALUE), &Error); \
155156
EXPECT_NE(Error, nullptr); \
156157
EXPECT_EQ(0ul, Actual); \
157158
EXPECT_EQ(ERROR_OFFSET, ErrorOffset); \
158-
Value = reinterpret_cast<const uint8_t *>(VALUE); \
159+
Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
159160
Error = nullptr; \
160161
Actual = decodeULEB128AndInc(Value, Value + strlen(VALUE), &Error); \
161162
EXPECT_NE(Error, nullptr); \
162163
EXPECT_EQ(0ul, Actual); \
163-
EXPECT_EQ(ERROR_OFFSET, Value - reinterpret_cast<const uint8_t *>(VALUE)); \
164+
EXPECT_EQ(ERROR_OFFSET, \
165+
Value - reinterpret_cast<const uint8_t *>(DefaultValue)); \
164166
} while (0)
165167

166168
// Buffer overflow.
@@ -222,20 +224,22 @@ TEST(LEB128Test, DecodeSLEB128) {
222224
TEST(LEB128Test, DecodeInvalidSLEB128) {
223225
#define EXPECT_INVALID_SLEB128(VALUE, ERROR_OFFSET) \
224226
do { \
225-
const uint8_t *Value = reinterpret_cast<const uint8_t *>(VALUE); \
227+
const char *DefaultValue = VALUE; \
228+
const uint8_t *Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
226229
const char *Error = nullptr; \
227230
unsigned ErrorOffset = 0; \
228231
uint64_t Actual = \
229232
decodeSLEB128(Value, &ErrorOffset, Value + strlen(VALUE), &Error); \
230233
EXPECT_NE(Error, nullptr); \
231234
EXPECT_EQ(0ul, Actual); \
232235
EXPECT_EQ(ERROR_OFFSET, ErrorOffset); \
233-
Value = reinterpret_cast<const uint8_t *>(VALUE); \
236+
Value = reinterpret_cast<const uint8_t *>(DefaultValue); \
234237
Error = nullptr; \
235238
Actual = decodeSLEB128AndInc(Value, Value + strlen(VALUE), &Error); \
236239
EXPECT_NE(Error, nullptr); \
237240
EXPECT_EQ(0ul, Actual); \
238-
EXPECT_EQ(ERROR_OFFSET, Value - reinterpret_cast<const uint8_t *>(VALUE)); \
241+
EXPECT_EQ(ERROR_OFFSET, \
242+
Value - reinterpret_cast<const uint8_t *>(DefaultValue)); \
239243
} while (0)
240244

241245
// Buffer overflow.
@@ -257,7 +261,9 @@ TEST(LEB128Test, DecodeInvalidSLEB128) {
257261
TEST(LEB128Test, DecodeAndInc) {
258262
#define EXPECT_LEB128(FUN, VALUE, SIZE) \
259263
do { \
260-
const uint8_t *V = reinterpret_cast<const uint8_t *>(VALUE), *P = V; \
264+
const char *DefaultValue = VALUE; \
265+
const uint8_t *V = reinterpret_cast<const uint8_t *>(DefaultValue), \
266+
*P = V; \
261267
auto Expected = FUN(P), Actual = FUN##AndInc(P, P + strlen(VALUE)); \
262268
EXPECT_EQ(Actual, Expected); \
263269
EXPECT_EQ(P - V, SIZE); \

0 commit comments

Comments
 (0)