fs: clear a UBSAN shift-out-of-bounds warning
Subsystems (2)
v2 cover fs: clear a UBSAN shift-out-of-bounds warning
v1 -- > v2:
1. Replace INT_LIMIT(loff_t) with OFFSET_MAX in btrfs.
2. Replace INT_LIMIT() with type_max().
Zhen Lei (2):
btrfs: replace INT_LIMIT(loff_t) with OFFSET_MAX
fs: clear a UBSAN shift-out-of-bounds warning
fs/btrfs/ordered-data.c | 6 +++--- include/linux/fs.h | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/2 btrfs: replace INT_LIMIT(loff_t) with OFFSET_MAX
OFFSET_MAX is self-annotated and more readable.
--- fs/btrfs/ordered-data.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/ordered-data.c +3 -3
@@ -761,11 +761,11 @@ int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)struct btrfs_ordered_extent *ordered;if (start + len < start) { - orig_end =INT_LIMIT (loff_t); + orig_end =OFFSET_MAX ; }else { orig_end = start + len -1 ; -if (orig_end >INT_LIMIT (loff_t)) - orig_end =INT_LIMIT (loff_t); +if (orig_end >OFFSET_MAX ) + orig_end =OFFSET_MAX ; }/* start IO across the range first to instantiate any delalloc
Signature · 1 line
2.25.1
Re: [PATCH v2 1/2] btrfs: replace INT_LIMIT(loff_t) with OFFSET_MAX
Quoted 3 lines · Zhen Lei
OFFSET_MAXis self-annotated and more readable.Signed-off-by Zhen Lei <[email protected]>
Re: [PATCH v2 1/2] btrfs: replace INT_LIMIT(loff_t) with OFFSET_MAX
In reply to Zhen LeiQuoted 17 lines · Zhen Lei
OFFSET_MAXis self-annotated and more readable.Signed-off-by Zhen Lei <[email protected]>--- fs/btrfs/ordered-data.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)fs/btrfs/ordered-data.c +0
-1@@ -761,11 +761,11 @@ int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len)struct btrfs_ordered_extent *ordered;if (start + len < start) { - orig_end =INT_LIMIT (loff_t);
fs/btrfs/ordered-data.c int btrfs_wait_ordered_range(struct inode *inode, u64 start, u64 len) · line 771
Quoted 10 lines
fs/btrfs/ordered-data.c +3
-2+ orig_end =OFFSET_MAX ; }else { orig_end = start + len -1 ; -if (orig_end >INT_LIMIT (loff_t)) - orig_end =INT_LIMIT (loff_t); +if (orig_end >OFFSET_MAX ) + orig_end =OFFSET_MAX ; }/* start IO across the range first to instantiate any delalloc
- Eric
PATCH v2 2/2 fs: clear a UBSAN shift-out-of-bounds warning
In reply to Zhen LeiUBSAN: shift-out-of-bounds in fs/locks.c:2572:16
left shift of 1 by 63 places cannot be represented in type 'long long int'
Switch the calculation method to type_max() can help us eliminate this false positive.
On the other hand, the old implementation has problems with char and short types, although not currently involved.printf("%d: %x\n", sizeof(char), INT_LIMIT(char)); printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
1: ffffff7f
2: ffff7fff
--- include/linux/fs.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
include/linux/fs.h +2 -3
@@ -1131,9 +1131,8 @@ struct file_lock_context {/* The following constant reflects the upper bound of the file/locking space */ #ifndef OFFSET_MAX -#define INT_LIMIT (x) (~((x)1 << (sizeof(x)*8 - 1))) -#define OFFSET_MAX INT_LIMIT(loff_t) -#define OFFT_OFFSET_MAX INT_LIMIT(off_t) +#define OFFSET_MAX type_max(loff_t) +#define OFFT_OFFSET_MAX type_max(off_t)#endif extern void send_sigio (struct fown_struct *fown,int fd,int band);
Signature · 1 line
2.25.1
Quoted 26 lines · Zhen Lei
UBSAN: shift-out-of-bounds in fs/locks.c:2572:16left shift of 1 by 63 places cannot be represented in type 'long long int'
Switch the calculation method to
type_max()can help us eliminate this false positive.On the other hand, the old implementation has problems with char and short types, although not currently involved.
printf("%d: %x\n", sizeof(char), INT_LIMIT(char));printf("%d: %x\n", sizeof(short), INT_LIMIT(short));
1:ffffff7f
2:ffff7fffSuggested-by Eric Biggers <[email protected]>Signed-off-by Zhen Lei <[email protected]>--- include/linux/fs.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)include/linux/fs.h +0
-0@@ -1131,9 +1131,8 @@ struct file_lock_context {
include/linux/fs.h struct file_lock_context { · line 1138
Quoted 10 lines
include/linux/fs.h +2
-3/* The following constant reflects the upper bound of the file/locking space */ #ifndef OFFSET_MAX -#define INT_LIMIT (x) (~((x)1 << (sizeof(x)*8 - 1))) -#define OFFSET_MAX INT_LIMIT(loff_t) -#define OFFT_OFFSET_MAX INT_LIMIT(off_t) +#define OFFSET_MAX type_max(loff_t) +#define OFFT_OFFSET_MAX type_max(off_t)#endif extern void send_sigio (struct fown_struct *fown,int fd,int band);
- Eric
Al Viro
maintainerQuoted 3 lines · Zhen Lei
v1 -- > v2:
1. ReplaceINT_LIMIT(loff_t)withOFFSET_MAXin btrfs.
2. ReplaceINT_LIMIT()withtype_max().
Looks fine, except that I'd rather go for commit message along the lines of "INT_LIMIT tries to do what type_max does, except that type_max doesn't rely upon undefined behaviour; might as well use type_max() instead"
If you want to credit UBSAN - sure, no problem, just don't clutter the commit message with that. As it is, it reads as "make $TOOL STFU"...
Quoted 9 lines · Al Viro
Quoted 3 lines · Zhen Lei
v1 -- > v2:
1. ReplaceINT_LIMIT(loff_t)withOFFSET_MAXin btrfs.
2. ReplaceINT_LIMIT()withtype_max().Looks fine, except that I'd rather go for commit message along the lines of "
INT_LIMITtries to do whattype_maxdoes, except thattype_maxdoesn't rely upon undefined behaviour; might as well usetype_max()instead"
Very good. Do I send v3, or do you update it?
Quoted 4 lines
If you want to credit UBSAN - sure, no problem, just don't clutter the commit message with that. As it is, it reads as "make $TOOL STFU"...
Okay, I'll pay attention next time. This USBAN problem is relatively simple and can be located without relying on other information, so I omitted the rest.
After changing to your suggested description, it seems that there is no need to mention UBSAN, after all, it is just a false positive and there is no real problem.
Quoted 3 lines
.
Signature · 2 lines
Regards, Zhen Lei