Skip to content
Open
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions kmir/src/kmir/kdist/mir-semantics/rt/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,30 @@ The first cast is reified as a `thunk`, the second one resolves it and eliminate
andBool lookupTy(TY_DEST_INNER) ==K lookupTy(TY_SRC_OUTER) // and is well-formed (invariant)
```

Transmuting a value `T` into a single-field wrapper struct `G<T>` (or vice versa) is sound when the struct
has its field at zero offset and `transmute` compiled (guaranteeing equal sizes).
These are essentially `#[repr(transparent)]` but are `#[repr(rust)]` by default without the annotation and
thus there are no compiler optimisations to remove the transmute (there would be otherwise for downcast).
The layout is the same for the wrapped type and so the cast in either direction is sound.

```k
// Up: T -> Wrapper(T)
rule <k> #cast(VAL:Value, castKindTransmute, TY_SOURCE, TY_TARGET)
=>
Aggregate(variantIdx(0), ListItem(VAL))
...
</k>
requires #transparentFieldTy(lookupTy(TY_TARGET)) ==K TY_SOURCE

// Down: Wrapper(T) -> T
rule <k> #cast(Aggregate(variantIdx(0), ListItem(VAL)), castKindTransmute, TY_SOURCE, TY_TARGET)
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM! And I trust you that you've already saw the red and added these to make it green. I'd like to give you approval to let you continue.

Copy link
Contributor

Choose a reason for hiding this comment

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

But would you mind to draft an issue about refactoring these casting things? It looks quite the same but we have to deal with them one by one.

=>
VAL
...
</k>
requires {#transparentFieldTy(lookupTy(TY_SOURCE))}:>Ty ==K TY_TARGET
```

Casting a byte array/slice to an integer reinterprets the bytes in little-endian order.

```k
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let a = Box::new(42i32);
assert!(*a == 42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

┌─ 1 (root, init)
│ #execTerminator ( terminator ( ... kind: terminatorKindCall ( ... func: operandC
│ span: 0
│ (199 steps)
└─ 3 (stuck, leaf)
#execIntrinsic ( IntrinsicFunction ( symbol ( "volatile_load" ) ) , operandConst
span: 32


┌─ 2 (root, leaf, target, terminal)
│ #EndProgram ~> .K


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

┌─ 1 (root, init)
│ #execTerminator ( terminator ( ... kind: terminatorKindCall ( ... func: operandC
│ span: 0
│ (17 steps)
└─ 3 (stuck, leaf)
#traverseProjection ( toLocal ( 3 ) , thunk ( #decodeConstant ( constantKindAllo
function: main
span: 53


┌─ 2 (root, leaf, target, terminal)
│ #EndProgram ~> .K


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

┌─ 1 (root, init)
│ #execTerminator ( terminator ( ... kind: terminatorKindCall ( ... func: operandC
│ span: 0
│ (17 steps)
└─ 3 (stuck, leaf)
#traverseProjection ( toLocal ( 4 ) , thunk ( #decodeConstant ( constantKindAllo
function: main
span: 57


┌─ 2 (root, leaf, target, terminal)
│ #EndProgram ~> .K


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct Wrapper(u64);

fn main() {
let wrapped = Wrapper(42);
let val: u64 = unsafe { core::mem::transmute::<Wrapper, u64>(wrapped) };
assert!(val == 42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
struct Wrapper(u64);

fn main() {
let val: u64 = 42;
let wrapped: Wrapper = unsafe { core::mem::transmute::<u64, Wrapper>(val) };
assert!(wrapped.0 == 42);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(core_intrinsics)]
static A: i32 = 5555;

fn main() {
let val: i32;
unsafe {
val = std::intrinsics::volatile_load(&A as *const i32);
}

assert!(val == 5555);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(core_intrinsics)]
static mut A: i32 = 5555;

fn main() {
unsafe {
std::intrinsics::volatile_store(&mut A as *mut i32, 7777);
}

unsafe {
assert!(A == 7777);
}
}
3 changes: 3 additions & 0 deletions kmir/src/tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
'test_offset_from-fail',
'ref-ptr-cast-elem-fail',
'ref-ptr-cast-elem-offset-fail',
'volatile_store_static-fail',
'volatile_load_static-fail',
'box_heap_alloc-fail',
]


Expand Down
Loading