Mesa (main): r300: fix UB caused by 1 << 31 and 2 << 30

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Oct 5 19:17:19 UTC 2021


Module: Mesa
Branch: main
Commit: a3f553beab095ba06db934b33af8c9559bf329b7
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=a3f553beab095ba06db934b33af8c9559bf329b7

Author: Filip Gawin <filip.gawin at zoho.com>
Date:   Sat Oct  2 18:28:17 2021 +0200

r300: fix UB caused by 1 << 31 and 2 << 30

Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13158>

---

 src/gallium/drivers/r300/compiler/r3xx_vertprog.c      |  8 ++++----
 src/gallium/drivers/r300/compiler/radeon_compiler.c    | 18 +++++++++---------
 src/gallium/drivers/r300/compiler/radeon_program_tex.c |  2 +-
 src/gallium/drivers/r300/r300_emit.c                   |  2 +-
 src/gallium/drivers/r300/r300_reg.h                    | 14 +++++++-------
 src/gallium/drivers/r300/r300_state_derived.c          |  8 ++++----
 src/gallium/drivers/r300/r300_tgsi_to_rc.c             | 12 ++++++------
 7 files changed, 32 insertions(+), 32 deletions(-)

diff --git a/src/gallium/drivers/r300/compiler/r3xx_vertprog.c b/src/gallium/drivers/r300/compiler/r3xx_vertprog.c
index 63cb447363b..88b8dfcd9fc 100644
--- a/src/gallium/drivers/r300/compiler/r3xx_vertprog.c
+++ b/src/gallium/drivers/r300/compiler/r3xx_vertprog.c
@@ -755,8 +755,8 @@ static void rc_vs_add_artificial_outputs(struct radeon_compiler *c, void *user)
 	int i;
 
 	for(i = 0; i < 32; ++i) {
-		if ((compiler->RequiredOutputs & (1 << i)) &&
-		    !(compiler->Base.Program.OutputsWritten & (1 << i))) {
+		if ((compiler->RequiredOutputs & (1U << i)) &&
+		    !(compiler->Base.Program.OutputsWritten & (1U << i))) {
 			struct rc_instruction * inst = rc_insert_new_instruction(&compiler->Base, compiler->Base.Program.Instructions.Prev);
 			inst->U.I.Opcode = RC_OPCODE_MOV;
 
@@ -768,7 +768,7 @@ static void rc_vs_add_artificial_outputs(struct radeon_compiler *c, void *user)
 			inst->U.I.SrcReg[0].Index = 0;
 			inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
 
-			compiler->Base.Program.OutputsWritten |= 1 << i;
+			compiler->Base.Program.OutputsWritten |= 1U << i;
 		}
 	}
 }
@@ -780,7 +780,7 @@ static void dataflow_outputs_mark_used(void * userdata, void * data,
 	int i;
 
 	for(i = 0; i < 32; ++i) {
-		if (c->RequiredOutputs & (1 << i))
+		if (c->RequiredOutputs & (1U << i))
 			callback(data, i, RC_MASK_XYZW);
 	}
 }
diff --git a/src/gallium/drivers/r300/compiler/radeon_compiler.c b/src/gallium/drivers/r300/compiler/radeon_compiler.c
index 081cd2d0d55..78902d98068 100644
--- a/src/gallium/drivers/r300/compiler/radeon_compiler.c
+++ b/src/gallium/drivers/r300/compiler/radeon_compiler.c
@@ -123,12 +123,12 @@ void rc_calculate_inputs_outputs(struct radeon_compiler * c)
 
 		for (i = 0; i < opcode->NumSrcRegs; ++i) {
 			if (inst->U.I.SrcReg[i].File == RC_FILE_INPUT)
-				c->Program.InputsRead |= 1 << inst->U.I.SrcReg[i].Index;
+				c->Program.InputsRead |= 1U << inst->U.I.SrcReg[i].Index;
 		}
 
 		if (opcode->HasDstReg) {
 			if (inst->U.I.DstReg.File == RC_FILE_OUTPUT)
-				c->Program.OutputsWritten |= 1 << inst->U.I.DstReg.Index;
+				c->Program.OutputsWritten |= 1U << inst->U.I.DstReg.Index;
 		}
 	}
 }
