mirror of
https://github.com/openharmony/ability_ability_runtime.git
synced 2026-08-24 22:21:36 -04:00
!17851 merge getstartTime into master
new_提供UIAbility获取开始启动时间的接口 Created-by: lidongrui Commit-by: lidongrui Merged-by: openharmony_ci Description: **IssueNo**: **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!17851
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "ets_data_struct_converter.h"
|
||||
|
||||
#include "ani_common_util.h"
|
||||
#include "ani_enum_convert.h"
|
||||
#include "hilog_tag_wrapper.h"
|
||||
#include "running_process_info.h"
|
||||
@@ -142,6 +143,16 @@ bool WrapLaunchParamInner(ani_env *env, const AAFwk::LaunchParam &launchParam, a
|
||||
TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to set lastExitDetailInfo");
|
||||
return false;
|
||||
}
|
||||
if ((status = env->Object_SetPropertyByName_Ref(object, "launchUptime",
|
||||
AppExecFwk::CreateLong(env, launchParam.launchUptime))) != ANI_OK) {
|
||||
TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to set launchUptime");
|
||||
return false;
|
||||
}
|
||||
if ((status = env->Object_SetPropertyByName_Ref(object, "launchUTCTime",
|
||||
AppExecFwk::CreateLong(env, launchParam.launchUTCTime))) != ANI_OK) {
|
||||
TAG_LOGE(AAFwkTag::ETSRUNTIME, "Failed to set launchUTCTime");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
@@ -25,6 +25,8 @@ namespace AbilityConstant {
|
||||
lastExitReason: LastExitReason;
|
||||
lastExitMessage: string;
|
||||
lastExitDetailInfo?: LastExitDetailInfo;
|
||||
launchUptime?: long;
|
||||
launchUTCTime?: long;
|
||||
}
|
||||
|
||||
export interface LastExitDetailInfo {
|
||||
@@ -126,6 +128,8 @@ class LaunchParamImpl implements AbilityConstant.LaunchParam {
|
||||
lastExitReason: AbilityConstant.LastExitReason = AbilityConstant.LastExitReason.UNKNOWN;
|
||||
lastExitMessage: string = '';
|
||||
lastExitDetailInfo?: AbilityConstant.LastExitDetailInfo = undefined;
|
||||
launchUptime?: long = 0;
|
||||
launchUTCTime?: long = 0;
|
||||
}
|
||||
|
||||
class LastExitDetailInfoImpl implements AbilityConstant.LastExitDetailInfo {
|
||||
|
||||
@@ -103,6 +103,8 @@ napi_value CreateJsLaunchParam(napi_env env, const AAFwk::LaunchParam& launchPar
|
||||
napi_set_named_property(env, object, "lastExitMessage", CreateJsValue(env, launchParam.lastExitMessage));
|
||||
napi_set_named_property(env, object, "lastExitDetailInfo", CreateLastExitDetailInfo(
|
||||
env, launchParam.lastExitDetailInfo));
|
||||
napi_set_named_property(env, object, "launchUptime", CreateJsValue(env, launchParam.launchUptime));
|
||||
napi_set_named_property(env, object, "launchUTCTime", CreateJsValue(env, launchParam.launchUTCTime));
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2023 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2021-2025 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
|
||||
@@ -81,6 +81,8 @@ struct LaunchParam : public Parcelable {
|
||||
std::string launchReasonMessage = "";
|
||||
std::string lastExitMessage = "";
|
||||
LastExitDetailInfo lastExitDetailInfo;
|
||||
int64_t launchUptime = 0;
|
||||
int64_t launchUTCTime = 0;
|
||||
|
||||
bool ReadFromParcel(Parcel &parcel);
|
||||
virtual bool Marshalling(Parcel &parcel) const override;
|
||||
|
||||
@@ -169,6 +169,15 @@ static constexpr int64_t MICROSECONDS = 1000000; // MICROSECONDS mean 10^6 mi
|
||||
return (int64_t)(t.tv_sec);
|
||||
}
|
||||
|
||||
[[maybe_unused]] static int64_t UTCTimeMillis()
|
||||
{
|
||||
struct timespec t;
|
||||
t.tv_sec = 0;
|
||||
t.tv_nsec = 0;
|
||||
clock_gettime(CLOCK_REALTIME, &t);
|
||||
return static_cast<int64_t>(t.tv_sec * 1000LL + t.tv_nsec / 1000000LL);
|
||||
}
|
||||
|
||||
[[maybe_unused]] static bool IsStartFreeInstall(const Want &want)
|
||||
{
|
||||
auto flags = want.GetFlags();
|
||||
|
||||
@@ -279,6 +279,8 @@ int AbilityRecord::LoadAbility(bool isShellCall, bool isStartupHide, pid_t calli
|
||||
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR, "LoadLifecycle: abilityName:%{public}s", abilityInfo_.name.c_str());
|
||||
startTime_ = AbilityUtil::SystemTimeMillis();
|
||||
lifeCycleStateInfo_.launchParam.launchUptime = startTime_;
|
||||
lifeCycleStateInfo_.launchParam.launchUTCTime = AbilityUtil::UTCTimeMillis();
|
||||
CHECK_POINTER_AND_RETURN(token_, ERR_INVALID_VALUE);
|
||||
// only for UIAbility
|
||||
if (!IsDebug() && abilityInfo_.type != AppExecFwk::AbilityType::DATA) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021-2024 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2021-2025 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
|
||||
@@ -42,7 +42,8 @@ bool LaunchParam::ReadFromParcel(Parcel &parcel)
|
||||
return false;
|
||||
}
|
||||
lastExitDetailInfo = *detailInfo;
|
||||
|
||||
launchUptime = parcel.ReadInt64();
|
||||
launchUTCTime = parcel.ReadInt64();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -83,6 +84,16 @@ bool LaunchParam::Marshalling(Parcel &parcel) const
|
||||
TAG_LOGE(AAFwkTag::ABILITYMGR, "write lastExitDetailInfo failed");
|
||||
return false;
|
||||
}
|
||||
// write launchUptime
|
||||
if (!parcel.WriteInt64(launchUptime)) {
|
||||
TAG_LOGE(AAFwkTag::ABILITYMGR, "write launchUptime failed");
|
||||
return false;
|
||||
}
|
||||
// write launchUTCTime
|
||||
if (!parcel.WriteInt64(launchUTCTime)) {
|
||||
TAG_LOGE(AAFwkTag::ABILITYMGR, "write launchUTCTime failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace AAFwk
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# Copyright (c) 2025 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.
|
||||
|
||||
import("//build/test.gni")
|
||||
import("//foundation/ability/ability_runtime/ability_runtime.gni")
|
||||
|
||||
module_output_path = "ability_runtime/ability_runtime/abilitymgr"
|
||||
|
||||
ohos_unittest("launch_param_test") {
|
||||
module_out_path = module_output_path
|
||||
|
||||
include_dirs = [
|
||||
"${ability_runtime_services_path}/abilitymgr/include",
|
||||
"${ability_runtime_innerkits_path}/ability_manager/include",
|
||||
]
|
||||
|
||||
sources = [
|
||||
"${ability_runtime_services_path}/abilitymgr/src/last_exit_detail_info.cpp",
|
||||
"${ability_runtime_services_path}/abilitymgr/src/launch_param.cpp",
|
||||
"launch_param_test.cpp",
|
||||
]
|
||||
|
||||
configs = [ "${ability_runtime_services_path}/abilitymgr:abilityms_config" ]
|
||||
|
||||
deps = [
|
||||
"${ability_runtime_innerkits_path}/ability_manager:ability_manager",
|
||||
"${ability_runtime_innerkits_path}/app_manager:app_manager",
|
||||
"${ability_runtime_innerkits_path}/deps_wrapper:ability_deps_wrapper",
|
||||
"${ability_runtime_services_path}/abilitymgr:abilityms",
|
||||
"${ability_runtime_services_path}/common:perm_verification",
|
||||
"${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit:aakit_mock",
|
||||
]
|
||||
|
||||
external_deps = [
|
||||
"bundle_framework:appexecfwk_base",
|
||||
"bundle_framework:appexecfwk_core",
|
||||
"c_utils:utils",
|
||||
"common_event_service:cesfwk_innerkits",
|
||||
"eventhandler:libeventhandler",
|
||||
"ffrt:libffrt",
|
||||
"googletest:gmock_main",
|
||||
"googletest:gtest_main",
|
||||
"hilog:libhilog",
|
||||
"hisysevent:libhisysevent",
|
||||
"init:libbegetutil",
|
||||
"ipc:ipc_core",
|
||||
"safwk:api_cache_manager",
|
||||
]
|
||||
}
|
||||
|
||||
group("unittest") {
|
||||
testonly = true
|
||||
|
||||
deps = [ ":launch_param_test" ]
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (c) 2025 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.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
#define private public
|
||||
#define protected public
|
||||
#include "launch_param.h"
|
||||
#undef private
|
||||
#undef protected
|
||||
|
||||
using namespace testing;
|
||||
using namespace testing::ext;
|
||||
|
||||
namespace OHOS {
|
||||
namespace AAFwk {
|
||||
class LaunchParamTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase();
|
||||
static void TearDownTestCase();
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
};
|
||||
|
||||
void LaunchParamTest::SetUpTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void LaunchParamTest::TearDownTestCase()
|
||||
{
|
||||
}
|
||||
|
||||
void LaunchParamTest::SetUp()
|
||||
{
|
||||
}
|
||||
|
||||
void LaunchParamTest::TearDown()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.number: LaunchParamTest_Marshalling_001
|
||||
* @tc.desc: Test LaunchParam Marshalling with valid data
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(LaunchParamTest, LaunchParamTest_Marshalling_001, TestSize.Level1)
|
||||
{
|
||||
Parcel parcel;
|
||||
LaunchParam launchParam;
|
||||
launchParam.launchReason = LaunchReason::LAUNCHREASON_START_ABILITY;
|
||||
launchParam.lastExitReason = LastExitReason::LASTEXITREASON_NORMAL;
|
||||
launchParam.launchReasonMessage = "Test launch reason";
|
||||
launchParam.lastExitMessage = "Test exit message";
|
||||
launchParam.lastExitDetailInfo.pid = 5523;
|
||||
launchParam.lastExitDetailInfo.uid = 520;
|
||||
launchParam.lastExitDetailInfo.exitSubReason = 1;
|
||||
launchParam.lastExitDetailInfo.rss = 0;
|
||||
launchParam.lastExitDetailInfo.pss = 0;
|
||||
launchParam.lastExitDetailInfo.processState = 1;
|
||||
launchParam.lastExitDetailInfo.timestamp = 0;
|
||||
launchParam.lastExitDetailInfo.processName = "launch_process";
|
||||
launchParam.lastExitDetailInfo.exitMsg = "exit message";
|
||||
launchParam.launchUptime = 0;
|
||||
launchParam.launchUTCTime = 0;
|
||||
EXPECT_TRUE(launchParam.Marshalling(parcel));
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.number: LaunchParam_Unmarshalling_001
|
||||
* @tc.desc: Test Unmarshalling with valid data
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(LaunchParamTest, LaunchParam_Unmarshalling_001, TestSize.Level1)
|
||||
{
|
||||
Parcel parcel;
|
||||
LaunchParam launchParam;
|
||||
launchParam.launchReason = LaunchReason::LAUNCHREASON_CALL;
|
||||
launchParam.lastExitReason = LastExitReason::LASTEXITREASON_CPP_CRASH;
|
||||
launchParam.launchReasonMessage = "call reason";
|
||||
launchParam.lastExitMessage = "crash message";
|
||||
launchParam.lastExitDetailInfo.pid = 5523;
|
||||
launchParam.lastExitDetailInfo.uid = 520;
|
||||
launchParam.lastExitDetailInfo.processName = "crash_process";
|
||||
launchParam.lastExitDetailInfo.exitMsg = "cpp crash";
|
||||
launchParam.launchUptime = 0;
|
||||
launchParam.launchUTCTime = 0;
|
||||
|
||||
EXPECT_TRUE(launchParam.Marshalling(parcel));
|
||||
|
||||
LaunchParam* result = LaunchParam::Unmarshalling(parcel);
|
||||
ASSERT_NE(result, nullptr);
|
||||
EXPECT_EQ(result->launchReason, LaunchReason::LAUNCHREASON_CALL);
|
||||
EXPECT_EQ(result->lastExitReason, LastExitReason::LASTEXITREASON_CPP_CRASH);
|
||||
EXPECT_EQ(result->launchReasonMessage, "call reason");
|
||||
EXPECT_EQ(result->lastExitMessage, "crash message");
|
||||
EXPECT_EQ(result->lastExitDetailInfo.pid, 5523);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.uid, 520);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.processName, "crash_process");
|
||||
EXPECT_EQ(result->lastExitDetailInfo.exitMsg, "cpp crash");
|
||||
EXPECT_EQ(result->launchUptime, 0);
|
||||
EXPECT_EQ(result->launchUTCTime, 0);
|
||||
delete result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.number: LaunchParam_Marshalling_Unmarshalling_002
|
||||
* @tc.desc: Test complete marshalling and unmarshalling cycle
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(LaunchParamTest, LaunchParam_Marshalling_Unmarshalling_002, TestSize.Level1)
|
||||
{
|
||||
Parcel parcel;
|
||||
LaunchParam original;
|
||||
original.launchReason = LaunchReason::LAUNCHREASON_APP_RECOVERY;
|
||||
original.lastExitReason = LastExitReason::LASTEXITREASON_JS_ERROR;
|
||||
original.launchReasonMessage = "recovery message";
|
||||
original.lastExitMessage = "js error details";
|
||||
original.lastExitDetailInfo.pid = 520;
|
||||
original.lastExitDetailInfo.uid = 5201314;
|
||||
original.lastExitDetailInfo.exitSubReason = 1;
|
||||
original.lastExitDetailInfo.rss = 666;
|
||||
original.lastExitDetailInfo.pss = 888;
|
||||
original.lastExitDetailInfo.processState = 1;
|
||||
original.lastExitDetailInfo.timestamp = 0;
|
||||
original.lastExitDetailInfo.processName = "recovery_process";
|
||||
original.lastExitDetailInfo.exitMsg = "js error occurred";
|
||||
original.launchUptime = 0;
|
||||
original.launchUTCTime = 0;
|
||||
|
||||
EXPECT_TRUE(original.Marshalling(parcel));
|
||||
|
||||
LaunchParam* result = LaunchParam::Unmarshalling(parcel);
|
||||
ASSERT_NE(result, nullptr);
|
||||
EXPECT_EQ(result->launchReason, original.launchReason);
|
||||
EXPECT_EQ(result->lastExitReason, original.lastExitReason);
|
||||
EXPECT_EQ(result->launchReasonMessage, original.launchReasonMessage);
|
||||
EXPECT_EQ(result->lastExitMessage, original.lastExitMessage);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.pid, original.lastExitDetailInfo.pid);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.uid, original.lastExitDetailInfo.uid);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.exitSubReason, original.lastExitDetailInfo.exitSubReason);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.rss, original.lastExitDetailInfo.rss);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.pss, original.lastExitDetailInfo.pss);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.processState, original.lastExitDetailInfo.processState);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.timestamp, original.lastExitDetailInfo.timestamp);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.processName, original.lastExitDetailInfo.processName);
|
||||
EXPECT_EQ(result->lastExitDetailInfo.exitMsg, original.lastExitDetailInfo.exitMsg);
|
||||
EXPECT_EQ(result->launchUptime, original.launchUptime);
|
||||
EXPECT_EQ(result->launchUTCTime, original.launchUTCTime);
|
||||
|
||||
delete result;
|
||||
}
|
||||
|
||||
} // namespace AAFwk
|
||||
} // namespace OHOS
|
||||
Reference in New Issue
Block a user