Fix undefined behaviours when shifting.

Found by libubsan.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14913
This commit is contained in:
Florian Krohm 2015-02-06 20:32:15 +00:00
parent 7a3d388632
commit c29515885b
5 changed files with 15 additions and 12 deletions

View File

@ -183,8 +183,8 @@ static void cachesim_initcache(cache_t config, cache_t2* c)
c->sets = (c->size / c->line_size) / c->assoc;
c->sets_min_1 = c->sets - 1;
c->line_size_bits = VG_(log2)(c->line_size);
c->tag_shift = c->line_size_bits + VG_(log2)(c->sets);
c->tag_mask = ~((1<<c->tag_shift)-1);
c->tag_shift = c->line_size_bits + VG_(log2)(c->sets);
c->tag_mask = ~((1u<<c->tag_shift)-1);
/* Can bits in tag entries be used for flags?
* Should be always true as MIN_LINE_SIZE >= 16 */
@ -650,7 +650,7 @@ void cacheuse_initcache(cache_t2* c)
else {
int bytes_per_bit = c->line_size/32;
start_mask = 1;
end_mask = 1 << 31;
end_mask = 1u << 31;
for(i=0;i<c->line_size;i++) {
c->line_start_mask[i] = start_val;
c->line_end_mask[c->line_size-i-1] = end_val;

View File

@ -2534,12 +2534,13 @@ static ULong step_le_u_encoded_literal ( DiCursor* data, UInt size )
static Long step_le_s_encoded_literal ( DiCursor* data, UInt size )
{
Long s64 = step_le_u_encoded_literal( data, size );
ULong u64 = step_le_u_encoded_literal( data, size );
Long s64;
switch (size) {
case 8: break;
case 4: s64 <<= 32; s64 >>= 32; break;
case 2: s64 <<= 48; s64 >>= 48; break;
case 1: s64 <<= 56; s64 >>= 56; break;
case 4: s64 = u64 << 32; s64 >>= 32; break;
case 2: s64 = u64 << 48; s64 >>= 48; break;
case 1: s64 = u64 << 56; s64 >>= 56; break;
default: vg_assert(0); /*NOTREACHED*/ return 0;
}
return s64;

View File

@ -554,7 +554,7 @@ static Bool getplatformoffset (SizeT *result)
static Bool getplatformoffset_called = False;
static Bool lm_modid_offset_found = False;
static SizeT lm_modid_offset = 1<<31; // Rubbish initial value.
static SizeT lm_modid_offset = 1u << 31; // Rubbish initial value.
// lm_modid_offset is a magic offset, retrieved using an external program.
if (!getplatformoffset_called) {

View File

@ -40,4 +40,4 @@ vbit_test_CPPFLAGS = $(AM_CPPFLAGS_PRI) \
vbit_test_CFLAGS = $(AM_CFLAGS_PRI) -std=c99
vbit_test_DEPENDENCIES =
vbit_test_LDADD =
vbit_test_LDFLAGS = $(AM_CFLAGS_PRI) -std=c99
vbit_test_LDFLAGS = $(AM_CFLAGS_PRI) -std=c99 -static-libubsan

View File

@ -408,9 +408,11 @@ concat_vbits(vbits_t v1, vbits_t v2)
vbits_t new = { .num_bits = v1.num_bits * 2 };
switch (v1.num_bits) {
case 8: new.bits.u16 = (v1.bits.u8 << 8) | v2.bits.u8; break;
case 16: new.bits.u32 = (v1.bits.u16 << 16) | v2.bits.u16; break;
case 32: new.bits.u64 = v1.bits.u32;
case 8: new.bits.u16 = v1.bits.u8;
new.bits.u16 = (new.bits.u16 << 8) | v2.bits.u8; break;
case 16: new.bits.u32 = v1.bits.u16;
new.bits.u32 = (new.bits.u32 << 16) | v2.bits.u16; break;
case 32: new.bits.u64 = v1.bits.u32;
new.bits.u64 = (new.bits.u64 << 32) | v2.bits.u32; break;
case 64:
if (__BYTE_ORDER == __LITTLE_ENDIAN) {