aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/ExecutionEngine/JITLink/JITLinkTestUtils.h
blob: dc077f900d19585749744e8e324513423174e3a3 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//===--- JITLinkTestUtils.h - Utilities for JITLink unit tests --*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Utilities for JITLink unit tests.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_UNITTESTS_EXECUTIONENGINE_JITLINK_JITLINKTESTUTILS_H
#define LLVM_UNITTESTS_EXECUTIONENGINE_JITLINK_JITLINKTESTUTILS_H

#include "llvm/ExecutionEngine/JITLink/JITLink.h"

class MockJITLinkMemoryManager : public llvm::jitlink::JITLinkMemoryManager {
public:
  class Alloc {
  public:
    virtual ~Alloc() {}
  };

  class SimpleAlloc : public Alloc {
  public:
    SimpleAlloc(const llvm::jitlink::JITLinkDylib *JD,
                llvm::jitlink::LinkGraph &G) {
      for (auto *B : G.blocks())
        (void)B->getMutableContent(G);
    }
  };

  class MockInFlightAlloc : public InFlightAlloc {
  public:
    MockInFlightAlloc(MockJITLinkMemoryManager &MJMM, std::unique_ptr<Alloc> A)
        : MJMM(MJMM), A(std::move(A)) {}

    void abandon(OnAbandonedFunction OnAbandoned) override {
      OnAbandoned(MJMM.Abandon(std::move(A)));
    }

    void finalize(OnFinalizedFunction OnFinalized) override {
      OnFinalized(MJMM.Finalize(std::move(A)));
    }

  private:
    MockJITLinkMemoryManager &MJMM;
    std::unique_ptr<Alloc> A;
  };

  MockJITLinkMemoryManager() {
    Allocate = [this](const llvm::jitlink::JITLinkDylib *JD,
                      llvm::jitlink::LinkGraph &G) {
      return defaultAllocate(JD, G);
    };

    Deallocate = [this](std::vector<FinalizedAlloc> Allocs) {
      return defaultDeallocate(std::move(Allocs));
    };

    Abandon = [this](std::unique_ptr<Alloc> A) {
      return defaultAbandon(std::move(A));
    };

    Finalize = [this](std::unique_ptr<Alloc> A) {
      return defaultFinalize(std::move(A));
    };
  }

  void allocate(const llvm::jitlink::JITLinkDylib *JD,
                llvm::jitlink::LinkGraph &G,
                OnAllocatedFunction OnAllocated) override {
    auto A = Allocate(JD, G);
    if (!A)
      OnAllocated(A.takeError());
    else
      OnAllocated(std::make_unique<MockInFlightAlloc>(*this, std::move(*A)));
  }

  void deallocate(std::vector<FinalizedAlloc> Allocs,
                  OnDeallocatedFunction OnDeallocated) override {
    OnDeallocated(Deallocate(std::move(Allocs)));
  }

  using JITLinkMemoryManager::allocate;
  using JITLinkMemoryManager::deallocate;

  llvm::Expected<std::unique_ptr<Alloc>>
  defaultAllocate(const llvm::jitlink::JITLinkDylib *JD,
                  llvm::jitlink::LinkGraph &G) {
    return std::make_unique<SimpleAlloc>(JD, G);
  }

  llvm::Error defaultDeallocate(std::vector<FinalizedAlloc> Allocs) {
    for (auto &A : Allocs)
      delete A.release().toPtr<Alloc *>();
    return llvm::Error::success();
  }

  llvm::Error defaultAbandon(std::unique_ptr<Alloc> A) {
    return llvm::Error::success();
  }

  llvm::Expected<FinalizedAlloc> defaultFinalize(std::unique_ptr<Alloc> A) {
    return FinalizedAlloc(llvm::orc::ExecutorAddr::fromPtr(A.release()));
  }

  llvm::unique_function<llvm::Expected<std::unique_ptr<Alloc>>(
      const llvm::jitlink::JITLinkDylib *, llvm::jitlink::LinkGraph &)>
      Allocate;
  llvm::unique_function<llvm::Error(std::vector<FinalizedAlloc>)> Deallocate;
  llvm::unique_function<llvm::Error(std::unique_ptr<Alloc>)> Abandon;
  llvm::unique_function<llvm::Expected<FinalizedAlloc>(std::unique_ptr<Alloc>)>
      Finalize;
};

void lookupResolveEverythingToNull(
    const llvm::jitlink::JITLinkContext::LookupMap &Symbols,
    std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation> LC);

