diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index b21736520a16e9a0f71506b922e97eacaeb38ed5..5f3ffa1de69e4ce38e43e3a4c0fc3bde91dd5c85 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -3420,6 +3420,238 @@ ohos_fuzztest("SplitStringFuzzTest") { defines = [ "STARTUP_INIT_TEST" ] } +ohos_fuzztest("SetBootCompletedFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/setbootcompleted_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "setbootcompleted_fuzzer/setbootcompleted_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + +ohos_fuzztest("ConvertMicrosecondToSecondFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/convertmicrosecondtosecond_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "convertmicrosecondtosecond_fuzzer/convertmicrosecondtosecond_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + +ohos_fuzztest("StringToLLFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/stringtoll_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "stringtoll_fuzzer/stringtoll_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + +ohos_fuzztest("StringToULLFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/stringtoull_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "stringtoull_fuzzer/stringtoull_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + +ohos_fuzztest("StringToIntFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/stringtoint_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "stringtoint_fuzzer/stringtoint_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + +ohos_fuzztest("StringToUintFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/stringtouint_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "stringtouint_fuzzer/stringtouint_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + +ohos_fuzztest("TrimHeadFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/trimhead_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "trimhead_fuzzer/trimhead_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + +ohos_fuzztest("TrimTailFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//base/startup/init/test/fuzztest/trimtail_fuzzer" + + include_dirs = [ + "//base/startup/init/interfaces/innerkits/include", + "//base/startup/init/test/fuzztest/utils/include", + ] + + deps = [ + "//base/startup/init/services/param:parameter", + "//base/startup/init/services/utils:libinit_utils", + ] + external_deps = [ + "bounds_checking_function:libsec_static", + "hilog:libhilog", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "trimtail_fuzzer/trimtail_fuzzer.cpp" ] + + defines = [ "STARTUP_INIT_TEST" ] +} + ohos_fuzztest("SendLocalChangeFuzzTest") { module_out_path = module_output_path fuzz_config_file = "//base/startup/init/test/fuzztest/sendlocalchange_fuzzer" @@ -3690,6 +3922,7 @@ group("fuzztest") { ":CheckAppWatchPermissionFuzzTest", ":CmdClientInitFuzzTest", ":CmdServiceProcessDelClientFuzzTest", + ":ConvertMicrosecondToSecondFuzzTest", ":DecodeGidFuzzTest", ":DecodeUidFuzzTest", ":DelRemoteWatcherFuzzTest", @@ -3783,15 +4016,22 @@ group("fuzztest") { ":ServiceSetReadyFuzzTest", ":ServiceWaitForStatusFuzzTest", ":ServiceWatchForStatusFuzzTest", + ":SetBootCompletedFuzzTest", ":SetParameterCFuzzTest", ":SetParameterCppFuzzTest", ":SplitStringFuzzTest", ":StartServiceByTimerFuzzTest", ":StopServiceTimerFuzzTest", + ":StringToIntFuzzTest", + ":StringToLLFuzzTest", + ":StringToUintFuzzTest", + ":StringToULLFuzzTest", ":SysCheckParamExistFuzzTest", ":SystemDumpParametersFuzzTest", ":SystemTraversalParameterFuzzTest", ":SystemWaitParameterFuzzTest", + ":TrimHeadFuzzTest", + ":TrimTailFuzzTest", ":UmountAllWithFstabFileFuzzTest", ":WaitParameterFuzzTest", ":WatchParameterFuzzTest", diff --git a/test/fuzztest/convertmicrosecondtosecond_fuzzer/convertmicrosecondtosecond_fuzzer.cpp b/test/fuzztest/convertmicrosecondtosecond_fuzzer/convertmicrosecondtosecond_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c23aac942e11a6aa407ffe813ba93a4daae1072 --- /dev/null +++ b/test/fuzztest/convertmicrosecondtosecond_fuzzer/convertmicrosecondtosecond_fuzzer.cpp @@ -0,0 +1,48 @@ +/* + * 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 +#include "init_utils.h" +#include "convertmicrosecondtosecond_fuzzer.h" +#include "securec.h" + +extern "C" { +float ConvertMicrosecondToSecond(int x); +} +namespace OHOS { + bool FuzzConvertMicrosecondToSecond(const uint8_t* data, size_t size) + { + if (data == nullptr || size < sizeof(int)) { + return false; + } + + int value = 0; + errno_t err = memcpy_s(&value, sizeof(value), data, sizeof(value)); + if (err != 0) { + return false; + } + + float ret = ConvertMicrosecondToSecond(value); + return ret != 0; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzConvertMicrosecondToSecond(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/convertmicrosecondtosecond_fuzzer/convertmicrosecondtosecond_fuzzer.h b/test/fuzztest/convertmicrosecondtosecond_fuzzer/convertmicrosecondtosecond_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..6a37878fd47aa37da0dde0ca9e75e88dbe81ba21 --- /dev/null +++ b/test/fuzztest/convertmicrosecondtosecond_fuzzer/convertmicrosecondtosecond_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_CONVERT_MICROSECONDE_TO_SECONDE_FUZZER_H +#define TEST_FUZZTEST_CONVERT_MICROSECONDE_TO_SECONDE_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "convertmicrosecondtosecond_fuzzer" +#endif diff --git a/test/fuzztest/convertmicrosecondtosecond_fuzzer/corpus/init b/test/fuzztest/convertmicrosecondtosecond_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8920b50aada32303b52fe86d15c0123ba33d51f4 --- /dev/null +++ b/test/fuzztest/convertmicrosecondtosecond_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 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/convertmicrosecondtosecond_fuzzer/project.xml b/test/fuzztest/convertmicrosecondtosecond_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..9e35ed57fbc05fcaac3016839def9debdf5b9d74 --- /dev/null +++ b/test/fuzztest/convertmicrosecondtosecond_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/setbootcompleted_fuzzer/corpus/init b/test/fuzztest/setbootcompleted_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8920b50aada32303b52fe86d15c0123ba33d51f4 --- /dev/null +++ b/test/fuzztest/setbootcompleted_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2024 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/setbootcompleted_fuzzer/project.xml b/test/fuzztest/setbootcompleted_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..9e35ed57fbc05fcaac3016839def9debdf5b9d74 --- /dev/null +++ b/test/fuzztest/setbootcompleted_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/setbootcompleted_fuzzer/setbootcompleted_fuzzer.cpp b/test/fuzztest/setbootcompleted_fuzzer/setbootcompleted_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0bc1bb46830add5048f9a8b84997423a40867e9 --- /dev/null +++ b/test/fuzztest/setbootcompleted_fuzzer/setbootcompleted_fuzzer.cpp @@ -0,0 +1,46 @@ +/* + * 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 +#include "init_utils.h" +#include "setbootcompleted_fuzzer.h" + + +namespace OHOS { + bool FuzzSetBootCompleted(const uint8_t* data, size_t size) + { + if (size == 0) { + return false; + } + bool ret = false; + bool shouldSet = (data[0] != 0); + bool original = IsBootCompleted(); + + SetBootCompleted(shouldSet); + if (IsBootCompleted() == shouldSet) { + ret = true; + } + SetBootCompleted(original); + return ret; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzSetBootCompleted(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/setbootcompleted_fuzzer/setbootcompleted_fuzzer.h b/test/fuzztest/setbootcompleted_fuzzer/setbootcompleted_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..6302c1b7822e4d12299bb88a7dbd3321122b7db8 --- /dev/null +++ b/test/fuzztest/setbootcompleted_fuzzer/setbootcompleted_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_SET_BOOT_COMPLETED_FUZZER_H +#define TEST_FUZZTEST_SET_BOOT_COMPLETED_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "setbootcompleted_fuzzer" +#endif diff --git a/test/fuzztest/stringtoint_fuzzer/corpus/init b/test/fuzztest/stringtoint_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..9e55ceefeefe0ae1985ff2391b8006333ecaff63 --- /dev/null +++ b/test/fuzztest/stringtoint_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/stringtoint_fuzzer/project.xml b/test/fuzztest/stringtoint_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..949d03efd7477905377ac79f3b0bb374c8ac2e9a --- /dev/null +++ b/test/fuzztest/stringtoint_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/stringtoint_fuzzer/stringtoint_fuzzer.cpp b/test/fuzztest/stringtoint_fuzzer/stringtoint_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ea6f71f81d55104a0dd3fb77cb18bc93edefbe2 --- /dev/null +++ b/test/fuzztest/stringtoint_fuzzer/stringtoint_fuzzer.cpp @@ -0,0 +1,47 @@ +/* + * 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 +#include "init_utils.h" +#include "securec.h" +#include "stringtoint_fuzzer.h" + + +namespace OHOS { + bool FuzzStringToInt(const uint8_t* data, size_t size) + { + if (size < sizeof(int)) { + return false; + } + + int extractedInt = 0; + errno_t err = memcpy_s(&extractedInt, sizeof(extractedInt), data, sizeof(int)); + if (err != 0) { + return false; + } + std::string num = std::to_string(std::abs(extractedInt)); + int value = 0; + StringToInt(num.c_str(), value); + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzStringToInt(data, size); + return 0; +} diff --git a/test/fuzztest/stringtoint_fuzzer/stringtoint_fuzzer.h b/test/fuzztest/stringtoint_fuzzer/stringtoint_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..f6c5c16e8158a1f15daa7d2dbbf2e41c481ed6cf --- /dev/null +++ b/test/fuzztest/stringtoint_fuzzer/stringtoint_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_STRING_TO_INT_FUZZER_H +#define TEST_FUZZTEST_STRING_TO_INT_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "stringtoint_fuzzer" +#endif \ No newline at end of file diff --git a/test/fuzztest/stringtoll_fuzzer/corpus/init b/test/fuzztest/stringtoll_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..9e55ceefeefe0ae1985ff2391b8006333ecaff63 --- /dev/null +++ b/test/fuzztest/stringtoll_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/stringtoll_fuzzer/project.xml b/test/fuzztest/stringtoll_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..949d03efd7477905377ac79f3b0bb374c8ac2e9a --- /dev/null +++ b/test/fuzztest/stringtoll_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/stringtoll_fuzzer/stringtoll_fuzzer.cpp b/test/fuzztest/stringtoll_fuzzer/stringtoll_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe4faa550fcdd0d640dee17d6be08d4eeed3170c --- /dev/null +++ b/test/fuzztest/stringtoll_fuzzer/stringtoll_fuzzer.cpp @@ -0,0 +1,47 @@ +/* + * 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 +#include "init_utils.h" +#include "securec.h" +#include "stringtoll_fuzzer.h" + + +namespace OHOS { + bool FuzzStringToLL(const uint8_t* data, size_t size) + { + if (size < sizeof(int)) { + return false; + } + + int extractedInt = 0; + errno_t err = memcpy_s(&extractedInt, sizeof(extractedInt), data, sizeof(int)); + if (err != 0) { + return false; + } + std::string num = std::to_string(std::abs(extractedInt)); + long long int value = 0; + StringToLL(num.c_str(), &value); + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzStringToLL(data, size); + return 0; +} diff --git a/test/fuzztest/stringtoll_fuzzer/stringtoll_fuzzer.h b/test/fuzztest/stringtoll_fuzzer/stringtoll_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..495c461890a6a623529a67c4e126c048824ae4f1 --- /dev/null +++ b/test/fuzztest/stringtoll_fuzzer/stringtoll_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_STRING_TO_LL_FUZZER_H +#define TEST_FUZZTEST_STRING_TO_LL_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "stringtoll_fuzzer" +#endif \ No newline at end of file diff --git a/test/fuzztest/stringtouint_fuzzer/corpus/init b/test/fuzztest/stringtouint_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..9e55ceefeefe0ae1985ff2391b8006333ecaff63 --- /dev/null +++ b/test/fuzztest/stringtouint_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/stringtouint_fuzzer/project.xml b/test/fuzztest/stringtouint_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..949d03efd7477905377ac79f3b0bb374c8ac2e9a --- /dev/null +++ b/test/fuzztest/stringtouint_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/stringtouint_fuzzer/stringtouint_fuzzer.cpp b/test/fuzztest/stringtouint_fuzzer/stringtouint_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..369bcd4722abbd198b1a2a976848888d67504bd5 --- /dev/null +++ b/test/fuzztest/stringtouint_fuzzer/stringtouint_fuzzer.cpp @@ -0,0 +1,47 @@ +/* + * 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 +#include "init_utils.h" +#include "securec.h" +#include "stringtouint_fuzzer.h" + + +namespace OHOS { + bool FuzzStringToUint(const uint8_t* data, size_t size) + { + if (size < sizeof(int)) { + return false; + } + + int extractedInt = 0; + errno_t err = memcpy_s(&extractedInt, sizeof(extractedInt), data, sizeof(int)); + if (err != 0) { + return false; + } + std::string num = std::to_string(std::abs(extractedInt)); + unsigned int value = 0; + StringToUint(num.c_str(), &value); + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzStringToUint(data, size); + return 0; +} diff --git a/test/fuzztest/stringtouint_fuzzer/stringtouint_fuzzer.h b/test/fuzztest/stringtouint_fuzzer/stringtouint_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..8034eb1d18cc1d1e8f415dbcc6c2a9490e97be7d --- /dev/null +++ b/test/fuzztest/stringtouint_fuzzer/stringtouint_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_STRING_TO_UINT_FUZZER_H +#define TEST_FUZZTEST_STRING_TO_UINT_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "stringtouint_fuzzer" +#endif \ No newline at end of file diff --git a/test/fuzztest/stringtoull_fuzzer/corpus/init b/test/fuzztest/stringtoull_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..9e55ceefeefe0ae1985ff2391b8006333ecaff63 --- /dev/null +++ b/test/fuzztest/stringtoull_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/stringtoull_fuzzer/project.xml b/test/fuzztest/stringtoull_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..949d03efd7477905377ac79f3b0bb374c8ac2e9a --- /dev/null +++ b/test/fuzztest/stringtoull_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/stringtoull_fuzzer/stringtoull_fuzzer.cpp b/test/fuzztest/stringtoull_fuzzer/stringtoull_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e0f42ea57062090aae4472fb7d5d0dae9c7fff3 --- /dev/null +++ b/test/fuzztest/stringtoull_fuzzer/stringtoull_fuzzer.cpp @@ -0,0 +1,47 @@ +/* + * 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 +#include "init_utils.h" +#include "securec.h" +#include "stringtoull_fuzzer.h" + + +namespace OHOS { + bool FuzzStringToULL(const uint8_t* data, size_t size) + { + if (size < sizeof(int)) { + return false; + } + + int extractedInt = 0; + errno_t err = memcpy_s(&extractedInt, sizeof(extractedInt), data, sizeof(int)); + if (err != 0) { + return false; + } + std::string num = std::to_string(std::abs(extractedInt)); + unsigned long long int value = 0; + StringToULL(num.c_str(), &value); + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzStringToULL(data, size); + return 0; +} diff --git a/test/fuzztest/stringtoull_fuzzer/stringtoull_fuzzer.h b/test/fuzztest/stringtoull_fuzzer/stringtoull_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..9ba34f71ed50a2994c02bc30a2b37b76b9462075 --- /dev/null +++ b/test/fuzztest/stringtoull_fuzzer/stringtoull_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_STRING_TO_ULL_FUZZER_H +#define TEST_FUZZTEST_STRING_TO_ULL_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "stringtoull_fuzzer" +#endif \ No newline at end of file diff --git a/test/fuzztest/trimhead_fuzzer/corpus/init b/test/fuzztest/trimhead_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..9e55ceefeefe0ae1985ff2391b8006333ecaff63 --- /dev/null +++ b/test/fuzztest/trimhead_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/trimhead_fuzzer/project.xml b/test/fuzztest/trimhead_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..949d03efd7477905377ac79f3b0bb374c8ac2e9a --- /dev/null +++ b/test/fuzztest/trimhead_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/trimhead_fuzzer/trimhead_fuzzer.cpp b/test/fuzztest/trimhead_fuzzer/trimhead_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c46772a6b808ddecd72c6a174a6adce0a2205318 --- /dev/null +++ b/test/fuzztest/trimhead_fuzzer/trimhead_fuzzer.cpp @@ -0,0 +1,59 @@ +/* + * 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 +#include "init_utils.h" +#include "securec.h" +#include "trimhead_fuzzer.h" +static const int MIN_CHARECTER = 32; +static const int MAX_CHARECTER = 126; + +namespace OHOS { + std::vector CreateSafeString(const uint8_t* data, size_t size) + { + if (size == 0) { + return {'\0'}; + } + + std::vector safeStr(size + 1); + for (size_t i = 0; i < size; ++i) { + safeStr[i] = (data[i] >= MIN_CHARECTER && data[i] <= MAX_CHARECTER) ? data[i] : 'a'; + } + safeStr[size] = '\0'; + return safeStr; + } + + bool FuzzTrimHead(const uint8_t* data, size_t size) + { + if (size == 0) { + return false; + } + + auto safeStr = CreateSafeString(data, size); + char* str = safeStr.data(); + char trimChar = (size > 0 && data[0] >= MIN_CHARECTER && data[0] <= MAX_CHARECTER) ? data[0] : 'a'; + TrimHead(str, trimChar); + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzTrimHead(data, size); + return 0; +} diff --git a/test/fuzztest/trimhead_fuzzer/trimhead_fuzzer.h b/test/fuzztest/trimhead_fuzzer/trimhead_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..6787380f96b7eea36f477f85441bfca44ec6ac51 --- /dev/null +++ b/test/fuzztest/trimhead_fuzzer/trimhead_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_TRIM_HEAD_FUZZER_H +#define TEST_FUZZTEST_TRIM_HEAD_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "trimhead_fuzzer" +#endif \ No newline at end of file diff --git a/test/fuzztest/trimtail_fuzzer/corpus/init b/test/fuzztest/trimtail_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..9e55ceefeefe0ae1985ff2391b8006333ecaff63 --- /dev/null +++ b/test/fuzztest/trimtail_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 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/trimtail_fuzzer/project.xml b/test/fuzztest/trimtail_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..949d03efd7477905377ac79f3b0bb374c8ac2e9a --- /dev/null +++ b/test/fuzztest/trimtail_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 100 + + 30 + + 2048 + + diff --git a/test/fuzztest/trimtail_fuzzer/trimtail_fuzzer.cpp b/test/fuzztest/trimtail_fuzzer/trimtail_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c41c80264cfd152fb1d3835ff8929ad8acb0a89b --- /dev/null +++ b/test/fuzztest/trimtail_fuzzer/trimtail_fuzzer.cpp @@ -0,0 +1,59 @@ +/* + * 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 +#include "init_utils.h" +#include "securec.h" +#include "trimtail_fuzzer.h" +static const int MIN_CHARECTER = 32; +static const int MAX_CHARECTER = 126; + +namespace OHOS { + std::vector CreateSafeString(const uint8_t* data, size_t size) + { + if (size == 0) { + return {'\0'}; + } + + std::vector safeStr(size + 1); + for (size_t i = 0; i < size; ++i) { + safeStr[i] = (data[i] >= MIN_CHARECTER && data[i] <= MAX_CHARECTER) ? data[i] : 'a'; + } + safeStr[size] = '\0'; + return safeStr; + } + + bool FuzzTrimTail(const uint8_t* data, size_t size) + { + if (size == 0) { + return false; + } + + auto safeStr = CreateSafeString(data, size); + char* str = safeStr.data(); + char trimChar = (size > 0 && data[0] >= MIN_CHARECTER && data[0] <= MAX_CHARECTER) ? data[0] : 'a'; + TrimTail(str, trimChar); + + return true; + } +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::FuzzTrimTail(data, size); + return 0; +} diff --git a/test/fuzztest/trimtail_fuzzer/trimtail_fuzzer.h b/test/fuzztest/trimtail_fuzzer/trimtail_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..4e524867974fb4e7639177a779697d2095a36773 --- /dev/null +++ b/test/fuzztest/trimtail_fuzzer/trimtail_fuzzer.h @@ -0,0 +1,20 @@ +/* + * 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 TEST_FUZZTEST_TRIM_TAIL_FUZZER_H +#define TEST_FUZZTEST_TRIM_TAIL_FUZZER_H +#include "fuzz_utils.h" +#define FUZZ_PROJECT_NAME "trimtail_fuzzer" +#endif \ No newline at end of file diff --git a/test/fuzztest/watchparameter_fuzzer/watchparameter_fuzzer.cpp b/test/fuzztest/watchparameter_fuzzer/watchparameter_fuzzer.cpp index 39d0fdf4ca7eaf86cdf56eef595485d118712a5a..8889249d3e3c23f12a487235c357a1fd0aa2eb39 100644 --- a/test/fuzztest/watchparameter_fuzzer/watchparameter_fuzzer.cpp +++ b/test/fuzztest/watchparameter_fuzzer/watchparameter_fuzzer.cpp @@ -17,8 +17,6 @@ #include #include "parameter.h" #include "fuzz_utils.h" -#include -static const int SLEEP_TIME = 100000; static void HandleParamChange(const char *key, const char *value, void *context) { @@ -31,13 +29,10 @@ static void HandleParamChange(const char *key, const char *value, void *context) namespace OHOS { bool FuzzWatchParameter(const uint8_t* data, size_t size) { - usleep(SLEEP_TIME); bool result = false; std::string str(reinterpret_cast(data), size); CloseStdout(); if (!WatchParameter(str.c_str(), HandleParamChange, NULL)) { - usleep(SLEEP_TIME); - WatchParameter(str.c_str(), NULL, NULL); result = true; } return result;