1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "apr_shm.h"
18 #include "apr_rmm.h"
19 #include "apr_errno.h"
20 #include "apr_general.h"
21 #include "apr_lib.h"
22 #include "apr_strings.h"
23 #include "apr_time.h"
24 #include "abts.h"
25 #include "testutil.h"
26
27 #if APR_HAS_SHARED_MEMORY
28
29 #define FRAG_SIZE 80
30 #define FRAG_COUNT 10
31 #define SHARED_SIZE (apr_size_t)(FRAG_SIZE * FRAG_COUNT * sizeof(char*))
32
test_rmm(abts_case * tc,void * data)33 static void test_rmm(abts_case *tc, void *data)
34 {
35 apr_status_t rv;
36 apr_pool_t *pool;
37 apr_shm_t *shm;
38 apr_rmm_t *rmm;
39 apr_size_t size, fragsize;
40 apr_rmm_off_t *off, off2;
41 int i;
42 void *entity;
43
44 rv = apr_pool_create(&pool, p);
45 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
46
47 /* We're going to want 10 blocks of data from our target rmm. */
48 size = SHARED_SIZE + apr_rmm_overhead_get(FRAG_COUNT + 1);
49 rv = apr_shm_create(&shm, size, NULL, pool);
50 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
51
52 if (rv != APR_SUCCESS)
53 return;
54
55 rv = apr_rmm_init(&rmm, NULL, apr_shm_baseaddr_get(shm), size, pool);
56 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
57
58 if (rv != APR_SUCCESS)
59 return;
60
61 /* Creating each fragment of size fragsize */
62 fragsize = SHARED_SIZE / FRAG_COUNT;
63 off = apr_palloc(pool, FRAG_COUNT * sizeof(apr_rmm_off_t));
64 for (i = 0; i < FRAG_COUNT; i++) {
65 off[i] = apr_rmm_malloc(rmm, fragsize);
66 }
67
68 /* Checking for out of memory allocation */
69 off2 = apr_rmm_malloc(rmm, FRAG_SIZE * FRAG_COUNT);
70 ABTS_TRUE(tc, !off2);
71
72 /* Checking each fragment for address alignment */
73 for (i = 0; i < FRAG_COUNT; i++) {
74 char *c = apr_rmm_addr_get(rmm, off[i]);
75 apr_size_t sc = (apr_size_t)c;
76
77 ABTS_TRUE(tc, !!off[i]);
78 ABTS_TRUE(tc, !(sc & 7));
79 }
80
81 /* Setting each fragment to a unique value */
82 for (i = 0; i < FRAG_COUNT; i++) {
83 int j;
84 char **c = apr_rmm_addr_get(rmm, off[i]);
85 for (j = 0; j < FRAG_SIZE; j++, c++) {
86 *c = apr_itoa(pool, i + j);
87 }
88 }
89
90 /* Checking each fragment for its unique value */
91 for (i = 0; i < FRAG_COUNT; i++) {
92 int j;
93 char **c = apr_rmm_addr_get(rmm, off[i]);
94 for (j = 0; j < FRAG_SIZE; j++, c++) {
95 char *d = apr_itoa(pool, i + j);
96 ABTS_STR_EQUAL(tc, d, *c);
97 }
98 }
99
100 /* Freeing each fragment */
101 for (i = 0; i < FRAG_COUNT; i++) {
102 rv = apr_rmm_free(rmm, off[i]);
103 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
104 }
105
106 /* Creating one large segment */
107 off[0] = apr_rmm_calloc(rmm, SHARED_SIZE);
108
109 /* Setting large segment */
110 for (i = 0; i < FRAG_COUNT * FRAG_SIZE; i++) {
111 char **c = apr_rmm_addr_get(rmm, off[0]);
112 c[i] = apr_itoa(pool, i);
113 }
114
115 /* Freeing large segment */
116 rv = apr_rmm_free(rmm, off[0]);
117 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
118
119 /* Creating each fragment of size fragsize */
120 for (i = 0; i < FRAG_COUNT; i++) {
121 off[i] = apr_rmm_malloc(rmm, fragsize);
122 }
123
124 /* Freeing each fragment backwards */
125 for (i = FRAG_COUNT - 1; i >= 0; i--) {
126 rv = apr_rmm_free(rmm, off[i]);
127 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
128 }
129
130 /* Creating one large segment (again) */
131 off[0] = apr_rmm_calloc(rmm, SHARED_SIZE);
132
133 /* Freeing large segment */
134 rv = apr_rmm_free(rmm, off[0]);
135 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
136
137 /* Checking realloc */
138 off[0] = apr_rmm_calloc(rmm, SHARED_SIZE - 100);
139 off[1] = apr_rmm_calloc(rmm, 100);
140 ABTS_TRUE(tc, !!off[0]);
141 ABTS_TRUE(tc, !!off[1]);
142
143 entity = apr_rmm_addr_get(rmm, off[1]);
144 rv = apr_rmm_free(rmm, off[0]);
145 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
146
147 {
148 unsigned char *c = entity;
149
150 /* Fill in the region; the first half with zereos, which will
151 * likely catch the apr_rmm_realloc offset calculation bug by
152 * making it think the old region was zero length. */
153 for (i = 0; i < 100; i++) {
154 c[i] = (i < 50) ? 0 : i;
155 }
156 }
157
158 /* now we can realloc off[1] and get many more bytes */
159 off[0] = apr_rmm_realloc(rmm, entity, SHARED_SIZE - 100);
160 ABTS_TRUE(tc, !!off[0]);
161
162 {
163 unsigned char *c = apr_rmm_addr_get(rmm, off[0]);
164
165 /* fill in the region */
166 for (i = 0; i < 100; i++) {
167 ABTS_TRUE(tc, c[i] == (i < 50 ? 0 : i));
168 }
169 }
170
171 rv = apr_rmm_destroy(rmm);
172 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
173
174 rv = apr_shm_destroy(shm);
175 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
176
177 apr_pool_destroy(pool);
178 }
179
180 #endif /* APR_HAS_SHARED_MEMORY */
181
testrmm(abts_suite * suite)182 abts_suite *testrmm(abts_suite *suite)
183 {
184 suite = ADD_SUITE(suite);
185
186 #if APR_HAS_SHARED_MEMORY
187 abts_run_test(suite, test_rmm, NULL);
188 #endif
189
190 return suite;
191 }
192