Skip to content

Commit

Permalink
o1vm/mips: use u64 to avoid overflow when bitlength is 0
Browse files Browse the repository at this point in the history
When bitlength = 0 and when compiling with `-Coverflow-checks=y`, the runtime
alerts us that there is an overflow while shifting.
Lifting the type to u64 and cast the value as u32 when the computation is done.
  • Loading branch information
dannywillems committed Dec 22, 2024
1 parent d1e0008 commit a709048
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion o1vm/src/interpreters/mips/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,13 @@ pub trait InterpreterEnv {
let pos = self.alloc_scratch();
unsafe { self.bitmask(x, bitlength, bitlength - 1, pos) }
};
high_bit * Self::constant(((1 << (32 - bitlength)) - 1) << bitlength) + x.clone()
// Casting in u64 for special case of bitlength = 0 to avoid overflow.
// No condition for constant time execution.
// Decomposing the steps for readability.
let v: u64 = (1u64 << (32 - bitlength)) - 1;
let v: u64 = v << bitlength;
let v: u32 = v as u32;
high_bit * Self::constant(v) + x.clone()
}

fn report_exit(&mut self, exit_code: &Self::Variable);
Expand Down

0 comments on commit a709048

Please sign in to comment.