File tree 3 files changed +73
-6
lines changed
3 files changed +73
-6
lines changed Original file line number Diff line number Diff line change 11
11
#include <strings.h> /* strcasecmp */
12
12
#include <sys/stat.h>
13
13
#include <sys/wait.h>
14
- #include <time.h>
15
14
#include <unistd.h>
16
15
16
+ #if defined(__APPLE__ )
17
+ #include <mach/mach_time.h>
18
+ #else /* Assume POSIX environments */
19
+ #include <time.h>
20
+ #endif
21
+
17
22
#include "dudect/fixture.h"
18
23
#include "list.h"
24
+ #include "random.h"
19
25
20
26
/* Shannon entropy */
21
27
extern double shannon_entropy (const uint8_t * input_data );
@@ -979,6 +985,26 @@ static bool sanity_check()
979
985
return true;
980
986
}
981
987
988
+ uintptr_t os_random (uintptr_t seed )
989
+ {
990
+ /* ASLR makes the address random */
991
+ uintptr_t x = (uintptr_t ) & os_random ^ seed ;
992
+ #if defined(__APPLE__ )
993
+ x ^= (uintptr_t ) mach_absolute_time ();
994
+ #else
995
+ struct timespec time ;
996
+ clock_gettime (CLOCK_MONOTONIC , & time );
997
+ x ^= (uintptr_t ) time .tv_sec ;
998
+ x ^= (uintptr_t ) time .tv_nsec ;
999
+ #endif
1000
+ /* Do a few randomization steps */
1001
+ uintptr_t max = ((x ^ (x >> 17 )) & 0x0F ) + 1 ;
1002
+ for (uintptr_t i = 0 ; i < max ; i ++ )
1003
+ x = random_shuffle (x );
1004
+ assert (x );
1005
+ return x ;
1006
+ }
1007
+
982
1008
#define BUFSIZE 256
983
1009
int main (int argc , char * argv [])
984
1010
{
@@ -1026,7 +1052,11 @@ int main(int argc, char *argv[])
1026
1052
}
1027
1053
}
1028
1054
1029
- srand ((unsigned int ) (time (NULL )));
1055
+ /* A better seed can be obtained by combining getpid() and its parent ID
1056
+ * with the Unix time.
1057
+ */
1058
+ srand (os_random (getpid () ^ getppid ()));
1059
+
1030
1060
queue_init ();
1031
1061
init_cmd ();
1032
1062
console_init ();
Original file line number Diff line number Diff line change @@ -13,4 +13,39 @@ static inline uint8_t randombit(void)
13
13
return ret & 1 ;
14
14
}
15
15
16
+ #if INTPTR_MAX == INT64_MAX
17
+ #define M_INTPTR_SHIFT (3)
18
+ #elif INTPTR_MAX == INT32_MAX
19
+ #define M_INTPTR_SHIFT (2)
20
+ #else
21
+ #error Platform pointers must be 32 or 64 bits
22
+ #endif
23
+
24
+ #define M_INTPTR_SIZE (1 << M_INTPTR_SHIFT)
25
+
26
+ static inline uintptr_t random_shuffle (uintptr_t x )
27
+ {
28
+ /* Ensure we do not get stuck in generating zeros */
29
+ if (x == 0 )
30
+ x = 17 ;
31
+
32
+ #if M_INTPTR_SIZE == 8
33
+ /* by Sebastiano Vigna, see: <http://xoshiro.di.unimi.it/splitmix64.c> */
34
+ x ^= x >> 30 ;
35
+ x *= 0xbf58476d1ce4e5b9UL ;
36
+ x ^= x >> 27 ;
37
+ x *= 0x94d049bb133111ebUL ;
38
+ x ^= x >> 31 ;
39
+ #elif M_INTPTR_SIZE == 4
40
+ /* by Chris Wellons, see: <https://nullprogram.com/blog/2018/07/31/> */
41
+ x ^= x >> 16 ;
42
+ x *= 0x7feb352dUL ;
43
+ x ^= x >> 15 ;
44
+ x *= 0x846ca68bUL ;
45
+ x ^= x >> 16 ;
46
+ #endif
47
+
48
+ return x ;
49
+ }
50
+
16
51
#endif
Original file line number Diff line number Diff line change 1
1
#! /usr/bin/env bash
2
2
3
- CPPCHECK_suppresses=" --inline-suppr harness.c --suppress=unmatchedSuppression:harness.c --suppress=missingIncludeSystem \
4
- --suppress=unusedFunction:linenoise.c \
5
- --suppress=ConfigurationNotChecked:random.c \
3
+ CPPCHECK_suppresses=" --inline-suppr harness.c --suppress=unmatchedSuppression:harness.c \
4
+ --suppress=missingIncludeSystem \
5
+ --suppress=noValidConfiguration \
6
+ --suppress=unusedFunction \
7
+ --suppress=unmatchedSuppression:qtest.c \
6
8
--suppress=identicalInnerCondition:log2_lshift16.h \
7
9
--suppress=nullPointerRedundantCheck:report.c \
8
10
--suppress=nullPointerRedundantCheck:harness.c \
9
- --suppress=nullPointer:qtest.c \
10
11
--suppress=nullPointer:queue.c \
12
+ --suppress=nullPointer:qtest.c \
11
13
--suppress=unmatchedSuppression:queue.c"
12
14
CPPCHECK_OPTS=" -I. --enable=all --error-exitcode=1 --force $CPPCHECK_suppresses ."
13
15
You can’t perform that action at this time.
0 commit comments