about summary refs log tree commit diff
path: root/tests/run-make/mte-ffi/bar_function.c
blob: 1fa48d32a0c88442b31543cb1d47110dc4dee630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;
}