From 60001ee29cf9116e4e4bfab65f8681b4e3145b28 Mon Sep 17 00:00:00 2001 From: wangyantian Date: Wed, 25 May 2022 11:28:27 +0800 Subject: [PATCH] Add fuzztest Signed-off-by: wangyantian Change-Id: I51b6e6e2bc08eb42a1fee73ab8586fc19823a873 --- bundle.json | 4 +- libpandafile/file.cpp | 15 +++- tests/fuzztest/BUILD.gn | 30 +++++++ tests/fuzztest/checkheader_fuzzer/BUILD.gn | 44 ++++++++++ .../checkheader_fuzzer/checkheader_fuzzer.cpp | 47 ++++++++++ .../checkheader_fuzzer/checkheader_fuzzer.h | 21 +++++ tests/fuzztest/checkheader_fuzzer/corpus/init | 14 +++ tests/fuzztest/checkheader_fuzzer/project.xml | 25 ++++++ .../literaldataaccessor_fuzzer/BUILD.gn | 45 ++++++++++ .../literaldataaccessor_fuzzer/corpus/init | 14 +++ .../literaldataaccessor_fuzzer.cpp | 38 ++++++++ .../literaldataaccessor_fuzzer.h | 21 +++++ .../literaldataaccessor_fuzzer/project.xml | 25 ++++++ .../methoddataaccessor_fuzzer/BUILD.gn | 45 ++++++++++ .../methoddataaccessor_fuzzer/corpus/init | 14 +++ .../methoddataaccessor_fuzzer.cpp | 48 ++++++++++ .../methoddataaccessor_fuzzer.h | 21 +++++ .../methoddataaccessor_fuzzer/project.xml | 25 ++++++ tests/fuzztest/open_fuzzer/BUILD.gn | 44 ++++++++++ tests/fuzztest/open_fuzzer/corpus/init | 14 +++ tests/fuzztest/open_fuzzer/open_fuzzer.cpp | 44 ++++++++++ tests/fuzztest/open_fuzzer/open_fuzzer.h | 21 +++++ tests/fuzztest/open_fuzzer/project.xml | 25 ++++++ .../openfrommemory1arg_fuzzer/BUILD.gn | 45 ++++++++++ .../openfrommemory1arg_fuzzer/corpus/init | 14 +++ .../openfrommemory1arg_fuzzer.cpp | 36 ++++++++ .../openfrommemory1arg_fuzzer.h | 21 +++++ .../openfrommemory1arg_fuzzer/project.xml | 25 ++++++ .../openfrommemory2arg_fuzzer/BUILD.gn | 45 ++++++++++ .../openfrommemory2arg_fuzzer/corpus/init | 14 +++ .../openfrommemory2arg_fuzzer.cpp | 37 ++++++++ .../openfrommemory2arg_fuzzer.h | 21 +++++ .../openfrommemory2arg_fuzzer/project.xml | 25 ++++++ tests/fuzztest/openpandafile_fuzzer/BUILD.gn | 44 ++++++++++ .../fuzztest/openpandafile_fuzzer/corpus/init | 14 +++ .../openpandafile_fuzzer.cpp | 61 +++++++++++++ .../openpandafile_fuzzer.h | 21 +++++ .../fuzztest/openpandafile_fuzzer/project.xml | 25 ++++++ .../openpandafilefrommemory_fuzzer/BUILD.gn | 45 ++++++++++ .../corpus/init | 14 +++ .../openpandafilefrommemory_fuzzer.cpp | 32 +++++++ .../openpandafilefrommemory_fuzzer.h | 21 +++++ .../project.xml | 25 ++++++ .../openpandafileorzip_fuzzer/BUILD.gn | 45 ++++++++++ .../openpandafileorzip_fuzzer/corpus/init | 14 +++ .../openpandafileorzip_fuzzer.cpp | 61 +++++++++++++ .../openpandafileorzip_fuzzer.h | 21 +++++ .../openpandafileorzip_fuzzer/project.xml | 25 ++++++ .../openuncompressedarchive_fuzzer/BUILD.gn | 45 ++++++++++ .../corpus/init | 14 +++ .../openuncompressedarchive_fuzzer.cpp | 87 +++++++++++++++++++ .../openuncompressedarchive_fuzzer.h | 21 +++++ .../project.xml | 25 ++++++ 53 files changed, 1584 insertions(+), 3 deletions(-) create mode 100644 tests/fuzztest/BUILD.gn create mode 100644 tests/fuzztest/checkheader_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.cpp create mode 100644 tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.h create mode 100644 tests/fuzztest/checkheader_fuzzer/corpus/init create mode 100644 tests/fuzztest/checkheader_fuzzer/project.xml create mode 100644 tests/fuzztest/literaldataaccessor_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/literaldataaccessor_fuzzer/corpus/init create mode 100644 tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.cpp create mode 100644 tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.h create mode 100644 tests/fuzztest/literaldataaccessor_fuzzer/project.xml create mode 100644 tests/fuzztest/methoddataaccessor_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/methoddataaccessor_fuzzer/corpus/init create mode 100644 tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.cpp create mode 100644 tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.h create mode 100644 tests/fuzztest/methoddataaccessor_fuzzer/project.xml create mode 100644 tests/fuzztest/open_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/open_fuzzer/corpus/init create mode 100644 tests/fuzztest/open_fuzzer/open_fuzzer.cpp create mode 100644 tests/fuzztest/open_fuzzer/open_fuzzer.h create mode 100644 tests/fuzztest/open_fuzzer/project.xml create mode 100644 tests/fuzztest/openfrommemory1arg_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/openfrommemory1arg_fuzzer/corpus/init create mode 100644 tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.cpp create mode 100644 tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.h create mode 100644 tests/fuzztest/openfrommemory1arg_fuzzer/project.xml create mode 100644 tests/fuzztest/openfrommemory2arg_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/openfrommemory2arg_fuzzer/corpus/init create mode 100644 tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.cpp create mode 100644 tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.h create mode 100644 tests/fuzztest/openfrommemory2arg_fuzzer/project.xml create mode 100644 tests/fuzztest/openpandafile_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/openpandafile_fuzzer/corpus/init create mode 100644 tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.cpp create mode 100644 tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.h create mode 100644 tests/fuzztest/openpandafile_fuzzer/project.xml create mode 100644 tests/fuzztest/openpandafilefrommemory_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/openpandafilefrommemory_fuzzer/corpus/init create mode 100644 tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.cpp create mode 100644 tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.h create mode 100644 tests/fuzztest/openpandafilefrommemory_fuzzer/project.xml create mode 100644 tests/fuzztest/openpandafileorzip_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/openpandafileorzip_fuzzer/corpus/init create mode 100644 tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.cpp create mode 100644 tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.h create mode 100644 tests/fuzztest/openpandafileorzip_fuzzer/project.xml create mode 100644 tests/fuzztest/openuncompressedarchive_fuzzer/BUILD.gn create mode 100644 tests/fuzztest/openuncompressedarchive_fuzzer/corpus/init create mode 100644 tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.cpp create mode 100644 tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.h create mode 100644 tests/fuzztest/openuncompressedarchive_fuzzer/project.xml diff --git a/bundle.json b/bundle.json index 5ae6f52..ca732bd 100644 --- a/bundle.json +++ b/bundle.json @@ -29,7 +29,9 @@ "//ark/runtime_core:ark_packages" ], "inner_kits": [], - "test": [] + "test": [ + "//ark/runtime_core/tests/fuzztest:fuzztest" + ] } } } diff --git a/libpandafile/file.cpp b/libpandafile/file.cpp index df261bc..0ff03d2 100644 --- a/libpandafile/file.cpp +++ b/libpandafile/file.cpp @@ -253,6 +253,13 @@ std::unique_ptr OpenPandaFile(std::string_view location, LOG(ERROR, PANDAFILE) << "GetCurrentFileInfo error"; return nullptr; } + // check that file is not empty, otherwise crash at CloseArchiveFile + if (entry.GetUncompressedSize() == 0) { + OpenPandaFileFromZipErrorHandler(zipfile); + LOG(ERROR, PANDAFILE) << "Invalid panda file '" << (try_default ? ARCHIVE_FILENAME : archive_filename) + << "'"; + return nullptr; + } if (OpenCurrentFile(zipfile) != ZIPARCHIVE_OK) { CloseCurrentFile(zipfile); OpenPandaFileFromZipErrorHandler(zipfile); @@ -531,7 +538,11 @@ std::unique_ptr File::OpenUncompressedArchive(int fd, const std::str bool CheckHeader(const os::mem::ConstBytePtr &ptr, const std::string_view &filename) { - auto header = reinterpret_cast(ptr.Get()); + if (ptr.Get() == nullptr || ptr.GetSize() < sizeof(File::Header)) { + LOG(ERROR, PANDAFILE) << "Invalid panda file '" << filename << "'"; + return false; + } + auto header = reinterpret_cast(reinterpret_cast(ptr.Get())); if (header->magic != File::MAGIC) { LOG(ERROR, PANDAFILE) << "Invalid panda file '" << filename << "'"; return false; @@ -543,7 +554,7 @@ bool CheckHeader(const os::mem::ConstBytePtr &ptr, const std::string_view &filen /* static */ std::unique_ptr File::OpenFromMemory(os::mem::ConstBytePtr &&ptr) { - auto header = reinterpret_cast(ptr.Get()); + auto header = reinterpret_cast(reinterpret_cast(ptr.Get())); if (header->magic != File::MAGIC) { LOG(ERROR, PANDAFILE) << "Invalid panda file"; return nullptr; diff --git a/tests/fuzztest/BUILD.gn b/tests/fuzztest/BUILD.gn new file mode 100644 index 0000000..bbfb8cb --- /dev/null +++ b/tests/fuzztest/BUILD.gn @@ -0,0 +1,30 @@ +# 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. + +group("fuzztest") { + testonly = true + deps = [] + + deps += [ + "checkheader_fuzzer:fuzztest", + "literaldataaccessor_fuzzer:fuzztest", + "methoddataaccessor_fuzzer:fuzztest", + "open_fuzzer:fuzztest", + "openfrommemory1arg_fuzzer:fuzztest", + "openfrommemory2arg_fuzzer:fuzztest", + "openpandafile_fuzzer:fuzztest", + "openpandafilefrommemory_fuzzer:fuzztest", + "openpandafileorzip_fuzzer:fuzztest", + "openuncompressedarchive_fuzzer:fuzztest", + ] +} diff --git a/tests/fuzztest/checkheader_fuzzer/BUILD.gn b/tests/fuzztest/checkheader_fuzzer/BUILD.gn new file mode 100644 index 0000000..439af15 --- /dev/null +++ b/tests/fuzztest/checkheader_fuzzer/BUILD.gn @@ -0,0 +1,44 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("CheckHeaderFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//ark/runtime_core/tests/fuzztest/checkheader_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "checkheader_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":CheckHeaderFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.cpp b/tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.cpp new file mode 100644 index 0000000..1421ca5 --- /dev/null +++ b/tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.cpp @@ -0,0 +1,47 @@ +/* + * 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. + */ + +#include "checkheader_fuzzer.h" +#include "libpandafile/file.h" + +namespace OHOS { +void CheckHeaderFuzzTest(const uint8_t *data, size_t size) +{ + // Write data into a temp file + const char *filename = "__ChechHeaderFuzzTest_data.tmp"; + FILE *fp = fopen(filename, "w+"); + if (fp == nullptr) { + return; + } + (void)fwrite(data, sizeof(uint8_t), size, fp); + (void)fseek(fp, 0, SEEK_SET); + + auto file = panda::os::file::File(fileno(fp)); + panda::os::mem::ConstBytePtr ptr = + panda::os::mem::MapFile(file, panda::os::mem::MMAP_PROT_READ, panda::os::mem::MMAP_FLAG_PRIVATE, size, 0) + .ToConst(); + panda::panda_file::CheckHeader(ptr, filename); + (void)fclose(fp); + (void)remove(filename); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::CheckHeaderFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.h b/tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.h new file mode 100644 index 0000000..e502a34 --- /dev/null +++ b/tests/fuzztest/checkheader_fuzzer/checkheader_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef CHECKHEADER_FUZZER_H +#define CHECKHEADER_FUZZER_H + +#define FUZZ_PROJECT_NAME "checkheader_fuzzer" + +#endif diff --git a/tests/fuzztest/checkheader_fuzzer/corpus/init b/tests/fuzztest/checkheader_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/checkheader_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/checkheader_fuzzer/project.xml b/tests/fuzztest/checkheader_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/checkheader_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/literaldataaccessor_fuzzer/BUILD.gn b/tests/fuzztest/literaldataaccessor_fuzzer/BUILD.gn new file mode 100644 index 0000000..86b45ce --- /dev/null +++ b/tests/fuzztest/literaldataaccessor_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("LiteralDataAccessorFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//ark/runtime_core/tests/fuzztest/literaldataaccessor_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "literaldataaccessor_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":LiteralDataAccessorFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/literaldataaccessor_fuzzer/corpus/init b/tests/fuzztest/literaldataaccessor_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/literaldataaccessor_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.cpp b/tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.cpp new file mode 100644 index 0000000..49e8bfb --- /dev/null +++ b/tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.cpp @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#include "literaldataaccessor_fuzzer.h" +#include "libpandafile/file.h" +#include "libpandafile/literal_data_accessor.h" + +namespace OHOS { +void LiteralDataAccessorFuzzTest(const uint8_t *data, size_t size) +{ + auto pf = panda::panda_file::OpenPandaFileFromMemory(data, size); + if (pf == nullptr) { + return; + } + panda::panda_file::File::EntityId literal_arrays_id = pf->GetLiteralArraysId(); + panda::panda_file::LiteralDataAccessor(*pf, literal_arrays_id); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::LiteralDataAccessorFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.h b/tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.h new file mode 100644 index 0000000..6012a91 --- /dev/null +++ b/tests/fuzztest/literaldataaccessor_fuzzer/literaldataaccessor_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef LITERALDATAACCESSOR_FUZZER_H +#define LITERALDATAACCESSOR_FUZZER_H + +#define FUZZ_PROJECT_NAME "literaldataaccessor_fuzzer" + +#endif diff --git a/tests/fuzztest/literaldataaccessor_fuzzer/project.xml b/tests/fuzztest/literaldataaccessor_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/literaldataaccessor_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/methoddataaccessor_fuzzer/BUILD.gn b/tests/fuzztest/methoddataaccessor_fuzzer/BUILD.gn new file mode 100644 index 0000000..caba471 --- /dev/null +++ b/tests/fuzztest/methoddataaccessor_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("MethodDataAccessorFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//ark/runtime_core/tests/fuzztest/methoddataaccessor_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "methoddataaccessor_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":MethodDataAccessorFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/methoddataaccessor_fuzzer/corpus/init b/tests/fuzztest/methoddataaccessor_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/methoddataaccessor_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.cpp b/tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.cpp new file mode 100644 index 0000000..0c047f3 --- /dev/null +++ b/tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.cpp @@ -0,0 +1,48 @@ +/* + * 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. + */ + +#include "methoddataaccessor_fuzzer.h" +#include "libpandafile/file.h" +#include "libpandafile/method_data_accessor.h" +#include "libpandafile/class_data_accessor-inl.h" + +namespace OHOS { +void MethodDataAccessorFuzzTest(const uint8_t *data, size_t size) +{ + auto pf = panda::panda_file::OpenPandaFileFromMemory(data, size); + if (pf == nullptr) { + return; + } + auto classes = pf->GetClasses(); + const auto &panda_file = *pf; + for (size_t i = 0; i < classes.Size(); i++) { + panda::panda_file::File::EntityId id(classes[i]); + if (panda_file.IsExternal(id)) { + continue; + } + + panda::panda_file::ClassDataAccessor cda(panda_file, id); + cda.EnumerateMethods([&](const panda::panda_file::MethodDataAccessor &mda) {}); + } +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::MethodDataAccessorFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.h b/tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.h new file mode 100644 index 0000000..7088415 --- /dev/null +++ b/tests/fuzztest/methoddataaccessor_fuzzer/methoddataaccessor_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef METHODDATAACCESSOR_FUZZER_H +#define METHODDATAACCESSOR_FUZZER_H + +#define FUZZ_PROJECT_NAME "methoddataaccessor_fuzzer" + +#endif diff --git a/tests/fuzztest/methoddataaccessor_fuzzer/project.xml b/tests/fuzztest/methoddataaccessor_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/methoddataaccessor_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/open_fuzzer/BUILD.gn b/tests/fuzztest/open_fuzzer/BUILD.gn new file mode 100644 index 0000000..d33a8fd --- /dev/null +++ b/tests/fuzztest/open_fuzzer/BUILD.gn @@ -0,0 +1,44 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("OpenFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//ark/runtime_core/tests/fuzztest/open_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "open_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":OpenFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/open_fuzzer/corpus/init b/tests/fuzztest/open_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/open_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/open_fuzzer/open_fuzzer.cpp b/tests/fuzztest/open_fuzzer/open_fuzzer.cpp new file mode 100644 index 0000000..13c9382 --- /dev/null +++ b/tests/fuzztest/open_fuzzer/open_fuzzer.cpp @@ -0,0 +1,44 @@ +/* + * 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. + */ + +#include "open_fuzzer.h" +#include "libpandafile/file.h" + +namespace OHOS { +void OpenFuzzTest(const uint8_t *data, size_t size) +{ + const char *filename = "__OpenFuzzTest_data.tmp"; + FILE *fp = fopen(filename, "w"); + if (fp == nullptr) { + return; + } + (void)fwrite(data, sizeof(uint8_t), size, fp); + (void)fclose(fp); + + { + panda::panda_file::File::Open(filename); + } + // Remove the temp file + (void)remove(filename); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::OpenFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/tests/fuzztest/open_fuzzer/open_fuzzer.h b/tests/fuzztest/open_fuzzer/open_fuzzer.h new file mode 100644 index 0000000..42e61ca --- /dev/null +++ b/tests/fuzztest/open_fuzzer/open_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef OPEN_FUZZER_H +#define OPEN_FUZZER_H + +#define FUZZ_PROJECT_NAME "open_fuzzer" + +#endif diff --git a/tests/fuzztest/open_fuzzer/project.xml b/tests/fuzztest/open_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/open_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/openfrommemory1arg_fuzzer/BUILD.gn b/tests/fuzztest/openfrommemory1arg_fuzzer/BUILD.gn new file mode 100644 index 0000000..3f43d3f --- /dev/null +++ b/tests/fuzztest/openfrommemory1arg_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("OpenFromMemory1ArgFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//ark/runtime_core/tests/fuzztest/openfrommemory1arg_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "openfrommemory1arg_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":OpenFromMemory1ArgFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/openfrommemory1arg_fuzzer/corpus/init b/tests/fuzztest/openfrommemory1arg_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/openfrommemory1arg_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.cpp b/tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.cpp new file mode 100644 index 0000000..3a83955 --- /dev/null +++ b/tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.cpp @@ -0,0 +1,36 @@ +/* + * 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. + */ + +#include "openfrommemory1arg_fuzzer.h" +#include +#include "libpandafile/file.h" + +namespace OHOS { +void OpenFromMemory1ArgFuzzTest(const uint8_t *data, size_t size) +{ + panda::os::mem::ConstBytePtr ptr( + reinterpret_cast(reinterpret_cast(const_cast(data))), size, + [](std::byte *, size_t) noexcept {}); + panda::panda_file::File::OpenFromMemory(std::move(ptr)); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::OpenFromMemory1ArgFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.h b/tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.h new file mode 100644 index 0000000..55872df --- /dev/null +++ b/tests/fuzztest/openfrommemory1arg_fuzzer/openfrommemory1arg_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef OPENFROMMEMORY1_FUZZER_H +#define OPENFROMMEMORY1_FUZZER_H + +#define FUZZ_PROJECT_NAME "openfrommemory1arg_fuzzer" + +#endif \ No newline at end of file diff --git a/tests/fuzztest/openfrommemory1arg_fuzzer/project.xml b/tests/fuzztest/openfrommemory1arg_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/openfrommemory1arg_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/openfrommemory2arg_fuzzer/BUILD.gn b/tests/fuzztest/openfrommemory2arg_fuzzer/BUILD.gn new file mode 100644 index 0000000..6d80130 --- /dev/null +++ b/tests/fuzztest/openfrommemory2arg_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("OpenFromMemory2ArgFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//ark/runtime_core/tests/fuzztest/openfrommemory2arg_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "openfrommemory2arg_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":OpenFromMemory2ArgFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/openfrommemory2arg_fuzzer/corpus/init b/tests/fuzztest/openfrommemory2arg_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/openfrommemory2arg_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.cpp b/tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.cpp new file mode 100644 index 0000000..b1b4125 --- /dev/null +++ b/tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.cpp @@ -0,0 +1,37 @@ +/* + * 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. + */ + +#include "openfrommemory2arg_fuzzer.h" +#include +#include "libpandafile/file.h" + +namespace OHOS { +void OpenFromMemory2ArgFuzzTest(const uint8_t *data, size_t size) +{ + panda::os::mem::ConstBytePtr ptr( + reinterpret_cast(reinterpret_cast(const_cast(data))), size, + [](std::byte *, size_t) noexcept {}); + std::hash hash; + panda::panda_file::File::OpenFromMemory(std::move(ptr), std::to_string(hash(data))); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::OpenFromMemory2ArgFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.h b/tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.h new file mode 100644 index 0000000..97d02d2 --- /dev/null +++ b/tests/fuzztest/openfrommemory2arg_fuzzer/openfrommemory2arg_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef OPENFROMMEMORY2_FUZZER_H +#define OPENFROMMEMORY2_FUZZER_H + +#define FUZZ_PROJECT_NAME "openfrommemory2arg_fuzzer" + +#endif \ No newline at end of file diff --git a/tests/fuzztest/openfrommemory2arg_fuzzer/project.xml b/tests/fuzztest/openfrommemory2arg_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/openfrommemory2arg_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/openpandafile_fuzzer/BUILD.gn b/tests/fuzztest/openpandafile_fuzzer/BUILD.gn new file mode 100644 index 0000000..a64cf87 --- /dev/null +++ b/tests/fuzztest/openpandafile_fuzzer/BUILD.gn @@ -0,0 +1,44 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("OpenPandaFileFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = "//ark/runtime_core/tests/fuzztest/openpandafile_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "openpandafile_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":OpenPandaFileFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/openpandafile_fuzzer/corpus/init b/tests/fuzztest/openpandafile_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/openpandafile_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.cpp b/tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.cpp new file mode 100644 index 0000000..4ec7747 --- /dev/null +++ b/tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.cpp @@ -0,0 +1,61 @@ +/* + * 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. + */ + +#include "openpandafile_fuzzer.h" +#include "libpandafile/file.h" +#include "libziparchive/zip_archive.h" + +namespace OHOS { +void OpenPandaFileFuzzTest(const uint8_t *data, size_t size) +{ + // Create zip file + const char *filename1 = panda::panda_file::ARCHIVE_FILENAME; + const char *filename2 = "classes1.abc"; + + const char *zip_filename1 = "__OpenPandaFileFuzzTest.zip"; + int ret1 = + panda::CreateOrAddFileIntoZip(zip_filename1, filename1, data, size, APPEND_STATUS_CREATE, Z_BEST_COMPRESSION); + int ret2 = + panda::CreateOrAddFileIntoZip(zip_filename1, filename2, data, size, APPEND_STATUS_ADDINZIP, Z_BEST_COMPRESSION); + if (ret1 != 0 || ret2 != 0) { + (void)remove(zip_filename1); + return; + } + + const char *zip_filename2 = "__OpenPandaFileFromZipNameAnonMem.zip"; + int ret3 = + panda::CreateOrAddFileIntoZip(zip_filename2, filename1, data, size, APPEND_STATUS_CREATE, Z_BEST_COMPRESSION); + if (ret3 != 0) { + (void)remove(zip_filename2); + return; + } + + // Call OpenPandaFile + { + panda::panda_file::OpenPandaFile(zip_filename1); + panda::panda_file::OpenPandaFile(zip_filename2); + } + (void)remove(zip_filename1); + (void)remove(zip_filename2); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::OpenPandaFileFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.h b/tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.h new file mode 100644 index 0000000..962c6e7 --- /dev/null +++ b/tests/fuzztest/openpandafile_fuzzer/openpandafile_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef OPENPANDAFILE_FUZZER_H +#define OPENPANDAFILE_FUZZER_H + +#define FUZZ_PROJECT_NAME "openpandafile_fuzzer" + +#endif diff --git a/tests/fuzztest/openpandafile_fuzzer/project.xml b/tests/fuzztest/openpandafile_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/openpandafile_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/openpandafilefrommemory_fuzzer/BUILD.gn b/tests/fuzztest/openpandafilefrommemory_fuzzer/BUILD.gn new file mode 100644 index 0000000..ab11634 --- /dev/null +++ b/tests/fuzztest/openpandafilefrommemory_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("OpenPandaFileFromMemoryFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//ark/runtime_core/tests/fuzztest/openpandafilefrommemory_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "openpandafilefrommemory_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":OpenPandaFileFromMemoryFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/openpandafilefrommemory_fuzzer/corpus/init b/tests/fuzztest/openpandafilefrommemory_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/openpandafilefrommemory_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.cpp b/tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.cpp new file mode 100644 index 0000000..36bf552 --- /dev/null +++ b/tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.cpp @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#include "openpandafilefrommemory_fuzzer.h" +#include "libpandafile/file.h" + +namespace OHOS { +void OpenPandaFileFromMemoryFuzzTest(const uint8_t *data, size_t size) +{ + panda::panda_file::OpenPandaFileFromMemory(data, size); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::OpenPandaFileFromMemoryFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.h b/tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.h new file mode 100644 index 0000000..0be44ae --- /dev/null +++ b/tests/fuzztest/openpandafilefrommemory_fuzzer/openpandafilefrommemory_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef OPENPANDAFILEFROMMEMORY_FUZZER_H +#define OPENPANDAFILEFROMMEMORY_FUZZER_H + +#define FUZZ_PROJECT_NAME "openpandafilefrommemory_fuzzer" + +#endif diff --git a/tests/fuzztest/openpandafilefrommemory_fuzzer/project.xml b/tests/fuzztest/openpandafilefrommemory_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/openpandafilefrommemory_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/openpandafileorzip_fuzzer/BUILD.gn b/tests/fuzztest/openpandafileorzip_fuzzer/BUILD.gn new file mode 100644 index 0000000..eb58e85 --- /dev/null +++ b/tests/fuzztest/openpandafileorzip_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("OpenPandaFileOrZipFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//ark/runtime_core/tests/fuzztest/openpandafileorzip_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "openpandafileorzip_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":OpenPandaFileOrZipFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/openpandafileorzip_fuzzer/corpus/init b/tests/fuzztest/openpandafileorzip_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/openpandafileorzip_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.cpp b/tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.cpp new file mode 100644 index 0000000..4043a53 --- /dev/null +++ b/tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.cpp @@ -0,0 +1,61 @@ +/* + * 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. + */ + +#include "openpandafileorzip_fuzzer.h" +#include "libpandafile/file.h" +#include "libziparchive/zip_archive.h" + +namespace OHOS { +void OpenPandaFileOrZipFuzzTest(const uint8_t *data, size_t size) +{ + const char *filename1 = panda::panda_file::ARCHIVE_FILENAME; + const char *filename2 = "classes1.abc"; + // Create uncompressed zip file + const char *uncompress_zip_filename = "__OpenPandaFileOrZipFuzzTest_uncompress.zip"; + int ret1 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename1, data, size, APPEND_STATUS_CREATE, + Z_NO_COMPRESSION); + int ret2 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename2, data, size, APPEND_STATUS_ADDINZIP, + Z_NO_COMPRESSION); + if (ret1 != 0 || ret2 != 0) { + (void)remove(uncompress_zip_filename); + return; + } + // Create compressed zip file + const char *compressed_zip_filename = "__OpenPandaFileOrZipFuzzTest_compressed.zip"; + ret1 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename1, data, size, APPEND_STATUS_CREATE, + Z_BEST_COMPRESSION); + ret2 = panda::CreateOrAddFileIntoZip(uncompress_zip_filename, filename2, data, size, APPEND_STATUS_ADDINZIP, + Z_BEST_COMPRESSION); + if (ret1 != 0 || ret2 != 0) { + (void)remove(compressed_zip_filename); + return; + } + + { + panda::panda_file::OpenPandaFileOrZip(uncompress_zip_filename); + panda::panda_file::OpenPandaFileOrZip(compressed_zip_filename); + } + (void)remove(uncompress_zip_filename); + (void)remove(compressed_zip_filename); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::OpenPandaFileOrZipFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.h b/tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.h new file mode 100644 index 0000000..1835554 --- /dev/null +++ b/tests/fuzztest/openpandafileorzip_fuzzer/openpandafileorzip_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef OPENPANDAFILEORZIP_FUZZER_H +#define OPENPANDAFILEORZIP_FUZZER_H + +#define FUZZ_PROJECT_NAME "openpandafileorzip_fuzzer" + +#endif diff --git a/tests/fuzztest/openpandafileorzip_fuzzer/project.xml b/tests/fuzztest/openpandafileorzip_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/openpandafileorzip_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/tests/fuzztest/openuncompressedarchive_fuzzer/BUILD.gn b/tests/fuzztest/openuncompressedarchive_fuzzer/BUILD.gn new file mode 100644 index 0000000..898358e --- /dev/null +++ b/tests/fuzztest/openuncompressedarchive_fuzzer/BUILD.gn @@ -0,0 +1,45 @@ +# 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. + +import("//build/ohos.gni") + +#####################hydra-fuzz################### +import("//build/test.gni") +module_output_path = "ark/runtime_core" + +##############################fuzztest########################################## +ohos_fuzztest("OpenUncompressedArchiveFuzzTest") { + module_out_path = module_output_path + fuzz_config_file = + "//ark/runtime_core/tests/fuzztest/openuncompressedarchive_fuzzer" + include_dirs = [] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "openuncompressedarchive_fuzzer.cpp" ] + deps = [ "//ark/runtime_core/libpandafile:libarkfile_static" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [] + deps += [ + # deps file + ":OpenUncompressedArchiveFuzzTest", + ] +} +############################################################################### diff --git a/tests/fuzztest/openuncompressedarchive_fuzzer/corpus/init b/tests/fuzztest/openuncompressedarchive_fuzzer/corpus/init new file mode 100644 index 0000000..d175fac --- /dev/null +++ b/tests/fuzztest/openuncompressedarchive_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# 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/tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.cpp b/tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.cpp new file mode 100644 index 0000000..8eb2fbe --- /dev/null +++ b/tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.cpp @@ -0,0 +1,87 @@ +/* + * 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. + */ + +#include "openuncompressedarchive_fuzzer.h" +#include "libpandafile/file.h" +#include "libziparchive/zip_archive.h" + +namespace OHOS { +void CloseAndRemoveZipFile(panda::ZipArchiveHandle &handle, FILE *fp, const char *filename) +{ + panda::CloseArchiveFile(handle); + (void)fclose(fp); + (void)remove(filename); +} + +void OpenUncompressedArchiveFuzzTest(const uint8_t *data, size_t size) +{ + // Create zip file + const char *zip_filename = "__OpenUncompressedArchiveFuzzTest.zip"; + const char *filename = panda::panda_file::ARCHIVE_FILENAME; + int ret = panda::CreateOrAddFileIntoZip(zip_filename, filename, data, size, APPEND_STATUS_CREATE, Z_NO_COMPRESSION); + if (ret != 0) { + (void)remove(zip_filename); + return; + } + + // Acquire entry +#ifdef PANDA_TARGET_WINDOWS + constexpr char const *mode = "rb"; +#else + constexpr char const *mode = "rbe"; +#endif + FILE *fp = fopen(zip_filename, mode); + if (fp == nullptr) { + (void)remove(zip_filename); + return; + } + panda::ZipArchiveHandle zipfile = nullptr; + if (panda::OpenArchiveFile(zipfile, fp) != panda::ZIPARCHIVE_OK) { + (void)fclose(fp); + (void)remove(zip_filename); + return; + } + if (panda::LocateFile(zipfile, filename) != panda::ZIPARCHIVE_OK) { + CloseAndRemoveZipFile(zipfile, fp, zip_filename); + return; + } + panda::EntryFileStat entry; + if (panda::GetCurrentFileInfo(zipfile, &entry) != panda::ZIPARCHIVE_OK) { + CloseAndRemoveZipFile(zipfile, fp, zip_filename); + return; + } + if (panda::OpenCurrentFile(zipfile) != panda::ZIPARCHIVE_OK) { + panda::CloseCurrentFile(zipfile); + CloseAndRemoveZipFile(zipfile, fp, zip_filename); + return; + } + panda::GetCurrentFileOffset(zipfile, &entry); + // Call OpenUncompressedArchive + { + panda::panda_file::File::OpenUncompressedArchive(fileno(fp), zip_filename, entry.GetUncompressedSize(), + entry.GetOffset()); + } + panda::CloseCurrentFile(zipfile); + CloseAndRemoveZipFile(zipfile, fp, zip_filename); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::OpenUncompressedArchiveFuzzTest(data, size); + return 0; +} diff --git a/tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.h b/tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.h new file mode 100644 index 0000000..bce7aec --- /dev/null +++ b/tests/fuzztest/openuncompressedarchive_fuzzer/openuncompressedarchive_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef OPENUNCOMPRESSEDARCHIVE_FUZZER_H +#define OPENUNCOMPRESSEDARCHIVE_FUZZER_H + +#define FUZZ_PROJECT_NAME "openuncompressedarchive_fuzzer" + +#endif \ No newline at end of file diff --git a/tests/fuzztest/openuncompressedarchive_fuzzer/project.xml b/tests/fuzztest/openuncompressedarchive_fuzzer/project.xml new file mode 100644 index 0000000..17cfa23 --- /dev/null +++ b/tests/fuzztest/openuncompressedarchive_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +