15ba71b47Sopenharmony_ci/* 25ba71b47Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 35ba71b47Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 45ba71b47Sopenharmony_ci * you may not use this file except in compliance with the License. 55ba71b47Sopenharmony_ci * You may obtain a copy of the License at 65ba71b47Sopenharmony_ci * 75ba71b47Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 85ba71b47Sopenharmony_ci * 95ba71b47Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 105ba71b47Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 115ba71b47Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125ba71b47Sopenharmony_ci * See the License for the specific language governing permissions and 135ba71b47Sopenharmony_ci * limitations under the License. 145ba71b47Sopenharmony_ci */ 155ba71b47Sopenharmony_ci 165ba71b47Sopenharmony_ci#include "samgrdumper_fuzzer.h" 175ba71b47Sopenharmony_ci 185ba71b47Sopenharmony_ci#define private public 195ba71b47Sopenharmony_ci#include "system_ability_manager.h" 205ba71b47Sopenharmony_ci#include "system_ability_manager_dumper.h" 215ba71b47Sopenharmony_ci#include "schedule/system_ability_state_scheduler.h" 225ba71b47Sopenharmony_ci#include "sam_mock_permission.h" 235ba71b47Sopenharmony_ci 245ba71b47Sopenharmony_cinamespace OHOS { 255ba71b47Sopenharmony_cinamespace Samgr { 265ba71b47Sopenharmony_cinamespace { 275ba71b47Sopenharmony_ci constexpr size_t THRESHOLD = 10; 285ba71b47Sopenharmony_ci constexpr const char* HIDUMPER_PROCESS_NAME = "hidumper_service"; 295ba71b47Sopenharmony_ci} 305ba71b47Sopenharmony_ci 315ba71b47Sopenharmony_ciint32_t BuildInt32FromData(const uint8_t* data, size_t size) 325ba71b47Sopenharmony_ci{ 335ba71b47Sopenharmony_ci if ((data == nullptr) || (size < sizeof(int32_t))) { 345ba71b47Sopenharmony_ci return 0; 355ba71b47Sopenharmony_ci } 365ba71b47Sopenharmony_ci int32_t int32Val = *reinterpret_cast<const int32_t *>(data); 375ba71b47Sopenharmony_ci return int32Val; 385ba71b47Sopenharmony_ci} 395ba71b47Sopenharmony_ci 405ba71b47Sopenharmony_cistd::string BuildStringFromData(const uint8_t* data, size_t size) 415ba71b47Sopenharmony_ci{ 425ba71b47Sopenharmony_ci if ((data == nullptr) || (size == 0)) { 435ba71b47Sopenharmony_ci return ""; 445ba71b47Sopenharmony_ci } 455ba71b47Sopenharmony_ci std::string strVal(reinterpret_cast<const char *>(data), size); 465ba71b47Sopenharmony_ci return strVal; 475ba71b47Sopenharmony_ci} 485ba71b47Sopenharmony_ci 495ba71b47Sopenharmony_civoid SamgrDumperFuzzTest(const uint8_t* data, size_t size) 505ba71b47Sopenharmony_ci{ 515ba71b47Sopenharmony_ci SamMockPermission::MockProcess(HIDUMPER_PROCESS_NAME); 525ba71b47Sopenharmony_ci std::string strVal = BuildStringFromData(data, size); 535ba71b47Sopenharmony_ci std::vector<std::string> args; 545ba71b47Sopenharmony_ci SplitStr(strVal, " ", args); 555ba71b47Sopenharmony_ci std::vector<std::u16string> argsWithStr16; 565ba71b47Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 575ba71b47Sopenharmony_ci argsWithStr16.emplace_back(Str8ToStr16(args[i])); 585ba71b47Sopenharmony_ci } 595ba71b47Sopenharmony_ci std::string result; 605ba71b47Sopenharmony_ci std::shared_ptr<SystemAbilityStateScheduler> scheduler = std::make_shared<SystemAbilityStateScheduler>(); 615ba71b47Sopenharmony_ci int32_t fd = -1; 625ba71b47Sopenharmony_ci SystemAbilityManagerDumper::FfrtDumpProc(scheduler, fd, args); 635ba71b47Sopenharmony_ci SystemAbilityManagerDumper::Dump(scheduler, args, result); 645ba71b47Sopenharmony_ci int32_t cmd = -1; 655ba71b47Sopenharmony_ci SystemAbilityManagerDumper::IpcDumpCmdParser(cmd, args); 665ba71b47Sopenharmony_ci std::string processName = BuildStringFromData(data, size); 675ba71b47Sopenharmony_ci SystemAbilityManagerDumper::IpcDumpIsSamgr(processName); 685ba71b47Sopenharmony_ci SystemAbilityManagerDumper::IpcDumpIsAllProcess(processName); 695ba71b47Sopenharmony_ci SystemAbilityManagerDumper::GetSamgrIpcStatistics(result); 705ba71b47Sopenharmony_ci SystemAbilityManagerDumper::StopSamgrIpcStatistics(result); 715ba71b47Sopenharmony_ci SystemAbilityManagerDumper::StartSamgrIpcStatistics(result); 725ba71b47Sopenharmony_ci SystemAbilityManagerDumper::GetFfrtDumpInfoProc(scheduler, args, result); 735ba71b47Sopenharmony_ci SystemAbilityManagerDumper::GetSAMgrFfrtInfo(result); 745ba71b47Sopenharmony_ci int32_t pid = BuildInt32FromData(data, size); 755ba71b47Sopenharmony_ci SystemAbilityManagerDumper::DumpFfrtInfoByProcName(pid, Str8ToStr16(processName), result); 765ba71b47Sopenharmony_ci 775ba71b47Sopenharmony_ci std::shared_ptr<SystemAbilityManager> manager = std::make_shared<SystemAbilityManager>(); 785ba71b47Sopenharmony_ci manager->abilityStateScheduler_ = std::make_shared<SystemAbilityStateScheduler>(); 795ba71b47Sopenharmony_ci manager->Dump(fd, argsWithStr16); 805ba71b47Sopenharmony_ci manager->IpcDumpProc(fd, args); 815ba71b47Sopenharmony_ci manager->IpcDumpAllProcess(fd, cmd); 825ba71b47Sopenharmony_ci manager->IpcDumpSamgrProcess(fd, cmd); 835ba71b47Sopenharmony_ci manager->IpcDumpSingleProcess(fd, cmd, processName); 845ba71b47Sopenharmony_ci} 855ba71b47Sopenharmony_ci} 865ba71b47Sopenharmony_ci} 875ba71b47Sopenharmony_ci 885ba71b47Sopenharmony_ci/* Fuzzer entry point */ 895ba71b47Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 905ba71b47Sopenharmony_ci{ 915ba71b47Sopenharmony_ci if (size < OHOS::Samgr::THRESHOLD) { 925ba71b47Sopenharmony_ci return 0; 935ba71b47Sopenharmony_ci } 945ba71b47Sopenharmony_ci 955ba71b47Sopenharmony_ci OHOS::Samgr::SamgrDumperFuzzTest(data, size); 965ba71b47Sopenharmony_ci 975ba71b47Sopenharmony_ci return 0; 985ba71b47Sopenharmony_ci} 995ba71b47Sopenharmony_ci 100