1dca77083Schristos /*
2dca77083Schristos * Copyright (c) 2003-2012 Tim Kientzle
3dca77083Schristos * Copyright (c) 2012 Andres Mejia
4dca77083Schristos * All rights reserved.
5dca77083Schristos *
6dca77083Schristos * Redistribution and use in source and binary forms, with or without
7dca77083Schristos * modification, are permitted provided that the following conditions
8dca77083Schristos * are met:
9dca77083Schristos * 1. Redistributions of source code must retain the above copyright
10dca77083Schristos * notice, this list of conditions and the following disclaimer.
11dca77083Schristos * 2. Redistributions in binary form must reproduce the above copyright
12dca77083Schristos * notice, this list of conditions and the following disclaimer in the
13dca77083Schristos * documentation and/or other materials provided with the distribution.
14dca77083Schristos *
15dca77083Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16dca77083Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17dca77083Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18dca77083Schristos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19dca77083Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20dca77083Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21dca77083Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22dca77083Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23dca77083Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24dca77083Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25dca77083Schristos */
26dca77083Schristos
27dca77083Schristos #include "test_utils.h"
28dca77083Schristos
29*65e637abSchristos #include <errno.h>
30dca77083Schristos #include <stdlib.h>
31*65e637abSchristos #include <stdio.h>
32dca77083Schristos #include <string.h>
33*65e637abSchristos #include <assert.h>
34dca77083Schristos
35*65e637abSchristos static inline uint64_t
xorshift64(uint64_t * state)36*65e637abSchristos xorshift64(uint64_t *state)
37*65e637abSchristos {
38*65e637abSchristos uint64_t x = *state;
39*65e637abSchristos x ^= x << 13;
40*65e637abSchristos x ^= x >> 7;
41*65e637abSchristos x ^= x << 17;
42*65e637abSchristos *state = x;
43*65e637abSchristos return (x);
44*65e637abSchristos }
45*65e637abSchristos
46*65e637abSchristos /*
47*65e637abSchristos * Fill a buffer with reproducible pseudo-random data using a simple xorshift
48*65e637abSchristos * algorithm. Originally, most tests filled buffers with a loop that calls
49*65e637abSchristos * rand() once for each byte. However, this initialization can be extremely
50*65e637abSchristos * slow when running on emulated platforms such as QEMU where 16M calls to
51*65e637abSchristos * rand() take a long time: Before the test_write_format_7zip_large_copy test
52*65e637abSchristos * took ~22 seconds, whereas using a xorshift random number generator (that can
53*65e637abSchristos * be inlined) reduces it to ~17 seconds on QEMU RISC-V.
54dca77083Schristos */
55*65e637abSchristos static void
fill_with_pseudorandom_data_seed(uint64_t seed,void * buffer,size_t size)56*65e637abSchristos fill_with_pseudorandom_data_seed(uint64_t seed, void *buffer, size_t size)
57dca77083Schristos {
58*65e637abSchristos uint64_t *aligned_buffer;
59*65e637abSchristos size_t num_values;
60*65e637abSchristos size_t i;
61*65e637abSchristos size_t unaligned_suffix;
62*65e637abSchristos size_t unaligned_prefix = 0;
63*65e637abSchristos /*
64*65e637abSchristos * To avoid unaligned stores we only fill the aligned part of the buffer
65*65e637abSchristos * with pseudo-random data and fill the unaligned prefix with 0xab and
66*65e637abSchristos * the suffix with 0xcd.
67*65e637abSchristos */
68*65e637abSchristos if ((uintptr_t)buffer % sizeof(uint64_t)) {
69*65e637abSchristos unaligned_prefix =
70*65e637abSchristos sizeof(uint64_t) - (uintptr_t)buffer % sizeof(uint64_t);
71*65e637abSchristos aligned_buffer =
72*65e637abSchristos (uint64_t *)((char *)buffer + unaligned_prefix);
73*65e637abSchristos memset(buffer, 0xab, unaligned_prefix);
74dca77083Schristos } else {
75*65e637abSchristos aligned_buffer = (uint64_t *)buffer;
76*65e637abSchristos }
77*65e637abSchristos assert((uintptr_t)aligned_buffer % sizeof(uint64_t) == 0);
78*65e637abSchristos num_values = (size - unaligned_prefix) / sizeof(uint64_t);
79*65e637abSchristos unaligned_suffix =
80*65e637abSchristos size - unaligned_prefix - num_values * sizeof(uint64_t);
81*65e637abSchristos for (i = 0; i < num_values; i++) {
82*65e637abSchristos aligned_buffer[i] = xorshift64(&seed);
83*65e637abSchristos }
84*65e637abSchristos if (unaligned_suffix) {
85*65e637abSchristos memset((char *)buffer + size - unaligned_suffix, 0xcd,
86*65e637abSchristos unaligned_suffix);
87dca77083Schristos }
88dca77083Schristos }
89*65e637abSchristos
90*65e637abSchristos void
fill_with_pseudorandom_data(void * buffer,size_t size)91*65e637abSchristos fill_with_pseudorandom_data(void *buffer, size_t size)
92*65e637abSchristos {
93*65e637abSchristos uint64_t seed;
94*65e637abSchristos const char* seed_str;
95*65e637abSchristos /*
96*65e637abSchristos * Check if a seed has been specified in the environment, otherwise fall
97*65e637abSchristos * back to using rand() as a seed.
98*65e637abSchristos */
99*65e637abSchristos if ((seed_str = getenv("TEST_RANDOM_SEED")) != NULL) {
100*65e637abSchristos errno = 0;
101*65e637abSchristos seed = strtoull(seed_str, NULL, 10);
102*65e637abSchristos if (errno != 0) {
103*65e637abSchristos fprintf(stderr, "strtoull(%s) failed: %s", seed_str,
104*65e637abSchristos strerror(errno));
105*65e637abSchristos seed = rand();
106*65e637abSchristos }
107dca77083Schristos } else {
108*65e637abSchristos seed = rand();
109dca77083Schristos }
110*65e637abSchristos fill_with_pseudorandom_data_seed(seed, buffer, size);
111dca77083Schristos }
112