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 <manjo@austin.ibm.com>, 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)65 int allocate_free(int scheme)
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 
132 	/* Success! */
133 	return 0;
134 }
135 
alloc_mem(void * threadnum)136 void *alloc_mem(void *threadnum)
137 {
138 	int err;
139 
140 	/* waiting for other threads starting */
141 	TST_CHECKPOINT_WAIT(0);
142 
143 	/* thread N will use growth scheme N mod 4 */
144 	err = allocate_free(((uintptr_t)threadnum) % 4);
145 	tst_res(TINFO,
146 		"Thread [%d]: allocate_free() returned %d, %s.  Thread exiting.\n",
147 		(int)(uintptr_t)threadnum, err,
148 		(err ? "failed" : "succeeded"));
149 	return (void *)(uintptr_t) (err ? -1 : 0);
150 }
151 
stress_malloc(void)152 static void stress_malloc(void)
153 {
154 	int thread_index;
155 
156 	for (thread_index = 0; thread_index < NUM_THREADS; thread_index++) {
157 		SAFE_PTHREAD_CREATE(&thread_id[thread_index], NULL, alloc_mem,
158 				    (void *)(uintptr_t)thread_index);
159 	}
160 
161 	/* Wake up all threads */
162 	TST_CHECKPOINT_WAKE2(0, NUM_THREADS);
163 
164 	/* wait for all threads to finish */
165 	for (thread_index = 0; thread_index < NUM_THREADS; thread_index++) {
166 		void *status;
167 
168 		SAFE_PTHREAD_JOIN(thread_id[thread_index], &status);
169 		if ((intptr_t)status != 0) {
170 			tst_res(TFAIL, "thread [%d] - exited with errors\n",
171 				thread_index);
172 		}
173 	}
174 
175 	tst_res(TPASS, "malloc stress test finished successfully");
176 }
177 
setup(void)178 static void setup(void)
179 {
180 	thread_id = SAFE_MALLOC(sizeof(pthread_t) * NUM_THREADS);
181 }
182 
cleanup(void)183 static void cleanup(void)
184 {
185 	if (thread_id) {
186 		free(thread_id);
187 		thread_id = NULL;
188 	}
189 }
190 
191 static struct tst_test test = {
192 	.timeout = 600,
193 	.needs_checkpoints = 1,
194 	.setup = setup,
195 	.cleanup = cleanup,
196 	.test_all = stress_malloc,
197 };
198