Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libyuv initial integration #4363

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions projects/libyuv/fuzz_common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* # Copyright 2020 Google Inc. */
/* # */
/* # Licensed under the Apache License, Version 2.0 (the "License"); */
/* # you may not use this file except in compliance with the License. */
/* # You may obtain a copy of the License at */
/* # */
/* # http://www.apache.org/licenses/LICENSE-2.0 */
/* # */
/* # Unless required by applicable law or agreed to in writing, software */
/* # distributed under the License is distributed on an "AS IS" BASIS, */
/* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* # See the License for the specific language governing permissions and */
/* # limitations under the License. */
/* # */
/* ################################################################################ */

#include <fstream>
#include <iostream>

#include "fuzz_common.h"
#include "libyuv.h"
#include <fuzzer/FuzzedDataProvider.h>

using std::ios;
using std::ofstream;
using namespace libyuv;

bool write_conf(int width, int height, int cpu_flags, int src_stride_y, int src_stride_uv, int dst_y_stride, int dst_uv_stride)
{
// ios::trunc
ofstream out_file("libyuv_fuzz.conf", ios::out | ios::ate);
if (out_file)
{
out_file << "width = " << width << " height= " << height << " cpu_flag = " << std::hex << cpu_flags << " src_stride_y = " << src_stride_y << " stride_uv = " << src_stride_uv << " dst_y_stride = " << dst_y_stride << " dst_uv_stride = " << dst_uv_stride << std::endl;
out_file.close();
return 1;
}
return 0;
}

int generate_cpuflags(int random_num)
{
int cpu_info = 0;
if (random_num & 0x01)
{
//disable all cpu specific optimizations.
cpu_info = 1;
return cpu_info;
}

//enable all cpu specific optimizations.
cpu_info = -1;

#if defined(__arm__) || defined(__aarch64__)
random_num &= (kCpuHasNEON);
return cpu_info &= (~random_num);
#endif

#if !defined(__pnacl__) && !defined(__CLR_VER) && (defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86))
random_num &= (kCpuHasX86 | kCpuHasSSE2 | kCpuHasSSSE3 | kCpuHasSSE41 | kCpuHasSSE42 | kCpuHasAVX | kCpuHasAVX2 | kCpuHasERMS | kCpuHasFMA3 | kCpuHasF16C | kCpuHasGFNI | kCpuHasAVX512BW | kCpuHasAVX512VL | kCpuHasAVX512VBMI | kCpuHasAVX512VBITALG | kCpuHasAVX512VPOPCNTDQ);
return cpu_info &= ~(random_num);
#endif

#if defined(__mips__) || defined(__linux__)
random_num &= (kCpuHasMSA | kCpuHasMMI);
return cpu_info &= ~(random_num);
#endif

return cpu_info;
}
25 changes: 25 additions & 0 deletions projects/libyuv/fuzz_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* # Copyright 2020 Google Inc. */
/* # */
/* # Licensed under the Apache License, Version 2.0 (the "License"); */
/* # you may not use this file except in compliance with the License. */
/* # You may obtain a copy of the License at */
/* # */
/* # http://www.apache.org/licenses/LICENSE-2.0 */
/* # */
/* # Unless required by applicable law or agreed to in writing, software */
/* # distributed under the License is distributed on an "AS IS" BASIS, */
/* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* # See the License for the specific language governing permissions and */
/* # limitations under the License. */
/* # */
/* ################################################################################ */

/*
* 'fuzz_common' aims to provide some common functions for all fuzz target
*/

#pragma once

bool write_conf(int width, int height,int cpu_flags,int src_stride_y,int src_stride_uv,int dst_stride_y,int dst_uv_stride);

int generate_cpuflags(int random_num);
77 changes: 77 additions & 0 deletions projects/libyuv/libyuv_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* # Copyright 2020 Google Inc. */
/* # */
/* # Licensed under the Apache License, Version 2.0 (the "License"); */
/* # you may not use this file except in compliance with the License. */
/* # You may obtain a copy of the License at */
/* # */
/* # http://www.apache.org/licenses/LICENSE-2.0 */
/* # */
/* # Unless required by applicable law or agreed to in writing, software */
/* # distributed under the License is distributed on an "AS IS" BASIS, */
/* # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* # See the License for the specific language governing permissions and */
/* # limitations under the License. */
/* # */
/* ################################################################################ */

#include <climits>
#include <iostream>
#include <vector>

#include "fuzz_common.h"
#include "libyuv.h"
#include <fuzzer/FuzzedDataProvider.h>

using namespace std;

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
if (size == 0) {
libyuv::I420ToNV21(data, 0, data, 0, data, 0, nullptr, 0, nullptr, 0, 0, 0);
return 0;
}

FuzzedDataProvider fuzzed_data(data, size);

int range_size = (static_cast<int>(size) + 1) / 3;
int width = 0, height = 0, kPixels = 0, kHalfPixels = 0;

width = fuzzed_data.ConsumeIntegralInRange(0, range_size);
height = fuzzed_data.ConsumeIntegralInRange(0, range_size / (width + 1));
kPixels = width * height;
kHalfPixels = ((width + 1) / 2) * ((height + 1) / 2);
std::vector<uint8_t> first_part = fuzzed_data.ConsumeBytes<uint8_t>(kPixels);
std::vector<uint8_t> second_part = fuzzed_data.ConsumeBytes<uint8_t>(kHalfPixels);
std::vector<uint8_t> third_part = fuzzed_data.ConsumeBytes<uint8_t>(kHalfPixels);

const uint8_t* src_y = first_part.data();
const uint8_t* src_u = second_part.data();
const uint8_t* src_v = third_part.data();

height = fuzzed_data.ConsumeBool() ? height : -height;

int random_num = fuzzed_data.ConsumeIntegralInRange(0, INT_MAX);
int cpu_flags = generate_cpuflags(random_num);
libyuv::MaskCpuFlags(cpu_flags);

uint8_t* dst_data = new uint8_t[size];

uint8_t* dst_y = dst_data;
uint8_t* dst_uv = dst_data + kPixels;

int src_stride_y = width;
int src_stride_uv = (width + 1) / 2;

int dst_stride = fuzzed_data.ConsumeIntegralInRange(0, width);

int dst_stride_y = dst_stride;
int dst_stride_uv = ((dst_stride + 1) / 2) * 2;

write_conf(width, height, cpu_flags, src_stride_y, src_stride_uv, dst_stride_y, dst_stride_uv);

libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_uv, src_v, src_stride_uv, dst_y, dst_stride_y, dst_uv, dst_stride_uv, width, height);

delete[] dst_data;

return 0;
}