1 /**
2 * Copyright (c) 2021-2022 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 #include "assembly-parser.h"
18 #include "assembly-emitter.h"
19 #include "runtime/include/runtime.h"
20
21 namespace panda::test {
22
separator()23 inline std::string separator()
24 {
25 #ifdef _WIN32
26 return "\\";
27 #else
28 return "/";
29 #endif
30 }
31
32 class IntrinsicsBlacklistTest : public testing::Test {
33 public:
~IntrinsicsBlacklistTest()34 ~IntrinsicsBlacklistTest()
35 {
36 if (runtime_created_) {
37 Runtime::Destroy();
38 }
39 }
40
CreateRuntime(RuntimeOptions & options)41 void CreateRuntime(RuntimeOptions &options)
42 {
43 ASSERT_FALSE(runtime_created_);
44 Runtime::Create(options);
45 runtime_created_ = true;
46 }
47
48 private:
49 bool runtime_created_ {false};
50 };
51
TEST_F(IntrinsicsBlacklistTest,DisableIntrinsic)52 TEST_F(IntrinsicsBlacklistTest, DisableIntrinsic)
53 {
54 auto source = R"(
55 .record Math <external>
56 .function i32 Math.absI32(i32 a0) <external>
57
58 .function i32 main() {
59 movi v0, 42
60 call.short Math.absI32, v0, v0
61 return
62 }
63 )";
64 RuntimeOptions options;
65 options.SetLoadRuntimes({"core"});
66 options.SetIntrinsicsBlacklist({"Math::absI32"});
67 auto exec_path = panda::os::file::File::GetExecutablePath();
68 std::string panda_std_lib =
69 exec_path.Value() + separator() + ".." + separator() + "pandastdlib" + separator() + "arkstdlib.abc";
70 options.SetBootPandaFiles({panda_std_lib});
71 CreateRuntime(options);
72 pandasm::Parser parser;
73 auto res = parser.Parse(source);
74 ASSERT_TRUE(res);
75 auto pf = pandasm::AsmEmitter::Emit(res.Value());
76 Runtime::GetCurrent()->GetClassLinker()->AddPandaFile(std::move(pf));
77
78 // There are no implementations of Math.absI32 other than intrinsic, so execution should be aborted
79 ASSERT_DEATH(Runtime::GetCurrent()->Execute("_GLOBAL::main", {}), "");
80 }
81 } // namespace panda::test
82