Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unchecked arithmetic overflow / underflow #79

Open
morganthomas opened this issue Oct 30, 2023 · 1 comment
Open

Avoid unchecked arithmetic overflow / underflow #79

morganthomas opened this issue Oct 30, 2023 · 1 comment

Comments

@morganthomas
Copy link
Collaborator

The following arithmetic operations may result in unchecked overflow, resulting in different behavior in debug vs release compilation mode. For each of these, the desired resolution is likely (a) the overflow should wrap around, and this should be made explicit and made to happen in both debug and release mode, or (b) overflow is deemed to be impossible for all inputs, and should result in a panic with an informative error message. However, other resolutions are possible and these must be considered on a case by case basis.

  1. In machine/src/core.rs:
    a. In the implementation of Add:add for Word<u8>, on line 57
    b. In the implementation of Sub::sub for Word<u8>, on line 67
  2. In alu_u32/src/add/mod.rs:
    a. In the implementation of Add32Chip::op_to_row:
    i. The addition on line 108
    ii. The first addition on line 109
    iii. The second addition on line 109
    iv. The first addition on line 113
    v. The second addition on line 113
    b. In the implementation of Instruction<M>::execute for Add32Instruction:
    i. The addition on line 141 (computing read_addr_1)
    ii. The addition on line 142 (computing write_addr_1)
    iii. The addition on line 149 (computing read_addr_2)
    iv. The addition on line 153 (computing a)
  3. In cpu/src/lib.rs:
    a. In Instruction<M>::execute for ReadAdviceInstruction:
    i. The two additions on line 410
    ii. The multiplication on line 410
    b. In Instruction<M>::execute for WriteAdviceInstruction:
    i. The first and second additions on line 436 (i.e., fp + mem_addr)
    ii. The third addition on line 436 (i.e., (fp + mem_addr) + mem_buf_len)
    c. In Instruction<M>::execute for Load32Instruction:
    i. The addition on line 470 (computing read_addr_1)
    ii. The addition on line 472 (computing write_addr)
    d. In Instruction<M>::execute for Store32Instruction:
    i. The addition on line 491 (computing read_addr)
    ii. The addition on line 492 (computing write_addr)
    e. In Instruction<M>::execute for JalInstruction:
    i. The addition on line 512 (computing write_addr)
    ii. The addition on line 513 (computing next_pc)
    ii. The addition on line 518
    f. In Instruction<M>::execute for JalvInstruction:
    i. The addition on line 536 (computing write_addr)
    ii. The addition on line 537 (computing next_pc)
    iii. The addition on line 540 (computing read_addr)
    iv. The addition on line 543 (computing read_addr)
    v. The additive assignment on line 545
    g. In Instruction<M>::execute for BeqInstruction:
    i. The addition on line 562 (computing read_addr_1)
    ii. The addition on line 570 (computing read_addr_2)
    iii. The addition on line 576
    h. In Instruction<M>::execute for BneInstruction:
    i. The addition on line 594 (computing read_addr_1)
    ii. The addition on line 602 (computing read_addr_2)
    iii. The addition on line 608
    i. In Instruction<M>::execute for Imm32Instruction:
    i. The addition on line 624 (computing write_addr)
    j. Every instance of state.cpu_mut().pc += 1;
    k. In impl CpuChip:
    i. On line 655 and 660, self.pc += 1;
    ii. On line 668, self.clock += 1;
  4. In alu_u32/src/bitwise/mod.rs:
    a. In Instruction<M>::execute for Xor32Instruction:
    i. The addition on line 145 (computing read_addr_1)
    ii. The addition on line 146 (computing write_addr)
    iii. The addition on line 153 (computing read_addr_2)
    b. In Instruction<M>::execute for And32Instruction:
    i. The addition on line 181 (computing read_addr_1)
    ii. The addition on line 182 (computing write_addr)
    iii. The addition on line 190 (computing read_addr_2)
    c. In Instruction<M>::execute for Or32Instruction:
    i. The addition on line 217 (computing read_addr_1)
    ii. The addition on line 218 (computing write_addr)
    iii. The addition on line 225 (computing read_addr_2)
  5. In alu_u32/src/div/mod.rs, in Instruction<M>::execute for Div32Instruction:
    a. The addition on line 77 (computing read_addr_1)
    b. The addition on line 78 (computing write_addr)
    c. The addition on line 85 (computing read_addr_2)
  6. In alu_u32/src/lt/mod.rs, in Instruction<M>::execute for Lt32Instruction:
    a. The addition on line 128 (computing read_addr_1)
    b. The addition on line 129 (computing write_addr)
    c. The addition on line 136 (computing read_addr_2)
  7. In alu_u32/src/mul/mod.rs, in Instruction::execute for Mul32Instruction:
    a. The addition on line 123 (computing read_addr_1)
    b. The addition on line 124 (computing write_addr)
    c. The addition on line 131 (computing read_addr_2)
    d. The multiplication on line 135 (computing a)
  8. In alu_u32/src/shift/mod.rs:
    a. In Instruction<M>::execute for Shl32Instruction:
    i. The addition on line 172 (computing read_addr_1)
    ii. The addition on line 172 (computing write_addr)
    iii. The addition on line 180 (computing read_addr_2)
    b. In Instruction<M>::execute for Shr32Instruction:
    i. The addition on line 216 (computing read_addr_1)
    ii. The addition on line 217 (computing write_addr)
    iii. The addition on line 224 (computing read_addr_2)
  9. In alu_u32/src/sub/mod.rs, in Instruction<M>::execute for Sub32Instruction:
    a. The addition on line 137 (computing read_addr_1)
    b. The addition on line 138 (computing write_addr)
    c. The addition on line 145 (computing read_addr_2)
    d. The subtraction on line 149 (computing a)
  10. In native_field/src/lib.rs:
    a. In Instruction<M>::execute for AddInstruction:
    i. The addition on line 157 (computing read_addr_1)
    ii. The addition on line 158 (computing write_addr)
    iii. The addition on line 165 (computing read_addr_2)
    b. In Instruction<M>::execute for SubInstruction:
    i. The addition on line 197 (computing read_addr_1)
    ii. The addition on line 198 (computing write_addr)
    iii. The addition on line 205 (computing read_addr_2)
    c. In Instruction<M>::execute for MulInstruction:
    i. The addition on line 237 (computing read_addr_1)
    ii. The addition on line 238 (computing write_addr)
    iii. The addition on line 245 (computing read_addr_2)
  11. In output/src/lib.rs:
    a. In Chip<M>::generate_trace for OutputChip:
    i. The subtraction on line 66 (computing cols.diff)
    b. In Instruction<M>::execute for WriteInstruction:
    i. The addition on line 149 (computing read_addr_1)
@varun-doshi
Copy link

@morganthomas let me know if this is this approach works.
Also I'd like to take this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants