xref: /freebsd/tests/sys/kern/coredump_phnum_helper.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1e8e39fc2SConrad Meyer /*
2e8e39fc2SConrad Meyer  * Copyright (c) 2016, Conrad Meyer
3e8e39fc2SConrad Meyer  * All rights reserved.
4e8e39fc2SConrad Meyer  *
5e8e39fc2SConrad Meyer  * Redistribution and use in source and binary forms, with or without
6e8e39fc2SConrad Meyer  * modification, are permitted provided that the following conditions are met:
7e8e39fc2SConrad Meyer  *
8e8e39fc2SConrad Meyer  * 1. Redistributions of source code must retain the above copyright notice,
9e8e39fc2SConrad Meyer  * this list of conditions and the following disclaimer.
10e8e39fc2SConrad Meyer  *
11e8e39fc2SConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright notice,
12e8e39fc2SConrad Meyer  * this list of conditions and the following disclaimer in the documentation
13e8e39fc2SConrad Meyer  * and/or other materials provided with the distribution.
14e8e39fc2SConrad Meyer  *
15e8e39fc2SConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16e8e39fc2SConrad Meyer  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e8e39fc2SConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e8e39fc2SConrad Meyer  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19e8e39fc2SConrad Meyer  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20e8e39fc2SConrad Meyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21e8e39fc2SConrad Meyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22e8e39fc2SConrad Meyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23e8e39fc2SConrad Meyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24e8e39fc2SConrad Meyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25e8e39fc2SConrad Meyer  * POSSIBILITY OF SUCH DAMAGE.
26e8e39fc2SConrad Meyer  */
27e8e39fc2SConrad Meyer 
28e8e39fc2SConrad Meyer #include <sys/param.h>
29e8e39fc2SConrad Meyer #include <sys/mman.h>
30e8e39fc2SConrad Meyer 
31e8e39fc2SConrad Meyer #include <err.h>
32e8e39fc2SConrad Meyer #include <stdint.h>
33e8e39fc2SConrad Meyer #include <stdlib.h>
34*df696a2fSAndrew Turner #include <unistd.h>
35e8e39fc2SConrad Meyer 
36e8e39fc2SConrad Meyer /*
37e8e39fc2SConrad Meyer  * This program is intended to create a bunch of segment mappings, then dump
38e8e39fc2SConrad Meyer  * core.
39e8e39fc2SConrad Meyer  */
40e8e39fc2SConrad Meyer int
main(int argc __unused,char ** argv __unused)41e8e39fc2SConrad Meyer main(int argc __unused, char **argv __unused)
42e8e39fc2SConrad Meyer {
43e8e39fc2SConrad Meyer 	void *v;
44*df696a2fSAndrew Turner 	size_t i, pages, page_size;
45e8e39fc2SConrad Meyer 
46*df696a2fSAndrew Turner 	page_size = getpagesize();
478ec4c5daSEd Maste 	pages = UINT16_MAX + 1000;
48*df696a2fSAndrew Turner 	v = mmap(NULL, pages * page_size, PROT_READ | PROT_WRITE,
49e8e39fc2SConrad Meyer 	    MAP_ANON | MAP_PRIVATE, -1, 0);
508ec4c5daSEd Maste 	if (v == NULL)
51e8e39fc2SConrad Meyer 		err(1, "mmap");
528ec4c5daSEd Maste 	for (i = 0; i < pages; i += 2) {
538ec4c5daSEd Maste 		/*
548ec4c5daSEd Maste 		 * Alternate protections to interleave RW and R PT_LOAD
558ec4c5daSEd Maste 		 * segments.
568ec4c5daSEd Maste 		 */
57*df696a2fSAndrew Turner 		if (mprotect((char *)v + i * page_size, page_size,
588ec4c5daSEd Maste 		    PROT_READ) != 0)
598ec4c5daSEd Maste 			err(1, "mprotect");
60e8e39fc2SConrad Meyer 	}
61e8e39fc2SConrad Meyer 
62e8e39fc2SConrad Meyer 	/* Dump core. */
63e8e39fc2SConrad Meyer 	abort();
64e8e39fc2SConrad Meyer }
65