xref: /llvm-project/llvm/unittests/ExecutionEngine/Orc/LookupAndRecordAddrsTest.cpp (revision dc11c0601577afb8f67513d041ee25dabe3555b9)
164288571SLang Hames //===- LookupAndRecordAddrsTest.cpp - Unit tests for LookupAndRecordAddrs -===//
264288571SLang Hames //
364288571SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
464288571SLang Hames // See https://llvm.org/LICENSE.txt for license information.
564288571SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
664288571SLang Hames //
764288571SLang Hames //===----------------------------------------------------------------------===//
864288571SLang Hames 
964288571SLang Hames #include "llvm/ExecutionEngine/Orc/LookupAndRecordAddrs.h"
10*dc11c060SLang Hames #include "OrcTestCommon.h"
11*dc11c060SLang Hames #include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
1264288571SLang Hames #include "llvm/Support/MSVCErrorWorkarounds.h"
1364288571SLang Hames #include "llvm/Testing/Support/Error.h"
1464288571SLang Hames 
1564288571SLang Hames #include <future>
1664288571SLang Hames 
1764288571SLang Hames using namespace llvm;
1864288571SLang Hames using namespace llvm::orc;
1964288571SLang Hames 
2064288571SLang Hames class LookupAndRecordAddrsTest : public CoreAPIsBasedStandardTest {};
2164288571SLang Hames 
2264288571SLang Hames namespace {
2364288571SLang Hames 
2464288571SLang Hames TEST_F(LookupAndRecordAddrsTest, AsyncRequiredSuccess) {
2564288571SLang Hames   cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
2664288571SLang Hames 
278b1771bdSLang Hames   ExecutorAddr ReturnedFooAddr, ReturnedBarAddr;
2864288571SLang Hames   std::promise<MSVCPError> ErrP;
2964288571SLang Hames 
3064288571SLang Hames   lookupAndRecordAddrs([&](Error Err) { ErrP.set_value(std::move(Err)); }, ES,
3164288571SLang Hames                        LookupKind::Static, makeJITDylibSearchOrder(&JD),
328b1771bdSLang Hames                        {{Foo, &ReturnedFooAddr}, {Bar, &ReturnedBarAddr}});
3364288571SLang Hames 
3464288571SLang Hames   Error Err = ErrP.get_future().get();
3564288571SLang Hames 
3664288571SLang Hames   EXPECT_THAT_ERROR(std::move(Err), Succeeded());
378b1771bdSLang Hames   EXPECT_EQ(ReturnedFooAddr, FooAddr);
388b1771bdSLang Hames   EXPECT_EQ(ReturnedBarAddr, BarAddr);
3964288571SLang Hames }
4064288571SLang Hames 
4164288571SLang Hames TEST_F(LookupAndRecordAddrsTest, AsyncRequiredFailure) {
428b1771bdSLang Hames   ExecutorAddr RecordedFooAddr, RecordedBarAddr;
4364288571SLang Hames   std::promise<MSVCPError> ErrP;
4464288571SLang Hames 
4564288571SLang Hames   lookupAndRecordAddrs([&](Error Err) { ErrP.set_value(std::move(Err)); }, ES,
4664288571SLang Hames                        LookupKind::Static, makeJITDylibSearchOrder(&JD),
478b1771bdSLang Hames                        {{Foo, &RecordedFooAddr}, {Bar, &RecordedBarAddr}});
4864288571SLang Hames 
4964288571SLang Hames   Error Err = ErrP.get_future().get();
5064288571SLang Hames 
5164288571SLang Hames   EXPECT_THAT_ERROR(std::move(Err), Failed());
5264288571SLang Hames }
5364288571SLang Hames 
5464288571SLang Hames TEST_F(LookupAndRecordAddrsTest, AsyncWeakReference) {
5564288571SLang Hames   cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
5664288571SLang Hames 
578b1771bdSLang Hames   ExecutorAddr RecordedFooAddr, RecordedBarAddr;
5864288571SLang Hames   std::promise<MSVCPError> ErrP;
5964288571SLang Hames 
6064288571SLang Hames   lookupAndRecordAddrs([&](Error Err) { ErrP.set_value(std::move(Err)); }, ES,
6164288571SLang Hames                        LookupKind::Static, makeJITDylibSearchOrder(&JD),
628b1771bdSLang Hames                        {{Foo, &RecordedFooAddr}, {Bar, &RecordedBarAddr}},
6364288571SLang Hames                        SymbolLookupFlags::WeaklyReferencedSymbol);
6464288571SLang Hames 
6564288571SLang Hames   Error Err = ErrP.get_future().get();
6664288571SLang Hames 
6764288571SLang Hames   EXPECT_THAT_ERROR(std::move(Err), Succeeded());
688b1771bdSLang Hames   EXPECT_EQ(RecordedFooAddr, FooAddr);
698b1771bdSLang Hames   EXPECT_EQ(RecordedBarAddr, ExecutorAddr());
7064288571SLang Hames }
7164288571SLang Hames 
7264288571SLang Hames TEST_F(LookupAndRecordAddrsTest, BlockingRequiredSuccess) {
7364288571SLang Hames   cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
7464288571SLang Hames 
758b1771bdSLang Hames   ExecutorAddr RecordedFooAddr, RecordedBarAddr;
7664288571SLang Hames   auto Err =
7764288571SLang Hames       lookupAndRecordAddrs(ES, LookupKind::Static, makeJITDylibSearchOrder(&JD),
788b1771bdSLang Hames                            {{Foo, &RecordedFooAddr}, {Bar, &RecordedBarAddr}});
7964288571SLang Hames 
8064288571SLang Hames   EXPECT_THAT_ERROR(std::move(Err), Succeeded());
818b1771bdSLang Hames   EXPECT_EQ(RecordedFooAddr, FooAddr);
828b1771bdSLang Hames   EXPECT_EQ(RecordedBarAddr, BarAddr);
8364288571SLang Hames }
8464288571SLang Hames 
8564288571SLang Hames TEST_F(LookupAndRecordAddrsTest, BlockingRequiredFailure) {
868b1771bdSLang Hames   ExecutorAddr RecordedFooAddr, RecordedBarAddr;
8764288571SLang Hames   auto Err =
8864288571SLang Hames       lookupAndRecordAddrs(ES, LookupKind::Static, makeJITDylibSearchOrder(&JD),
898b1771bdSLang Hames                            {{Foo, &RecordedFooAddr}, {Bar, &RecordedBarAddr}});
9064288571SLang Hames 
9164288571SLang Hames   EXPECT_THAT_ERROR(std::move(Err), Failed());
9264288571SLang Hames }
9364288571SLang Hames 
9464288571SLang Hames TEST_F(LookupAndRecordAddrsTest, BlockingWeakReference) {
9564288571SLang Hames   cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
9664288571SLang Hames 
978b1771bdSLang Hames   ExecutorAddr RecordedFooAddr, RecordedBarAddr;
9864288571SLang Hames   auto Err =
9964288571SLang Hames       lookupAndRecordAddrs(ES, LookupKind::Static, makeJITDylibSearchOrder(&JD),
1008b1771bdSLang Hames                            {{Foo, &RecordedFooAddr}, {Bar, &RecordedBarAddr}},
10164288571SLang Hames                            SymbolLookupFlags::WeaklyReferencedSymbol);
10264288571SLang Hames 
10364288571SLang Hames   EXPECT_THAT_ERROR(std::move(Err), Succeeded());
1048b1771bdSLang Hames   EXPECT_EQ(RecordedFooAddr, FooAddr);
1058b1771bdSLang Hames   EXPECT_EQ(RecordedBarAddr, ExecutorAddr());
10664288571SLang Hames }
10764288571SLang Hames 
10864288571SLang Hames } // namespace
109