Skip to content

Commit 7980746

Browse files
committed
Fix illegal behavior from syscalls on powerpc64le
On powerpc64le Linux, the registers used for passing syscall parameters (r4-r8, as well as r0 for the syscall number) are volatile, or caller-saved. However, Zig's syscall wrappers for this architecture do not include all such registers in the list of clobbers, leading the compiler to assume these registers will maintain their values after the syscall completes. In practice, this resulted in a segfault when allocating memory with `std.heap.SmpAllocator`, which calls `std.os.linux.sched_getaffinity`. The third parameter to `sched_getaffinity` is a pointer to a `cpu_set_t` and is stored in register r5. After the syscall, the code attempts to access data in the `cpu_set_t`, but because the compiler doesn't realize the value of r5 may have changed, it uses r5 as the memory address, which in practice resulted in a memory access at address 0x8. This commit adds all volatile registers to the list of clobbers.
1 parent 75d0ec9 commit 7980746

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/std/os/linux/powerpc64.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn syscall0(number: SYS) usize {
2222
\\ 1:
2323
: [ret] "={r3}" (-> usize),
2424
: [number] "{r0}" (@intFromEnum(number)),
25-
: "memory", "cr0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
25+
: "memory", "cr0", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
2626
);
2727
}
2828

@@ -35,7 +35,7 @@ pub fn syscall1(number: SYS, arg1: usize) usize {
3535
: [ret] "={r3}" (-> usize),
3636
: [number] "{r0}" (@intFromEnum(number)),
3737
[arg1] "{r3}" (arg1),
38-
: "memory", "cr0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
38+
: "memory", "cr0", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
3939
);
4040
}
4141

@@ -49,7 +49,7 @@ pub fn syscall2(number: SYS, arg1: usize, arg2: usize) usize {
4949
: [number] "{r0}" (@intFromEnum(number)),
5050
[arg1] "{r3}" (arg1),
5151
[arg2] "{r4}" (arg2),
52-
: "memory", "cr0", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
52+
: "memory", "cr0", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
5353
);
5454
}
5555

@@ -64,7 +64,7 @@ pub fn syscall3(number: SYS, arg1: usize, arg2: usize, arg3: usize) usize {
6464
[arg1] "{r3}" (arg1),
6565
[arg2] "{r4}" (arg2),
6666
[arg3] "{r5}" (arg3),
67-
: "memory", "cr0", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
67+
: "memory", "cr0", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
6868
);
6969
}
7070

@@ -80,7 +80,7 @@ pub fn syscall4(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize)
8080
[arg2] "{r4}" (arg2),
8181
[arg3] "{r5}" (arg3),
8282
[arg4] "{r6}" (arg4),
83-
: "memory", "cr0", "r7", "r8", "r9", "r10", "r11", "r12"
83+
: "memory", "cr0", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
8484
);
8585
}
8686

@@ -97,7 +97,7 @@ pub fn syscall5(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize,
9797
[arg3] "{r5}" (arg3),
9898
[arg4] "{r6}" (arg4),
9999
[arg5] "{r7}" (arg5),
100-
: "memory", "cr0", "r8", "r9", "r10", "r11", "r12"
100+
: "memory", "cr0", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
101101
);
102102
}
103103

@@ -123,7 +123,7 @@ pub fn syscall6(
123123
[arg4] "{r6}" (arg4),
124124
[arg5] "{r7}" (arg5),
125125
[arg6] "{r8}" (arg6),
126-
: "memory", "cr0", "r9", "r10", "r11", "r12"
126+
: "memory", "cr0", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12"
127127
);
128128
}
129129

0 commit comments

Comments
 (0)