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

Fix memory protection in regression tests #481

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/regression/cocotb.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async def run(self, mem_model: CoreMemoryModel, timeout_cycles: int = 5000) -> b
instr_wb = WishboneSlave(self.dut, "wb_instr", self.dut.clk, mem_model, is_instr_bus=True)
cocotb.start_soon(instr_wb.start())

data_wb = WishboneSlave(self.dut, "wb_data", self.dut.clk, mem_model, is_instr_bus=True)
data_wb = WishboneSlave(self.dut, "wb_data", self.dut.clk, mem_model, is_instr_bus=False)
cocotb.start_soon(data_wb.start())

res = await with_timeout(self.finish_event.wait(), timeout_cycles, "ns")
Expand Down
8 changes: 4 additions & 4 deletions test/regression/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def write(self, req: WriteRequest) -> WriteReply:
return WriteReply(status=ReplyStatus.ERROR)


def load_segments_from_elf(file_path: str) -> list[RandomAccessMemory]:
def load_segments_from_elf(file_path: str, *, disable_write_protection: bool = False) -> list[RandomAccessMemory]:
segments: list[RandomAccessMemory] = []

with open(file_path, "rb") as f:
Expand All @@ -160,11 +160,11 @@ def align_down(n: int) -> int:
data = b"\x00" * (paddr - seg_start) + segment.data() + b"\x00" * (seg_end - (paddr + len(segment.data())))

flags = SegmentFlags(0)
if flags_raw & P_FLAGS.PF_R == flags_raw & P_FLAGS.PF_R:
if flags_raw & P_FLAGS.PF_R:
flags |= SegmentFlags.READ
if flags_raw & P_FLAGS.PF_W == flags_raw & P_FLAGS.PF_W:
if flags_raw & P_FLAGS.PF_W or disable_write_protection:
flags |= SegmentFlags.WRITE
if flags_raw & P_FLAGS.PF_X == flags_raw & P_FLAGS.PF_X:
if flags_raw & P_FLAGS.PF_X:
flags |= SegmentFlags.EXECUTABLE

segments.append(RandomAccessMemory(range(seg_start, seg_end), flags, data))
Expand Down
8 changes: 7 additions & 1 deletion test/regression/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
test_dir = Path(__file__).parent.parent
riscv_tests_dir = test_dir.joinpath("external/riscv-tests")

# disable write protection for specific tests with writes to .text section
exclude_write_protection = ["rv32uc-rvc"]


class MMIO(MemorySegment):
def __init__(self, on_finish: Callable[[], None]):
Expand All @@ -31,7 +34,10 @@ async def run_test(sim_backend: SimulationBackend, test_name: str):
mmio = MMIO(lambda: sim_backend.stop())

mem_segments: list[MemorySegment] = []
mem_segments += load_segments_from_elf(str(riscv_tests_dir.joinpath("test-" + test_name)))
mem_segments += load_segments_from_elf(
str(riscv_tests_dir.joinpath("test-" + test_name)),
disable_write_protection=test_name in exclude_write_protection,
)
mem_segments.append(mmio)

mem_model = CoreMemoryModel(mem_segments)
Expand Down