Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime error when using UBSan with bfloat16 conversion intrinsic #61983

Closed
ostannard opened this issue Apr 6, 2023 · 2 comments
Closed

Runtime error when using UBSan with bfloat16 conversion intrinsic #61983

ostannard opened this issue Apr 6, 2023 · 2 comments

Comments

@ostannard
Copy link
Collaborator

ostannard commented Apr 6, 2023

When this code is compiled for AArch64 with UBSan, it reports a UBSan error, even though this is valid code:

#include <arm_neon.h>

float foo(__bf16 a) {
  return vcvtah_f32_bf16(a);
}
$ /work/llvm/build/bin/clang --target=aarch64--none-eabi -march=armv8.2-a+bf16 -c test.c -o - -S -O1 -fsanitize=undefined -fsanitize-minimal-runtime -fno-sanitize-recover
        .text
        .file   "test.c"
        .globl  foo                             // -- Begin function foo
        .p2align        2
        .type   foo,@function
foo:                                    // @foo
        .cfi_startproc
// %bb.0:                               // %entry
        stp     x29, x30, [sp, #-16]!           // 16-byte Folded Spill
        .cfi_def_cfa_offset 16
        mov     x29, sp
        .cfi_def_cfa w29, 16
        .cfi_offset w30, -8
        .cfi_offset w29, -16
        bl      __ubsan_handle_type_mismatch_minimal_abort
.Lfunc_end0:
        .size   foo, .Lfunc_end0-foo
        .cfi_endproc
                                        // -- End function
        .ident  "clang version 17.0.0 (git@github.com:llvm/llvm-project.git 626b7e5dd249f569203e024141c1a2a0f618df9c)"
        .section        ".note.GNU-stack","",@progbits
        .addrsig
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 6, 2023

@llvm/issue-subscribers-backend-aarch64

ostannard pushed a commit that referenced this issue Apr 29, 2024
do not bitcast 16bit `bfloat16` to 32bit `int32_t` directly
bitcast to `int16_t`, and then upcast to `int32_t`

Fix ASAN runtime error when calling vcvtah_f32_bf16
`==21842==ERROR: AddressSanitizer: stack-buffer-overflow on address
0x007fda1dd063 at pc 0x005c0361c234 bp 0x007fda1dd030 sp 0x007fda1dd028
`

without patch
```c
__ai __attribute__((target("bf16"))) float32_t vcvtah_f32_bf16(bfloat16_t __p0) {
  float32_t __ret;
bfloat16_t __reint = __p0;
int32_t __reint1 = *(int32_t *) &__reint << 16;
  __ret = *(float32_t *) &__reint1;
  return __ret;
}
```

with this patch
```c
__ai __attribute__((target("bf16"))) float32_t vcvtah_f32_bf16(bfloat16_t __p0) {
  float32_t __ret;
bfloat16_t __reint = __p0;
int32_t __reint1 = (int32_t)(*(int16_t *) &__reint) << 16;
  __ret = *(float32_t *) &__reint1;
  return __ret;
}
```

fix issue #61983
@ostannard
Copy link
Collaborator Author

Fixed by #89062.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants