From bde123e05faa5e138a3b6eacfe25b1a1e6f39519 Mon Sep 17 00:00:00 2001 From: Alex Forencich Date: Mon, 27 Feb 2023 16:38:34 -0800 Subject: [PATCH] Add transfer length checks Signed-off-by: Alex Forencich --- cocotbext/axi/axi_master.py | 12 ++++++++++++ cocotbext/axi/axil_master.py | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/cocotbext/axi/axi_master.py b/cocotbext/axi/axi_master.py index 6f395ab..97010cb 100644 --- a/cocotbext/axi/axi_master.py +++ b/cocotbext/axi/axi_master.py @@ -291,6 +291,9 @@ class AxiMasterWrite(Region, Reset): if isinstance(data, int): raise ValueError("Expected bytes or bytearray for data") + if burst != AxiBurstType.FIXED and address+len(data) >= 2**self.address_width: + raise ValueError("Requested transfer overruns end of address space") + if awid is None or awid < 0: awid = None elif awid > self.id_count: @@ -357,6 +360,9 @@ class AxiMasterWrite(Region, Reset): if isinstance(data, int): raise ValueError("Expected bytes or bytearray for data") + if burst != AxiBurstType.FIXED and address+len(data) >= 2**self.address_width: + raise ValueError("Requested transfer overruns end of address space") + if awid is None or awid < 0: awid = None elif awid > self.id_count: @@ -717,6 +723,9 @@ class AxiMasterRead(Region, Reset): if length < 0: raise ValueError("Read length must be positive") + if burst != AxiBurstType.FIXED and address+length >= 2**self.address_width: + raise ValueError("Requested transfer overruns end of address space") + if arid is None or arid < 0: arid = None elif arid > self.id_count: @@ -771,6 +780,9 @@ class AxiMasterRead(Region, Reset): if length < 0: raise ValueError("Read length must be positive") + if burst != AxiBurstType.FIXED and address+length >= 2**self.address_width: + raise ValueError("Requested transfer overruns end of address space") + if arid is None or arid < 0: arid = None elif arid > self.id_count: diff --git a/cocotbext/axi/axil_master.py b/cocotbext/axi/axil_master.py index d1f48fd..f833362 100644 --- a/cocotbext/axi/axil_master.py +++ b/cocotbext/axi/axil_master.py @@ -182,6 +182,9 @@ class AxiLiteMasterWrite(Region, Reset): if isinstance(data, int): raise ValueError("Expected bytes or bytearray for data") + if address+len(data) >= 2**self.address_width: + raise ValueError("Requested transfer overruns end of address space") + if not self.awprot_present and prot != AxiProt.NONSECURE: raise ValueError("awprot sideband signal value specified, but signal is not connected") @@ -419,6 +422,12 @@ class AxiLiteMasterRead(Region, Reset): if address < 0 or address >= 2**self.address_width: raise ValueError("Address out of range") + if length < 0: + raise ValueError("Read length must be positive") + + if address+length >= 2**self.address_width: + raise ValueError("Requested transfer overruns end of address space") + if not self.arprot_present and prot != AxiProt.NONSECURE: raise ValueError("arprot sideband signal value specified, but signal is not connected")