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