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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
/*
* bytecode.c
* variable expansion bytecode evaluator
*
* SPDX-License-Identifier: pkgconf
*
* Copyright (c) 2026 pkgconf authors (see AUTHORS).
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <libpkgconf/stdinc.h>
#include <libpkgconf/libpkgconf.h>
/*
* !doc
*
* libpkgconf `bytecode` module
* ============================
*
* The libpkgconf `bytecode` module contains the functions related to
* evaluating variable expansion bytecode.
*/
#define PKGCONF_EVAL_MAX_OUTPUT (PKGCONF_BUFSIZE - 1)
#define PKGCONF_EVAL_MAX_ITERATIONS (512)
static bool
pkgconf_bytecode_eval_append_slice(pkgconf_bytecode_eval_ctx_t *ctx, pkgconf_buffer_t *out, const char *p, size_t n)
{
size_t cur = pkgconf_buffer_len(out);
if (cur >= PKGCONF_EVAL_MAX_OUTPUT)
return false;
if (n > PKGCONF_EVAL_MAX_OUTPUT - cur)
{
pkgconf_warn(ctx->client, "warning: truncating very long variable to 64KB\n");
n = PKGCONF_EVAL_MAX_OUTPUT - cur;
if (!pkgconf_buffer_append_slice(out, p, n))
pkgconf_error(ctx->client, "pkgconf_bytecode_eval_append_slice: failed to append to slice");
return false;
}
return pkgconf_buffer_append_slice(out, p, n);
}
static bool
pkgconf_bytecode_eval_append(pkgconf_bytecode_eval_ctx_t *ctx, pkgconf_buffer_t *out, const char *s)
{
if (s == NULL || *s == '\0')
return true;
return pkgconf_bytecode_eval_append_slice(ctx, out, s, strlen(s));
}
static bool
pkgconf_bytecode_eval_internal(pkgconf_bytecode_eval_ctx_t *ctx, const pkgconf_bytecode_t *bc, pkgconf_buffer_t *out, bool *saw_sysroot);
static pkgconf_variable_t *
pkgconf_bytecode_eval_scan(const pkgconf_list_t *vars, const char *name, size_t nlen, unsigned int require_flags, unsigned int forbid_flags)
{
const pkgconf_node_t *node;
PKGCONF_FOREACH_LIST_ENTRY(vars->head, node)
{
pkgconf_variable_t *v = node->data;
if ((v->flags & require_flags) != require_flags)
continue;
if ((v->flags & forbid_flags) != 0)
continue;
if (pkgconf_str_eq_slice(v->key, name, nlen))
return v;
}
return NULL;
}
pkgconf_variable_t *
pkgconf_bytecode_eval_lookup_var(pkgconf_bytecode_eval_ctx_t *ctx, const char *name, size_t nlen)
{
pkgconf_variable_t *v;
if ((v = pkgconf_bytecode_eval_scan(&ctx->client->global_vars, name, nlen, PKGCONF_VARIABLEF_OVERRIDE, 0)) != NULL)
return v;
if (ctx->vars != NULL && (v = pkgconf_bytecode_eval_scan(ctx->vars, name, nlen, 0, 0)) != NULL)
return v;
if ((v = pkgconf_bytecode_eval_scan(&ctx->client->global_vars, name, nlen, 0, PKGCONF_VARIABLEF_OVERRIDE)) != NULL)
return v;
return NULL;
}
static bool
pkgconf_bytecode_eval_var(pkgconf_bytecode_eval_ctx_t *ctx, const char *name, size_t nlen, pkgconf_buffer_t *out, bool *saw_sysroot)
{
pkgconf_variable_t *v;
v = pkgconf_bytecode_eval_lookup_var(ctx, name, nlen);
if (v == NULL)
return true;
if (v->expanding)
return false;
v->expanding = true;
bool inner_saw = false;
bool ok = pkgconf_bytecode_eval_internal(ctx, &v->bc, out, &inner_saw);
v->expanding = false;
if (!ok)
return false;
if (saw_sysroot != NULL)
*saw_sysroot |= inner_saw;
return true;
}
static bool
pkgconf_bytecode_eval_internal(pkgconf_bytecode_eval_ctx_t *ctx, const pkgconf_bytecode_t *bc, pkgconf_buffer_t *out, bool *saw_sysroot)
{
(void) ctx;
if (bc == NULL || out == NULL)
return false;
const uint8_t *p = bc->base;
const uint8_t *end = bc->base + bc->len;
if (++ctx->expansions > PKGCONF_EVAL_MAX_ITERATIONS)
{
pkgconf_warn(ctx->client,
"warning: bytecode program exceeds iteration limit (" SIZE_FMT_SPECIFIER ")\n",
ctx->expansions - 1);
return false;
}
while (p < end)
{
const pkgconf_bytecode_op_t *op =
(const pkgconf_bytecode_op_t *)p;
if ((const uint8_t *)op + sizeof(*op) > end)
return false;
if ((const uint8_t *)op + sizeof(*op) + op->size > end)
return false;
switch (op->tag)
{
case PKGCONF_BYTECODE_OP_TEXT:
/* this only fails due to truncation */
if (!pkgconf_bytecode_eval_append_slice(ctx, out, op->data, op->size))
return false;
break;
case PKGCONF_BYTECODE_OP_VAR:
if (!pkgconf_bytecode_eval_var(ctx, op->data, op->size, out, saw_sysroot))
return false;
break;
case PKGCONF_BYTECODE_OP_SYSROOT:
if (saw_sysroot != NULL)
*saw_sysroot = true;
if (!pkgconf_bytecode_eval_append(ctx, out, pkgconf_buffer_str_or_empty(&ctx->sysroot)))
return false;
break;
default:
/* reserved/unimplemented */
return false;
}
p = (const uint8_t *)pkgconf_bytecode_op_next(op);
}
return true;
}
static bool
pkgconf_bytecode_eval_ctx_init(pkgconf_bytecode_eval_ctx_t *ctx, const pkgconf_client_t *client, const pkgconf_list_t *vars)
{
memset(ctx, 0, sizeof(*ctx));
ctx->client = client;
ctx->vars = vars;
const char *raw = pkgconf_client_get_sysroot_dir(client);
/* disabled sysroot cases */
if (raw == NULL || *raw == '\0')
return true;
if (raw[0] == '.' && raw[1] == '\0')
return true;
if (raw[0] == '/' && raw[1] == '\0')
return true;
if (!pkgconf_buffer_append(&ctx->sysroot, raw))
return false;
while (pkgconf_buffer_len(&ctx->sysroot) > 1 && ctx->sysroot.end[-1] == '/')
{
if (!pkgconf_buffer_trim_byte(&ctx->sysroot))
return false;
}
/* if normalization yields "/", disable by making buffer empty */
if (pkgconf_buffer_len(&ctx->sysroot) == 1 && ctx->sysroot.base[0] == '/')
{
if (!pkgconf_buffer_trim_byte(&ctx->sysroot))
return false;
}
return true;
}
bool
pkgconf_bytecode_eval(const pkgconf_client_t *client, const pkgconf_list_t *vars, const pkgconf_bytecode_t *bc, pkgconf_buffer_t *out, bool *saw_sysroot)
{
bool ret;
if (client == NULL || bc == NULL || out == NULL)
return false;
pkgconf_bytecode_eval_ctx_t ctx;
if (!pkgconf_bytecode_eval_ctx_init(&ctx, client, vars))
return false;
if (saw_sysroot != NULL)
*saw_sysroot = false;
ret = pkgconf_bytecode_eval_internal(&ctx, bc, out, saw_sysroot);
pkgconf_buffer_finalize(&ctx.sysroot);
return ret;
}
bool
pkgconf_bytecode_emit(pkgconf_buffer_t *buf, enum pkgconf_bytecode_op tag, const void *data, uint32_t size)
{
pkgconf_bytecode_op_t op = {
.tag = tag,
.size = size,
};
if (!pkgconf_buffer_append_slice(buf, (const char *) &op, sizeof(op)))
return false;
if (size != 0)
{
if (!pkgconf_buffer_append_slice(buf, (const char *) data, (size_t) size))
return false;
}
return true;
}
bool
pkgconf_bytecode_emit_text(pkgconf_buffer_t *buf, const char *p, size_t n)
{
if (p == NULL || n == 0)
return true;
return pkgconf_bytecode_emit(buf, PKGCONF_BYTECODE_OP_TEXT, p, (uint32_t) n);
}
bool
pkgconf_bytecode_emit_var(pkgconf_buffer_t *buf, const char *name, size_t nlen)
{
if (name == NULL || nlen == 0)
return true;
return pkgconf_bytecode_emit(buf, PKGCONF_BYTECODE_OP_VAR, name, (uint32_t) nlen);
}
bool
pkgconf_bytecode_emit_sysroot(pkgconf_buffer_t *buf)
{
return pkgconf_bytecode_emit(buf, PKGCONF_BYTECODE_OP_SYSROOT, NULL, 0);
}
void
pkgconf_bytecode_from_buffer(pkgconf_bytecode_t *bc, const pkgconf_buffer_t *buf)
{
bc->base = (const uint8_t *)buf->base;
bc->len = (size_t)(buf->end - buf->base);
}
bool
pkgconf_bytecode_compile(pkgconf_buffer_t *out, const char *value)
{
const char *p, *text_start;
if (out == NULL || value == NULL)
return false;
p = value;
text_start = value;
for (; *p != '\0'; p++)
{
const char *name, *q;
if (*p != '$')
continue;
/* $$ escapes to a literal $ */
if (p[1] == '$')
{
if (p > text_start)
{
if (!pkgconf_bytecode_emit_text(out, text_start, (size_t)(p - text_start)))
return false;
}
if (!pkgconf_bytecode_emit_text(out, "$", 1))
return false;
p++;
text_start = p + 1;
continue;
}
if (p[1] != '{')
continue;
if (p > text_start)
{
if (!pkgconf_bytecode_emit_text(out, text_start, (size_t)(p - text_start)))
return false;
}
name = p + 2;
q = name;
for (; *q != '\0' && *q != '}'; q++)
;
/* make sure a variable expansion ends with } */
if (*q != '}')
{
text_start = p;
continue;
}
/* if this is not a valid variable, emit it as text */
size_t nlen = (size_t)(q - name);
if (nlen == 0 || nlen >= PKGCONF_ITEM_SIZE)
{
if (!pkgconf_bytecode_emit_text(out, p, (size_t)((q + 1) - p)))
return false;
p = q;
text_start = p + 1;
continue;
}
/* we need to special-case ${pc_sysrootdir} and emit OP_SYSROOT instead... */
if (nlen == strlen("pc_sysrootdir") && !memcmp(name, "pc_sysrootdir", nlen))
{
if (!pkgconf_bytecode_emit_sysroot(out))
return false;
}
else
{
if (!pkgconf_bytecode_emit_var(out, name, nlen))
return false;
}
p = q;
text_start = p + 1;
}
if (p > text_start)
{
if (!pkgconf_bytecode_emit_text(out, text_start, (size_t)(p - text_start)))
return false;
}
return true;
}
bool
pkgconf_bytecode_eval_str_to_buf(const pkgconf_client_t *client, const pkgconf_list_t *vars, const char *input, bool *saw_sysroot, pkgconf_buffer_t *out)
{
pkgconf_buffer_t bcbuf = PKGCONF_BUFFER_INITIALIZER;
pkgconf_bytecode_t bc;
bool ret = false;
if (!pkgconf_bytecode_compile(&bcbuf, input))
{
pkgconf_buffer_finalize(&bcbuf);
return false;
}
pkgconf_bytecode_from_buffer(&bc, &bcbuf);
ret = pkgconf_bytecode_eval(client, vars, &bc, out, saw_sysroot);
pkgconf_buffer_finalize(&bcbuf);
return ret;
}
char *
pkgconf_bytecode_eval_str(const pkgconf_client_t *client, const pkgconf_list_t *vars, const char *input, bool *saw_sysroot)
{
pkgconf_buffer_t out = PKGCONF_BUFFER_INITIALIZER;
if (!pkgconf_bytecode_eval_str_to_buf(client, vars, input, saw_sysroot, &out))
{
if (pkgconf_buffer_len(&out) > 0)
return pkgconf_buffer_freeze(&out);
pkgconf_buffer_finalize(&out);
return NULL;
}
if (pkgconf_buffer_len(&out) == 0)
{
pkgconf_buffer_finalize(&out);
return strdup("");
}
return pkgconf_buffer_freeze(&out);
}
bool
pkgconf_bytecode_references_var(const pkgconf_buffer_t *buf, const char *key)
{
const uint8_t *p, *end;
size_t klen;
if (buf == NULL || key == NULL)
return false;
klen = strlen(key);
p = (uint8_t *) buf->base;
end = (uint8_t *) buf->end;
while (p < end)
{
const pkgconf_bytecode_op_t *op = (const pkgconf_bytecode_op_t *)p;
if (p + sizeof(*op) > end)
return false;
if (p + sizeof(*op) + op->size > end)
return false;
if (op->tag == PKGCONF_BYTECODE_OP_VAR)
{
if (op->size == (uint32_t) klen && memcmp(op->data, key, klen) == 0)
return true;
}
p += sizeof(*op) + op->size;
}
return false;
}
static bool
pkgconf_bytecode_op_is_selfref(const pkgconf_bytecode_op_t *op, const char *key)
{
const size_t klen = strlen(key);
if (op->tag != PKGCONF_BYTECODE_OP_VAR)
return false;
if (op->size != (uint32_t) klen)
return false;
return memcmp(op->data, key, klen) == 0;
}
static bool
pkgconf_bytecode_append_stream(pkgconf_buffer_t *dst, const pkgconf_buffer_t *bcbuf)
{
if (dst == NULL || bcbuf == NULL || pkgconf_buffer_str(bcbuf) == NULL)
return true;
return pkgconf_buffer_append_slice(dst, pkgconf_buffer_str(bcbuf), pkgconf_buffer_len(bcbuf));
}
bool
pkgconf_bytecode_rewrite_selfrefs(pkgconf_buffer_t *out, const pkgconf_buffer_t *rhs, const char *key, const pkgconf_buffer_t *prev)
{
const uint8_t *p = (uint8_t *) rhs->base;
const uint8_t *end = (uint8_t *) rhs->end;
while (p < end)
{
const pkgconf_bytecode_op_t *op = (const pkgconf_bytecode_op_t *)p;
if (p + sizeof(*op) > end)
return false;
if (p + sizeof(*op) + op->size > end)
return false;
if (pkgconf_bytecode_op_is_selfref(op, key))
{
if (!pkgconf_bytecode_append_stream(out, prev))
return false;
}
else
{
if (!pkgconf_buffer_append_slice(out, (const char *) op, sizeof(*op) + op->size))
return false;
}
p += sizeof(*op) + op->size;
}
return true;
}
|