Coverage Report

Created: 2026-05-08 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/astc-encoder/Source/Fuzzers/fuzz_astc_decompress.cpp
Line
Count
Source
1
// SPDX-License-Identifier: Apache-2.0
2
// ----------------------------------------------------------------------------
3
// Copyright 2026 Arm Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
6
// use this file except in compliance with the License. You may obtain a copy
7
// of the License at:
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
// License for the specific language governing permissions and limitations
15
// under the License.
16
// ----------------------------------------------------------------------------
17
18
#include "astcenc.h"
19
#include <fuzzer/FuzzedDataProvider.h>
20
#include <vector>
21
#include <iostream>
22
23
extern "C" int LLVMFuzzerTestOneInput(
24
  const uint8_t *data,
25
  size_t size
26
1.10k
) {
27
1.10k
  FuzzedDataProvider fdp(data, size);
28
29
  // Randomize block size
30
  // Legal 2D block sizes: 4x4, 5x4, 5x5, 6x5, 6x6, 8x5, 8x6, 8x8, 10x5, 10x6, 10x8, 10x10, 12x10, 12x12
31
1.10k
  static const struct { uint8_t x, y; } block_sizes[] = {
32
1.10k
    {4, 4}, {5, 4}, {5, 5}, {6, 5}, {6, 6}, {8, 5}, {8, 6}, {8, 8},
33
1.10k
    {10, 5}, {10, 6}, {10, 8}, {10, 10}, {12, 10}, {12, 12}
34
1.10k
  };
35
36
1.10k
  int bs_idx = fdp.ConsumeIntegralInRange<int>(0, (sizeof(block_sizes) / sizeof(block_sizes[0])) - 1);
37
1.10k
  uint32_t block_x = block_sizes[bs_idx].x;
38
1.10k
  uint32_t block_y = block_sizes[bs_idx].y;
39
1.10k
  uint32_t block_z = 1;
40
41
  // Randomize profile
42
1.10k
  astcenc_profile profile = (astcenc_profile)fdp.ConsumeIntegralInRange<int>(0, 3);
43
44
  // Randomize quality
45
1.10k
  float quality = fdp.ConsumeFloatingPointInRange<float>(ASTCENC_PRE_FASTEST, ASTCENC_PRE_EXHAUSTIVE);
46
47
  // Randomize flags, but constrain to be safe with arbitrary input data
48
1.10k
  unsigned int flags = fdp.ConsumeIntegralInRange<unsigned int>(0, ASTCENC_ALL_FLAGS);
49
1.10k
  flags &= ~ASTCENC_FLG_SELF_DECOMPRESS_ONLY;
50
51
1.10k
  astcenc_config config;
52
1.10k
  astcenc_error status = astcenc_config_init(profile, block_x, block_y, block_z, quality, flags, &config);
53
1.10k
  if (status != ASTCENC_SUCCESS)
54
26
  {
55
26
    return 0;
56
26
  }
57
58
1.07k
  astcenc_context* context = nullptr;
59
1.07k
  status = astcenc_context_alloc(&config, 1, &context, nullptr);
60
1.07k
  if (status != ASTCENC_SUCCESS)
61
0
  {
62
0
    return 0;
63
0
  }
64
65
  // Prepare for decompression
66
  // We need some "compressed" data. ASTC blocks are 16 bytes.
67
  // Let's decide how many blocks to decompress.
68
1.07k
  size_t num_blocks_x = fdp.ConsumeIntegralInRange<size_t>(1, 4);
69
1.07k
  size_t num_blocks_y = fdp.ConsumeIntegralInRange<size_t>(1, 4);
70
1.07k
  size_t compressed_data_len = num_blocks_x * num_blocks_y * 16;
71
72
1.07k
  if (fdp.remaining_bytes() < compressed_data_len)
73
104
  {
74
104
    astcenc_context_free(context);
75
104
    return 0;
76
104
  }
77
78
975
  std::vector<uint8_t> compressed_data = fdp.ConsumeBytes<uint8_t>(compressed_data_len);
79
80
  // Image dimensions
81
975
  uint32_t dim_x = num_blocks_x * block_x;
82
975
  uint32_t dim_y = num_blocks_y * block_y;
83
975
  uint32_t dim_z = 1;
84
85
  // Allocate output image
86
975
  astcenc_image image_out;
87
975
  image_out.dim_x = dim_x;
88
975
  image_out.dim_y = dim_y;
89
975
  image_out.dim_z = dim_z;
90
975
  image_out.data_type = ASTCENC_TYPE_U8;
91
92
975
  uint8_t* slice_data = new uint8_t[dim_x * dim_y * 4];
93
975
  void* slices[] = { slice_data };
94
975
  image_out.data = slices;
95
96
975
  astcenc_swizzle swizzle = {
97
975
    ASTCENC_SWZ_R, ASTCENC_SWZ_G, ASTCENC_SWZ_B, ASTCENC_SWZ_A
98
975
  };
99
100
975
  status = astcenc_decompress_image(context, compressed_data.data(), compressed_data.size(), &image_out, &swizzle, 0);
101
102
975
  delete[] slice_data;
103
975
  astcenc_context_free(context);
104
105
975
  return 0;
106
1.07k
}