1 /**
2 * Copyright (c) 2021-2024 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 <cstdlib>
17 #include <iostream>
18 #include <ostream>
19
20 #include <gtest/gtest.h>
21
22 #include "libpandabase/utils/logger.h"
23 #include "plugins/ets/runtime/ets_coroutine.h"
24 #include "plugins/ets/runtime/ets_vm.h"
25 #include "runtime/include/class.h"
26 #include "runtime/include/managed_thread.h"
27 #include "runtime/include/object_header.h"
28 #include "runtime/include/panda_vm.h"
29 #include "runtime/include/runtime_options.h"
30 #include "runtime/include/runtime.h"
31
32 namespace ark::ets::test {
33
34 class EtsVMInitPreallocTest : public testing::Test {
35 public:
EtsVMInitPreallocTest()36 EtsVMInitPreallocTest()
37 {
38 RuntimeOptions options;
39 options.SetShouldLoadBootPandaFiles(true);
40 options.SetShouldInitializeIntrinsics(false);
41 options.SetCompilerEnableJit(false);
42 options.SetGcType("epsilon");
43 options.SetLoadRuntimes({"ets"});
44
45 auto stdlib = std::getenv("PANDA_STD_LIB");
46 if (stdlib == nullptr) {
47 std::cerr << "PANDA_STD_LIB env variable should be set and point to mock_stdlib.abc" << std::endl;
48 std::abort();
49 }
50 options.SetBootPandaFiles({stdlib});
51
52 Logger::InitializeStdLogging(Logger::Level::ERROR, 0);
53
54 Runtime::Create(options);
55 }
56
~EtsVMInitPreallocTest()57 ~EtsVMInitPreallocTest() override
58 {
59 Runtime::Destroy();
60 }
61
62 NO_COPY_SEMANTIC(EtsVMInitPreallocTest);
63 NO_MOVE_SEMANTIC(EtsVMInitPreallocTest);
64 };
65
TEST_F(EtsVMInitPreallocTest,PreallocatedOOMObjectTest)66 TEST_F(EtsVMInitPreallocTest, PreallocatedOOMObjectTest)
67 {
68 auto coroutine = EtsCoroutine::GetCurrent();
69 auto etsVm = coroutine->GetPandaVM();
70 auto oomObj = etsVm->GetOOMErrorObject();
71
72 ASSERT_NE(oomObj, nullptr);
73
74 auto oomClass = oomObj->ClassAddr<ark::Class>();
75 ASSERT_NE(oomClass->GetDescriptor(), nullptr);
76
77 auto ctx = coroutine->GetLanguageContext();
78 auto descriptorGot = utf::Mutf8AsCString(oomClass->GetDescriptor());
79 auto descriptorExp = utf::Mutf8AsCString(ctx.GetOutOfMemoryErrorClassDescriptor());
80 ASSERT_STREQ(descriptorGot, descriptorExp);
81 }
82
83 } // namespace ark::ets::test
84