kasan: fix -Wstringop-overflow warning
Subsystems (2)
PATCH 1/1 kasan: fix -Wstringop-overflow warning
Compiling with gcc-9.2.1 points out below warnings.
In function 'memmove',
inlined from 'kmalloc_memmove_invalid_size' at lib/test_kasan.c:301:2:include/linux/string.h:441:9: warning: '__builtin_memmove' specified bound 18446744073709551614 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]
Why generate this warnings?
Because our test function deliberately pass a negative number in memmove(), so we need to make it "volatile" so that compiler doesn't see it.
--- lib/test_kasan.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
lib/test_kasan.c +2 -1
@@ -289,6 +289,7 @@ static noinline void __init kmalloc_memmove_invalid_size(void) {char *ptr;size_t size =64 ; +volatile size_t invalid_size =-2 ;pr_info ("invalid size in memmove\n" ); ptr =kmalloc (size,GFP_KERNEL );
@@ -298,7 +299,7 @@ static noinline void __init kmalloc_memmove_invalid_size(void) }memset ((char *)ptr,0 ,64 ); -memmove ((char *)ptr, (char *)ptr +4 ,-2 ); +memmove ((char *)ptr, (char *)ptr +4 , invalid_size);kfree (ptr); }
Signature · 1 line
2.18.0
Andrew Morton
maintainerQuoted 25 lines · On Wed, 11 Mar 2020 21:42:44 +0800 Walter Wu <[email protected]> wrote:
Compiling with gcc-9.2.1 points out below warnings.
In function 'memmove',
inlined from 'kmalloc_memmove_invalid_size' atlib/test_kasan.c:301:2:include/linux/string.h:441:9: warning: '__builtin_memmove' specified bound 18446744073709551614 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]Why generate this warnings?
Because our test function deliberately pass a negative number inmemmove(), so we need to make it "volatile" so that compiler doesn't see it....
lib/test_kasan.c +1
-0@@ -289,6 +289,7 @@ static noinline void __init kmalloc_memmove_invalid_size(void) {char *ptr;size_t size =64 ; +volatile size_t invalid_size =-2 ;pr_info ("invalid size in memmove\n" ); ptr =kmalloc (size,GFP_KERNEL );
lib/test_kasan.c static noinline void __init kmalloc_memmove_invalid_size(void) · line 304
Quoted 7 lines
lib/test_kasan.c +1
-1@@ -298,7 +299,7 @@ static noinline void __init kmalloc_memmove_invalid_size(void) }memset ((char *)ptr,0 ,64 ); -memmove ((char *)ptr, (char *)ptr +4 ,-2 ); +memmove ((char *)ptr, (char *)ptr +4 , invalid_size);kfree (ptr); }
Huh. Why does this trick suppress the warning?
Do we have any guarantee that this it will contiue to work in future gcc's?
Quoted 29 lines · Andrew Morton
Quoted 27 lines · On Wed, 11 Mar 2020 21:42:44 +0800 Walter Wu <[email protected]> wrote:
Compiling with gcc-9.2.1 points out below warnings.
In function 'memmove',
inlined from 'kmalloc_memmove_invalid_size' atlib/test_kasan.c:301:2:include/linux/string.h:441:9: warning: '__builtin_memmove' specified bound 18446744073709551614 exceeds maximum object size 9223372036854775807 [-Wstringop-overflow=]Why generate this warnings?
Because our test function deliberately pass a negative number inmemmove(), so we need to make it "volatile" so that compiler doesn't see it....
lib/test_kasan.c +1
-0@@ -289,6 +289,7 @@ static noinline void __init kmalloc_memmove_invalid_size(void) {char *ptr;size_t size =64 ; +volatile size_t invalid_size =-2 ;pr_info ("invalid size in memmove\n" ); ptr =kmalloc (size,GFP_KERNEL );@@ -298,7 +299,7 @@ static noinline void __init kmalloc_memmove_invalid_size(void) }
Quoted 8 lines
lib/test_kasan.c static noinline void __init kmalloc_memmove_invalid_size(void) · line 304
Quoted 5 lines
lib/test_kasan.c +1
-1memset ((char *)ptr,0 ,64 ); -memmove ((char *)ptr, (char *)ptr +4 ,-2 ); +memmove ((char *)ptr, (char *)ptr +4 , invalid_size);kfree (ptr); }Huh. Why does this trick suppress the warning?
We read below the document, so we try to verify whether it is work for another checking. After we changed the code, It is ok.
https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html#Warning-Options
"They do not occur for variables or elements declared volatile. Because these warnings depend on optimization, the exact variables or elements for which there are warnings depends on the precise optimization options and version of GCC used."
Quoted 3 lines
Do we have any guarantee that this it will contiue to work in future gcc's?
Sorry, I am not compiler expert, so I can't guarantee gcc will not modify the rule, but at least it is work before gcc-9.