forked from capablevms/cheri-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
general_bounds.c
66 lines (62 loc) · 1.76 KB
/
general_bounds.c
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
/***
* This program is a fusion of ``bounds.c``
* and ``set_bounds.c``.
* CHERI limits the range of addresses that may be dereferenced
* and reading the value outside the range will always give
* an "In-address space security exception". However, if we try to
* extend the bounderies of a capable pointer by setting them explicitly,
* the program fails on "riscv64".
***/
#include "include/common.h"
#include <cheri/cheric.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int32_t array[12] = {0};
int32_t *typed_array = &array;
u_int32_t bounds = 48;
#if defined(__aarch64__) && __ARM_ARCH == 8
// Do a dereference
uint64_t length = cheri_getlength(typed_array);
for (uint32_t counter = 0; counter <= (length / sizeof(int32_t)) + 11; counter++)
{
pp_cap(typed_array + counter);
// Read value to crash
if (counter == 12)
{
printf("--> On Morello (ARMv8) dereferencing outside the range"
" causes the following exception:\n");
fflush(stdout);
}
printf("Count: %d, Value: %d\n", counter, *(typed_array + counter));
}
#elif defined(__riscv)
if (argc < 2)
{
// Simply increase the bounds
printf("Bounds [Choose a value greater than 48]:\n");
if (0 == scanf("%u", &bounds))
{
error("Extraneous input");
}
}
// Command line argument to simplify testing
else if (atoi(argv[1]) > 48)
{
bounds = atoi(argv[1]);
}
else
{
printf("Please choose a value greater than 48.");
// This will cause the test to fail if a value lower than 64 has been chosen
exit(0);
}
printf("Explicitly setting the bounds outside the range causes the following exception: ");
fflush(stdout);
int32_t custom_bounds_array = cheri_setbounds(array, bounds);
#else
#error Platform not currently supported.
#endif
}