-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mte_malloc.cpp
73 lines (65 loc) · 2.37 KB
/
test_mte_malloc.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/auxv.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/types.h>
/*
* From arch/arm64/include/uapi/asm/hwcap.h
*/
#define HWCAP2_MTE (1 << 18)
/*
* From arch/arm64/include/uapi/asm/mman.h
*/
#define PROT_MTE 0x20
/*
* From include/uapi/linux/prctl.h
*/
#define PR_SET_TAGGED_ADDR_CTRL 55
#define PR_GET_TAGGED_ADDR_CTRL 56
# define PR_TAGGED_ADDR_ENABLE (1UL << 0)
# define PR_MTE_TCF_SHIFT 1
# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT)
# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT)
# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT)
# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT)
# define PR_MTE_TAG_SHIFT 3
# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT)
void pointerPrint(int* ip, char s[])
{
// Print the address pointed to by iptr %p : void*
printf("PTR: %s Addr_pointed_to_by_ptr: %p %lu \n", s, ip, ip );
/*Print the address of iptr itself*/
printf("Size of ptr: %lu Address_of_ptr: %p\n", sizeof(ip), &ip );
}
int main()
{
unsigned long hwcap2 = getauxval(AT_HWCAP2);
/* check if MTE is present */
if (!(hwcap2 & HWCAP2_MTE))
return EXIT_FAILURE;
/*
* Enable the tagged address ABI [for this thread], synchronous or asynchronous MTE
* tag check faults (based on per-CPU preference) and allow all
* non-zero tags in the randomly generated set.
*/
if (prctl(PR_SET_TAGGED_ADDR_CTRL,
PR_TAGGED_ADDR_ENABLE | PR_MTE_TCF_SYNC | PR_MTE_TCF_ASYNC |
(0xfffe << PR_MTE_TAG_SHIFT),
0, 0, 0)) {
perror("prctl() failed");
return EXIT_FAILURE;
}
// TESTING heap tagging, as per https://stackoverflow.com/questions/67071289/qemu-hosting-mte-enabled-kernel-does-not-raise-fault/71137575#71137575
printf("Hi %ld\n", (long)getpid());
printf("sizeof int: %lu [malloc'ed arr of szof(int)+1] \n", sizeof(int));
int *array = (int*) malloc (sizeof(int) * 1);
pointerPrint(array, "malloc_array");
printf("About to write to array[32] \n");
array[32] = 1;
printf("here is the value of array[32]: %d \n", array[32]);
return 0;
}