diff --git a/compiler_service/include/aot_args_handler.h b/compiler_service/include/aot_args_handler.h index 9af571ebbe904dae38ecbc1850fd10afc7b84664..39e3a5e497766b2663a647dd89c6b5514f533513 100644 --- a/compiler_service/include/aot_args_handler.h +++ b/compiler_service/include/aot_args_handler.h @@ -34,7 +34,7 @@ class AOTArgsParserBase; class AOTArgsHandler { public: - AOTArgsHandler(const std::unordered_map &argsMap); + AOTArgsHandler(const std::unordered_map &argsMap, bool IsEnableStaticCompiler = false); AOTArgsHandler() = default; ~AOTArgsHandler() = default; @@ -108,8 +108,9 @@ public: class AOTArgsParserFactory { public: - static std::optional> GetParser(const std::unordered_map &argsMap); + static std::optional> GetParser( + const std::unordered_map &argsMap, bool IsEnableStaticCompiler = false); }; } // namespace OHOS::ArkCompiler #endif // OHOS_ARKCOMPILER_AOT_ARGS_HANDLER_H + \ No newline at end of file diff --git a/compiler_service/src/aot_args_handler.cpp b/compiler_service/src/aot_args_handler.cpp index ab2f6cee9b0ffc428022806a0df026f610a2d893..3f6c27c05a08fc82a4206ebae7e18f1e6fe14226 100644 --- a/compiler_service/src/aot_args_handler.cpp +++ b/compiler_service/src/aot_args_handler.cpp @@ -49,13 +49,19 @@ const std::string ETS_PATH = "/ets"; const std::string OWNERID_SHARED_TAG = "SHARED_LIB_ID"; #ifdef ENABLE_COMPILER_SERVICE_GET_PARAMETER -// Default closed, open on qiangji +// disable on master branch defaultly, only enable on feature branch const bool ARK_AOT_ENABLE_STATIC_COMPILER_DEFAULT_VALUE = false; #endif -AOTArgsHandler::AOTArgsHandler(const std::unordered_map &argsMap) : argsMap_(argsMap) +AOTArgsHandler::AOTArgsHandler(const std::unordered_map &argsMap, + bool IsEnableStaticCompiler) : argsMap_(argsMap) { - parser_ = *AOTArgsParserFactory::GetParser(argsMap); + auto parserOpt = AOTArgsParserFactory::GetParser(argsMap, IsEnableStaticCompiler); + if (parserOpt.has_value()) { + parser_ = std::move(parserOpt.value()); + } else { + parser_ = nullptr; + } } int32_t AOTArgsHandler::Handle(int32_t thermalLevel) @@ -64,6 +70,10 @@ int32_t AOTArgsHandler::Handle(int32_t thermalLevel) LOG_SA(ERROR) << "pass empty args to aot sa"; return ERR_AOT_COMPILER_PARAM_FAILED; } + if (!parser_) { + LOG_SA(ERROR) << "AOTArgsParser is null, invalid parameters"; + return ERR_AOT_COMPILER_PARAM_FAILED; + } std::lock_guard lock(hapArgsMutex_); int32_t ret = parser_->Parse(argsMap_, hapArgs_, thermalLevel); @@ -300,13 +310,24 @@ std::string StaticAOTArgsParser::ParseLocation(std::string &anFilePath) } std::optional> AOTArgsParserFactory::GetParser( - const std::unordered_map &argsMap) + const std::unordered_map &argsMap, [[maybe_unused]] bool IsEnableStaticCompiler) { -#ifdef ENABLE_COMPILER_SERVICE_GET_PARAMETER - if (!AOTArgsParserBase::IsEnableStaticCompiler()) { + std::string arkTsMode; + if (AOTArgsParserBase::FindArgsIdxToString(argsMap, ArgsIdx::ARKTS_MODE, arkTsMode) != ERR_OK) { + LOG_SA(INFO) << "aot sa failed to get arkTsMode"; + arkTsMode = ARKTS_DYNAMIC; + } + if (arkTsMode == ARKTS_DYNAMIC) { + LOG_SA(INFO) << "aot sa use default compiler"; return std::make_unique(); + } else if (arkTsMode != ARKTS_STATIC && arkTsMode != ARKTS_HYBRID) { + LOG_SA(ERROR) << "aot sa get invalid code arkTsMode"; + return std::nullopt; + } + // After this, only arkTsMode that is static or hybrid will proceed downwards + if (!IsEnableStaticCompiler) { + return std::nullopt; } -#endif int32_t isSystemComponent = 0; if ((AOTArgsParserBase::FindArgsIdxToInteger(argsMap, ArgsIdx::IS_SYSTEM_COMPONENT, isSystemComponent) != ERR_OK)) { LOG_SA(INFO) << "aot sa failed to get isSystemComponent"; @@ -314,21 +335,7 @@ std::optional> AOTArgsParserFactory::GetParse if (isSystemComponent) { return std::make_unique(); } - std::string arkTsMode; - if (AOTArgsParserBase::FindArgsIdxToString(argsMap, ArgsIdx::ARKTS_MODE, arkTsMode) != ERR_OK) { - LOG_SA(INFO) << "aot sa failed to get language version"; - arkTsMode = ARKTS_DYNAMIC; - } - - if (arkTsMode == ARKTS_DYNAMIC) { - LOG_SA(INFO) << "aot sa use default compiler"; - return std::make_unique(); - } else if (arkTsMode == ARKTS_STATIC || arkTsMode == ARKTS_HYBRID) { - LOG_SA(INFO) << "aot sa use static compiler"; - return std::make_unique(); - } - LOG_SA(FATAL) << "aot sa get invalid code language version"; - return std::nullopt; + return std::make_unique(); } bool StaticFrameworkAOTArgsParser::IsFileExists(const std::string &fileName) diff --git a/compiler_service/src/aot_compiler_impl.cpp b/compiler_service/src/aot_compiler_impl.cpp index 6fc85de2d9700f13335ef98c9a70c6263401d940..f2c6690657c04c34c4583f13f4b1369f69861435 100644 --- a/compiler_service/src/aot_compiler_impl.cpp +++ b/compiler_service/src/aot_compiler_impl.cpp @@ -167,8 +167,11 @@ int32_t AotCompilerImpl::EcmascriptAotCompiler(const std::unordered_map(argsMap); + bool IsEnableStaticCompiler = false; +#ifdef ENABLE_COMPILER_SERVICE_GET_PARAMETER + IsEnableStaticCompiler = AOTArgsParserBase::IsEnableStaticCompiler(); +#endif + argsHandler_ = std::make_unique(argsMap, IsEnableStaticCompiler); if (argsHandler_->Handle(thermalLevel_) != ERR_OK) { return ERR_AOT_COMPILER_PARAM_FAILED; } diff --git a/compiler_service/test/unittest/aotcompilerargshandler_unit/BUILD.gn b/compiler_service/test/unittest/aotcompilerargshandler_unit/BUILD.gn index 4d0b7078dbb80b742ab3b9956cc0386978a8b10a..4234503693de78d1a92c11c04580a965ade16916 100644 --- a/compiler_service/test/unittest/aotcompilerargshandler_unit/BUILD.gn +++ b/compiler_service/test/unittest/aotcompilerargshandler_unit/BUILD.gn @@ -81,6 +81,8 @@ ohos_unittest("AotCompilerArgsHandlerUnitTest") { "samgr:samgr_proxy", ] defines = [] + external_deps += [ "init:libbegetutil" ] + defines += [ "ENABLE_COMPILER_SERVICE_GET_PARAMETER" ] if (code_sign_enable_test) { external_deps += [ "code_signature:liblocal_code_sign_sdk" ] defines += [ "CODE_SIGN_ENABLE" ] diff --git a/compiler_service/test/unittest/aotcompilerargshandler_unit/aotcompilerargshandler_unit.cpp b/compiler_service/test/unittest/aotcompilerargshandler_unit/aotcompilerargshandler_unit.cpp index 8a9fa88cf2461f20511a13b208087fe6cd7454e7..01097e862806f0e7b24a73db41322865bb8a50c0 100644 --- a/compiler_service/test/unittest/aotcompilerargshandler_unit/aotcompilerargshandler_unit.cpp +++ b/compiler_service/test/unittest/aotcompilerargshandler_unit/aotcompilerargshandler_unit.cpp @@ -36,8 +36,25 @@ public: static void SetUpTestCase() {} static void TearDownTestCase() {} - void SetUp() override {} - void TearDown() override {}; + void SetUp() override { + mkdir(systemDir_, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + mkdir(systemFrameworkDir_, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + std::string bootpathJsonStr = + "{\"bootpath\":\"/system/framework/etsstdlib_bootabc.abc:/system/framework/arkoala.abc\"}"; + std::ofstream file(bootPathJson_); + file << bootpathJsonStr << std::endl; + file.close(); + } + + void TearDown() override { + unlink(bootPathJson_); + rmdir(systemFrameworkDir_); + rmdir(bootPathJson_); + }; + + const char *systemDir_ = "/system"; + const char *systemFrameworkDir_ = "/system/framework"; + const char *bootPathJson_ = "/system/framework/bootpath.json"; }; /** @@ -168,7 +185,7 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_007, TestSize.Level0) { std::unordered_map argsMap(argsMapForTest); argsMap.emplace(ArgsIdx::ARKTS_MODE, "dynamic"); - std::unique_ptr argsHandler = std::make_unique(argsMap); + std::unique_ptr argsHandler = std::make_unique(argsMap, true); int32_t ret = argsHandler->Handle(0); EXPECT_EQ(ret, ERR_OK); std::vector argv = argsHandler->GetAotArgs(); @@ -184,7 +201,7 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_008, TestSize.Level0) { std::unordered_map argsMap(argsMapForTest); argsMap.emplace(ArgsIdx::ARKTS_MODE, "static"); - std::unique_ptr argsHandler = std::make_unique(argsMap); + std::unique_ptr argsHandler = std::make_unique(argsMap, true); int32_t ret = argsHandler->Handle(0); EXPECT_EQ(ret, ERR_OK); std::vector argv = argsHandler->GetAotArgs(); @@ -209,24 +226,14 @@ const std::unordered_map framewordArgsMapForTest { {"outputPath", "/data/service/el1/public/for-all-app/framework_ark_cache/etsstdlib_bootabc.an"}, {"anFileName", "/data/service/el1/public/for-all-app/framework_ark_cache/etsstdlib_bootabc.an"}, {"isSysComp", "1"}, + {ArgsIdx::ARKTS_MODE, "static"}, {"sysCompPath", "/system/framework/etsstdlib_bootabc.abc"}, {"ABC-Path", "/system/framework/etsstdlib_bootabc.abc"} }; HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_010, TestSize.Level0) { - const char *systemDir = "/system"; - const char *systemFrameworkDir = "/system/framework"; - const char *bootPathJson = "/system/framework/bootpath.json"; - mkdir(systemDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - mkdir(systemFrameworkDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - std::string bootpathJsonStr = - "{\"bootpath\":\"/system/framework/etsstdlib_bootabc.abc:/system/framework/arkoala.abc\"}"; - std::ofstream file(bootPathJson); - file << bootpathJsonStr << std::endl; - file.close(); - - std::unique_ptr argsHandler = std::make_unique(framewordArgsMapForTest); + std::unique_ptr argsHandler = std::make_unique(framewordArgsMapForTest, true); int32_t ret = argsHandler->Handle(0); EXPECT_EQ(ret, ERR_OK); std::string fileName = argsHandler->GetFileName(); @@ -243,9 +250,6 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_010, TestSize.Level0) EXPECT_STREQ(arg, "--boot-panda-files=/system/framework/etsstdlib_bootabc.abc"); } } - unlink(bootPathJson); - rmdir(systemFrameworkDir); - rmdir(systemDir); } /** @@ -259,7 +263,7 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_011, TestSize.Level0) { {ArgsIdx::ARKTS_MODE, "static"} }; - auto result = AOTArgsParserFactory::GetParser(argsMap); + auto result = AOTArgsParserFactory::GetParser(argsMap, true); EXPECT_TRUE(result.has_value()); EXPECT_NE(result.value(), nullptr); } @@ -274,7 +278,7 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_012, TestSize.Level0) { {ArgsIdx::ARKTS_MODE, "hybrid"} }; - auto result = AOTArgsParserFactory::GetParser(argsMap); + auto result = AOTArgsParserFactory::GetParser(argsMap, true); EXPECT_TRUE(result.has_value()); EXPECT_NE(result.value(), nullptr); } @@ -291,7 +295,7 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_013, TestSize.Level0) {ArgsIdx::ARKTS_MODE, "dynamic"} }; - auto result = AOTArgsParserFactory::GetParser(argsMap); + auto result = AOTArgsParserFactory::GetParser(argsMap, true); EXPECT_TRUE(result.has_value()); EXPECT_NE(result.value(), nullptr); } @@ -308,7 +312,7 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_014, TestSize.Level0) {ArgsIdx::ARKTS_MODE, "dynamic"} }; - auto result = AOTArgsParserFactory::GetParser(argsMap); + auto result = AOTArgsParserFactory::GetParser(argsMap, true); EXPECT_NE(result, nullptr); EXPECT_NE(result.value(), nullptr); } @@ -324,8 +328,72 @@ HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_015, TestSize.Level0) {ArgsIdx::IS_SYSTEM_COMPONENT, "0"} }; - auto result = AOTArgsParserFactory::GetParser(argsMap); + auto result = AOTArgsParserFactory::GetParser(argsMap, true); EXPECT_TRUE(result.has_value()); EXPECT_NE(result.value(), nullptr); } -} // namespace OHOS::ArkCompiler \ No newline at end of file + +HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_016, TestSize.Level0) +{ + std::unordered_map argsMap = { + {ArgsIdx::ARKTS_MODE, "static"}, + {ArgsIdx::IS_SYSTEM_COMPONENT, "1"} + }; + std::unique_ptr argsHandler = std::make_unique(argsMap); + int32_t ret = argsHandler->Handle(0); + EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED); +} + +HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_017, TestSize.Level0) +{ + std::unordered_map argsMap = { + {ArgsIdx::ARKTS_MODE, "dynamic"}, + {ArgsIdx::IS_SYSTEM_COMPONENT, "0"} + }; + std::unique_ptr argsHandler = std::make_unique(argsMap); + int32_t ret = argsHandler->Handle(0); + EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED); +} + +HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_018, TestSize.Level0) +{ + std::unordered_map argsMap = { + {ArgsIdx::IS_SYSTEM_COMPONENT, "0"} + }; + std::unique_ptr argsHandler = std::make_unique(argsMap); + int32_t ret = argsHandler->Handle(0); + EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED); +} + +HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_019, TestSize.Level0) +{ + std::unordered_map argsMap = { + {ArgsIdx::ARKTS_MODE, "static"}, + {ArgsIdx::IS_SYSTEM_COMPONENT, "1"} + }; + std::unique_ptr argsHandler = std::make_unique(argsMap, true); + int32_t ret = argsHandler->Handle(0); + EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED); +} + +HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_020, TestSize.Level0) +{ + std::unordered_map argsMap = { + {ArgsIdx::ARKTS_MODE, "static"}, + {ArgsIdx::IS_SYSTEM_COMPONENT, "0"} + }; + std::unique_ptr argsHandler = std::make_unique(argsMap, true); + int32_t ret = argsHandler->Handle(0); + EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED); +} + +HWTEST_F(AotArgsHandlerTest, AotArgsHandlerTest_021, TestSize.Level0) +{ + std::unordered_map argsMap = { + {ArgsIdx::ARKTS_MODE, "INVALID_VALUE"} + }; + std::unique_ptr argsHandler = std::make_unique(argsMap, true); + int32_t ret = argsHandler->Handle(0); + EXPECT_EQ(ret, ERR_AOT_COMPILER_PARAM_FAILED); +} +} // namespace OHOS::ArkCompiler \ No newline at end of file