diff --git a/src/perf_event_record.cpp b/src/perf_event_record.cpp index 11665f16b1d50173595e3d084b09e9f25702c7ec..5eee9905bf235b74154d48839816b1a3ca5f2dc0 100644 --- a/src/perf_event_record.cpp +++ b/src/perf_event_record.cpp @@ -139,15 +139,15 @@ void PerfEventRecord::GetHeaderBinary(std::vector &buf) const void PerfEventRecord::Dump(int indent, std::string outputFilename, FILE *outputDump) const { - if (outputDump != nullptr) { - g_outputDump = outputDump; - } else if (!outputFilename.empty() && g_outputDump == nullptr) { + if (!outputFilename.empty() && g_outputDump == nullptr) { std::string resolvedPath = CanonicalizeSpecPath(outputFilename.c_str()); g_outputDump = fopen(resolvedPath.c_str(), "w"); if (g_outputDump == nullptr) { printf("unable open file to '%s' because '%d'\n", outputFilename.c_str(), errno); return; } + } else { + g_outputDump = outputDump; } PRINT_INDENT(indent, "\n"); PRINT_INDENT(indent, "record %s: type %u, misc %u, size %zu\n", GetName(), GetType(), diff --git a/src/subcommand_dump.cpp b/src/subcommand_dump.cpp index 48bb81e11c8bb244be957ff57812e0c9d8af80aa..4325ff0b216ff56d87e3a70ab6974b49002dc1a5 100644 --- a/src/subcommand_dump.cpp +++ b/src/subcommand_dump.cpp @@ -189,6 +189,10 @@ HiperfError SubCommandDump::OnSubCommand(std::vector& args) DumpFeaturePortion(indent_); } + if (g_outputDump != nullptr && g_outputDump != stdout) { + fclose(g_outputDump); + g_outputDump = nullptr; + } return HiperfError::NO_ERR; } diff --git a/src/subcommand_report.cpp b/src/subcommand_report.cpp index 31b9711867cd9769b54de91f10fce620884bee3a..c7d09eb2be4e336b0faf5a3cdb5d40fb53be2b0f 100644 --- a/src/subcommand_report.cpp +++ b/src/subcommand_report.cpp @@ -152,11 +152,11 @@ bool SubCommandReport::VerifyOption() const float min = 0.0; const float max = 100.0; if (reportOption_.heatLimit_ < min || reportOption_.heatLimit_ > max) { - printf("head limit error. must in (0 <= limit < 100).\n"); + printf("head limit error. must in (0 <= limit <= 100).\n"); return false; } if (reportOption_.callStackHeatLimit_ < min || reportOption_.callStackHeatLimit_ > max) { - printf("head limit error. must in (0 <= limit < 100).\n"); + printf("head limit error. must in (0 <= limit <= 100).\n"); return false; } if (recordFile_[FIRST].empty()) { diff --git a/test/BUILD.gn b/test/BUILD.gn index 149250ccb52f5c51ccbc9bcb5f1edb0ea0d403e3..f96f0126c811efd8326385554cb13a01635313c4 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -310,17 +310,134 @@ ohos_fuzztest("PerfFileFormatFuzzTest") { sources = [ "fuzztest/perffileformat_fuzzer/PerfFileFormat_fuzzer.cpp" ] } +ohos_fuzztest("CommandLine100FuzzTest") { + module_out_path = hiperf_fuzz_test_output_path + fuzz_config_file = "${hiperf_path}/test/fuzztest/commandline100_fuzzer" + configs = [ ":hiperf_test_config" ] + cflags = fuzz_cflags + deps = common_deps + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "faultloggerd:libunwinder", + "hilog:libhilog", + "ipc:ipc_single", + "protobuf:protobuf_lite", + ] + defines = [ "FUZZER_TEST" ] + sources = [ + "${hiperf_path}/src/main.cpp", + "fuzztest/commandline100_fuzzer/CommandLine_fuzzer.cpp", + ] +} + +ohos_fuzztest("LibReport100FuzzTest") { + module_out_path = hiperf_fuzz_test_output_path + fuzz_config_file = "${hiperf_path}/test/fuzztest/libreport100_fuzzer" + configs = [ ":hiperf_test_config" ] + cflags = fuzz_cflags + deps = common_deps + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_single", + "protobuf:protobuf_lite", + ] + defines = [ "FUZZER_TEST" ] + sources = [ "fuzztest/libreport100_fuzzer/LibReport_fuzzer.cpp" ] +} + +ohos_fuzztest("ClientApi100FuzzTest") { + module_out_path = hiperf_fuzz_test_output_path + fuzz_config_file = "${hiperf_path}/test/fuzztest/clientapi100_fuzzer" + configs = [ ":hiperf_test_config" ] + cflags = fuzz_cflags + deps = common_deps + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_single", + "protobuf:protobuf_lite", + ] + defines = [ "FUZZER_TEST" ] + sources = [ "fuzztest/clientapi100_fuzzer/ClientApi_fuzzer.cpp" ] +} + +ohos_fuzztest("SpeDecoder100FuzzTest") { + module_out_path = hiperf_fuzz_test_output_path + fuzz_config_file = "${hiperf_path}/test/fuzztest/spedecoder100_fuzzer" + configs = [ ":hiperf_test_config" ] + cflags = fuzz_cflags + deps = common_deps + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_single", + "protobuf:protobuf_lite", + ] + defines = [ "FUZZER_TEST" ] + sources = [ "fuzztest/spedecoder100_fuzzer/SpeDecoder_fuzzer.cpp" ] +} + +ohos_fuzztest("PerfFile100FuzzTest") { + module_out_path = hiperf_fuzz_test_output_path + fuzz_config_file = "${hiperf_path}/test/fuzztest/perffile100_fuzzer" + resource_config_file = "fuzztest/resource/ohos_test.xml" + configs = [ ":hiperf_test_config" ] + cflags = fuzz_cflags + deps = common_deps + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "faultloggerd:libunwinder", + "hilog:libhilog", + "ipc:ipc_single", + "protobuf:protobuf_lite", + ] + defines = [ "FUZZER_TEST" ] + sources = [ "fuzztest/perffile100_fuzzer/PerfFile_fuzzer.cpp" ] +} + +ohos_fuzztest("PerfFileFormat100FuzzTest") { + module_out_path = hiperf_fuzz_test_output_path + fuzz_config_file = "${hiperf_path}/test/fuzztest/perffileformat100_fuzzer" + configs = [ ":hiperf_test_config" ] + cflags = fuzz_cflags + deps = common_deps + external_deps = [ + "bounds_checking_function:libsec_shared", + "c_utils:utils", + "faultloggerd:libunwinder", + "hilog:libhilog", + "ipc:ipc_single", + "protobuf:protobuf_lite", + ] + defines = [ "FUZZER_TEST" ] + sources = [ "fuzztest/perffileformat100_fuzzer/PerfFileFormat_fuzzer.cpp" ] +} + group("hiperf_fuzztest") { testonly = true deps = [ + ":ClientApi100FuzzTest", ":ClientApiFuzzTest", + ":CommandLine100FuzzTest", ":CommandLineFuzzTest", + ":PerfFile100FuzzTest", + ":PerfFileFormat100FuzzTest", ":PerfFileFormatFuzzTest", ":PerfFileFuzzTest", + ":SpeDecoder100FuzzTest", ":SpeDecoderFuzzTest", ] if (!ohos_indep_compiler_enable) { - deps += [ ":LibReportFuzzTest" ] + deps += [ + ":LibReport100FuzzTest", + ":LibReportFuzzTest", + ] } } diff --git a/test/fuzztest/clientapi100_fuzzer/ClientApi_fuzzer.cpp b/test/fuzztest/clientapi100_fuzzer/ClientApi_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..067cdee7b88075191a8e82d4424244de34879be2 --- /dev/null +++ b/test/fuzztest/clientapi100_fuzzer/ClientApi_fuzzer.cpp @@ -0,0 +1,115 @@ +/* + * 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 "ClientApi_fuzzer.h" + +using namespace OHOS::Developtools::HiPerf; + +namespace OHOS { +const static int32_t MAX_TEST_ARGS_NUMBER = 20; +const static size_t MAX_TEST_ARGS_LEN = 4096; +std::vector DataToStringVector(const uint8_t *data, size_t size) +{ + const char *argptr = reinterpret_cast(data); + const char *argsdata = argptr; + std::vector argv; + // argv[*] + for (size_t i = 0; i < std::min(MAX_TEST_ARGS_LEN, size); i++) { + if (argsdata[i] == '\0') { + argv.emplace_back(argptr); + argptr = &argsdata[i + 1]; + } + if (argv.size() > MAX_TEST_ARGS_NUMBER) { + break; + } + } + return argv; +} + +std::vector DataToPidVector(const uint8_t *data, size_t size) +{ + std::vector argv; + // argv[*] + for (size_t i = 0; i < std::min(MAX_TEST_ARGS_LEN, size); i++) { + argv.emplace_back(static_cast(data[i])); + if (argv.size() > MAX_TEST_ARGS_NUMBER) { + break; + } + } + return argv; +} + +bool FuzzClientApiOption(const uint8_t *data, size_t size) +{ + HiperfClient::RecordOption opt; + std::string stringArg(reinterpret_cast(data), size); + std::vector stringArgs = DataToStringVector(data, size); + std::vector pids = DataToPidVector(data, size); + + opt.SetOutputFilename(stringArg); + opt.SetTimeStopSec(size); + opt.SetFrequency(size); + opt.SetPeriod(size); + opt.SetSelectEvents(stringArgs); + opt.SetSelectGroups(stringArgs); + opt.SetSelectPids(pids); + opt.SetSelectTids(pids); + opt.SetCpuPercent(size); + opt.SetCallGraph(stringArg); + opt.SetSymbolDir(stringArg); + opt.SetDataLimit(stringArg); + opt.SetAppPackage(stringArg); + opt.SetClockId(stringArg); + opt.SetVecBranchSampleTypes(stringArgs); + opt.SetMmapPages(size); + opt.GetOptionVecString(); + + HiperfClient::Client myHiperf; + myHiperf.Start(opt); + myHiperf.Stop(); + return 0; +} + +bool FuzzClientApiClient(const uint8_t *data, size_t size) +{ + HiperfClient::Client myHiperf; + std::string stringArg(reinterpret_cast(data), size); + std::vector stringArgs = DataToStringVector(data, size); + + myHiperf.Setup(stringArg); + myHiperf.IsReady(); + myHiperf.Start(stringArgs); + myHiperf.Pause(); + myHiperf.Resume(); + myHiperf.Stop(); + return 0; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +/* Run your code on data */ +#ifdef DEBUG_HIPERF_FUZZ + ScopeDebugLevel mix(LEVEL_DEBUG, true); + DebugLogger::GetInstance()->Disable(false); +#else + OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w"); +#endif + + OHOS::FuzzClientApiClient(data, size); + OHOS::FuzzClientApiOption(data, size); + return 0; +} diff --git a/test/fuzztest/clientapi100_fuzzer/ClientApi_fuzzer.h b/test/fuzztest/clientapi100_fuzzer/ClientApi_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..4c83760c6446411c77d6787be8db915b45482fa9 --- /dev/null +++ b/test/fuzztest/clientapi100_fuzzer/ClientApi_fuzzer.h @@ -0,0 +1,34 @@ +/* + * 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 CLIENTAPI_FUZZER_H +#define CLIENTAPI_FUZZER_H + +#include +#include +#include +#include + +#include +#include + +#include "hiperf_client.h" +#include "utilities.h" + +#define FUZZ_PROJECT_NAME "ClientApi_fuzzer" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#endif // CLIENTAPI_FUZZER_H diff --git a/test/fuzztest/clientapi100_fuzzer/corpus/init b/test/fuzztest/clientapi100_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/test/fuzztest/clientapi100_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/clientapi100_fuzzer/project.xml b/test/fuzztest/clientapi100_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..941ab218c6927af3b691fbb43755a4cad73c221a --- /dev/null +++ b/test/fuzztest/clientapi100_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 100 + + 4096 + + diff --git a/test/fuzztest/clientapi_fuzzer/corpus/init b/test/fuzztest/clientapi_fuzzer/corpus/init index e06a62b8b7fd45613309e15673f23335bdd980db..eec10bc943fe02056a326119773380942f88cea6 100644 --- a/test/fuzztest/clientapi_fuzzer/corpus/init +++ b/test/fuzztest/clientapi_fuzzer/corpus/init @@ -12,5 +12,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + FUZZ \ No newline at end of file diff --git a/test/fuzztest/commandline100_fuzzer/CommandLine_fuzzer.cpp b/test/fuzztest/commandline100_fuzzer/CommandLine_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..333ecd6bfc56a6b4524bb350d3a6f3fbfeae9c8c --- /dev/null +++ b/test/fuzztest/commandline100_fuzzer/CommandLine_fuzzer.cpp @@ -0,0 +1,99 @@ +/* + * 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 "CommandLine_fuzzer.h" +#include "subcommand_help.h" +#include "subcommand_list.h" +#include "subcommand_record.h" +#include "subcommand_report.h" +#include "subcommand_stat.h" + +namespace OHOS { +const static int32_t MAX_TEST_ARGS_NUMBER = 20; +const static size_t MAX_TEST_ARGS_LEN = 4096; + +bool FuzzCommandLine(const char *subcommand, const uint8_t *data, size_t size) +{ + char buf[DATA_MAX_SIZE] = { 0 }; + if (memcpy_s(buf, sizeof(buf) - 1, data, size) != 0) { // 1 : make sure end of '\0' + return false; + } + const char *argptr = reinterpret_cast(buf); + const char *argsdata = argptr; + std::vector argv; + + // argv[0] + argv.emplace_back("hiperf"); + + // argv[1] + if (subcommand != nullptr and subcommand[0] != '\0') { + argv.emplace_back(subcommand); + } + + // argv[2] + argv.emplace_back(argptr); + + // argv[*] + for (size_t i = 0; i < std::min(MAX_TEST_ARGS_LEN, size); i++) { + if (argsdata[i] == '\0') { + argv.emplace_back(argptr); + argptr = &argsdata[i + 1]; + } + if (argv.size() > MAX_TEST_ARGS_NUMBER) { + break; + } + } +#ifdef DEBUG_HIPERF_FUZZ + std::cout << " size " << argv.size() << std::endl; +#endif + HiperfFuzzerMain(static_cast(argv.size()), argv.data()); + return 0; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +#ifdef DEBUG_HIPERF_FUZZ + ScopeDebugLevel mix(LEVEL_VERBOSE, true); + DebugLogger::GetInstance()->Disable(false); +#else + OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w"); +#endif + /* Run your code on data */ + OHOS::FuzzCommandLine("", data, size); + OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands(); + OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("stat", + std::make_unique()); + OHOS::FuzzCommandLine("stat", data, size); + OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands(); + OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("record", + std::make_unique()); + OHOS::FuzzCommandLine("record", data, size); + usleep(100000); // sleep 100000 us + OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands(); + OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("report", + std::make_unique()); + OHOS::FuzzCommandLine("report", data, size); + OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands(); + OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("list", + std::make_unique()); + OHOS::FuzzCommandLine("list", data, size); + OHOS::Developtools::HiPerf::SubCommand::ClearSubCommands(); + OHOS::Developtools::HiPerf::SubCommand::RegisterSubCommand("help", + std::make_unique()); + OHOS::FuzzCommandLine("help", data, size); + return 0; +} diff --git a/test/fuzztest/commandline100_fuzzer/CommandLine_fuzzer.h b/test/fuzztest/commandline100_fuzzer/CommandLine_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..4794a6cfbbd40a78d7d3cb9db4c1dfb1edee8cb5 --- /dev/null +++ b/test/fuzztest/commandline100_fuzzer/CommandLine_fuzzer.h @@ -0,0 +1,34 @@ +/* + * 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 COMMANDLINE_FUZZER_H +#define COMMANDLINE_FUZZER_H + +#include +#include +#include +#include + +#include +#include + +#include "utilities.h" + +#define FUZZ_PROJECT_NAME "CommandLine_fuzzer" + +extern int HiperfFuzzerMain(const int argc, const char *argv[]); +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#endif // COMMANDLINE_FUZZER_H diff --git a/test/fuzztest/commandline100_fuzzer/corpus/init b/test/fuzztest/commandline100_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/test/fuzztest/commandline100_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/commandline100_fuzzer/project.xml b/test/fuzztest/commandline100_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..d539bed1123e46a0d5df000a7dbe0074f04bab8b --- /dev/null +++ b/test/fuzztest/commandline100_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 60 + + 4096 + + diff --git a/test/fuzztest/commandline_fuzzer/corpus/init b/test/fuzztest/commandline_fuzzer/corpus/init index e06a62b8b7fd45613309e15673f23335bdd980db..300546081798c2aff85833bb4dd0c6ab3caef411 100644 --- a/test/fuzztest/commandline_fuzzer/corpus/init +++ b/test/fuzztest/commandline_fuzzer/corpus/init @@ -1,16 +1,16 @@ -/* - * Copyright (c) 2021 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. - */ - +/* + * Copyright (c) 2021 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. + */ + FUZZ \ No newline at end of file diff --git a/test/fuzztest/libreport100_fuzzer/LibReport_fuzzer.cpp b/test/fuzztest/libreport100_fuzzer/LibReport_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f271fb6066c0fbb5566e02703f6b6be6154ec5e --- /dev/null +++ b/test/fuzztest/libreport100_fuzzer/LibReport_fuzzer.cpp @@ -0,0 +1,52 @@ +/* + * 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 "LibReport_fuzzer.h" + +namespace OHOS { +bool FuzzLibReport(const uint8_t *data, size_t size) +{ + char buf[DATA_MAX_SIZE] = { 0 }; + if (memcpy_s(buf, sizeof(buf) - 1, data, size) != 0) { // 1 : make sure end of '\0' + return false; + } + const char *dataPtr = reinterpret_cast(buf); + + EchoLoopBack(dataPtr); + Report(dataPtr, dataPtr, dataPtr); + ReportJson(dataPtr, dataPtr); + ReportUnwindJson(dataPtr, dataPtr, dataPtr); + Dump(dataPtr); + ReportGetSymbolFiles(dataPtr); + ReportGetBuildId(dataPtr); + ReportGetElfArch(dataPtr); + + return 0; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +#ifdef DEBUG_HIPERF_FUZZ + ScopeDebugLevel mix(LEVEL_VERBOSE, true); + DebugLogger::GetInstance()->Disable(false); +#else + OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w"); +#endif + /* Run your code on data */ + OHOS::FuzzLibReport(data, size); + return 0; +} diff --git a/test/fuzztest/libreport100_fuzzer/LibReport_fuzzer.h b/test/fuzztest/libreport100_fuzzer/LibReport_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..35d06f306139830c8a2f39889b7056ff3b425e48 --- /dev/null +++ b/test/fuzztest/libreport100_fuzzer/LibReport_fuzzer.h @@ -0,0 +1,34 @@ +/* + * 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 LIBREPORT_FUZZER_H +#define LIBREPORT_FUZZER_H + +#include +#include +#include +#include + +#include +#include + +#include "hiperf_libreport.h" +#include "utilities.h" + +#define FUZZ_PROJECT_NAME "LibReport_fuzzer" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#endif // LIBREPORT_FUZZER_H diff --git a/test/fuzztest/libreport100_fuzzer/corpus/init b/test/fuzztest/libreport100_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/test/fuzztest/libreport100_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/libreport100_fuzzer/project.xml b/test/fuzztest/libreport100_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..941ab218c6927af3b691fbb43755a4cad73c221a --- /dev/null +++ b/test/fuzztest/libreport100_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 100 + + 4096 + + diff --git a/test/fuzztest/libreport_fuzzer/corpus/init b/test/fuzztest/libreport_fuzzer/corpus/init index e06a62b8b7fd45613309e15673f23335bdd980db..eec10bc943fe02056a326119773380942f88cea6 100644 --- a/test/fuzztest/libreport_fuzzer/corpus/init +++ b/test/fuzztest/libreport_fuzzer/corpus/init @@ -12,5 +12,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + FUZZ \ No newline at end of file diff --git a/test/fuzztest/perffile100_fuzzer/PerfFile_fuzzer.cpp b/test/fuzztest/perffile100_fuzzer/PerfFile_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..026fff3bb0126e15ed9a1feefd5db5c9b28940d0 --- /dev/null +++ b/test/fuzztest/perffile100_fuzzer/PerfFile_fuzzer.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 "PerfFile_fuzzer.h" +#include "utilities.h" + +namespace OHOS { +using namespace OHOS::Developtools::HiPerf; +class PerfFileReaderFuzzer : public PerfFileReader { +public: + explicit PerfFileReaderFuzzer(const std::string &fileName, FILE *fp) + : PerfFileReader(fileName, fp) {} + + static std::unique_ptr Instance(const std::string &fileName, + const uint8_t *data, size_t size) + { + std::string resolvedPath = CanonicalizeSpecPath(fileName.c_str()); + FILE *fpApp = fopen(resolvedPath.c_str(), "ab"); + if (fpApp == nullptr) { + printf("fail to append file %s\n", fileName.c_str()); + return nullptr; + } else { + (void)fwrite(data, sizeof(uint8_t), size, fpApp); + (void)fclose(fpApp); + fpApp = nullptr; + } + FILE *fp = fopen(resolvedPath.c_str(), "rb"); + if (fp == nullptr) { + printf("fail to open file %s\n", fileName.c_str()); + return nullptr; + } + + std::unique_ptr reader = + std::make_unique(resolvedPath, fp); + if (!reader->ReadFileHeader()) { + printf("head read error\n"); + (void)fclose(fp); + return nullptr; + } + if (!reader->ReadAttrSection()) { + printf("attr read error\n"); + (void)fclose(fp); + return nullptr; + } + (void)fclose(fp); + return reader; + }; +}; + +bool FuzzPerfFileReader(const uint8_t *data, size_t size) +{ + const std::string testData = "/data/test/resource/testdata/report_test.data"; + HLOGV("test data size %zu\n", size); + if (size == 0) { + return false; + } + auto reader = PerfFileReaderFuzzer::Instance(testData, data, size); + if (reader == nullptr) { + printf("test open failed %s\n", testData.c_str()); + return false; + } + + reader->ReadFeatureSection(); + auto recordCallback = [](PerfEventRecord& record) { + // nothing to do + return true; + }; + reader->ReadDataSection(recordCallback); + return false; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +#ifdef DEBUG_HIPERF_FUZZ + OHOS::ScopeDebugLevel mix(OHOS::LEVEL_VERBOSE, true); + OHOS::DebugLogger::GetInstance()->Disable(false); +#else + OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w"); +#endif + + /* Run your code on data */ + OHOS::FuzzPerfFileReader(data, size); + return 0; +} diff --git a/test/fuzztest/perffile100_fuzzer/PerfFile_fuzzer.h b/test/fuzztest/perffile100_fuzzer/PerfFile_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..e9803a15cd2acba19e2844a89a9611ca99eb14b3 --- /dev/null +++ b/test/fuzztest/perffile100_fuzzer/PerfFile_fuzzer.h @@ -0,0 +1,35 @@ +/* + * 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 PERFFILE_FUZZER_H +#define PERFFILE_FUZZER_H + +#include +#include +#include +#include + +#include +#include + +#include "debug_logger.h" +#include "perf_file_reader.h" +#include "utilities.h" + +#define FUZZ_PROJECT_NAME "PerfFile_fuzzer" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#endif // PERFFILE_FUZZER_H diff --git a/test/fuzztest/perffile100_fuzzer/corpus/init b/test/fuzztest/perffile100_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/test/fuzztest/perffile100_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/perffile100_fuzzer/project.xml b/test/fuzztest/perffile100_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..941ab218c6927af3b691fbb43755a4cad73c221a --- /dev/null +++ b/test/fuzztest/perffile100_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 100 + + 4096 + + diff --git a/test/fuzztest/perffile_fuzzer/corpus/init b/test/fuzztest/perffile_fuzzer/corpus/init index e06a62b8b7fd45613309e15673f23335bdd980db..300546081798c2aff85833bb4dd0c6ab3caef411 100644 --- a/test/fuzztest/perffile_fuzzer/corpus/init +++ b/test/fuzztest/perffile_fuzzer/corpus/init @@ -1,16 +1,16 @@ -/* - * Copyright (c) 2021 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. - */ - +/* + * Copyright (c) 2021 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. + */ + FUZZ \ No newline at end of file diff --git a/test/fuzztest/perffileformat100_fuzzer/PerfFileFormat_fuzzer.cpp b/test/fuzztest/perffileformat100_fuzzer/PerfFileFormat_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..770613bc82e371eef1eaebf6eb53b07482cc628f --- /dev/null +++ b/test/fuzztest/perffileformat100_fuzzer/PerfFileFormat_fuzzer.cpp @@ -0,0 +1,54 @@ +/* + * 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 "PerfFileFormat_fuzzer.h" +#include "perf_file_format.h" + +using namespace OHOS::Developtools::HiPerf; + +namespace OHOS { +bool FuzzPerfFileFormat(const uint8_t *data, size_t size) +{ + char buf[DATA_MAX_SIZE] = { 0 }; + if (memcpy_s(buf, sizeof(buf) - 1, data, size) != 0) { // 1 : make sure end of '\0' + return false; + } + std::unique_ptr perfFileSectionString = + std::make_unique(FEATURE::HIPERF_FILES_SYMBOL, buf, size); + perfFileSectionString->GetBinary(buf, size); + std::unique_ptr perfFileSectionNrCpus = + std::make_unique(FEATURE::HIPERF_FILES_SYMBOL, buf, size); + perfFileSectionNrCpus->GetBinary(buf, size); + std::unique_ptr perfFileSectionU64 = + std::make_unique(FEATURE::HIPERF_FILES_SYMBOL, buf, size); + perfFileSectionU64->GetBinary(buf, size); + return 0; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +/* Run your code on data */ +#ifdef DEBUG_HIPERF_FUZZ + ScopeDebugLevel mix(LEVEL_DEBUG, true); + DebugLogger::GetInstance()->Disable(false); +#else + OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w"); +#endif + + OHOS::FuzzPerfFileFormat(data, size); + return 0; +} diff --git a/test/fuzztest/perffileformat100_fuzzer/PerfFileFormat_fuzzer.h b/test/fuzztest/perffileformat100_fuzzer/PerfFileFormat_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..b6d4afa3fef895bccad370cc852bce2187d559cd --- /dev/null +++ b/test/fuzztest/perffileformat100_fuzzer/PerfFileFormat_fuzzer.h @@ -0,0 +1,25 @@ +/* + * 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 PERFFILEFORMAT_FUZZER_H +#define PERFFILEFORMAT_FUZZER_H + +#include "utilities.h" + +#define FUZZ_PROJECT_NAME "PerfFileFormat_fuzzer" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#endif // PERFFILEFORMAT_FUZZER_H diff --git a/test/fuzztest/perffileformat100_fuzzer/corpus/init b/test/fuzztest/perffileformat100_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/test/fuzztest/perffileformat100_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/perffileformat100_fuzzer/project.xml b/test/fuzztest/perffileformat100_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..d539bed1123e46a0d5df000a7dbe0074f04bab8b --- /dev/null +++ b/test/fuzztest/perffileformat100_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 60 + + 4096 + + diff --git a/test/fuzztest/perffileformat_fuzzer/corpus/init b/test/fuzztest/perffileformat_fuzzer/corpus/init index a6717d373dbd6cfe6d1dd060b83887b2dce546dc..d27c29f2d9d44dbc2999359e54b643d02932fd86 100644 --- a/test/fuzztest/perffileformat_fuzzer/corpus/init +++ b/test/fuzztest/perffileformat_fuzzer/corpus/init @@ -12,5 +12,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + FUZZ \ No newline at end of file diff --git a/test/fuzztest/spedecoder100_fuzzer/SpeDecoder_fuzzer.cpp b/test/fuzztest/spedecoder100_fuzzer/SpeDecoder_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d275cac1b4cf39bba4303214fcdf034d18e273b0 --- /dev/null +++ b/test/fuzztest/spedecoder100_fuzzer/SpeDecoder_fuzzer.cpp @@ -0,0 +1,50 @@ +/* + * 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 "SpeDecoder_fuzzer.h" +#include "spe_decoder.h" + +using namespace OHOS::Developtools::HiPerf; + +namespace OHOS { +bool FuzzSpeDecoder(const uint8_t *data, size_t size) +{ + SpeDecoder *decoder = SpeDecoderDataNew(data, size); + std::vector records; + int ret = SpeDecode(decoder); + if (ret <= 0) { + printf("SpeDecode failed.\n"); + } + struct SpeRecord record = SpeRecord(decoder->record); + records.emplace_back(record); + SpeDecoderFree(decoder); + return 0; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +/* Run your code on data */ +#ifdef DEBUG_HIPERF_FUZZ + ScopeDebugLevel mix(LEVEL_DEBUG, true); + DebugLogger::GetInstance()->Disable(false); +#else + OHOS::Developtools::HiPerf::StdoutRecord noStdOut("/dev/null", "w"); +#endif + + OHOS::FuzzSpeDecoder(data, size); + return 0; +} diff --git a/test/fuzztest/spedecoder100_fuzzer/SpeDecoder_fuzzer.h b/test/fuzztest/spedecoder100_fuzzer/SpeDecoder_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..837f80ef71aece95f71dc61b679b8791e543dfa7 --- /dev/null +++ b/test/fuzztest/spedecoder100_fuzzer/SpeDecoder_fuzzer.h @@ -0,0 +1,25 @@ +/* + * 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 SPEDECODER_FUZZER_H +#define SPEDECODER_FUZZER_H + +#include "utilities.h" + +#define FUZZ_PROJECT_NAME "SpeDecoder_fuzzer" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); + +#endif // SPEDECODER_FUZZER_H diff --git a/test/fuzztest/spedecoder100_fuzzer/corpus/init b/test/fuzztest/spedecoder100_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/test/fuzztest/spedecoder100_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/spedecoder100_fuzzer/project.xml b/test/fuzztest/spedecoder100_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..d539bed1123e46a0d5df000a7dbe0074f04bab8b --- /dev/null +++ b/test/fuzztest/spedecoder100_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 60 + + 4096 + + diff --git a/test/fuzztest/spedecoder_fuzzer/corpus/init b/test/fuzztest/spedecoder_fuzzer/corpus/init index a6717d373dbd6cfe6d1dd060b83887b2dce546dc..d27c29f2d9d44dbc2999359e54b643d02932fd86 100644 --- a/test/fuzztest/spedecoder_fuzzer/corpus/init +++ b/test/fuzztest/spedecoder_fuzzer/corpus/init @@ -12,5 +12,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + FUZZ \ No newline at end of file diff --git a/test/unittest/common/native/callstack_test.cpp b/test/unittest/common/native/callstack_test.cpp index 41727204b657acbd2ba0335a7948fd3878c90577..e03185ec0eff69f634f80c6eb0a14d9dc70c3fed 100644 --- a/test/unittest/common/native/callstack_test.cpp +++ b/test/unittest/common/native/callstack_test.cpp @@ -600,6 +600,39 @@ HWTEST_F(CallStackTest, ExpendCallStackABABAB, TestSize.Level1) ASSERT_EQ(callStack.ExpandCallStack(0, stack4), 5u); } +/** + * @tc.name: ExpendCallStack256 + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(CallStackTest, ExpendCallStack256, TestSize.Level1) +{ + ScopeDebugLevel tempLogLevel(LEVEL_MUCH, true); + CallStack callStack; + std::vector stack1; + for (int i = 0; i < 85; i++) { // 85: 256 / 3 + stack1.push_back({0xc, 0xc}); + stack1.push_back({0xb, 0xb}); + stack1.push_back({0xa, 0xa}); + } + std::vector stack2 = { + {0xc, 0xc}, + {0xb, 0xb}, + }; + std::vector stack3 = { + {0xd, 0xd}, + {0xc, 0xc}, + }; + std::vector stack4 = { + {0xc, 0xc}, + {0xd, 0xd}, + }; + ASSERT_EQ(callStack.ExpandCallStack(0, stack1), 0u); + ASSERT_EQ(callStack.ExpandCallStack(0, stack2), 253u); // 253u: stack level is 254 + ASSERT_EQ(callStack.ExpandCallStack(0, stack3), 254u); // 254u: stack level is 255 + ASSERT_EQ(callStack.ExpandCallStack(0, stack4), 0u); +} + /** * @tc.name: UnwindCallStack * @tc.desc: diff --git a/test/unittest/common/native/subcommand_dump_test.cpp b/test/unittest/common/native/subcommand_dump_test.cpp index 654c3fbc2391eeef8e7f6e27e18a6767914c1627..0afb4fdcdc7b3db395110b69e908e6dba7722878 100644 --- a/test/unittest/common/native/subcommand_dump_test.cpp +++ b/test/unittest/common/native/subcommand_dump_test.cpp @@ -31,6 +31,12 @@ using namespace testing::ext; namespace OHOS { namespace Developtools { namespace HiPerf { +#if is_ohos + const std::string RESOURCE_PATH = "/data/test/resource/testdata/"; +#else + const std::string RESOURCE_PATH = "./resource/testdata/"; +#endif +constexpr int MAX_POOL = 98; class SubCommandDumpTest : public testing::Test { public: static void SetUpTestCase(void); @@ -39,8 +45,47 @@ public: void TearDown(); void TestDumpCommand(const std::string &option, bool expect = true) const; + bool FileCompare(const std::string &stringOut, const std::string &targetFile) const; }; +bool SubCommandDumpTest::FileCompare(const std::string &actualFile, + const std::string &targetFile) const +{ + std::fstream ifs(targetFile.c_str(), std::ios::in); + if (!ifs.is_open()) { + perror("read target failed\n"); + return false; + } + std::string line; + std::vector expectLines; + int expectCount = 0; + while (getline(ifs, line) && expectCount < MAX_POOL) { + expectLines.push_back(line); + expectCount++; + } + std::vector actualLines; + std::fstream ifsOut(actualFile.c_str(), std::ios::in); + if (!ifsOut.is_open()) { + perror("read out failed\n"); + return false; + } + std::string actualLine; + int actualCount = 0; + while (getline(ifsOut, actualLine) && actualCount < MAX_POOL) { + actualLines.push_back(actualLine); + actualCount++; + } + auto actual = actualLines.begin(); + auto expect = expectLines.begin(); + EXPECT_EQ(actualLines.size(), expectLines.size()); + while (actual != actualLines.end() and expect != expectLines.end() and !HasFailure()) { + EXPECT_STREQ(actual->c_str(), expect->c_str()); + actual++; + expect++; + } + return !HasFailure(); +} + void SubCommandDumpTest::SetUpTestCase() { SubCommand::ClearSubCommands(); @@ -95,24 +140,16 @@ void SubCommandDumpTest::TestDumpCommand(const std::string &option, bool expect) * @tc.desc: record * @tc.type: FUNC */ - HWTEST_F(SubCommandDumpTest, Test_LibReport_Success, TestSize.Level1) { StdoutRecord stdoutRecord; stdoutRecord.Start(); - std::string cmdString = "dump -i /data/test/resource/testdata/report/perf.data.libreport"; + std::string cmdString = + "dump -i /data/test/resource/testdata/report/perf.data.libreport -o /data/local/tmp/dump_tmp.txt"; EXPECT_EQ(Command::DispatchCommand(cmdString), true); std::string stringOut = stdoutRecord.Stop(); - size_t symbolsCount = 39; - size_t buildIdCount = 32; - size_t sampleCount = 1000; - size_t featureCount = 10; - - EXPECT_EQ(stringOut.find("magic: PERFILE2") != std::string::npos, true); - EXPECT_EQ(SubStringCount(stringOut, "fileid:"), symbolsCount); - EXPECT_EQ(SubStringCount(stringOut, "buildId:"), buildIdCount); - EXPECT_EQ(SubStringCount(stringOut, "record sample:"), sampleCount); - EXPECT_EQ(SubStringCount(stringOut, "feature:"), featureCount); + std::string targetFile = RESOURCE_PATH + "dump_libreport.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpInputFilename1, TestSize.Level1) @@ -122,7 +159,10 @@ HWTEST_F(SubCommandDumpTest, DumpInputFilename1, TestSize.Level1) HWTEST_F(SubCommandDumpTest, DumpInputFilename2, TestSize.Level1) { - TestDumpCommand("-i /data/test/resource/testdata/perf.data "); + TestDumpCommand( + "-i /data/test/resource/testdata/perf.data -o /data/local/tmp/dump_tmp.txt"); + std::string targetFile = RESOURCE_PATH + "dump_perf_data.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpInputFilenamErr, TestSize.Level1) @@ -132,17 +172,23 @@ HWTEST_F(SubCommandDumpTest, DumpInputFilenamErr, TestSize.Level1) HWTEST_F(SubCommandDumpTest, DumpHeaderAttrs, TestSize.Level1) { - TestDumpCommand("-i /data/test/resource/testdata/perf.data --head "); + TestDumpCommand("-i /data/test/resource/testdata/perf.data --head -o /data/local/tmp/dump_tmp.txt"); + std::string targetFile = RESOURCE_PATH + "dump_head.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpData, TestSize.Level1) { - TestDumpCommand("-i /data/test/resource/testdata/perf.data -d "); + TestDumpCommand("-i /data/test/resource/testdata/perf.data -d -o /data/local/tmp/dump_tmp.txt"); + std::string targetFile = RESOURCE_PATH + "dump_data.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpFeatures, TestSize.Level1) { - TestDumpCommand("-i /data/test/resource/testdata/perf.data -f "); + TestDumpCommand("-i /data/test/resource/testdata/perf.data -f -o /data/local/tmp/dump_tmp.txt"); + std::string targetFile = RESOURCE_PATH + "dump_features.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpSympath, TestSize.Level1) @@ -157,12 +203,16 @@ HWTEST_F(SubCommandDumpTest, DumpSympathErr, TestSize.Level1) HWTEST_F(SubCommandDumpTest, DumpExportUserdata0, TestSize.Level1) { - TestDumpCommand("-i /data/test/resource/testdata/perf.data --export 0"); + TestDumpCommand("-i /data/test/resource/testdata/perf.data --export 0 -o /data/local/tmp/dump_tmp.txt"); + std::string targetFile = RESOURCE_PATH + "dump_export0.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpExportUserdata1, TestSize.Level1) { - TestDumpCommand("-i /data/test/resource/testdata/perf.data --export 1"); + TestDumpCommand("-i /data/test/resource/testdata/perf.data --export 1 -o /data/local/tmp/dump_tmp.txt"); + std::string targetFile = RESOURCE_PATH + "dump_export1.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpElffile, TestSize.Level1) @@ -206,11 +256,12 @@ HWTEST_F(SubCommandDumpTest, DumpCompressDwarfStackTable, TestSize.Level1) { StdoutRecord stdoutRecord; stdoutRecord.Start(); - std::string cmdString = "dump -i /data/test/resource/testdata/dwarf.compress.data"; + std::string cmdString = "dump -i /data/test/resource/testdata/dwarf.compress.data -o /data/local/tmp/dump_tmp.txt"; EXPECT_EQ(Command::DispatchCommand(cmdString), true); std::string stringOut = stdoutRecord.Stop(); - EXPECT_EQ(stringOut.find("hiperf_stack_table") != std::string::npos, true); + std::string targetFile = RESOURCE_PATH + "dump_dwarf_compress.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpCompressDwarfStackid, TestSize.Level1) @@ -360,11 +411,12 @@ HWTEST_F(SubCommandDumpTest, DumpCompressFpStackTable, TestSize.Level1) { StdoutRecord stdoutRecord; stdoutRecord.Start(); - std::string cmdString = "dump -i /data/test/resource/testdata/fp.compress.data"; + std::string cmdString = "dump -i /data/test/resource/testdata/fp.compress.data -o /data/local/tmp/dump_tmp.txt"; EXPECT_EQ(Command::DispatchCommand(cmdString), true); std::string stringOut = stdoutRecord.Stop(); - EXPECT_EQ(stringOut.find("hiperf_stack_table") != std::string::npos, true); + std::string targetFile = RESOURCE_PATH + "dump_fp_compress.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpCompressFpStackid, TestSize.Level1) @@ -514,11 +566,13 @@ HWTEST_F(SubCommandDumpTest, DumpUncompressDwarfStackTable, TestSize.Level1) { StdoutRecord stdoutRecord; stdoutRecord.Start(); - std::string cmdString = "dump -i /data/test/resource/testdata/dwarf.uncompress.data"; + std::string cmdString = + "dump -i /data/test/resource/testdata/dwarf.uncompress.data -o /data/local/tmp/dump_tmp.txt"; EXPECT_EQ(Command::DispatchCommand(cmdString), true); std::string stringOut = stdoutRecord.Stop(); - EXPECT_EQ(stringOut.find("hiperf_stack_table") != std::string::npos, false); + std::string targetFile = RESOURCE_PATH + "dump_dwarf_uncompress.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpUncompressDwarfStackid, TestSize.Level1) @@ -668,11 +722,12 @@ HWTEST_F(SubCommandDumpTest, DumpUncompressFpStackTable, TestSize.Level1) { StdoutRecord stdoutRecord; stdoutRecord.Start(); - std::string cmdString = "dump -i /data/test/resource/testdata/fp.uncompress.data"; + std::string cmdString = "dump -i /data/test/resource/testdata/fp.uncompress.data -o /data/local/tmp/dump_tmp.txt"; EXPECT_EQ(Command::DispatchCommand(cmdString), true); std::string stringOut = stdoutRecord.Stop(); - EXPECT_EQ(stringOut.find("hiperf_stack_table") != std::string::npos, false); + std::string targetFile = RESOURCE_PATH + "dump_fp_uncompress.txt"; + EXPECT_EQ(FileCompare("/data/local/tmp/dump_tmp.txt", targetFile), true); } HWTEST_F(SubCommandDumpTest, DumpUncompressFpStackid, TestSize.Level1) @@ -838,4 +893,4 @@ HWTEST_F(SubCommandDumpTest, GetInstance, TestSize.Level1) } } // namespace HiPerf } // namespace Developtools -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/common/native/subcommand_record_test.cpp b/test/unittest/common/native/subcommand_record_test.cpp index 18f20fc041b715c2da23be6783c61f2b275ca3e2..22adb8fde80e28f9581858e9ab0b0217aef5b02b 100644 --- a/test/unittest/common/native/subcommand_record_test.cpp +++ b/test/unittest/common/native/subcommand_record_test.cpp @@ -413,6 +413,20 @@ HWTEST_F(SubCommandRecordTest, SelectCpuMaxErr, TestSize.Level1) TestRecordCommand(opt, false); } +HWTEST_F(SubCommandRecordTest, SelectCpuMax, TestSize.Level1) +{ + int maxCpuid = sysconf(_SC_NPROCESSORS_CONF) - 1; + SubCommandRecord::CheckRecordCallBack callback = [maxCpuid](const PerfEventRecord& record) { + if (record.GetType() == PERF_RECORD_SAMPLE) { + const PerfRecordSample& recordSample = static_cast(record); + if (recordSample.data_.cpu != maxCpuid) { + _exit(1); + } + } + }; + ForkAndRunTest("-d 2 -c " + std::to_string(maxCpuid), true, true, callback); +} + HWTEST_F(SubCommandRecordTest, SelectCpuInputErr, TestSize.Level1) { TestRecordCommand("-d 2 -c abc ", false); diff --git a/test/unittest/common/native/subcommand_report_test.cpp b/test/unittest/common/native/subcommand_report_test.cpp index e9e3b48ed155917a435bf969b086f19c87012e1a..08afa32d6179baab34bdad0926d275aac8fabbc9 100644 --- a/test/unittest/common/native/subcommand_report_test.cpp +++ b/test/unittest/common/native/subcommand_report_test.cpp @@ -521,6 +521,81 @@ HWTEST_F(SubCommandReportTest, TestOnSubCommand_limit, TestSize.Level1) EXPECT_EQ(FileCompare(stringOut, targetFile), true); } +/** + * @tc.name: TestOnSubCommand_callstack_limit + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(SubCommandReportTest, TestOnSubCommand_callstack_limit, TestSize.Level1) +{ + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = std::chrono::steady_clock::now(); + EXPECT_EQ(Command::DispatchCommand("report -i " + RESOURCE_PATH + + "report_test.data --call-stack-limit-percent 5"), + true); + const auto costMs = std::chrono::duration_cast( + std::chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), DEFAULT_RUN_TIMEOUT_MS); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } + std::string targetFile = RESOURCE_PATH + "report_test_callstack_limit.txt"; + EXPECT_EQ(FileCompare(stringOut, targetFile), true); +} + +/** + * @tc.name: TestOnSubCommand_callstack_limit_err1 + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(SubCommandReportTest, TestOnSubCommand_callstack_limit_err1, TestSize.Level1) +{ + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = std::chrono::steady_clock::now(); + EXPECT_EQ(Command::DispatchCommand("report -i " + RESOURCE_PATH + + "report_test.data --call-stack-limit-percent -1"), + false); + const auto costMs = std::chrono::duration_cast( + std::chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), DEFAULT_RUN_TIMEOUT_MS); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } + const std::string expectStr = "head limit error. must in (0 <= limit <= 100)"; + EXPECT_EQ(FindExpectStr(stringOut, expectStr), true); +} + +/** + * @tc.name: TestOnSubCommand_callstack_limit_err2 + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(SubCommandReportTest, TestOnSubCommand_callstack_limit_err2, TestSize.Level1) +{ + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = std::chrono::steady_clock::now(); + EXPECT_EQ(Command::DispatchCommand("report -i " + RESOURCE_PATH + + "report_test.data --call-stack-limit-percent 101"), + false); + const auto costMs = std::chrono::duration_cast( + std::chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), DEFAULT_RUN_TIMEOUT_MS); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } + const std::string expectStr = "head limit error. must in (0 <= limit <= 100)"; + EXPECT_EQ(FindExpectStr(stringOut, expectStr), true); +} + /** * @tc.name: TestOnSubCommand_limit1 * @tc.desc: @@ -592,7 +667,7 @@ HWTEST_F(SubCommandReportTest, TestOnSubCommand_limit3, TestSize.Level1) if (HasFailure()) { printf("output:\n%s", stringOut.c_str()); } - const std::string expectStr = "head limit error. must in (0 <= limit < 100)"; + const std::string expectStr = "head limit error. must in (0 <= limit <= 100)"; EXPECT_EQ(FindExpectStr(stringOut, expectStr), true); } @@ -617,7 +692,7 @@ HWTEST_F(SubCommandReportTest, TestOnSubCommand_limit4, TestSize.Level1) if (HasFailure()) { printf("output:\n%s", stringOut.c_str()); } - const std::string expectStr = "head limit error. must in (0 <= limit < 100)"; + const std::string expectStr = "head limit error. must in (0 <= limit <= 100)"; EXPECT_EQ(FindExpectStr(stringOut, expectStr), true); } diff --git a/test/unittest/common/native/subcommand_stat_test.cpp b/test/unittest/common/native/subcommand_stat_test.cpp index 46e9fc4aedcb7fd43ddb1e45e9934a44349100f3..e22231d67e5dae0aa70111fb3b9feb0296ba9320 100644 --- a/test/unittest/common/native/subcommand_stat_test.cpp +++ b/test/unittest/common/native/subcommand_stat_test.cpp @@ -44,6 +44,7 @@ public: static void TestCodeThread(int &tid); bool FindExpectStr(const std::string &stringOut, const std::string &counterNames) const; + void CheckCounter(const std::string &stringOut, const std::vector &counterNames) const; uint EffectiveCounter(const std::string &stringOut, const std::vector &counterNames, uint &effectiveHeadCounter) const; @@ -54,6 +55,7 @@ public: const std::string &groupCounterName) const; const std::vector defaultConfigNames_ = { + "hw-branch-instructions", "hw-branch-misses", "hw-cpu-cycles", "hw-instructions", @@ -119,6 +121,27 @@ void SubCommandStatTest::TestCodeThread(int &tid) printf("TestCodeThread:%d --\n", tid); } +void SubCommandStatTest::CheckCounter(const std::string &stringOut, const std::vector &counterNames) const +{ + auto lines = StringSplit(stringOut, "\n"); + auto check = [](const std::string& line, const std::string& e) { + if (line.find(e) == std::string::npos) { + return; + } + for (auto &c : line) { + if (c != ' ') { + EXPECT_EQ(c != '0', true); + break; + } + } + }; + for (auto &line : lines) { + for (auto &e : counterNames) { + check(line, e); + } + } +} + uint SubCommandStatTest::EffectiveCounter(const std::string &stringOut, const std::string &counterNames, uint &effectiveHeadCounter) const @@ -272,7 +295,11 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_a1, TestSize.Level1) if (HasFailure()) { printf("output:\n%s", stringOut.c_str()); } - + std::vector events = {"hw-cpu-cycles", + "hw-instructions", + "sw-context-switches", + "sw-task-clock"}; + CheckCounter(stringOut, events); // some times 'sw-page-faults' is 0 uint effectiveHeadCounter = 0; EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter), @@ -298,7 +325,11 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_a2, TestSize.Level1) if (HasFailure()) { printf("output:\n%s", stringOut.c_str()); } - + std::vector events = {"hw-cpu-cycles", + "hw-instructions", + "sw-context-switches", + "sw-task-clock"}; + CheckCounter(stringOut, events); // some times 'sw-page-faults' is 0 uint effectiveHeadCounter = 0; EXPECT_GE(EffectiveCounter(stringOut, defaultConfigNames_, effectiveHeadCounter), @@ -1428,6 +1459,84 @@ HWTEST_F(SubCommandStatTest, TestOnSubCommand_e4, TestSize.Level1) t1.join(); } +/** + * @tc.name: TestOnSubCommand_e5 + * @tc.desc: -e261 + * @tc.type: FUNC + */ +HWTEST_F(SubCommandStatTest, TestOnSubCommand_e5, TestSize.Level1) +{ + int tid1 = 0; + std::thread t1(SubCommandStatTest::TestCodeThread, std::ref(tid1)); + while (tid1 == 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + std::string cmdstr = "stat -p "; + cmdstr += std::to_string(tid1); + cmdstr += " -e hw-stalled-cycles-frontend,sw-bpf-output,raw-sw-incr,hw-cache-l1d"; + cmdstr += " -c 0 -d 3"; + + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = std::chrono::steady_clock::now(); + g_wait = true; + EXPECT_EQ(Command::DispatchCommand(cmdstr), true); + g_wait = false; + { + std::unique_lock lock(mtx); + cv.notify_all(); + } + const auto costMs = std::chrono::duration_cast( + std::chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), defaultRunTimeoutMs); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } + const std::vector configNmaes = {"hw-stalled-cycles-frontend", + "sw-bpf-output", + "raw-sw-incr", + "hw-cache-l1d"}; + uint effectiveHeadCounter = 0u; + EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size()); + t1.join(); +} + +/** + * @tc.name: TestOnSubCommand_e6 + * @tc.desc: -e261 + * @tc.type: FUNC + */ +HWTEST_F(SubCommandStatTest, TestOnSubCommand_e6, TestSize.Level1) +{ + std::string cmdstr = "stat -a"; + cmdstr += " -e hw-stalled-cycles-frontend,sw-bpf-output,raw-sw-incr,hw-cache-l1d"; + cmdstr += " -c 0 -d 3"; + + StdoutRecord stdoutRecord; + stdoutRecord.Start(); + const auto startTime = std::chrono::steady_clock::now(); + EXPECT_EQ(Command::DispatchCommand(cmdstr), true); + const auto costMs = std::chrono::duration_cast( + std::chrono::steady_clock::now() - startTime); + EXPECT_LE(costMs.count(), defaultRunTimeoutMs); + + std::string stringOut = stdoutRecord.Stop(); + if (HasFailure()) { + printf("output:\n%s", stringOut.c_str()); + } + const std::vector configNmaes = {"hw-stalled-cycles-frontend", + "sw-bpf-output", + "raw-sw-incr", + "hw-cache-l1d"}; + const std::vector events = {"hw-stalled-cycles-frontend", "hw-cache-l1d"}; + CheckCounter(stringOut, events); + uint effectiveHeadCounter = 0u; + EXPECT_GE(EffectiveCounter(stringOut, configNmaes, effectiveHeadCounter), configNmaes.size()); +} + /** * @tc.name: TestOnSubCommand_g * @tc.desc: -g diff --git a/test/unittest/common/native/utilities_test.cpp b/test/unittest/common/native/utilities_test.cpp index 004e29bacaba13139b27875613a100aac4c00978..f6f7f588cd07527f09c82c533386a89b6e0c38a5 100644 --- a/test/unittest/common/native/utilities_test.cpp +++ b/test/unittest/common/native/utilities_test.cpp @@ -22,6 +22,16 @@ using namespace testing::ext; namespace OHOS { namespace Developtools { namespace HiPerf { +const vector HIPERF_OUTPUT_PATH = { + "/system/lib/libhiperfplugin.z.so", + "/system/lib/platformsdk/libhiperf_client.z.so", + "/system/lib/module/libhiperf_napi.z.so", + "/system/lib64/libhiperfplugin.z.so", + "/system/lib64/platformsdk/libhiperf_client.z.so", + "/system/lib64/module/libhiperf_napi.z.so", + "/system/bin/hiperf" +}; +const uint64_t HIPERF_BASELINE_SIZE = 1400 * 1024; class UtilitiesTest : public testing::Test { public: static void SetUpTestCase(void); @@ -826,6 +836,24 @@ HWTEST_F(UtilitiesTest, IsArkJsFile, TestSize.Level1) EXPECT_EQ(IsArkJsFile("test.hqf"), true); EXPECT_EQ(IsArkJsFile("test.so"), false); } + +/** + * @tc.name: RomTest + * @tc.desc: Testing hiperf Rom + * @tc.type: FUNC + */ +HWTEST_F(UtilitiesTest, RomTest, TestSize.Level1) +{ + uint64_t realSize = 0; + for (int i = 0; i < HIPERF_OUTPUT_PATH.size(); i++) { + struct stat st = {0}; + stat(HIPERF_OUTPUT_PATH[i].c_str(), &st); + realSize += static_cast(st.st_size); + } + + std::cout << "realSize: " << realSize << std::endl; + EXPECT_LT(realSize, HIPERF_BASELINE_SIZE); +} } // namespace HiPerf } // namespace Developtools } // namespace OHOS diff --git a/test/unittest/resource/ohos_test.xml b/test/unittest/resource/ohos_test.xml index e3354ca35c3f24cecae987ec42b78200597ef92f..fe52dd24e5c1ec0dcb0db775909eb8abcc22c6c2 100644 --- a/test/unittest/resource/ohos_test.xml +++ b/test/unittest/resource/ohos_test.xml @@ -95,6 +95,18 @@