Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e8ce9fa
Add tests to ensure MTE tags are preserved across FFI boundaries
dheaton-arm Jun 13, 2024
ba5ff07
Introduce create_dll_import_libs function
bjorn3 Jul 25, 2024
ee89db9
Move temp file name generation out of the create_dll_import_lib method
bjorn3 Jul 25, 2024
bb764bd
Move is_mingw_gnu_toolchain and i686_decorated_name to cg_ssa
bjorn3 Jul 25, 2024
3c987cb
Move computation of decorated names out of the create_dll_import_lib …
bjorn3 Jul 25, 2024
216686b
Move mingw dlltool invocation to cg_ssa
bjorn3 Jul 25, 2024
732037c
Remove redundant information and simplify `only` condition
dheaton-arm Jul 31, 2024
44da6fa
Update ar_archive_writer to 0.3.2
bjorn3 Jul 25, 2024
d8c2b76
run-make: enable msvc for link-dedup
ChrisDenton Aug 4, 2024
1737845
Enable msvc for run-make/rust-lld
ChrisDenton Aug 4, 2024
f895fe0
rewrite pdb-buildinfo-cl-cmd to rmake
Oneirical Jul 29, 2024
840a661
rewrite pgo-indirect-call-promotion to rmake
Oneirical Jul 29, 2024
1054054
rewrite simd-ffi to rmake
Oneirical Aug 5, 2024
51e68c3
Update `compiler-builtins` to 0.1.117
tgross35 Aug 5, 2024
74653b6
Add implied target features to target_feature attribute
calebzulawski Jul 26, 2024
22c5952
Add test to ensure implied target features work with asm, and fix fai…
calebzulawski Jul 26, 2024
520a5a5
Fix codegen tests
calebzulawski Jul 26, 2024
c866e1f
Add missing features
calebzulawski Jul 29, 2024
34f29a2
Add +sse4.2 due to #128426
calebzulawski Jul 31, 2024
3c48f65
Bless tests
calebzulawski Jul 31, 2024
fbd618d
Refactor and fill out target feature lists
calebzulawski Aug 2, 2024
484aca8
Don't use LLVM's target features
calebzulawski Aug 3, 2024
a25da07
Don't use LLVM to compute -Ctarget-feature
calebzulawski Aug 4, 2024
5006711
Remove redundant implied features
calebzulawski Aug 4, 2024
6b96a60
Add implied features to non-target-feature functions
calebzulawski Aug 4, 2024
83276f5
Hide implicit target features from diagnostics when possible
calebzulawski Aug 5, 2024
0b98a0c
Fix typo
calebzulawski Aug 5, 2024
8818c95
Disallow enabling features without their implied features
calebzulawski Aug 6, 2024
3153855
Rollup merge of #128206 - bjorn3:import_lib_writing_refactor, r=jieyouxu
tgross35 Aug 7, 2024
0665f3c
Rollup merge of #128221 - calebzulawski:implied-target-features, r=Am…
tgross35 Aug 7, 2024
ffdc8ec
Rollup merge of #128363 - Oneirical:not-to-be-undertestimated, r=jiey…
tgross35 Aug 7, 2024
a3c645c
Rollup merge of #128384 - dheaton-arm:mte-test, r=jieyouxu
tgross35 Aug 7, 2024
21f3c96
Rollup merge of #128638 - ChrisDenton:link-dedup, r=jieyouxu
tgross35 Aug 7, 2024
fde485e
Rollup merge of #128656 - ChrisDenton:rust-lld, r=lqd
tgross35 Aug 7, 2024
e9337da
Rollup merge of #128691 - tgross35:update-builtins, r=Amanieu
tgross35 Aug 7, 2024
edc58d0
Rollup merge of #128700 - Oneirical:i-ffind-these-tests-quite-simdple…
tgross35 Aug 7, 2024
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
1 change: 1 addition & 0 deletions src/tools/compiletest/src/command-list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
"only-32bit",
"only-64bit",
"only-aarch64",
"only-aarch64-unknown-linux-gnu",
"only-apple",
"only-arm",
"only-avr",
Expand Down
43 changes: 43 additions & 0 deletions tests/run-make/mte-ffi/bar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef __BAR_H
#define __BAR_H

#include <sys/mman.h>
#include <sys/auxv.h>
#include <sys/prctl.h>
#include <unistd.h>
#include <stdio.h>

