arch/m68k/include/asm/string.h:72:25: warning: '__builtin_memcpy' forming offset 8 is out of the bounds [0, 7]
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): 201903042049.npxcZzps%[email protected]
Geert Uytterhoeven
maintainerQuoted 37 lines · kbuild test robot
tree: https://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k.git master head: e223cadc191661c67cb419b3a53c7854ecc39e8b commit: e223cadc191661c67cb419b3a53c7854ecc39e8b [1174/1174] Merge tag 'v5.0' config: m68k-allmodconfig (attached as .config) compiler: m68k-linux-gnu-gcc (Debian 8.2.0-11) 8.2.0reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
git checkoute223cadc191661c67cb419b3a53c7854ecc39e8b
# save the attached .config to linux build tree
GCC_VERSION=8.2.0 make.cross ARCH=m68kAll warnings (new ones prefixed by >>):
In file included from
include/linux/string.h:20,
frominclude/linux/bitmap.h:9,
frominclude/linux/nodemask.h:95,
frominclude/linux/mmzone.h:17,
frominclude/linux/gfp.h:6,
frominclude/linux/umh.h:4,
frominclude/linux/kmod.h:22,
frominclude/linux/module.h:13,
fromdrivers/nvme/target/admin-cmd.c:15:
In function 'memcpy_and_pad',
inlined from 'nvmet_execute_identify_ctrl' atdrivers/nvme/target/admin-cmd.c:309:2:Quoted 1 line
Quoted 1 line
arch/m68k/include/asm/string.h:72:25: warning: '__builtin_memcpy' forming offset 8 is out of the bounds [0, 7] [-Warray-bounds]#define memcpy(d, s, n) __builtin_memcpy(d, s, n) ^~~~~~~~~~~~~~~~~~~~~~~~~ include/linux/string.h:456:3: note: in expansion of macro 'memcpy' memcpy(dest, src, dest_len); ^~~~~~vim +/__builtin_memcpy +72
arch/m68k/include/asm/string.hea61bc46 Greg Ungerer 2010-09-07 69 ea61bc46 Greg Ungerer 2010-09-07 70 #define __HAVE_ARCH_MEMCPY ea61bc46 Greg Ungerer 2010-09-07 71 extern void *memcpy(void *, const void *, __kernel_size_t);
Quoted 8 lines
ea61bc46Greg Ungerer 2010-09-07 @72 #definememcpy(d, s, n)__builtin_memcpy(d, s, n)ea61bc46Greg Ungerer 2010-09-07 73:::::: The code at line 72 was first introduced by commit
::::::ea61bc461d09e8d331a307916530aaae808c72a2m68k/m68knommu: merge MMU and non-MMUstring.h:::::: TO: Greg Ungerer <[email protected]> :::::: CC: Geert Uytterhoeven <[email protected]>
Yep, that's a funny one, which I saw myself this morning, and decided to dive into.
Apparently this is due 1) the recently added -ffreestanding and
2) the kernel version being shorter than 8 characters (indeed, it didn't happen with allmodconfig on v5.0.0-rcX).
The offending code is:
memcpy_and_pad(id->fr, sizeof(id->fr),
UTS_RELEASE, strlen(UTS_RELEASE), ' ');with UTS_RELEASE being "5.0.0+", which calls into:
static inline void memcpy_and_pad(void *dest, size_t dest_len,
const void *src, size_t count, int pad)
{
if (dest_len > count) {
Of course this branch is taken, right?
memcpy(dest, src, count);
memset(dest + count, pad, dest_len - count);
} else
memcpy(dest, src, dest_len);
}This assembles to:
.LC1:
.string "5.0.0+"
| drivers/nvme/target/admin-cmd.c:311: memcpy_and_pad(id->fr,sizeof(id->fr),
pea .LC1 |
jsr strlen |
Woops, gcc no longer optimizes this away, due to -ffreestanding.
| drivers/nvme/target/admin-cmd.c:311: memcpy_and_pad(id->fr,
sizeof(id->fr),
lea (64,%a3),%a1 |, ret, _7
| include/linux/string.h:452: if (dest_len > count) {
lea (16,%sp),%sp |,
moveq #7,%d1 |,
cmp.l %d0,%d1 | _6,
jcs .L53 |And we end up with retaining both branches:
| include/linux/string.h:453: memcpy(dest, src, count);
move.l %d0,-(%sp) | _6,
pea .LC1 |
move.l %a1,-(%sp) | _7,
move.l %d0,-12(%fp) |,
move.l %a1,-16(%fp) |,
jsr memcpy |
| include/linux/string.h:454: memset(dest + count,
pad, dest_len - count);
move.l -12(%fp),%d0 |,
moveq #8,%d1 |,
sub.l %d0,%d1 | _6,
move.l %d1,-(%sp) |,
pea 32.w |
move.l -16(%fp),%a1 |,
pea (%a1,%d0.l) |
jsr memset |
lea (24,%sp),%sp |,
jra .L54 |
.L53:
| include/linux/string.h:456: memcpy(dest, src, dest_len);
move.l .LC1,(%a1) | MEM[(void *)"5.0.0+"], MEM[(void *)_7]
move.l .LC1+4,4(%a1) | MEM[(void *)"5.0.0+"], MEM[(void *)_7]But given the warning, the compiler must have devised that taking the second branch would read beyond the source buffer???
.L54:
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 58 lines · Geert Uytterhoeven
Quoted 37 lines · kbuild test robot
tree: https://git.kernel.org/pub/scm/linux/kernel/git/geert/linux-m68k.git master head: e223cadc191661c67cb419b3a53c7854ecc39e8b commit: e223cadc191661c67cb419b3a53c7854ecc39e8b [1174/1174] Merge tag 'v5.0' config: m68k-allmodconfig (attached as .config) compiler: m68k-linux-gnu-gcc (Debian 8.2.0-11) 8.2.0reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
git checkoute223cadc191661c67cb419b3a53c7854ecc39e8b
# save the attached .config to linux build tree
GCC_VERSION=8.2.0 make.cross ARCH=m68kAll warnings (new ones prefixed by >>):
In file included from
include/linux/string.h:20,
frominclude/linux/bitmap.h:9,
frominclude/linux/nodemask.h:95,
frominclude/linux/mmzone.h:17,
frominclude/linux/gfp.h:6,
frominclude/linux/umh.h:4,
frominclude/linux/kmod.h:22,
frominclude/linux/module.h:13,
fromdrivers/nvme/target/admin-cmd.c:15:
In function 'memcpy_and_pad',
inlined from 'nvmet_execute_identify_ctrl' atdrivers/nvme/target/admin-cmd.c:309:2:Quoted 1 line
Quoted 1 line
arch/m68k/include/asm/string.h:72:25: warning: '__builtin_memcpy' forming offset 8 is out of the bounds [0, 7] [-Warray-bounds]#define memcpy(d, s, n) __builtin_memcpy(d, s, n) ^~~~~~~~~~~~~~~~~~~~~~~~~ include/linux/string.h:456:3: note: in expansion of macro 'memcpy' memcpy(dest, src, dest_len); ^~~~~~vim +/__builtin_memcpy +72
arch/m68k/include/asm/string.hea61bc46 Greg Ungerer 2010-09-07 69 ea61bc46 Greg Ungerer 2010-09-07 70 #define __HAVE_ARCH_MEMCPY ea61bc46 Greg Ungerer 2010-09-07 71 extern void *memcpy(void *, const void *, __kernel_size_t);Quoted 8 lines
ea61bc46Greg Ungerer 2010-09-07 @72 #definememcpy(d, s, n)__builtin_memcpy(d, s, n)ea61bc46Greg Ungerer 2010-09-07 73:::::: The code at line 72 was first introduced by commit
::::::ea61bc461d09e8d331a307916530aaae808c72a2m68k/m68knommu: merge MMU and non-MMUstring.h:::::: TO: Greg Ungerer <[email protected]> :::::: CC: Geert Uytterhoeven <[email protected]>
Yep, that's a funny one, which I saw myself this morning, and decided to dive into.
Apparently this is due 1) the recently added -ffreestanding and
2) the kernel version being shorter than 8 characters (indeed, it didn't happen with allmodconfig on v5.0.0-rcX).The offending code is:
memcpy_and_pad(id->fr, sizeof(id->fr), UTS_RELEASE, strlen(UTS_RELEASE), ' ');
Quoted 9 lines
with
UTS_RELEASEbeing "5.0.0+", which calls into:static inline void memcpy_and_pad(void *dest,
size_tdest_len,
const void *src,size_tcount, int pad)
{
if (dest_len> count) {Of course this branch is taken, right?
Apparently that's not known at compile time. The compiler knows dest_len is 8 but apparently doesn't know what count is, because -ffreestanding means it can't evaluate strlen(UTS_RELEASE). If you change that to __builtin_strlen(UTS_RELEASE), the problem goes away.
Quoted 53 lines
memcpy(dest, src, count); memset(dest + count, pad, dest_len - count); } else memcpy(dest, src, dest_len); }This assembles to:
.LC1: .string "5.0.0+" | drivers/nvme/target/admin-cmd.c:311: memcpy_and_pad(id->fr,
sizeof(id->fr),
pea .LC1 |
jsr strlen |Woops, gcc no longer optimizes this away, due to -ffreestanding.
| drivers/nvme/target/admin-cmd.c:311: memcpy_and_pad(id->fr, sizeof(id->fr), lea (64,%a3),%a1 |, ret, _7 | include/linux/string.h:452: if (dest_len > count) { lea (16,%sp),%sp |, moveq #7,%d1 |, cmp.l %d0,%d1 | _6, jcs .L53 |And we end up with retaining both branches:
| include/linux/string.h:453: memcpy(dest, src, count); move.l %d0,-(%sp) | _6, pea .LC1 | move.l %a1,-(%sp) | _7, move.l %d0,-12(%fp) |, move.l %a1,-16(%fp) |, jsr memcpy | | include/linux/string.h:454: memset(dest + count, pad, dest_len - count); move.l -12(%fp),%d0 |, moveq #8,%d1 |, sub.l %d0,%d1 | _6, move.l %d1,-(%sp) |, pea 32.w | move.l -16(%fp),%a1 |, pea (%a1,%d0.l) | jsr memset | lea (24,%sp),%sp |, jra .L54 | .L53: | include/linux/string.h:456: memcpy(dest, src, dest_len); move.l .LC1,(%a1) | MEM[(void *)"5.0.0+"], MEM[(void *)_7] move.l .LC1+4,4(%a1) | MEM[(void *)"5.0.0+"], MEM[(void *)_7]
Quoted 3 lines
But given the warning, the compiler must have devised that taking the second branch would read beyond the source buffer???
Looks bogus to me.
If you change memcpy to __builtin_memcpy, then we avoid the macro and the warning changes to,
./include/linux/string.h:456:3: warning: '__builtin_memcpy' forming offset [7, 8] is out of the bounds [0, 6] [-Warray-bounds]
__builtin_memcpy(dest, src, dest_len);
The compiler has nothing to complain about here. dest is known to be id->fr and dest_len is known to be sizeof(id->fr).
The error message indicates that gcc has applied the bounds [0, 6] to dest when in fact those are the bounds for src.
Quoted 8 lines
.L54:Gr{oetje,eeting}s,
Geert
Quoted 6 lines · Finn Thain
Looks bogus to me.
If you change memcpy to
__builtin_memcpy, then we avoid the macro and the warning changes to,
Quoted 9 lines
./include/linux/string.h:456:3: warning: '__builtin_memcpy' forming offset [7, 8] is out of the bounds [0, 6] [-Warray-bounds]
__builtin_memcpy(dest, src, dest_len);The compiler has nothing to complain about here. dest is known to be
id->franddest_lenis known to besizeof(id->fr).The error message indicates that gcc has applied the bounds [0, 6] to dest when in fact those are the bounds for src.
My mistake. GCC is right, it seems memcpy will read past the end of "5.0.0+".
Geert Uytterhoeven
maintainerHi Finn,
Quoted 12 lines · Finn Thain
Quoted 11 lines · Finn Thain
Looks bogus to me.
If you change memcpy to
__builtin_memcpy, then we avoid the macro and the warning changes to,
./include/linux/string.h:456:3: warning: '__builtin_memcpy' forming offset [7, 8] is out of the bounds [0, 6] [-Warray-bounds]
__builtin_memcpy(dest, src, dest_len);The compiler has nothing to complain about here. dest is known to be
id->franddest_lenis known to besizeof(id->fr).
Quoted 6 lines
Quoted 3 lines
The error message indicates that gcc has applied the bounds [0, 6] to dest when in fact those are the bounds for src.
My mistake. GCC is right, it seems memcpy will read past the end of "5.0.0+".
But only if the else branch is taken, which is not the case.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 13 lines · Geert Uytterhoeven
Quoted 12 lines · Finn Thain
Quoted 11 lines · Finn Thain
Looks bogus to me.
If you change memcpy to
__builtin_memcpy, then we avoid the macro and the warning changes to,
./include/linux/string.h:456:3: warning: '__builtin_memcpy' forming offset [7, 8] is out of the bounds [0, 6] [-Warray-bounds]
__builtin_memcpy(dest, src, dest_len);The compiler has nothing to complain about here. dest is known to be
id->franddest_lenis known to besizeof(id->fr).
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
The error message indicates that gcc has applied the bounds [0, 6] to dest when in fact those are the bounds for src.
My mistake. GCC is right, it seems memcpy will read past the end of "5.0.0+".
But only if the else branch is taken, which is not the case.
You and I know that, because we can see what values get passed to memcpy_and_pad(). But how is gcc to know that?
If we replace strlen with __builtin_strlen, this problem goes away. It's interesting that the kernel's strlen implementation in include/linux/string.h can't achieve this.
Quoted 5 lines
Gr{oetje,eeting}s,
Geert
Geert Uytterhoeven
maintainerHi Finn,
Quoted 18 lines · Finn Thain
Quoted 17 lines · Geert Uytterhoeven
Quoted 16 lines · Finn Thain
Quoted 14 lines · Finn Thain
Looks bogus to me.
If you change memcpy to
__builtin_memcpy, then we avoid the macro and the warning changes to,
./include/linux/string.h:456:3: warning: '__builtin_memcpy' forming offset [7, 8] is out of the bounds [0, 6] [-Warray-bounds]
__builtin_memcpy(dest, src, dest_len);The compiler has nothing to complain about here. dest is known to be
id->franddest_lenis known to besizeof(id->fr).The error message indicates that gcc has applied the bounds [0, 6] to dest when in fact those are the bounds for src.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
My mistake. GCC is right, it seems memcpy will read past the end of "5.0.0+".
But only if the else branch is taken, which is not the case.
You and I know that, because we can see what values get passed to
memcpy_and_pad(). But how is gcc to know that?
Gcc also sees (partly) what values get passed, else it would not give that warning.
Still, should gcc give warnings based on branches that may or may not be taken? I guess there are lots of cases in the kernel where this could lead to false positives.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 2 lines · Finn Thain
interesting that the kernel's strlen implementation in
include/linux/string.hcan't achieve this.
This implementation is only available if ARCH_HAS_FORTIFY_SOURCE.
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."
Quoted 7 lines · Andreas Schwab
Quoted 2 lines · Finn Thain
interesting that the kernel's strlen implementation in
include/linux/string.hcan't achieve this.This implementation is only available if
ARCH_HAS_FORTIFY_SOURCE.
I see. Perhaps we could add another definition to that file:
#if !defined(__NO_FORTIFY) && defined(__OPTIMIZE__) && defined(CONFIG_FORTIFY_SOURCE)
...
#else__FORTIFY_INLINE __kernel_size_t strlen(const char *p)
{
return __builtin_strlen(p);
}#endif
I didn't test that. But the following patch seems to work...
arch/m68k/include/asm/string.h +2 -0
@@ -71,4 +71,6 @@ extern void *memset(void *, int, __kernel_size_t);extern void *memcpy (void *,const void *,__kernel_size_t );#define memcpy (d, s, n) __builtin_memcpy(d, s, n) +#define strlen (s) __builtin_strlen(s) +#endif /* _M68K_STRING_H_ */
lib/string.c +1 -0
@@ -472,6 +472,7 @@ char *strim(char *s)EXPORT_SYMBOL (strim);#ifndef __HAVE_ARCH_STRLEN +#undef strlen/** * strlen - Find the length of a string * @s: The string to be sized
Quoted 3 lines
Andreas.
Geert Uytterhoeven
maintainerHi Finn,
Quoted 27 lines · Finn Thain
Quoted 7 lines · Andreas Schwab
Quoted 2 lines · Finn Thain
interesting that the kernel's strlen implementation in
include/linux/string.hcan't achieve this.This implementation is only available if
ARCH_HAS_FORTIFY_SOURCE.I see. Perhaps we could add another definition to that file:
#if !defined(__NO_FORTIFY) &&
defined(__OPTIMIZE__)&&defined(CONFIG_FORTIFY_SOURCE)
...
#else__FORTIFY_INLINE__kernel_size_tstrlen(const char *p){ return __builtin_strlen(p); }#endif
I didn't test that. But the following patch seems to work...
arch/m68k/include/asm/string.h +0
-0
arch/m68k/include/asm/string.h extern void *memset(void *, int, __kernel_size_t); · line 74
Quoted 4 lines
arch/m68k/include/asm/string.h +1
-0@@ -71,4 +71,6 @@ extern void *memset(void *, int, __kernel_size_t);extern void *memcpy (void *,const void *,__kernel_size_t );#define memcpy (d, s, n) __builtin_memcpy(d, s, n) +#define strlen (s) __builtin_strlen(s)
Shouldn't you add
#define __HAVE_ARCH_STRLEN
here...
lib/string.c char *strim(char *s) · line 475
Quoted 11 lines
arch/m68k/include/asm/string.h +1
-0+#endif /* _M68K_STRING_H_ */ lib/string.c +1
-0@@ -472,6 +472,7 @@ char *strim(char *s)EXPORT_SYMBOL (strim);#ifndef __HAVE_ARCH_STRLEN +#undef strlen
... so you can drop this change?
lib/string.c char *strim(char *s) · line 478
Quoted 3 lines
lib/string.c +0
-0/** * strlen - Find the length of a string * @s: The string to be sized
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 21 lines · Geert Uytterhoeven
Quoted 20 lines · Finn Thain
Quoted 7 lines · Andreas Schwab
Quoted 2 lines · Finn Thain
interesting that the kernel's strlen implementation in
include/linux/string.hcan't achieve this.This implementation is only available if
ARCH_HAS_FORTIFY_SOURCE.I see. Perhaps we could add another definition to that file:
#if !defined(__NO_FORTIFY) &&
defined(__OPTIMIZE__)&&defined(CONFIG_FORTIFY_SOURCE)
...
#else__FORTIFY_INLINE__kernel_size_tstrlen(const char *p){ return __builtin_strlen(p); }#endif
Quoted 1 line
Quoted 1 line
I didn't test that.
I've tested it now, it works too. This may be a better solution than defining a strlen macro.
include/linux/string.h +7 -0
@@ -436,6 +436,13 @@ __FORTIFY_INLINE char *strcpy(char *p, const char *q)return p; } +#else + +__FORTIFY_INLINE __kernel_size_t strlen(const char *p) +{ +return __builtin_strlen (p); +} +#endif /**
Quoted 10 lines
But the following patch seems to work...
Quoted 9 lines
arch/m68k/include/asm/string.h +0
-0@@ -71,4 +71,6 @@ extern void *memset(void *, int, __kernel_size_t);extern void *memcpy (void *,const void *,__kernel_size_t );#define memcpy (d, s, n) __builtin_memcpy(d, s, n)
Quoted 8 lines
arch/m68k/include/asm/string.h extern void *memset(void *, int, __kernel_size_t); · line 74
Quoted 1 line
arch/m68k/include/asm/string.h +1
-0+#define strlen (s) __builtin_strlen(s)Shouldn't you add
#define __HAVE_ARCH_STRLENhere...
No, the link fails because the compiler still emits some references to strlen().
Quoted 23 lines
lib/string.c char *strim(char *s) · line 475
Quoted 11 lines
arch/m68k/include/asm/string.h +1
-0+#endif /* _M68K_STRING_H_ */ lib/string.c +1
-0@@ -472,6 +472,7 @@ char *strim(char *s)EXPORT_SYMBOL (strim);#ifndef __HAVE_ARCH_STRLEN +#undef strlen... so you can drop this change?
lib/string.c char *strim(char *s) · line 478
Quoted 3 lines
lib/string.c +0
-0/** * strlen - Find the length of a string * @s: The string to be sized Gr{oetje,eeting}s,
Geert
Geert Uytterhoeven
maintainerHi Finn,
Quoted 60 lines · Finn Thain
Quoted 21 lines · Geert Uytterhoeven
Quoted 20 lines · Finn Thain
Quoted 7 lines · Andreas Schwab
Quoted 2 lines · Finn Thain
interesting that the kernel's strlen implementation in
include/linux/string.hcan't achieve this.This implementation is only available if
ARCH_HAS_FORTIFY_SOURCE.I see. Perhaps we could add another definition to that file:
#if !defined(__NO_FORTIFY) &&
defined(__OPTIMIZE__)&&defined(CONFIG_FORTIFY_SOURCE)
...
#else__FORTIFY_INLINE__kernel_size_tstrlen(const char *p){ return __builtin_strlen(p); }#endif
Quoted 1 line
Quoted 1 line
I didn't test that.
I've tested it now, it works too. This may be a better solution than defining a strlen macro.
include/linux/string.h +7
-0@@ -436,6 +436,13 @@ __FORTIFY_INLINE char *strcpy(char *p, const char *q)return p; } +#else + +__FORTIFY_INLINE __kernel_size_t strlen(const char *p) +{ +return __builtin_strlen (p); +} +#endif /** Quoted 15 lines
But the following patch seems to work...
arch/m68k/include/asm/string.h extern void *memset(void *, int, __kernel_size_t); · line 74
Quoted 10 lines
arch/m68k/include/asm/string.h +1
-0@@ -71,4 +71,6 @@ extern void *memset(void *, int, __kernel_size_t);extern void *memcpy (void *,const void *,__kernel_size_t );#define memcpy (d, s, n) __builtin_memcpy(d, s, n) +#define strlen (s) __builtin_strlen(s)Shouldn't you add
Quoted 7 lines
Quoted 4 lines
#define __HAVE_ARCH_STRLENhere...
No, the link fails because the compiler still emits some references to
strlen().
Despite -ffreestanding?!?
Quoted 9 lines
Quoted 9 lines
lib/string.c char *strim(char *s) · line 475
Quoted 7 lines
lib/string.c +1
-0@@ -472,6 +472,7 @@ char *strim(char *s)EXPORT_SYMBOL (strim);#ifndef __HAVE_ARCH_STRLEN +#undef strlen... so you can drop this change?
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 7 lines · Geert Uytterhoeven
Hi Finn,
Quoted 2 lines · Finn Thain
No, the link fails because the compiler still emits some references to
strlen().Despite -ffreestanding?!?
*Because* of -ffreestanding. Without that, strlen would be recognized and turned into __builtin_strlen.
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."
Geert Uytterhoeven
maintainerHi Andreas,
Quoted 9 lines · Andreas Schwab
Quoted 5 lines · Geert Uytterhoeven
Quoted 2 lines · Finn Thain
No, the link fails because the compiler still emits some references to
strlen().Despite -ffreestanding?!?
*Because* of -ffreestanding. Without that, strlen would be recognized and turned into
__builtin_strlen.
Now I'm confused: if we have a static inline or #define for strlen(), why would the compiler still emit references to the strlen() symbol?
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 8 lines · Geert Uytterhoeven
Hi Andreas,
Quoted 5 lines · Andreas Schwab
Quoted 4 lines · Geert Uytterhoeven
Quoted 2 lines · Finn Thain
No, the link fails because the compiler still emits some references to
strlen().
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Despite -ffreestanding?!?
*Because* of -ffreestanding. Without that, strlen would be recognized and turned into
__builtin_strlen.Now I'm confused: if we have a static inline or #define for
strlen(),
Do you?
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."
Geert Uytterhoeven
maintainerHi Andreas,
Quoted 7 lines · Andreas Schwab
Quoted 6 lines · Geert Uytterhoeven
Quoted 5 lines · Andreas Schwab
Quoted 4 lines · Geert Uytterhoeven
Quoted 2 lines · Finn Thain
No, the link fails because the compiler still emits some references to
strlen().
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Despite -ffreestanding?!?
*Because* of -ffreestanding. Without that, strlen would be recognized and turned into
__builtin_strlen.Now I'm confused: if we have a static inline or #define for
strlen(),Do you?
I don't, but Finn's patch has, IINM.
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 10 lines · Geert Uytterhoeven
Quoted 9 lines · Andreas Schwab
Quoted 8 lines · Geert Uytterhoeven
Quoted 7 lines · Andreas Schwab
Quoted 5 lines · Geert Uytterhoeven
Quoted 2 lines · Finn Thain
No, the link fails because the compiler still emits some references to
strlen().Despite -ffreestanding?!?
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
*Because* of -ffreestanding. Without that, strlen would be recognized and turned into
__builtin_strlen.Now I'm confused: if we have a static inline or #define for
strlen(),Do you?
I don't, but Finn's patch has, IINM.
You're mixing up two separate patches there. One uses a #define and the other uses a forced inline function. We were discussing the former patch when I answered your question about __HAVE_ARCH_STRLEN (which got snipped).
m68k doesn't define __HAVE_ARCH_STRLEN and relies on the strlen() implementation in lib/string.c. The former patch doesn't alter this but reduces the number of callers because some call sites get optimized away. That's how it avoids the warning you raised.
Anyway, I don't like pre-processor kludges. So I did another experiment with the latter (forced inline) approach, to see if some optimizations can still be used with -ffreestanding.
include/linux/string.h +52 -0
@@ -436,6 +436,58 @@ __FORTIFY_INLINE char *strcpy(char *p, const char *q)return p; } +#else + +//__FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size) +//{ +// return __builtin_strncpy(p, q, size); +//} + +__FORTIFY_INLINE char *strcat(char *p, const char *q) +{ +return __builtin_strcat (p, q); +} + +__FORTIFY_INLINE __kernel_size_t strlen(const char *p) +{ +return __builtin_strlen (p); +} + +__FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) +{ +return __builtin_strncat (p, q, count); +} + +__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size) +{ +return __builtin_memset (p, c, size); +} + +//__FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) +//{ +// return __builtin_memcpy(p, q, size); +//} + +__FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) +{ +return __builtin_memmove (p, q, size); +} + +__FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size) +{ +return __builtin_memcmp (p, q, size); +} + +__FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size) +{ +return __builtin_memchr (p, c, size); +} + +__FORTIFY_INLINE char *strcpy(char *p, const char *q) +{ +return __builtin_strcpy (p, q); +} +#endif /**
The result of this patch really is confusing. It still suppresses the warning you raised:
arch/m68k/include/asm/string.h:72:25: warning: '__builtin_memcpy' forming offset 8 is out of the bounds [0, 7] [-Warray-bounds]
#define memcpy(d, s, n) __builtin_memcpy(d, s, n)
^~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/string.h:456:3: note: in expansion of macro 'memcpy'
memcpy(dest, src, dest_len);
^~~~~~But it also causes new ones, because of __builtin_memset():
drivers/video/fbdev/core/fbcvt.c: In function 'fb_find_mode_cvt':drivers/video/fbdev/core/fbcvt.c:312:16: warning: 'cvt.flags' may be used uninit ialized in this function [-Wmaybe-uninitialized]
cvt.flags |= FB_CVT_FLAG_MARGINS;
^~Apparently the compiler doesn't understand that __builtin_memset() has the effect of initialization. Weird.
Quoted 5 lines
Gr{oetje,eeting}s,
Geert
Quoted 81 lines · On Tue, 12 Mar 2019, I wrote:
... I did another experiment with the latter (forced inline) approach, to see if some optimizations can still be used with -ffreestanding.
include/linux/string.h +52
-0@@ -436,6 +436,58 @@ __FORTIFY_INLINE char *strcpy(char *p, const char *q)return p; } +#else + +//__FORTIFY_INLINE char *strncpy(char *p, const char *q, __kernel_size_t size) +//{ +// return __builtin_strncpy(p, q, size); +//} + +__FORTIFY_INLINE char *strcat(char *p, const char *q) +{ +return __builtin_strcat (p, q); +} + +__FORTIFY_INLINE __kernel_size_t strlen(const char *p) +{ +return __builtin_strlen (p); +} + +__FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count) +{ +return __builtin_strncat (p, q, count); +} + +__FORTIFY_INLINE void *memset(void *p, int c, __kernel_size_t size) +{ +return __builtin_memset (p, c, size); +} + +//__FORTIFY_INLINE void *memcpy(void *p, const void *q, __kernel_size_t size) +//{ +// return __builtin_memcpy(p, q, size); +//} + +__FORTIFY_INLINE void *memmove(void *p, const void *q, __kernel_size_t size) +{ +return __builtin_memmove (p, q, size); +} + +__FORTIFY_INLINE int memcmp(const void *p, const void *q, __kernel_size_t size) +{ +return __builtin_memcmp (p, q, size); +} + +__FORTIFY_INLINE void *memchr(const void *p, int c, __kernel_size_t size) +{ +return __builtin_memchr (p, c, size); +} + +__FORTIFY_INLINE char *strcpy(char *p, const char *q) +{ +return __builtin_strcpy (p, q); +} +#endif /** The result of this patch really is confusing. It still suppresses the warning you raised:
arch/m68k/include/asm/string.h:72:25: warning: '__builtin_memcpy' forming offset 8 is out of the bounds [0, 7] [-Warray-bounds]#define memcpy(d, s, n) __builtin_memcpy(d, s, n) ^~~~~~~~~~~~~~~~~~~~~~~~~ include/linux/string.h:456:3: note: in expansion of macro 'memcpy' memcpy(dest, src, dest_len); ^~~~~~But it also causes new ones, because of
__builtin_memset():
Quoted 8 lines
drivers/video/fbdev/core/fbcvt.c: In function 'fb_find_mode_cvt':drivers/video/fbdev/core/fbcvt.c:312:16: warning: 'cvt.flags' may be used uninit ialized in this function [-Wmaybe-uninitialized]cvt.flags |= FB_CVT_FLAG_MARGINS; ^~Apparently the compiler doesn't understand that
__builtin_memset()has the effect of initialization. Weird.
The other weird thing is that this warning doesn't show up when CONFIG_FORTIFY_SOURCE=y, even though the technique is much the same, that is, __builtin_memset() gets wrapped in a static inline memset() function.
Anyway, a quick and dirty microbenchmark under qemu-m68k shows that this patch reduces system time for 'time find / -false' by approx. 10%.
Interestingly, the -ffreestanding option doesn't make much difference to this particular microbenchmark.