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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <sys/wait.h>
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pdfw.h"
/* Only pdfw.c/ttf.c allocations are redirected, never libc or codec internals. */
static void *live[1024];
static size_t calls, fail_at;
static int persistent, failed;
static int
reject(void)
{
calls++;
if (fail_at && (calls == fail_at || (persistent && calls > fail_at))) {
failed = 1;
return 1;
}
return 0;
}
static void
record(void *p)
{
size_t i;
if (p == NULL)
return;
for (i = 0; i < sizeof(live) / sizeof(live[0]); i++)
if (live[i] == NULL) {
live[i] = p;
return;
}
abort();
}
void *
test_malloc(size_t n)
{
void *p;
if (reject())
return NULL;
p = malloc(n);
record(p);
return p;
}
void *
test_calloc(size_t n, size_t size)
{
void *p;
if (reject())
return NULL;
p = calloc(n, size);
record(p);
return p;
}
void *
test_realloc(void *old, size_t n)
{
void *p;
size_t i;
if (reject())
return NULL;
if (old == NULL) {
p = realloc(NULL, n);
record(p);
return p;
}
for (i = 0; i < sizeof(live) / sizeof(live[0]); i++)
if (live[i] == old)
break;
assert(i < sizeof(live) / sizeof(live[0]));
p = realloc(old, n);
if (p != NULL)
live[i] = p;
return p;
}
char *
test_strdup(const char *s)
{
char *p = test_malloc(strlen(s) + 1);
if (p != NULL)
strcpy(p, s);
return p;
}
void
test_free(void *p)
{
size_t i;
if (p == NULL)
return;
for (i = 0; i < sizeof(live) / sizeof(live[0]); i++)
if (live[i] == p)
break;
assert(i < sizeof(live) / sizeof(live[0]));
live[i] = NULL;
free(p);
}
static int
check_leaks(void)
{
size_t i, n = 0;
for (i = 0; i < sizeof(live) / sizeof(live[0]); i++)
if (live[i] != NULL)
n++;
if (n)
fprintf(stderr, "FAIL: %zu writer/font allocations leaked\n", n);
return n ? 1 : 0;
}
static int
exercise(const char *mode, const char *font_path, const char *image,
const char *output)
{
pdfw *p;
char text[12000];
int font, rc, image_rc;
if (strcmp(mode, "open") == 0) {
fail_at = 2;
persistent = 1;
}
p = pdfw_open(output);
if (p == NULL)
return check_leaks();
if (strcmp(mode, "open") == 0) {
pdfw_close(p);
fprintf(stderr, "FAIL: open accepted failed object allocation\n");
return 1;
}
pdfw_page(p, 595, 842);
if (strcmp(mode, "growth") == 0) {
pdfw_text(p, PDFW_HELVETICA, 12, 10, 20, "initial buffer");
fail_at = calls + 1;
}
if (strcmp(mode, "font-used") == 0)
fail_at = calls + 2; /* ttf bytes, then glyph bookkeeping */
if (strcmp(mode, "font-path") == 0)
fail_at = calls + 3;
if (strcmp(mode, "growth") == 0)
font = PDFW_HELVETICA;
else
font = pdfw_font(p, font_path);
memset(text, 'A', sizeof(text) - 1);
text[sizeof(text) - 1] = '\0';
pdfw_text(p, font, 12, 10, 20, text);
pdfw_clip_circle(p, 100, 100, 20);
image_rc = pdfw_image(p, image, 80, 80, 40, 40);
pdfw_clip_end(p);
pdfw_page(p, 842, 595);
pdfw_text(p, font, 12, 10, 20, "Second page (text) \\ and Unicode: ₹");
pdfw_image(p, image, 80, 80, 40, 40);
rc = pdfw_close(p);
if (fail_at && !failed) {
fprintf(stderr, "FAIL: allocation injection not reached\n");
return 1;
}
if ((!fail_at && (rc != 0 || font == PDFW_HELVETICA || image_rc != 0)) ||
(failed && (strcmp(mode, "sweep") != 0 ||
(font != PDFW_HELVETICA && image_rc == 0)) && rc != -1)) {
fprintf(stderr, "FAIL: unexpected render/close result %d\n", rc);
return 1;
}
return check_leaks();
}
static int
run_case(const char *mode, const char *font, const char *image,
const char *output, size_t at, int permanent)
{
pid_t pid;
int status;
fflush(NULL);
pid = fork();
assert(pid != -1);
if (pid == 0) {
alarm(3);
calls = 0;
fail_at = at;
persistent = permanent;
exit(exercise(mode, font, image, output));
}
assert(waitpid(pid, &status, 0) == pid);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
fprintf(stderr, "FAIL: %s allocation=%zu persistent=%d status=%d\n",
mode, at, permanent, status);
return 1;
}
return 0;
}
int
main(int argc, char **argv)
{
const char *modes[] = { "open", "growth", "font-used", "font-path" };
size_t i, total;
int errors = 0, permanent;
assert(argc == 4);
/* Keep the healthy two-page PDF for independent qpdf validation. */
if (exercise("healthy", argv[1], argv[2], argv[3]) != 0)
return 1;
total = calls;
for (i = 0; i < sizeof(modes) / sizeof(modes[0]); i++)
errors += run_case(modes[i], argv[1], argv[2], "/dev/null", 0, 0);
for (permanent = 0; permanent <= 1; permanent++)
for (i = 1; i <= total; i++)
errors += run_case("sweep", argv[1], argv[2], "/dev/null",
i, permanent);
printf("pdfw allocation contract: %zu sites, transient/persistent failures, "
"%d failed cases\n", total, errors);
return errors ? 1 : 0;
}