dovecot-2.2: lib: add rand helper library

dovecot at dovecot.org dovecot at dovecot.org
Fri Jun 27 13:22:47 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/f9deece0002f
changeset: 17530:f9deece0002f
user:      Phil Carmody <phil at dovecot.fi>
date:      Fri Jun 27 16:15:24 2014 +0300
description:
lib: add rand helper library
Initially, just wrap srand() so that we can find out what the last-used
seed was. In situations where srand() is called only once (via this helper)
this lets us reproduce exactly the same stream of random data again in
order to reproduce rare crashes.

Signed-off-by: Phil Carmody <phil at dovecot.fi>

diffstat:

 src/lib/Makefile.am |   2 ++
 src/lib/lib.h       |   1 +
 src/lib/rand.c      |  24 ++++++++++++++++++++++++
 src/lib/rand.h      |  19 +++++++++++++++++++
 4 files changed, 46 insertions(+), 0 deletions(-)

diffs (81 lines):

diff -r 60fbbf7ea75d -r f9deece0002f src/lib/Makefile.am
--- a/src/lib/Makefile.am	Fri Jun 27 16:13:37 2014 +0300
+++ b/src/lib/Makefile.am	Fri Jun 27 16:15:24 2014 +0300
@@ -111,6 +111,7 @@
 	process-title.c \
 	priorityq.c \
 	randgen.c \
+	rand.c \
 	read-full.c \
 	restrict-access.c \
 	restrict-process-size.c \
@@ -229,6 +230,7 @@
 	printf-format-fix.h \
 	process-title.h \
 	priorityq.h \
+	rand.h \
 	randgen.h \
 	read-full.h \
 	restrict-access.h \
diff -r 60fbbf7ea75d -r f9deece0002f src/lib/lib.h
--- a/src/lib/lib.h	Fri Jun 27 16:13:37 2014 +0300
+++ b/src/lib/lib.h	Fri Jun 27 16:15:24 2014 +0300
@@ -28,6 +28,7 @@
 #include "data-stack.h"
 #include "mempool.h"
 #include "imem.h"
+#include "rand.h"
 
 typedef struct buffer buffer_t;
 typedef struct buffer string_t;
diff -r 60fbbf7ea75d -r f9deece0002f src/lib/rand.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/rand.c	Fri Jun 27 16:15:24 2014 +0300
@@ -0,0 +1,24 @@
+/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */
+
+/* Wrap srand() so that we can reproduce fuzzed tests */
+
+#include "lib.h"
+#include <stdlib.h>
+
+static int seeded = 0;
+static unsigned int seed;
+
+int rand_get_seed_count(void)
+{
+	return seeded;
+}
+unsigned int rand_get_last_seed(void)
+{
+	i_assert(seeded > 0);
+	return seed;
+}
+void rand_set_seed(unsigned int s)
+{
+	seeded++;
+	srand(seed = s);
+}
diff -r 60fbbf7ea75d -r f9deece0002f src/lib/rand.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/rand.h	Fri Jun 27 16:15:24 2014 +0300
@@ -0,0 +1,19 @@
+#ifndef RAND_H
+#define RAND_H
+
+/* Wrap srand() so that we can reproduce fuzzed tests */
+
+/* If we have seeded the prng precisely once, and we remember what
+ * value that was with, then we can reproduce any failing test cases
+ * that depend on that randomness by forcing the seed value (e.g. 
+ * in a debugger, by putting a breakpoint on rand_set_seed()).
+ */
+
+/* Number of times we've been seeded */ 
+int rand_get_seed_count(void);
+/* That last seed */
+unsigned int rand_get_last_seed(void);
+/* Actually seed the prng (could add char* for name of function?) */
+void rand_set_seed(unsigned int s);
+
+#endif


More information about the dovecot-cvs mailing list