Coverage Report

Created: 2026-10-17 08:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libultrahdr/fuzzer/ultrahdr_dec_fuzzer.cpp
Line
Count
Source
1
/*
2
 * Copyright 2023 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7
 * option. This file may not be copied, modified, or distributed
8
 * except according to those terms.
9
 */
10
11
#include <fuzzer/FuzzedDataProvider.h>
12
13
#include "ultrahdr_api.h"
14
#include "ultrahdr/ultrahdrcommon.h"
15
16
using namespace ultrahdr;
17
18
// Transfer functions for image data, sync with ultrahdr.h
19
constexpr int kTfMin = UHDR_CT_UNSPECIFIED;
20
constexpr int kTfMax = UHDR_CT_SRGB;
21
22
class UltraHdrDecFuzzer {
23
 public:
24
16.4k
  UltraHdrDecFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {};
25
  void process();
26
27
 private:
28
  FuzzedDataProvider mFdp;
29
};
30
31
16.4k
void UltraHdrDecFuzzer::process() {
32
16.4k
  auto output_ct =
33
16.4k
      static_cast<uhdr_color_transfer>(mFdp.ConsumeIntegralInRange<int8_t>(kTfMin, kTfMax));
34
16.4k
  auto displayBoost = mFdp.ConsumeFloatingPointInRange<float>(-10.0f, 100.0f);
35
16.4k
  auto enableGpu = mFdp.ConsumeBool();
36
37
  // editing effects
38
16.4k
  auto applyMirror = mFdp.ConsumeBool();
39
16.4k
  uhdr_mirror_direction_t direction =
40
16.4k
      mFdp.ConsumeBool() ? UHDR_MIRROR_VERTICAL : UHDR_MIRROR_HORIZONTAL;
41
42
16.4k
  auto applyRotate = mFdp.ConsumeBool();
43
16.4k
  int degrees = degrees = mFdp.PickValueInArray({-90, 0, 90, 180, 270});
44
45
16.4k
  auto applyCrop = mFdp.ConsumeBool();
46
16.4k
  int left = mFdp.ConsumeIntegral<int16_t>();
47
16.4k
  int right = mFdp.ConsumeIntegral<int16_t>();
48
16.4k
  int top = mFdp.ConsumeIntegral<int16_t>();
49
16.4k
  int bottom = mFdp.ConsumeIntegral<int16_t>();
50
51
16.4k
  auto applyResize = mFdp.ConsumeBool();
52
16.4k
  int resizeWidth = mFdp.ConsumeIntegralInRange<int32_t>(-32, kMaxWidth + 128);
53
16.4k
  int resizeHeight = mFdp.ConsumeIntegralInRange<int32_t>(-32, kMaxHeight + 128);
54
55
16.4k
  auto buffer = mFdp.ConsumeRemainingBytes<uint8_t>();
56
57
16.4k
  uhdr_compressed_image_t jpegImgR{
58
16.4k
      buffer.data(),       (unsigned int)buffer.size(), (unsigned int)buffer.size(),
59
16.4k
      UHDR_CG_UNSPECIFIED, UHDR_CT_UNSPECIFIED,         UHDR_CR_UNSPECIFIED};
60
16.4k
#define ON_ERR(x)                              \
61
111k
  {                                            \
62
111k
    uhdr_error_info_t status_ = (x);           \
63
111k
    if (status_.error_code != UHDR_CODEC_OK) { \
64
12.7k
      if (status_.has_detail) {                \
65
12.7k
        ALOGE("%s", status_.detail);           \
66
12.7k
      }                                        \
67
12.7k
    }                                          \
68
111k
  }
69
70
16.4k
  (void)is_uhdr_image(buffer.data(), buffer.size());
71
72
16.4k
  uhdr_codec_private_t* dec_handle = uhdr_create_decoder();
73
16.4k
  if (dec_handle) {
74
16.4k
    ON_ERR(uhdr_dec_set_image(dec_handle, &jpegImgR))
75
16.4k
    ON_ERR(uhdr_dec_set_out_color_transfer(dec_handle, output_ct))
76
16.4k
    if (output_ct == UHDR_CT_LINEAR)
77
3.54k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_64bppRGBAHalfFloat))
78
12.9k
    else if (output_ct == UHDR_CT_SRGB)
79
4.05k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA8888))
80
8.89k
    else
81
8.89k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA1010102))
82
16.4k
    ON_ERR(uhdr_dec_set_out_max_display_boost(dec_handle, displayBoost))
83
16.4k
    ON_ERR(uhdr_enable_gpu_acceleration(dec_handle, enableGpu))
84
16.4k
    if (applyMirror) ON_ERR(uhdr_add_effect_mirror(dec_handle, direction))
85
16.4k
    if (applyRotate) ON_ERR(uhdr_add_effect_rotate(dec_handle, degrees))
86
16.4k
    if (applyCrop) ON_ERR(uhdr_add_effect_crop(dec_handle, left, right, top, bottom))
87
16.4k
    if (applyResize) ON_ERR(uhdr_add_effect_resize(dec_handle, resizeWidth, resizeHeight))
88
16.4k
    uhdr_dec_probe(dec_handle);
89
16.4k
    auto width = uhdr_dec_get_image_width(dec_handle);
90
16.4k
    auto height = uhdr_dec_get_image_height(dec_handle);
91
16.4k
    auto gainmap_width = uhdr_dec_get_gainmap_width(dec_handle);
92
16.4k
    auto gainmap_height = uhdr_dec_get_gainmap_height(dec_handle);
93
94
16.4k
    ALOGV("image dimensions %d x %d ", (int)width, (int)height);
95
16.4k
    ALOGV("gainmap image dimensions %d x %d ", (int)gainmap_width, (int)gainmap_height);
96
16.4k
    ALOGV("output color transfer %d ", (int)output_ct);
97
16.4k
    ALOGV("max display boost %f ", (float)displayBoost);
98
16.4k
    ALOGV("enable gpu %d ", (int)enableGpu);
99
16.4k
    if (applyMirror) ALOGV("added mirror effect, direction %d", (int)direction);
100
16.4k
    if (applyRotate) ALOGV("added rotate effect, degrees %d", (int)degrees);
101
16.4k
    if (applyCrop)
102
6.87k
      ALOGV("added crop effect, crop-left %d, crop-right %d, crop-top %d, crop-bottom %d", left,
103
16.4k
            right, top, bottom);
104
16.4k
    if (applyResize)
105
6.79k
      ALOGV("added resize effect, resize wd %d, resize ht %d", resizeWidth, resizeHeight);
106
107
16.4k
    uhdr_dec_get_exif(dec_handle);
108
16.4k
    uhdr_dec_get_icc(dec_handle);
109
16.4k
    uhdr_dec_get_base_image(dec_handle);
110
16.4k
    uhdr_dec_get_gainmap_image(dec_handle);
111
16.4k
    uhdr_dec_get_gainmap_metadata(dec_handle);
112
16.4k
    uhdr_decode(dec_handle);
113
16.4k
    uhdr_get_decoded_image(dec_handle);
114
16.4k
    uhdr_get_decoded_gainmap_image(dec_handle);
115
16.4k
    uhdr_reset_decoder(dec_handle);
116
16.4k
    uhdr_release_decoder(dec_handle);
117
16.4k
  }
118
16.4k
}
119
120
16.4k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
121
16.4k
  (void)uhdr_is_supported_gainmap_image(data, size);
122
16.4k
  UltraHdrDecFuzzer fuzzHandle(data, size);
123
16.4k
  fuzzHandle.process();
124
16.4k
  return 0;
125
16.4k
}