Coverage Report

Created: 2026-11-24 06:45

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
344
void fuzzTensor(FuzzedDataProvider* stream) {
25
344
  int numDims = stream->ConsumeIntegralInRange<int>(1, kMaxDimensions);
26
344
  Eigen::array<Eigen::Index, kMaxDimensions> dims;
27
2.06k
  for (int i = 0; i < kMaxDimensions; ++i) {
28
1.72k
    if (i < numDims) {
29
462
      dims[i] = stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize);
30
1.25k
    } else {
31
1.25k
      dims[i] = 1;
32
1.25k
    }
33
1.72k
  }
34
35
  // We'll use a fixed rank for simplicity in templating, but varied sizes.
36
344
  Eigen::Tensor<Scalar, 3> tensor(
37
344
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
38
344
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
39
344
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize));
40
  
41
40.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
40.0k
    } else {
45
40.0k
      tensor(i) = stream->ConsumeFloatingPoint<Scalar>();
46
40.0k
    }
47
40.0k
  }
48
49
  // Basic operations
50
344
  (void)tensor.maximum();
51
344
  (void)tensor.minimum();
52
344
  (void)tensor.sum();
53
344
  (void)tensor.mean();
54
55
  // Chipping
56
344
  if (tensor.dimension(0) > 0) {
57
344
    (void)tensor.chip(0, 0);
58
344
  }
59
60
  // Shuffling
61
344
  Eigen::array<int, 3> shuffle_dims = {1, 0, 2};
62
344
  (void)tensor.shuffle(shuffle_dims);
63
64
  // Striding
65
344
  Eigen::array<Eigen::Index, 3> strides = {2, 1, 1};
66
344
  (void)tensor.stride(strides);
67
344
}
tensor_fuzzer.cc:void (anonymous namespace)::fuzzTensor<float>(FuzzedDataProvider*)
Line
Count
Source
24
175
void fuzzTensor(FuzzedDataProvider* stream) {
25
175
  int numDims = stream->ConsumeIntegralInRange<int>(1, kMaxDimensions);
26
175
  Eigen::array<Eigen::Index, kMaxDimensions> dims;
27
1.05k
  for (int i = 0; i < kMaxDimensions; ++i) {
28
875
    if (i < numDims) {
29
246
      dims[i] = stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize);
30
629
    } else {
31
629
      dims[i] = 1;
32
629
    }
33
875
  }
34
35
  // We'll use a fixed rank for simplicity in templating, but varied sizes.
36
175
  Eigen::Tensor<Scalar, 3> tensor(
37
175
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
38
175
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
39
175
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize));
40
  
41
21.3k
  for (Eigen::Index i = 0; i < tensor.size(); ++i) {
42
    if constexpr (std::is_integral_v<Scalar>) {
43
      tensor(i) = stream->ConsumeIntegral<Scalar>();
44
21.1k
    } else {
45
21.1k
      tensor(i) = stream->ConsumeFloatingPoint<Scalar>();
46
21.1k
    }
47
21.1k
  }
48
49
  // Basic operations
50
175
  (void)tensor.maximum();
51
175
  (void)tensor.minimum();
52
175
  (void)tensor.sum();
53
175
  (void)tensor.mean();
54
55
  // Chipping
56
175
  if (tensor.dimension(0) > 0) {
57
175
    (void)tensor.chip(0, 0);
58
175
  }
59
60
  // Shuffling
61
175
  Eigen::array<int, 3> shuffle_dims = {1, 0, 2};
62
175
  (void)tensor.shuffle(shuffle_dims);
63
64
  // Striding
65
175
  Eigen::array<Eigen::Index, 3> strides = {2, 1, 1};
66
175
  (void)tensor.stride(strides);
67
175
}
tensor_fuzzer.cc:void (anonymous namespace)::fuzzTensor<double>(FuzzedDataProvider*)
Line
Count
Source
24
169
void fuzzTensor(FuzzedDataProvider* stream) {
25
169
  int numDims = stream->ConsumeIntegralInRange<int>(1, kMaxDimensions);
26
169
  Eigen::array<Eigen::Index, kMaxDimensions> dims;
27
1.01k
  for (int i = 0; i < kMaxDimensions; ++i) {
28
845
    if (i < numDims) {
29
216
      dims[i] = stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize);
30
629
    } else {
31
629
      dims[i] = 1;
32
629
    }
33
845
  }
34
35
  // We'll use a fixed rank for simplicity in templating, but varied sizes.
36
169
  Eigen::Tensor<Scalar, 3> tensor(
37
169
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
38
169
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize),
39
169
      stream->ConsumeIntegralInRange<Eigen::Index>(1, kMaxDimSize));
40
  
41
19.0k
  for (Eigen::Index i = 0; i < tensor.size(); ++i) {
42
    if constexpr (std::is_integral_v<Scalar>) {
43
      tensor(i) = stream->ConsumeIntegral<Scalar>();
44
18.9k
    } else {
45
18.9k
      tensor(i) = stream->ConsumeFloatingPoint<Scalar>();
46
18.9k
    }
47
18.9k
  }
48
49
  // Basic operations
50
169
  (void)tensor.maximum();
51
169
  (void)tensor.minimum();
52
169
  (void)tensor.sum();
53
169
  (void)tensor.mean();
54
55
  // Chipping
56
169
  if (tensor.dimension(0) > 0) {
57
169
    (void)tensor.chip(0, 0);
58
169
  }
59
60
  // Shuffling
61
169
  Eigen::array<int, 3> shuffle_dims = {1, 0, 2};
62
169
  (void)tensor.shuffle(shuffle_dims);
63
64
  // Striding
65
169
  Eigen::array<Eigen::Index, 3> strides = {2, 1, 1};
66
169
  (void)tensor.stride(strides);
67
169
}
68
69
} // namespace
70
71
3.08k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
72
3.08k
  FuzzedDataProvider stream(data, size);
73
74
3.08k
  uint8_t type = stream.ConsumeIntegral<uint8_t>();
75
3.08k
  switch (type % 2) {
76
1.67k
    case 0:
77
1.67k
      fuzzTensor<float>(&stream);
78
1.67k
      break;
79
1.41k
    case 1:
80
1.41k
      fuzzTensor<double>(&stream);
81
1.41k
      break;
82
3.08k
  }
83
84
3.08k
  return 0;
85
3.08k
}