Skip to content

Commit 031f117

Browse files
committed
Fix all warnings given by UBSAN
The easiest way to compile and test the server with UBSAN is to run: ./BUILD/compile-pentium64-ubsan and then run mysql-test-run. After this commit, one should be able to run this without any UBSAN warnings. There is still a few compiler warnings that should be fixed at some point, but these do not expose any real bugs. The 'special' cases where we disable, suppress or circumvent UBSAN are: - ref10 source (as here we intentionally do some shifts that UBSAN complains about. - x86 version of optimized int#korr() methods. UBSAN do not like unaligned memory access of integers. Fixed by using byte_order_generic.h when compiling with UBSAN - We use smaller thread stack with ASAN and UBSAN, which forced me to disable a few tests that prints the thread stack size. - Verifying class types does not work for shared libraries. I added suppression in mysql-test-run.pl for this case. - Added '#ifdef WITH_UBSAN' when using integer arithmetic where it is safe to have overflows (two cases, in item_func.cc). Things fixed: - Don't left shift signed values (byte_order_generic.h, mysqltest.c, item_sum.cc and many more) - Don't assign not non existing values to enum variables. - Ensure that bool and enum values are properly initialized in constructors. This was needed as UBSAN checks that these types has correct values when one copies an object. (gcalc_tools.h, ha_partition.cc, item_sum.cc, partition_element.h ...) - Ensure we do not called handler functions on unallocated objects or deleted objects. (events.cc, sql_acl.cc). - Fixed bugs in Item_sp::Item_sp() where we did not call constructor on Query_arena object. - Fixed several cast of objects to an incompatible class! (Item.cc, Item_buff.cc, item_timefunc.cc, opt_subselect.cc, sql_acl.cc, sql_select.cc ...) - Ensure we do not do integer arithmetic that causes over or underflows. This includes also ++ and -- of integers. (Item_func.cc, Item_strfunc.cc, item_timefunc.cc, sql_base.cc ...) - Added JSON_VALUE_UNITIALIZED to json_value_types and ensure that value_type is initialized to this instead of to -1, which is not a valid enum value for json_value_types. - Ensure we do not call memcpy() when second argument could be null. - Fixed that Item_func_str::make_empty_result() creates an empty string instead of a null string (safer as it ensures we do not do arithmetic on null strings). Other things: - Changed struct st_position to an OBJECT and added an initialization function to it to ensure that we do not copy or use uninitialized members. The change to a class was also motived that we used "struct st_position" and POSITION randomly trough the code which was confusing. - Notably big rewrite in sql_acl.cc to avoid using deleted objects. - Changed in sql_partition to use '^' instead of '-'. This is safe as the operator is either 0 or 0x8000000000000000ULL. - Added check for select_nr < INT_MAX in JOIN::build_explain() to avoid bug when get_select() could return NULL. - Reordered elements in POSITION for better alignment. - Changed sql_test.cc::print_plan() to use pointers instead of objects. - Fixed bug in find_set() where could could execute '1 << -1'. - Added variable have_sanitizer, used by mtr. (This variable was before only in 10.5 and up). It can now have one of two values: ASAN or UBSAN. - Moved ~Archive_share() from ha_archive.cc to ha_archive.h and marked it virtual. This was an effort to get UBSAN to work with loaded storage engines. I kept the change as the new place is better. - Added in CONNECT engine COLBLK::SetName(), to get around a wrong cast in tabutil.cpp. - Added HAVE_REPLICATION around usage of rgi_slave, to get embedded server to compile with UBSAN. (Patch from Marko). - Added #ifdef for powerpc64 to avoid a bug in old gcc versions related to integer arithmetic. Changes that should not be needed but had to be done to suppress warnings from UBSAN: - Added static_cast<<uint16_t>> around shift to get rid of a LOT of compiler warnings when using UBSAN. - Had to change some '/' of 2 base integers to shift to get rid of some compile time warnings. Reviewed by: - Json changes: Alexey Botchkov - Charset changes in ctype-uca.c: Alexander Barkov - InnoDB changes & Embedded server: Marko Mäkelä - sql_acl.cc changes: Vicențiu Ciorbaru - build_explain() changes: Sergey Petrunia
1 parent eb4123e commit 031f117

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+528
-291
lines changed

BUILD/SETUP.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ elif [ "x$warning_mode" = "xmaintainer" ]; then
141141
debug_extra_cflags="-g3"
142142
else
143143
# Both C and C++ warnings
144-
warnings="-Wall -Wextra -Wunused -Wwrite-strings -Wno-uninitialized -Wno-strict-aliasing -Wimplicit-fallthrough=2"
144+
warnings="-Wall -Wextra -Wunused -Wwrite-strings -Wno-uninitialized -Wno-strict-aliasing -Wimplicit-fallthrough=2 -Wformat-security -Wvla"
145145

146146
# For more warnings, uncomment the following line
147147
# warnings="$warnings -Wshadow"

BUILD/compile-pentium64-ubsan

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#! /bin/sh
2+
# Copyright (c) 2018, MariaDB Corporation.
3+
#
4+
# This program is free software; you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation; version 2 of the License.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
16+
17+
# Compilation with UBSAN, The Undefined Behavior Sanitizer
18+
# We have to use -Wno-uninitialized and -Wno-unitialized we get a lot of false
19+
# positive warnings for this when compiling with -fsanitize=undefined.
20+
# We also have to compile without Spider as linking with Spider library does
21+
# not work. (errno: 11, undefined symbol: _ZTI12ha_partition)
22+
23+
path=`dirname $0`
24+
. "$path/SETUP.sh"
25+
26+
extra_flags="$pentium64_cflags $debug_cflags -fsanitize=undefined -DWITH_UBSAN -Wno-conversion -Wno-uninitialized"
27+
extra_configs="$pentium_configs $debug_configs -DWITH_UBSAN=ON -DMYSQL_MAINTAINER_MODE=NO --without-spider --without-tokudb"
28+
29+
. "$path/FINISH.sh"

client/mysqltest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10905,7 +10905,7 @@ int get_next_bit(REP_SET *set,uint lastpos)
1090510905

1090610906
start=set->bits+ ((lastpos+1) / WORD_BIT);
1090710907
end=set->bits + set->size_of_bits;
10908-
bits=start[0] & ~((1 << ((lastpos+1) % WORD_BIT)) -1);
10908+
bits=start[0] & ~((1U << ((lastpos+1) % WORD_BIT)) -1);
1090910909

1091010910
while (! bits && ++start < end)
1091110911
bits=start[0];

cmake/configure.pl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ sub check_compiler
9393
{
9494
$option = substr($option, 2);
9595
}
96+
elsif (substr ($option, 0, 2) eq "-D")
97+
{
98+
# Must be cmake config option
99+
$option = substr($option, 1);
100+
}
96101
else
97102
{
98103
# This must be environment variable
@@ -119,6 +124,11 @@ sub check_compiler
119124
$just_print=1;
120125
next;
121126
}
127+
if ($option =~ /D.*=/)
128+
{
129+
$cmakeargs = $cmakeargs." -".$option;
130+
next;
131+
}
122132
if($option =~ /with-plugins=/)
123133
{
124134
my @plugins= split(/,/, substr($option,13));

include/byte_order_generic.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
(((uint32) (uchar) (A)[2]) << 16) |\
2929
(((uint32) (uchar) (A)[1]) << 8) | \
3030
((uint32) (uchar) (A)[0])))
31-
#define sint4korr(A) (int32) (((int32) ((uchar) (A)[0])) |\
32-
(((int32) ((uchar) (A)[1]) << 8)) |\
33-
(((int32) ((uchar) (A)[2]) << 16)) |\
34-
(((int32) ((int16) (A)[3]) << 24)))
31+
#define sint4korr(A) (int32) (((uint32) ((uchar) (A)[0])) |\
32+
(((uint32) ((uchar) (A)[1]) << 8)) |\
33+
(((uint32) ((uchar) (A)[2]) << 16)) |\
34+
(((uint32) ((uchar) (A)[3]) << 24)))
3535
#define sint8korr(A) (longlong) uint8korr(A)
3636
#define uint2korr(A) (uint16) (((uint16) ((uchar) (A)[0])) |\
3737
((uint16) ((uchar) (A)[1]) << 8))

