Skip to content

Commit 43a4914

Browse files
agattidpgeorge
authored andcommitted
tests/micropython: Add a test for checking viper value clobbering.
This commit introduces a test that should check whether viper load or store operations won't clobber either the buffer address or the index value being used. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent 424ae08 commit 43a4914

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
RESULTS = []
2+
3+
LOAD_TEMPLATE_REG = """
4+
BUFFER = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
5+
OFFSET = 1
6+
@micropython.viper
7+
def test_invariants_load{}_reg(buf: ptr{}, offset: int):
8+
offset_copy1: int = offset
9+
temporary1 = buf[offset]
10+
offset_copy2: int = offset
11+
temporary2 = buf[offset]
12+
RESULTS.append(["LOAD{}", temporary1 == temporary2, offset == offset_copy1 == offset_copy2])
13+
test_invariants_load{}_reg(BUFFER, OFFSET)
14+
"""
15+
16+
17+
STORE_TEMPLATE_REG = """
18+
BUFFER = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
19+
OFFSET = 1
20+
@micropython.viper
21+
def test_invariants_store{}_reg(buf: ptr{}, offset: int):
22+
offset_copy: int = offset
23+
value: uint = {}
24+
buf[offset] = value
25+
temporary = buf[offset]
26+
RESULTS.append(["STORE{}", temporary == value, offset == offset_copy])
27+
test_invariants_store{}_reg(BUFFER, OFFSET)
28+
"""
29+
30+
try:
31+
for width, value in ((8, 0x11), (16, 0x1111), (32, 0x11111111)):
32+
exec(LOAD_TEMPLATE_REG.format(width, width, width, width, width))
33+
exec(STORE_TEMPLATE_REG.format(width, width, value, width, width, width))
34+
except MemoryError:
35+
print("SKIP-TOO-LARGE")
36+
raise SystemExit
37+
38+
for line in RESULTS:
39+
print(" ".join([str(i) for i in line]))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LOAD8 True True
2+
STORE8 True True
3+
LOAD16 True True
4+
STORE16 True True
5+
LOAD32 True True
6+
STORE32 True True

0 commit comments

Comments
 (0)