Coverage Report

Created: 2025-07-14 07:19

/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 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#include <fuzzer/FuzzedDataProvider.h>
18
19
#include "ultrahdr_api.h"
20
#include "ultrahdr/ultrahdrcommon.h"
21
22
using namespace ultrahdr;
23
24
// Transfer functions for image data, sync with ultrahdr.h
25
constexpr int kTfMin = UHDR_CT_UNSPECIFIED;
26
constexpr int kTfMax = UHDR_CT_SRGB;
27
28
class UltraHdrDecFuzzer {
29
 public:
30
9.46k
  UltraHdrDecFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {};
31
  void process();
32
33
 private:
34
  FuzzedDataProvider mFdp;
35
};
36
37
9.46k
void UltraHdrDecFuzzer::process() {
38
9.46k
  auto output_ct =
39
9.46k
      static_cast<uhdr_color_transfer>(mFdp.ConsumeIntegralInRange<int8_t>(kTfMin, kTfMax));
40
9.46k
  auto displayBoost = mFdp.ConsumeFloatingPointInRange<float>(-10.0f, 100.0f);
41
9.46k
  auto enableGpu = mFdp.ConsumeBool();
42
43
  // editing effects
44
9.46k
  auto applyMirror = mFdp.ConsumeBool();
45
9.46k
  uhdr_mirror_direction_t direction =
46
9.46k
      mFdp.ConsumeBool() ? UHDR_MIRROR_VERTICAL : UHDR_MIRROR_HORIZONTAL;
47
48
9.46k
  auto applyRotate = mFdp.ConsumeBool();
49
9.46k
  int degrees = degrees = mFdp.PickValueInArray({-90, 0, 90, 180, 270});
50
51
9.46k
  auto applyCrop = mFdp.ConsumeBool();
52
9.46k
  int left = mFdp.ConsumeIntegral<int16_t>();
53
9.46k
  int right = mFdp.ConsumeIntegral<int16_t>();
54
9.46k
  int top = mFdp.ConsumeIntegral<int16_t>();
55
9.46k
  int bottom = mFdp.ConsumeIntegral<int16_t>();
56
57
9.46k
  auto applyResize = mFdp.ConsumeBool();
58
9.46k
  int resizeWidth = mFdp.ConsumeIntegralInRange<int32_t>(-32, kMaxWidth + 128);
59
9.46k
  int resizeHeight = mFdp.ConsumeIntegralInRange<int32_t>(-32, kMaxHeight + 128);
60
61
9.46k
  auto buffer = mFdp.ConsumeRemainingBytes<uint8_t>();
62
63
9.46k
  uhdr_compressed_image_t jpegImgR{
64
9.46k
      buffer.data(),       (unsigned int)buffer.size(), (unsigned int)buffer.size(),
65
9.46k
      UHDR_CG_UNSPECIFIED, UHDR_CT_UNSPECIFIED,         UHDR_CR_UNSPECIFIED};
66
9.46k
#define ON_ERR(x)                              \
67
65.3k
  {                                            \
68
65.3k
    uhdr_error_info_t status_ = (x);           \
69
65.3k
    if (status_.error_code != UHDR_CODEC_OK) { \
70
6.86k
      if (status_.has_detail) {                \
71
6.86k
        ALOGE("%s", status_.detail);           \
72
6.86k
      }                                        \
73
6.86k
    }                                          \
74
65.3k
  }
75
76
9.46k
  (void)is_uhdr_image(buffer.data(), buffer.size());
77
78
9.46k
  uhdr_codec_private_t* dec_handle = uhdr_create_decoder();
79
9.46k
  if (dec_handle) {
80
9.46k
    ON_ERR(uhdr_dec_set_image(dec_handle, &jpegImgR))
81
9.46k
    ON_ERR(uhdr_dec_set_out_color_transfer(dec_handle, output_ct))
82
9.46k
    if (output_ct == UHDR_CT_LINEAR)
83
2.01k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_64bppRGBAHalfFloat))
84
7.45k
    else if (output_ct == UHDR_CT_SRGB)
85
2.55k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA8888))
86
4.90k
    else
87
4.90k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA1010102))
88
9.46k
    ON_ERR(uhdr_dec_set_out_max_display_boost(dec_handle, displayBoost))
89
9.46k
    ON_ERR(uhdr_enable_gpu_acceleration(dec_handle, enableGpu))
90
9.46k
    if (applyMirror) ON_ERR(uhdr_add_effect_mirror(dec_handle, direction))
91
9.46k
    if (applyRotate) ON_ERR(uhdr_add_effect_rotate(dec_handle, degrees))
92
9.46k
    if (applyCrop) ON_ERR(uhdr_add_effect_crop(dec_handle, left, right, top, bottom))
93
9.46k
    if (applyResize) ON_ERR(uhdr_add_effect_resize(dec_handle, resizeWidth, resizeHeight))
94
9.46k
    uhdr_dec_probe(dec_handle);
95
9.46k
    auto width = uhdr_dec_get_image_width(dec_handle);
96
9.46k
    auto height = uhdr_dec_get_image_height(dec_handle);
97
9.46k
    auto gainmap_width = uhdr_dec_get_gainmap_width(dec_handle);
98
9.46k
    auto gainmap_height = uhdr_dec_get_gainmap_height(dec_handle);
99
100
9.46k
    ALOGV("image dimensions %d x %d ", (int)width, (int)height);
101
9.46k
    ALOGV("gainmap image dimensions %d x %d ", (int)gainmap_width, (int)gainmap_height);
102
9.46k
    ALOGV("output color transfer %d ", (int)output_ct);
103
9.46k
    ALOGV("max display boost %f ", (float)displayBoost);
104
9.46k
    ALOGV("enable gpu %d ", (int)enableGpu);
105
9.46k
    if (applyMirror) ALOGV("added mirror effect, direction %d", (int)direction);
106
9.46k
    if (applyRotate) ALOGV("added rotate effect, degrees %d", (int)degrees);
107
9.46k
    if (applyCrop)
108
3.98k
      ALOGV("added crop effect, crop-left %d, crop-right %d, crop-top %d, crop-bottom %d", left,
109
9.46k
            right, top, bottom);
110
9.46k
    if (applyResize)
111
4.72k
      ALOGV("added resize effect, resize wd %d, resize ht %d", resizeWidth, resizeHeight);
112
113
9.46k
    uhdr_dec_get_exif(dec_handle);
114
9.46k
    uhdr_dec_get_icc(dec_handle);
115
9.46k
    uhdr_dec_get_base_image(dec_handle);
116
9.46k
    uhdr_dec_get_gainmap_image(dec_handle);
117
9.46k
    uhdr_dec_get_gainmap_metadata(dec_handle);
118
9.46k
    uhdr_decode(dec_handle);
119
9.46k
    uhdr_get_decoded_image(dec_handle);
120
9.46k
    uhdr_get_decoded_gainmap_image(dec_handle);
121
9.46k
    uhdr_reset_decoder(dec_handle);
122
9.46k
    uhdr_release_decoder(dec_handle);
123
9.46k
  }
124
9.46k
}
125
126
14.4k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
127
14.4k
  UltraHdrDecFuzzer fuzzHandle(data, size);
128
14.4k
  fuzzHandle.process();
129
14.4k
  return 0;
130
14.4k
}