about summary refs log tree commit diff
path: root/tests/run-make/mte-ffi/bar_float.c
blob: a1590f62765a6404fc0923708dbc6c9d3ffed0d5 (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
40
41
42
43
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;
}