rust/tests/run-make/mte-ffi/foo_string.rs
Damian Heaton e8ce9fac85 Add tests to ensure MTE tags are preserved across FFI boundaries
Added run-make tests to verify that, between a Rust-C FFI boundary in both directions,
any MTE tags included in a pointer are preserved for the following pointer types, as
well as any information stored using TBI:
- int
- float
- string
- function
2024-07-30 10:12:51 +01:00

28 lines
630 B
Rust

#![crate_type = "cdylib"]
#![crate_name = "foo"]
use std::arch::asm;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
extern "C" {
fn bar(ptr: *const c_char);
}
#[no_mangle]
pub extern "C" fn foo(ptr: *const c_char) {
assert_eq!((ptr as usize) >> 56, 0x1f);
let s = unsafe { CStr::from_ptr(ptr) };
assert_eq!(s.to_str().unwrap(), "ab");
let s = CString::from_vec_with_nul("cd\0".into()).unwrap();
let mut p = ((s.as_ptr() as usize) | (0x2f << 56)) as *const c_char;
unsafe {
#[cfg(target_feature = "mte")]
asm!("stg {p}, [{p}]", p = inout(reg) p);
bar(p);
}
}