1*d1c5dc47SRobert Mustacchi /*
2*d1c5dc47SRobert Mustacchi * This file and its contents are supplied under the terms of the
3*d1c5dc47SRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0.
4*d1c5dc47SRobert Mustacchi * You may only use this file in accordance with the terms of version
5*d1c5dc47SRobert Mustacchi * 1.0 of the CDDL.
6*d1c5dc47SRobert Mustacchi *
7*d1c5dc47SRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this
8*d1c5dc47SRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at
9*d1c5dc47SRobert Mustacchi * http://www.illumos.org/license/CDDL.
10*d1c5dc47SRobert Mustacchi */
11*d1c5dc47SRobert Mustacchi
12*d1c5dc47SRobert Mustacchi /*
13*d1c5dc47SRobert Mustacchi * Copyright 2016 Joyent, Inc.
14*d1c5dc47SRobert Mustacchi */
15*d1c5dc47SRobert Mustacchi
16*d1c5dc47SRobert Mustacchi /*
17*d1c5dc47SRobert Mustacchi * Basic tests for aligned_alloc(3C). Note that we test ENOMEM failure by
18*d1c5dc47SRobert Mustacchi * relying on the implementation of the current libc malloc. Specifically we go
19*d1c5dc47SRobert Mustacchi * through and add a mapping so we can't expand the heap and then use it up. If
20*d1c5dc47SRobert Mustacchi * the memory allocator is ever changed, this test will start failing, at which
21*d1c5dc47SRobert Mustacchi * point, it may not be worth the cost of keeping it around.
22*d1c5dc47SRobert Mustacchi */
23*d1c5dc47SRobert Mustacchi
24*d1c5dc47SRobert Mustacchi #include <stdlib.h>
25*d1c5dc47SRobert Mustacchi #include <errno.h>
26*d1c5dc47SRobert Mustacchi #include <libproc.h>
27*d1c5dc47SRobert Mustacchi #include <sys/sysmacros.h>
28*d1c5dc47SRobert Mustacchi #include <sys/mman.h>
29*d1c5dc47SRobert Mustacchi #include <sys/debug.h>
30*d1c5dc47SRobert Mustacchi
31*d1c5dc47SRobert Mustacchi int
main(void)32*d1c5dc47SRobert Mustacchi main(void)
33*d1c5dc47SRobert Mustacchi {
34*d1c5dc47SRobert Mustacchi pstatus_t status;
35*d1c5dc47SRobert Mustacchi void *buf;
36*d1c5dc47SRobert Mustacchi
37*d1c5dc47SRobert Mustacchi /*
38*d1c5dc47SRobert Mustacchi * Alignment must be sizeof (void *) (word) aligned.
39*d1c5dc47SRobert Mustacchi */
40*d1c5dc47SRobert Mustacchi VERIFY3P(aligned_alloc(sizeof (void *) - 1, 16), ==, NULL);
41*d1c5dc47SRobert Mustacchi VERIFY3S(errno, ==, EINVAL);
42*d1c5dc47SRobert Mustacchi
43*d1c5dc47SRobert Mustacchi VERIFY3P(aligned_alloc(sizeof (void *) + 1, 16), ==, NULL);
44*d1c5dc47SRobert Mustacchi VERIFY3S(errno, ==, EINVAL);
45*d1c5dc47SRobert Mustacchi
46*d1c5dc47SRobert Mustacchi
47*d1c5dc47SRobert Mustacchi VERIFY3P(aligned_alloc(23, 16), ==, NULL);
48*d1c5dc47SRobert Mustacchi VERIFY3S(errno, ==, EINVAL);
49*d1c5dc47SRobert Mustacchi
50*d1c5dc47SRobert Mustacchi buf = aligned_alloc(sizeof (void *), 16);
51*d1c5dc47SRobert Mustacchi VERIFY3P(buf, !=, NULL);
52*d1c5dc47SRobert Mustacchi free(buf);
53*d1c5dc47SRobert Mustacchi
54*d1c5dc47SRobert Mustacchi /*
55*d1c5dc47SRobert Mustacchi * Cause ENOMEM
56*d1c5dc47SRobert Mustacchi */
57*d1c5dc47SRobert Mustacchi VERIFY0(proc_get_status(getpid(), &status));
58*d1c5dc47SRobert Mustacchi VERIFY3P(mmap((caddr_t)P2ROUNDUP(status.pr_brkbase +
59*d1c5dc47SRobert Mustacchi status.pr_brksize, 0x1000), 0x1000,
60*d1c5dc47SRobert Mustacchi PROT_READ, MAP_ANON | MAP_FIXED | MAP_PRIVATE, -1, 0),
61*d1c5dc47SRobert Mustacchi !=, (void *)-1);
62*d1c5dc47SRobert Mustacchi
63*d1c5dc47SRobert Mustacchi for (;;) {
64*d1c5dc47SRobert Mustacchi if (malloc(16) == NULL)
65*d1c5dc47SRobert Mustacchi break;
66*d1c5dc47SRobert Mustacchi }
67*d1c5dc47SRobert Mustacchi
68*d1c5dc47SRobert Mustacchi for (;;) {
69*d1c5dc47SRobert Mustacchi if (aligned_alloc(sizeof (void *), 16) == NULL)
70*d1c5dc47SRobert Mustacchi break;
71*d1c5dc47SRobert Mustacchi }
72*d1c5dc47SRobert Mustacchi
73*d1c5dc47SRobert Mustacchi VERIFY3P(aligned_alloc(sizeof (void *), 16), ==, NULL);
74*d1c5dc47SRobert Mustacchi VERIFY3S(errno, ==, ENOMEM);
75*d1c5dc47SRobert Mustacchi
76*d1c5dc47SRobert Mustacchi return (0);
77*d1c5dc47SRobert Mustacchi }
78