From c1ea224523ad79201349b17ae3f6e3e7184767b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=91=E5=B1=91=E5=B1=91?= Date: Mon, 11 Aug 2025 20:32:47 +0800 Subject: [PATCH] sync code and add staticcheck MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 屑屑屑 --- .../innerkitsimpl/common/src/pixel_map.cpp | 2 +- .../imageinterfacepixelmap_fuzzer/BUILD.gn | 18 +- .../src/image_interface_pixelmap_fuzzer.cpp | 177 ------------------ .../imagepixelmapBase_fuzzer/BUILD.gn | 9 +- .../src/image_pixelmap_fuzzer.cpp | 95 ++++++++-- 5 files changed, 98 insertions(+), 203 deletions(-) diff --git a/frameworks/innerkitsimpl/common/src/pixel_map.cpp b/frameworks/innerkitsimpl/common/src/pixel_map.cpp index 8fb43e94b..f50f71cb3 100644 --- a/frameworks/innerkitsimpl/common/src/pixel_map.cpp +++ b/frameworks/innerkitsimpl/common/src/pixel_map.cpp @@ -531,7 +531,7 @@ void PixelMap::UpdatePixelsAlphaType() return; } - uint8_t pixelBytes = GetPixelBytes(); + int32_t pixelBytes = GetPixelBytes(); int32_t stride = GetRowStride(); int32_t rowBytes = GetRowBytes(); diff --git a/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/BUILD.gn b/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/BUILD.gn index 758735d40..d8dd57dad 100644 --- a/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/BUILD.gn +++ b/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/BUILD.gn @@ -14,21 +14,25 @@ import("//build/test.gni") import("//foundation/multimedia/image_framework/ide/image_decode_config.gni") -config("image_external_config") { +ohos_fuzztest("ImageInterfacePixelmapFuzzTest") { + module_out_path = "multimedia_image/image_framework" + fuzz_config_file = "$image_subsystem/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer" + include_dirs = [ "./include", "$image_subsystem/interfaces/innerkits/include", ] -} - -ohos_fuzztest("ImageInterfacePixelmapFuzzTest") { - public_configs = [ ":image_external_config" ] sources = [ "src/image_interface_pixelmap_fuzzer.cpp" ] + + cflags =[ + "-DIMAGE_DEBUG_FLAG", + "-DIMAGE_COLORSPACE_FLAG", + ] + deps = [ "$image_subsystem/interfaces/innerkits:image_native" ] external_deps = [ "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core", ] - fuzz_config_file = "$image_subsystem/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer" - module_out_path = "multimedia_image/image_framework" } diff --git a/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/src/image_interface_pixelmap_fuzzer.cpp b/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/src/image_interface_pixelmap_fuzzer.cpp index 0567131c7..8cb62507c 100644 --- a/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/src/image_interface_pixelmap_fuzzer.cpp +++ b/frameworks/innerkitsimpl/test/fuzztest/imageinterfacepixelmap_fuzzer/src/image_interface_pixelmap_fuzzer.cpp @@ -31,9 +31,6 @@ constexpr uint32_t MAX_LENGTH_MODULO = 1024; constexpr uint32_t PIXELFORMAT_MODULO = 8; constexpr uint32_t ALPHATYPE_MODULO = 4; constexpr uint32_t SCALEMODE_MODULO = 2; -constexpr uint32_t DMA_WIDTH = 1024; -constexpr uint32_t DMA_HEIGHT = 512; -constexpr uint32_t BOOLMODULE = 2; namespace OHOS { namespace Media { @@ -108,12 +105,6 @@ std::unique_ptr GetPixelMapFromOpts(const Media::Initialization return pixelmap; } -bool IsYuv(Media::PixelFormat format) -{ - return format == Media::PixelFormat::NV12 || format == Media::PixelFormat::NV21 || - format == Media::PixelFormat::YCBCR_P010 || format == Media::PixelFormat::YCRCB_P010; -} - void FuzzTestSetMemoryName() { Media::InitializationOptions opts = GetInitialRandomOpts(); @@ -272,17 +263,6 @@ void FuzzTestReadARGBPixels() } } -void FuzzTestAboutMap() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (opts.pixelFormat == PixelFormat::ALPHA_8 || !pixelmap) { - return; - } - pixelmap->UnMap(); - pixelmap->ReMap(); -} - static Rect GetRandomRect(int32_t width = 0, int32_t height = 0) { Rect rect; @@ -293,26 +273,6 @@ static Rect GetRandomRect(int32_t width = 0, int32_t height = 0) return rect; } -void FuzzTestReadWith5Args(bool isEditable) -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - if (opts.pixelFormat == PixelFormat::ALPHA_8) { - return; - } - opts.editable = isEditable; - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (!pixelmap) { - return; - } - Rect region = GetRandomRect(opts.size.width, opts.size.height); - int32_t srcSize = pixelmap->GetByteCount(); - uint32_t offset = GetData() % (srcSize >> 2); - uint32_t regionStride = pixelmap->GetPixelBytes() * region.width; - uint64_t dstSize = regionStride * region.height; - std::unique_ptr regionBuffer = std::make_unique(dstSize); - pixelmap->ReadPixels(dstSize, offset, regionStride, region, regionBuffer.get()); -} - void FuzzTestReadSinglePixel() { Media::InitializationOptions opts = GetInitialRandomOpts(); @@ -329,121 +289,6 @@ void FuzzTestReadSinglePixel() } } -void FuzzTestPureRandomSetAlpha() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - if (opts.pixelFormat == PixelFormat::ALPHA_8) { - return; - } - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (!pixelmap) { - return; - } - float percent = static_cast((GetData()) % 10) / 10.0; - if (SUCCESS != pixelmap->SetAlpha(percent)) { - return; - } -} - -void FuzzTestRGBA_F16PixelMapSetAlpha() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - if (opts.pixelFormat == PixelFormat::ALPHA_8) { - return; - } - opts.pixelFormat = PixelFormat::RGBA_F16; - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (!pixelmap) { - return; - } - float percent = static_cast((GetData()) % 10) / 10.0; - if (SUCCESS != pixelmap->SetAlpha(percent)) { - return; - } -} - -void FuzzTestRGBA_1010102PixelMapSetAlpha() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - if (opts.pixelFormat == PixelFormat::ALPHA_8) { - return; - } - opts.pixelFormat = PixelFormat::RGBA_1010102; - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (!pixelmap) { - return; - } - float percent = static_cast((GetData()) % 10) / 10.0; - if (SUCCESS != pixelmap->SetAlpha(percent)) { - return; - } -} - -void FuzzTestWritePixelsWith5Args(bool isEditable) -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - if (opts.pixelFormat == PixelFormat::ALPHA_8) { - return; - } - opts.editable = isEditable; - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (!pixelmap) { - return; - } - Rect region = GetRandomRect(opts.size.width, opts.size.height); - int32_t srcSize = pixelmap->GetByteCount(); - uint32_t offset = GetData() % (srcSize >> 2); - uint32_t regionStride = pixelmap->GetPixelBytes() * region.width; - uint64_t dstSize = regionStride * region.height; - std::unique_ptr regionBuffer_with_write = std::make_unique(dstSize); - pixelmap->WritePixels(regionBuffer_with_write.get(), dstSize, offset, regionStride, region); -} - -void FuzzTestCrop() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - if (opts.pixelFormat == PixelFormat::ALPHA_8 || opts.pixelFormat == PixelFormat::RGB_888) { - return; - } - opts.useDMA = GetData() % BOOLMODULE; - if (opts.useDMA) { - opts.size.height += DMA_HEIGHT; - opts.size.width += DMA_HEIGHT; - } - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (!pixelmap) { - return; - } - Rect rect = GetRandomRect(opts.size.width, opts.size.height); - pixelmap->crop(rect); -} - -void FuzzTestSetFunc() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - std::unique_ptr pixelmap = GetPixelMapFromOpts(opts); - if (!pixelmap) { - return; - } - pixelmap->SetFreePixelMapProc([](void *addr, void *context, uint32_t size) { - return; - }); -} - -void FuzzTestClone() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - if (IsYuv(opts.pixelFormat)) { - return; - } - std::unique_ptr pixelMap = GetPixelMapFromOpts(opts); - if (pixelMap != nullptr && pixelMap->IsAstc()) { - return; - } - int32_t errorCode = 0; - pixelMap->Clone(errorCode); -} - void CropCreate() { Media::InitializationOptions opts = GetInitialRandomOpts(); @@ -460,22 +305,9 @@ void CropCreate() PixelMap::Create(*(pixelmap.get()), rect, opts); } -void DmaAndYuvPixelMapCreate() -{ - Media::InitializationOptions opts = GetInitialRandomOpts(); - opts.size.height = DMA_HEIGHT; - opts.size.width = DMA_WIDTH; - opts.useDMA = true; - opts.pixelFormat = PixelFormat::YCBCR_P010; - std::unique_ptr pixelmap_dma_yuv = GetPixelMapFromOpts(opts); - opts.pixelFormat = PixelFormat::NV12; - std::unique_ptr pixelmap_dma_yuv_nv12 = GetPixelMapFromOpts(opts); -} - void CreatePixelMapFuzzTest() { CropCreate(); - DmaAndYuvPixelMapCreate(); } void PixelMapInterfaceFuzzTest() @@ -489,15 +321,6 @@ void PixelMapInterfaceFuzzTest() FuzzTestReadPixelsAndWritePixels(); FuzzTestReadARGBPixels(); FuzzTestReadSinglePixel(); - FuzzTestAboutMap(); - FuzzTestReadWith5Args(GetData() % 2); // 2: true or false - FuzzTestPureRandomSetAlpha(); - FuzzTestRGBA_F16PixelMapSetAlpha(); - FuzzTestRGBA_1010102PixelMapSetAlpha(); - FuzzTestWritePixelsWith5Args(GetData() % 2); // 2: true or false - FuzzTestCrop(); - FuzzTestSetFunc(); - FuzzTestClone(); } bool PixelMapMainFuzzTest(const uint8_t* data, size_t size) diff --git a/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/BUILD.gn b/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/BUILD.gn index 940c9c6ab..7f248d17f 100644 --- a/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/BUILD.gn +++ b/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/BUILD.gn @@ -24,10 +24,17 @@ config("image_external_config") { ohos_fuzztest("ImagePixelmapBaseFuzzTest") { public_configs = [ ":image_external_config" ] sources = [ "src/image_pixelmap_fuzzer.cpp" ] - deps = [ "$image_subsystem/interfaces/innerkits:image_native" ] + deps = [ + "$image_subsystem/interfaces/innerkits:utils:image_utils", + "$image_subsystem/interfaces/innerkits:image_native" + ] external_deps = [ "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core", + ] + cflags = [ + "-Dprotected=public", ] fuzz_config_file = "$image_subsystem/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer" module_out_path = "multimedia_image/image_framework" diff --git a/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/src/image_pixelmap_fuzzer.cpp b/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/src/image_pixelmap_fuzzer.cpp index d78b287cc..b3f688e59 100644 --- a/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/src/image_pixelmap_fuzzer.cpp +++ b/frameworks/innerkitsimpl/test/fuzztest/imagepixelmapBase_fuzzer/src/image_pixelmap_fuzzer.cpp @@ -15,15 +15,17 @@ #include "image_pixelmap_fuzzer.h" +#include #include #include -#include - -#include "pixel_map.h" - -#include #include + #include "image_log.h" +#include "image_utils.h" +#include "media_errors.h" +#include "message_parcel.h" +#include "pixel_map.h" +#include "securec.h" constexpr uint32_t WIDTH_FACTOR = 4; constexpr uint32_t STRING_LENGTH = 10; @@ -82,7 +84,7 @@ std::string GetStringFromData() /* * get parcel from g_data */ -bool ChangeParcel(Parcel &parcel) +bool ChangeParcel(MessageParcel &parcel) { if (!parcel.WriteBuffer(g_data, g_size)) { return false; @@ -150,20 +152,57 @@ std::unique_ptr GetPixelMapFromData(Media::PixelFormat pixelFor * get a pixelmap from other Pixelmap */ std::unique_ptr GetPixelMapFromPixelmap(std::unique_ptr &pixelMap, - Media::PixelFormat pixelFormat = PixelFormat::UNKNOWN) + Media::PixelFormat pixelFormat = PixelFormat::UNKNOWN, + bool crop = false) { int32_t width = pixelMap->GetWidth() / DIVISOR + NUM_1; int32_t height = pixelMap->GetHeight() / DIVISOR + NUM_1; + Rect rect; + if (crop) { + rect.width = width / 2; + rect.height = height / 2; + } Media::InitializationOptions opts; opts.size.width = width; opts.size.height = height; - auto outPixelmap = Media::PixelMap::Create(*pixelMap, opts); + auto outPixelmap = Media::PixelMap::Create(*pixelMap, rect, opts); if (outPixelmap == nullptr) { return nullptr; } return outPixelmap; } +/* + * Construct a HEAP allocated PixelMap + */ +std::unique_ptr ConstructHeapPixelMap(PixelFormat format) +{ + std::unique_ptr pixelMap = std::make_unique(); + ImageInfo info; + imageInfo.width = GetData() % MAX_LENGTH_MODULO; + imageInfo.height = GetData() % MAX_LENGTH_MODULO; + info.pixelFormat = format; + pixelMap->SetImageInfo(info); + + int32_t rowDataSize = ImageUtils::GetRowDataSizeByPixelFormat(info.size.width, info.pixelFormat); + if (rowDataSize <= 0) { + return nullptr; + } + size_t bufferSize = rowDataSize * info.size.height; + void* buffer = malloc(bufferSize); // Buffer's lifecycle will be held by pixelMap + if (!buffer) { + return nullptr; + } + char* ch = static_cast(buffer); + for (unsigned int i = 0; i < bufferSize; i++) { + *(ch++) = (char)i; + } + + pixelMap->SetPixelsAddr(buffer, nullptr, bufferSize, AllocatorType::HEAP_ALLOC, nullptr); + + return pixelMap; +} + /* * get yuv info */ @@ -368,9 +407,6 @@ bool PixelMapSetImagePropertiesTest(std::unique_ptr &pixelMap) size.height = GetData(); Media::PixelFormat pixelFormat = static_cast(GetData()); pixelMap->ResetConfig(size, pixelFormat); - // Set Alpha Type - Media::AlphaType alphaType = static_cast(GetData()); - pixelMap->SetAlphaType(alphaType); // Set Memory Name std::string memoryName = GetStringFromData(); pixelMap->SetMemoryName(memoryName); @@ -409,15 +445,21 @@ bool PixelMapSetImagePropertiesTest(std::unique_ptr &pixelMap) } /* - * test pixelmap is same image + * test pixelmap convert alpha format */ -bool PixelMapIsSameImageTest(std::unique_ptr &pixelMap) +bool PixelMapConvertAlphaFormatTest(std::unique_ptr &rPixelMap) { - std::unique_ptr otherPixelMap = GetPixelMapFromOpts(); - if (!otherPixelMap) { + std::unique_ptr wPixelMap = GetPixelMapFromOpts(); + if (!wPixelMap) { return false; } - pixelMap->IsSameImage(*(otherPixelMap.get())); + if (!rPixelMap->IsSameImage(*wPixelMap)) { + bool isPremul = GetData(); + uint32_t res = rPixelMap->ConvertAlphaFormat(*wPixelMap, isPremul); + if (res != SUCCESS) { + return false; + } + } return true; } @@ -550,7 +592,7 @@ bool PixelMapCSTest(std::unique_ptr &pixelMap) bool PixelMapIPCTest(std::unique_ptr &pixelMap) { // test parcel pixelmap - Parcel parcel; + MessageParcel parcel; pixelMap->SetMemoryName("MarshallingPixelMap"); if (!pixelMap->Marshalling(parcel)) { IMAGE_LOGI("PixelMapIPCTest Marshalling failed id: %{public}d, isUnmap: %{public}d", @@ -630,6 +672,13 @@ bool PixelMapMainFuzzTest(const uint8_t* data, size_t size) return false; } + // create cropped PixelMap from other PixelMap + std::unique_ptr pixelMapCropFromOtherPixelMap = GetPixelMapFromPixelmap(pixelMapFromOpts_rgba8888, + Media::PixelFormat::RGBA_8888, true); + if (!pixelMapCropFromOtherPixelMap) { + return false; + } + return true; } @@ -655,6 +704,18 @@ bool PixelMapFromDataMainFuzzTest() if (!pixelMapFromData) { return false; } + if (!pixelMapFromData->CloseFd()) { + return false; + } + + // create HEAP allocated + std::unique_ptr pixelMapHeap = ConstructHeapPixelMap(Media::PixelFormat::RGBA_8888); + if (!pixelMapHeap) { + return false; + } + if (!pixelMapHeap->CloseFd()) { + return false; + } return true; } -- Gitee