skia2/include/sksl/SkSLErrorReporter.h
Brian Osman 00aa1a9259 Prevent 'binding' and 'set' on struct/interface block fields
I should have realized the fuzzer would find this assert when I added
it. Now the front-end rejects these layout qualifiers on both struct
fields and interface block fields. LayoutInInterfaceBlock.sksl is a
reformatted version of the fuzzer input. LayoutInStruct is hand-crafted
to trigger the same failure on a different code path. Both would
previously assert in the SPIRV generator. Now, neither one gets that
far.

Bug: oss-fuzz:41347
Change-Id: Iff69d8f5482da7b772e9331c4fd2d58e89813c46
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/476396
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2021-11-29 15:53:57 +00:00

116 lines
2.6 KiB
C++

/*
* Copyright 2021 Google LLC.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SKSL_ERROR_REPORTER
#define SKSL_ERROR_REPORTER
#include "include/core/SkStringView.h"
#include "include/core/SkTypes.h"
#include "include/private/SkSLString.h"
#include <string>
#include <vector>
namespace SkSL {
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
class PositionInfo {
public:
PositionInfo(const char* file = nullptr, int line = -1)
: fFile(file)
, fLine(line) {}
#if __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)
static PositionInfo Capture(const char* file = __builtin_FILE(), int line = __builtin_LINE()) {
return PositionInfo(file, line);
}
#else
static PositionInfo Capture() { return PositionInfo(); }
#endif // __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE)
const char* file_name() const {
return fFile;
}
int line() const {
return fLine;
}
private:
const char* fFile = nullptr;
int32_t fLine = -1;
};
/**
* Class which is notified in the event of an error.
*/
class ErrorReporter {
public:
ErrorReporter() {}
virtual ~ErrorReporter() {
SkASSERT(fPendingErrors.empty());
}
void error(skstd::string_view msg, PositionInfo position);
/**
* Reports an error message at the given line of the source text. Errors reported
* with a line of -1 will be queued until line number information can be determined.
*/
void error(int line, skstd::string_view msg);
const char* source() const { return fSource; }
void setSource(const char* source) { fSource = source; }
void reportPendingErrors(PositionInfo pos) {
for (const String& msg : fPendingErrors) {
this->handleError(msg, pos);
}
fPendingErrors.clear();
}
int errorCount() const {
return fErrorCount;
}
void resetErrorCount() {
fErrorCount = 0;
}
protected:
/**
* Called when an error is reported.
*/
virtual void handleError(skstd::string_view msg, PositionInfo position) = 0;
private:
PositionInfo position(int offset) const;
const char* fSource = nullptr;
std::vector<String> fPendingErrors;
int fErrorCount = 0;
};
/**
* Error reporter for tests that need an SkSL context; aborts immediately if an error is reported.
*/
class TestingOnly_AbortErrorReporter : public ErrorReporter {
public:
void handleError(skstd::string_view msg, PositionInfo pos) override {
SK_ABORT("%.*s", (int)msg.length(), msg.data());
}
};
} // namespace SkSL
#endif