File: overread.c

package info (click to toggle)
libffi 3.5.2-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 4,056 kB
  • sloc: ansic: 39,406; asm: 14,495; sh: 3,567; exp: 815; makefile: 357; python: 319; perl: 171; cpp: 134
file content (54 lines) | stat: -rw-r--r-- 1,402 bytes parent folder | download | duplicates (2)
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
/* Area:        ffi_call
   Purpose:     Tests if ffi_call reads data beyond end.
   Limitations: needs mmap.
   PR:          887
   Originator:  Mikulas Patocka <mikulas@twibright.com>  */

/* { dg-do run } */

#include "ffitest.h"

#ifdef __linux__
#include <sys/mman.h>
#include <unistd.h>

static int ABI_ATTR fn(unsigned char a, unsigned short b, unsigned int c, unsigned long d)
{
	return (int)(a + b + c + d);
}
#endif

int main(void)
{
#ifdef __linux__
	ffi_cif cif;
	ffi_type *args[MAX_ARGS];
	void *values[MAX_ARGS];
	ffi_arg rint;
	char *m;
	int ps;
	args[0] = &ffi_type_uchar;
	args[1] = &ffi_type_ushort;
	args[2] = &ffi_type_uint;
	args[3] = &ffi_type_ulong;
	CHECK(ffi_prep_cif(&cif, ABI_NUM, 4, &ffi_type_sint, args) == FFI_OK);
	ps = getpagesize();
	m = mmap(NULL, ps * 3, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	CHECK(m != MAP_FAILED);
	CHECK(mprotect(m, ps, PROT_NONE) == 0);
	CHECK(mprotect(m + ps * 2, ps, PROT_NONE) == 0);
	values[0] = m + ps * 2 - sizeof(unsigned char);
	values[1] = m + ps * 2 - sizeof(unsigned short);
	values[2] = m + ps * 2 - sizeof(unsigned int);
	values[3] = m + ps * 2 - sizeof(unsigned long);
	ffi_call(&cif, FFI_FN(fn), &rint, values);
	CHECK((int)rint == 0);
	values[0] = m + ps;
	values[1] = m + ps;
	values[2] = m + ps;
	values[3] = m + ps;
	ffi_call(&cif, FFI_FN(fn), &rint, values);
	CHECK((int)rint == 0);
#endif
	exit(0);
}