From 1c53cadf08c07fb3fa70993c6210355c58c3b149 Mon Sep 17 00:00:00 2001 From: Ben Langmuir Date: Tue, 24 Aug 2021 11:10:11 -0700 Subject: [PATCH] [orc] Fix unit tests that use ORC C API * c_api_tests was failing to build after the API change to __orc_rt_CWrapperFunctionResultAllocate * wrapper_function_utils_test was causing an assertion failure, because it was creating a result for `void(void)` with Size = 0, but seeing an uninitialized pointer, which it considered to be an out-of-bound error. I noticed locally that making modifications to c_api.h is not causing these unit tests to be rebuilt, which may be how the bug slipped in in the first place. Differential Revision: https://reviews.llvm.org/D108649 --- compiler-rt/lib/orc/c_api.h | 2 ++ compiler-rt/lib/orc/unittests/c_api_test.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/compiler-rt/lib/orc/c_api.h b/compiler-rt/lib/orc/c_api.h index 18eb96d..47f46b8 100644 --- a/compiler-rt/lib/orc/c_api.h +++ b/compiler-rt/lib/orc/c_api.h @@ -95,6 +95,8 @@ static inline __orc_rt_CWrapperFunctionResult __orc_rt_CWrapperFunctionResultAllocate(size_t Size) { __orc_rt_CWrapperFunctionResult R; R.Size = Size; + // If Size is 0 ValuePtr must be 0 or it is considered an out-of-band error. + R.Data.ValuePtr = 0; if (Size > sizeof(R.Data.Value)) R.Data.ValuePtr = (char *)malloc(Size); return R; diff --git a/compiler-rt/lib/orc/unittests/c_api_test.cpp b/compiler-rt/lib/orc/unittests/c_api_test.cpp index 4583feb..a14ea18 100644 --- a/compiler-rt/lib/orc/unittests/c_api_test.cpp +++ b/compiler-rt/lib/orc/unittests/c_api_test.cpp @@ -30,8 +30,8 @@ TEST(CAPITest, CWrapperFunctionResultInit) { TEST(CAPITest, CWrapperFunctionResultAllocSmall) { constexpr size_t SmallAllocSize = sizeof(const char *); - __orc_rt_CWrapperFunctionResult R; - char *DataPtr = __orc_rt_CWrapperFunctionResultAllocate(&R, SmallAllocSize); + auto R = __orc_rt_CWrapperFunctionResultAllocate(SmallAllocSize); + char *DataPtr = __orc_rt_CWrapperFunctionResultData(&R); for (size_t I = 0; I != SmallAllocSize; ++I) DataPtr[I] = 0x55 + I; @@ -60,8 +60,8 @@ TEST(CAPITest, CWrapperFunctionResultAllocSmall) { TEST(CAPITest, CWrapperFunctionResultAllocLarge) { constexpr size_t LargeAllocSize = sizeof(const char *) + 1; - __orc_rt_CWrapperFunctionResult R; - char *DataPtr = __orc_rt_CWrapperFunctionResultAllocate(&R, LargeAllocSize); + auto R = __orc_rt_CWrapperFunctionResultAllocate(LargeAllocSize); + char *DataPtr = __orc_rt_CWrapperFunctionResultData(&R); for (size_t I = 0; I != LargeAllocSize; ++I) DataPtr[I] = 0x55 + I; -- 2.7.4