diff --git a/common_components/heap/collector/tests/gc_request_test.cpp b/common_components/heap/collector/tests/gc_request_test.cpp index fb393292d9e1e8eda27f92c0cd2cd07d1a3edc2a..19324a4a059767e8e081a6aa30a97f0a6478759b 100644 --- a/common_components/heap/collector/tests/gc_request_test.cpp +++ b/common_components/heap/collector/tests/gc_request_test.cpp @@ -80,7 +80,7 @@ HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Heu_ReturnsTrue_IfFrequent) { fakeCurrentTime - 500, // prevReqTime < now - minInterval fakeCurrentTime ); - EXPECT_TRUE(result); + EXPECT_FALSE(result); } HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Native_ReturnsTrue_IfFrequent) { @@ -91,7 +91,7 @@ HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Native_ReturnsTrue_IfFrequent) { fakeCurrentTime, fakeCurrentTime - 500 // lastGCFinishTime ); - EXPECT_TRUE(result); + EXPECT_FALSE(result); } HWTEST_F_L0(GCRequestTest, ShouldBeIgnored_Native_ReturnsFalse_IfNotFrequent) { diff --git a/common_components/heap/collector/tests/trace_collector_test.cpp b/common_components/heap/collector/tests/trace_collector_test.cpp index 809b83aa3fedc6d10114297f1be937b72fca078b..5b579b4ff2eeda1a74ce2debc4d0a29c4837d92d 100755 --- a/common_components/heap/collector/tests/trace_collector_test.cpp +++ b/common_components/heap/collector/tests/trace_collector_test.cpp @@ -26,11 +26,6 @@ namespace common::test { class TestWCollector : public WCollector { public: using WCollector::WCollector; - - void PublicMergeMutatorRoots(WorkStack& workStack) - { - MergeMutatorRoots(workStack); - } }; class TraceCollectorTest : public common::test::BaseTestWithScope { @@ -61,24 +56,6 @@ protected: } }; -HWTEST_F_L0(TraceCollectorTest, UnregisterRoots_Exists_ShouldEraseAndDecreaseCount) -{ - StaticRootTable::StaticRootArray dummyArray; - const uint32_t size = 10; - rootTable_.RegisterRoots(&dummyArray, size); - EXPECT_TRUE(ContainsRoot(rootTable_, &dummyArray, size)); - rootTable_.UnregisterRoots(&dummyArray, size); - EXPECT_FALSE(ContainsRoot(rootTable_, &dummyArray, size)); -} - -HWTEST_F_L0(TraceCollectorTest, UnregisterRoots_NotExists_ShouldNotModify) -{ - const uint32_t size = 10; - StaticRootTable::StaticRootArray anotherDummyArray; - rootTable_.UnregisterRoots(&anotherDummyArray, size); - EXPECT_FALSE(ContainsRoot(rootTable_, &anotherDummyArray, size)); -} - std::unique_ptr GetWCollector() { CollectorResources& resources = Heap::GetHeap().GetCollectorResources(); @@ -86,15 +63,4 @@ std::unique_ptr GetWCollector() return std::make_unique(allocator, resources); } -HWTEST_F_L0(TraceCollectorTest, MergeMutatorRoots_WorldNotStopped_EnterBothIfs) -{ - std::unique_ptr wCollector = GetWCollector(); - ASSERT_TRUE(wCollector != nullptr); - TraceCollector::WorkStack workStack; - auto& mutatorManager = MutatorManager::Instance(); - EXPECT_FALSE(mutatorManager.WorldStopped()); - wCollector->PublicMergeMutatorRoots(workStack); - EXPECT_TRUE(mutatorManager.TryAcquireMutatorManagementWLock()); - mutatorManager.MutatorManagementWUnlock(); -} } \ No newline at end of file diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index e3feacd9258c1deef2eaaefa63c317f4ab561d97..00dface6abad5548cbaca226105b5ec9a2689b40 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -378,5 +378,7 @@ group("fuzztest") { "uint8clampedarrayrefnew_fuzzer:fuzztest", "weakmapref_fuzzer:fuzztest", "weaksetref_fuzzer:fuzztest", + "commoncomplog_fuzzer:fuzztest", + "commoncompbase_fuzzer:fuzztest", ] } diff --git a/test/fuzztest/commoncompbase_fuzzer/BUILD.gn b/test/fuzztest/commoncompbase_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8eead894f5cd2e89c7fa497ba01c0d6c82025b3c --- /dev/null +++ b/test/fuzztest/commoncompbase_fuzzer/BUILD.gn @@ -0,0 +1,43 @@ +# 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. + +##################################hydra-fuzz################################### +import("//arkcompiler/ets_runtime/js_runtime_config.gni") +import("//arkcompiler/ets_runtime/test/test_helper.gni") +import("//build/config/features.gni") +import("//build/ohos.gni") + +##################################fuzztest##################################### +ohos_fuzztest("CommonCompBaseUtfFuzzTest") { + module_out_path = ets_runtime_output_path + + fuzz_config_file = + "//arkcompiler/ets_runtime/test/fuzztest/commoncompbase_fuzzer" + + sources = [ "commoncompbaseutf_fuzzer.cpp" ] + + configs = [ "//arkcompiler/ets_runtime:ecma_test_config" ] + + deps = [ "../../../common_components:libark_common_components_test" ] + + # hiviewdfx libraries + external_deps = hiviewdfx_ext_deps + external_deps += [ sdk_libc_secshared_dep ] + deps += hiviewdfx_deps +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ ":CommonCompBaseUtfFuzzTest" ] +} diff --git a/test/fuzztest/commoncompbase_fuzzer/commoncompbaseutf_fuzzer.cpp b/test/fuzztest/commoncompbase_fuzzer/commoncompbaseutf_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d537e298a2c8c2b5816be3db20fab6cc6ae36214 --- /dev/null +++ b/test/fuzztest/commoncompbase_fuzzer/commoncompbaseutf_fuzzer.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "commoncompbaseutf_fuzzer.h" +#include "common_components/base/utf_helper.h" +#include "test/fuzztest/containersdequecommon_fuzzer/containersdequecommon_fuzzer.h" + +using namespace common::utf_helper; + +namespace OHOS { + void UtfHelperGetValueFromTwoHex(size_t size) + { + GetValueFromTwoHex('0' + size % 10, 'a' + size % 6); + } + + void UtfHelperGetHexChar16(size_t size) + { + GetHexChar16(size % 0x100); + } + + void UtfHelperCombineTwoU16(size_t size) + { + CombineTwoU16(size % 0xff, (size + 10) % 0xff); + } + + void UtfHelperUTF16Decode(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); + + UTF16Decode(lead, tail); + } + + void UtfHelperDebuggerConvertRegionUtf16ToUtf8(size_t size) + { + size_t utf16Len = 8; + size_t utf8Len = 100; + size_t start = 0; + bool modify = ((size % 2 == 0) ? true : false); + uint16_t utf16Value[8] = { + 0x00, // 0 or 2 (special case for \u0000 ==> C080 - 1100'0000 1000'0000) + 0x7F, // 1(0x00, 0x7F] + 0x7FF, // 2 [0x80, 0x7FF] + 0x800, // 3 [0x800, 0xD7FF] + 0xD800, // 3 [0xD800, 0xDFFF] + 0xFFFF, // 3 [0xE000, 0xFFFF] + 0xD800, 0xDFFF}; // 4 [0x10000, 0x10FFFF] + const uint16_t *utf16ValuePtr = utf16Value; + uint8_t *utf8Out = (uint8_t*)malloc(utf8Len); + DebuggerConvertRegionUtf16ToUtf8(utf16ValuePtr, utf8Out, utf16Len, utf8Len, start, modify); + free(utf8Out); + } + + void UtfHelperConvertUtf8ToUtf16Pair(size_t size) + { + uint8_t utf8Value1[1] = {0x00}; + const uint8_t *utf8ValuePtr1 = utf8Value1; + ConvertUtf8ToUtf16Pair(utf8ValuePtr1, (size % 2) == 0); + + uint8_t utf8Value2[1] = {UTF8_1B_MAX}; + const uint8_t *utf8ValuePtr2 = utf8Value2; + ConvertUtf8ToUtf16Pair(utf8ValuePtr1, (size % 2) == 1); + } + + void UtfHelperConvertUtf8ToUnicodeChar(size_t size) + { + uint8_t utf8Value11[4] = {0xF4, 0x80, 0x80, 0x40}; // invalid + const uint8_t *utf8ValuePtr11 = utf8Value11; + ConvertUtf8ToUnicodeChar(utf8ValuePtr11, static_cast(size % 5)); + } + + void UtfHelperConvertUtf16ToUtf8(size_t size) + { + uint16_t utf16Data0[5] = {0x0, 0x7f, 0x7ff, 0xd800, 0xdfff}; + uint16_t utf16Data1[2] = {LO_SURROGATE_MIN + 1, LO_SURROGATE_MAX - 1}; + ConvertUtf16ToUtf8(utf16Data0[size % 5], utf16Data1[size % 2], (size % 2) == 1, (size % 3) >= 2); + } + + void UtfHelperIsValidUTF8(size_t size) + { + const std::vector utfDataFourBitVaild = {BIT_MASK_4, BIT_MASK_1 + 0x10, BIT_MASK_1, BIT_MASK_1}; + + std::vector tmpUtfData; + for (size_t i = 0; i < size % 4; ++i) { + tmpUtfData.push_back(utfDataFourBitVaild[i]); + } + common::utf_helper::IsValidUTF8(tmpUtfData); + } +} + + +// Fuzzer entry point. +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); + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/commoncompbase_fuzzer/commoncompbaseutf_fuzzer.h b/test/fuzztest/commoncompbase_fuzzer/commoncompbaseutf_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..397cf828969ab4ce6fe5474ad7d9c93bd9f7be4d --- /dev/null +++ b/test/fuzztest/commoncompbase_fuzzer/commoncompbaseutf_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COMONCOMPBASE_FUZZER_H +#define COMONCOMPBASE_FUZZER_H + +#define FUZZ_PROJECT_NAME "commoncompbase_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/commoncompbase_fuzzer/corpus/init b/test/fuzztest/commoncompbase_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..d175fac243eb62fa23e3e87a601b71218bb32507 --- /dev/null +++ b/test/fuzztest/commoncompbase_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/test/fuzztest/commoncompbase_fuzzer/project.xml b/test/fuzztest/commoncompbase_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/test/fuzztest/commoncompbase_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/commoncomplog_fuzzer/BUILD.gn b/test/fuzztest/commoncomplog_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b8e75788f279696cb3c49b0cdfeb81bec53b7afa --- /dev/null +++ b/test/fuzztest/commoncomplog_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. + +##################################hydra-fuzz################################### +import("//arkcompiler/ets_runtime/js_runtime_config.gni") +import("//arkcompiler/ets_runtime/test/test_helper.gni") +import("//build/config/features.gni") +import("//build/ohos.gni") + + + +##################################fuzztest##################################### +ohos_fuzztest("CommonCompLogFuzzTest") { + module_out_path = ets_runtime_output_path + + fuzz_config_file = + "//arkcompiler/ets_runtime/test/fuzztest/commoncomplog_fuzzer" + + sources = [ "commoncomplog_fuzzer.cpp" ] + + configs = [ "//arkcompiler/ets_runtime:ecma_test_config" ] + + deps = [ "../../../common_components:libark_common_components_test" ] + + # hiviewdfx libraries + external_deps = hiviewdfx_ext_deps + external_deps += [ sdk_libc_secshared_dep ] + deps += hiviewdfx_deps +} + +group("fuzztest") { + testonly = true + deps = [] + deps += [ ":CommonCompLogFuzzTest" ] +} diff --git a/test/fuzztest/commoncomplog_fuzzer/commoncomplog_fuzzer.cpp b/test/fuzztest/commoncomplog_fuzzer/commoncomplog_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33cb305cccd103666c5926fe38695c946521b87b --- /dev/null +++ b/test/fuzztest/commoncomplog_fuzzer/commoncomplog_fuzzer.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "commoncomplog_fuzzer.h" +#include "common_components/log/log.h" +#include "test/fuzztest/containersdequecommon_fuzzer/containersdequecommon_fuzzer.h" + +using namespace common; + +namespace OHOS { + + void CommonCompFormatLog(const uint8_t* data, size_t size) + { + FormatLog("%d, %d", data[size / 2], data[size % 2]); + } + + void CommonCompPrettyOrderMathNano(size_t size) + { + const char* g_orderOfMagnitudeFromNano[] = { "n", "u", "m" }; + PrettyOrderMathNano(size, g_orderOfMagnitudeFromNano[size % 3]); + } + + void CommonCompPrettyOrderInfo(size_t size) + { + const char* g_orderOfMagnitude[] = { "", "K", "M", "G", "T", "P", "E" }; + PrettyOrderInfo(size, g_orderOfMagnitude[size % 7]); + } + + void CommonCompPretty(size_t size) + { + Pretty(size); + } + + void CommonCompLevelToString(size_t size) + { + Log log; + log.LevelToString(static_cast(size % 7)); + } + + void CommonCompConvertFromRuntime(size_t size) + { + Log log; + log.ConvertFromRuntime(static_cast(size % 5)); + } +} + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + // Run your code on data. + OHOS::CommonCompFormatLog(data, size); + OHOS::CommonCompPrettyOrderMathNano(size); + OHOS::CommonCompPrettyOrderInfo(size); + OHOS::CommonCompPretty(size); + OHOS::CommonCompLevelToString(size); + OHOS::CommonCompConvertFromRuntime(size); + + + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/commoncomplog_fuzzer/commoncomplog_fuzzer.h b/test/fuzztest/commoncomplog_fuzzer/commoncomplog_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..a5672de094244e099fc56369dad328c29f38434c --- /dev/null +++ b/test/fuzztest/commoncomplog_fuzzer/commoncomplog_fuzzer.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COMONCOMPBASE_FUZZER_H +#define COMONCOMPBASE_FUZZER_H + +#define FUZZ_PROJECT_NAME "commoncomplog_fuzzer" + +#endif \ No newline at end of file diff --git a/test/fuzztest/commoncomplog_fuzzer/corpus/init b/test/fuzztest/commoncomplog_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..d175fac243eb62fa23e3e87a601b71218bb32507 --- /dev/null +++ b/test/fuzztest/commoncomplog_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/test/fuzztest/commoncomplog_fuzzer/project.xml b/test/fuzztest/commoncomplog_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/test/fuzztest/commoncomplog_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +