Add fuzztests and the check of source code

- Add fuzztests for five methods and the failure of these tests are also fixed
  methods including: WriteByte, OpenArchive, ReadOsFile, two constructors of JsonObject
- Source code may be empty in some cases, so add check before getting it

Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/I5YVN7
Test: fuzztest

Signed-off-by: wangyantian <wangyantian@huawei.com>
Change-Id: I9fb01cbda2462c0c2c5e0dd6c2f4af23b336dc57
This commit is contained in:
wangyantian 2022-11-01 11:37:58 +08:00
parent 2e45596a49
commit 00f455a6e2
35 changed files with 822 additions and 6 deletions

View File

@ -27,6 +27,11 @@ config("arkbase_public_config") {
}
}
config("arkbase_fuzz_config") {
configs = [ "$build_root/config/compiler:exceptions" ]
defines = [ "SUPPORT_KNOWN_EXCEPTION" ]
}
ark_gen_file("events_gen") {
template_file = "events/events_gen.h.erb"
data_file = "events/events.yaml"
@ -179,6 +184,15 @@ libarkbase_deps = [
sdk_libc_secshared_dep,
]
source_set("libarkbase_static_fuzz") {
sources = libarkbase_sources
public_configs = libarkbase_configs
public_configs += [ ":arkbase_fuzz_config" ]
deps = libarkbase_deps
}
source_set("libarkbase_static") {
sources = libarkbase_sources

View File

@ -16,6 +16,7 @@
#include "dfx_option.h"
#include "macros.h"
#include "utils/utils.h"
namespace panda::os::dfx_option {
@ -42,7 +43,11 @@ DfxOptionHandler::DfxOption DfxOptionHandler::DfxOptionFromString(const std::str
}
DFX_OPTION_LIST(D)
#undef D
#ifndef SUPPORT_KNOWN_EXCEPTION
UNREACHABLE();
#else
throw panda::UnreachableException(INVALID_DFX_OPTION);
#endif
}
/* static */

View File

@ -22,6 +22,7 @@
namespace panda::os::dfx_option {
constexpr const char *INVALID_DFX_OPTION = "Invalid dfx option";
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define DFX_OPTION_ELEM(D, NAME, STR) D(NAME, DfxOptionHandler::DfxOptionId::NAME##_ID, STR)

View File

@ -75,10 +75,18 @@ void DfxController::ResetOptionValueFromString(const std::string &s)
last_pos = s.find_first_not_of(';', pos);
pos = s.find(';', last_pos);
std::string option_str = arg.substr(0, arg.find(':'));
uint8_t value = static_cast<uint8_t>(std::stoi(arg.substr(arg.find(':') + 1)));
std::string value_str = arg.substr(arg.find(':') + 1);
char *end_ptr = nullptr;
constexpr int DECIMAL = 10;
long value = strtol(value_str.c_str(), &end_ptr, DECIMAL);
if ((errno != 0 && value == 0) || end_ptr == value_str.c_str() || value > std::numeric_limits<uint8_t>::max() ||
value < std::numeric_limits<uint8_t>::min()) {
LOG(ERROR, DFX) << "Invalid argument: " << s;
return;
}
auto dfx_option = DfxOptionHandler::DfxOptionFromString(option_str);
if (dfx_option != DfxOptionHandler::END_FLAG) {
DfxController::SetOptionValue(dfx_option, value);
DfxController::SetOptionValue(dfx_option, static_cast<uint8_t>(value));
#ifdef PANDA_TARGET_UNIX
if (dfx_option == DfxOptionHandler::MOBILE_LOG) {
if (value == 0) {

View File

@ -238,7 +238,7 @@ bool JsonObject::Parser::GetBool()
bool JsonObject::Parser::GetValue()
{
auto symbol = PeekSymbol();
auto pos_start = istream_.tellg();
size_t pos_start = istream_.tellg();
bool res = false;
switch (symbol) {
case 't':
@ -283,8 +283,11 @@ bool JsonObject::Parser::GetValue()
}
// Save source string of parsed value:
auto pos_end = istream_.tellg();
auto size = static_cast<size_t>(pos_end - pos_start);
size_t pos_end = istream_.tellg();
if (pos_end == static_cast<size_t>(-1)) {
return false;
}
size_t size = pos_end - pos_start;
string_temp_.resize(size, '\0');
istream_.seekg(pos_start);
istream_.read(&string_temp_[0], size);

View File

@ -15,6 +15,10 @@
#ifndef PANDA_LIBPANDABASE_UTILS_UTILS_H_
#define PANDA_LIBPANDABASE_UTILS_UTILS_H_
#include <exception>
#include <string>
namespace panda {
// ----------------------------------------------------------------------------
// General helper functions
@ -39,5 +43,19 @@ inline uint32_t HexValue(uint32_t c)
return -1;
}
// General helper class
class UnreachableException : public std::exception {
public:
explicit UnreachableException(const char *msg) : msg_(msg) {}
explicit UnreachableException(const std::string_view &msg) : msg_(msg) {}
const char *what() const noexcept override
{
return msg_.c_str();
}
private:
std::string msg_;
};
} // namespace panda
#endif

View File

@ -242,6 +242,9 @@ void DebugInfoExtractor::Extract(const File *pf)
File::EntityId method_id = mda.GetMethodId();
const char *source_file = utf::Mutf8AsCString(handler.GetFile());
const char *source_code = utf::Mutf8AsCString(handler.GetSourceCode());
if (UNLIKELY(source_code == nullptr)) {
source_code = "";
}
methods_.push_back({source_file, source_code, method_id, handler.GetLineNumberTable(),
handler.GetLocalVariableTable(), std::move(param_info),
handler.GetColumnNumberTable()});

View File

@ -38,7 +38,7 @@ class FileAccessException : public std::exception {
public:
explicit FileAccessException(const char *msg) : msg_(msg) {}
explicit FileAccessException(const std::string_view &msg) : msg_(msg) {}
virtual const char *what() const noexcept
const char *what() const noexcept override
{
return msg_.c_str();
}

View File

@ -60,6 +60,9 @@ public:
const uint8_t *GetSourceCode() const
{
if (UNLIKELY(!HasSourceCode())) {
return nullptr;
}
return pf_.GetStringData(source_code_).data;
}

View File

@ -28,17 +28,22 @@ group("fuzztest") {
"fielddataaccessor_fuzzer:fuzztest",
"int64tracepoint_fuzzer:fuzztest",
"inttracepoint_fuzzer:fuzztest",
"jsonobjectctor1_fuzzer:fuzztest",
"jsonobjectctor2_fuzzer:fuzztest",
"literaldataaccessor_fuzzer:fuzztest",
"load_fuzzer:fuzztest",
"methoddataaccessor_fuzzer:fuzztest",
"open_fuzzer:fuzztest",
"openarchive_fuzzer:fuzztest",
"openfrommemory1arg_fuzzer:fuzztest",
"openfrommemory2arg_fuzzer:fuzztest",
"openpandafile_fuzzer:fuzztest",
"openpandafilefrommemory_fuzzer:fuzztest",
"openpandafileorzip_fuzzer:fuzztest",
"openuncompressedarchive_fuzzer:fuzztest",
"readosfile_fuzzer:fuzztest",
"resolvesymbol_fuzzer:fuzztest",
"writebyte_fuzzer:fuzztest",
]
}

View File

@ -0,0 +1,50 @@
# 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 = "arkcompiler/runtime_core"
##############################fuzztest##########################################
ohos_fuzztest("JsonObjectCtor1FuzzTest") {
module_out_path = module_output_path
include_dirs = []
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "jsonobjectctor1_fuzzer.cpp" ]
deps = [ "//arkcompiler/runtime_core/libpandabase:libarkbase_static" ]
# Temporary adaptation for fuzztest building without musl
if (!use_musl && (current_cpu == "arm64" || current_cpu == "arm")) {
deps += [ "//arkcompiler/runtime_core/tests/fuzztest:runtime_core_fuzz_rt" ]
}
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":JsonObjectCtor1FuzzTest",
]
}
###############################################################################

View File

@ -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

View File

@ -0,0 +1,34 @@
/*
* 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 "jsonobjectctor1_fuzzer.h"
#include "utils/json_parser.h"
namespace OHOS {
void JsonObjectCtor1FuzzTest(const uint8_t *data, size_t size)
{
std::string text(data, data + size);
panda::JsonObject json_object(text);
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::JsonObjectCtor1FuzzTest(data, size);
return 0;
}

View File

@ -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 TESTS_FUZZTEST_JSONOBJECTCTOR1_FUZZER_JSONOBJECTCTOR1_FUZZER_H
#define TESTS_FUZZTEST_JSONOBJECTCTOR1_FUZZER_JSONOBJECTCTOR1_FUZZER_H
#define FUZZ_PROJECT_NAME "jsonobjectctor1_fuzzer"
#endif // TESTS_FUZZTEST_JSONOBJECTCTOR1_FUZZER_JSONOBJECTCTOR1_FUZZER_H

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -0,0 +1,49 @@
# 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 = "arkcompiler/runtime_core"
##############################fuzztest##########################################
ohos_fuzztest("JsonObjectCtor2FuzzTest") {
module_out_path = module_output_path
include_dirs = []
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "jsonobjectctor2_fuzzer.cpp" ]
deps = [ "//arkcompiler/runtime_core/libpandabase:libarkbase_static" ]
# Temporary adaptation for fuzztest building without musl
if (!use_musl && (current_cpu == "arm64" || current_cpu == "arm")) {
deps += [ "//arkcompiler/runtime_core/tests/fuzztest:runtime_core_fuzz_rt" ]
}
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":JsonObjectCtor2FuzzTest",
]
}
###############################################################################

View File

@ -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

View File

@ -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 "jsonobjectctor2_fuzzer.h"
#include <sstream>
#include "utils/json_parser.h"
namespace OHOS {
void JsonObjectCtor2FuzzTest(const uint8_t *data, size_t size)
{
std::stringbuf str_buf(std::string(data, data + size), std::ios::in);
panda::JsonObject json_object(&str_buf);
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::JsonObjectCtor2FuzzTest(data, size);
return 0;
}

View File

@ -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 TESTS_FUZZTEST_JSONOBJECTCTOR2_FUZZER_JSONOBJECTCTOR2_FUZZER_H
#define TESTS_FUZZTEST_JSONOBJECTCTOR2_FUZZER_JSONOBJECTCTOR2_FUZZER_H
#define FUZZ_PROJECT_NAME "jsonobjectctor2_fuzzer"
#endif // TESTS_FUZZTEST_JSONOBJECTCTOR2_FUZZER_JSONOBJECTCTOR2_FUZZER_H

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -0,0 +1,49 @@
# 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 = "arkcompiler/runtime_core"
##############################fuzztest##########################################
ohos_fuzztest("OpenArchiveFuzzTest") {
module_out_path = module_output_path
include_dirs = []
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "openarchive_fuzzer.cpp" ]
deps = [ "//arkcompiler/runtime_core/libziparchive:libarkziparchive_static" ]
# Temporary adaptation for fuzztest building without musl
if (!use_musl && (current_cpu == "arm64" || current_cpu == "arm")) {
deps += [ "//arkcompiler/runtime_core/tests/fuzztest:runtime_core_fuzz_rt" ]
}
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":OpenArchiveFuzzTest",
]
}
###############################################################################

View File

@ -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

View File

@ -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 "openarchive_fuzzer.h"
#include "libziparchive/zip_archive.h"
namespace OHOS {
void OpenArchiveFuzzTest(const uint8_t *data, size_t size)
{
// Create zip file
const char *zip_filename = "__OpenArchiveFuzzTest.zip";
const char *filename = "__OpenArchiveFuzzTest_tmp.data";
int ret = panda::CreateOrAddFileIntoZip(zip_filename, filename, data, size, APPEND_STATUS_CREATE, Z_NO_COMPRESSION);
if (ret != 0) {
(void)remove(zip_filename);
return;
}
panda::ZipArchiveHandle zipfile = nullptr;
if (panda::OpenArchive(zipfile, zip_filename) != 0) {
(void)remove(zip_filename);
return;
}
panda::CloseArchive(zipfile);
(void)remove(zip_filename);
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::OpenArchiveFuzzTest(data, size);
return 0;
}

View File

@ -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 TESTS_FUZZTEST_OPENARCHIVE_FUZZER_OPENARCHIVE_FUZZER_H
#define TESTS_FUZZTEST_OPENARCHIVE_FUZZER_OPENARCHIVE_FUZZER_H
#define FUZZ_PROJECT_NAME "openarchive_fuzzer"
#endif

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -0,0 +1,49 @@
# 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 = "arkcompiler/runtime_core"
##############################fuzztest##########################################
ohos_fuzztest("ReadOsFileFuzzTest") {
module_out_path = module_output_path
include_dirs = []
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "readosfile_fuzzer.cpp" ]
deps = [ "//arkcompiler/runtime_core/libpandabase:libarkbase_static" ]
# Temporary adaptation for fuzztest building without musl
if (!use_musl && (current_cpu == "arm64" || current_cpu == "arm")) {
deps += [ "//arkcompiler/runtime_core/tests/fuzztest:runtime_core_fuzz_rt" ]
}
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":ReadOsFileFuzzTest",
]
}
###############################################################################

View File

@ -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

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -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 "readosfile_fuzzer.h"
#include "platforms/unix/libpandabase/native_stack.h"
namespace OHOS {
void ReadOsFileFuzzTest(const uint8_t *data, size_t size)
{
#if defined(PANDA_TARGET_UNIX)
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);
std::string result;
panda::os::unix::native_stack::ReadOsFile(filename, &result);
// Remove the temp file
(void)remove(filename);
#endif
// TODO(huangyu): add test for windows
}
} // namespace OHOS
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
/* Run your code on data */
OHOS::ReadOsFileFuzzTest(data, size);
return 0;
}

View File

@ -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 TESTS_FUZZTEST_READOSFILE_FUZZER_READOSFILE_FUZZER_H
#define TESTS_FUZZTEST_READOSFILE_FUZZER_READOSFILE_FUZZER_H
#define FUZZ_PROJECT_NAME "readosfile_fuzzer"
#endif // TESTS_FUZZTEST_READOSFILE_FUZZER_READOSFILE_FUZZER_H

View File

@ -0,0 +1,49 @@
# 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 = "arkcompiler/runtime_core"
##############################fuzztest##########################################
ohos_fuzztest("WriteByteFuzzTest") {
module_out_path = module_output_path
include_dirs = []
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "writebyte_fuzzer.cpp" ]
deps = [ "//arkcompiler/runtime_core/libpandafile:libarkfile_static" ]
# Temporary adaptation for fuzztest building without musl
if (!use_musl && (current_cpu == "arm64" || current_cpu == "arm")) {
deps += [ "//arkcompiler/runtime_core/tests/fuzztest:runtime_core_fuzz_rt" ]
}
}
###############################################################################
group("fuzztest") {
testonly = true
deps = []
deps += [
# deps file
":WriteByteFuzzTest",
]
}
###############################################################################

View File

@ -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

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>1000</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>300</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>4096</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -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 "writebyte_fuzzer.h"
#include "file_writer.h"
namespace OHOS {
void WriteByteFuzzTest(const uint8_t *data, size_t size)
{
const char *filename = "__WriteByteFuzzTest_data.tmp";
{
auto writer = panda::panda_file::FileWriter(filename);
if (!writer) {
return;
}
for (size_t i = 0; i < size; ++i) {
writer.WriteByte(data[i]);
}
}
(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::WriteByteFuzzTest(data, size);
return 0;
}

View File

@ -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 TESTS_FUZZTEST_WRITEBYTE_FUZZER_WRITEBYTE_FUZZER_H
#define TESTS_FUZZTEST_WRITEBYTE_FUZZER_WRITEBYTE_FUZZER_H
#define FUZZ_PROJECT_NAME "writebyte_fuzzer"
#endif // TESTS_FUZZTEST_WRITEBYTE_FUZZER_WRITEBYTE_FUZZER_H