Files
ability_ability_runtime/interfaces/inner_api/runtime/include/js_runtime.h
T
openharmony_ci 6ee55360b5 !18772 merge master into master
hidumper support merge jsheap raw file

Created-by: LiuZX1997
Commit-by: liuzhenxiong
Merged-by: openharmony_ci
Description: **IssueNo**:
https://gitcode.com/openharmony/hiviewdfx_hidumper/issues/555

**Description**:

**稳定性自检:**
| 自检项                                                       | 自检结果  |
| ------------------------------------------------------------ | -------- |
| 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发              |          |
| 成员变量进行赋值或创建需要排查并发                               |          |
| 谨慎在lambda表达式中使用引用捕获                                |          |
| 谨慎在未经拷贝的情况下使用外部传入的string、C字符串               |          |
| map\vector\list\set等stl模板类使用时需要排查并发                |          |
| 谨慎考虑加锁范围                                               |          |
| 在IPC通信中谨慎使用同步通信方式                                 |          |
| 禁止传递this指针至其他模块或线程(特别是eventhandler任务)        |          |
| 禁止将外部传入的裸指针在内部直接构造智能指针                      |          |
| 禁止多个独立创建的智能指针管理同一地址                           |          |
| 禁止在析构函数中抛异步任务                                      |          |
| 禁止js对象在非js线程(例如在IPC线程)创建、使用或销毁             |          |
| 禁止在对外接口中未经判空直接使用外部传入的指针                    |          |
| 禁止接口返回局部变量引用                                        |          |
| 禁止在信号函数中加锁                                            |          |
| 禁止在关键流程(SA启动、应用启动等主流程)执行耗时的操作           |          |
| 禁止将同一个cpp编译在不同的so中                                 |          |

**安全编码自检:**
| 自检项                                                          | 自检结果 |
| -------------------------------------------------------------- | -------- |
| 裸指针避免通过隐式转换构造为sptr                                 |          |
| json对象在取值之前必须先判断类型,避免类型不匹配                   |          |
| 序列化时必须对传入的数组大小进行校验,避免出现超大数组              |          |
| 避免使用未明确位宽的整型,选择使用int8_t、uint8_t等类型            |          |
| 外部传入的路径要做规范化校验,对路径中的.、..、../等特殊字符严格校验 |          |
| 指针变量、表示资源描述符的变量、bool变量必须赋初值                  |          |
| readParcelable获取的对象使用前需要判空                            |          |
| 分配和释放内存的函数需要成对出现                                   |          |
| 申请内存后异常退出前需要及时进行内存释放                            |          |
| 内存申请前必须对内存大小进行合法性校验                              |          |
| 内存分配后必须判断是否成功                                         |          |
| 禁止使用realloc、alloca函数                                       |          |
| 禁止打印文件路径、口令等敏感信息,如有需要,使用private修饰          |          |
| 禁止打印内存地址                                                  |          |
| 整数之间运算时必须严格检查,确保不会出现溢出、反转、除0               |          |
| 禁止对有符号整数进行位操作符运算                                    |          |
| 禁止对指针进行逻辑或位运算                                         |          |
| 循环次数如果收外部数据控制,需要检验其合法性                         |          |
| 禁止使用内存操作类危险函数,需要使用安全函数                         |          |
| 谨慎使用不可重入函数                                               |          |
| 必须检查安全函数的返回值,并进行正确处理                             |          |
| 禁止仅通过TokenType类型判断绕过权限校验                             |          |

**TDD Result**:

**XTS Result**:

### 是否已执行L0用例
- [ ] 已验证
- [ ] 不涉及。如不涉及,请写明理由


See merge request: openharmony/ability_ability_runtime!18772
2026-04-03 15:51:40 +08:00

229 lines
9.8 KiB
C++

