Fix bug 10025 - Lack of Sanity Checking in calls to malloc()/calloc().
[Samba/wip.git] / lib / ntdb / tools / ntdbtorture.c
blob7ddb5c3acb648ab934f80f58dcacf5bc0fca2ef2
1 /* this tests ntdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
3 */
5 #include "config.h"
6 #include "ntdb.h"
7 #include <ccan/err/err.h>
8 #ifdef HAVE_LIBREPLACE
9 #include <replace.h>
10 #else
11 #include <stdlib.h>
12 #include <getopt.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 #include <time.h>
21 #include <sys/wait.h>
22 #endif
24 //#define REOPEN_PROB 30
25 #define DELETE_PROB 8
26 #define STORE_PROB 4
27 #define APPEND_PROB 6
28 #define TRANSACTION_PROB 10
29 #define TRANSACTION_PREPARE_PROB 2
30 #define LOCKSTORE_PROB 5
31 #define TRAVERSE_PROB 20
32 #define TRAVERSE_MOD_PROB 100
33 #define TRAVERSE_ABORT_PROB 500
34 #define CULL_PROB 100
35 #define KEYLEN 3
36 #define DATALEN 100
38 static struct ntdb_context *db;
39 static int in_transaction;
40 static int in_traverse;
41 static int error_count;
42 #if TRANSACTION_PROB
43 static int always_transaction = 0;
44 #endif
45 static int loopnum;
46 static int count_pipe;
47 static union ntdb_attribute log_attr;
48 static union ntdb_attribute seed_attr;
49 static union ntdb_attribute hsize_attr;
51 static void ntdb_log(struct ntdb_context *ntdb,
52 enum ntdb_log_level level,
53 enum NTDB_ERROR ecode,
54 const char *message,
55 void *data)
57 printf("ntdb:%s:%s:%s\n",
58 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
59 fflush(stdout);
60 #if 0
62 char str[200];
63 signal(SIGUSR1, SIG_IGN);
64 sprintf(str,"xterm -e gdb /proc/%u/exe %u", (unsigned int)getpid(), (unsigned int)getpid());
65 system(str);
67 #endif
70 #include "../private.h"
72 static void segv_handler(int sig, siginfo_t *info, void *p)
74 char string[100];
76 sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
77 (unsigned int)getpid(), info->si_addr, db->file->map_ptr,
78 (size_t)db->file->map_size);
79 if (write(2, string, strlen(string)) > 0)
80 sleep(60);
81 _exit(11);
84 static void warn_on_err(enum NTDB_ERROR e, struct ntdb_context *ntdb,
85 const char *why)
87 if (e != NTDB_SUCCESS) {
88 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), why,
89 ntdb ? ntdb_errorstr(e) : "(no ntdb)");
90 error_count++;
94 static char *randbuf(int len)
96 char *buf;
97 int i;
98 buf = (char *)malloc(len+1);
99 if (buf == NULL) {
100 perror("randbuf: unable to allocate memory for buffer.\n");
101 exit(1);
104 for (i=0;i<len;i++) {
105 buf[i] = 'a' + (rand() % 26);
107 buf[i] = 0;
108 return buf;
111 static void addrec_db(void);
112 static int modify_traverse(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
113 void *state)
115 #if CULL_PROB
116 if (random() % CULL_PROB == 0) {
117 ntdb_delete(ntdb, key);
119 #endif
121 #if TRAVERSE_MOD_PROB
122 if (random() % TRAVERSE_MOD_PROB == 0) {
123 addrec_db();
125 #endif
127 #if TRAVERSE_ABORT_PROB
128 if (random() % TRAVERSE_ABORT_PROB == 0)
129 return 1;
130 #endif
132 return 0;
135 static void addrec_db(void)
137 int klen, dlen;
138 char *k, *d;
139 NTDB_DATA key, data;
140 enum NTDB_ERROR e;
142 klen = 1 + (rand() % KEYLEN);
143 dlen = 1 + (rand() % DATALEN);
145 k = randbuf(klen);
146 d = randbuf(dlen);
148 key.dptr = (unsigned char *)k;
149 key.dsize = klen+1;
151 data.dptr = (unsigned char *)d;
152 data.dsize = dlen+1;
154 #if REOPEN_PROB
155 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
156 ntdb_reopen_all(0);
157 goto next;
159 #endif
161 #if TRANSACTION_PROB
162 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
163 e = ntdb_transaction_start(db);
164 warn_on_err(e, db, "ntdb_transaction_start failed");
165 in_transaction++;
166 goto next;
168 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
169 if (random() % TRANSACTION_PREPARE_PROB == 0) {
170 e = ntdb_transaction_prepare_commit(db);
171 warn_on_err(e, db, "ntdb_transaction_prepare_commit failed");
173 e = ntdb_transaction_commit(db);
174 warn_on_err(e, db, "ntdb_transaction_commit failed");
175 in_transaction--;
176 goto next;
179 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
180 ntdb_transaction_cancel(db);
181 in_transaction--;
182 goto next;
184 #endif
186 #if DELETE_PROB
187 if (random() % DELETE_PROB == 0) {
188 ntdb_delete(db, key);
189 goto next;
191 #endif
193 #if STORE_PROB
194 if (random() % STORE_PROB == 0) {
195 e = ntdb_store(db, key, data, NTDB_REPLACE);
196 warn_on_err(e, db, "ntdb_store failed");
197 goto next;
199 #endif
201 #if APPEND_PROB
202 if (random() % APPEND_PROB == 0) {
203 e = ntdb_append(db, key, data);
204 warn_on_err(e, db, "ntdb_append failed");
205 goto next;
207 #endif
209 #if LOCKSTORE_PROB
210 if (random() % LOCKSTORE_PROB == 0) {
211 ntdb_chainlock(db, key);
212 if (ntdb_fetch(db, key, &data) != NTDB_SUCCESS) {
213 data.dsize = 0;
214 data.dptr = NULL;
216 e = ntdb_store(db, key, data, NTDB_REPLACE);
217 warn_on_err(e, db, "ntdb_store failed");
218 if (data.dptr) free(data.dptr);
219 ntdb_chainunlock(db, key);
220 goto next;
222 #endif
224 #if TRAVERSE_PROB
225 /* FIXME: recursive traverses break transactions? */
226 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
227 in_traverse++;
228 ntdb_traverse(db, modify_traverse, NULL);
229 in_traverse--;
230 goto next;
232 #endif
234 if (ntdb_fetch(db, key, &data) == NTDB_SUCCESS)
235 free(data.dptr);
237 next:
238 free(k);
239 free(d);
242 static int traverse_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
243 void *state)
245 ntdb_delete(ntdb, key);
246 return 0;
249 static void usage(void)
251 printf("Usage: ntdbtorture"
252 #if TRANSACTION_PROB
253 " [-t]"
254 #endif
255 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S] [-H HASH_SIZE]\n");
256 exit(0);
259 static void send_count_and_suicide(int sig)
261 /* This ensures our successor can continue where we left off. */
262 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
263 exit(2);
264 /* This gives a unique signature. */
265 kill(getpid(), SIGUSR2);
268 static int run_child(const char *filename, int i, int seed, unsigned num_loops,
269 unsigned start, int ntdb_flags)
271 struct sigaction act = { .sa_sigaction = segv_handler,
272 .sa_flags = SA_SIGINFO };
273 sigaction(11, &act, NULL);
275 db = ntdb_open(filename, ntdb_flags, O_RDWR | O_CREAT, 0600,
276 &log_attr);
277 if (!db) {
278 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), filename,
279 "db open failed");
280 exit(1);
283 #if 0
284 if (i == 0) {
285 printf("pid %u\n", (unsigned int)getpid());
286 sleep(9);
287 } else
288 sleep(10);
289 #endif
291 srand(seed + i);
292 srandom(seed + i);
294 /* Set global, then we're ready to handle being killed. */
295 loopnum = start;
296 signal(SIGUSR1, send_count_and_suicide);
298 for (;loopnum<num_loops && error_count == 0;loopnum++) {
299 addrec_db();
302 if (error_count == 0) {
303 enum NTDB_ERROR e;
305 ntdb_traverse(db, NULL, NULL);
306 #if TRANSACTION_PROB
307 if (always_transaction) {
308 while (in_transaction) {
309 ntdb_transaction_cancel(db);
310 in_transaction--;
312 e = ntdb_transaction_start(db);
313 if (e) {
314 warn_on_err(e, db,
315 "ntdb_transaction_start failed");
316 exit(1);
319 #endif
320 ntdb_traverse(db, traverse_fn, NULL);
321 ntdb_traverse(db, traverse_fn, NULL);
323 #if TRANSACTION_PROB
324 if (always_transaction) {
325 e = ntdb_transaction_commit(db);
326 warn_on_err(e, db, "ntdb_transaction_commit failed");
328 #endif
331 ntdb_close(db);
333 return (error_count < 100 ? error_count : 100);
336 static char *test_path(const char *filename)
338 const char *prefix = getenv("TEST_DATA_PREFIX");
340 if (prefix) {
341 char *path = NULL;
342 int ret;
344 ret = asprintf(&path, "%s/%s", prefix, filename);
345 if (ret == -1) {
346 return NULL;
348 return path;
351 return strdup(filename);
354 int main(int argc, char * const *argv)
356 int i, seed = -1;
357 int num_loops = 5000;
358 int num_procs = 3;
359 int c, pfds[2];
360 extern char *optarg;
361 pid_t *pids;
362 int kill_random = 0;
363 int *done;
364 int ntdb_flags = NTDB_DEFAULT;
365 char *test_ntdb;
366 enum NTDB_ERROR e;
368 log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
369 log_attr.base.next = &seed_attr;
370 log_attr.log.fn = ntdb_log;
371 seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
372 seed_attr.base.next = &hsize_attr;
373 hsize_attr.base.attr = NTDB_ATTRIBUTE_HASHSIZE;
374 hsize_attr.base.next = NULL;
375 hsize_attr.hashsize.size = 2; /* stress it by default. */
377 while ((c = getopt(argc, argv, "n:l:s:thkSH:")) != -1) {
378 switch (c) {
379 case 'n':
380 num_procs = strtol(optarg, NULL, 0);
381 break;
382 case 'l':
383 num_loops = strtol(optarg, NULL, 0);
384 break;
385 case 's':
386 seed = strtol(optarg, NULL, 0);
387 break;
388 case 'S':
389 ntdb_flags = NTDB_NOSYNC;
390 break;
391 case 't':
392 #if TRANSACTION_PROB
393 always_transaction = 1;
394 #else
395 fprintf(stderr, "Transactions not supported\n");
396 usage();
397 #endif
398 break;
399 case 'k':
400 kill_random = 1;
401 break;
402 case 'H':
403 hsize_attr.hashsize.size = strtol(optarg, NULL, 0);
404 break;
405 default:
406 usage();
410 test_ntdb = test_path("torture.ntdb");
412 unlink(test_ntdb);
414 if (seed == -1) {
415 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
417 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
419 if (num_procs == 1 && !kill_random) {
420 /* Don't fork for this case, makes debugging easier. */
421 error_count = run_child(test_ntdb, 0, seed, num_loops, 0,
422 ntdb_flags);
423 goto done;
426 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
427 done = (int *)calloc(sizeof(int), num_procs);
429 if (pipe(pfds) != 0) {
430 perror("Creating pipe");
431 exit(1);
433 count_pipe = pfds[1];
435 for (i=0;i<num_procs;i++) {
436 if ((pids[i]=fork()) == 0) {
437 close(pfds[0]);
438 if (i == 0) {
439 printf("testing with %d processes, %d loops, seed=%d%s\n",
440 num_procs, num_loops, seed,
441 #if TRANSACTION_PROB
442 always_transaction ? " (all within transactions)" : ""
443 #else
445 #endif
448 exit(run_child(test_ntdb, i, seed, num_loops, 0,
449 ntdb_flags));
453 while (num_procs) {
454 int status, j;
455 pid_t pid;
457 if (error_count != 0) {
458 /* try and stop the test on any failure */
459 for (j=0;j<num_procs;j++) {
460 if (pids[j] != 0) {
461 kill(pids[j], SIGTERM);
466 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
467 if (pid == 0) {
468 struct timespec ts;
470 /* Sleep for 1/10 second. */
471 ts.tv_sec = 0;
472 ts.tv_nsec = 100000000;
473 nanosleep(&ts, NULL);
475 /* Kill someone. */
476 kill(pids[random() % num_procs], SIGUSR1);
477 continue;
480 if (pid == -1) {
481 perror("failed to wait for child\n");
482 exit(1);
485 for (j=0;j<num_procs;j++) {
486 if (pids[j] == pid) break;
488 if (j == num_procs) {
489 printf("unknown child %d exited!?\n", (int)pid);
490 exit(1);
492 if (WIFSIGNALED(status)) {
493 if (WTERMSIG(status) == SIGUSR2
494 || WTERMSIG(status) == SIGUSR1) {
495 /* SIGUSR2 means they wrote to pipe. */
496 if (WTERMSIG(status) == SIGUSR2) {
497 if (read(pfds[0], &done[j],
498 sizeof(done[j]))
499 != sizeof(done[j]))
500 err(1,
501 "Short read from child?");
503 pids[j] = fork();
504 if (pids[j] == 0)
505 exit(run_child(test_ntdb, j, seed,
506 num_loops, done[j],
507 ntdb_flags));
508 printf("Restarting child %i for %u-%u\n",
509 j, done[j], num_loops);
510 continue;
512 printf("child %d exited with signal %d\n",
513 (int)pid, WTERMSIG(status));
514 error_count++;
515 } else {
516 if (WEXITSTATUS(status) != 0) {
517 printf("child %d exited with status %d\n",
518 (int)pid, WEXITSTATUS(status));
519 error_count++;
522 memmove(&pids[j], &pids[j+1],
523 (num_procs - j - 1)*sizeof(pids[0]));
524 num_procs--;
527 free(pids);
529 done:
530 if (error_count == 0) {
531 db = ntdb_open(test_ntdb, NTDB_DEFAULT, O_RDWR | O_CREAT,
532 0600, &log_attr);
533 if (!db) {
534 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), test_ntdb,
535 "db open failed");
536 exit(1);
538 e = ntdb_check(db, NULL, NULL);
539 if (e) {
540 warn_on_err(e, db, "db check failed");
541 exit(1);
543 ntdb_close(db);
544 printf("OK\n");
547 free(test_ntdb);
548 return error_count;