T81340: UBSan: load of value .. not valid for GPU enum type

The underlying type of the enum cannot be fixed here due to its usage
in C code.

All the values possible in the width of the underlying type are not
valid for an enum.
Only 0 to (2*max - 1) if all enumerators are unsigned.
So the macro asks for the biggest value among the //listed// ones.
If any enumerator C is set to say `A|B`, then C would be the maximum.
(2*max-1) is used as the mask.

The warnings (for each enum modified in this commit):
GPU_vertex_buffer.h:43:1: runtime error: load of value 4294967291
which is not a valid value for type 'GPUVertBufStatus'

https://github.com/llvm/llvm-project/commit/1c2c9867

Ref T81340

Reviewed By: fclem

Differential Revision: https://developer.blender.org/D9067
This commit is contained in:
Ankit Meel 2020-10-01 23:51:01 +05:30
parent 569510d704
commit a31a87f894
Notes: blender-bot 2024-05-02 21:33:31 +02:00
Referenced by commit 83e91485d0, GPU: Fix NOT ~ operator for eGPUSamplerState
Referenced by issue #81572, Viewport Shading Cavity Glitch
Referenced by issue #81340, UBSan: several runtime errors at launch.
8 changed files with 13 additions and 12 deletions

View File

@ -782,7 +782,8 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size);
/* Useful to port C code using enums to C++ where enums are strongly typed.
* To use after the enum declaration. */
# define ENUM_OPERATORS(_enum_type) \
/* If any enumerator `C` is set to say `A|B`, then `C` would be the max enum value. */
# define ENUM_OPERATORS(_enum_type, _max_enum_value) \
inline constexpr _enum_type operator|(_enum_type a, _enum_type b) \
{ \
return static_cast<_enum_type>(static_cast<int>(a) | b); \
@ -793,7 +794,7 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size);
} \
inline constexpr _enum_type operator~(_enum_type a) \
{ \
return static_cast<_enum_type>(~static_cast<int>(a)); \
return static_cast<_enum_type>(~static_cast<int>(a) & (2 * _max_enum_value - 1)); \
} \
inline _enum_type &operator|=(_enum_type &a, _enum_type b) \
{ \
@ -806,7 +807,7 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size);
#else
/* Output nothing. */
# define ENUM_OPERATORS(_type)
# define ENUM_OPERATORS(_type, _max)
#endif
/** \} */

View File

@ -66,7 +66,7 @@ typedef enum eGPUBatchFlag {
BLI_STATIC_ASSERT(GPU_BATCH_OWNS_INDEX < GPU_BATCH_INIT,
"eGPUBatchFlag: Error: status flags are shadowed by the ownership bits!")
ENUM_OPERATORS(eGPUBatchFlag)
ENUM_OPERATORS(eGPUBatchFlag, GPU_BATCH_DIRTY)
#ifdef __cplusplus
extern "C" {

View File

@ -38,7 +38,7 @@ typedef enum eGPUFrameBufferBits {
GPU_STENCIL_BIT = (1 << 2),
} eGPUFrameBufferBits;
ENUM_OPERATORS(eGPUFrameBufferBits)
ENUM_OPERATORS(eGPUFrameBufferBits, GPU_STENCIL_BIT)
#ifdef __cplusplus
extern "C" {

View File

@ -39,7 +39,7 @@ typedef enum eGPUDeviceType {
GPU_DEVICE_ANY = (0xff),
} eGPUDeviceType;
ENUM_OPERATORS(eGPUDeviceType)
ENUM_OPERATORS(eGPUDeviceType, GPU_DEVICE_ANY)
typedef enum eGPUOSType {
GPU_OS_WIN = (1 << 8),

View File

@ -33,7 +33,7 @@ typedef enum eGPUWriteMask {
GPU_WRITE_COLOR = (GPU_WRITE_RED | GPU_WRITE_GREEN | GPU_WRITE_BLUE | GPU_WRITE_ALPHA),
} eGPUWriteMask;
ENUM_OPERATORS(eGPUWriteMask)
ENUM_OPERATORS(eGPUWriteMask, GPU_WRITE_COLOR)
typedef enum eGPUBarrier {
GPU_BARRIER_NONE = 0,
@ -41,7 +41,7 @@ typedef enum eGPUBarrier {
GPU_BARRIER_TEXTURE_FETCH = (1 << 1),
} eGPUBarrier;
ENUM_OPERATORS(eGPUBarrier)
ENUM_OPERATORS(eGPUBarrier, GPU_BARRIER_TEXTURE_FETCH)
/**
* Defines the fixed pipeline blending equation.

View File

@ -62,7 +62,7 @@ typedef enum eGPUSamplerState {
GPU_SAMPLER_MAX = (GPU_SAMPLER_ICON + 1),
} eGPUSamplerState;
ENUM_OPERATORS(eGPUSamplerState)
ENUM_OPERATORS(eGPUSamplerState, GPU_SAMPLER_REPEAT)
#ifdef __cplusplus
extern "C" {

View File

@ -40,7 +40,7 @@ typedef enum {
GPU_VERTBUF_DATA_UPLOADED = (1 << 2),
} GPUVertBufStatus;
ENUM_OPERATORS(GPUVertBufStatus)
ENUM_OPERATORS(GPUVertBufStatus, GPU_VERTBUF_DATA_UPLOADED)
#ifdef __cplusplus
extern "C" {

View File

@ -42,7 +42,7 @@ typedef enum eGPUTextureFormatFlag {
GPU_FORMAT_DEPTH_STENCIL = (GPU_FORMAT_DEPTH | GPU_FORMAT_STENCIL),
} eGPUTextureFormatFlag;
ENUM_OPERATORS(eGPUTextureFormatFlag)
ENUM_OPERATORS(eGPUTextureFormatFlag, GPU_FORMAT_DEPTH_STENCIL)
typedef enum eGPUTextureType {
GPU_TEXTURE_1D = (1 << 0),
@ -57,7 +57,7 @@ typedef enum eGPUTextureType {
GPU_TEXTURE_CUBE_ARRAY = (GPU_TEXTURE_CUBE | GPU_TEXTURE_ARRAY),
} eGPUTextureType;
ENUM_OPERATORS(eGPUTextureType)
ENUM_OPERATORS(eGPUTextureType, GPU_TEXTURE_CUBE_ARRAY)
#ifdef DEBUG
# define DEBUG_NAME_LEN 64