/*
* Copyright (c) 2021-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
#define OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "native_engine/native_engine.h"
#include "runtime.h"
namespace panda::ecmascript {
class EcmaVM;
} // namespace panda::ecmascript
namespace panda {
struct HmsMap;
}
namespace OHOS {
namespace AppExecFwk {
class EventHandler;
} // namespace AppExecFwk
namespace AbilityBase {
class Extractor;
class FileMapper;
} // namespace AbilityBase
namespace JsEnv {
class JsEnvironment;
class SourceMapOperator;
struct ErrorObject;
struct UncaughtExceptionInfo;
using UncatchableTask = std::function<void(std::string summary, const JsEnv::ErrorObject errorObject, napi_env env,
napi_value exception)>;
} // namespace JsEnv
using AppLibPathMap = std::map<std::string, std::vector<std::string>>;
#ifdef APP_USE_ARM
constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.z.so";
#elif defined(APP_USE_X86_64)
constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.z.so";
#else
constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.z.so";
#endif
namespace AbilityRuntime {
class TimerTask;
inline void *DetachCallbackFunc(napi_env env, void *value, void *)
{
return value;
}
class JsRuntime : public Runtime {
public:
using Runtime::PreloadModule;
static std::unique_ptr<JsRuntime> Create(const Options& options);
static void SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp = false);
static void SetOrUpdateLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp = false);
static void InheritPluginNamespace(const std::vector<std::string> &moduleNames);
static void CreatePluginDefaultNamespace(const std::string &lddictorys);
static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content);
JsRuntime();
~JsRuntime() override;
NativeEngine& GetNativeEngine() const;
napi_env GetNapiEnv() const;
Language GetLanguage() const override
{
return Language::JS;
}
void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime);
void PostSyncTask(const std::function<void()>& task, const std::string& name);
void RemoveTask(const std::string& name);
void DumpHeapSnapshot(bool isPrivate) override;
void DumpCpuProfile() override;
void DestroyHeapProfiler() override;
void ForceFullGC() override;
void ForceFullGC(uint32_t tid) override;
void DumpHeapSnapshot(uint32_t tid, bool isFullGC, bool isBinary = false) override;
void DumpHeapSnapshot(uint32_t tid, const OHOS::AbilityRuntime::Runtime::JsHeapDumpParam &param) override;
void AllowCrossThreadExecution() override;
void GetHeapPrepare() override;
void NotifyApplicationState(bool isBackground) override;
bool SuspendVM(uint32_t tid) override;
void ResumeVM(uint32_t tid) override;
bool RunSandboxScript(const std::string& path, const std::string& hapPath);
bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false,
const std::string& srcEntrance = "");
void PreloadSystemModule(const std::string& moduleName) override;
void PreloadMainAbility(const std::string& moduleName, const std::string& srcPath,
const std::string& hapPath, bool isEsMode, const std::string& srcEntrance) override;
void PreloadModule(const std::string& moduleName, const std::string& srcPath,
const std::string& hapPath, bool isEsMode, bool useCommonTrunk) override;
bool PopPreloadObj(const std::string& key, std::unique_ptr<NativeReference>& obj);
void StartDebugMode(const DebugOption debugOption) override;
bool ShouldSkipDebugMode(const DebugOption debugOption);
void SetDebugOption(const DebugOption debugOption) override;
void StartLocalDebugMode(bool isDebugFromLocal) override;
void DebuggerConnectionHandler(bool isDebugApp, bool isStartWithDebug);
void StopDebugMode();
bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override;
bool UnLoadRepairPatch(const std::string& hqfFile) override;
bool NotifyHotReloadPage() override;
void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask);
bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle,
const std::string& srcEntrance = "");
bool StartDebugger(bool needBreakPoint, uint32_t instanceId);
void StopDebugger();
NativeEngine* GetNativeEnginePointer() const;
panda::ecmascript::EcmaVM* GetEcmaVm() const;
void UpdateModuleNameAndAssetPath(const std::string& moduleName);
void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override;
static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer,
bool isABC = true);
static std::shared_ptr<AbilityBase::FileMapper> GetSafeData(const std::string& path, std::string& fileFullName);
void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorImpl);
void InitSourceMap(const std::string hqfFilePath);
void FreeNativeReference(std::unique_ptr<NativeReference> reference);
void FreeNativeReference(std::shared_ptr<NativeReference>&& reference);
void StartProfiler(const DebugOption debugOption) override;
void SetExtensionApiCheckCallback(
std::function<bool(const std::string &className, const std::string &fileName)> &cb) override {}
void DebuggerConnectionManager(bool isDebugApp, bool isStartWithDebug, const DebugOption dOption);
void ReloadFormComponent(); // Reload ArkTS-Card component
void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate) const override;
static std::unique_ptr<NativeReference> LoadSystemModuleByEngine(napi_env env,
const std::string& moduleName, const napi_value* argv, size_t argc);
std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath,
const std::string& hapPath, bool esmodule = false, bool useCommonChunk = false,
const std::string& srcEntrance = "");
std::unique_ptr<NativeReference> LoadSystemModule(
const std::string& moduleName, const napi_value* argv = nullptr, size_t argc = 0);
std::unique_ptr<AbilityBase::FileMapper> ExecuteSecureWithOhmUrl(const std::string &moduleName,
const std::string &hapPath, const std::string &srcEntrance);
napi_value GetExportObjectFromOhmUrl(const std::string &srcEntrance, const std::string &key);
void SetDeviceDisconnectCallback(const std::function<bool()> &cb) override;
void SetStopPreloadSoCallback(const std::function<void()> &callback);
void SetPkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName);
void UpdatePkgContextInfoJson(const std::string& moduleName, const std::string& hapPath,
const std::string& packageName);
bool Init(const Options& options);
size_t GetHeapTotalSize();
size_t GetHeapObjectSize();
private:
void FinishPreload() override;
bool Initialize(const Options& options);
void Deinitialize();
int32_t JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue);
napi_value LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
napi_value LoadJsModule(const std::string& path, const std::string& hapPath, const std::string& srcEntrance = "");
bool preloaded_ = false;
bool isBundle_ = true;
bool isOhmUrl_ = false;
std::string codePath_;
std::string moduleName_;
std::unique_ptr<NativeReference> methodRequireNapiRef_;
std::unordered_map<std::string, NativeReference*> modules_;
std::shared_ptr<JsEnv::JsEnvironment> jsEnv_ = nullptr;
uint32_t instanceId_ = 0;
std::string bundleName_;
int32_t apiTargetVersion_ = 0;
std::map<std::string, std::string> pkgContextInfoJsonStringMap_;
std::map<std::string, std::string> packageNameList_;
std::map<std::string, std::unique_ptr<NativeReference>> preloadList_;
static std::atomic<bool> hasInstance;
DebugOption debugOption_;
private:
bool CreateJsEnv(const Options& options);
void PreloadAce(const Options& options);
bool InitLoop(bool isStage = true);
inline bool IsUseAbilityRuntime(const Options& options) const;
void FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
std::shared_ptr<NativeReference>&& sharedNativeRef);
void InitConsoleModule();
void InitTimerModule();
void InitWorkerModule(const Options& options);
void ReInitJsEnvImpl(const Options& options);
void PostPreload(const Options& options);
void LoadAotFile(const Options& options);
void SetRequestAotCallback();
std::string GetSystemKitPath();
std::vector<panda::HmsMap> GetSystemKitsMap(uint32_t version);
};
} // namespace AbilityRuntime
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H