Skip to content

LLVM lint: Memory reference address is misaligned #28912

Closed

Description

@sanxiyn

This is a subissue of #7463.

pub enum H<A, B> {
    L(A),
    R(B),
}
pub fn test() -> H<i32, i64> {
    H::L(0)
}

Compile with rustc -C passes=lint and you get:

Undefined behavior: Memory reference address is misaligned
  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %2, i8* getelementptr inbounds ({ i8, i32, [8 x i8] }, { i8, i32, [8 x i8] }* @const3020, i32 0, i32 0), i64 16, i32 8, i1 false)

Manually setting alignment of const3020 to 8 in LLVM IR fixes the lint.

Activity

  1. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    on Oct 8, 2015
  2. dotdash commented on Oct 8, 2015

    @dotdash
    Contributor

    Seems to be caused by using the sizing_type for alignment.

  3. dotdash commented on Oct 8, 2015

    @dotdash
    Contributor

    Oh, nevermind, I completely misread the problem here and confused it with a different issue. The alignment of the sizing type is fine and the right thing would indeed be to add the alignment info to the constant.

  4. sanxiyn commented on Oct 8, 2015

    @sanxiyn
    ContributorAuthor

    Eh, I am confused. Isn't the alignment of { i8, i32, [8 x i8] } 4, i32 being the most aligned? Then why are we passing 8 as alignment to memcpy intrinsic? Is this related to sizing type?

  5. dotdash commented on Oct 8, 2015

    @dotdash
    Contributor

    { i8, i32, [8 x i8] } is just the specific representation for the L variant. The type H<i32, i64> is represented as %"H<i32, i64>" = type { i8, [7 x i8], [1 x i64] }, so its alignment is 8. And I think all our code deals with types rather than concrete variants when checking for alignment.

    We could probably extend that to allow for better packing of constants in some cases, but I'm not sure whether that's worth the trouble.

  6. added a commit that references this issue on Oct 10, 2015
    39376de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions