1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
5#include <assert.h>
6#include "pixman-private.h" /* For 'inline' definition */
7#include "utils-prng.h"
8
9#define ARRAY_LENGTH(A) ((int) (sizeof (A) / sizeof ((A) [0])))
10
11/* A primitive pseudorandom number generator,
12 * taken from POSIX.1-2001 example
13 */
14
15extern prng_t prng_state_data;
16extern prng_t *prng_state;
17#ifdef USE_OPENMP
18#pragma omp threadprivate(prng_state_data)
19#pragma omp threadprivate(prng_state)
20#endif
21
22static inline uint32_t
23prng_rand (void)
24{
25    return prng_rand_r (prng_state);
26}
27
28static inline void
29prng_srand (uint32_t seed)
30{
31    if (!prng_state)
32    {
33        /* Without setting a seed, PRNG does not work properly (is just
34         * returning zeros). So we only initialize the pointer here to
35         * make sure that 'prng_srand' is always called before any
36         * other 'prng_*' function. The wrongdoers violating this order
37         * will get a segfault. */
38        prng_state = &prng_state_data;
39    }
40    prng_srand_r (prng_state, seed);
41}
42
43static inline uint32_t
44prng_rand_n (int max)
45{
46    return prng_rand () % max;
47}
48
49static inline void
50prng_randmemset (void *buffer, size_t size, prng_randmemset_flags_t flags)
51{
52    prng_randmemset_r (prng_state, buffer, size, flags);
53}
54
55/* CRC 32 computation
56 */
57uint32_t
58compute_crc32 (uint32_t    in_crc32,
59	       const void *buf,
60	       size_t      buf_len);
61
62uint32_t
63compute_crc32_for_image (uint32_t        in_crc32,
64			 pixman_image_t *image);
65
66/* Returns TRUE if running on a little endian system
67 */
68static force_inline pixman_bool_t
69is_little_endian (void)
70{
71    unsigned long endian_check_var = 1;
72    return *(unsigned char *)&endian_check_var == 1;
73}
74
75/* perform endian conversion of pixel data
76 */
77void
78image_endian_swap (pixman_image_t *img);
79
80/* Allocate memory that is bounded by protected pages,
81 * so that out-of-bounds access will cause segfaults
82 */
83void *
84fence_malloc (int64_t len);
85
86void
87fence_free (void *data);
88
89/* Generate n_bytes random bytes in fence_malloced memory */
90uint8_t *
91make_random_bytes (int n_bytes);
92
93/* Return current time in seconds */
94double
95gettime (void);
96
97uint32_t
98get_random_seed (void);
99
100/* main body of the fuzzer test */
101int
102fuzzer_test_main (const char *test_name,
103		  int         default_number_of_iterations,
104		  uint32_t    expected_checksum,
105		  uint32_t    (*test_function)(int testnum, int verbose),
106		  int         argc,
107		  const char *argv[]);
108
109void
110fail_after (int seconds, const char *msg);
111
112/* If possible, enable traps for floating point exceptions */
113void enable_divbyzero_exceptions(void);
114
115/* Converts a8r8g8b8 pixels to pixels that
116 *  - are not premultiplied,
117 *  - are stored in this order in memory: R, G, B, A, regardless of
118 *    the endianness of the computer.
119 * It is allowed for @src and @dst to point to the same memory buffer.
120 */
121void
122a8r8g8b8_to_rgba_np (uint32_t *dst, uint32_t *src, int n_pixels);
123
124pixman_bool_t
125write_png (pixman_image_t *image, const char *filename);
126
127void
128draw_checkerboard (pixman_image_t *image,
129		   int check_size,
130		   uint32_t color1, uint32_t color2);
131
132/* A pair of macros which can help to detect corruption of
133 * floating point registers after a function call. This may
134 * happen if _mm_empty() call is forgotten in MMX/SSE2 fast
135 * path code, or ARM NEON assembly optimized function forgets
136 * to save/restore d8-d15 registers before use.
137 */
138
139#define FLOAT_REGS_CORRUPTION_DETECTOR_START()                 \
140    static volatile double frcd_volatile_constant1 = 123451;   \
141    static volatile double frcd_volatile_constant2 = 123452;   \
142    static volatile double frcd_volatile_constant3 = 123453;   \
143    static volatile double frcd_volatile_constant4 = 123454;   \
144    static volatile double frcd_volatile_constant5 = 123455;   \
145    static volatile double frcd_volatile_constant6 = 123456;   \
146    static volatile double frcd_volatile_constant7 = 123457;   \
147    static volatile double frcd_volatile_constant8 = 123458;   \
148    double frcd_canary_variable1 = frcd_volatile_constant1;    \
149    double frcd_canary_variable2 = frcd_volatile_constant2;    \
150    double frcd_canary_variable3 = frcd_volatile_constant3;    \
151    double frcd_canary_variable4 = frcd_volatile_constant4;    \
152    double frcd_canary_variable5 = frcd_volatile_constant5;    \
153    double frcd_canary_variable6 = frcd_volatile_constant6;    \
154    double frcd_canary_variable7 = frcd_volatile_constant7;    \
155    double frcd_canary_variable8 = frcd_volatile_constant8;
156
157#define FLOAT_REGS_CORRUPTION_DETECTOR_FINISH()                \
158    assert (frcd_canary_variable1 == frcd_volatile_constant1); \
159    assert (frcd_canary_variable2 == frcd_volatile_constant2); \
160    assert (frcd_canary_variable3 == frcd_volatile_constant3); \
161    assert (frcd_canary_variable4 == frcd_volatile_constant4); \
162    assert (frcd_canary_variable5 == frcd_volatile_constant5); \
163    assert (frcd_canary_variable6 == frcd_volatile_constant6); \
164    assert (frcd_canary_variable7 == frcd_volatile_constant7); \
165    assert (frcd_canary_variable8 == frcd_volatile_constant8);
166
167/* Try to get an aligned memory chunk */
168void *
169aligned_malloc (size_t align, size_t size);
170
171double
172convert_srgb_to_linear (double component);
173
174double
175convert_linear_to_srgb (double component);
176
177void
178initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb);
179
180const char *
181operator_name (pixman_op_t op);
182
183const char *
184format_name (pixman_format_code_t format);
185
186typedef struct
187{
188    double r, g, b, a;
189} color_t;
190
191void
192do_composite (pixman_op_t op,
193	      const color_t *src,
194	      const color_t *mask,
195	      const color_t *dst,
196	      color_t *result,
197	      pixman_bool_t component_alpha);
198
199void
200round_color (pixman_format_code_t format, color_t *color);
201
202typedef struct
203{
204    pixman_format_code_t format;
205    uint32_t am, rm, gm, bm;
206    uint32_t as, rs, gs, bs;
207    uint32_t aw, rw, gw, bw;
208} pixel_checker_t;
209
210void
211pixel_checker_init (pixel_checker_t *checker, pixman_format_code_t format);
212
213void
214pixel_checker_split_pixel (const pixel_checker_t *checker, uint32_t pixel,
215			   int *a, int *r, int *g, int *b);
216
217void
218pixel_checker_get_max (const pixel_checker_t *checker, color_t *color,
219		       int *a, int *r, int *g, int *b);
220
221void
222pixel_checker_get_min (const pixel_checker_t *checker, color_t *color,
223		       int *a, int *r, int *g, int *b);
224
225pixman_bool_t
226pixel_checker_check (const pixel_checker_t *checker,
227		     uint32_t pixel, color_t *color);
228
229void
230pixel_checker_convert_pixel_to_color (const pixel_checker_t *checker,
231                                      uint32_t pixel, color_t *color);
232
233void
234pixel_checker_get_masks (const pixel_checker_t *checker,
235                         uint32_t              *am,
236                         uint32_t              *rm,
237                         uint32_t              *gm,
238                         uint32_t              *bm);
239