File: libyuv_scale_fuzzer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (150 lines) | stat: -rw-r--r-- 5,034 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "testing/libfuzzer/fuzzers/libyuv_scale_fuzzer.h"

#include <stddef.h>
#include <stdint.h>

#include <cstdlib>
#include <random>
#include <string>

#include "third_party/libyuv/include/libyuv.h"

static void FillBufferWithRandomData(uint8_t* dst,
                                     size_t len,
                                     std::minstd_rand0 rng) {
  size_t i;
  for (i = 0; i + 3 < len; i += 4) {
    *reinterpret_cast<uint32_t*>(dst) = rng();
    dst += 4;
  }
  for (; i < len; ++i) {
    *dst++ = rng();
  }
}

void Scale(bool is420,
           int src_width,
           int src_height,
           int dst_width,
           int dst_height,
           int filter_num,
           std::string seed_str) {
  int src_width_uv, src_height_uv;
  if (is420) {
    src_width_uv = (std::abs(src_width) + 1) >> 1;
    src_height_uv = (std::abs(src_height) + 1) >> 1;
  } else {
    src_width_uv = (std::abs(src_width));
    src_height_uv = (std::abs(src_height));
  }

  size_t src_y_plane_size = (std::abs(src_width)) * (std::abs(src_height));
  size_t src_uv_plane_size = (src_width_uv) * (src_height_uv);

  int src_stride_y = std::abs(src_width);
  int src_stride_uv = src_width_uv;

  uint8_t* src_y = reinterpret_cast<uint8_t*>(malloc(src_y_plane_size));
  uint8_t* src_u = reinterpret_cast<uint8_t*>(malloc(src_uv_plane_size));
  uint8_t* src_v = reinterpret_cast<uint8_t*>(malloc(src_uv_plane_size));

  uint16_t* p_src_y_16 =
      reinterpret_cast<uint16_t*>(malloc(src_y_plane_size * 2));
  uint16_t* p_src_u_16 =
      reinterpret_cast<uint16_t*>(malloc(src_uv_plane_size * 2));
  uint16_t* p_src_v_16 =
      reinterpret_cast<uint16_t*>(malloc(src_uv_plane_size * 2));

  std::seed_seq seed(seed_str.begin(), seed_str.end());
  std::minstd_rand0 rng(seed);

  // TODO - consider taking directly as parameters when this code
  // is being run using FuzzTest, though it would probably require
  // complex domains to ensure they're the right size.
  FillBufferWithRandomData(src_y, src_y_plane_size, rng);
  FillBufferWithRandomData(src_u, src_uv_plane_size, rng);
  FillBufferWithRandomData(src_v, src_uv_plane_size, rng);

  for (size_t i = 0; i < src_y_plane_size; ++i) {
    p_src_y_16[i] = src_y[i];
  }
  for (size_t i = 0; i < src_uv_plane_size; ++i) {
    p_src_u_16[i] = src_u[i];
    p_src_v_16[i] = src_v[i];
  }

  int dst_width_uv, dst_height_uv;
  if (is420) {
    dst_width_uv = (dst_width + 1) >> 1;
    dst_height_uv = (dst_height + 1) >> 1;
  } else {
    dst_width_uv = dst_width;
    dst_height_uv = dst_height;
  }

  size_t dst_y_plane_size = (dst_width) * (dst_height);
  size_t dst_uv_plane_size = (dst_width_uv) * (dst_height_uv);

  int dst_stride_y = dst_width;
  int dst_stride_uv = dst_width_uv;

  uint8_t* dst_y_c = reinterpret_cast<uint8_t*>(malloc(dst_y_plane_size));
  uint8_t* dst_u_c = reinterpret_cast<uint8_t*>(malloc(dst_uv_plane_size));
  uint8_t* dst_v_c = reinterpret_cast<uint8_t*>(malloc(dst_uv_plane_size));

  uint16_t* p_dst_y_16 =
      reinterpret_cast<uint16_t*>(malloc(dst_y_plane_size * 2));
  uint16_t* p_dst_u_16 =
      reinterpret_cast<uint16_t*>(malloc(dst_uv_plane_size * 2));
  uint16_t* p_dst_v_16 =
      reinterpret_cast<uint16_t*>(malloc(dst_uv_plane_size * 2));

  if (is420) {
    I420Scale(src_y, src_stride_y, src_u, src_stride_uv, src_v, src_stride_uv,
              src_width, src_height, dst_y_c, dst_stride_y, dst_u_c,
              dst_stride_uv, dst_v_c, dst_stride_uv, dst_width, dst_height,
              static_cast<libyuv::FilterMode>(filter_num));

    I420Scale_16(p_src_y_16, src_stride_y, p_src_u_16, src_stride_uv,
                 p_src_v_16, src_stride_uv, src_width, src_height, p_dst_y_16,
                 dst_stride_y, p_dst_u_16, dst_stride_uv, p_dst_v_16,
                 dst_stride_uv, dst_width, dst_height,
                 static_cast<libyuv::FilterMode>(filter_num));
  } else {
    I444Scale(src_y, src_stride_y, src_u, src_stride_uv, src_v, src_stride_uv,
              src_width, src_height, dst_y_c, dst_stride_y, dst_u_c,
              dst_stride_uv, dst_v_c, dst_stride_uv, dst_width, dst_height,
              static_cast<libyuv::FilterMode>(filter_num));

    I444Scale_16(p_src_y_16, src_stride_y, p_src_u_16, src_stride_uv,
                 p_src_v_16, src_stride_uv, src_width, src_height, p_dst_y_16,
                 dst_stride_y, p_dst_u_16, dst_stride_uv, p_dst_v_16,
                 dst_stride_uv, dst_width, dst_height,
                 static_cast<libyuv::FilterMode>(filter_num));
  }

  free(src_y);
  free(src_u);
  free(src_v);

  free(p_src_y_16);
  free(p_src_u_16);
  free(p_src_v_16);

  free(dst_y_c);
  free(dst_u_c);
  free(dst_v_c);

  free(p_dst_y_16);
  free(p_dst_u_16);
  free(p_dst_v_16);
}