void lookupErrorOut(
    const llvm::jitlink::JITLinkContext::LookupMap &Symbols,
    std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation> LC);

class MockJITLinkContext : public llvm::jitlink::JITLinkContext {
public:
  using HandleFailedFn = llvm::unique_function<void(llvm::Error)>;

  MockJITLinkContext(std::unique_ptr<MockJITLinkMemoryManager> MJMM,
                     HandleFailedFn HandleFailed)
      : JITLinkContext(&JD), MJMM(std::move(MJMM)),
        HandleFailed(std::move(HandleFailed)) {}

  ~MockJITLinkContext() {
    if (auto Err = MJMM->deallocate(std::move(FinalizedAllocs)))
      notifyFailed(std::move(Err));
  }

  llvm::jitlink::JITLinkMemoryManager &getMemoryManager() override {
    return *MJMM;
  }

  void notifyFailed(llvm::Error Err) override { HandleFailed(std::move(Err)); }

  void lookup(const LookupMap &Symbols,
              std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation> LC)
      override {
    Lookup(Symbols, std::move(LC));
  }

  llvm::Error notifyResolved(llvm::jitlink::LinkGraph &G) override {
    return NotifyResolved ? NotifyResolved(G) : llvm::Error::success();
  }

  void notifyFinalized(
      llvm::jitlink::JITLinkMemoryManager::FinalizedAlloc Alloc) override {
    if (NotifyFinalized)
      NotifyFinalized(std::move(Alloc));
    else
      FinalizedAllocs.push_back(std::move(Alloc));
  }

  bool shouldAddDefaultTargetPasses(const llvm::Triple &TT) const override {
    return true;
  }

  llvm::jitlink::LinkGraphPassFunction
  getMarkLivePass(const llvm::Triple &TT) const override {
    return MarkLivePass ? llvm::jitlink::LinkGraphPassFunction(
                              [this](llvm::jitlink::LinkGraph &G) {
                                return MarkLivePass(G);
                              })
                        : llvm::jitlink::LinkGraphPassFunction(
                              [](llvm::jitlink::LinkGraph &G) {
                                return markAllSymbolsLive(G);
                              });
  }

  llvm::Error
  modifyPassConfig(llvm::jitlink::LinkGraph &G,
                   llvm::jitlink::PassConfiguration &Config) override {
    if (ModifyPassConfig)
      return ModifyPassConfig(G, Config);
    return llvm::Error::success();
  }

  llvm::jitlink::JITLinkDylib JD{"JD"};
  std::unique_ptr<MockJITLinkMemoryManager> MJMM;
  HandleFailedFn HandleFailed;
  llvm::unique_function<void(
      const LookupMap &,
      std::unique_ptr<llvm::jitlink::JITLinkAsyncLookupContinuation>)>
      Lookup;
  llvm::unique_function<llvm::Error(llvm::jitlink::LinkGraph &)> NotifyResolved;
  llvm::unique_function<void(
      llvm::jitlink::JITLinkMemoryManager::FinalizedAlloc)>
      NotifyFinalized;
  mutable llvm::unique_function<llvm::Error(llvm::jitlink::LinkGraph &)>
      MarkLivePass;
  llvm::unique_function<llvm::Error(llvm::jitlink::LinkGraph &,
                                    llvm::jitlink::PassConfiguration &)>
      ModifyPassConfig;

  std::vector<llvm::jitlink::JITLinkMemoryManager::FinalizedAlloc>
      FinalizedAllocs;
};

std::unique_ptr<MockJITLinkContext> makeMockContext(
    llvm::unique_function<void(llvm::Error)> HandleFailed,
    llvm::unique_function<void(MockJITLinkMemoryManager &)> SetupMemMgr,
    llvm::unique_function<void(MockJITLinkContext &)> SetupContext);

void defaultMemMgrSetup(MockJITLinkMemoryManager &);
void defaultCtxSetup(MockJITLinkContext &);

class JoinErrorsInto {
public:
  JoinErrorsInto(llvm::Error &Err) : Err(Err) {}
  void operator()(llvm::Error E2) {
    Err = llvm::joinErrors(std::move(Err), std::move(E2));
  }

private:
  llvm::Error &Err;
};

extern llvm::ArrayRef<char> BlockContent;

#endif // LLVM_UNITTESTS_EXECUTIONENGINE_JITLINK_JITLINKTESTUTILS_H