Coverage Report

Created: 2026-07-25 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tensor_fuzzer.cc
Line
Count
Source
1
// Copyright 2026 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <fuzzer/FuzzedDataProvider.h>
16
#include <unsupported/Eigen/CXX11/Tensor>
17
18
namespace {
19
20
static constexpr int kMaxDimensions = 5;
21
static constexpr int kMaxDimSize = 10;
22
23
template <typename Scalar>
24
341
void fuzzTensor(FuzzedDataProvider* stream) {
25
341
  int numDims = stream->ConsumeIntegralInRange<int>(1, kMaxDimensions);
26
341
  Eigen::array<Eigen::Index, kMaxDimensions> dims;
27
2.04k
  for (int i = 0; i < kMaxDimensions; ++i) {
28
1.70k
    if (i < numDims) {
29
459
      dims[i] = stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize);
30
1.24k
    } else {
31
1.24k
      dims[i] = 1;
32
1.24k
    }
33
1.70k
  }
34
35
  // We'll use a fixed rank for simplicity in templating, but varied sizes.
36
341
  Eigen::Tensor<Scalar, 3> tensor(
37
341
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
38
341
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
39
341
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize));
40
  
41
54.1k
  for (Eigen::Index i = 0; i < tensor.size(); ++i) {
42
    if constexpr (std::is_integral_v<Scalar>) {
43
      tensor(i) = stream->ConsumeIntegral<Scalar>();
44
53.7k
    } else {
45
53.7k
      tensor(i) = stream->ConsumeFloatingPoint<Scalar>();
46
53.7k
    }
47
53.7k
  }
48
49
  // Basic operations
50
341
  (void)tensor.maximum();
51
341
  (void)tensor.minimum();
52
341
  (void)tensor.sum();
53
341
  (void)tensor.mean();
54
55
  // Chipping
56
341
  if (tensor.dimension(0) > 0) {
57
341
    (void)tensor.chip(0, 0);
58
341
  }
59
60
  // Shuffling
61
341
  Eigen::array<int, 3> shuffle_dims = {1, 0, 2};
62
341
  (void)tensor.shuffle(shuffle_dims);
63
64
  // Striding
65
341
  Eigen::array<Eigen::Index, 3> strides = {2, 1, 1};
66
341
  (void)tensor.stride(strides);
67
341
}
tensor_fuzzer.cc:void (anonymous namespace)::fuzzTensor<float>(FuzzedDataProvider*)
Line
Count
Source
24
174
void fuzzTensor(FuzzedDataProvider* stream) {
25
174
  int numDims = stream->ConsumeIntegralInRange<int>(1, kMaxDimensions);
26
174
  Eigen::array<Eigen::Index, kMaxDimensions> dims;
27
1.04k
  for (int i = 0; i < kMaxDimensions; ++i) {
28
870
    if (i < numDims) {
29
241
      dims[i] = stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize);
30
629
    } else {
31
629
      dims[i] = 1;
32
629
    }
33
870
  }
34
35
  // We'll use a fixed rank for simplicity in templating, but varied sizes.
36
174
  Eigen::Tensor<Scalar, 3> tensor(
37
174
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
38
174
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
39
174
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize));
40
  
41
24.4k
  for (Eigen::Index i = 0; i < tensor.size(); ++i) {
42
    if constexpr (std::is_integral_v<Scalar>) {
43
      tensor(i) = stream->ConsumeIntegral<Scalar>();
44
24.2k
    } else {
45
24.2k
      tensor(i) = stream->ConsumeFloatingPoint<Scalar>();
46
24.2k
    }
47
24.2k
  }
48
49
  // Basic operations
50
174
  (void)tensor.maximum();
51
174
  (void)tensor.minimum();
52
174
  (void)tensor.sum();
53
174
  (void)tensor.mean();
54
55
  // Chipping
56
174
  if (tensor.dimension(0) > 0) {
57
174
    (void)tensor.chip(0, 0);
58
174
  }
59
60
  // Shuffling
61
174
  Eigen::array<int, 3> shuffle_dims = {1, 0, 2};
62
174
  (void)tensor.shuffle(shuffle_dims);
63
64
  // Striding
65
174
  Eigen::array<Eigen::Index, 3> strides = {2, 1, 1};
66
174
  (void)tensor.stride(strides);
67
174
}
tensor_fuzzer.cc:void (anonymous namespace)::fuzzTensor<double>(FuzzedDataProvider*)
Line
Count
Source
24
167
void fuzzTensor(FuzzedDataProvider* stream) {
25
167
  int numDims = stream->ConsumeIntegralInRange<int>(1, kMaxDimensions);
26
167
  Eigen::array<Eigen::Index, kMaxDimensions> dims;
27
1.00k
  for (int i = 0; i < kMaxDimensions; ++i) {
28
835
    if (i < numDims) {
29
218
      dims[i] = stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize);
30
617
    } else {
31
617
      dims[i] = 1;
32
617
    }
33
835
  }
34
35
  // We'll use a fixed rank for simplicity in templating, but varied sizes.
36
167
  Eigen::Tensor<Scalar, 3> tensor(
37
167
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
38
167
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
39
167
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize));
40
  
41
29.6k
  for (Eigen::Index i = 0; i < tensor.size(); ++i) {
42
    if constexpr (std::is_integral_v<Scalar>) {
43
      tensor(i) = stream->ConsumeIntegral<Scalar>();
44
29.4k
    } else {
45
29.4k
      tensor(i) = stream->ConsumeFloatingPoint<Scalar>();
46
29.4k
    }
47
29.4k
  }
48
49
  // Basic operations
50
167
  (void)tensor.maximum();
51
167
  (void)tensor.minimum();
52
167
  (void)tensor.sum();
53
167
  (void)tensor.mean();
54
55
  // Chipping
56
167
  if (tensor.dimension(0) > 0) {
57
167
    (void)tensor.chip(0, 0);
58
167
  }
59
60
  // Shuffling
61
167
  Eigen::array<int, 3> shuffle_dims = {1, 0, 2};
62
167
  (void)tensor.shuffle(shuffle_dims);
63
64
  // Striding
65
167
  Eigen::array<Eigen::Index, 3> strides = {2, 1, 1};
66
167
  (void)tensor.stride(strides);
67
167
}
68
69
} // namespace
70
71
3.05k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
72
3.05k
  FuzzedDataProvider stream(data, size);
73
74
3.05k
  uint8_t type = stream.ConsumeIntegral<uint8_t>();
75
3.05k
  switch (type % 2) {
76
1.68k
    case 0:
77
1.68k
      fuzzTensor<float>(&stream);
78
1.68k
      break;
79
1.36k
    case 1:
80
1.36k
      fuzzTensor<double>(&stream);
81
1.36k
      break;
82
3.05k
  }
83
84
3.05k
  return 0;
85
3.05k
}