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

Apple/Darwin layout for thread state doesn't match in Apple Silicon (ARM) #1977

Closed
syrusakbary opened this issue Nov 19, 2020 · 0 comments · Fixed by #1990
Closed

Apple/Darwin layout for thread state doesn't match in Apple Silicon (ARM) #1977

syrusakbary opened this issue Nov 19, 2020 · 0 comments · Fixed by #1990
Labels
C-bug Category: bug

Comments

@syrusakbary
Copy link

syrusakbary commented Nov 19, 2020

When working on making Wasmer and work in the Apple Silicon, I realized there were some issues on the layout that rust libc thinks the thread state has, which are different in x86_64 and arm64.

Currently rust libc uses __darwin_x86_thread_state64 when accessing to ucontext_t.uc_mcontext.__ss, which has an invalid layout in the Apple ARM M1 context.

pub struct __darwin_x86_thread_state64 {
pub __rax: u64,
pub __rbx: u64,
pub __rcx: u64,
pub __rdx: u64,
pub __rdi: u64,
pub __rsi: u64,
pub __rbp: u64,
pub __rsp: u64,
pub __r8: u64,
pub __r9: u64,
pub __r10: u64,
pub __r11: u64,
pub __r12: u64,
pub __r13: u64,
pub __r14: u64,
pub __r15: u64,
pub __rip: u64,
pub __rflags: u64,
pub __cs: u64,
pub __fs: u64,
pub __gs: u64,
}

The structure in macOS Big Sur is defined as follows (mach/arm/_structs.h):

_STRUCT_ARM_THREAD_STATE64
{
	__uint64_t x[29];       /* General purpose registers x0-x28 */
	void*      __opaque_fp; /* Frame pointer x29 */
	void*      __opaque_lr; /* Link register x30 */
	void*      __opaque_sp; /* Stack pointer x31 */
	void*      __opaque_pc; /* Program counter */
	__uint32_t cpsr;        /* Current program status register */
	__uint32_t __opaque_flags; /* Flags describing structure format */
};

Which translates roughly to:

                    pub struct __darwin_arm_thread_state64 {
                        pub __x: [u64; 29], /* General purpose registers x0-x28 */
                        pub __fp: u64,    /* Frame pointer x29 */
                        pub __lr: u64,    /* Link register x30 */
                        pub __sp: u64,    /* Stack pointer x31 */
                        pub __pc: u64,   /* Program counter */
                        pub __cpsr: u32,  /* Current program status register */
                        pub __pad: u32,   /* Same size for 32-bit or 64-bit clients */
                    };

The codebase should be updated to use the new layout for the Apple Silicon

@syrusakbary syrusakbary added the C-bug Category: bug label Nov 19, 2020
bors added a commit that referenced this issue Dec 12, 2020
Fix `thread_state64` on `aarch64-apple-darwin`

Fixes #1977
bors added a commit that referenced this issue Dec 12, 2020
Fix `thread_state64` on `aarch64-apple-darwin`

Fixes #1977
@bors bors closed this as completed in 0ec925d Dec 13, 2020
Hywan added a commit to wasmerio/wasmer that referenced this issue Mar 5, 2021
Since libc 0.2.82, the issue
rust-lang/libc#1977 has been fixed with
rust-lang/libc#1990. Our current version of
libc is 0.2.87. Let's update our code to remove the
`__darwin_arm_thread_state64` type, and use the one from libc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant