• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 #ifndef PANDA_RUNTIME_OPTIONS_H_
16 #define PANDA_RUNTIME_OPTIONS_H_
17 
18 #include "generated/runtime_options_gen.h"
19 #include "utils/logger.h"
20 #include "runtime/plugins.h"
21 
22 namespace ark {
23 
24 /**
25  * @brief Class represents runtime options
26  *
27  * It extends Options that represents public options (that described in options.yaml) and
28  * adds some private options related to runtime initialization that cannot be controlled
29  * via command line tools. Now they are used in unit tests to create minimal runtime for
30  * testing.
31  *
32  * To control private options from any class/function we need make it friend for this class.
33  */
34 class PANDA_PUBLIC_API RuntimeOptions : public Options {
35 public:
Options(exePath)36     explicit RuntimeOptions(const std::string &exePath = "") : Options(exePath) {}
37 
ShouldLoadBootPandaFiles()38     bool ShouldLoadBootPandaFiles() const
39     {
40         return shouldLoadBootPandaFiles_;
41     }
42 
ShouldInitializeIntrinsics()43     bool ShouldInitializeIntrinsics() const
44     {
45         return shouldInitializeIntrinsics_;
46     }
47 
GetMobileLog()48     void *GetMobileLog()
49     {
50         return mlogBufPrintPtr_;
51     }
52 
GetFingerprint()53     const std::string &GetFingerprint() const
54     {
55         return fingerPrint_;
56     }
57 
SetFingerprint(const std::string & in)58     void SetFingerprint(const std::string &in)
59     {
60         fingerPrint_.assign(in);
61     }
62 
IsVerifyRuntimeLibraries()63     bool IsVerifyRuntimeLibraries() const
64     {
65         return verifyRuntimeLibraries_;
66     }
67 
SetVerifyRuntimeLibraries(bool in)68     void SetVerifyRuntimeLibraries(bool in)
69     {
70         verifyRuntimeLibraries_ = in;
71     }
72 
SetUnwindStack(void * in)73     void SetUnwindStack(void *in)
74     {
75         unwindstack_ = reinterpret_cast<char *>(in);
76     }
77 
GetUnwindStack()78     void *GetUnwindStack() const
79     {
80         return unwindstack_;
81     }
82 
SetCrashConnect(void * in)83     void SetCrashConnect(void *in)
84     {
85         crashConnect_ = reinterpret_cast<char *>(in);
86     }
87 
GetCrashConnect()88     void *GetCrashConnect() const
89     {
90         return crashConnect_;
91     }
92 
SetMobileLog(void * mlogBufPrintPtr)93     void SetMobileLog(void *mlogBufPrintPtr)
94     {
95         mlogBufPrintPtr_ = mlogBufPrintPtr;
96         Logger::SetMobileLogPrintEntryPointByPtr(mlogBufPrintPtr);
97     }
98 
SetForSnapShotStart()99     void SetForSnapShotStart()
100     {
101         shouldLoadBootPandaFiles_ = false;
102         shouldInitializeIntrinsics_ = false;
103     }
104 
SetShouldLoadBootPandaFiles(bool value)105     void SetShouldLoadBootPandaFiles(bool value)
106     {
107         shouldLoadBootPandaFiles_ = value;
108     }
109 
SetShouldInitializeIntrinsics(bool value)110     void SetShouldInitializeIntrinsics(bool value)
111     {
112         shouldInitializeIntrinsics_ = value;
113     }
114 
UseMallocForInternalAllocations()115     bool UseMallocForInternalAllocations() const
116     {
117         bool useMalloc = false;
118         auto option = GetInternalAllocatorType();
119         if (option == "default") {
120 #ifdef NDEBUG
121             useMalloc = true;
122 #else
123             useMalloc = false;
124 #endif
125         } else if (option == "malloc") {
126             useMalloc = true;
127         } else if (option == "panda_allocators") {
128             useMalloc = false;
129         } else {
130             UNREACHABLE();
131         }
132         return useMalloc;
133     }
134 
IsG1TrackFreedObjects()135     bool IsG1TrackFreedObjects() const
136     {
137         bool track = false;
138         auto option = GetG1TrackFreedObjects();
139         if (option == "default") {
140 #ifdef NDEBUG
141             track = false;
142 #else
143             track = true;
144 #endif
145         } else if (option == "true") {
146             track = true;
147         } else if (option == "false") {
148             track = false;
149         } else {
150             UNREACHABLE();
151         }
152         return track;
153     }
154 
InitializeRuntimeSpacesAndType()155     void InitializeRuntimeSpacesAndType()
156     {
157         CheckAndFixIntrinsicSpaces();
158         if (WasSetLoadRuntimes()) {
159             std::vector<std::string> loadRuntimes = GetLoadRuntimes();
160             std::string runtimeType = "core";
161 
162             // Select first non-core runtime
163             for (auto &runtime : loadRuntimes) {
164                 if (runtime != "core") {
165                     runtimeType = runtime;
166                     break;
167                 }
168             }
169 
170             SetRuntimeType(runtimeType);
171             SetBootClassSpaces(loadRuntimes);
172             SetBootIntrinsicSpaces(loadRuntimes);
173         }
174     }
175 
176 private:
177     // Fix default value for possible missing plugins.
CheckAndFixIntrinsicSpaces()178     void CheckAndFixIntrinsicSpaces()
179     {
180         bool intrSet = WasSetBootIntrinsicSpaces();
181         std::vector<std::string> spaces = GetBootIntrinsicSpaces();
182         for (auto it = spaces.begin(); it != spaces.end();) {
183             if (ark::plugins::HasRuntime(*it)) {
184                 ++it;
185                 continue;
186             }
187 
188             if (intrSet) {
189                 LOG(FATAL, RUNTIME) << "Missing runtime for intrinsic space " << *it;
190             }
191             it = spaces.erase(it);
192         }
193 
194         SetBootIntrinsicSpaces(spaces);
195     }
196 
197     bool shouldLoadBootPandaFiles_ {true};
198     bool shouldInitializeIntrinsics_ {true};
199     void *mlogBufPrintPtr_ {nullptr};
200     std::string fingerPrint_ {"unknown"};
201     void *unwindstack_ {nullptr};
202     void *crashConnect_ {nullptr};
203     bool verifyRuntimeLibraries_ {false};
204 };
205 }  // namespace ark
206 
207 #endif  // PANDA_RUNTIME_OPTIONS_H_
208