Repository navigation
modify armv7_tick prototype to return uint64_t instead of uint32_t - #17555
marler8997 wants to merge 1 commit into
Conversation
Addresses an ABI issue on 32-bit arm. The implementation of armv7_tick calls the mrrc instruction to retrieve a counter from the p15 coprocessor. This is a 64-bit counter and it is stored into the 2 32-bit registers r0 and r1. The problem is that the C prototype only declares the function as returning a 32-bit value, so the compiler is not aware that the r1 register is being clobbered. This results in undefined behavior and I confirmed that I would get unpredictable results with a small test app that duplicates this problem. Modifying the return type to uint64_t fixed the unpredictable results that I observed. I confirmed in the 32-bit arm ABI documentation that a 64-bit result from a function is stored in r0 and r1, which would inform the compiler that r1 is being overwritten by the armv7_tick function properly. This could be related to: openssl#14838 openssl#17009 openssl#17465 CLA: trivial
|
I'm a bit surprised, since r1-r3 are scratch registers. |
|
To build the example you'll need the zig compiler: https://ziglang.org/download/ Source code is here:
const std = @import("std");
// Use T = u32 to reproduce ABI issue, T = u64 will fix it
const T = u32;
fn armv7_tick() callconv(.C) T {
return asm (
\\ mrrc p15,1,%[ret],r1,c14
: [ret] "={r0}" (-> T)
:
//: "r1" (NOTE: telling compiler that r1 is clobbered will also fix ABI issue)
);
}
var val: T = 0;
const val_ptr: *volatile T = &val;
pub export fn _start() noreturn {
var i: usize = 0;
log("starting armv7_tick call loop...", .{});
// NOTE: using while(true) won't reproduce the segfault from using incorrect u32 ABI
while (i < 10) : (i += 1) {
val_ptr.* = armv7_tick();
if ((i % 100000000) == 0) {
log("{}: last value was 0x{x}", .{i, val_ptr.*});
}
}
log("done, last value was {}", .{val_ptr.*});
std.os.exit(0);
}
pub fn panic(msg: []const u8, trace: ?*std.builtin.StackTrace) noreturn {
_ = trace;
std.log.err("panic: {s}", .{msg});
std.os.exit(1);
}
fn log(comptime msg: []const u8, args: anytype) void {
std.io.getStdOut().writer().print(msg ++ "\n", args) catch @panic("write to stdout failed");
}Build with: zig build-exe -O ReleaseSmall -target arm-linux-musl cntvct.zig --single-threadedThis will cross-compile the code for arm32, the executable will be named SELinux also prints this: The error goes away if I turn the |
|
But with your patch the OPENSSL_rdtsc function changes just from this: to this: so it still clobbers r1, but is less performance, since it has two additional instructions. |
|
Remember that this is "undefined behavior" which means that it could work or may not work. I gave you an example where it produces a segfault, but I don't know what your example with your toolchain and your optimization/sanitazation/whatever flags do. In the example I gave you where it produces a segfault because of the clobber, the diff between that and telling the compiler that the clobber occurs is this (original on left, fixed on right): So the original is this: And the fixed is this: If I modify the ABI to use |
|
I should also clarify I'm not familiar enough with arm asm to critique the assembly you provided. But you said that |
|
But your compiler does inline this function, the ABI is not about inline, it is about call conventions for external functions. |
|
Whether it's inlined doesn't matter, the compiler still doesn't know that the And a correction on my assembly diff. What I gave you was from GodBolt but it's slightly different from what my local compilation did. Here is the diff with objdump before and after I let the compiler know the assembly clobbers the Also note that the next line is: In the bad version, the compiler loads the |
|
According to gcc and Microsoft, R1 is not saved by functions. Overwriting it should not cause problems for the caller. As @bernd-edlinger noted, your inline assembler will cause problems because it isn't a function call and it says that it doesn't clobber R1. I'd still like to see our rdtsc function return a 64 bit value but I don't think this is the underlying issue. |
|
Oh ok, so it looks like C compilers assume r0 through r3 are never guaranteed. It looks in Zig's inline assembly it doesn't assume this and requires the clobbers to be explicit. Thanks for the pointers |
|
Exactly. The same thing would happen with inline assembly for any compiler. |

Addresses an ABI issue on 32-bit arm. The implementation of
armv7_tickcalls themrrcinstruction to retrieve a counter from the p15 coprocessor. This is a 64-bit counter and it is stored into the 2 32-bit registers r0 and r1. The problem is that the C prototype only declares the function as returning a 32-bit value, so the compiler is not aware that the r1 register is being clobbered. This results in undefined behavior and I confirmed that I would get unpredictable results with a small test app that duplicates this problem. Modifying the return type to uint64_t fixed the unpredictable results that I observed. I confirmed in the 32-bit arm ABI documentation that a 64-bit result from a function is stored in r0 and r1, which would inform the compiler that r1 is being overwritten by the armv7_tick function properly.This could be related to:
#14838
#17009
#17465