xref: /titanic_52/usr/src/test/util-tests/tests/libsff/libsff_efault.c (revision 7fd791373689a6af05e27efec3b1ab556e02aa23)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2017, Joyent, Inc.
14  */
15 
16 /*
17  * Test various error cases all of which should return EFAULT.
18  */
19 
20 #include <stdio.h>
21 #include <errno.h>
22 #include <strings.h>
23 #include <err.h>
24 #include <libsff.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 
28 int
29 main(void)
30 {
31 	int ret;
32 	void *addr;
33 	nvlist_t *nvl;
34 	size_t len = getpagesize();
35 
36 	/*
37 	 * Get an unreadable page
38 	 */
39 	if ((addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE | MAP_ANON, -1,
40 	    0)) == MAP_FAILED) {
41 		err(1, "TEST FAILED: failed to mmap private page");
42 	}
43 
44 	if (mprotect(addr, len, PROT_NONE) != 0) {
45 		err(1, "TEST FAILED: failed to protect private page");
46 	}
47 
48 	if ((ret = libsff_parse(addr, 128, 0xa0, &nvl)) != EFAULT) {
49 		errx(1, "TEST FAILED: failed to return EFAULT on bad"
50 		    "data buffer\n");
51 	}
52 
53 	return (0);
54 }
55