Skip to contentSkip to conversation

ubsan: Implement __ubsan_handle_alignment_assumption

PATCH Superseded Nathan Chancellor 5 participants 14 messages Jan 12, 2021 – Jan 27, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ubsan: Implement __ubsan_handle_alignment_assumption

When building ARCH=mips 32r2el_defconfig with CONFIG_UBSAN_ALIGNMENT:

ld.lld: error: undefined symbol: __ubsan_handle_alignment_assumption

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

referenced by slab.h:557 (include/linux/slab.h:557)
main.o:(do_initcalls) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(rd_load_image) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(identify_ramdisk_image) in archive init/built-in.a
referenced 1579 more times

Implement this for the kernel based on LLVM's handleAlignmentAssumptionImpl because the kernel is not linked against the compiler runtime.

---
 lib/ubsan.c | 28 ++++++++++++++++++++++++++++
 lib/ubsan.h |  6 ++++++
 2 files changed, 34 insertions(+)
lib/ubsan.c +28 -0
index 3e3352f3d0da..a1e6cc9993f8 100644
@@ -427,3 +427,31 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
 	ubsan_epilogue();
 }
 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
+
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+					 unsigned long align,
+					 unsigned long offset)
+{
+	struct alignment_assumption_data *data = _data;
+	unsigned long real_ptr;
+
+	if (suppress_report(&data->location))
+		return;
+
+	ubsan_prologue(&data->location, "alignment-assumption");
+
+	if (offset)
+		pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
+		       align, offset, data->type->type_name);
+	else
+		pr_err("assumption of %lu byte alignment for pointer of type %s failed",
+		       align, data->type->type_name);
+
+	real_ptr = ptr - offset;
+	pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
+	       offset ? "offset " : "", BIT(ffs(real_ptr)),
+	       real_ptr & (align - 1));
+
+	ubsan_epilogue();
+}
+EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
lib/ubsan.h +6 -0
index 7b56c09473a9..9a0b71c5ff9f 100644
@@ -78,6 +78,12 @@ struct invalid_value_data {
 	struct type_descriptor *type;
 };
 
+struct alignment_assumption_data {
+	struct source_location location;
+	struct source_location assumption_location;
+	struct type_descriptor *type;
+};
+
 #if defined(CONFIG_ARCH_SUPPORTS_INT128)
 typedef __int128 s_max;
 typedef unsigned __int128 u_max;

base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837

Signature · 1 line
2.30.0
#1 lore raw
Quoted 45 lines · Nathan Chancellor

When building ARCH=mips 32r2el_defconfig with CONFIG_UBSAN_ALIGNMENT:

ld.lld: error: undefined symbol: __ubsan_handle_alignment_assumption

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

referenced by slab.h:557 (include/linux/slab.h:557)
main.o:(do_initcalls) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(rd_load_image) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(identify_ramdisk_image) in archive init/built-in.a
referenced 1579 more times

Implement this for the kernel based on LLVM's handleAlignmentAssumptionImpl because the kernel is not linked against the compiler runtime.

---
 lib/ubsan.c | 28 ++++++++++++++++++++++++++++
 lib/ubsan.h |  6 ++++++
 2 files changed, 34 insertions(+)
lib/ubsan.c +13 -0
index 3e3352f3d0da..a1e6cc9993f8 100644
@@ -427,3 +427,31 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
        ubsan_epilogue();
 }
 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
+
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+                                        unsigned long align,
+                                        unsigned long offset)
+{
+       struct alignment_assumption_data *data = _data;
+       unsigned long real_ptr;
+
+       if (suppress_report(&data->location))
+               return;
+
+       ubsan_prologue(&data->location, "alignment-assumption");
+

lib/ubsan.c void __ubsan_handle_load_invalid_value(void *_data, void *val) · line 452

Quoted 10 lines
lib/ubsan.c +10 -0
+       if (offset)
+               pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
+                      align, offset, data->type->type_name);
+       else
+               pr_err("assumption of %lu byte alignment for pointer of type %s failed",
+                      align, data->type->type_name);
+
+       real_ptr = ptr - offset;
+       pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
+              offset ? "offset " : "", BIT(ffs(real_ptr)),

