From 5b2e2aa959725f571532063e0c4111fba2bb1044 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Tue, 22 Jul 2025 16:42:46 +0800 Subject: [PATCH] file_api add fuzztest Signed-off-by: yangjingbo10 Change-Id: Ibec087b97d64b84be2ee72d710cb0c4e45ebd104 --- bundle.json | 3 +- interfaces/test/fuzztest/BUILD.gn | 23 +++++ .../test/fuzztest/fileio_fuzzer/BUILD.gn | 47 ++++++++++ .../test/fuzztest/fileio_fuzzer/corpus/init | 16 ++++ .../fuzztest/fileio_fuzzer/fileio_fuzzer.cpp | 50 +++++++++++ .../fuzztest/fileio_fuzzer/fileio_fuzzer.h | 21 +++++ .../test/fuzztest/fileio_fuzzer/project.xml | 25 ++++++ .../fuzz_common/fileapi_fuzzer_helper.h | 86 +++++++++++++++++++ .../test/fuzztest/hyperaio_fuzzer/BUILD.gn | 60 +++++++++++++ .../test/fuzztest/hyperaio_fuzzer/corpus/init | 16 ++++ .../hyperaio_fuzzer/hyperaio_fuzzer.cpp | 79 +++++++++++++++++ .../hyperaio_fuzzer/hyperaio_fuzzer.h | 21 +++++ .../test/fuzztest/hyperaio_fuzzer/project.xml | 25 ++++++ .../test/fuzztest/rustfile_fuzzer/BUILD.gn | 43 ++++++++++ .../test/fuzztest/rustfile_fuzzer/corpus/init | 16 ++++ .../test/fuzztest/rustfile_fuzzer/project.xml | 25 ++++++ .../rustfile_fuzzer/rustfile_fuzzer.cpp | 43 ++++++++++ .../rustfile_fuzzer/rustfile_fuzzer.h | 21 +++++ 18 files changed, 619 insertions(+), 1 deletion(-) create mode 100644 interfaces/test/fuzztest/BUILD.gn create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h create mode 100644 interfaces/test/fuzztest/fileio_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h create mode 100644 interfaces/test/fuzztest/hyperaio_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/rustfile_fuzzer/BUILD.gn create mode 100644 interfaces/test/fuzztest/rustfile_fuzzer/corpus/init create mode 100644 interfaces/test/fuzztest/rustfile_fuzzer/project.xml create mode 100644 interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.cpp create mode 100644 interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.h diff --git a/bundle.json b/bundle.json index b0c965533..4b7dae035 100644 --- a/bundle.json +++ b/bundle.json @@ -212,7 +212,8 @@ } ], "test": [ - "//foundation/filemanagement/file_api/interfaces/test/unittest:file_api_unittest" + "//foundation/filemanagement/file_api/interfaces/test/unittest:file_api_unittest", + "//foundation/filemanagement/file_api/interfaces/test/fuzztest:file_api_fuzztest" ] } } diff --git a/interfaces/test/fuzztest/BUILD.gn b/interfaces/test/fuzztest/BUILD.gn new file mode 100644 index 000000000..993107634 --- /dev/null +++ b/interfaces/test/fuzztest/BUILD.gn @@ -0,0 +1,23 @@ +# 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. +import("//foundation/filemanagement/file_api/file_api.gni") + +group("file_api_fuzztest") { + testonly = true + + deps = [ + "${file_api_path}/interfaces/test/fuzztest/fileio_fuzzer:FileioFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/rustfile_fuzzer:RustFileFuzzTest", + "${file_api_path}/interfaces/test/fuzztest/hyperaio_fuzzer:HyperaioFuzzTest", + ] +} diff --git a/interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn b/interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn new file mode 100644 index 000000000..0b4ea42c9 --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/BUILD.gn @@ -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. + +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("FileioFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/fileio_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/c/fileio", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ + "${file_api_path}/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp", + ] + defines = [] + deps = [ + "${file_api_path}/interfaces/kits/c/fileio:ohfileio", + "${file_api_path}/interfaces/kits/native:fileio_native", + ] + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":FileioFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/fileio_fuzzer/corpus/init b/interfaces/test/fuzztest/fileio_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/fileio_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/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp b/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.cpp new file mode 100644 index 000000000..0e45e4075 --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/fileio_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 "fileio_fuzzer.h" + +#include "fileio.h" +#include "fileapi_fuzzer_helper.h" + +namespace OHOS { +bool FileioFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return false; + } + + FuzzData fuzzData(data, size); + // uriLength + int32_t uriLength = fuzzData.GetData(); + // uri + int len = static_cast(fuzzData.GetRemainSize()); + if (len < uriLength) { + len = uriLength; + } + std::string uriStr = fuzzData.GetStringFromData(len); + // location + int location = 0; + + OH_FileIO_GetFileLocation(const_cast(uriStr.c_str()), uriLength, &location); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::FileioFuzzTest(data, size); + return 0; +} diff --git a/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h b/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h new file mode 100644 index 000000000..40606a76e --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/fileio_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 FILEIO_FUZZER_H +#define FILEIO_FUZZER_H + +#define FUZZ_PROJECT_NAME "fileio_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/fileio_fuzzer/project.xml b/interfaces/test/fuzztest/fileio_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/fileio_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h b/interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h new file mode 100644 index 000000000..c82491be9 --- /dev/null +++ b/interfaces/test/fuzztest/fuzz_common/fileapi_fuzzer_helper.h @@ -0,0 +1,86 @@ +/* + * 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 FILEAPI_FUZZER_HELPER_H +#define FILEAPI_FUZZER_HELPER_H + +#include "securec.h" +#include + +namespace OHOS { +class FuzzData { +public: + FuzzData(const uint8_t *data, size_t size) + { + baseData = data; + baseSize = size; + } + + void ResetData(size_t newSize) + { + if (newSize <= baseSize) { + baseSize = newSize; + } + basePos = 0; + } + + template + T GetData() + { + T object{}; + size_t objectSize = sizeof(object); + if (baseData == nullptr || objectSize > baseSize - basePos) { + return object; + } + if (memcpy_s(&object, objectSize, baseData + basePos, objectSize) != EOK) { + return {}; + } + basePos = basePos + objectSize; + return object; + } + + size_t GetRemainSize() const + { + return baseSize - basePos; + } + + std::string GetStringFromData(int len) + { + if (len <= 0 || baseData == nullptr || basePos >= baseSize) { + return ""; + } + + std::string cstr; + for (int i = 0; i < len; ++i) { + if (basePos >= baseSize) { + basePos = 0; + } + char tmp = GetData(); + if (tmp == '\0') { + cstr.push_back('1'); + } else { + cstr.push_back(tmp); + } + } + return cstr; + } + +private: + const uint8_t *baseData{nullptr}; + size_t baseSize{0}; + size_t basePos{0}; +}; +} // namespace OHOS +#endif // FILEAPI_FUZZER_HELPER_H diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn b/interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn new file mode 100644 index 000000000..73f9f489b --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/BUILD.gn @@ -0,0 +1,60 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("HyperaioFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/hyperaio_fuzzer" + file_api_feature_hyperaio = true + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/hyperaio/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = + [ "${file_api_path}/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp" ] + defines = [] + deps = [ + "${file_api_path}/interfaces/kits/hyperaio:HyperAio", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "access_token:libaccesstoken_sdk", + "hitrace:hitrace_meter", + ] + if (file_api_feature_hyperaio) { + external_deps += [ "liburing:liburing" ] + } + + if (file_api_feature_hyperaio) { + defines += [ "HYPERAIO_USE_LIBURING" ] + } +} + +group("fuzztest") { + testonly = true + deps = [ ":HyperaioFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/corpus/init b/interfaces/test/fuzztest/hyperaio_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_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/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp new file mode 100644 index 000000000..0298c9966 --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.cpp @@ -0,0 +1,79 @@ +/* + * 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 "hyperaio_fuzzer.h" + +#include +#include "hyperaio.h" +#include "fileapi_fuzzer_helper.h" + +namespace OHOS { +std::function)> callBack = [](std::unique_ptr response) {}; + +bool HyperaioStartOpenReqsFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return false; + } + + std::unique_ptr hyperAio_ = std::make_unique(); + hyperAio_->CtxInit(&callBack); + + FuzzData fuzzData(data, size); + // userData + int32_t userData = fuzzData.GetData(); + + OHOS::HyperAio::OpenInfo openInfo = {0, O_RDWR, 0, nullptr, userData}; + OHOS::HyperAio::OpenReqs openReqs = {1, &openInfo}; + + hyperAio_->StartOpenReqs(&openReqs); + hyperAio_->DestroyCtx(); + + return true; +} + +bool HyperaioStartOpenReqsBatchFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return false; + } + + std::unique_ptr hyperAio_ = std::make_unique(); + hyperAio_->CtxInit(&callBack); + + FuzzData fuzzData(data, size); + // userData + int32_t userData = fuzzData.GetData(); + int32_t batchSize = fuzzData.GetData(); + + OHOS::HyperAio::OpenInfo openInfo = {0, O_RDWR, 0, nullptr, userData}; + OHOS::HyperAio::OpenReqs openReqs = {batchSize, &openInfo}; + + hyperAio_->StartOpenReqs(&openReqs); + hyperAio_->DestroyCtx(); + + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ +#ifdef HYPERAIO_USE_LIBURING + /* Run your code on data */ + OHOS::HyperaioStartOpenReqsFuzzTest(data, size); + OHOS::HyperaioStartOpenReqsBatchFuzzTest(data, size); +#endif + return 0; +} diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h new file mode 100644 index 000000000..990c437c6 --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/hyperaio_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 HYPERAIO_FUZZER_H +#define HYPERAIO_FUZZER_H + +#define FUZZ_PROJECT_NAME "hyperaio_fuzzer" + +#endif \ No newline at end of file diff --git a/interfaces/test/fuzztest/hyperaio_fuzzer/project.xml b/interfaces/test/fuzztest/hyperaio_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/hyperaio_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/rustfile_fuzzer/BUILD.gn b/interfaces/test/fuzztest/rustfile_fuzzer/BUILD.gn new file mode 100644 index 000000000..eb898d747 --- /dev/null +++ b/interfaces/test/fuzztest/rustfile_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/filemanagement/file_api/file_api.gni") + +ohos_fuzztest("RustFileFuzzTest") { + module_out_path = "file_api/file_api" + fuzz_config_file = "${file_api_path}/interfaces/test/fuzztest/rustfile_fuzzer" + + include_dirs = [ + "${file_api_path}/interfaces/test/fuzztest/fuzz_common", + "${file_api_path}/interfaces/kits/rust/include", + ] + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "${file_api_path}/interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.cpp" ] + defines = [] + deps = [ "${file_api_path}/interfaces/kits/rust:rust_file" ] + + external_deps = [ "c_utils:utils" ] +} + +group("fuzztest") { + testonly = true + deps = [ ":RustFileFuzzTest" ] +} diff --git a/interfaces/test/fuzztest/rustfile_fuzzer/corpus/init b/interfaces/test/fuzztest/rustfile_fuzzer/corpus/init new file mode 100644 index 000000000..7ade8a0fa --- /dev/null +++ b/interfaces/test/fuzztest/rustfile_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/interfaces/test/fuzztest/rustfile_fuzzer/project.xml b/interfaces/test/fuzztest/rustfile_fuzzer/project.xml new file mode 100644 index 000000000..8cd958170 --- /dev/null +++ b/interfaces/test/fuzztest/rustfile_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.cpp b/interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.cpp new file mode 100644 index 000000000..2708fd57e --- /dev/null +++ b/interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.cpp @@ -0,0 +1,43 @@ +/* + * 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 "rustfile_fuzzer.h" + +#include +#include "fileapi_fuzzer_helper.h" +#include "rust_file.h" + +namespace OHOS { +bool ReaderIteratorFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size < sizeof(int32_t))) { + return false; + } + + FuzzData fuzzData(data, size); + int len = static_cast(fuzzData.GetRemainSize()); + std::string path = fuzzData.GetStringFromData(len); + + ReaderIterator(const_cast(path.c_str())); + return true; +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::ReaderIteratorFuzzTest(data, size); + return 0; +} diff --git a/interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.h b/interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.h new file mode 100644 index 000000000..0a37dfe6a --- /dev/null +++ b/interfaces/test/fuzztest/rustfile_fuzzer/rustfile_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 RUSTFILE_FUZZER_H +#define RUSTFILE_FUZZER_H + +#define FUZZ_PROJECT_NAME "rustfile_fuzzer" + +#endif \ No newline at end of file -- Gitee