Hash :
6f80f0f0
Author :
Date :
2022-08-06T02:29:19
Translator: Clean up the compile flag passing interface Historically, compile flags were sent to the translator as a bitmask. Recently, we were getting close to running out of bits. Additionally, direct-to-metal work had started to introduce constants to be passed to the translator, which were misplaced in ShBuiltInResources and Caps. Recent work on Pixel Local Storage adds even more constants, aggravating the situation. In this change, the interface to passing compile flags is reworked. A struct is passed (instead of a bitmask) that has one bit for each flag. This can be indefinitely extended. Additionally, the constants needed by metal and PLS are also placed in this struct. In turn, the backends can set these options directly, and don't have to hack them into Caps to further get hacked into ShBuiltInResources. Bug: angleproject:7559 Change-Id: If93f1e1b8818ad3a0ac708ab04ab93b4b397d114 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3812562 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com> Auto-Submit: Shahbaz Youssefi <syoussefi@chromium.org>
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// translator_fuzzer.cpp: A libfuzzer fuzzer for the shader translator.
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <memory>
#include <unordered_map>
#include "angle_gl.h"
#include "anglebase/no_destructor.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/util.h"
using namespace sh;
namespace
{
struct TranslatorCacheKey
{
bool operator==(const TranslatorCacheKey &other) const
{
return type == other.type && spec == other.spec && output == other.output;
}
uint32_t type = 0;
uint32_t spec = 0;
uint32_t output = 0;
};
} // anonymous namespace
namespace std
{
template <>
struct hash<TranslatorCacheKey>
{
std::size_t operator()(const TranslatorCacheKey &k) const
{
return (hash<uint32_t>()(k.type) << 1) ^ (hash<uint32_t>()(k.spec) >> 1) ^
hash<uint32_t>()(k.output);
}
};
} // namespace std
struct TCompilerDeleter
{
void operator()(TCompiler *compiler) const { DeleteCompiler(compiler); }
};
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
// Reserve some size for future compile options
const size_t kHeaderSize = 128;
if (size <= kHeaderSize)
{
return 0;
}
// Make sure the rest of data will be a valid C string so that we don't have to copy it.
if (data[size - 1] != 0)
{
return 0;
}
uint32_t type = *reinterpret_cast<const uint32_t *>(data);
uint32_t spec = *reinterpret_cast<const uint32_t *>(data + 4);
uint32_t output = *reinterpret_cast<const uint32_t *>(data + 8);
ShCompileOptions options = *reinterpret_cast<const ShCompileOptions *>(data + 12);
if (type != GL_FRAGMENT_SHADER && type != GL_VERTEX_SHADER)
{
return 0;
}
if (spec != SH_GLES2_SPEC && type != SH_WEBGL_SPEC && spec != SH_GLES3_SPEC &&
spec != SH_WEBGL2_SPEC)
{
return 0;
}
ShShaderOutput shaderOutput = static_cast<ShShaderOutput>(output);
bool hasUnsupportedOptions = false;
if (!IsOutputGLSL(shaderOutput) && !IsOutputESSL(shaderOutput))
{
hasUnsupportedOptions =
hasUnsupportedOptions || options.emulateAtan2FloatFunction || options.clampFragDepth ||
options.regenerateStructNames || options.rewriteRepeatedAssignToSwizzled ||
options.useUnusedStandardSharedBlocks || options.selectViewInNvGLSLVertexShader;
#if !defined(ANGLE_PLATFORM_APPLE)
hasUnsupportedOptions = hasUnsupportedOptions || options.rewriteFloatUnaryMinusOperator ||
options.addAndTrueToLoopCondition || options.rewriteDoWhileLoops ||
options.unfoldShortCircuit || options.rewriteRowMajorMatrices;
#endif
}
if (!IsOutputVulkan(shaderOutput))
{
hasUnsupportedOptions =
hasUnsupportedOptions || options.emulateSeamfulCubeMapSampling ||
options.useSpecializationConstant || options.addVulkanXfbEmulationSupportCode ||
options.roundOutputAfterDithering || options.addAdvancedBlendEquationsEmulation;
}
if (!IsOutputHLSL(shaderOutput))
{
hasUnsupportedOptions = hasUnsupportedOptions ||
options.expandSelectHLSLIntegerPowExpressions ||
options.allowTranslateUniformBlockToStructuredBuffer ||
options.rewriteIntegerUnaryMinusOperator;
}
// If there are any options not supported with this output, don't attempt to run the translator.
if (hasUnsupportedOptions)
{
return 0;
}
// Make sure the rest of the options are in a valid range.
options.pls.fragmentSynchronizationType = static_cast<ShFragmentSynchronizationType>(
static_cast<uint32_t>(options.pls.fragmentSynchronizationType) %
static_cast<uint32_t>(ShFragmentSynchronizationType::InvalidEnum));
std::vector<uint32_t> validOutputs;
validOutputs.push_back(SH_ESSL_OUTPUT);
validOutputs.push_back(SH_GLSL_COMPATIBILITY_OUTPUT);
validOutputs.push_back(SH_GLSL_130_OUTPUT);
validOutputs.push_back(SH_GLSL_140_OUTPUT);
validOutputs.push_back(SH_GLSL_150_CORE_OUTPUT);
validOutputs.push_back(SH_GLSL_330_CORE_OUTPUT);
validOutputs.push_back(SH_GLSL_400_CORE_OUTPUT);
validOutputs.push_back(SH_GLSL_410_CORE_OUTPUT);
validOutputs.push_back(SH_GLSL_420_CORE_OUTPUT);
validOutputs.push_back(SH_GLSL_430_CORE_OUTPUT);
validOutputs.push_back(SH_GLSL_440_CORE_OUTPUT);
validOutputs.push_back(SH_GLSL_450_CORE_OUTPUT);
validOutputs.push_back(SH_HLSL_3_0_OUTPUT);
validOutputs.push_back(SH_HLSL_4_1_OUTPUT);
validOutputs.push_back(SH_HLSL_4_0_FL9_3_OUTPUT);
bool found = false;
for (auto valid : validOutputs)
{
found = found || (valid == output);
}
if (!found)
{
return 0;
}
size -= kHeaderSize;
data += kHeaderSize;
sh::InitializeGlslang();
if (!sh::Initialize())
{
return 0;
}
TranslatorCacheKey key;
key.type = type;
key.spec = spec;
key.output = output;
using UniqueTCompiler = std::unique_ptr<TCompiler, TCompilerDeleter>;
static angle::base::NoDestructor<angle::HashMap<TranslatorCacheKey, UniqueTCompiler>>
translators;
if (translators->find(key) == translators->end())
{
UniqueTCompiler translator(
ConstructCompiler(type, static_cast<ShShaderSpec>(spec), shaderOutput));
if (translator == nullptr)
{
return 0;
}
ShBuiltInResources resources;
sh::InitBuiltInResources(&resources);
// Enable all the extensions to have more coverage
resources.OES_standard_derivatives = 1;
resources.OES_EGL_image_external = 1;
resources.OES_EGL_image_external_essl3 = 1;
resources.NV_EGL_stream_consumer_external = 1;
resources.ARB_texture_rectangle = 1;
resources.EXT_blend_func_extended = 1;
resources.EXT_draw_buffers = 1;
resources.EXT_frag_depth = 1;
resources.EXT_shader_texture_lod = 1;
resources.EXT_shader_framebuffer_fetch = 1;
resources.NV_shader_framebuffer_fetch = 1;
resources.ARM_shader_framebuffer_fetch = 1;
resources.EXT_YUV_target = 1;
resources.APPLE_clip_distance = 1;
resources.MaxDualSourceDrawBuffers = 1;
resources.EXT_gpu_shader5 = 1;
resources.MaxClipDistances = 1;
resources.EXT_shadow_samplers = 1;
resources.EXT_clip_cull_distance = 1;
resources.EXT_primitive_bounding_box = 1;
resources.OES_primitive_bounding_box = 1;
if (!translator->Init(resources))
{
return 0;
}
(*translators)[key] = std::move(translator);
}
auto &translator = (*translators)[key];
const char *shaderStrings[] = {reinterpret_cast<const char *>(data)};
translator->compile(shaderStrings, 1, options);
return 0;
}