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

Script to generate signature from test ELFs #466

Merged
merged 15 commits into from
Nov 2, 2023
18 changes: 15 additions & 3 deletions test/regression/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dataclasses import dataclass, replace
from elftools.elf.constants import P_FLAGS
from elftools.elf.elffile import ELFFile, Segment
from coreblocks.params.configurations import CoreConfiguration

all = [
"ReplyStatus",
Expand Down Expand Up @@ -155,9 +156,20 @@ def load_segment(segment: Segment, *, disable_write_protection: bool = False) ->
if flags_raw & P_FLAGS.PF_X:
flags |= SegmentFlags.EXECUTABLE

# append safe memory region for instruction fetch prediction
seg_end += 0x10
data += b"\0" * 0x10
if flags_raw & P_FLAGS.PF_X:
# align only instruction section to full icache lines
alignment = 2 ** CoreConfiguration().icache_block_size_bits

def align_down(n: int) -> int:
return (n // alignment) * alignment

align_front = seg_start - align_down(seg_start)
align_back = align_down(seg_end + alignment - 1) - seg_end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a clumsily named align_to_power_of_two function in coreblocks.utils.utils, which rounds up, you could use that. Rounding down can be calculated by masking; but maybe this should get an utility function as well? The code you've written is fine, but it's not very readable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this strategy might fail if multiple segments use the same cache line. As this is unlikely to occur (?), that's a fine workaround for now.


data = b"\x00" * align_front + data + b"\x00" * align_back

seg_start -= align_front
seg_end += align_back

return RandomAccessMemory(range(seg_start, seg_end), flags, data)

Expand Down