@@ -141,7 +141,7 @@ void rc_move_input(struct radeon_compiler * c, unsigned input, struct rc_src_reg
 {
 	struct rc_instruction * inst;
 
-	c->Program.InputsRead &= ~(1 << input);
+	c->Program.InputsRead &= ~(1U << input);
 
 	for(inst = c->Program.Instructions.Next; inst != &c->Program.Instructions; inst = inst->Next) {
 		const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
@@ -157,7 +157,7 @@ void rc_move_input(struct radeon_compiler * c, unsigned input, struct rc_src_reg
 					inst->U.I.SrcReg[i].Abs = new_input.Abs;
 				}
 
-				c->Program.InputsRead |= 1 << new_input.Index;
+				c->Program.InputsRead |= 1U << new_input.Index;
 			}
 		}
 	}
@@ -173,7 +173,7 @@ void rc_move_output(struct radeon_compiler * c, unsigned output, unsigned new_ou
 {
 	struct rc_instruction * inst;
 
-	c->Program.OutputsWritten &= ~(1 << output);
+	c->Program.OutputsWritten &= ~(1U << output);
 
 	for(inst = c->Program.Instructions.Next; inst != &c->Program.Instructions; inst = inst->Next) {
 		const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
@@ -183,7 +183,7 @@ void rc_move_output(struct radeon_compiler * c, unsigned output, unsigned new_ou
 				inst->U.I.DstReg.Index = new_output;
 				inst->U.I.DstReg.WriteMask &= writemask;
 
-				c->Program.OutputsWritten |= 1 << new_output;
+				c->Program.OutputsWritten |= 1U << new_output;
 			}
 		}
 	}
@@ -227,7 +227,7 @@ void rc_copy_output(struct radeon_compiler * c, unsigned output, unsigned dup_ou
 	inst->U.I.SrcReg[0].Index = tempreg;
 	inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
 
-	c->Program.OutputsWritten |= 1 << dup_output;
+	c->Program.OutputsWritten |= 1U << dup_output;
 }
 
 
@@ -243,8 +243,8 @@ void rc_transform_fragment_wpos(struct radeon_compiler * c, unsigned wpos, unsig
 	struct rc_instruction * inst_mad;
 	struct rc_instruction * inst;
 
-	c->Program.InputsRead &= ~(1 << wpos);
-	c->Program.InputsRead |= 1 << new_input;
+	c->Program.InputsRead &= ~(1U << wpos);
+	c->Program.InputsRead |= 1U << new_input;
 
 	/* perspective divide */
 	inst_rcp = rc_insert_new_instruction(c, &c->Program.Instructions);
diff --git a/src/gallium/drivers/r300/compiler/radeon_program_tex.c b/src/gallium/drivers/r300/compiler/radeon_program_tex.c
index 17d6ee9aebb..a7b3ad74809 100644
--- a/src/gallium/drivers/r300/compiler/radeon_program_tex.c
+++ b/src/gallium/drivers/r300/compiler/radeon_program_tex.c
@@ -140,7 +140,7 @@ int radeonTransformTEX(
 
 	/* ARB_shadow & EXT_shadow_funcs */
 	if (inst->U.I.Opcode != RC_OPCODE_KIL &&
-		((c->Program.ShadowSamplers & (1 << inst->U.I.TexSrcUnit)) ||
+		((c->Program.ShadowSamplers & (1U << inst->U.I.TexSrcUnit)) ||
 		 (compiler->state.unit[inst->U.I.TexSrcUnit].compare_mode_enabled))) {
 		rc_compare_func comparefunc = compiler->state.unit[inst->U.I.TexSrcUnit].texture_compare_func;
 
diff --git a/src/gallium/drivers/r300/r300_emit.c b/src/gallium/drivers/r300/r300_emit.c
index 374377b5bd1..c91f9851a2e 100644
--- a/src/gallium/drivers/r300/r300_emit.c
+++ b/src/gallium/drivers/r300/r300_emit.c
@@ -1353,7 +1353,7 @@ validate:
     if (r300->textures_state.dirty) {
         /* ...textures... */
         for (i = 0; i < texstate->count; i++) {
-            if (!(texstate->tx_enable & (1 << i))) {
+            if (!(texstate->tx_enable & (1U << i))) {
                 continue;
             }
 
diff --git a/src/gallium/drivers/r300/r300_reg.h b/src/gallium/drivers/r300/r300_reg.h
index d2d3fc6556e..56be6d690d9 100644
--- a/src/gallium/drivers/r300/r300_reg.h
+++ b/src/gallium/drivers/r300/r300_reg.h
@@ -305,7 +305,7 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 #define R300_VAP_PSC_SGN_NORM_CNTL                0x21dc
 #   define SGN_NORM_ZERO                                    0
 #   define SGN_NORM_ZERO_CLAMP_MINUS_ONE                    1
-#   define SGN_NORM_NO_ZERO                                 2
+#   define SGN_NORM_NO_ZERO                                 2U
 #   define R300_SGN_NORM_NO_ZERO (SGN_NORM_NO_ZERO | \
         (SGN_NORM_NO_ZERO << 2) | (SGN_NORM_NO_ZERO << 4) | \
         (SGN_NORM_NO_ZERO << 6) | (SGN_NORM_NO_ZERO << 8) | \
@@ -822,8 +822,8 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 #	define R500_RS_COL_PTR(x)		        ((x) << 24)
 #       define R500_RS_COL_FMT(x)                       ((x) << 27)
 /* gap */
-#define R500_RS_IP_OFFSET_DIS 				(0 << 31)
-#define R500_RS_IP_OFFSET_EN 				(1 << 31)
+#define R500_RS_IP_OFFSET_DIS 				(0U << 31)
+#define R500_RS_IP_OFFSET_EN 				(1U << 31)
 
 /* gap */
 
@@ -2078,7 +2078,7 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 #       define R300_ALU_OUTC_MOD_DIV8           (6 << R300_ALU_OUTC_MOD_SHIFT)
 
 #       define R300_ALU_OUTC_CLAMP              (1 << 30)
-#       define R300_ALU_INSERT_NOP              (1 << 31)
+#       define R300_ALU_INSERT_NOP              (1U << 31)
 
 #define R300_US_ALU_ALPHA_INST_0                 0x49C0
 #       define R300_ALU_ARGA_SRC0C_X            0
@@ -2311,7 +2311,7 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 #       define R300_DISCARD_SRC_PIXELS_SRC_COLOR_1     (5 << 3)
 #       define R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1     (6 << 3)
 #       define R500_SRC_ALPHA_0_NO_READ                (1 << 30)
-#       define R500_SRC_ALPHA_1_NO_READ                (1 << 31)
+#       define R500_SRC_ALPHA_1_NO_READ                (1U << 31)
 
 /* the following are shared between CBLEND and ABLEND */
 #       define R300_FCN_MASK                         (3  << 12)
@@ -3310,7 +3310,7 @@ enum {
 #   define R500_FC_KBOOL(x)				(x)
 #define R500_US_FC_CTRL					0x4624
 #   define R500_FC_TEST_EN				(1 << 30)
-#   define R500_FC_FULL_FC_EN				(1 << 31)
+#   define R500_FC_FULL_FC_EN				(1U << 31)
 #define R500_US_FC_INST_0				0x9800
 #   define R500_FC_OP_JUMP				(0 << 0)
 #   define R500_FC_OP_LOOP				(1 << 0)
@@ -3489,7 +3489,7 @@ enum {
 #define R300_PACKET3_INDX_BUFFER            0x00003300
 #    define R300_INDX_BUFFER_DST_SHIFT          0
 #    define R300_INDX_BUFFER_SKIP_SHIFT         16
-#    define R300_INDX_BUFFER_ONE_REG_WR		(1<<31)
+#    define R300_INDX_BUFFER_ONE_REG_WR		(1U << 31)
 
 /* Same as R300_PACKET3_3D_DRAW_VBUF but without VAP_VTX_FMT */
 #define R300_PACKET3_3D_DRAW_VBUF_2         0x00003400
diff --git a/src/gallium/drivers/r300/r300_state_derived.c b/src/gallium/drivers/r300/r300_state_derived.c
index c31fce66e80..92f895e4d72 100644
--- a/src/gallium/drivers/r300/r300_state_derived.c
+++ b/src/gallium/drivers/r300/r300_state_derived.c
@@ -102,7 +102,7 @@ static void r300_draw_emit_all_attribs(struct r300_context* r300)
     gen_count = 0;
     for (i = 0; i < ATTR_GENERIC_COUNT && gen_count < 8; i++) {
         if (vs_outputs->generic[i] != ATTR_UNUSED &&
-            !(r300->sprite_coord_enable & (1 << i))) {
+            !(r300->sprite_coord_enable & (1U << i))) {
             r300_draw_emit_attrib(r300, EMIT_4F, vs_outputs->generic[i]);
             gen_count++;
         }
@@ -441,7 +441,7 @@ static void r300_update_rs_block(struct r300_context *r300)
 	for (i = 0; i < ATTR_GENERIC_COUNT && col_count < 2; i++) {
 	    /* Cannot use color varyings for sprite coords. */
 	    if (fs_inputs->generic[i] != ATTR_UNUSED &&
-		(r300->sprite_coord_enable & (1 << i))) {
+		(r300->sprite_coord_enable & (1U << i))) {
 		break;
 	    }
 
@@ -807,7 +807,7 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
 
     for (i = 0; i < count; i++) {
         if (state->sampler_views[i] && state->sampler_states[i]) {
-            state->tx_enable |= 1 << i;
+            state->tx_enable |= 1U << i;
 
             view = state->sampler_views[i];
             tex = r300_resource(view->base.texture);
@@ -973,7 +973,7 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
                         (struct pipe_sampler_view**)&state->sampler_views[i],
                         &r300->texkill_sampler->base);
 
-                state->tx_enable |= 1 << i;
+                state->tx_enable |= 1U << i;
 
                 texstate = &state->regs[i];
 
diff --git a/src/gallium/drivers/r300/r300_tgsi_to_rc.c b/src/gallium/drivers/r300/r300_tgsi_to_rc.c
index 14fb3182d40..fbc827afcf2 100644
--- a/src/gallium/drivers/r300/r300_tgsi_to_rc.c
+++ b/src/gallium/drivers/r300/r300_tgsi_to_rc.c
@@ -225,17 +225,17 @@ static void transform_texture(struct rc_instruction * dst, struct tgsi_instructi
         case TGSI_TEXTURE_SHADOW1D:
             dst->U.I.TexSrcTarget = RC_TEXTURE_1D;
             dst->U.I.TexShadow = 1;
-            *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
+            *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
             break;
         case TGSI_TEXTURE_SHADOW2D:
             dst->U.I.TexSrcTarget = RC_TEXTURE_2D;
             dst->U.I.TexShadow = 1;
-            *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
+            *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
             break;
         case TGSI_TEXTURE_SHADOWRECT:
             dst->U.I.TexSrcTarget = RC_TEXTURE_RECT;
             dst->U.I.TexShadow = 1;
-            *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
+            *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
             break;
         case TGSI_TEXTURE_1D_ARRAY:
             dst->U.I.TexSrcTarget = RC_TEXTURE_1D_ARRAY;
@@ -246,17 +246,17 @@ static void transform_texture(struct rc_instruction * dst, struct tgsi_instructi
         case TGSI_TEXTURE_SHADOW1D_ARRAY:
             dst->U.I.TexSrcTarget = RC_TEXTURE_1D_ARRAY;
             dst->U.I.TexShadow = 1;
-            *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
+            *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
             break;
         case TGSI_TEXTURE_SHADOW2D_ARRAY:
             dst->U.I.TexSrcTarget = RC_TEXTURE_2D_ARRAY;
             dst->U.I.TexShadow = 1;
-            *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
+            *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
             break;
         case TGSI_TEXTURE_SHADOWCUBE:
             dst->U.I.TexSrcTarget = RC_TEXTURE_CUBE;
             dst->U.I.TexShadow = 1;
-            *shadowSamplers |= 1 << dst->U.I.TexSrcUnit;
+            *shadowSamplers |= 1U << dst->U.I.TexSrcUnit;
             break;
     }
     dst->U.I.TexSwizzle = RC_SWIZZLE_XYZW;



More information about the mesa-commit mailing list