Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/src/compiler/fuzz/translator_fuzzer.cpp
1693 views
1
//
2
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
3
// Use of this source code is governed by a BSD-style license that can be
4
// found in the LICENSE file.
5
//
6
7
// translator_fuzzer.cpp: A libfuzzer fuzzer for the shader translator.
8
9
#include <cstddef>
10
#include <cstdint>
11
#include <iostream>
12
#include <memory>
13
#include <unordered_map>
14
15
#include "angle_gl.h"
16
#include "anglebase/no_destructor.h"
17
#include "compiler/translator/Compiler.h"
18
#include "compiler/translator/util.h"
19
20
using namespace sh;
21
22
namespace
23
{
24
25
// Options supported by any output
26
constexpr ShCompileOptions kCommonOptions =
27
SH_VALIDATE | SH_VALIDATE_LOOP_INDEXING | SH_INTERMEDIATE_TREE | SH_OBJECT_CODE | SH_VARIABLES |
28
SH_LINE_DIRECTIVES | SH_SOURCE_PATH | SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3 |
29
SH_EMULATE_ABS_INT_FUNCTION | SH_ENFORCE_PACKING_RESTRICTIONS | SH_CLAMP_INDIRECT_ARRAY_BOUNDS |
30
SH_LIMIT_EXPRESSION_COMPLEXITY | SH_LIMIT_CALL_STACK_DEPTH | SH_INIT_GL_POSITION |
31
SH_INIT_OUTPUT_VARIABLES | SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS |
32
SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL | SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL |
33
SH_REWRITE_TEXELFETCHOFFSET_TO_TEXELFETCH | SH_EMULATE_ISNAN_FLOAT_FUNCTION |
34
SH_INITIALIZE_UNINITIALIZED_LOCALS | SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW |
35
SH_CLAMP_POINT_SIZE | SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES |
36
SH_SKIP_D3D_CONSTANT_REGISTER_ZERO | SH_EMULATE_GL_DRAW_ID | SH_INIT_SHARED_VARIABLES |
37
SH_FORCE_ATOMIC_VALUE_RESOLUTION | SH_EMULATE_GL_BASE_VERTEX_BASE_INSTANCE |
38
SH_TAKE_VIDEO_TEXTURE_AS_EXTERNAL_OES | SH_VALIDATE_AST | SH_ADD_BASE_VERTEX_TO_VERTEX_ID |
39
SH_REMOVE_DYNAMIC_INDEXING_OF_SWIZZLED_VECTOR | SH_DISABLE_ARB_TEXTURE_RECTANGLE |
40
SH_IGNORE_PRECISION_QUALIFIERS | SH_FORCE_SHADER_PRECISION_HIGHP_TO_MEDIUMP;
41
42
// Options supported by GLSL or ESSL only
43
constexpr ShCompileOptions kGLSLOrESSLOnlyOptions =
44
SH_EMULATE_ATAN2_FLOAT_FUNCTION | SH_CLAMP_FRAG_DEPTH | SH_REGENERATE_STRUCT_NAMES |
45
SH_REWRITE_REPEATED_ASSIGN_TO_SWIZZLED | SH_USE_UNUSED_STANDARD_SHARED_BLOCKS |
46
SH_SELECT_VIEW_IN_NV_GLSL_VERTEX_SHADER;
47
48
#if defined(ANGLE_PLATFORM_APPLE)
49
// Options supported by GLSL only on mac
50
constexpr ShCompileOptions kGLSLMacOnlyOptions =
51
SH_REWRITE_FLOAT_UNARY_MINUS_OPERATOR | SH_ADD_AND_TRUE_TO_LOOP_CONDITION |
52
SH_REWRITE_DO_WHILE_LOOPS | SH_UNFOLD_SHORT_CIRCUIT | SH_REWRITE_ROW_MAJOR_MATRICES;
53
#endif
54
55
// Options supported by Vulkan GLSL only
56
constexpr ShCompileOptions kVulkanGLSLOnlyOptions =
57
SH_ADD_PRE_ROTATION | SH_EMULATE_SEAMFUL_CUBE_MAP_SAMPLING |
58
SH_ADD_BRESENHAM_LINE_RASTER_EMULATION | SH_EARLY_FRAGMENT_TESTS_OPTIMIZATION |
59
SH_USE_SPECIALIZATION_CONSTANT | SH_ADD_VULKAN_XFB_EMULATION_SUPPORT_CODE;
60
61
// Options supported by HLSL output only
62
constexpr ShCompileOptions kHLSLOnlyOptions = SH_EXPAND_SELECT_HLSL_INTEGER_POW_EXPRESSIONS |
63
SH_ALLOW_TRANSLATE_UNIFORM_BLOCK_TO_STRUCTUREDBUFFER |
64
SH_REWRITE_INTEGER_UNARY_MINUS_OPERATOR;
65
66
struct TranslatorCacheKey
67
{
68
bool operator==(const TranslatorCacheKey &other) const
69
{
70
return type == other.type && spec == other.spec && output == other.output;
71
}
72
73
uint32_t type = 0;
74
uint32_t spec = 0;
75
uint32_t output = 0;
76
};
77
} // anonymous namespace
78
79
namespace std
80
{
81
82
template <>
83
struct hash<TranslatorCacheKey>
84
{
85
std::size_t operator()(const TranslatorCacheKey &k) const
86
{
87
return (hash<uint32_t>()(k.type) << 1) ^ (hash<uint32_t>()(k.spec) >> 1) ^
88
hash<uint32_t>()(k.output);
89
}
90
};
91
} // namespace std
92
93
struct TCompilerDeleter
94
{
95
void operator()(TCompiler *compiler) const { DeleteCompiler(compiler); }
96
};
97
98
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
99
{
100
// Reserve some size for future compile options
101
const size_t kHeaderSize = 128;
102
103
if (size <= kHeaderSize)
104
{
105
return 0;
106
}
107
108
// Make sure the rest of data will be a valid C string so that we don't have to copy it.
109
if (data[size - 1] != 0)
110
{
111
return 0;
112
}
113
114
uint32_t type = *reinterpret_cast<const uint32_t *>(data);
115
uint32_t spec = *reinterpret_cast<const uint32_t *>(data + 4);
116
uint32_t output = *reinterpret_cast<const uint32_t *>(data + 8);
117
uint64_t options = *reinterpret_cast<const uint64_t *>(data + 12);
118
119
if (type != GL_FRAGMENT_SHADER && type != GL_VERTEX_SHADER)
120
{
121
return 0;
122
}
123
124
if (spec != SH_GLES2_SPEC && type != SH_WEBGL_SPEC && spec != SH_GLES3_SPEC &&
125
spec != SH_WEBGL2_SPEC)
126
{
127
return 0;
128
}
129
130
ShShaderOutput shaderOutput = static_cast<ShShaderOutput>(output);
131
132
ShCompileOptions supportedOptions = kCommonOptions;
133
134
if (IsOutputGLSL(shaderOutput) || IsOutputESSL(shaderOutput))
135
{
136
supportedOptions |= kGLSLOrESSLOnlyOptions;
137
#if defined(ANGLE_PLATFORM_APPLE)
138
supportedOptions |= kGLSLMacOnlyOptions;
139
#endif
140
}
141
else if (IsOutputVulkan(shaderOutput))
142
{
143
supportedOptions |= kVulkanGLSLOnlyOptions;
144
}
145
else if (IsOutputHLSL(shaderOutput))
146
{
147
supportedOptions |= kHLSLOnlyOptions;
148
}
149
150
// If there are any options not supported with this output, don't attempt to run the translator.
151
if ((options & ~supportedOptions) != 0)
152
{
153
return 0;
154
}
155
156
std::vector<uint32_t> validOutputs;
157
validOutputs.push_back(SH_ESSL_OUTPUT);
158
validOutputs.push_back(SH_GLSL_COMPATIBILITY_OUTPUT);
159
validOutputs.push_back(SH_GLSL_130_OUTPUT);
160
validOutputs.push_back(SH_GLSL_140_OUTPUT);
161
validOutputs.push_back(SH_GLSL_150_CORE_OUTPUT);
162
validOutputs.push_back(SH_GLSL_330_CORE_OUTPUT);
163
validOutputs.push_back(SH_GLSL_400_CORE_OUTPUT);
164
validOutputs.push_back(SH_GLSL_410_CORE_OUTPUT);
165
validOutputs.push_back(SH_GLSL_420_CORE_OUTPUT);
166
validOutputs.push_back(SH_GLSL_430_CORE_OUTPUT);
167
validOutputs.push_back(SH_GLSL_440_CORE_OUTPUT);
168
validOutputs.push_back(SH_GLSL_450_CORE_OUTPUT);
169
validOutputs.push_back(SH_HLSL_3_0_OUTPUT);
170
validOutputs.push_back(SH_HLSL_4_1_OUTPUT);
171
validOutputs.push_back(SH_HLSL_4_0_FL9_3_OUTPUT);
172
bool found = false;
173
for (auto valid : validOutputs)
174
{
175
found = found || (valid == output);
176
}
177
if (!found)
178
{
179
return 0;
180
}
181
182
size -= kHeaderSize;
183
data += kHeaderSize;
184
185
sh::InitializeGlslang();
186
if (!sh::Initialize())
187
{
188
return 0;
189
}
190
191
TranslatorCacheKey key;
192
key.type = type;
193
key.spec = spec;
194
key.output = output;
195
196
using UniqueTCompiler = std::unique_ptr<TCompiler, TCompilerDeleter>;
197
static angle::base::NoDestructor<angle::HashMap<TranslatorCacheKey, UniqueTCompiler>>
198
translators;
199
200
if (translators->find(key) == translators->end())
201
{
202
UniqueTCompiler translator(
203
ConstructCompiler(type, static_cast<ShShaderSpec>(spec), shaderOutput));
204
205
if (translator == nullptr)
206
{
207
return 0;
208
}
209
210
ShBuiltInResources resources;
211
sh::InitBuiltInResources(&resources);
212
213
// Enable all the extensions to have more coverage
214
resources.OES_standard_derivatives = 1;
215
resources.OES_EGL_image_external = 1;
216
resources.OES_EGL_image_external_essl3 = 1;
217
resources.NV_EGL_stream_consumer_external = 1;
218
resources.ARB_texture_rectangle = 1;
219
resources.EXT_blend_func_extended = 1;
220
resources.EXT_draw_buffers = 1;
221
resources.EXT_frag_depth = 1;
222
resources.EXT_shader_texture_lod = 1;
223
resources.EXT_shader_framebuffer_fetch = 1;
224
resources.NV_shader_framebuffer_fetch = 1;
225
resources.ARM_shader_framebuffer_fetch = 1;
226
resources.EXT_YUV_target = 1;
227
resources.APPLE_clip_distance = 1;
228
resources.MaxDualSourceDrawBuffers = 1;
229
resources.EXT_gpu_shader5 = 1;
230
resources.MaxClipDistances = 1;
231
resources.EXT_shadow_samplers = 1;
232
resources.EXT_clip_cull_distance = 1;
233
resources.EXT_primitive_bounding_box = 1;
234
235
if (!translator->Init(resources))
236
{
237
return 0;
238
}
239
240
(*translators)[key] = std::move(translator);
241
}
242
243
auto &translator = (*translators)[key];
244
245
const char *shaderStrings[] = {reinterpret_cast<const char *>(data)};
246
translator->compile(shaderStrings, 1, options);
247
248
return 0;
249
}
250
251