[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
This commit is contained in:
Ben Langmuir 2021-08-24 11:10:11 -07:00
parent 35b0b1a64a
commit 1c53cadf08
2 changed files with 6 additions and 4 deletions

View File

@ -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;

View File

@ -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;