summaryrefslogtreecommitdiff
path: root/test/overflow.c
blob: cc07541113c6ed68fb1fdfff829108876595477d (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright © 2021 Adrian Johnson
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author: Adrian Johnson <ajohnson@redneon.com>
 */


#include "cairo-test.h"

#include <cairo.h>

/* Uncomment to enable faulty test data in order to force a
 * failure. This allows the error logging path to be tested.
 */
/* #define FORCE_FAILURE 1 */

struct test_data {
    uint64_t a;
    uint64_t b;
    uint64_t result;
    cairo_bool_t overflow;
};

#if SIZEOF_SIZE_T == 4
static const struct test_data add_32bit_test_data[] = {
    { 0x00000000, 0x00000000, 0x00000000, 0 },
    { 0x00000001, 0x00000000, 0x00000001, 0 },
    { 0x00000000, 0x00000001, 0x00000001, 0 },
    { 0xffffffff, 0x00000001, 0x00000000, 1 },
    { 0x00000001, 0xffffffff, 0x00000000, 1 },
    { 0xfffffffe, 0x00000001, 0xffffffff, 0 },
    { 0x00000001, 0xfffffffe, 0xffffffff, 0 },
    { 0x12345678, 0x98765432, 0xaaaaaaaa, 0 },
    { 0x80000000, 0x80000000, 0x00000000, 1 },

#if FORCE_FAILURE
    { 0x00000001, 0x00000002, 0x00000004, 1 },
#endif
};

static const struct test_data mul_32bit_test_data[] = {
    { 0x00000000, 0x00000000, 0x00000000, 0 },
    { 0x0000ffff, 0x0000ffff, 0xfffe0001, 0 },
    { 0x00010000, 0x00010000, 0x00000000, 1 },
    { 0x00000002, 0x80000000, 0x00000000, 1 },
    { 0x80000000, 0x00000002, 0x00000000, 1 },
    { 0xffffffff, 0x00000001, 0xffffffff, 0 },
};
#endif

#if SIZEOF_SIZE_T == 8
static const struct test_data add_64bit_test_data[] = {
    { 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0 },
    { 0x0000000000000001, 0x0000000000000000, 0x0000000000000001, 0 },
    { 0x0000000000000000, 0x0000000000000001, 0x0000000000000001, 0 },
    { 0xffffffffffffffff, 0x0000000000000001, 0x0000000000000000, 1 },
    { 0x0000000000000001, 0xffffffffffffffff, 0x0000000000000000, 1 },
    { 0x0000000000000001, 0xfffffffffffffffe, 0xffffffffffffffff, 0 },
    { 0xfffffffffffffffe, 0x0000000000000001, 0xffffffffffffffff, 0 },
    { 0x123456789abcdef0, 0x987654320fedcbba, 0xaaaaaaaaaaaaaaaa, 0 },
    { 0x8000000000000000, 0x8000000000000000, 0x0000000000000000, 1 },

#if FORCE_FAILURE
    { 0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 1 },
#endif
};

static const struct test_data mul_64bit_test_data[] = {
    { 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0 },
    { 0x00000000ffffffff, 0x00000000ffffffff, 0xfffffffe00000001, 0 },
    { 0x0000000100000000, 0x0000000100000000, 0x0000000000000000, 1 },
    { 0x0000000000000002, 0x8000000000000000, 0x0000000000000000, 1 },
    { 0x8000000000000000, 0x0000000000000002, 0x0000000000000000, 1 },
    { 0xffffffffffffffff, 0x0000000000000001, 0xffffffffffffffff, 0 },
};
#endif

static cairo_bool_t
check_if_result_fail (cairo_test_context_t *ctx,
                      size_t result,
                      cairo_bool_t overflow,
                      const struct test_data *data,
                      const char *func_name)
{
    int hex_digits = SIZEOF_SIZE_T * 2;
    if (overflow != data->overflow || (!data->overflow && result != data->result)) {
        cairo_test_log (ctx, "%s a = 0x%0*llx b = 0x%0*llx result = 0x%0*llx overflow = %d\n",
                        func_name,
                        hex_digits,
                        (unsigned long long)data->a,
                        hex_digits,
                        (unsigned long long)data->b,
                        hex_digits,
                        (unsigned long long)result,
                        overflow);
        if (data->overflow)
            cairo_test_log (ctx, "EXPECTED overflow = 1\n");
        else
            cairo_test_log (ctx, "EXPECTED result = 0x%0*llx overflow = 0\n",
                            hex_digits,
                            (unsigned long long)data->result);
        return TRUE;
    }
    return FALSE;
}

static cairo_test_status_t
preamble (cairo_test_context_t *ctx)
{
    int i;
    cairo_bool_t overflow;
    size_t result;

#if SIZEOF_SIZE_T == 4
    const struct test_data *add_data = add_32bit_test_data;
    int num_add_tests = ARRAY_LENGTH(add_32bit_test_data);
    const struct test_data *mul_data = mul_32bit_test_data;
    int num_mul_tests = ARRAY_LENGTH(mul_32bit_test_data);
#elif SIZEOF_SIZE_T == 8
    const struct test_data *add_data = add_64bit_test_data;
    int num_add_tests = ARRAY_LENGTH(add_64bit_test_data);
    const struct test_data *mul_data = mul_64bit_test_data;
    int num_mul_tests = ARRAY_LENGTH(mul_64bit_test_data);
#else
    cairo_test_log (ctx, "sizeof(size_t) = %lld is not supported by this test\n",
                    (unsigned long long)sizeof(size_t));
    return CAIRO_TEST_UNTESTED;
#endif

    /* First check the fallback versions of the overflow functions. */
    for (i = 0; i < num_add_tests; i++) {
        const struct test_data *data = &add_data[i];
        overflow = _cairo_fallback_add_size_t_overflow (data->a, data->b, &result);
        if (check_if_result_fail (ctx,
                                  result,
                                  overflow,
                                  data,
                                  "_cairo_fallback_add_size_t_overflow"))
        {
            return CAIRO_TEST_FAILURE;
        }
    }

    for (i = 0; i < num_mul_tests; i++) {
        const struct test_data *data = &mul_data[i];
        overflow = _cairo_fallback_mul_size_t_overflow (data->a, data->b, &result);
        if (check_if_result_fail (ctx,
                                  result,
                                  overflow,
                                  data,
                                  "_cairo_fallback_mul_size_t_overflow"))
        {
            return CAIRO_TEST_FAILURE;
        }
    }
    /* Next check the compiler builtins (if available, otherwise the
     * fallback versions are tested again). This is to ensure the fallback version
     * produces identical results to the compiler builtins.
     */
    for (i = 0; i < num_add_tests; i++) {
        const struct test_data *data = &add_data[i];
        overflow = _cairo_add_size_t_overflow (data->a, data->b, &result);
        if (check_if_result_fail (ctx,
                                  result,
                                  overflow,
                                  data,
                                  "_cairo_add_size_t_overflow"))
        {
            return CAIRO_TEST_FAILURE;
        }
    }

    for (i = 0; i < num_mul_tests; i++) {
        const struct test_data *data = &mul_data[i];
        overflow = _cairo_mul_size_t_overflow (data->a, data->b, &result);
        if (check_if_result_fail (ctx,
                                  result,
                                  overflow,
                                  data,
                                  "_cairo_mul_size_t_overflow"))
        {
            return CAIRO_TEST_FAILURE;
        }
    }

    return CAIRO_TEST_SUCCESS;
}

CAIRO_TEST (overflow,
	    "Test the _cairo_*_size_t_overflow functions.",
	    "memory", /* keywords */
	    NULL, /* requirements */
	    0, 0,
	    preamble, NULL)