|
| 1 | +//===- AllocActionTest.cpp ------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Tests for orc-rt's AllocAction.h APIs. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "orc-rt/AllocAction.h" |
| 14 | +#include "orc-rt/ExecutorAddress.h" |
| 15 | +#include "orc-rt/SPSAllocAction.h" |
| 16 | + |
| 17 | +#include "SimplePackedSerializationTestUtils.h" |
| 18 | +#include "gtest/gtest.h" |
| 19 | + |
| 20 | +using namespace orc_rt; |
| 21 | + |
| 22 | +TEST(AllocActionTest, DefaultConstruct) { |
| 23 | + AllocAction AA; |
| 24 | + EXPECT_FALSE(AA); |
| 25 | +} |
| 26 | + |
| 27 | +static orc_rt_WrapperFunctionBuffer noopAction(const char *ArgData, |
| 28 | + size_t ArgSize) { |
| 29 | + return WrapperFunctionBuffer().release(); |
| 30 | +} |
| 31 | + |
| 32 | +TEST(AllocActionTest, ConstructWithAction) { |
| 33 | + AllocAction AA(noopAction, WrapperFunctionBuffer()); |
| 34 | + EXPECT_TRUE(AA); |
| 35 | +} |
| 36 | + |
| 37 | +// Increments int via pointer. |
| 38 | +static orc_rt_WrapperFunctionBuffer |
| 39 | +increment_sps_allocaction(const char *ArgData, size_t ArgSize) { |
| 40 | + return SPSAllocActionFunction<SPSExecutorAddr>::handle( |
| 41 | + ArgData, ArgSize, |
| 42 | + [](ExecutorAddr IntPtr) { |
| 43 | + *IntPtr.toPtr<int *>() += 1; |
| 44 | + return WrapperFunctionBuffer(); |
| 45 | + }) |
| 46 | + .release(); |
| 47 | +} |
| 48 | + |
| 49 | +// Increments int via pointer. |
| 50 | +static orc_rt_WrapperFunctionBuffer |
| 51 | +decrement_sps_allocaction(const char *ArgData, size_t ArgSize) { |
| 52 | + return SPSAllocActionFunction<SPSExecutorAddr>::handle( |
| 53 | + ArgData, ArgSize, |
| 54 | + [](ExecutorAddr IntPtr) { |
| 55 | + *IntPtr.toPtr<int *>() -= 1; |
| 56 | + return WrapperFunctionBuffer(); |
| 57 | + }) |
| 58 | + .release(); |
| 59 | +} |
| 60 | + |
| 61 | +template <typename T> |
| 62 | +static WrapperFunctionBuffer makeExecutorAddrBuffer(T *P) { |
| 63 | + return *spsSerialize<SPSArgList<SPSExecutorAddr>>(ExecutorAddr::fromPtr(P)); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(AllocActionTest, RunBasicAction) { |
| 67 | + int Val = 0; |
| 68 | + AllocAction IncVal(increment_sps_allocaction, makeExecutorAddrBuffer(&Val)); |
| 69 | + EXPECT_TRUE(IncVal); |
| 70 | + auto B = IncVal(); |
| 71 | + EXPECT_TRUE(B.empty()); |
| 72 | + EXPECT_EQ(Val, 1); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(AllocActionTest, RunFinalizationActionsComplete) { |
| 76 | + int Val = 0; |
| 77 | + |
| 78 | + std::vector<AllocActionPair> InitialActions; |
| 79 | + |
| 80 | + auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); }; |
| 81 | + InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()}, |
| 82 | + {decrement_sps_allocaction, MakeArgBuffer()}}); |
| 83 | + InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()}, |
| 84 | + {decrement_sps_allocaction, MakeArgBuffer()}}); |
| 85 | + |
| 86 | + auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions))); |
| 87 | + |
| 88 | + EXPECT_EQ(Val, 2); |
| 89 | + |
| 90 | + runDeallocActions(std::move(DeallocActions)); |
| 91 | + |
| 92 | + EXPECT_EQ(Val, 0); |
| 93 | +} |
| 94 | + |
| 95 | +static orc_rt_WrapperFunctionBuffer fail_sps_allocaction(const char *ArgData, |
| 96 | + size_t ArgSize) { |
| 97 | + return WrapperFunctionBuffer::createOutOfBandError("failed").release(); |
| 98 | +} |
| 99 | + |
| 100 | +TEST(AllocActionTest, RunFinalizeActionsFail) { |
| 101 | + int Val = 0; |
| 102 | + |
| 103 | + std::vector<AllocActionPair> InitialActions; |
| 104 | + |
| 105 | + auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); }; |
| 106 | + InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()}, |
| 107 | + {decrement_sps_allocaction, MakeArgBuffer()}}); |
| 108 | + InitialActions.push_back({{fail_sps_allocaction, MakeArgBuffer()}, |
| 109 | + {decrement_sps_allocaction, MakeArgBuffer()}}); |
| 110 | + |
| 111 | + auto DeallocActions = runFinalizeActions(std::move(InitialActions)); |
| 112 | + |
| 113 | + if (DeallocActions) { |
| 114 | + ADD_FAILURE() << "Failed to report error from runFinalizeActions"; |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + EXPECT_EQ(toString(DeallocActions.takeError()), std::string("failed")); |
| 119 | + |
| 120 | + // Check that we ran the decrement corresponding to the first increment. |
| 121 | + EXPECT_EQ(Val, 0); |
| 122 | +} |
| 123 | + |
| 124 | +TEST(AllocActionTest, RunFinalizeActionsNullFinalize) { |
| 125 | + int Val = 0; |
| 126 | + |
| 127 | + std::vector<AllocActionPair> InitialActions; |
| 128 | + |
| 129 | + auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); }; |
| 130 | + InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()}, |
| 131 | + {decrement_sps_allocaction, MakeArgBuffer()}}); |
| 132 | + InitialActions.push_back({{nullptr, WrapperFunctionBuffer()}, |
| 133 | + {decrement_sps_allocaction, MakeArgBuffer()}}); |
| 134 | + |
| 135 | + auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions))); |
| 136 | + |
| 137 | + // Both dealloc actions should be included in the returned list, despite one |
| 138 | + // of them having a null finalize action. |
| 139 | + EXPECT_EQ(DeallocActions.size(), 2U); |
| 140 | + |
| 141 | + runDeallocActions(std::move(DeallocActions)); |
| 142 | + |
| 143 | + EXPECT_EQ(Val, -1); |
| 144 | +} |
| 145 | + |
| 146 | +TEST(AllocActionTest, RunFinalizeActionsNullDealloc) { |
| 147 | + int Val = 0; |
| 148 | + |
| 149 | + std::vector<AllocActionPair> InitialActions; |
| 150 | + |
| 151 | + auto MakeArgBuffer = [&]() { return makeExecutorAddrBuffer(&Val); }; |
| 152 | + InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()}, |
| 153 | + {decrement_sps_allocaction, MakeArgBuffer()}}); |
| 154 | + InitialActions.push_back({{increment_sps_allocaction, MakeArgBuffer()}, |
| 155 | + {nullptr, WrapperFunctionBuffer()}}); |
| 156 | + |
| 157 | + auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions))); |
| 158 | + |
| 159 | + // Null dealloc actions should be filtered out of the returned list. |
| 160 | + EXPECT_EQ(DeallocActions.size(), 1U); |
| 161 | + |
| 162 | + runDeallocActions(std::move(DeallocActions)); |
| 163 | + |
| 164 | + EXPECT_EQ(Val, 1); |
| 165 | +} |
0 commit comments