// Set the allocation tag on the destination address using the STG instruction.
#define set_tag(tagged_addr) do { \
asm volatile("stg %0, [%0]" : : "r" (tagged_addr) : "memory"); \
} while (0)

int mte_enabled() {
return (getauxval(AT_HWCAP2)) & HWCAP2_MTE;
}

void *alloc_page() {
// Enable MTE with synchronous checking
if (prctl(PR_SET_TAGGED_ADDR_CTRL,
PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_SYNC | (0xfffe << PR_MTE_TAG_SHIFT),
0, 0, 0))
{
perror("prctl() failed");
}

// Using `mmap` allows us to ensure that, on systems which support MTE, the allocated
// memory is 16-byte aligned for MTE.
// This also allows us to explicitly specify whether the region should be protected by
// MTE or not.
if (mte_enabled()) {
void *ptr = mmap(NULL, sysconf(_SC_PAGESIZE),
PROT_READ | PROT_WRITE | PROT_MTE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
} else {
void *ptr = mmap(NULL, sysconf(_SC_PAGESIZE),
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
}
}

#endif // __BAR_H
44 changes: 44 additions & 0 deletions tests/run-make/mte-ffi/bar_float.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bar.h"

extern void foo(char*);

void bar(char *ptr) {
if (((uintptr_t)ptr >> 56) != 0x1f) {
fprintf(stderr, "Top byte corrupted on Rust -> C FFI boundary!\n");
exit(1);
}
}

int main(void)
{
float *ptr = alloc_page();
if (ptr == MAP_FAILED)
{
perror("mmap() failed");
return EXIT_FAILURE;
}

// Store an arbitrary tag in bits 56-59 of the pointer (where an MTE tag may be),
// and a different value in the ignored top 4 bits.
ptr = (float *)((uintptr_t)ptr | 0x1fl << 56);

if (mte_enabled()) {
set_tag(ptr);
}

ptr[0] = 2.0f;
ptr[1] = 1.5f;

foo(ptr); // should change the contents of the page and call `bar`

if (ptr[0] != 0.5f || ptr[1] != 0.2f) {
fprintf(stderr, "invalid data in memory; expected '0.5 0.2', got '%f %f'\n",
ptr[0], ptr[1]);
return EXIT_FAILURE;
}

return 0;
}
39 changes: 39 additions & 0 deletions tests/run-make/mte-ffi/bar_function.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bar.h"

typedef void (*fp)(int (*)());

extern void foo(fp);

void bar(int (*ptr)()) {
if (((uintptr_t)ptr >> 56) != 0x2f) {
fprintf(stderr, "Top byte corrupted on Rust -> C FFI boundary!\n");
exit(1);
}

int r = (*ptr)();
if (r != 32) {
fprintf(stderr, "invalid return value; expected 32, got '%d'\n", r);
exit(1);
}
}

int main(void)
{
fp ptr = alloc_page();
if (ptr == MAP_FAILED)
{
perror("mmap() failed");
return EXIT_FAILURE;
}

// Store an arbitrary tag in bits 56-59 of the pointer (where an MTE tag may be),
// and a different value in the ignored top 4 bits.
ptr = (fp)((uintptr_t)&bar | 0x1fl << 56);

foo(ptr);

return 0;
}
47 changes: 47 additions & 0 deletions tests/run-make/mte-ffi/bar_int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bar.h"

extern void foo(unsigned int *);

void bar(char *ptr) {
if (((uintptr_t)ptr >> 56) != 0x1f) {
fprintf(stderr, "Top byte corrupted on Rust -> C FFI boundary!\n");
exit(1);
}
}

int main(void)
{
// Construct a pointer with an arbitrary tag in bits 56-59, simulating an MTE tag.
// It's only necessary that the tag is preserved across FFI bounds for this test.
unsigned int *ptr;

ptr = alloc_page();
if (ptr == MAP_FAILED)
{
perror("mmap() failed");
return EXIT_FAILURE;
}

// Store an arbitrary tag in bits 56-59 of the pointer (where an MTE tag may be),
// and a different value in the ignored top 4 bits.
ptr = (unsigned int *)((uintptr_t)ptr | 0x1fl << 56);

if (mte_enabled()) {
set_tag(ptr);
}

ptr[0] = 61;
ptr[1] = 62;

foo(ptr); // should change the contents of the page to start with 0x63 0x64 and call `bar`

if (ptr[0] != 0x63 || ptr[1] != 0x64) {
fprintf(stderr, "invalid data in memory; expected '63 64', got '%d %d'\n", ptr[0], ptr[1]);
return EXIT_FAILURE;
}

return 0;
}
48 changes: 48 additions & 0 deletions tests/run-make/mte-ffi/bar_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bar.h"

extern void foo(char*);

void bar(char *ptr) {
if (((uintptr_t)ptr >> 56) != 0x2f) {
fprintf(stderr, "Top byte corrupted on Rust -> C FFI boundary!\n");
exit(1);
}

if (strcmp(ptr, "cd")) {
fprintf(stderr, "invalid data in memory; expected 'cd', got '%s'\n", ptr);
exit(1);
}
}

int main(void)
{
// Construct a pointer with an arbitrary tag in bits 56-59, simulating an MTE tag.
// It's only necessary that the tag is preserved across FFI bounds for this test.
char *ptr;

ptr = alloc_page();
if (ptr == MAP_FAILED)
{
perror("mmap() failed");
return EXIT_FAILURE;
}

// Store an arbitrary tag in bits 56-59 of the pointer (where an MTE tag may be),
// and a different value in the ignored top 4 bits.
ptr = (unsigned int *)((uintptr_t)ptr | 0x1fl << 56);

if (mte_enabled()) {
set_tag(ptr);
}

ptr[0] = 'a';
ptr[1] = 'b';
ptr[2] = '\0';

foo(ptr);

return 0;
}
19 changes: 19 additions & 0 deletions tests/run-make/mte-ffi/foo_float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![crate_type = "cdylib"]
#![crate_name = "foo"]

use std::os::raw::c_float;

extern "C" {
fn bar(ptr: *const c_float);
}

#[no_mangle]
pub extern "C" fn foo(ptr: *mut c_float) {
assert_eq!((ptr as usize) >> 56, 0x1f);

unsafe {
*ptr = 0.5;
*ptr.wrapping_add(1) = 0.2;
bar(ptr);
}
}
17 changes: 17 additions & 0 deletions tests/run-make/mte-ffi/foo_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![crate_type = "cdylib"]
#![crate_name = "foo"]

extern "C" fn ret32() -> i32 {
32
}

#[no_mangle]
pub extern "C" fn foo(ptr: extern "C" fn(extern "C" fn() -> i32)) {
assert_eq!((ptr as usize) >> 56, 0x1f);

// Store an arbitrary tag in the tag bits, and convert back to the correct pointer type.
let p = ((ret32 as usize) | (0x2f << 56)) as *const ();
let p: extern "C" fn() -> i32 = unsafe { std::mem::transmute(p) };

unsafe { ptr(p) }
}
19 changes: 19 additions & 0 deletions tests/run-make/mte-ffi/foo_int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![crate_type = "cdylib"]
#![crate_name = "foo"]

use std::os::raw::c_uint;

extern "C" {
fn bar(ptr: *const c_uint);
}

#[no_mangle]
pub extern "C" fn foo(ptr: *mut c_uint) {
assert_eq!((ptr as usize) >> 56, 0x1f);

unsafe {
*ptr = 0x63;
*ptr.wrapping_add(1) = 0x64;
bar(ptr);
}
}
27 changes: 27 additions & 0 deletions tests/run-make/mte-ffi/foo_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![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);
}
}
38 changes: 38 additions & 0 deletions tests/run-make/mte-ffi/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Tests that MTE tags and values stored in the top byte of a pointer (TBI) are
// preserved across FFI boundaries (C <-> Rust).
// This test does not require MTE: whilst the test will use MTE if available, if it is not,
// arbitrary tag bits are set using TBI.

// This test is only valid for AArch64.
// The linker must be explicitly specified when cross-compiling, so it is limited to
// `aarch64-unknown-linux-gnu`.
//@ only-aarch64-unknown-linux-gnu

use run_make_support::{cc, dynamic_lib_name, extra_c_flags, run, rustc, target};

fn main() {
run_test("int");
run_test("float");
run_test("string");
run_test("function");
}

fn run_test(variant: &str) {
let flags = {
let mut flags = extra_c_flags();
flags.push("-march=armv8.5-a+memtag");
flags
};
println!("{variant} test...");
rustc()
.input(format!("foo_{variant}.rs"))
.target(target())
.linker("aarch64-linux-gnu-gcc")
.run();
cc().input(format!("bar_{variant}.c"))
.input(dynamic_lib_name("foo"))
.out_exe("test")
.args(&flags)
.run();
run("test");
}