aboutsummaryrefslogtreecommitdiff
path: root/orc-rt/unittests/AllocActionTest.cpp
blob: 08e2a64cde331f7b5302c147d4740130549dba3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===- AllocActionTest.cpp ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Tests for orc-rt's AllocAction.h APIs.
//
//===----------------------------------------------------------------------===//

#include "orc-rt/AllocAction.h"
#include "orc-rt/ExecutorAddress.h"
#include "orc-rt/SPSAllocAction.h"

#include "AllocActionTestUtils.h"
#include "gtest/gtest.h"

using namespace orc_rt;

TEST(AllocActionTest, DefaultConstruct) {
  AllocAction AA;
  EXPECT_FALSE(AA);
}

static orc_rt_WrapperFunctionBuffer noopAction(const char *ArgData,
                                               size_t ArgSize) {
  return WrapperFunctionBuffer().release();
}

TEST(AllocActionTest, ConstructWithAction) {
  AllocAction AA(noopAction, WrapperFunctionBuffer());
  EXPECT_TRUE(AA);
}

// Increments int via pointer.
static orc_rt_WrapperFunctionBuffer
increment_sps_allocaction(const char *ArgData, size_t ArgSize) {
  return SPSAllocActionFunction<SPSExecutorAddr>::handle(
             ArgData, ArgSize,
             [](ExecutorAddr IntPtr) {
               *IntPtr.toPtr<int *>() += 1;
               return WrapperFunctionBuffer();
             })
      .release();
}

// Increments int via pointer.
static orc_rt_WrapperFunctionBuffer
decrement_sps_allocaction(const char *ArgData, size_t ArgSize) {
  return SPSAllocActionFunction<SPSExecutorAddr>::handle(
             ArgData, ArgSize,
             [](ExecutorAddr IntPtr) {
               *IntPtr.toPtr<int *>() -= 1;
               return WrapperFunctionBuffer();
             })
      .release();
}

template <typename T>
static WrapperFunctionBuffer makeExecutorAddrBuffer(T *P) {
  return *spsSerialize<SPSArgList<SPSExecutorAddr>>(ExecutorAddr::fromPtr(P));
}

TEST(AllocActionTest, RunBasicAction) {
  int Val = 0;
  AllocAction IncVal(increment_sps_allocaction, makeExecutorAddrBuffer(&Val));
  EXPECT_TRUE(IncVal);
  auto B = IncVal();
  EXPECT_TRUE(B.empty());
  EXPECT_EQ(Val, 1);
}

TEST(AllocActionTest, RunFinalizationActionsComplete) {
  int Val = 0;

  std::vector<AllocActionPair> InitialActions;

  auto MakeAAOnVal = [&](AllocActionFn Fn) {
    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
                                                   ExecutorAddr::fromPtr(&Val));
  };
  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
                            MakeAAOnVal(decrement_sps_allocaction)});
  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
                            MakeAAOnVal(decrement_sps_allocaction)});

  auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));

  EXPECT_EQ(Val, 2);

  runDeallocActions(std::move(DeallocActions));

  EXPECT_EQ(Val, 0);
}

static orc_rt_WrapperFunctionBuffer fail_sps_allocaction(const char *ArgData,
                                                         size_t ArgSize) {
  return WrapperFunctionBuffer::createOutOfBandError("failed").release();
}

TEST(AllocActionTest, RunFinalizeActionsFail) {
  int Val = 0;

  std::vector<AllocActionPair> InitialActions;

  auto MakeAAOnVal = [&](AllocActionFn Fn) {
    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
                                                   ExecutorAddr::fromPtr(&Val));
  };
  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
                            MakeAAOnVal(decrement_sps_allocaction)});
  InitialActions.push_back({*MakeAllocAction<>::from(fail_sps_allocaction),
                            MakeAAOnVal(decrement_sps_allocaction)});

  auto DeallocActions = runFinalizeActions(std::move(InitialActions));

  if (DeallocActions) {
    ADD_FAILURE() << "Failed to report error from runFinalizeActions";
    return;
  }

  EXPECT_EQ(toString(DeallocActions.takeError()), std::string("failed"));

  // Check that we ran the decrement corresponding to the first increment.
  EXPECT_EQ(Val, 0);
}

TEST(AllocActionTest, RunFinalizeActionsNullFinalize) {
  int Val = 0;

  std::vector<AllocActionPair> InitialActions;

  auto MakeAAOnVal = [&](AllocActionFn Fn) {
    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
                                                   ExecutorAddr::fromPtr(&Val));
  };
  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
                            MakeAAOnVal(decrement_sps_allocaction)});
  InitialActions.push_back({*MakeAllocAction<>::from(nullptr),
                            MakeAAOnVal(decrement_sps_allocaction)});

  auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));

  // Both dealloc actions should be included in the returned list, despite one
  // of them having a null finalize action.
  EXPECT_EQ(DeallocActions.size(), 2U);

  runDeallocActions(std::move(DeallocActions));

  EXPECT_EQ(Val, -1);
}

TEST(AllocActionTest, RunFinalizeActionsNullDealloc) {
  int Val = 0;

  std::vector<AllocActionPair> InitialActions;

  auto MakeAAOnVal = [&](AllocActionFn Fn) {
    return *MakeAllocAction<SPSExecutorAddr>::from(Fn,
                                                   ExecutorAddr::fromPtr(&Val));
  };
  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
                            MakeAAOnVal(decrement_sps_allocaction)});
  InitialActions.push_back({MakeAAOnVal(increment_sps_allocaction),
                            *MakeAllocAction<>::from(nullptr)});

  auto DeallocActions = cantFail(runFinalizeActions(std::move(InitialActions)));

  // Null dealloc actions should be filtered out of the returned list.
  EXPECT_EQ(DeallocActions.size(), 1U);

  runDeallocActions(std::move(DeallocActions));

  EXPECT_EQ(Val, 1);
}