amaranth icon indicating copy to clipboard operation
amaranth copied to clipboard

sim._pyrtl: work around Python's new integer-string convertion limits

Open Mrmaxmeier opened this issue 2 years ago • 0 comments

Hi,

some of my code recently started failing with this exception after a Python version bump:

Traceback (most recent call last):
[...]
  File "/usr/lib/python3.10/site-packages/amaranth/hdl/xfrm.py", line 209, in on_statement
    new_stmt = self.on_Assign(stmt)
  File "/usr/lib/python3.10/site-packages/amaranth/sim/_pyrtl.py", line 349, in on_Assign
    gen_rhs = f"({(1 << len(stmt.rhs)) - 1} & {gen_rhs_value})"
ValueError: Exceeds the limit (4300) for integer string conversion

Here's a small reproducer:

from amaranth import Module, Signal, sim
dut = Module()
dut.d.sync += Signal(1).eq(Signal(1) << Signal(14))
sim.Simulator(dut)

This fails because the generated code uses a large mask value, which raises an ValueError during decimal formatting in Python versions that include a mitigation for CVE-2020-10735. Formatting to hexadecimal instead avoids the algorithmic complexity and is not impacted by the new conversion limits.

I'm not sure if this is worth merging as affected modules are already near the existing value limits enforced by pyrtl, so feel free to close/reject this patch ^^

Note: There's an open proposal that changes int's repr implementation to hex for large values and would make this patch a bit more self-explanatory ({v!r} vs {v:#x}): https://github.com/python/cpython/issues/96601

Mrmaxmeier avatar Sep 20 '22 13:09 Mrmaxmeier