Debian Codegraph @

testsuite/libffi.call/overread.c

raw
1 /* Area: ffi_call
2 Purpose: Tests if ffi_call reads data beyond end.
3 Limitations: needs mmap.
4 PR: 887
5 Originator: Mikulas Patocka <[email protected]> */
6
7 /* { dg-do run } */
8
9 #include "ffitest.h"
10
11 #ifdef __linux__
12 #include <sys/mman.h>
13 #include <unistd.h>
14
15 static int ABI_ATTR fn(unsigned char a, unsigned short b, unsigned int c, unsigned long d)
16 {
17 return (int)(a + b + c + d);
18 }
19 #endif
20
21 int main(void)
22 {
23 #ifdef __linux__
24 ffi_cif cif;
25 ffi_type *args[MAX_ARGS];
26 void *values[MAX_ARGS];
27 ffi_arg rint;
28 char *m;
29 int ps;
30 args[0] = &ffi_type_uchar;
31 args[1] = &ffi_type_ushort;
32 args[2] = &ffi_type_uint;
33 args[3] = &ffi_type_ulong;
34 CHECK(ffi_prep_cif(&cif, ABI_NUM, 4, &ffi_type_sint, args) == FFI_OK);
35 ps = getpagesize();
36 m = mmap(NULL, ps * 3, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
37 CHECK(m != MAP_FAILED);
38 CHECK(mprotect(m, ps, PROT_NONE) == 0);
39 CHECK(mprotect(m + ps * 2, ps, PROT_NONE) == 0);
40 values[0] = m + ps * 2 - sizeof(unsigned char);
41 values[1] = m + ps * 2 - sizeof(unsigned short);
42 values[2] = m + ps * 2 - sizeof(unsigned int);
43 values[3] = m + ps * 2 - sizeof(unsigned long);
44 ffi_call(&cif, FFI_FN(fn), &rint, values);
45 CHECK((int)rint == 0);
46 values[0] = m + ps;
47 values[1] = m + ps;
48 values[2] = m + ps;
49 values[3] = m + ps;
50 ffi_call(&cif, FFI_FN(fn), &rint, values);
51 CHECK((int)rint == 0);
52 #endif
53 exit(0);
54 }