KhronosGroup / KhronosGroup/glslang

Crash using below code

Open
#3,136 5 comments 0 reactions 0 assignees View on GitHub
bug SPIR-V
Dominant language
C++
Stars
3.6k
Forks
989
Avg merge
1d 2h
Merged PRs (30d)
31

Description

Hello, I am using git tag version 11.13.0 and I have below code
```c++
std::vector glslToSpirv(const std::vector& data,
EShLanguage shaderStage, const char* entryPoint) {
static bool glslangInitialized = false;

if (!glslangInitialized) {
glslang::InitializeProcess();
glslangInitialized = true;
}

glslang::TShader tshader(shaderStage);
const char* glslCStr = data.data();
tshader.setStrings(&glslCStr, 1);

glslang::EshTargetClientVersion clientVersion = glslang::EShTargetVulkan_1_1;
glslang::EShTargetLanguageVersion langVersion = glslang::EShTargetSpv_1_3;

tshader.setEnvInput(glslang::EShSourceGlsl, shaderStage,
glslang::EShClientVulkan,
460); // 100 means version glsl version 1.0 (as defined
// by KHR_vulkan_glsl)

tshader.setEnvClient(glslang::EShClientVulkan, clientVersion);
tshader.setEnvTarget(glslang::EShTargetSpv, langVersion);

#ifdef _DEBUG
tshader.setDebugInfo(true);
#endif

tshader.setEntryPoint(entryPoint);
tshader.setSourceEntryPoint(entryPoint);

const TBuiltInResource* resources = GetDefaultResources();
const EShMessages messages =
static_cast(EShMsgSpvRules | EShMsgVulkanRules);
glslang::TShader::ForbidIncluder includer;

std::string preprocessedGLSL;
if (!tshader.preprocess(resources, 460, ENoProfile, false, false, messages,
&preprocessedGLSL, includer)) {
std::cout << "Preprocessing failed for shader " << std::endl;
std::cout << tshader.getInfoLog() << std::endl;
std::cout << tshader.getInfoDebugLog() << std::endl;
ASSERT(false, "preprocess failed");
return std::vector();
}

const char* preprocessedGLSLStr = preprocessedGLSL.c_str();
tshader.setStrings(&preprocessedGLSLStr, 1);

if (!tshader.parse(resources, 460, false, messages)) {
std::cout << "Parsing failed for shader " << std::endl;
std::cout << tshader.getInfoLog() << std::endl;
std::cout << tshader.getInfoDebugLog() << std::endl;
ASSERT(false, "parse failed");
return std::vector();
}

glslang::SpvOptions options;

#ifdef _DEBUG
options.generateDebugInfo = false;
options.disableOptimizer = false;
options.optimizeSize = true;
options.stripDebugInfo = true;
#else
options.disableOptimizer = false;
options.disableOptimizer = false;
options.optimizeSize = true;
options.stripDebugInfo = true;
#endif

glslang::TProgram program;
program.addShader(&tshader);
if (!program.link(messages)) {
std::cout << "Parsing failed for shader " << std::endl;
std::cout << program.getInfoLog() << std::endl;
std::cout << program.getInfoDebugLog() << std::endl;
ASSERT(false, "link failed");
}

std::vector spirvData;
spv::SpvBuildLogger spvLogger;
glslang::GlslangToSpv(*program.getIntermediate(shaderStage), spirvData,
&spvLogger, &options);

std::vector byteCode;
byteCode.resize(spirvData.size() * (sizeof(uint32_t) / sizeof(char)));
std::memcpy(byteCode.data(), spirvData.data(), byteCode.size());

return byteCode;
}
```
As soon as I add if/else to my shader it crashes? Below is my shader code
```glsl
#version 460

layout(location = 0) in vec4 inColor;
layout(location = 1) in vec2 texCoords;
layout(location = 0) out vec4 outColor;

layout (set = 0, binding = 0) uniform texture2D textureCheckered;
layout (set = 1, binding = 0) uniform sampler samplerTex;

void main() {
float myval = 1.0;
if(myval == 1.0) {
const vec4 tex = texture(sampler2D(textureCheckered, samplerTex), texCoords) * inColor;
outColor = tex;
} else {
vec4 tex = texture(sampler2D(textureCheckered, samplerTex), texCoords) * inColor;
tex.r = 0.0;
outColor = tex;
}
}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with the provided C++ function and GLSL shader, starting at TShader::preprocess and following the parse, TProgram::link, and GlslangToSpv calls. Compare the behavior with and without the if/else block and inspect the reported info logs and debug logs. Done means the cause of the crash is identified and the same shader path no longer crashes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.