diff --git a/begetd.gni b/begetd.gni index 3b74061ca808dc8adcc1993d438841b88e8adfcc..1bcc555e8d9daab81c7468ff5492c74aeec1cc20 100644 --- a/begetd.gni +++ b/begetd.gni @@ -90,5 +90,5 @@ declare_args() { init_feature_custom_sandbox = false # init sasn support - init_feature_support_asan = false + init_feature_support_asan = true } diff --git a/services/init/standard/init_firststage.c b/services/init/standard/init_firststage.c index 59f08633cdf9f1c2f4a73622f136e074127e50c5..af34e652ae5d1f3f89b503144370863201987315 100755 --- a/services/init/standard/init_firststage.c +++ b/services/init/standard/init_firststage.c @@ -135,6 +135,9 @@ static void MountRequiredPartitions(void) #ifdef ASAN_DETECTOR static void ChekcAndRunAsanInit(char * const args[]) { +#ifdef STARTUP_INIT_TEST + return; +#endif const char* asanInitPath = "/system/asan/bin/init"; char rebootReason[MAX_BUFFER_LEN] = {0}; INIT_LOGI("ChekcAndRunAsan Begin"); @@ -155,8 +158,8 @@ static void ChekcAndRunAsanInit(char * const args[]) OpenKmsg(); setenv("ASAN_OPTIONS", "include=/system/etc/asan.options", 1); - setenv("TSAN_OPTIONS", "include=/system/etc/asan.options", 1); - setenv("UBSAN_OPTIONS", "include=/system/etc/asan.options", 1); + setenv("TSAN_OPTIONS", "include=/system/etc/tsan.options", 1); + setenv("UBSAN_OPTIONS", "print_stacktrace=1:print_module_map=2:log_exe_name=1", 1); setenv("HWASAN_OPTIONS", "include=/system/etc/asan.options", 1); INIT_LOGI("Execute %s, process id %d.", asanInitPath, getpid()); if (execv(asanInitPath, args) != 0) { @@ -164,7 +167,7 @@ static void ChekcAndRunAsanInit(char * const args[]) } } #endif -static void StartSecondStageInit(long long uptime) +INIT_STATIC void StartSecondStageInit(long long uptime) { INIT_LOGI("Start init second stage."); // It will panic if close stdio before execv("/bin/sh", NULL) @@ -182,7 +185,9 @@ static void StartSecondStageInit(long long uptime) NULL, }; #ifdef ASAN_DETECTOR - ChekcAndRunAsanInit(args); + if (access("/log/asanEnable", F_OK) == 0) { + ChekcAndRunAsanInit(args); + } #endif if (execv("/bin/init", args) != 0) { INIT_LOGE("Failed to exec \"/bin/init\", err = %d", errno); diff --git a/test/BUILD.gn b/test/BUILD.gn index d292b6b81939af8c56787726a1b3b009305714b5..5b0fe408e4513712ed9f96fa43d34844297eaadf 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -24,6 +24,7 @@ group("testgroup") { "unittest:init_dmverify_unittest", "unittest:init_fshvb_unittest", "unittest:init_unittest", + "unittest/single_test/init_firststage:init_firststage_unittest", ] } else { if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { diff --git a/test/unittest/single_test/init_firststage/BUILD.gn b/test/unittest/single_test/init_firststage/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..985ed1eb204f1837887487f761f643e52143f6a6 --- /dev/null +++ b/test/unittest/single_test/init_firststage/BUILD.gn @@ -0,0 +1,60 @@ +# 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("//base/startup/init/begetd.gni") +import("//build/test.gni") + +ohos_unittest("init_firststage_unittest") { + module_out_path = "init/init" + include_dirs = [ + "//base/startup/init/test/unittest/single_test/init_firststage/include", + "//base/startup/init/services/init/include", + "//base/startup/init/services/log", + "//base/startup/init/services/init/standard", + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/ueventd/include", + "//base/startup/init/interfaces/innerkits/fs_manager/switch_root/include", + "//base/startup/init/interfaces/innerkits/init_module_engine/include", + ] + + sources = [ + "//base/startup/init/services/init/standard/init_firststage.c", + "//base/startup/init/test/unittest/single_test/init_firststage/src/init_firststage_test.cpp", + ] + + cflags_cc = [ "-fexceptions" ] + cflags = [ + "-Dexecv=ExecvStub", + "-DCloseStdio=CloseStdioStub", + "-Daccess=AccessStub", + ] + + deps = [ + "//base/startup/init/interfaces/innerkits:libbegetutil", + "//base/startup/init/interfaces/innerkits/fs_manager:libfsmanager_static", + ] + + defines = [ + "STARTUP_INIT_UT_PATH =\"/data/init_ut\"", + "ASAN_DETECTOR", + "STARTUP_INIT_TEST", + ] + + configs = [] + + external_deps = [ + "cJSON:cjson", + "c_utils:utils", + "googletest:gtest", + "hilog:libhilog", + ] +} diff --git a/test/unittest/single_test/init_firststage/include/init_firststage_test.h b/test/unittest/single_test/init_firststage/include/init_firststage_test.h new file mode 100644 index 0000000000000000000000000000000000000000..9d82a5655a5e5f6fe5b3eed201899199686136f9 --- /dev/null +++ b/test/unittest/single_test/init_firststage/include/init_firststage_test.h @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#ifndef INIT_FIRSTSTAGE_TEST_H +#define INIT_FIRSTSTAGE_TEST_H + +#ifdef __cplusplus +extern "C" { +#endif + +void StartSecondStageInit(long long uptime); + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/test/unittest/single_test/init_firststage/src/init_firststage_test.cpp b/test/unittest/single_test/init_firststage/src/init_firststage_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c93be8c4ce78432e97dfd7bfa5e0949060a5467c --- /dev/null +++ b/test/unittest/single_test/init_firststage/src/init_firststage_test.cpp @@ -0,0 +1,98 @@ +/* + * 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 "init_firststage_test.h" +#include + +#include + +using namespace testing; +using namespace testing::ext; +static int g_access = 0; + + +#ifdef __cplusplus + extern "C" { +#endif +int ExecvStub(const char *pathname, char *const argv[]) +{ + printf("do execv %s \n", pathname); + return 0; +} + +int CreateTestFile(const char *fileName, const char *data) +{ + FILE *tmpFile = fopen(fileName, "wr"); + if (tmpFile != nullptr) { + fprintf(tmpFile, "%s", data); + (void)fflush(tmpFile); + fclose(tmpFile); + } + return tmpFile != nullptr; +} + +int AccessStub(const char *pathname, int mode) +{ + printf("access %s \n", pathname); + return g_access; +} + +void CloseStdioStub(void) +{ + printf("close stdio \n"); +} + +#ifdef __cplusplus + } +#endif + +namespace OHOS { +class InitFirststageTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void InitFirststageTest::SetUpTestCase() +{ + GTEST_LOG_(INFO) << "Hnp_Installer_TEST SetUpTestCase"; +} + +void InitFirststageTest::TearDownTestCase() +{ + GTEST_LOG_(INFO) << "Hnp_Installer_TEST TearDownTestCase"; +} + +void InitFirststageTest::SetUp() +{ + GTEST_LOG_(INFO) << "Hnp_Installer_TEST SetUp"; +} + +void InitFirststageTest::TearDown() +{ + GTEST_LOG_(INFO) << "Hnp_Installer_TEST TearDown"; +} + +HWTEST_F(InitFirststageTest, AsanTest_001, TestSize.Level0) +{ + int ret = CreateTestFile("/log/asanTest", "test"); + StartSecondStageInit(0); + g_access = -1; + StartSecondStageInit(0); + EXPECT_TRUE(ret); +} + +} \ No newline at end of file