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