1 /**
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "types/ets_runtime_linker.h"
19 #include "tests/runtime/types/ets_test_mirror_classes.h"
20
21 #include "plugins/ets/runtime/ets_class_linker_extension.h"
22 #include "runtime/include/class_linker.h"
23 #include "libpandabase/utils/utf.h"
24
25 #include "ets_coroutine.h"
26 #include "ets_vm.h"
27
28 namespace ark::ets::test {
29
30 class EtsRuntimeLinkerTest : public testing::Test {
31 public:
EtsRuntimeLinkerTest()32 EtsRuntimeLinkerTest()
33 {
34 options_.SetShouldLoadBootPandaFiles(true);
35 options_.SetShouldInitializeIntrinsics(false);
36 options_.SetCompilerEnableJit(false);
37 options_.SetGcType("epsilon");
38 options_.SetLoadRuntimes({"ets"});
39
40 auto stdlib = std::getenv("PANDA_STD_LIB");
41 if (stdlib == nullptr) {
42 std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
43 std::abort();
44 }
45 options_.SetBootPandaFiles({stdlib});
46
47 Runtime::Create(options_);
48 }
49
~EtsRuntimeLinkerTest()50 ~EtsRuntimeLinkerTest() override
51 {
52 Runtime::Destroy();
53 }
54
55 NO_COPY_SEMANTIC(EtsRuntimeLinkerTest);
56 NO_MOVE_SEMANTIC(EtsRuntimeLinkerTest);
57
SetUp()58 void SetUp() override
59 {
60 coroutine_ = EtsCoroutine::GetCurrent();
61 vm_ = coroutine_->GetPandaVM();
62 coroutine_->ManagedCodeBegin();
63 }
64
TearDown()65 void TearDown() override
66 {
67 coroutine_->ManagedCodeEnd();
68 }
69
GetClassMembers()70 static std::vector<MirrorFieldInfo> GetClassMembers()
71 {
72 return std::vector<MirrorFieldInfo> {
73 MIRROR_FIELD_INFO(EtsRuntimeLinker, classLinkerCtxPtr_, "classLinkerCtxPtr")};
74 }
75
76 protected:
77 PandaEtsVM *vm_ = nullptr; // NOLINT(misc-non-private-member-variables-in-classes)
78
79 private:
80 RuntimeOptions options_;
81 EtsCoroutine *coroutine_ = nullptr;
82 };
83
TEST_F(EtsRuntimeLinkerTest,RuntimeLinkerMemoryLayout)84 TEST_F(EtsRuntimeLinkerTest, RuntimeLinkerMemoryLayout)
85 {
86 EtsClass *runtimeLinkerClass = PlatformTypes(vm_)->coreRuntimeLinker;
87 MirrorFieldInfo::CompareMemberOffsets(runtimeLinkerClass, GetClassMembers());
88 }
89
90 class SuppressErrorHandler : public ark::ClassLinkerErrorHandler {
OnError(ark::ClassLinker::Error error,const ark::PandaString & message)91 void OnError([[maybe_unused]] ark::ClassLinker::Error error,
92 [[maybe_unused]] const ark::PandaString &message) override
93 {
94 }
95 };
96
TEST_F(EtsRuntimeLinkerTest,GetClassReturnsNullWhenErrorSuppressed)97 TEST_F(EtsRuntimeLinkerTest, GetClassReturnsNullWhenErrorSuppressed)
98 {
99 auto *ext = static_cast<ark::ets::EtsClassLinkerExtension *>(
100 ark::Runtime::GetCurrent()->GetClassLinker()->GetExtension(ark::panda_file::SourceLang::ETS));
101
102 const auto *fakeDescriptor = reinterpret_cast<const uint8_t *>("Lark/test/Fake;");
103 SuppressErrorHandler handler;
104
105 Class *klass = ext->GetClass(fakeDescriptor, true, nullptr, &handler);
106 ASSERT_EQ(klass, nullptr);
107 }
108
109 } // namespace ark::ets::test
110