if real_ptr is an unsigned long, do we want to use __ffs(real_ptr) + 1 here rather than ffs which takes an int? It seems the kernel is missing a definition of ffsl. :(

Otherwise code LGTM.

Quoted 27 lines
lib/ubsan.c +5 -0
+              real_ptr & (align - 1));
+
+       ubsan_epilogue();
+}
+EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
lib/ubsan.h +6 -0
index 7b56c09473a9..9a0b71c5ff9f 100644
@@ -78,6 +78,12 @@ struct invalid_value_data {
        struct type_descriptor *type;
 };
 
+struct alignment_assumption_data {
+       struct source_location location;
+       struct source_location assumption_location;
+       struct type_descriptor *type;
+};
+
 #if defined(CONFIG_ARCH_SUPPORTS_INT128)
 typedef __int128 s_max;
 typedef unsigned __int128 u_max;

base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837 --
2.30.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
Quoted 53 lines · Nick Desaulniers
Quoted 51 lines · Nathan Chancellor

When building ARCH=mips 32r2el_defconfig with CONFIG_UBSAN_ALIGNMENT:

ld.lld: error: undefined symbol: __ubsan_handle_alignment_assumption

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

referenced by slab.h:557 (include/linux/slab.h:557)
main.o:(do_initcalls) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(rd_load_image) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(identify_ramdisk_image) in archive init/built-in.a
referenced 1579 more times

Implement this for the kernel based on LLVM's handleAlignmentAssumptionImpl because the kernel is not linked against the compiler runtime.

---
 lib/ubsan.c | 28 ++++++++++++++++++++++++++++
 lib/ubsan.h |  6 ++++++
 2 files changed, 34 insertions(+)
lib/ubsan.c +19 -0
index 3e3352f3d0da..a1e6cc9993f8 100644
@@ -427,3 +427,31 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
        ubsan_epilogue();
 }
 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
+
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+                                        unsigned long align,
+                                        unsigned long offset)
+{
+       struct alignment_assumption_data *data = _data;
+       unsigned long real_ptr;
+
+       if (suppress_report(&data->location))
+               return;
+
+       ubsan_prologue(&data->location, "alignment-assumption");
+
+       if (offset)
+               pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
+                      align, offset, data->type->type_name);
+       else
+               pr_err("assumption of %lu byte alignment for pointer of type %s failed",
+                      align, data->type->type_name);
Quoted 8 lines

lib/ubsan.c void __ubsan_handle_load_invalid_value(void *_data, void *val) · line 452

Quoted 4 lines
lib/ubsan.c +4 -0
+
+       real_ptr = ptr - offset;
+       pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
+              offset ? "offset " : "", BIT(ffs(real_ptr)),

