Skip to content

Commit b43baea

Browse files
committed
getCapBounds now returns all three: base, length and top. fixed the source of error in pdf (PDF is still inconsitent with sail spec)
1 parent a9b51ec commit b43baea

File tree

4 files changed

+31
-38
lines changed

4 files changed

+31
-38
lines changed

archdoc/chap-isaref-riscv.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ \section{Constant Definitions}
120120
\sailRISCVtype{cap\_cE\_width}
121121
\sailRISCVtype{cap\_max\_E}
122122
\sailRISCVtypecapSizze{}
123-
\sailRISCVtypecapMantissaWidth{}
123+
\sailRISCVtypecapBWidth{}
124124
\sailRISCVtype{cap\_perms\_width}
125125
\sailRISCVtype{cap\_cperms\_width}
126126
\sailRISCVtype{cap\_otype\_width}

properties/props.sail

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ $property
9494
function prop_setbounds(reqBase : CapAddrBits, reqLen : CapAddrBits) -> bool = {
9595
let (exact, c) = setCapBounds(root_cap_mem, reqBase, reqLen);
9696
let encodable = capEncodable(c);
97-
let (b2, l2) = getCapBoundsBits(c);
98-
let t2 = (0b0 @ b2) + l2;
97+
let (b2, l2, t2) = getCapBoundsBits(c);
9998
let reqTop = (0b0 @ reqBase) + (0b0 @ reqLen);
10099
let saneTop = reqTop <=_u 0b1@0x00000000;
101100
saneTop --> (
@@ -122,15 +121,13 @@ function prop_setbounds_monotonic(
122121
reqBase1 : CapAddrBits, reqLen1 : CapAddrBits,
123122
reqBase2 : CapAddrBits, reqLen2 : CapAddrBits) -> bool = {
124123
let (exact1, c1) = setCapBounds(root_cap_mem, reqBase1, reqLen1);
125-
let (b1, l1) = getCapBoundsBits(c1);
126-
let t1 = (0b0 @ b1) + l1;
124+
let (b1, l1, t1) = getCapBoundsBits(c1);
127125
let reqTop1 = (0b0 @ reqBase1) + (0b0 @ reqLen1);
128126
let saneTop1 = reqTop1 <=_u 0b1@0x00000000;
129127
let reqTop2 = (0b0 @ reqBase2) + (0b0 @ reqLen2);
130128
let saneTop2 = reqTop2 <=_u 0b1@0x00000000;
131129
let (exact2, c2) = setCapBounds(c1, reqBase2, reqLen2);
132-
let (b2, l2) = getCapBoundsBits(c2);
133-
let t2 = (0b0 @ b2) + l2;
130+
let (b2, l2, t2) = getCapBoundsBits(c2);
134131
let requestMonotonic = (b1 <=_u reqBase2) & (reqTop2 <=_u t1);
135132
let resultMonotonic = (b1 <=_u b2) & (t2 <=_u t1);
136133
(saneTop1 & saneTop2 & requestMonotonic) --> resultMonotonic
@@ -145,12 +142,10 @@ $property
145142
function prop_setbounds_precise(reqBase1 : CapAddrBits, reqLen1 : CapAddrBits,
146143
c2 : CapBits) -> bool = {
147144
let (exact, c1) = setCapBounds(root_cap_mem, reqBase1, reqLen1);
148-
let (b1, l1) = getCapBoundsBits(c1);
149-
let t1 = (0b0 @ b1) + l1;
145+
let (b1, l1, t1) = getCapBoundsBits(c1);
150146
let reqTop1 = (0b0 @ reqBase1) + (0b0 @ reqLen1);
151147
let saneTop1 = reqTop1 <=_u 0b1@0x00000000;
152-
let (b2, l2) = getCapBoundsBits(capBitsToCapability(false, c2));
153-
let t2 = (0b0 @ b2) + l2;
148+
let (b2, l2, t2) = getCapBoundsBits(capBitsToCapability(false, c2));
154149
let validBase = b2 <=_u reqBase1;
155150
let betterBase = b1 <_u b2;
156151
let validTop = reqTop1 <=_u t2;
@@ -182,8 +177,7 @@ function prop_repbounds_c(reqBase : CapAddrBits, reqLen : CapAddrBits, newAddr :
182177
let (exact, c) = setCapBounds(root_cap_mem, reqBase, reqLen);
183178
let reqTop = (0b0 @ reqBase) + (0b0 @ reqLen);
184179
let saneTop = reqTop <=_u 0b1@0x00000000;
185-
let (b, l) = getCapBoundsBits(c);
186-
let t = (0b0 @ b) + l;
180+
let (b, l, t) = getCapBoundsBits(c);
187181
let (representable, newCap) = setCapAddr(c, newAddr);
188182
let inCBounds = (b <=_u newAddr) & ((0b0 @ newAddr) <=_u t);
189183
(saneTop & inCBounds) --> representable
@@ -198,8 +192,7 @@ function prop_repbounds(reqBase : CapAddrBits, reqLen : CapAddrBits, newAddr : C
198192
let (exact, c) = setCapBounds(root_cap_mem, reqBase, reqLen);
199193
let reqTop = (0b0 @ reqBase) + (0b0 @ reqLen);
200194
let saneTop = reqTop <=_u 0b1@0x00000000;
201-
let (b, l) = getCapBoundsBits(c);
202-
let t = (0b0 @ b) + l;
195+
let (b, l, t) = getCapBoundsBits(c);
203196
let (representable, newCap) = setCapAddr(c, newAddr);
204197
let repTop = (0b0 @ b) + ((to_bits(33,1) << unsigned(c.E)) << 9);
205198
/* The representable bounds check: either E is max or address is in range */

src/cheri_cap_common.sail

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -404,20 +404,21 @@ let null_cap_bits : CapBits = capToBits(null_cap)
404404
/*!
405405
* Returns the decoded base and top of the given Capability.
406406
*/
407-
function getCapBoundsBits(c) : Capability -> (CapAddrBits, CapLenBits) = {
407+
function getCapBoundsBits(c) : Capability -> (CapAddrBits, CapLenBits, CapLenBits) = {
408408
let E = if c.E >=_u cap_max_E_bits then cap_max_E_bits else c.E;
409409
let a : CapAddrBits = c.address;
410410
let a_mid = truncate(a >> E, cap_B_width);
411411
let a_hi = if a_mid <_u c.B then 1 else 0;
412412
let a_top = a >> (E + cap_B_width);
413413
let base : CapAddrBits = truncate(((a_top - a_hi) @ c.B) << E, cap_addr_width);
414414
let length : CapLenBits = truncate((zeros(24) @ c.M) << E, cap_len_width);
415-
(base, length)
415+
let top : CapLenBits = (0b0 @ base) + length;
416+
(base, length, top)
416417
}
417418

418-
function getCapBounds(cap) : Capability -> (CapAddrInt, CapLen) =
419-
let (base : CapAddrBits, length : CapLenBits) = getCapBoundsBits(cap) in
420-
(unsigned(base), unsigned(length))
419+
function getCapBounds(cap) : Capability -> (CapAddrInt, CapLen, CapLen) =
420+
let (base : CapAddrBits, length: CapLenBits, top : CapLenBits) = getCapBoundsBits(cap) in
421+
(unsigned(base), unsigned(length), unsigned(top))
421422

422423
val count_ones : forall 'ni 'no, 'no >= 1. (int('no), bits('ni)) -> bits('no)
423424
function count_ones (no, x) = {
@@ -565,15 +566,15 @@ function unsealCap(cap) : Capability -> Capability =
565566
{cap with otype=to_bits(cap_otype_width, otype_unsealed)}
566567

567568
function getCapBaseBits(c) : Capability -> CapAddrBits =
568-
let (base, _) = getCapBoundsBits(c) in
569+
let (base, _, _) = getCapBoundsBits(c) in
569570
base
570571

571572
function getCapBase(c) : Capability -> CapAddrInt =
572573
unsigned(getCapBaseBits(c))
573574

574575
function getCapTopBits(c) : Capability -> CapLenBits =
575-
let (base, length) = getCapBoundsBits(c) in {
576-
zero_extend(base) + length
576+
let (_, _, top) = getCapBoundsBits(c) in {
577+
top
577578
}
578579

579580
function getCapTop (c) : Capability -> CapLen =
@@ -587,7 +588,7 @@ function getCapOffset(c) : Capability -> CapAddrInt =
587588
unsigned(getCapOffsetBits(c))
588589

589590
function getCapLength(c) : Capability -> CapLen =
590-
let (base, length) = getCapBoundsBits(c) in
591+
let (_, length, _) = getCapBoundsBits(c) in
591592
unsigned(length)
592593

593594
/*!
@@ -598,9 +599,9 @@ function getCapLength(c) : Capability -> CapLen =
598599
*/
599600
val inCapBounds : (Capability, CapAddrBits, CapLen) -> bool
600601
function inCapBounds (cap, addr, size) = {
601-
let (base, length) = getCapBounds(cap);
602+
let (base, length, top) = getCapBounds(cap);
602603
let a = unsigned(addr);
603-
(a >= base) & (size <= length)
604+
(a >= base) & (a + size <= top)
604605
}
605606

606607
function int_to_cap (offset) : CapAddrBits -> Capability =
@@ -630,8 +631,8 @@ function clearTag(cap) : Capability -> Capability =
630631
clearTagIf(cap, true)
631632

632633
function capBoundsEqual (c1, c2) : (Capability, Capability) -> bool =
633-
let (base1, length1) = getCapBounds(c1) in
634-
let (base2, length2) = getCapBounds(c2) in
634+
let (base1, length1, _) = getCapBounds(c1) in
635+
let (base2, length2, _) = getCapBounds(c2) in
635636
(base1 == base2) & (length1 == length2)
636637

637638
/*!
@@ -688,9 +689,8 @@ function incCapAddr(c, delta) =
688689

689690
val capToString : (Capability) -> string
690691
function capToString (cap) = {
691-
let top = getCapTop(cap);
692-
let top_str = BitStr(to_bits(cap_len_width + 3, top));
693-
let (base, length) = getCapBoundsBits(cap);
692+
let (base, length, top) = getCapBoundsBits(cap);
693+
let top_str = BitStr(0b000 @ top);
694694
let len_str = BitStr(0b000 @ length);
695695
BitStr(cap.address)
696696
^ " (v:" ^ (if cap.tag then "1 " else "0 ")

src/cheri_insts.sail

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -661,16 +661,16 @@ function clause execute (CTestSubset(rd, cs1, cs2)) = {
661661
let cs1_val = C(cs1);
662662
let cs2_val = C(cs2);
663663

664-
let (cs2_base, cs2_length) = getCapBounds(cs2_val);
665-
let (cs1_base, cs1_length) = getCapBounds(cs1_val);
664+
let (cs2_base, cs2_length, cs2_top) = getCapBounds(cs2_val);
665+
let (cs1_base, cs1_length, cs1_top) = getCapBounds(cs1_val);
666666
let cs2_perms = getCapPerms(cs2_val);
667667
let cs1_perms = getCapPerms(cs1_val);
668668

669669
let result = if cs1_val.tag != cs2_val.tag then
670670
0b0
671671
else if cs2_base < cs1_base then
672672
0b0
673-
else if cs2_base + cs2_length > cs1_base + cs1_length then
673+
else if cs2_top > cs1_top then
674674
0b0
675675
else if (cs2_perms & cs1_perms) != cs2_perms then
676676
0b0
@@ -706,7 +706,7 @@ function clause execute (CSeal(cd, cs1, cs2)) = {
706706
let cs2_val = C(cs2);
707707

708708
let cs2_addr = unsigned(cs2_val.address);
709-
let (cs2_base, cs2_length) = getCapBounds(cs2_val);
709+
let (cs2_base, cs2_length, cs2_top) = getCapBounds(cs2_val);
710710

711711
let isPermittedOtype : bool =
712712
if cs1_val.permit_execute then
@@ -728,7 +728,7 @@ function clause execute (CSeal(cd, cs1, cs2)) = {
728728
& not(isCapSealed(cs2_val))
729729
& cs2_val.permit_seal
730730
& (cs2_addr >= cs2_base)
731-
& (cs2_addr < cs2_base + cs2_length)
731+
& (cs2_addr < cs2_top)
732732
& isPermittedOtype;
733733

734734
let inCap = clearTagIfSealed(cs1_val);
@@ -750,14 +750,14 @@ function clause execute (CUnseal(cd, cs1, cs2)) = {
750750
let cs1_val = C(cs1);
751751
let cs2_val = C(cs2);
752752
let cs2_addr = unsigned(cs2_val.address);
753-
let (cs2_base, cs2_length) = getCapBounds(cs2_val);
753+
let (cs2_base, cs2_length, cs2_top) = getCapBounds(cs2_val);
754754
let permitted = cs2_val.tag
755755
& isCapSealed(cs1_val)
756756
& not(isCapSealed(cs2_val))
757757
& (cs2_addr == unsigned(cs1_val.otype))
758758
& cs2_val.permit_unseal
759759
& (cs2_addr >= cs2_base)
760-
& (cs2_addr < cs2_base + cs2_length);
760+
& (cs2_addr < cs2_top);
761761
let new_global = cs1_val.global & cs2_val.global;
762762
let newCap = {unsealCap(cs1_val) with global=new_global};
763763
C(cd) = clearTagIf(newCap, not(permitted));

0 commit comments

Comments
 (0)