CensoredUsername / dynasm-rs

A dynasm-like tool for rust.
https://censoredusername.github.io/dynasm-rs/language/index.html
Mozilla Public License 2.0
716 stars 52 forks source link

Panic when trying to use 0xff as immediate for a logical operation #60

Closed ptitSeb closed 2 years ago

ptitSeb commented 2 years ago

trying to use dynasm!(self ; and w0, w0, 255); Will lead to thread '<unnamed>' panicked at 'attempt to shift left with overflow', /home/pi/.cargo/registry/src/github.com-1ecc6299db9ec823/dynasmrt-1.2.0/src/aarch64.rs:208:27

(the element_size is 32)

0xff can be converted to Logical Immediate. It should lead to immr==0 and imms==7

CensoredUsername commented 2 years ago

Thanks for the report. Looks like the logical immediate code accidentally overflows a left shift when the element size is the word size. Should be a simple question of replacing it by .checked_shl().unwrap_or(0). I'll get to it tomorrow probably.

CensoredUsername commented 2 years ago

Aside of that, was this the actual code? Or was the 255 actually an expression. I would expect this error to be raised at compile time if it was a literal.

ptitSeb commented 2 years ago

the 255 wasn't a direct litteral. I have a fn emit_and(&mut self, sz: Size, src1: Location, src2: Location, dst: Location) to handle the different cases of immediate or regs and different size of regs, using match (sz, src1, src2, dst) and things like

            (Size::S32, Location::GPR(src1), Location::Imm32(src2), Location::GPR(dst)) => {
                let src1 = src1.into_index() as u32;
                let src2 = src2 as u32;
                let dst = dst.into_index() as u32;
                if !encode_logical_immediate_32bit(src2).is_some() {
                    unreachable!();
                }
                dynasm!(self ; and W(dst), W(src1), src2);
            }
CensoredUsername commented 2 years ago

Makes sense, I couldn't reproduce the same error from your testcase, working on it now.

CensoredUsername commented 2 years ago

Should be fixed in 1.2.1

ptitSeb commented 2 years ago

Yes, it fixed the issue, thanks!