This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt] Mark $t* as clobbered for Linux/LoongArch syscalls
ClosedPublic

Authored by lixing-star on Nov 4 2022, 12:23 AM.

Details

Summary

Linux/LoongArch doesn't preserve temporary registers across syscalls,
so we have to explicitly mark them as clobbered to avoid trashing local variables.

Diff Detail

Event Timeline

lixing-star created this revision.Nov 4 2022, 12:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2022, 12:23 AM
lixing-star requested review of this revision.Nov 4 2022, 12:23 AM
This revision now requires changes to proceed.Nov 4 2022, 12:34 AM
xry111 added inline comments.Nov 4 2022, 12:36 AM
compiler-rt/lib/sanitizer_common/sanitizer_syscall_linux_loongarch64.inc
25

Only this part is needed.

lixing-star edited the summary of this revision. (Show Details)
lixing-star added a reviewer: wangleiat.

According to the ABI document, only t0~t8 need to be added to the clobber field

xry111 accepted this revision.Nov 4 2022, 12:53 AM

Thanks for fixing another instance of stupid programming errors made by me!

This revision is now accepted and ready to land.Nov 4 2022, 12:53 AM
xen0n accepted this revision.Nov 4 2022, 12:58 AM
xen0n retitled this revision from [compiler-rt] Add clobber registers for LoongArch syscall to [compiler-rt] Mark $t* as clobbered for Linux/LoongArch syscalls.
xen0n edited the summary of this revision. (Show Details)

Edited the patch summary for you so it's more concise. The code changes look good. Thanks!

lixing-star edited the summary of this revision. (Show Details)Nov 4 2022, 1:02 AM
tangyouling accepted this revision.Nov 4 2022, 1:08 AM
SixWeining accepted this revision.Nov 4 2022, 6:27 PM

LGTM.
But I see the comments in this file:

// Kernel ABI...
//  syscall number is passed in a7
//  (http://man7.org/linux/man-pages/man2/syscall.2.html) results are return in
//  a0 and a1 (http://man7.org/linux/man-pages/man2/syscall.2.html) arguments
//  are passed in: a0-a7 (confirmed by inspecting glibc sources).

// results are return in a0 and a1

Is this correct?

https://lore.kernel.org/loongarch/1f353678-3398-e30b-1c87-6edb278f74db@xen0n.name/T/#m4964e2aad6c5ee28ccd51d4b22f56d46f93fc369 says:

Upon return, `a0` contains the return value

xry111 added a comment.Nov 4 2022, 6:49 PM

LGTM.
But I see the comments in this file:

// Kernel ABI...
//  syscall number is passed in a7
//  (http://man7.org/linux/man-pages/man2/syscall.2.html) results are return in
//  a0 and a1 (http://man7.org/linux/man-pages/man2/syscall.2.html) arguments
//  are passed in: a0-a7 (confirmed by inspecting glibc sources).

// results are return in a0 and a1

Is this correct?

No. Another of my stupid errors, caused by copying from RISC-V without a close inspection.

xry111 added a comment.Nov 4 2022, 6:57 PM
-// Kernel ABI...
-//  syscall number is passed in a7
-//  (http://man7.org/linux/man-pages/man2/syscall.2.html) results are return in
-//  a0 and a1 (http://man7.org/linux/man-pages/man2/syscall.2.html) arguments
-//  are passed in: a0-a7 (confirmed by inspecting glibc sources).
+// Kernel ABI:
+// https://lore.kernel.org/loongarch/1f353678-3398-e30b-1c87-6edb278f74db@xen0n.name/T/#m1613bc86c2d7bf5f6da92bd62984302bfd699a2f
+//  syscall number is placed in a7
+//  parameters, if present, are placed in a0-a6
+//  upon return:
+//    the return value is placed in a0
+//    t0-t8 should be considered clobbered
+//    all other registers are preserved

Is it possible to add this part into this diff? If not I'll create a new diff.

-// Kernel ABI...
-//  syscall number is passed in a7
-//  (http://man7.org/linux/man-pages/man2/syscall.2.html) results are return in
-//  a0 and a1 (http://man7.org/linux/man-pages/man2/syscall.2.html) arguments
-//  are passed in: a0-a7 (confirmed by inspecting glibc sources).
+// Kernel ABI:
+// https://lore.kernel.org/loongarch/1f353678-3398-e30b-1c87-6edb278f74db@xen0n.name/T/#m1613bc86c2d7bf5f6da92bd62984302bfd699a2f
+//  syscall number is placed in a7
+//  parameters, if present, are placed in a0-a6
+//  upon return:
+//    the return value is placed in a0
+//    t0-t8 should be considered clobbered
+//    all other registers are preserved

Is it possible to add this part into this diff? If not I'll create a new diff.

ok, I will change the comment and submit again

compiler-rt/lib/sanitizer_common/sanitizer_syscall_linux_loongarch64.inc
25

yes, you are right, the handle_syscall saved a0~a7 already

xry111 accepted this revision.Nov 4 2022, 7:34 PM
SixWeining accepted this revision.Nov 4 2022, 7:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 11 2022, 5:24 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript