1f08be4d7f
* Mark comptime int hardcoded address as a run time variable #1171 * test case for dereferencing hardcoded address intToPtr
28 lines
714 B
Zig
28 lines
714 B
Zig
const builtin = @import("builtin");
|
|
const std = @import("std");
|
|
const assertOrPanic = std.debug.assertOrPanic;
|
|
|
|
test "casting random address to function pointer" {
|
|
randomAddressToFunction();
|
|
comptime randomAddressToFunction();
|
|
}
|
|
|
|
fn randomAddressToFunction() void {
|
|
var addr: usize = 0xdeadbeef;
|
|
var ptr = @intToPtr(fn () void, addr);
|
|
}
|
|
|
|
test "mutate through ptr initialized with constant intToPtr value" {
|
|
forceCompilerAnalyzeBranchHardCodedPtrDereference(false);
|
|
}
|
|
|
|
fn forceCompilerAnalyzeBranchHardCodedPtrDereference(x: bool) void {
|
|
const hardCodedP = @intToPtr(*volatile u8, 0xdeadbeef);
|
|
if (x) {
|
|
hardCodedP.* = hardCodedP.* | 10;
|
|
} else {
|
|
return;
|
|
}
|
|
}
|
|
|