skia2/fuzz/oss_fuzz/FuzzSkRuntimeEffect.cpp
Brian Osman 3e8fae7193 Revert "Add inline threshold to SkSL::Program::Settings"
This reverts commit ad3db40d78.

Reason for revert: Chrome doesn't like static initializers.

Original change's description:
> Add inline threshold to SkSL::Program::Settings
> 
> Keep the same default value, but add a (private) API to change it when
> compiling SkRuntimeEffect code.
> 
> Use the new API to improve fuzzer coverage, by fuzzing with inlining
> disabled and (enthusiastically) enabled.
> 
> Change-Id: I36424bac95144aeb727cfb949754fbe998d5d7de
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308181
> Commit-Queue: Brian Osman <brianosman@google.com>
> Reviewed-by: Kevin Lubick <kjlubick@google.com>
> Reviewed-by: Ethan Nicholas <ethannicholas@google.com>

TBR=kjlubick@google.com,brianosman@google.com,ethannicholas@google.com

Change-Id: Ic233203f3728a7285a1958c53567d915e56023af
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308757
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2020-08-07 18:09:49 +00:00

68 lines
2.2 KiB
C++

/*
* Copyright 2020 Google, LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkCanvas.h"
#include "include/core/SkPaint.h"
#include "include/core/SkSurface.h"
#include "include/effects/SkRuntimeEffect.h"
#include "src/gpu/GrShaderCaps.h"
#include "fuzz/Fuzz.h"
static constexpr size_t kReservedBytes = 256;
/**
* The fuzzer will take in the bytes and divide into two parts.
* original bytes : [... code bytes ... | 256 bytes]
* The first part is codeBytes, the original bytes minus 256 bytes, which will be treated
* as sksl code, intending to create SkRuntimeEffect.
* For the second part, it will first reserve 256 bytes and then allocate bytes with same size
* as effect->inputSize() to uniformBytes. The uniformBytes is intended to create makeShader().
* Note that if uniformBytes->size() != effect->inputSize() the shader won't be created.
*/
bool FuzzSkRuntimeEffect(sk_sp<SkData> bytes) {
if (bytes->size() < kReservedBytes) {
return false;
}
sk_sp<SkData> codeBytes = SkData::MakeSubset(bytes.get(), 0, bytes->size() - kReservedBytes);
SkRuntimeEffect::EffectResult tuple = SkRuntimeEffect::Make(
SkString((const char*) codeBytes->data(), codeBytes->size())
);
SkRuntimeEffect* effect = std::get<0>(tuple).get();
if (!effect || effect->inputSize() > kReservedBytes) { // if there is not enough uniform bytes
return false;
}
sk_sp<SkData> uniformBytes = SkData::MakeSubset(bytes.get(), bytes->size() - kReservedBytes,
effect->inputSize());
auto shader = effect->makeShader(uniformBytes, nullptr, 0, nullptr, false);
if (!shader) {
return false;
}
SkPaint paint;
paint.setShader(std::move(shader));
sk_sp<SkSurface> s = SkSurface::MakeRasterN32Premul(128, 128);
if (!s) {
return false;
}
s->getCanvas()->drawPaint(paint);
return true;
}
#if defined(IS_FUZZING_WITH_LIBFUZZER)
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size > 3000) {
return 0;
}
auto bytes = SkData::MakeWithoutCopy(data, size);
FuzzSkRuntimeEffect(bytes);
return 0;
}
#endif