about summary refs log tree commit diff
path: root/stdlib/tst-arc4random-fork.c
blob: b9d77615c33fb2a58d64f591c44688aa08f5cc68 (plain) (blame)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Test that subprocesses generate distinct streams of randomness.
   Copyright (C) 2022-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* Collect random data from subprocesses and check that all the
   results are unique.  */

#include <array_length.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/xthread.h>
#include <support/xunistd.h>
#include <unistd.h>

/* Perform multiple runs.  The subsequent runs start with an
   already-initialized random number generator.  */
enum { runs = 10 };

/* Total number of spawned processes on each run.  */
enum { subprocesses = 49 };

/* The total number of processes.  */
enum { processes = subprocesses + 1 };

/* Number of bytes of randomness to generate per process.  Large
   enough to make false positive duplicates extremely unlikely.  */
enum { random_size = 16 };

/* Generated bytes of randomness.  */
struct result
{
  unsigned char bytes[random_size];
};

/* Shared across all processes.  */
static struct shared_data
{
  pthread_barrier_t barrier;
  struct result results[runs][processes];
} *shared_data;

static void
generate_arc4random (unsigned char *bytes)
{
  for (int i = 0; i < random_size / sizeof (uint32_t); i++)
    {
      uint32_t x = arc4random ();
      memcpy (&bytes[4 * i], &x, sizeof x);
    }
}

static void
generate_arc4random_buf (unsigned char *bytes)
{
  arc4random_buf (bytes, random_size);
}

static void
generate_arc4random_uniform (unsigned char *bytes)
{
  for (int i = 0; i < random_size; i++)
    bytes[i] = arc4random_uniform (256);
}

/* Invoked to collect data from a subprocess.  */
static void
subprocess (int run, int process_index, void (*func)(unsigned char *))
{
  xpthread_barrier_wait (&shared_data->barrier);
  func (shared_data->results[run][process_index].bytes);
}

/* Used to sort the results.  */
struct index
{
  int run;
  int process_index;
};

/* Used to sort an array of struct index values.  */
static int
index_compare (const void *left1, const void *right1)
{
  const struct index *left = left1;
  const struct index *right = right1;

  return memcmp (shared_data->results[left->run][left->process_index].bytes,
                 shared_data->results[right->run][right->process_index].bytes,
                 random_size);
}

static int
do_test_func (void (*func)(unsigned char *bytes))
{
  /* Collect random data.  */
  for (int run = 0; run < runs; ++run)
    {
      pid_t pids[subprocesses];
      for (int process_index = 0; process_index < subprocesses;
           ++process_index)
        {
          pids[process_index] = xfork ();
          if (pids[process_index] == 0)
            {
              subprocess (run, process_index, func);
              _exit (0);
            }
        }

      /* Trigger all subprocesses.  Also add data from the parent
         process.  */
      subprocess (run, subprocesses, func);

      for (int process_index = 0; process_index < subprocesses;
           ++process_index)
        {
          int status;
          xwaitpid (pids[process_index], &status, 0);
          if (status != 0)
            FAIL_EXIT1 ("subprocess index %d (PID %d) exit status %d\n",
                        process_index, (int) pids[process_index], status);
        }
    }

  /* Check for duplicates.  */
  struct index indexes[runs * processes];
  for (int run = 0; run < runs; ++run)
    for (int process_index = 0; process_index < processes; ++process_index)
      indexes[run * processes + process_index]
        = (struct index) { .run = run, .process_index = process_index };
  qsort (indexes, array_length (indexes), sizeof (indexes[0]), index_compare);
  for (size_t i = 1; i < array_length (indexes); ++i)
    {
      if (index_compare (indexes + i - 1, indexes + i) == 0)
        {
          support_record_failure ();
          unsigned char *bytes
            = shared_data->results[indexes[i].run]
                [indexes[i].process_index].bytes;
          char *quoted = support_quote_blob (bytes, random_size);
          printf ("error: duplicate randomness data: \"%s\"\n"
                  "  run %d, subprocess %d\n"
                  "  run %d, subprocess %d\n",
                  quoted, indexes[i - 1].run, indexes[i - 1].process_index,
                  indexes[i].run, indexes[i].process_index);
          free (quoted);
        }
    }

  return 0;
}

static int
do_test (void)
{
  shared_data = support_shared_allocate (sizeof (*shared_data));
  {
    pthread_barrierattr_t attr;
    xpthread_barrierattr_init (&attr);
    xpthread_barrierattr_setpshared (&attr, PTHREAD_PROCESS_SHARED);
    xpthread_barrier_init (&shared_data->barrier, &attr, processes);
    xpthread_barrierattr_destroy (&attr);
  }

  do_test_func (generate_arc4random);
  do_test_func (generate_arc4random_buf);
  do_test_func (generate_arc4random_uniform);

  xpthread_barrier_destroy (&shared_data->barrier);
  support_shared_free (shared_data);
  shared_data = NULL;

  return 0;
}

#define TIMEOUT 40
#include <support/test-driver.c>