Skip to content

Commit

Permalink
add ziglang/zig#9703 reproduction
Browse files Browse the repository at this point in the history
  • Loading branch information
190n committed May 16, 2024
1 parent 30ab718 commit a328071
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

zig build-exe repro.zig -O ReleaseSafe || exit 2
./repro 2>&1 | grep inactive
if [ $? -eq 0 ]; then
exit 0
else
exit 2
fi
55 changes: 55 additions & 0 deletions repro.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const std = @import("std");

const SideData = union {
foo: u8,
bar: u8,
};

const Inst = struct {
func: Gadget,
data: SideData,
};

const Emulator = struct {
memory: [128]u8,
code: [64]Inst,
pc: u8 = 0,
};

const Gadget = *const fn (*Emulator, SideData) void;

fn foo(emulator: *Emulator, side_data: SideData) void {
_ = emulator;
std.log.info("foo {}", .{side_data.foo});
}

fn bar(emulator: *Emulator, side_data: SideData) void {
_ = emulator;
_ = side_data;
@trap();
}

fn invalid(emulator: *Emulator, _: SideData) void {
_ = emulator;
}

fn decode(emulator: *Emulator, _: SideData) void {
const raw_instruction = std.mem.readInt(u16, emulator.memory[emulator.pc..][0..2], .big);
const inst: Inst = switch ((raw_instruction >> 8) & 0xf) {
0xf => .{ .func = &foo, .data = .{ .foo = @truncate(raw_instruction) } },
0xb => .{ .func = &bar, .data = .{ .bar = @truncate(raw_instruction) } },
else => .{ .func = &invalid, .data = undefined },
};
@call(.always_tail, inst.func, .{ emulator, inst.data });
}

pub fn main() void {
var emulator = Emulator{
.memory = undefined,
.code = undefined,
};
@memset(&emulator.code, .{ .func = &decode, .data = undefined });
const rom = [_]u8{ 0x0f, 0x23, 0x0b, 0x80 };
@memcpy(emulator.memory[0..rom.len], &rom);
emulator.code[emulator.pc].func(&emulator, emulator.code[emulator.pc].data);
}

0 comments on commit a328071

Please sign in to comment.