Coverage Report

Created: 2026-05-30 07:28

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
9.84k
  UltraHdrDecFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {};
25
  void process();
26
27
 private:
28
  FuzzedDataProvider mFdp;
29
};
30
31
9.84k
void UltraHdrDecFuzzer::process() {
32
9.84k
  auto output_ct =
33
9.84k
      static_cast<uhdr_color_transfer>(mFdp.ConsumeIntegralInRange<int8_t>(kTfMin, kTfMax));
34
9.84k
  auto displayBoost = mFdp.ConsumeFloatingPointInRange<float>(-10.0f, 100.0f);
35
9.84k
  auto enableGpu = mFdp.ConsumeBool();
36
37
  // editing effects
38
9.84k
  auto applyMirror = mFdp.ConsumeBool();
39
9.84k
  uhdr_mirror_direction_t direction =
40
9.84k
      mFdp.ConsumeBool() ? UHDR_MIRROR_VERTICAL : UHDR_MIRROR_HORIZONTAL;
41
42
9.84k
  auto applyRotate = mFdp.ConsumeBool();
43
9.84k
  int degrees = degrees = mFdp.PickValueInArray({-90, 0, 90, 180, 270});
44
45
9.84k
  auto applyCrop = mFdp.ConsumeBool();
46
9.84k
  int left = mFdp.ConsumeIntegral<int16_t>();
47
9.84k
  int right = mFdp.ConsumeIntegral<int16_t>();
48
9.84k
  int top = mFdp.ConsumeIntegral<int16_t>();
49
9.84k
  int bottom = mFdp.ConsumeIntegral<int16_t>();
50
51
9.84k
  auto applyResize = mFdp.ConsumeBool();
52
9.84k
  int resizeWidth = mFdp.ConsumeIntegralInRange<int32_t>(-32, kMaxWidth + 128);
53
9.84k
  int resizeHeight = mFdp.ConsumeIntegralInRange<int32_t>(-32, kMaxHeight + 128);
54
55
9.84k
  auto buffer = mFdp.ConsumeRemainingBytes<uint8_t>();
56
57
9.84k
  uhdr_compressed_image_t jpegImgR{
58
9.84k
      buffer.data(),       (unsigned int)buffer.size(), (unsigned int)buffer.size(),
59
9.84k
      UHDR_CG_UNSPECIFIED, UHDR_CT_UNSPECIFIED,         UHDR_CR_UNSPECIFIED};
60
9.84k
#define ON_ERR(x)                              \
61
68.1k
  {                                            \
62
68.1k
    uhdr_error_info_t status_ = (x);           \
63
68.1k
    if (status_.error_code != UHDR_CODEC_OK) { \
64
6.12k
      if (status_.has_detail) {                \
65
6.12k
        ALOGE("%s", status_.detail);           \
66
6.12k
      }                                        \
67
6.12k
    }                                          \
68
68.1k
  }
69
70
9.84k
  (void)is_uhdr_image(buffer.data(), buffer.size());
71
72
9.84k
  uhdr_codec_private_t* dec_handle = uhdr_create_decoder();
73
9.84k
  if (dec_handle) {
74
9.84k
    ON_ERR(uhdr_dec_set_image(dec_handle, &jpegImgR))
75
9.84k
    ON_ERR(uhdr_dec_set_out_color_transfer(dec_handle, output_ct))
76
9.84k
    if (output_ct == UHDR_CT_LINEAR)
77
2.27k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_64bppRGBAHalfFloat))
78
7.57k
    else if (output_ct == UHDR_CT_SRGB)
79
2.94k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA8888))
80
4.62k
    else
81
4.62k
      ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA1010102))
82
9.84k
    ON_ERR(uhdr_dec_set_out_max_display_boost(dec_handle, displayBoost))
83
9.84k
    ON_ERR(uhdr_enable_gpu_acceleration(dec_handle, enableGpu))
84
9.84k
    if (applyMirror) ON_ERR(uhdr_add_effect_mirror(dec_handle, direction))
85
9.84k
    if (applyRotate) ON_ERR(uhdr_add_effect_rotate(dec_handle, degrees))
86
9.84k
    if (applyCrop) ON_ERR(uhdr_add_effect_crop(dec_handle, left, right, top, bottom))
87
9.84k
    if (applyResize) ON_ERR(uhdr_add_effect_resize(dec_handle, resizeWidth, resizeHeight))
88
9.84k
    uhdr_dec_probe(dec_handle);
89
9.84k
    auto width = uhdr_dec_get_image_width(dec_handle);
90
9.84k
    auto height = uhdr_dec_get_image_height(dec_handle);
91
9.84k
    auto gainmap_width = uhdr_dec_get_gainmap_width(dec_handle);
92
9.84k
    auto gainmap_height = uhdr_dec_get_gainmap_height(dec_handle);
93
94
9.84k
    ALOGV("image dimensions %d x %d ", (int)width, (int)height);
95
9.84k
    ALOGV("gainmap image dimensions %d x %d ", (int)gainmap_width, (int)gainmap_height);
96
9.84k
    ALOGV("output color transfer %d ", (int)output_ct);
97
9.84k
    ALOGV("max display boost %f ", (float)displayBoost);
98
9.84k
    ALOGV("enable gpu %d ", (int)enableGpu);
99
9.84k
    if (applyMirror) ALOGV("added mirror effect, direction %d", (int)direction);
100
9.84k
    if (applyRotate) ALOGV("added rotate effect, degrees %d", (int)degrees);
101
9.84k
    if (applyCrop)
102
4.48k
      ALOGV("added crop effect, crop-left %d, crop-right %d, crop-top %d, crop-bottom %d", left,
103
9.84k
            right, top, bottom);
104
9.84k
    if (applyResize)
105
4.79k
      ALOGV("added resize effect, resize wd %d, resize ht %d", resizeWidth, resizeHeight);
106
107
9.84k
    uhdr_dec_get_exif(dec_handle);
108
9.84k
    uhdr_dec_get_icc(dec_handle);
109
9.84k
    uhdr_dec_get_base_image(dec_handle);
110
9.84k
    uhdr_dec_get_gainmap_image(dec_handle);
111
9.84k
    uhdr_dec_get_gainmap_metadata(dec_handle);
112
9.84k
    uhdr_decode(dec_handle);
113
9.84k
    uhdr_get_decoded_image(dec_handle);
114
9.84k
    uhdr_get_decoded_gainmap_image(dec_handle);
115
9.84k
    uhdr_reset_decoder(dec_handle);
116
9.84k
    uhdr_release_decoder(dec_handle);
117
9.84k
  }
118
9.84k
}
119
120
9.84k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
121
9.84k
  UltraHdrDecFuzzer fuzzHandle(data, size);
122
9.84k
  fuzzHandle.process();
123
9.84k
  return 0;
124
9.84k
}