llvm.org GIT mirror llvm / a8d99b7
Fix another UBSan test error from r248897 and follow on fix r249689 While here fix a few more issues with potential overflow and add new tests for these cases. Ensured that test now passes with UBSan. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249745 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 4 years ago
2 changed file(s) with 101 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
7676 &value,
7777 sizeof(value_type));
7878 }
79
80 template
81 using make_unsigned_t = typename std::make_unsigned::type;
7982
8083 /// Read a value of a particular endianness from memory, for a location
8184 /// that starts at the given bit offset within the first byte.
9598 val[1] = byte_swap(val[1]);
9699
97100 // Shift bits from the lower value into place.
98 unsigned lowerVal = val[0] >> startBit;
101 make_unsigned_t lowerVal = val[0] >> startBit;
99102 // Mask off upper bits after right shift in case of signed type.
100 unsigned numBitsFirstVal = (sizeof(value_type) * 8) - startBit;
101 lowerVal &= (1 << numBitsFirstVal) - 1;
103 make_unsigned_t numBitsFirstVal =
104 (sizeof(value_type) * 8) - startBit;
105 lowerVal &= ((make_unsigned_t)1 << numBitsFirstVal) - 1;
102106
103107 // Get the bits from the upper value.
104 unsigned upperVal = val[1] & ((1 << startBit) - 1);
108 make_unsigned_t upperVal =
109 val[1] & (((make_unsigned_t)1 << startBit) - 1);
105110 // Shift them in to place.
106111 upperVal <<= numBitsFirstVal;
107112
129134
130135 // Mask off any existing bits in the upper part of the lower value that
131136 // we want to replace.
132 val[0] &= (1 << startBit) - 1;
133 unsigned numBitsFirstVal = (sizeof(value_type) * 8) - startBit;
134 unsigned lowerVal = value;
137 val[0] &= ((make_unsigned_t)1 << startBit) - 1;
138 make_unsigned_t numBitsFirstVal =
139 (sizeof(value_type) * 8) - startBit;
140 make_unsigned_t lowerVal = value;
135141 if (startBit > 0) {
136142 // Mask off the upper bits in the new value that are not going to go into
137143 // the lower value. This avoids a left shift of a negative value, which
138144 // is undefined behavior.
139 lowerVal &= ((1 << numBitsFirstVal) - 1);
145 lowerVal &= (((make_unsigned_t)1 << numBitsFirstVal) - 1);
140146 // Now shift the new bits into place
141147 lowerVal <<= startBit;
142148 }
144150
145151 // Mask off any existing bits in the lower part of the upper value that
146152 // we want to replace.
147 val[1] &= ~((1 << startBit) - 1);
153 val[1] &= ~(((make_unsigned_t)1 << startBit) - 1);
148154 // Next shift the bits that go into the upper value into position.
149 unsigned upperVal = value >> numBitsFirstVal;
155 make_unsigned_t upperVal = value >> numBitsFirstVal;
150156 // Mask off upper bits after right shift in case of signed type.
151 upperVal &= (1 << startBit) - 1;
157 upperVal &= ((make_unsigned_t)1 << startBit) - 1;
152158 val[1] |= upperVal;
153159
154160 // Finally, rewrite values.
4949 0x0f000000);
5050 EXPECT_EQ((endian::readAtBitAlignment(&bigval2[0], 4)),
5151 0x0f000000);
52 // Test to make sure left shift of start bit doesn't overflow.
53 EXPECT_EQ(
54 (endian::readAtBitAlignment(&littleval2[0], 1)),
55 0x78000000);
56 EXPECT_EQ((endian::readAtBitAlignment(&bigval2[0], 1)),
57 0x78000000);
58 // Test to make sure 64-bit int doesn't overflow.
59 unsigned char littleval3[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
61 unsigned char bigval3[] = {0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
63 EXPECT_EQ((endian::readAtBitAlignment(
64 &littleval3[0], 4)),
65 0x0f00000000000000);
66 EXPECT_EQ(
67 (endian::readAtBitAlignment(&bigval3[0], 4)),
68 0x0f00000000000000);
5269 }
5370
5471 TEST(Endian, WriteBitAligned) {
7794 EXPECT_EQ(littleval[5], 0x00);
7895 EXPECT_EQ(littleval[6], 0x00);
7996 EXPECT_EQ(littleval[7], 0x00);
97
98 // This test makes sure 1<<31 doesn't overflow.
99 // Test to make sure left shift of start bit doesn't overflow.
100 unsigned char bigval2[8] = {0x00};
101 endian::writeAtBitAlignment(bigval2, (int)0xffffffff,
102 1);
103 EXPECT_EQ(bigval2[0], 0xff);
104 EXPECT_EQ(bigval2[1], 0xff);
105 EXPECT_EQ(bigval2[2], 0xff);
106 EXPECT_EQ(bigval2[3], 0xfe);
107 EXPECT_EQ(bigval2[4], 0x00);
108 EXPECT_EQ(bigval2[5], 0x00);
109 EXPECT_EQ(bigval2[6], 0x00);
110 EXPECT_EQ(bigval2[7], 0x01);
111
112 unsigned char littleval2[8] = {0x00};
113 endian::writeAtBitAlignment(littleval2,
114 (int)0xffffffff, 1);
115 EXPECT_EQ(littleval2[0], 0xfe);
116 EXPECT_EQ(littleval2[1], 0xff);
117 EXPECT_EQ(littleval2[2], 0xff);
118 EXPECT_EQ(littleval2[3], 0xff);
119 EXPECT_EQ(littleval2[4], 0x01);
120 EXPECT_EQ(littleval2[5], 0x00);
121 EXPECT_EQ(littleval2[6], 0x00);
122 EXPECT_EQ(littleval2[7], 0x00);
123
124 // Test to make sure 64-bit int doesn't overflow.
125 unsigned char bigval64[16] = {0x00};
126 endian::writeAtBitAlignment(
127 bigval64, (int64_t)0xffffffffffffffff, 1);
128 EXPECT_EQ(bigval64[0], 0xff);
129 EXPECT_EQ(bigval64[1], 0xff);
130 EXPECT_EQ(bigval64[2], 0xff);
131 EXPECT_EQ(bigval64[3], 0xff);
132 EXPECT_EQ(bigval64[4], 0xff);
133 EXPECT_EQ(bigval64[5], 0xff);
134 EXPECT_EQ(bigval64[6], 0xff);
135 EXPECT_EQ(bigval64[7], 0xfe);
136 EXPECT_EQ(bigval64[8], 0x00);
137 EXPECT_EQ(bigval64[9], 0x00);
138 EXPECT_EQ(bigval64[10], 0x00);
139 EXPECT_EQ(bigval64[11], 0x00);
140 EXPECT_EQ(bigval64[12], 0x00);
141 EXPECT_EQ(bigval64[13], 0x00);
142 EXPECT_EQ(bigval64[14], 0x00);
143 EXPECT_EQ(bigval64[15], 0x01);
144
145 unsigned char littleval64[16] = {0x00};
146 endian::writeAtBitAlignment(
147 littleval64, (int64_t)0xffffffffffffffff, 1);
148 EXPECT_EQ(littleval64[0], 0xfe);
149 EXPECT_EQ(littleval64[1], 0xff);
150 EXPECT_EQ(littleval64[2], 0xff);
151 EXPECT_EQ(littleval64[3], 0xff);
152 EXPECT_EQ(littleval64[4], 0xff);
153 EXPECT_EQ(littleval64[5], 0xff);
154 EXPECT_EQ(littleval64[6], 0xff);
155 EXPECT_EQ(littleval64[7], 0xff);
156 EXPECT_EQ(littleval64[8], 0x01);
157 EXPECT_EQ(littleval64[9], 0x00);
158 EXPECT_EQ(littleval64[10], 0x00);
159 EXPECT_EQ(littleval64[11], 0x00);
160 EXPECT_EQ(littleval64[12], 0x00);
161 EXPECT_EQ(littleval64[13], 0x00);
162 EXPECT_EQ(littleval64[14], 0x00);
163 EXPECT_EQ(littleval64[15], 0x00);
80164 }
81165
82166 TEST(Endian, Write) {