From 8e2c19a2961b214a519815a8beffdcc0078636a6 Mon Sep 17 00:00:00 2001 From: lhc Date: Wed, 13 Aug 2025 09:44:27 +0800 Subject: [PATCH] Modify the alarm of fuzz Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/ICSNVT?from=project-issue Signed-off-by: lhc Change-Id: I27132775007aeb2ec943a2e4be283e5141a68ddf --- common_components/BUILD.gn | 75 +++++++++++++++ .../commoncompbaseutf_fuzzer/BUILD.gn | 4 +- .../commoncompbaseutf_fuzzer.cpp | 93 ++++++++++++------- .../commoncompbaseutf_fuzzer/project.xml | 2 +- test/fuzztest/commoncomplog_fuzzer/BUILD.gn | 4 +- .../commoncomplog_fuzzer.cpp | 45 ++++++--- .../fuzztest/commoncomplog_fuzzer/corpus/init | 2 +- .../fuzztest/commoncomplog_fuzzer/project.xml | 2 +- test/resource/js_runtime/ohos_test.xml | 10 ++ 9 files changed, 182 insertions(+), 55 deletions(-) diff --git a/common_components/BUILD.gn b/common_components/BUILD.gn index c0f867d4c4..c7c3dba5e4 100755 --- a/common_components/BUILD.gn +++ b/common_components/BUILD.gn @@ -445,3 +445,78 @@ ohos_shared_library("libark_common_components_test") { part_name = "ets_runtime" subsystem_name = "arkcompiler" } + +ohos_shared_library("libark_common_components_fuzz_test") { + testonly = true + stack_protector_ret = false + + configs = [ ":common_components_test_config" ] + + sources = [ + "log/log.cpp", + "base/utf_helper.cpp", + ] + + if (is_mingw) { + sources += [ + "platform/windows/cpu.cpp", + "platform/windows/os.cpp", + ] + } else if (is_mac) { + sources += [ + "platform/unix/mac/cpu.cpp", + "platform/unix/mac/os.cpp", + ] + } else if (is_ohos || target_os == "android") { + sources += [ + "platform/unix/linux/cpu.cpp", + "platform/unix/linux/os.cpp", + ] + } else if (is_linux) { + sources += [ + "platform/unix/linux/cpu.cpp", + "platform/unix/linux/os.cpp", + ] + } + + # deps = [ ":libarkcommon-runtime" ] + + public_configs = [ ":common_components_public_config" ] + public_configs += [ "//arkcompiler/ets_runtime:include_llvm" ] + + external_deps = [] + if (!ark_standalone_build) { + public_external_deps = [ + "zlib:libz", + ] + public_external_deps += hiviewdfx_ext_deps + } else { + external_deps += [ + "zlib:libz", + ] + external_deps += hiviewdfx_ext_deps + } + + ldflags = [] + if (enable_coverage) { + ldflags += [ "--coverage" ] + cflags_cc = [ "--coverage" ] + } + if (!ark_standalone_build) { + ldflags += [ "-Wl,--lto-O0" ] + } + + install_enable = false + if (!is_mingw && !is_mac) { + output_extension = "so" + } + + if (!is_arkui_x) { + external_deps += [ "runtime_core:libarkfile_runtime_static" ] + } else { + deps += [ "$ark_root/libpandafile:libarkfile_runtime_static" ] + } + + part_name = "ets_runtime" + subsystem_name = "arkcompiler" +} diff --git a/test/fuzztest/commoncompbaseutf_fuzzer/BUILD.gn b/test/fuzztest/commoncompbaseutf_fuzzer/BUILD.gn index 7e7475182a..0a47fdd76c 100644 --- a/test/fuzztest/commoncompbaseutf_fuzzer/BUILD.gn +++ b/test/fuzztest/commoncompbaseutf_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -28,7 +28,7 @@ ohos_fuzztest("CommonCompBaseUtfFuzzTest") { configs = [ "//arkcompiler/ets_runtime:ecma_test_config" ] - deps = [ "../../../common_components:libark_common_components_test" ] + deps = [ "../../../common_components:libark_common_components_fuzz_test" ] # hiviewdfx libraries external_deps = hiviewdfx_ext_deps diff --git a/test/fuzztest/commoncompbaseutf_fuzzer/commoncompbaseutf_fuzzer.cpp b/test/fuzztest/commoncompbaseutf_fuzzer/commoncompbaseutf_fuzzer.cpp index ccd8d6941d..b379f00d41 100644 --- a/test/fuzztest/commoncompbaseutf_fuzzer/commoncompbaseutf_fuzzer.cpp +++ b/test/fuzztest/commoncompbaseutf_fuzzer/commoncompbaseutf_fuzzer.cpp @@ -20,36 +20,51 @@ using namespace common::utf_helper; namespace OHOS { - void UtfHelperGetValueFromTwoHex(size_t size) + void UtfHelperGetValueFromTwoHex(const uint8_t* data, size_t size) { - GetValueFromTwoHex('0' + size % 10, // 10: val in [0x0, 0x9], convert to ['0', '9'] - 'a' + size % 6); // 6:val in ['a', 'f'], convert to ['a', 'f'] + if (size == 0) { + return; + } + GetValueFromTwoHex('0' + data[size - 1] % 10, // 10: val in [0x0, 0x9], convert to ['0', '9'] + 'a' + data[size - 1] % 6); // 6:val in ['a', 'f'], convert to ['a', 'f'] } - void UtfHelperGetHexChar16(size_t size) + void UtfHelperGetHexChar16(const uint8_t* data, size_t size) { - GetHexChar16(size % 0xff); // 0xff: Maximum value of uint8 + if (size == 0) { + return; + } + GetHexChar16(data[size - 1] % 0xff); // 0xff: Maximum value of uint8 } - void UtfHelperCombineTwoU16(size_t size) + void UtfHelperCombineTwoU16(const uint8_t* data, size_t size) { - CombineTwoU16(size % 0xff, (size / 2) % 0xff); // 2:half of the size,0xff: Maximum value of uint8 + if (size == 0) { + return; + } + CombineTwoU16(data[size - 1] % 0xff, (size / 2) % 0xff); // 2:half of the size,0xff: Maximum value of uint8 } - void UtfHelperUTF16Decode(size_t size) + void UtfHelperUTF16Decode(const uint8_t* data, size_t size) { - uint16_t lead = DECODE_LEAD_LOW + size % (DECODE_LEAD_HIGH - DECODE_LEAD_LOW); - uint16_t tail = DECODE_TRAIL_LOW + size % (DECODE_TRAIL_HIGH - DECODE_TRAIL_LOW); + if (size == 0) { + return; + } + uint16_t lead = DECODE_LEAD_LOW + data[size - 1] % (DECODE_LEAD_HIGH - DECODE_LEAD_LOW); + uint16_t tail = DECODE_TRAIL_LOW + data[size - 1] % (DECODE_TRAIL_HIGH - DECODE_TRAIL_LOW); UTF16Decode(lead, tail); } - void UtfHelperDebuggerConvertRegionUtf16ToUtf8(size_t size) + void UtfHelperDebuggerConvertRegionUtf16ToUtf8(const uint8_t* data, size_t size) { + if (size == 0) { + return; + } size_t utf16Len = 8; // 8:length of u16 size_t utf8Len = 100; // 100:buff len size_t start = 0; - bool modify = ((size % 2 == 0) ? true : false); // 2:Check size is an even number + bool modify = ((data[size - 1] % 2 == 0) ? true : false); // 2:Check size is an even number uint16_t utf16Value[8] = { 0x00, // 0 or 2 (special case for \u0000 ==> C080 - 1100'0000 1000'0000) 0x7F, // 1(0x00, 0x7F] @@ -64,39 +79,51 @@ namespace OHOS { free(utf8Out); } - void UtfHelperConvertUtf8ToUtf16Pair(size_t size) + void UtfHelperConvertUtf8ToUtf16Pair(const uint8_t* data, size_t size) { + if (size == 0) { + return; + } uint8_t utf8Value1[1] = {0x0}; const uint8_t *utf8ValuePtr1 = utf8Value1; - ConvertUtf8ToUtf16Pair(utf8ValuePtr1, (size % 2) == 0); // 2:Check size is even number + ConvertUtf8ToUtf16Pair(utf8ValuePtr1, (data[size - 1] % 2) == 0); // 2:Check size is even number uint8_t utf8Value2[1] = {UTF8_1B_MAX}; // 1: array len const uint8_t *utf8ValuePtr2 = utf8Value2; - ConvertUtf8ToUtf16Pair(utf8ValuePtr1, (size % 2) == 1); // 2:Check size is even number + ConvertUtf8ToUtf16Pair(utf8ValuePtr1, (data[size - 1] % 2) == 1); // 2:Check size is even number } - void UtfHelperConvertUtf8ToUnicodeChar(size_t size) + void UtfHelperConvertUtf8ToUnicodeChar(const uint8_t* data, size_t size) { + if (size == 0) { + return; + } uint8_t utf8Value11[4] = {0xF4, 0x80, 0x80, 0x40}; // invalid const uint8_t *utf8ValuePtr11 = utf8Value11; - ConvertUtf8ToUnicodeChar(utf8ValuePtr11, static_cast(size % 4)); // 4: lenth of array + ConvertUtf8ToUnicodeChar(utf8ValuePtr11, static_cast(data[size - 1] % 4)); // 4: lenth of array } - void UtfHelperConvertUtf16ToUtf8(size_t size) + void UtfHelperConvertUtf16ToUtf8(const uint8_t* data, size_t size) { + if (size == 0) { + return; + } uint16_t utf16Data0[5] = {0x0, 0x7f, 0x7ff, 0xd800, 0xdfff}; // invalid uint16_t utf16Data1[2] = {LO_SURROGATE_MIN + 1, LO_SURROGATE_MAX - 1}; - ConvertUtf16ToUtf8(utf16Data0[size % 5], // 5:length of utf16Data0 - utf16Data1[size % 2], (size % 2) == 1, // 2:Check size is even number - (size % 3) >= 2); // 3:Get Remainder + ConvertUtf16ToUtf8(utf16Data0[data[size - 1] % 5], // 5:length of utf16Data0 + utf16Data1[data[size - 1] % 2], (data[size - 1] % 2) == 1, // 2:Check size is even number + (data[size - 1] % 3) >= 2); // 3:Get Remainder } - void UtfHelperIsValidUTF8(size_t size) + void UtfHelperIsValidUTF8(const uint8_t* data, size_t size) { + if (size == 0) { + return; + } const std::vector utfDataFourBitVaild = {BIT_MASK_4, BIT_MASK_1 + 0x10, BIT_MASK_1, BIT_MASK_1}; std::vector tmpUtfData; - size_t vecLen = (size % 4 == 0 ? 1 : size % 4); // 4:number of utfDataFourBitVaild array + size_t vecLen = (data[size - 1] % 4 == 0 ? 1 : data[size - 1] % 4); // 4:number of utfDataFourBitVaild array for (size_t i = 0; i < vecLen; ++i) { tmpUtfData.push_back(utfDataFourBitVaild[i]); } @@ -109,16 +136,16 @@ namespace OHOS { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { // Run your code on data. - OHOS::UtfHelperGetValueFromTwoHex(size); - OHOS::UtfHelperGetHexChar16(size); - - OHOS::UtfHelperCombineTwoU16(size); - OHOS::UtfHelperUTF16Decode(size); - OHOS::UtfHelperDebuggerConvertRegionUtf16ToUtf8(size); - OHOS::UtfHelperConvertUtf8ToUtf16Pair(size); - OHOS::UtfHelperConvertUtf8ToUnicodeChar(size); - OHOS::UtfHelperConvertUtf16ToUtf8(size); - OHOS::UtfHelperIsValidUTF8(size); + OHOS::UtfHelperGetValueFromTwoHex(data, size); + OHOS::UtfHelperGetHexChar16(data, size); + + OHOS::UtfHelperCombineTwoU16(data, size); + OHOS::UtfHelperUTF16Decode(data, size); + OHOS::UtfHelperDebuggerConvertRegionUtf16ToUtf8(data, size); + OHOS::UtfHelperConvertUtf8ToUtf16Pair(data, size); + OHOS::UtfHelperConvertUtf8ToUnicodeChar(data, size); + OHOS::UtfHelperConvertUtf16ToUtf8(data, size); + OHOS::UtfHelperIsValidUTF8(data, size); return 0; } \ No newline at end of file diff --git a/test/fuzztest/commoncompbaseutf_fuzzer/project.xml b/test/fuzztest/commoncompbaseutf_fuzzer/project.xml index 6e8ad2cfde..66e1dcac47 100644 --- a/test/fuzztest/commoncompbaseutf_fuzzer/project.xml +++ b/test/fuzztest/commoncompbaseutf_fuzzer/project.xml @@ -1,5 +1,5 @@ -