#Integer overflow while trying to patch jumps in a JIT
1 messages · Page 1 of 1 (latest)
pub const BasicBlock = struct {
instructions: std.ArrayList(Instruction),
jump_stack: std.ArrayList(usize),
offset: usize,
allocator: std.mem.Allocator,
index: usize
};
pub const Program = struct {
blocks: std.ArrayList(*BasicBlock),
allocator: std.mem.Allocator
};
in his video instead of appending .items.len he appends .size(), from what I know (Write Vector).size() returns m_size which should be the same
If anyone is interested, I can open the repo
and the emits:
pub inline fn emit8(self: *Assembler, v: u8) !void {
try self.m_output.append(v);
}
pub inline fn emit32(self: *Assembler, v: u32) !void {
try self.m_output.writer().writeIntLittle(u32, v);
}```
what's b.offset and j look like in this example
probably just going negative with bad arithmatic
as seen here
okay and what's 1-2?
i know what integer overflow is.
I wanna know why it does work in C++
i mean c++ isn't very safe but it did run
idk if it's different in c++, but in C, unsigned integer overflow is defined behavior
it just wraps around
but in zig, unsigned overflow is UB
which iteration gives off the error?
so it works if i only have two instructions in 2 blocks
1st block:
jmp 2nd block
2nd block:
ret
compiled to
jmp 0x5
ret
if i comment out these instructions
it actually compiles
if i uncomment atleast one of them, it fails
Maybe try debugging to see what is acutally happening? Either with std.debug.print or a debugger.
Block iter: {block iter index}={offset}, Jump iter: {jump iter index} = { jump }
well 100 - 300 is overflow for a usize
yep...
i am confused
this code probably has UB
it's saying block 1 has a jump but...
or your math is wrong
yep
but I wonder why c++ works
i mean
yeah
defined behaviour
but what should I do instead
Figure out what’s wrong with the logic. Either using a debugger or print debugging
what do you want me to debug?
you've done the job of transcribing someone elses code. Now do the hard work of understanding what the program is supposed to do and what is wrong with your transcription
Maybe start by doing the same printf debugging you've done so far on the C++ version and seeing what is different. These aren't really Zig related questions though.
the first revision of c++ code that was uplaoded was after a couple of episodes
but okay.
What do you expect someone trying to help you to do in the current situation?
anything but not say "Oh debug smthn. idk"
i'll just copy the c++ code and debug then
bro, there's no longer any zig issues. You haven't even posted your full code. You're asking someone to solve your problem without putting in the effort to solve it yourself
I sent code that is mostly related, and also I said I am willing to provide the entire source code
^
I have a feeling that something might be out of sequence.
but it looks exactly like a situation where you need to do just that – debug. It's not a situation that's related to zig at all. You have a bug, so go debug.
But it would be best if next time you trim more whatever you give us. Reduce your problem to the minimum that's required for it to display itself. The less noise, the more help. Most of the time, you'll have caught the bug yourself by that time.