xref: /titanic_50/usr/src/test/libc-tests/tests/aligned_alloc.c (revision 24bb1048b1197ebc9afe761e4098573aeedfe8c9)
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 2016 Joyent, Inc.
14  */
15 
16 /*
17  * Basic tests for aligned_alloc(3C). Note that we test ENOMEM failure by
18  * relying on the implementation of the current libc malloc. Specifically we go
19  * through and add a mapping so we can't expand the heap and then use it up. If
20  * the memory allocator is ever changed, this test will start failing, at which
21  * point, it may not be worth the cost of keeping it around.
22  */
23 
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <libproc.h>
27 #include <sys/sysmacros.h>
28 #include <sys/mman.h>
29 #include <sys/debug.h>
30 
31 int
32 main(void)
33 {
34 	pstatus_t status;
35 	void *buf;
36 
37 	/*
38 	 * Alignment must be sizeof (void *) (word) aligned.
39 	 */
40 	VERIFY3P(aligned_alloc(sizeof (void *) - 1, 16), ==, NULL);
41 	VERIFY3S(errno, ==, EINVAL);
42 
43 	VERIFY3P(aligned_alloc(sizeof (void *) + 1, 16), ==, NULL);
44 	VERIFY3S(errno, ==, EINVAL);
45 
46 
47 	VERIFY3P(aligned_alloc(23, 16), ==, NULL);
48 	VERIFY3S(errno, ==, EINVAL);
49 
50 	buf = aligned_alloc(sizeof (void *), 16);
51 	VERIFY3P(buf, !=, NULL);
52 	free(buf);
53 
54 	/*
55 	 * Cause ENOMEM
56 	 */
57 	VERIFY0(proc_get_status(getpid(), &status));
58 	VERIFY3P(mmap((caddr_t)P2ROUNDUP(status.pr_brkbase +
59 	    status.pr_brksize, 0x1000), 0x1000,
60 	    PROT_READ, MAP_ANON | MAP_FIXED | MAP_PRIVATE, -1, 0),
61 	    !=, (void *)-1);
62 
63 	for (;;) {
64 		if (malloc(16) == NULL)
65 			break;
66 	}
67 
68 	for (;;) {
69 		if (aligned_alloc(sizeof (void *), 16) == NULL)
70 			break;
71 	}
72 
73 	VERIFY3P(aligned_alloc(sizeof (void *), 16), ==, NULL);
74 	VERIFY3S(errno, ==, ENOMEM);
75 
76 	return (0);
77 }
78