if real_ptr is an unsigned long, do we want to use __ffs(real_ptr) + 1 here rather than ffs which takes an int? It seems the kernel is missing a definition of ffsl. :(

Why the + 1? I think if we use __ffs (which it seems like we should), I think that needs to become

BIT(real_ptr ? __ffs(real_ptr) : 0)

I have made that change locally and will send it for v2 in a day or so to give Kees some time to check it out.

Thanks for the review!
Nathan

#3 lore raw
Quoted 7 lines · Nathan Chancellor
Quoted 4 lines

if real_ptr is an unsigned long, do we want to use __ffs(real_ptr) + 1 here rather than ffs which takes an int? It seems the kernel is missing a definition of ffsl. :(

Why the + 1? I think if we use __ffs (which it seems like we should), I think that needs to become

This came up recently in an internal code review; ffs and __ffs differ in output by one. See also the definition of ffs for alpha in arch/alpha/include/asm/bitops.h.

Also, I just confirmed that:

```
#include <stdio.h>

// include/asm-generic/bitops/ffs.h
static inline int ffs(int x)
{
        int r = 1;

        if (!x)
                return 0;
        if (!(x & 0xffff)) {
                x >>= 16;
                r += 16;
        }
        if (!(x & 0xff)) {
                x >>= 8;
                r += 8;
        }
        if (!(x & 0xf)) {
                x >>= 4;
                r += 4;
        }
        if (!(x & 3)) {
                x >>= 2;
                r += 2;
        }
        if (!(x & 1)) {
                x >>= 1;
                r += 1;
        }
        return r;
}

// include/asm-generic/bitops/__ffs.h
static __always_inline unsigned long __ffs(unsigned long word)
{
        int num = 0;

        if ((word & 0xffffffff) == 0) {
                num += 32;
                word >>= 32;
        }
        if ((word & 0xffff) == 0) {
                num += 16;
                word >>= 16;
        }
        if ((word & 0xff) == 0) {
                num += 8;
                word >>= 8;
        }
        if ((word & 0xf) == 0) {
                num += 4;
                word >>= 4;
        }
        if ((word & 0x3) == 0) {
                num += 2;
                word >>= 2;
        }
        if ((word & 0x1) == 0)
                num += 1;
        return num;
}

int main() {
    int x = 3;
    unsigned long y = 3;
    printf("%d\n%lu\n", ffs(x), __ffs(y));
    return 0;
}
```

will print:
1
0

Signature · 2 lines
Thanks,
~Nick Desaulniers
#4 lore raw
Quoted 7 lines · Nick Desaulniers
Quoted 5 lines · Nathan Chancellor
Quoted 4 lines

if real_ptr is an unsigned long, do we want to use __ffs(real_ptr) + 1 here rather than ffs which takes an int? It seems the kernel is missing a definition of ffsl. :(

Quoted 6 lines
Quoted 2 lines

Why the + 1? I think if we use __ffs (which it seems like we should), I think that needs to become

This came up recently in an internal code review; ffs and __ffs differ in output by one. See also the definition of ffs for alpha in arch/alpha/include/asm/bitops.h.

Interesting, thanks for bringing it up! Looks like ffs returns 1-32 and __ffs returns 0-31. I think that we want __ffs here because we are shifting (1UL << 32 overflows on 32-bit architectures) and the code in LLVM appears to agree. LeastSignificantSetBitIndex evaluates to __builtin_ctzl, which is the asm-generic implementation of __ffs.

Cheers,
NAthan

Acked-by
#5 lore raw
Quoted 15 lines · Nathan Chancellor
Quoted 7 lines · Nick Desaulniers
Quoted 5 lines · Nathan Chancellor
Quoted 4 lines

if real_ptr is an unsigned long, do we want to use __ffs(real_ptr) + 1 here rather than ffs which takes an int? It seems the kernel is missing a definition of ffsl. :(

Quoted 6 lines
Quoted 2 lines

Why the + 1? I think if we use __ffs (which it seems like we should), I think that needs to become

This came up recently in an internal code review; ffs and __ffs differ in output by one. See also the definition of ffs for alpha in arch/alpha/include/asm/bitops.h.

Quoted 5 lines

Interesting, thanks for bringing it up! Looks like ffs returns 1-32 and __ffs returns 0-31. I think that we want __ffs here because we are shifting (1UL << 32 overflows on 32-bit architectures) and the code in LLVM appears to agree. LeastSignificantSetBitIndex evaluates to __builtin_ctzl, which is the asm-generic implementation of __ffs.

Sounds good. With __ffs, consider your v2:

Signature · 1 line
Kees Cook
#6 lore raw
In reply to Nathan Chancellor
I love your patch! Perhaps something to improve:

Hi Nathan,

I love your patch! Perhaps something to improve:

[auto build test WARNING on 7c53f6b671f4aba70ff15e1b05148b10d58c2837]

url:    https://github.com/0day-ci/linux/commits/Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714
base:    7c53f6b671f4aba70ff15e1b05148b10d58c2837
config: m68k-allmodconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/775adad26a60878926c0ee6cd460a1375bbe51e6
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714 git checkout 775adad26a60878926c0ee6cd460a1375bbe51e6
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=m68k

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

lib/ubsan.c:192:6: warning: no previous prototype for '__ubsan_handle_add_overflow' [-Wmissing-prototypes]
192 | void __ubsan_handle_add_overflow(void *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/ubsan.c:200:6: warning: no previous prototype for '__ubsan_handle_sub_overflow' [-Wmissing-prototypes]
200 | void __ubsan_handle_sub_overflow(void *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/ubsan.c:207:6: warning: no previous prototype for '__ubsan_handle_mul_overflow' [-Wmissing-prototypes]
207 | void __ubsan_handle_mul_overflow(void *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/ubsan.c:214:6: warning: no previous prototype for '__ubsan_handle_negate_overflow' [-Wmissing-prototypes]
214 | void __ubsan_handle_negate_overflow(void *_data, void *old_val)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/ubsan.c:234:6: warning: no previous prototype for '__ubsan_handle_divrem_overflow' [-Wmissing-prototypes]
234 | void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/ubsan.c:315:6: warning: no previous prototype for '__ubsan_handle_type_mismatch' [-Wmissing-prototypes]
315 | void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib/ubsan.c:329:6: warning: no previous prototype for '__ubsan_handle_type_mismatch_v1' [-Wmissing-prototypes]

     329 | void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/ubsan.c:343:6: warning: no previous prototype for '__ubsan_handle_out_of_bounds' [-Wmissing-prototypes]
     343 | void __ubsan_handle_out_of_bounds(void *_data, void *index)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/ubsan.c:360:6: warning: no previous prototype for '__ubsan_handle_shift_out_of_bounds' [-Wmissing-prototypes]
     360 | void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/ubsan.c:402:6: warning: no previous prototype for '__ubsan_handle_builtin_unreachable' [-Wmissing-prototypes]
     402 | void __ubsan_handle_builtin_unreachable(void *_data)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/ubsan.c:412:6: warning: no previous prototype for '__ubsan_handle_load_invalid_value' [-Wmissing-prototypes]
     412 | void __ubsan_handle_load_invalid_value(void *_data, void *val)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> lib/ubsan.c:431:6: warning: no previous prototype for '__ubsan_handle_alignment_assumption' [-Wmissing-prototypes]

431 | void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/__ubsan_handle_alignment_assumption +431 lib/ubsan.c

359

 > 360	void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
   361	{
   362		struct shift_out_of_bounds_data *data = _data;
   363		struct type_descriptor *rhs_type = data->rhs_type;
   364		struct type_descriptor *lhs_type = data->lhs_type;
   365		char rhs_str[VALUE_LENGTH];
   366		char lhs_str[VALUE_LENGTH];
   367		unsigned long ua_flags = user_access_save();
   368	
   369		if (suppress_report(&data->location))
   370			goto out;
   371	
   372		ubsan_prologue(&data->location, "shift-out-of-bounds");
   373	
   374		val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
   375		val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
   376	
   377		if (val_is_negative(rhs_type, rhs))
   378			pr_err("shift exponent %s is negative\n", rhs_str);
   379	
   380		else if (get_unsigned_val(rhs_type, rhs) >=
   381			type_bit_width(lhs_type))
   382			pr_err("shift exponent %s is too large for %u-bit type %s\n",
   383				rhs_str,
   384				type_bit_width(lhs_type),
   385				lhs_type->type_name);
   386		else if (val_is_negative(lhs_type, lhs))
   387			pr_err("left shift of negative value %s\n",
   388				lhs_str);
   389		else
   390			pr_err("left shift of %s by %s places cannot be"
   391				" represented in type %s\n",
   392				lhs_str, rhs_str,
   393				lhs_type->type_name);
   394	
   395		ubsan_epilogue();
   396	out:
   397		user_access_restore(ua_flags);
   398	}
   399	EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
   400	
   401	
   402	void __ubsan_handle_builtin_unreachable(void *_data)
   403	{
   404		struct unreachable_data *data = _data;
   405		ubsan_prologue(&data->location, "unreachable");
   406		pr_err("calling __builtin_unreachable()\n");
   407		ubsan_epilogue();
   408		panic("can't return from __builtin_unreachable()");
   409	}
   410	EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
   411	
   412	void __ubsan_handle_load_invalid_value(void *_data, void *val)
   413	{
   414		struct invalid_value_data *data = _data;
   415		char val_str[VALUE_LENGTH];
   416	
   417		if (suppress_report(&data->location))
   418			return;
   419	
   420		ubsan_prologue(&data->location, "invalid-load");
   421	
   422		val_to_string(val_str, sizeof(val_str), data->type, val);
   423	
   424		pr_err("load of value %s is not a valid value for type %s\n",
   425			val_str, data->type->type_name);
   426	
   427		ubsan_epilogue();
   428	}
   429	EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
   430	
 > 431	void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw
In reply to Nathan Chancellor
I love your patch! Perhaps something to improve:

Hi Nathan,

I love your patch! Perhaps something to improve:

[auto build test WARNING on 7c53f6b671f4aba70ff15e1b05148b10d58c2837]

url:    https://github.com/0day-ci/linux/commits/Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714
base:    7c53f6b671f4aba70ff15e1b05148b10d58c2837
config: arm64-randconfig-r031-20210112 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 32bcfcda4e28375e5a85268d2acfabcfcc011abf)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/775adad26a60878926c0ee6cd460a1375bbe51e6
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714 git checkout 775adad26a60878926c0ee6cd460a1375bbe51e6
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

lib/ubsan.c:192:6: warning: no previous prototype for function '__ubsan_handle_add_overflow' [-Wmissing-prototypes] void __ubsan_handle_add_overflow(void *data,

        ^
   lib/ubsan.c:192:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_add_overflow(void *data,
   ^
   static 
   lib/ubsan.c:200:6: warning: no previous prototype for function '__ubsan_handle_sub_overflow' [-Wmissing-prototypes]
   void __ubsan_handle_sub_overflow(void *data,
        ^
   lib/ubsan.c:200:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_sub_overflow(void *data,
   ^
   static 
   lib/ubsan.c:207:6: warning: no previous prototype for function '__ubsan_handle_mul_overflow' [-Wmissing-prototypes]
   void __ubsan_handle_mul_overflow(void *data,
        ^
   lib/ubsan.c:207:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_mul_overflow(void *data,
   ^
   static 
   lib/ubsan.c:214:6: warning: no previous prototype for function '__ubsan_handle_negate_overflow' [-Wmissing-prototypes]
   void __ubsan_handle_negate_overflow(void *_data, void *old_val)
        ^
   lib/ubsan.c:214:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_negate_overflow(void *_data, void *old_val)
   ^
   static 
   lib/ubsan.c:234:6: warning: no previous prototype for function '__ubsan_handle_divrem_overflow' [-Wmissing-prototypes]
   void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
        ^
   lib/ubsan.c:234:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_divrem_overflow(void *_data, void *lhs, void *rhs)
   ^
   static 
   lib/ubsan.c:315:6: warning: no previous prototype for function '__ubsan_handle_type_mismatch' [-Wmissing-prototypes]
   void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
        ^
   lib/ubsan.c:315:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
   ^
   static 
   lib/ubsan.c:329:6: warning: no previous prototype for function '__ubsan_handle_type_mismatch_v1' [-Wmissing-prototypes]
   void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
        ^
   lib/ubsan.c:329:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_type_mismatch_v1(void *_data, void *ptr)
   ^
   static 
   lib/ubsan.c:343:6: warning: no previous prototype for function '__ubsan_handle_out_of_bounds' [-Wmissing-prototypes]
   void __ubsan_handle_out_of_bounds(void *_data, void *index)
        ^
   lib/ubsan.c:343:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_out_of_bounds(void *_data, void *index)
   ^
   static 
   lib/ubsan.c:360:6: warning: no previous prototype for function '__ubsan_handle_shift_out_of_bounds' [-Wmissing-prototypes]
   void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
        ^
   lib/ubsan.c:360:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_shift_out_of_bounds(void *_data, void *lhs, void *rhs)
   ^
   static 
   lib/ubsan.c:402:6: warning: no previous prototype for function '__ubsan_handle_builtin_unreachable' [-Wmissing-prototypes]
   void __ubsan_handle_builtin_unreachable(void *_data)
        ^
   lib/ubsan.c:402:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_builtin_unreachable(void *_data)
   ^
   static 
   lib/ubsan.c:412:6: warning: no previous prototype for function '__ubsan_handle_load_invalid_value' [-Wmissing-prototypes]
   void __ubsan_handle_load_invalid_value(void *_data, void *val)
        ^
   lib/ubsan.c:412:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_load_invalid_value(void *_data, void *val)
   ^
   static 
>> lib/ubsan.c:431:6: warning: no previous prototype for function '__ubsan_handle_alignment_assumption' [-Wmissing-prototypes]
   void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
        ^

lib/ubsan.c:431:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
^
static
12 warnings generated.

vim +/__ubsan_handle_alignment_assumption +431 lib/ubsan.c

411

 > 412	void __ubsan_handle_load_invalid_value(void *_data, void *val)
   413	{
   414		struct invalid_value_data *data = _data;
   415		char val_str[VALUE_LENGTH];
   416	
   417		if (suppress_report(&data->location))
   418			return;
   419	
   420		ubsan_prologue(&data->location, "invalid-load");
   421	
   422		val_to_string(val_str, sizeof(val_str), data->type, val);
   423	
   424		pr_err("load of value %s is not a valid value for type %s\n",
   425			val_str, data->type->type_name);
   426	
   427		ubsan_epilogue();
   428	}
   429	EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
   430	
 > 431	void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#8 lore raw
Quoted 37 lines · kernel test robot

Hi Nathan,

I love your patch! Perhaps something to improve:

[auto build test WARNING on 7c53f6b671f4aba70ff15e1b05148b10d58c2837]

url:    https://github.com/0day-ci/linux/commits/Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714
base:    7c53f6b671f4aba70ff15e1b05148b10d58c2837
config: arm64-randconfig-r031-20210112 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 32bcfcda4e28375e5a85268d2acfabcfcc011abf)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/775adad26a60878926c0ee6cd460a1375bbe51e6
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714 git checkout 775adad26a60878926c0ee6cd460a1375bbe51e6
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

lib/ubsan.c:192:6: warning: no previous prototype for function '__ubsan_handle_add_overflow' [-Wmissing-prototypes] void __ubsan_handle_add_overflow(void *data,

        ^
   lib/ubsan.c:192:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_add_overflow(void *data,
   ^
   static 
   lib/ubsan.c:200:6: warning: no previous prototype for function '__ubsan_handle_sub_overflow' [-Wmissing-prototypes]
   void __ubsan_handle_sub_overflow(void *data,
        ^
Quoted 3 lines

lib/ubsan.c:200:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void __ubsan_handle_sub_overflow(void *data,
^

Given that these are compiler inserted functions, there is not much of a point to having prototypes to them. If people feel shutting these warnings up is worthwhile, we can just add the prototypes right above the function definition in a follow up patch.

Cheers,
Nathan

#9 lore raw
Quoted 43 lines · Nathan Chancellor
Quoted 37 lines · kernel test robot

Hi Nathan,

I love your patch! Perhaps something to improve:

[auto build test WARNING on 7c53f6b671f4aba70ff15e1b05148b10d58c2837]

url:    https://github.com/0day-ci/linux/commits/Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714
base:    7c53f6b671f4aba70ff15e1b05148b10d58c2837
config: arm64-randconfig-r031-20210112 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 32bcfcda4e28375e5a85268d2acfabcfcc011abf)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/775adad26a60878926c0ee6cd460a1375bbe51e6
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714 git checkout 775adad26a60878926c0ee6cd460a1375bbe51e6
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

lib/ubsan.c:192:6: warning: no previous prototype for function '__ubsan_handle_add_overflow' [-Wmissing-prototypes] void __ubsan_handle_add_overflow(void *data,

        ^
   lib/ubsan.c:192:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_add_overflow(void *data,
   ^
   static
   lib/ubsan.c:200:6: warning: no previous prototype for function '__ubsan_handle_sub_overflow' [-Wmissing-prototypes]
   void __ubsan_handle_sub_overflow(void *data,
        ^
Quoted 3 lines

lib/ubsan.c:200:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void __ubsan_handle_sub_overflow(void *data,
^

Quoted 4 lines

Given that these are compiler inserted functions, there is not much of a point to having prototypes to them. If people feel shutting these warnings up is worthwhile, we can just add the prototypes right above the function definition in a follow up patch.

Same as stpcpy; it would be nice though. ;)

Signature · 2 lines
Thanks,
~Nick Desaulniers
#10 lore raw
Quoted 46 lines · Nick Desaulniers
Quoted 43 lines · Nathan Chancellor
Quoted 40 lines · kernel test robot

Hi Nathan,

I love your patch! Perhaps something to improve:

[auto build test WARNING on 7c53f6b671f4aba70ff15e1b05148b10d58c2837]

url:    https://github.com/0day-ci/linux/commits/Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714
base:    7c53f6b671f4aba70ff15e1b05148b10d58c2837
config: arm64-randconfig-r031-20210112 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 32bcfcda4e28375e5a85268d2acfabcfcc011abf)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/775adad26a60878926c0ee6cd460a1375bbe51e6
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Nathan-Chancellor/ubsan-Implement-__ubsan_handle_alignment_assumption/20210113-055714 git checkout 775adad26a60878926c0ee6cd460a1375bbe51e6
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

lib/ubsan.c:192:6: warning: no previous prototype for function '__ubsan_handle_add_overflow' [-Wmissing-prototypes] void __ubsan_handle_add_overflow(void *data,

        ^
   lib/ubsan.c:192:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   void __ubsan_handle_add_overflow(void *data,
   ^
   static
   lib/ubsan.c:200:6: warning: no previous prototype for function '__ubsan_handle_sub_overflow' [-Wmissing-prototypes]
   void __ubsan_handle_sub_overflow(void *data,
        ^

lib/ubsan.c:200:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void __ubsan_handle_sub_overflow(void *data,
^

Quoted 6 lines
Quoted 4 lines

Given that these are compiler inserted functions, there is not much of a point to having prototypes to them. If people feel shutting these warnings up is worthwhile, we can just add the prototypes right above the function definition in a follow up patch.

Same as stpcpy; it would be nice though. ;)

If you would be so kind, I'd be happy to sign off on such a v3.

Quoted 3 lines

--
Thanks,
~Nick Desaulniers

Signature · 2 lines
Thanks,
~Nick Desaulniers
#11 lore raw

PATCH v2 1/1 ubsan: Implement __ubsan_handle_alignment_assumption

In reply to Nathan Chancellor

When building ARCH=mips 32r2el_defconfig with CONFIG_UBSAN_ALIGNMENT:

ld.lld: error: undefined symbol: __ubsan_handle_alignment_assumption

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

referenced by slab.h:557 (include/linux/slab.h:557)
main.o:(do_initcalls) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(rd_load_image) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(identify_ramdisk_image) in archive init/built-in.a
referenced 1579 more times

Implement this for the kernel based on LLVM's handleAlignmentAssumptionImpl because the kernel is not linked against the compiler runtime.

---

v1 -> v2:

* Use __ffs instead of ffs because due to size of input (unsigned long vs int) and we want a zero based index (Nick Desaulniers).

* Pick up Kees's ack.

 lib/ubsan.c | 28 ++++++++++++++++++++++++++++
 lib/ubsan.h |  6 ++++++
 2 files changed, 34 insertions(+)
lib/ubsan.c +28 -0
index 3e3352f3d0da..1432a8645224 100644
@@ -427,3 +427,31 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
 	ubsan_epilogue();
 }
 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
+
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+					 unsigned long align,
+					 unsigned long offset)
+{
+	struct alignment_assumption_data *data = _data;
+	unsigned long real_ptr;
+
+	if (suppress_report(&data->location))
+		return;
+
+	ubsan_prologue(&data->location, "alignment-assumption");
+
+	if (offset)
+		pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
+		       align, offset, data->type->type_name);
+	else
+		pr_err("assumption of %lu byte alignment for pointer of type %s failed",
+		       align, data->type->type_name);
+
+	real_ptr = ptr - offset;
+	pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
+	       offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
+	       real_ptr & (align - 1));
+
+	ubsan_epilogue();
+}
+EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
lib/ubsan.h +6 -0
index 7b56c09473a9..9a0b71c5ff9f 100644
@@ -78,6 +78,12 @@ struct invalid_value_data {
 	struct type_descriptor *type;
 };
 
+struct alignment_assumption_data {
+	struct source_location location;
+	struct source_location assumption_location;
+	struct type_descriptor *type;
+};
+
 #if defined(CONFIG_ARCH_SUPPORTS_INT128)
 typedef __int128 s_max;
 typedef unsigned __int128 u_max;

base-commit: 7c53f6b671f4aba70ff15e1b05148b10d58c2837

Signature · 1 line
2.30.0
#12 lore raw

PATCH v3 1/1 ubsan: Implement __ubsan_handle_alignment_assumption

When building ARCH=mips 32r2el_defconfig with CONFIG_UBSAN_ALIGNMENT:

ld.lld: error: undefined symbol: __ubsan_handle_alignment_assumption

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

referenced by slab.h:557 (include/linux/slab.h:557)
main.o:(do_initcalls) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(rd_load_image) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(identify_ramdisk_image) in archive init/built-in.a
referenced 1579 more times

Implement this for the kernel based on LLVM's handleAlignmentAssumptionImpl because the kernel is not linked against the compiler runtime.

---

v2 -> v3:

* Add prototype right above definition to avoid introducing a warning with W=1.

v1 -> v2:

* Use __ffs instead of ffs because due to size of input (unsigned long vs int) and we want a zero based index (Nick Desaulniers).

* Pick up Kees's ack.

 lib/ubsan.c | 31 +++++++++++++++++++++++++++++++
 lib/ubsan.h |  6 ++++++
 2 files changed, 37 insertions(+)
lib/ubsan.c +31 -0
index 3e3352f3d0da..bec38c64d6a6 100644
@@ -427,3 +427,34 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
 	ubsan_epilogue();
 }
 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
+
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+					 unsigned long align,
+					 unsigned long offset);
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+					 unsigned long align,
+					 unsigned long offset)
+{
+	struct alignment_assumption_data *data = _data;
+	unsigned long real_ptr;
+
+	if (suppress_report(&data->location))
+		return;
+
+	ubsan_prologue(&data->location, "alignment-assumption");
+
+	if (offset)
+		pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
+		       align, offset, data->type->type_name);
+	else
+		pr_err("assumption of %lu byte alignment for pointer of type %s failed",
+		       align, data->type->type_name);
+
+	real_ptr = ptr - offset;
+	pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
+	       offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
+	       real_ptr & (align - 1));
+
+	ubsan_epilogue();
+}
+EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
lib/ubsan.h +6 -0
index 7b56c09473a9..9a0b71c5ff9f 100644
@@ -78,6 +78,12 @@ struct invalid_value_data {
 	struct type_descriptor *type;
 };
 
+struct alignment_assumption_data {
+	struct source_location location;
+	struct source_location assumption_location;
+	struct type_descriptor *type;
+};
+
 #if defined(CONFIG_ARCH_SUPPORTS_INT128)
 typedef __int128 s_max;
 typedef unsigned __int128 u_max;

base-commit: 6ee1d745b7c9fd573fba142a2efdad76a9f1cb04

Signature · 1 line
2.30.0
Reviewed-by on v3 1/1
#13 lore raw
Quoted 12 lines · Nathan Chancellor

When building ARCH=mips 32r2el_defconfig with CONFIG_UBSAN_ALIGNMENT:

ld.lld: error: undefined symbol: __ubsan_handle_alignment_assumption

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

referenced by slab.h:557 (include/linux/slab.h:557)
main.o:(do_initcalls) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(rd_load_image) in archive init/built-in.a
referenced by slab.h:448 (include/linux/slab.h:448)
do_mounts_rd.o:(identify_ramdisk_image) in archive init/built-in.a
referenced 1579 more times

Quoted 8 lines

Implement this for the kernel based on LLVM's handleAlignmentAssumptionImpl because the kernel is not linked against the compiler runtime.

Thanks for the patch!

Quoted 79 lines

---

v2 -> v3:

* Add prototype right above definition to avoid introducing a warning with W=1.

v1 -> v2:

* Use __ffs instead of ffs because due to size of input (unsigned long vs int) and we want a zero based index (Nick Desaulniers).

* Pick up Kees's ack.

 lib/ubsan.c | 31 +++++++++++++++++++++++++++++++
 lib/ubsan.h |  6 ++++++
 2 files changed, 37 insertions(+)
lib/ubsan.c +31 -0
index 3e3352f3d0da..bec38c64d6a6 100644
@@ -427,3 +427,34 @@ void __ubsan_handle_load_invalid_value(void *_data, void *val)
        ubsan_epilogue();
 }
 EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
+
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+                                        unsigned long align,
+                                        unsigned long offset);
+void __ubsan_handle_alignment_assumption(void *_data, unsigned long ptr,
+                                        unsigned long align,
+                                        unsigned long offset)
+{
+       struct alignment_assumption_data *data = _data;
+       unsigned long real_ptr;
+
+       if (suppress_report(&data->location))
+               return;
+
+       ubsan_prologue(&data->location, "alignment-assumption");
+
+       if (offset)
+               pr_err("assumption of %lu byte alignment (with offset of %lu byte) for pointer of type %s failed",
+                      align, offset, data->type->type_name);
+       else
+               pr_err("assumption of %lu byte alignment for pointer of type %s failed",
+                      align, data->type->type_name);
+
+       real_ptr = ptr - offset;
+       pr_err("%saddress is %lu aligned, misalignment offset is %lu bytes",
+              offset ? "offset " : "", BIT(real_ptr ? __ffs(real_ptr) : 0),
+              real_ptr & (align - 1));
+
+       ubsan_epilogue();
+}
+EXPORT_SYMBOL(__ubsan_handle_alignment_assumption);
lib/ubsan.h +6 -0
index 7b56c09473a9..9a0b71c5ff9f 100644
@@ -78,6 +78,12 @@ struct invalid_value_data {
        struct type_descriptor *type;
 };
 
+struct alignment_assumption_data {
+       struct source_location location;
+       struct source_location assumption_location;
+       struct type_descriptor *type;
+};
+
 #if defined(CONFIG_ARCH_SUPPORTS_INT128)
 typedef __int128 s_max;
 typedef unsigned __int128 u_max;

base-commit: 6ee1d745b7c9fd573fba142a2efdad76a9f1cb04 --
2.30.0

Signature · 2 lines
Thanks,
~Nick Desaulniers