KhronosGroup / glslang

Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
Other
3.08k stars 845 forks source link

Warn about bogus comparisons #775

Open gfxstrand opened 7 years ago

gfxstrand commented 7 years ago

Today, someone filed a bug against our Vulkan driver saying that it hangs when you try to run the following compute shader compiled, I believe, with glslang:

#version 450
#extension GL_ARB_separate_shader_objects : enable

shared int roi[8];

void main() {
  for (uint i = 7; i >= 0; i--) {
    roi[i] = 1;
  }
}

The problem with the shader was that they used an unsigned integer for i and a condition of i >= 0 which always evaluates to true yielding an infinite loop. Since we do nothing in our Vulkan driver to guard against infinite loops (why would we?) they got a GPU hang.

It would be really nice if glslang could emit some sort of a warning when someone tries to do an unsafe comparison in a similar way to what GCC does with -Wsign-compare. If it had such a warning, they probably would never have even submitted the bug.

elmar-k commented 11 months ago

Hi, unfortunately I just lost two full days hunting down a similar case in my own shader. This 'uint' comparison pitfalls happens often when converting OpenCL code to GLSL/Vulkan, because things that work with 'int' in OpenCL often require 'uint' in GLSL/Vulkan, and then you overlook the comparison. Catching such simple bugs would really be important! Thanks, Elmar