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

python: sanitize number formatting #130

Merged
merged 3 commits into from
Jun 17, 2024
Merged
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
35 changes: 16 additions & 19 deletions camkes/runner/Context.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import itertools
import functools
import numbers
from math import log10
import \
ordered_set, os, pdb, re, six, sys, textwrap, math
from capdl.Object import ObjectType, ObjectRights, ARMIRQMode
Expand Down Expand Up @@ -66,7 +65,7 @@ def new_context(entity, assembly, render_state, state_key, outfile_name,
if obj_space else None,

# Cap allocator
'alloc_cap': None if cap_space is None else \
'alloc_cap': None if cap_space is None else
(lambda name, obj, **kwargs: alloc_cap((entity.label(),
cap_space), cap_space, name, obj, **kwargs)),

Expand All @@ -81,7 +80,7 @@ def new_context(entity, assembly, render_state, state_key, outfile_name,
# you see `set y = alloc('foo', bar, moo)` in template code, think:
# set x = alloc_obj('foo_obj', bar)
# set y = alloc_cap('foo_cap', x, moo)
'alloc': None if cap_space is None else \
'alloc': None if cap_space is None else
(lambda name, type, label=entity.label(), **kwargs:
alloc_cap((entity.label(), cap_space), cap_space, name,
alloc_obj((entity.label(), obj_space), obj_space, '%s_%s' %
Expand All @@ -95,14 +94,14 @@ def new_context(entity, assembly, render_state, state_key, outfile_name,
# same shared variable. The local name (lname) will later be used by us
# to locate the relevant ELF frame(s) to remap. Note that we assume
# address spaces and CSpaces are 1-to-1.
'register_shared_variable': None if cap_space is None else \
'register_shared_variable': None if cap_space is None else
(lambda global_name, symbol, size, frame_size=None, paddr=None,
perm='RWX', cached=True, label=entity.parent.label(), with_mapping_caps=None:
register_shared_variable(
addr_space, obj_space, global_name, symbol, size, cap_space,
frame_size, paddr, perm, cached, label, with_mapping_caps)),

'get_shared_variable_backing_frames': None if cap_space is None else \
'get_shared_variable_backing_frames': None if cap_space is None else
(lambda global_name, size, frame_size=None, label=entity.parent.label():
get_shared_variable_backing_frames(
obj_space, global_name, size, frame_size, label)),
Expand Down Expand Up @@ -486,12 +485,11 @@ def register_shared_variable(addr_space, obj_space, global_name, symbol, size, c
size = int(size)
frame_size = calc_frame_size(size, frame_size, obj_space.spec.arch)
num_frames = size//frame_size
digits = str(int(log10(num_frames + 1)) + 1)
namefmt = '%s_%0' + digits + 'd_obj'
digits = len(str(num_frames - 1)) # 0 frames is a no-op below anyway
# If these frames have been allocated already then the allocator will return them.
# Therefore calls to register_shared_variable with the same global_name have to have the same size.
frames = [obj_space.alloc(ObjectType.seL4_FrameObject,
name=namefmt % (global_name, i),
name=f'{global_name}_{i:0{digits}}_obj',
size=frame_size,
label=label)
for i in range(num_frames)]
Expand Down Expand Up @@ -539,33 +537,31 @@ def get_shared_variable_backing_frames(obj_space, global_name, size, frame_size=
size = int(size)
frame_size = calc_frame_size(size, frame_size, obj_space.spec.arch)
num_frames = size//frame_size
digits = str(int(log10(num_frames + 1)) + 1)
namefmt = '%s_%0' + digits + 'd_obj'
digits = len(str(num_frames - 1)) # 0 frames is a no-op below anyway
return [obj_space.alloc(ObjectType.seL4_FrameObject,
name=namefmt % (global_name, i),
name=f'{global_name}_{i:0{digits}}_obj',
size=frame_size,
label=label)
for i in range(num_frames)]


def register_fill_frame(addr_space, symbol, fill, size, obj_space, label):
'''
Take a symbol and create a collection of 4K frames that can comfortably store
Take a symbol and create a collection of 4K frames that can comfortably store
a region in the bootinfo.

Return a static_assert checking that the symbol is of the correct size
'''
assert addr_space
number_frames = size//4096
digits = str(int(log10(number_frames + 1)) + 1)
namefmt = '%s_%s_%0' + digits + 'd_obj'
digits = len(str(number_frames - 1)) # 0 frames is a no-op below anyway
frames = []
for i in range(number_frames):
fill_str = ['%d %d %s %d' % (0, 4096 if (size - (i * 4096)) >=
4096 else (size - (i * 4096)), fill, i * 4096)]
name = namefmt % (symbol, label, i)
frames.append(obj_space.alloc(ObjectType.seL4_FrameObject,
name=name, label=label, fill=fill_str, size=4096))
name=f'{symbol}_{label}_{i:0{digits}}_obj',
label=label, fill=fill_str, size=4096))
caps = [Cap(frame, read=True, write=False, grant=False) for frame in frames]
sizes = [4096] * number_frames
addr_space.add_symbol_with_caps(symbol, sizes, caps)
Expand All @@ -582,9 +578,10 @@ def register_stack_symbol(addr_space, symbol, size, obj_space, label):
'''
assert addr_space
number_frames = size//4096
digits = str(int(log10(number_frames + 1)) + 1)
namefmt = 'stack_%s_%0' + digits + 'd_%s_obj'
frames = [obj_space.alloc(ObjectType.seL4_FrameObject, name=namefmt % (symbol, i, label), label=label, size=4096)
digits = len(str(number_frames - 1)) # 0 frames is a no-op below anyway
frames = [obj_space.alloc(ObjectType.seL4_FrameObject,
name=f'stack_{symbol}_{i:0{digits}}_{label}_obj',
label=label, size=4096)
for i in range(number_frames)]
# We create 2 additional mappings with None caps that are for the guard pages.
sizes = [4096] * (number_frames + 2)
Expand Down