Skip to content

Commit

Permalink
python: sanitize number formatting
Browse files Browse the repository at this point in the history
- Fix the calculation of leading zeros. Using "int(log10(cnt + 1)) + 1"
  is broken, it evaluates to 2 for cnt=9, and thus results in the
  strings "00"-"08". Using "1 if (n <= 1) else (1 + int(log10(n - 1)))"
  seems overly complicated here, so a naive and readable way is used.
- switch to f-strings

Signed-off-by: Axel Heider <[email protected]>
  • Loading branch information
axel-h committed May 17, 2023
1 parent bb3d548 commit 215f311
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 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 @@ -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,10 +537,9 @@ 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)]
Expand All @@ -557,15 +554,14 @@ def register_fill_frame(addr_space, symbol, fill, size, obj_space, label):
'''
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

0 comments on commit 215f311

Please sign in to comment.