Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | /*- * Copyright (c) 2011 David Schultz * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include <math.h> #include <unistd.h> #include <pthread.h> #include <sys/cdefs.h> #include <sys/types.h> #include <sys/mman.h> #include <sys/wait.h> #include <darwintest.h> #include <darwintest_utils.h> #define LEN (1024 * 32) static void * stress(void *ptr __unused) { uint32_t value; uint8_t buf[LEN + 1]; buf[LEN] = 0; for (int i = 0; i < LEN; i++){ value = arc4random(); if (value % 100 == 0) arc4random_stir(); if ((value & 0x70) == 0) arc4random_uniform(value); if ((value & 0x7) == 0) arc4random_buf(buf, (size_t)i); } T_ASSERT_EQ(buf[LEN], 0, NULL); return NULL; } T_DECL(arc4random_stress, "arc4random() stress") { const int ncpu = dt_ncpu(); pthread_t thr[ncpu]; for (int i = 0; i < ncpu; i++){ T_ASSERT_POSIX_ZERO(pthread_create(&thr[i], NULL, stress, NULL), NULL); } for (int i = 0; i < ncpu; i++){ T_ASSERT_POSIX_ZERO(pthread_join(thr[i], NULL), NULL); } } /* * BUFSIZE is the number of bytes of rc4 output to compare. The probability * that this test fails spuriously is 2**(-BUFSIZE * 8). */ #define BUFSIZE 8 T_DECL(arc4random_fork, "arc4random() shouldn't return the same sequence in child post-fork()") { struct shared_page { char parentbuf[BUFSIZE]; char childbuf[BUFSIZE]; } *page; pid_t pid; char c; page = mmap(NULL, sizeof(struct shared_page), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); T_ASSERT_NE(page, MAP_FAILED, "mmap()"); arc4random_buf(&c, 1); pid = fork(); if (pid < 0) { T_ASSERT_FAIL("fork() failed"); } else if (pid == 0) { /* child */ arc4random_buf(page->childbuf, BUFSIZE); exit(0); } else { /* parent */ int status; arc4random_buf(page->parentbuf, BUFSIZE); T_ASSERT_EQ(wait(&status), pid, "wait() returns child pid"); } T_EXPECT_NE(memcmp(page->parentbuf, page->childbuf, BUFSIZE), 0, "sequences are distinct"); } #define ARC4_EXPECTATION ((2u << 31) - 1)/2 #define ITER 10000000 #define PROB_THRESH sqrt(6*log2(ITER)/(ITER*1.0)) /* Simple randomness test using a chernoff bound * If this fails with probability (1 - 2/(ITER)^{4}) * arc4random is NOT a uniform distribution between * 0 and 2**32 - 1. Note that this test does not guarantee * that arc4random. Is correct, just that it isn't terribly * wrong. * */ T_DECL(arc4random_stats, "ensure arc4random() is random") { int i; int total = 0; for (i = 0; i < ITER; i++) { total += arc4random() > ARC4_EXPECTATION; } double prob = total/(ITER*1.0); T_EXPECT_LT(fabs(prob - 0.5), PROB_THRESH, "probability is within threshold"); } |