[llvm-dev] load with alignment of 1 crashes from being unaligned

Friedman, Eli via llvm-dev llvm-dev at lists.llvm.org
Mon Oct 2 12:17:44 PDT 2017


On 9/30/2017 6:49 PM, Andrew Kelley via llvm-dev wrote:
> Below is attached a full IR module that can reproduce this issue, but 
> the part to notice is this:
>
> %Foo96Bits = type <{ i24, i24, i24, i24 }>
>
> define internal fastcc i16 @main.0.1() unnamed_addr #2 !dbg !113 {
> Entry:
>   %value = alloca %Foo96Bits, align 1
>   %b = alloca i24, align 4
>   %0 = bitcast %Foo96Bits* %value to i8*, !dbg !129
>   call void @llvm.memcpy.p0i8.p0i8.i64(i8* %0, i8* bitcast 
> (%Foo96Bits* @4 to i8*), i64 16, i32 1, i1 false), !dbg !129
>   call void @llvm.dbg.declare(metadata %Foo96Bits* %value, metadata 
> !118, metadata !75), !dbg !129
>   %1 = getelementptr inbounds %Foo96Bits, %Foo96Bits* %value, i32 0, 
> i32 0, !dbg !130
>   %2 = load i24, i24* %1, align 1, !dbg !130
>   store i24 %2, i24* %b, align 4, !dbg !131
>   call void @llvm.dbg.declare(metadata i24* %b, metadata !127, 
> metadata !75), !dbg !131
>   ret i16 0, !dbg !132
> }
>
> Notice that the Foo96Bits struct is allocated with alignment value of 
> 1. Later on there is a load which also has this alignment value of 1.
>
> LLVM docs for alignment say: "An alignment of 1 is always safe." 
>  (http://llvm.org/docs/LangRef.html#id189)
>
> This program when run crashes with a segfault.
>
> However if I change the
>
>   %value = alloca %Foo96Bits, align 1
>
> to
>
>   %value = alloca %Foo96Bits, align 32
>
> and the corresponding load
>
>   %2 = load i24, i24* %1, align 32
>
> Then the program runs fine.
>
>
> I was unable to reproduce the issue with this IR code and clang, 
> because clang doesn't support cross compiling as seamlessly as my 
> frontend:
> $ clang -o test.exe test.ll -target x86_64-unknown-windows-msvc 
> -fuse-ld=lld -Wl,-entry:_start
> clang-5.0: warning: unable to find a Visual Studio installation; try 
> running Clang from a developer command prompt [-Wmsvc-not-found]
> /nix/store/jxm0qg5cazlv8h7f7kp6cgk7ka7ag5kr-lld-5.0.0rc3/bin/lld-link: 
> error: could not open libcmt.lib: No such file or directory
> clang-5.0: error: linker command failed with exit code 1 (use -v to 
> see invocation)
>
> But if you manage to get it linked I am sure this IR would trigger the 
> issue. It even causes the segfault in wine64 in addition to native 
> windows.
>
>
>
> Can someone confirm this is a bug in llvm? Or is the documentation 
> incorrect?

The documentation is correct; LLVM should always find some way to lower 
an unaligned load to a correct sequence. So probably an LLVM bug, unless 
you've done something weird like call the function with a misaligned stack.

-Eli

-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project



More information about the llvm-dev mailing list