include/byte_order_generic_x86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
/*
1818
Optimized function-like macros for the x86 architecture (_WIN32 included).
1919
*/
20+
2021
#define sint2korr(A) (*((const int16 *) (A)))
2122
#define sint3korr(A) ((int32) ((((uchar) (A)[2]) & 128) ? \
2223
(((uint32) 255L << 24) | \

include/json_lib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ enum json_states {
172172

173173
enum json_value_types
174174
{
175+
JSON_VALUE_UNINITALIZED=0,
175176
JSON_VALUE_OBJECT=1,
176177
JSON_VALUE_ARRAY=2,
177178
JSON_VALUE_STRING=3,

include/my_byteorder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
format (low byte first). There are 'korr' (assume 'corrector') variants
3232
for integer types, but 'get' (assume 'getter') for floating point types.
3333
*/
34-
#if defined(__i386__) || defined(_WIN32)
34+
#if (defined(__i386__) || defined(_WIN32)) && !defined(WITH_UBSAN)
3535
#define MY_BYTE_ORDER_ARCH_OPTIMIZED
3636
#include "byte_order_generic_x86.h"
37-
#elif defined(__x86_64__)
37+
#elif defined(__x86_64__) && !defined(WITH_UBSAN)
3838
#include "byte_order_generic_x86_64.h"
3939
#else
4040
#include "byte_order_generic.h"

include/my_compare.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Copyright (c) 2011, Oracle and/or its affiliates.
2-
Copyright (c) Monty Program Ab; 1991-2011
2+
Copyright (c) 1991, 2020, MariaDB Corporation.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -95,15 +95,16 @@ static inline uchar get_rec_bits(const uchar *ptr, uchar ofs, uint len)
9595
{
9696
uint16 val= ptr[0];
9797
if (ofs + len > 8)
98-
val|= (uint16)(ptr[1]) << 8;
99-
return (val >> ofs) & ((1 << len) - 1);
98+
val|= (uint16)(((uint) ptr[1]) << 8);
99+
return (uchar) ((val >> ofs) & ((1 << len) - 1));
100100
}
101101

102102
static inline void set_rec_bits(uint16 bits, uchar *ptr, uchar ofs, uint len)
103103
{
104-
ptr[0]= (ptr[0] & ~(((1 << len) - 1) << ofs)) | (bits << ofs);
104+
ptr[0]= (uchar) ((ptr[0] & ~(((1 << len) - 1) << ofs)) | (bits << ofs));
105105
if (ofs + len > 8)
106-
ptr[1]= (ptr[1] & ~((1 << (len - 8 + ofs)) - 1)) | (bits >> (8 - ofs));
106+
ptr[1]= (uchar) ((ptr[1] & ~((1 << (len - 8 + ofs)) - 1)) |
107+
bits >> (8 - ofs));
107108
}
108109

109110
#define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \

include/myisampack.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define mi_uint1korr(A) ((uint8)(*A))
3131

3232
#define mi_sint2korr(A) ((int16) (((int16) (((const uchar*) (A))[1])) |\
33-
((int16) ((int16) ((const char*) (A))[0]) << 8)))
33+
((int16) ((uint16) ((const uchar*) (A))[0]) << 8)))
3434
#define mi_sint3korr(A) ((int32) (((((const uchar*) (A))[0]) & 128) ? \
3535
(((uint32) 255L << 24) | \
3636
(((uint32) ((const uchar*) (A))[0]) << 16) |\
@@ -39,10 +39,10 @@
3939
(((uint32) ((const uchar*) (A))[0]) << 16) |\
4040
(((uint32) ((const uchar*) (A))[1]) << 8) | \
4141
((uint32) ((const uchar*) (A))[2])))
42-
#define mi_sint4korr(A) ((int32) (((int32) (((const uchar*) (A))[3])) |\
43-
((int32) (((const uchar*) (A))[2]) << 8) |\
44-
((int32) (((const uchar*) (A))[1]) << 16) |\
45-
((int32) ((int16) ((const char*) (A))[0]) << 24)))
42+
#define mi_sint4korr(A) ((int32) (((uint32) (((const uchar*) (A))[3])) |\
43+
((uint32) (((const uchar*) (A))[2]) << 8) |\
44+
((uint32) (((const uchar*) (A))[1]) << 16) |\
45+
((uint32) (((const uchar*) (A))[0]) << 24)))
4646
#define mi_sint8korr(A) ((longlong) mi_uint8korr(A))
4747
#define mi_uint2korr(A) ((uint16) (((uint16) (((const uchar*) (A))[1])) |\
4848
((uint16) (((const uchar*) (A))[0]) << 8)))

0 commit comments

Comments
 (0)