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

Weird code generation on RISC-V 64 #79

Open
gjz010 opened this issue May 8, 2021 · 1 comment
Open

Weird code generation on RISC-V 64 #79

gjz010 opened this issue May 8, 2021 · 1 comment

Comments

@gjz010
Copy link
Contributor

gjz010 commented May 8, 2021

The following code in memory.rs emits strange assembly code causing an infinite loop:

// T is specialized to some struct sized 336..
#[naked]
#[inline(never)]
#[link_section = ".text.copy_user"]
unsafe extern "C" fn write_user<T>(dst: *mut T, src: *const T) -> usize {
    dst.copy_from_nonoverlapping(src, 1);
    0
}

Generated assembly:

ffffffffc02000d6 <_ZN5rcore6memory12copy_to_user10write_user17h75f60bbde319fcafE>:
ffffffffc02000d6:	15000613          	li	a2,336 // struct size. 3rd argument of memcpy.
ffffffffc02000da:	00086097          	auipc	ra,0x86 // WHY IS IT USING ra FOR INTERMEDIATE ADDRESS CALCULATION?
ffffffffc02000de:	0f0080e7          	jalr	240(ra) # ffffffffc02861ca <memcpy> // now ra = ffffffffc02000e2 <li a0, 0>
ffffffffc02000e2:	4501                	li	a0,0 // BANG!
ffffffffc02000e4:	8082                	ret // goto BANG!

It seems that rustc optimizing copy_from_nonoverlapping into memcpy (in the naked function) results in this issue, but I'm not sure.

@wangrunji0408
Copy link
Member

Seems these codes were introduced by @jiegec:
92a9674#diff-daec1639ddc202cd1b84917f388b4323b107696dab34069cdb0af3133837102d

@jiegec 🍉🍉🍉

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

No branches or pull requests

2 participants