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 "runtime/include/loadable_agent.h"
17 
18 #include "runtime/include/runtime.h"
19 
20 namespace ark {
LibraryAgent(os::memory::Mutex &mutex, PandaString libraryPath, PandaString loadCallbackName, PandaString unloadCallbackName)21 LibraryAgent::LibraryAgent(os::memory::Mutex &mutex, PandaString libraryPath, PandaString loadCallbackName,
22                            PandaString unloadCallbackName)
23     : lock_(mutex),
24       libraryPath_(std::move(libraryPath)),
25       loadCallbackName_(std::move(loadCallbackName)),
26       unloadCallbackName_(std::move(unloadCallbackName))
27 {
28 }
29 
Load()30 bool LibraryAgent::Load()
31 {
32     ASSERT(!handle_.IsValid());
33 
34     auto handle = os::library_loader::Load(libraryPath_);
35     if (!handle) {
36         LOG(ERROR, RUNTIME) << "Couldn't load library '" << libraryPath_ << "': " << handle.Error().ToString();
37         return false;
38     }
39 
40     auto loadCallback = os::library_loader::ResolveSymbol(handle.Value(), loadCallbackName_);
41     if (!loadCallback) {
42         LOG(ERROR, RUNTIME) << "Couldn't resolve '" << loadCallbackName_ << "' in '" << libraryPath_
43                             << "':" << loadCallback.Error().ToString();
44         return false;
45     }
46 
47     auto unloadCallback = os::library_loader::ResolveSymbol(handle.Value(), unloadCallbackName_);
48     if (!unloadCallback) {
49         LOG(ERROR, RUNTIME) << "Couldn't resolve '" << unloadCallbackName_ << "' in '" << libraryPath_
50                             << "':" << unloadCallback.Error().ToString();
51         return false;
52     }
53 
54     if (!CallLoadCallback(loadCallback.Value())) {
55         LOG(ERROR, RUNTIME) << "'" << loadCallbackName_ << "' failed in '" << libraryPath_ << "'";
56         return false;
57     }
58 
59     handle_ = std::move(handle.Value());
60     unloadCallback_ = unloadCallback.Value();
61 
62     return true;
63 }
64 
Unload()65 bool LibraryAgent::Unload()
66 {
67     ASSERT(handle_.IsValid());
68 
69     if (!CallUnloadCallback(unloadCallback_)) {
70         LOG(ERROR, RUNTIME) << "'" << unloadCallbackName_ << "' failed in '" << libraryPath_ << "'";
71         return false;
72     }
73 
74     return true;
75 }
76 }  // namespace ark
77