1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Author: Manoj Iyer, IBM Austin TX <[email protected]>, 2001
5 *
6 * Stress the VMM and C library by spawning N threads which malloc
7 * blocks of increasing size until malloc returns NULL.
8 */
9
10 #include <stdio.h>
11 #include <pthread.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <math.h>
15 #include <assert.h>
16 #include <errno.h>
17 #include <stdint.h>
18 #include <sys/types.h>
19
20 #include "tst_test.h"
21 #include "tst_safe_pthread.h"
22
23 /* Number of loops per-thread */
24 #define NUM_LOOPS 100
25
26 /* Number of threads to create */
27 #define NUM_THREADS 60
28
29 /* Define SPEW_SIGNALS to tickle thread_create bug (it fails if interrupted). */
30 #define SPEW_SIGNALS
31
32 static pthread_t *thread_id; /* Spawned thread */
33
my_yield(void)34 static void my_yield(void)
35 {
36 #ifdef SPEW_SIGNALS
37 /* usleep just happens to use signals in glibc at moment.
38 * This is good because it allows us to test whether pthread_create
39 * improperly returns EINTR (which would violate SUSv3)
40 */
41 usleep(0);
42 #else
43 /* If you want this test to pass, don't define SPEW_SIGNALS,
44 * as pthread_create is broken at moment, and fails if interrupted
45 */
46 static const struct timespec t0 = { 0, 0 };
47 nanosleep(&t0, NULL);
48 #endif
49 }
50
51 /*
52 * allocate_free() - Allocate and free test called per-thread
53 *
54 * @scheme: 0-3; selects how fast memory size grows
55 *
56 * This function does the allocation and free by calling malloc
57 * and free functions. The size of the memory to be malloced is
58 * determined by the caller of this function. The size can be
59 * a number from the fibannoaci series, power of 2 or 3 or 5
60 *
61 * Return:
62 * 0: success
63 * 1: failure
64 */
allocate_free(int scheme,int threadnum)65 int allocate_free(int scheme, int threadnum)
66 {
67 int loop;
68 const int MAXPTRS = 50; /* only 42 or so get used on 32 bit machine */
69
70 for (loop = 0; loop < NUM_LOOPS; loop++) {
71 size_t oldsize = 5;
72 size_t size = sizeof(long);
73 long *ptrs[MAXPTRS];
74 int num_alloc;
75 int i;
76
77 /* loop terminates in one of three ways:
78 * 1. after MAXPTRS iterations
79 * 2. if malloc fails
80 * 3. if new size overflows
81 */
82 for (num_alloc = 0; num_alloc < MAXPTRS; num_alloc++) {
83 size_t newsize = 0;
84
85 /* Malloc the next block */
86 ptrs[num_alloc] = malloc(size);
87 /* terminate loop if malloc fails */
88 if (!ptrs[num_alloc])
89 break;
90 ptrs[num_alloc][0] = num_alloc;
91
92 /* Increase size according to one of four schedules. */
93 switch (scheme) {
94 case 0:
95 newsize = size + oldsize;
96 oldsize = size;
97 break;
98 case 1:
99 newsize = size * 2;
100 break;
101 case 2:
102 newsize = size * 3;
103 break;
104 case 3:
105 newsize = size * 5;
106 break;
107 default:
108 assert(0);
109 }
110 /* terminate loop on overflow */
111 if (newsize < size)
112 break;
113 size = newsize;
114
115 my_yield();
116 }
117
118 for (i = 0; i < num_alloc; i++) {
119 if (ptrs[i][0] != i) {
120 tst_res(TFAIL,
121 "pid[%d]: fail: bad sentinel value\n",
122 getpid());
123 return 1;
124 }
125 free(ptrs[i]);
126 my_yield();
127 }
128
129 my_yield();
130
131 if (!tst_remaining_runtime()) {
132 tst_res(TINFO, "Thread [%d]: Test runtime is over, exiting", threadnum);
133 break;
134 }
135 }
136
137 /* Success! */
138 return 0;
139 }
140
alloc_mem(void * threadnum)141 void *alloc_mem(void *threadnum)
142 {
143 int err;
144
145 /* waiting for other threads starting */
146 TST_CHECKPOINT_WAIT(0);
147
148 /* thread N will use growth scheme N mod 4 */
149 err = allocate_free(((uintptr_t)threadnum) % 4, (uintptr_t)threadnum);
150 tst_res(TINFO,
151 "Thread [%d]: allocate_free() returned %d, %s. Thread exiting.\n",
152 (int)(uintptr_t)threadnum, err,
153 (err ? "failed" : "succeeded"));
154 return (void *)(uintptr_t) (err ? -1 : 0);
155 }
156
stress_malloc(void)157 static void stress_malloc(void)
158 {
159 int thread_index;
160
161 for (thread_index = 0; thread_index < NUM_THREADS; thread_index++) {
162 SAFE_PTHREAD_CREATE(&thread_id[thread_index], NULL, alloc_mem,
163 (void *)(uintptr_t)thread_index);
164 }
165
166 /* Wake up all threads */
167 TST_CHECKPOINT_WAKE2(0, NUM_THREADS);
168
169 /* wait for all threads to finish */
170 for (thread_index = 0; thread_index < NUM_THREADS; thread_index++) {
171 void *status;
172
173 SAFE_PTHREAD_JOIN(thread_id[thread_index], &status);
174 if ((intptr_t)status != 0) {
175 tst_res(TFAIL, "thread [%d] - exited with errors",
176 thread_index);
177 }
178 }
179
180 tst_res(TPASS, "malloc stress test finished successfully");
181 }
182
setup(void)183 static void setup(void)
184 {
185 thread_id = SAFE_MALLOC(sizeof(pthread_t) * NUM_THREADS);
186 }
187
cleanup(void)188 static void cleanup(void)
189 {
190 if (thread_id) {
191 free(thread_id);
192 thread_id = NULL;
193 }
194 }
195
196 static struct tst_test test = {
197 .max_runtime = 600,
198 .needs_checkpoints = 1,
199 .setup = setup,
200 .cleanup = cleanup,
201 .test_all = stress_malloc,
202 };
203