mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 08:44:35 +00:00
e8ce9fac85
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
49 lines
1.1 KiB
C
49 lines
1.1 KiB
C
#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;
|
|
}
|