Coverage Report

Created: 2026-11-28 17:05

/home/runner/work/panackelty/panackelty/src/vm/decode.c
Line
Count
Source (jump to first uncovered line)
1
#include "decode.h"
2
3
#include "program.h"
4
#include "utf8.h"
5
6
#include <stdlib.h>
7
#include <string.h>
8
9
/* Bounded version-8 decoding. No runtime values or host operations are created here. */
10
11
17.8k
#define MAX_FUNCTIONS 4096u
12
279k
#define MAX_INSTRUCTIONS 4000000u
13
18.3M
#define MAX_NAME 1024u
14
1.57M
#define MAX_TEXT (1024u * 1024u)
15
1.34M
#define MAX_DIGITS 4096u
16
17
typedef struct {
18
    const uint8_t *data;
19
    size_t len, at;
20
    const char *error;
21
} Reader;
22
23
static void fail(Reader *r, const char *message)
24
6.23k
{
25
6.23k
    if (!r->error) {
  Branch (25:9): [True: 5.86k, False: 370]
26
5.86k
        r->error = message;
27
5.86k
    }
28
6.23k
}
29
30
static bool take(Reader *r, size_t n, const uint8_t **out)
31
41.8M
{
32
41.8M
    if (n > r->len - r->at) {
  Branch (32:9): [True: 2.24k, False: 41.8M]
33
2.24k
        fail(r, "truncated data");
34
2.24k
        return false;
35
2.24k
    }
36
41.8M
    *out = r->data + r->at;
37
41.8M
    r->at += n;
38
41.8M
    return true;
39
41.8M
}
40
41
static bool u8(Reader *r, uint8_t *out)
42
18.0M
{
43
18.0M
    const uint8_t *p;
44
18.0M
    if (!take(r, 1, &p)) {
  Branch (44:9): [True: 633, False: 18.0M]
45
633
        return false;
46
633
    }
47
18.0M
    *out = p[0];
48
18.0M
    return true;
49
18.0M
}
50
51
static bool u16(Reader *r, uint16_t *out)
52
10.1M
{
53
10.1M
    const uint8_t *p;
54
10.1M
    if (!take(r, 2, &p)) {
  Branch (54:9): [True: 635, False: 10.1M]
55
635
        return false;
56
635
    }
57
10.1M
    *out = (uint16_t)((p[0] << 8) | p[1]);
58
10.1M
    return true;
59
10.1M
}
60
61
static bool u32(Reader *r, uint32_t *out)
62
3.35M
{
63
3.35M
    const uint8_t *p;
64
3.35M
    if (!take(r, 4, &p)) {
  Branch (64:9): [True: 313, False: 3.35M]
65
313
        return false;
66
313
    }
67
3.35M
    *out = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3];
68
3.35M
    return true;
69
3.35M
}
70
71
static char *text(Reader *r, bool name)
72
19.9M
{
73
19.9M
    uint32_t n32 = 0;
74
19.9M
    uint16_t n16 = 0;
75
19.9M
    size_t n;
76
19.9M
    if (name) {
  Branch (76:9): [True: 9.16M, False: 788k]
  Branch (76:9): [True: 9.16M, False: 788k]
77
18.3M
        if (!u16(r, &n16)) {
  Branch (77:13): [True: 603, False: 9.16M]
  Branch (77:13): [True: 603, False: 9.16M]
78
1.20k
            return NULL;
79
1.20k
        }
80
18.3M
        n = n16;
81
18.3M
        if (n > MAX_NAME) {
  Branch (81:13): [True: 581, False: 9.16M]
  Branch (81:13): [True: 581, False: 9.16M]
82
1.16k
            fail(r, "name exceeds limit");
83
1.16k
            return NULL;
84
1.16k
        }
85
18.3M
    } else {
86
1.57M
        if (!u32(r, &n32)) {
  Branch (86:13): [True: 14, False: 788k]
  Branch (86:13): [True: 14, False: 788k]
87
28
            return NULL;
88
28
        }
89
1.57M
        n = n32;
90
1.57M
        if (n > MAX_TEXT) {
  Branch (90:13): [True: 104, False: 788k]
  Branch (90:13): [True: 104, False: 788k]
91
208
            fail(r, "text exceeds limit");
92
208
            return NULL;
93
208
        }
94
1.57M
    }
95
19.9M
    const uint8_t *p;
96
19.9M
    if (!take(r, n, &p)) {
  Branch (96:9): [True: 568, False: 9.95M]
  Branch (96:9): [True: 568, False: 9.95M]
97
1.13k
        return NULL;
98
1.13k
    }
99
19.9M
    if (!utf8(p, n)) {
  Branch (99:9): [True: 981, False: 9.94M]
  Branch (99:9): [True: 981, False: 9.94M]
100
1.96k
        fail(r, "invalid UTF-8");
101
1.96k
        return NULL;
102
1.96k
    }
103
19.8M
    char *v = malloc(n + 1);
104
19.8M
    if (!v) {
  Branch (104:9): [True: 28, False: 9.94M]
  Branch (104:9): [True: 28, False: 9.94M]
105
56
        fail(r, "out of memory");
106
56
        return NULL;
107
56
    }
108
19.8M
    memcpy(v, p, n);
109
19.8M
    v[n] = 0;
110
19.8M
    return v;
111
19.8M
}
decode.c:text
Line
Count
Source
72
9.95M
{
73
9.95M
    uint32_t n32 = 0;
74
9.95M
    uint16_t n16 = 0;
75
9.95M
    size_t n;
76
9.95M
    if (name) {
  Branch (76:9): [True: 9.16M, False: 788k]
77
9.16M
        if (!u16(r, &n16)) {
  Branch (77:13): [True: 603, False: 9.16M]
78
603
            return NULL;
79
603
        }
80
9.16M
        n = n16;
81
9.16M
        if (n > MAX_NAME) {
  Branch (81:13): [True: 581, False: 9.16M]
82
581
            fail(r, "name exceeds limit");
83
581
            return NULL;
84
581
        }
85
9.16M
    } else {
86
788k
        if (!u32(r, &n32)) {
  Branch (86:13): [True: 14, False: 788k]
87
14
            return NULL;
88
14
        }
89
788k
        n = n32;
90
788k
        if (n > MAX_TEXT) {
  Branch (90:13): [True: 104, False: 788k]
91
104
            fail(r, "text exceeds limit");
92
104
            return NULL;
93
104
        }
94
788k
    }
95
9.95M
    const uint8_t *p;
96
9.95M
    if (!take(r, n, &p)) {
  Branch (96:9): [True: 568, False: 9.95M]
97
568
        return NULL;
98
568
    }
99
9.95M
    if (!utf8(p, n)) {
  Branch (99:9): [True: 981, False: 9.94M]
100
981
        fail(r, "invalid UTF-8");
101
981
        return NULL;
102
981
    }
103
9.94M
    char *v = malloc(n + 1);
104
9.94M
    if (!v) {
  Branch (104:9): [True: 28, False: 9.94M]
105
28
        fail(r, "out of memory");
106
28
        return NULL;
107
28
    }
108
9.94M
    memcpy(v, p, n);
109
9.94M
    v[n] = 0;
110
9.94M
    return v;
111
9.94M
}
decode.c:text
Line
Count
Source
72
9.95M
{
73
9.95M
    uint32_t n32 = 0;
74
9.95M
    uint16_t n16 = 0;
75
9.95M
    size_t n;
76
9.95M
    if (name) {
  Branch (76:9): [True: 9.16M, False: 788k]
77
9.16M
        if (!u16(r, &n16)) {
  Branch (77:13): [True: 603, False: 9.16M]
78
603
            return NULL;
79
603
        }
80
9.16M
        n = n16;
81
9.16M
        if (n > MAX_NAME) {
  Branch (81:13): [True: 581, False: 9.16M]
82
581
            fail(r, "name exceeds limit");
83
581
            return NULL;
84
581
        }
85
9.16M
    } else {
86
788k
        if (!u32(r, &n32)) {
  Branch (86:13): [True: 14, False: 788k]
87
14
            return NULL;
88
14
        }
89
788k
        n = n32;
90
788k
        if (n > MAX_TEXT) {
  Branch (90:13): [True: 104, False: 788k]
91
104
            fail(r, "text exceeds limit");
92
104
            return NULL;
93
104
        }
94
788k
    }
95
9.95M
    const uint8_t *p;
96
9.95M
    if (!take(r, n, &p)) {
  Branch (96:9): [True: 568, False: 9.95M]
97
568
        return NULL;
98
568
    }
99
9.95M
    if (!utf8(p, n)) {
  Branch (99:9): [True: 981, False: 9.94M]
100
981
        fail(r, "invalid UTF-8");
101
981
        return NULL;
102
981
    }
103
9.94M
    char *v = malloc(n + 1);
104
9.94M
    if (!v) {
  Branch (104:9): [True: 28, False: 9.94M]
105
28
        fail(r, "out of memory");
106
28
        return NULL;
107
28
    }
108
9.94M
    memcpy(v, p, n);
109
9.94M
    v[n] = 0;
110
9.94M
    return v;
111
9.94M
}
112
113
static bool read_nat(Reader *r, PnBigInt *out)
114
668k
{
115
668k
    uint16_t n;
116
668k
    const uint8_t *p;
117
668k
    pn_big_init(out);
118
668k
    if (!u16(r, &n)) {
  Branch (118:9): [True: 5, False: 334k]
  Branch (118:9): [True: 5, False: 334k]
119
10
        return false;
120
10
    }
121
668k
    size_t max_bytes = (MAX_DIGITS * 3322u + 7999u) / 8000u;
122
668k
    if (n > max_bytes) {
  Branch (122:9): [True: 34, False: 334k]
  Branch (122:9): [True: 34, False: 334k]
123
68
        fail(r, "integer exceeds digit limit");
124
68
        return false;
125
68
    }
126
668k
    if (!take(r, n, &p)) {
  Branch (126:9): [True: 70, False: 334k]
  Branch (126:9): [True: 70, False: 334k]
127
140
        return false;
128
140
    }
129
668k
    if (n && p[0] == 0) {
  Branch (129:9): [True: 205k, False: 128k]
  Branch (129:14): [True: 3, False: 205k]
  Branch (129:9): [True: 205k, False: 128k]
  Branch (129:14): [True: 3, False: 205k]
130
6
        fail(r, "non-minimal integer");
131
6
        return false;
132
6
    }
133
668k
    if (!pn_big_from_bytes(out, p, n)) {
  Branch (133:9): [True: 10, False: 334k]
  Branch (133:9): [True: 10, False: 334k]
134
20
        fail(r, "out of memory");
135
20
        return false;
136
20
    }
137
668k
    char *digits = pn_big_string(out);
138
668k
    if (!digits) {
  Branch (138:9): [True: 2, False: 334k]
  Branch (138:9): [True: 2, False: 334k]
139
4
        fail(r, "out of memory");
140
4
        return false;
141
4
    }
142
668k
    if (strlen(digits) > MAX_DIGITS) {
  Branch (142:9): [True: 0, False: 334k]
  Branch (142:9): [True: 0, False: 334k]
143
0
        fail(r, "integer exceeds digit limit");
144
0
    }
145
668k
    free(digits);
146
668k
    return !r->error;
147
668k
}
decode.c:read_nat
Line
Count
Source
114
334k
{
115
334k
    uint16_t n;
116
334k
    const uint8_t *p;
117
334k
    pn_big_init(out);
118
334k
    if (!u16(r, &n)) {
  Branch (118:9): [True: 5, False: 334k]
119
5
        return false;
120
5
    }
121
334k
    size_t max_bytes = (MAX_DIGITS * 3322u + 7999u) / 8000u;
122
334k
    if (n > max_bytes) {
  Branch (122:9): [True: 34, False: 334k]
123
34
        fail(r, "integer exceeds digit limit");
124
34
        return false;
125
34
    }
126
334k
    if (!take(r, n, &p)) {
  Branch (126:9): [True: 70, False: 334k]
127
70
        return false;
128
70
    }
129
334k
    if (n && p[0] == 0) {
  Branch (129:9): [True: 205k, False: 128k]
  Branch (129:14): [True: 3, False: 205k]
130
3
        fail(r, "non-minimal integer");
131
3
        return false;
132
3
    }
133
334k
    if (!pn_big_from_bytes(out, p, n)) {
  Branch (133:9): [True: 10, False: 334k]
134
10
        fail(r, "out of memory");
135
10
        return false;
136
10
    }
137
334k
    char *digits = pn_big_string(out);
138
334k
    if (!digits) {
  Branch (138:9): [True: 2, False: 334k]
139
2
        fail(r, "out of memory");
140
2
        return false;
141
2
    }
142
334k
    if (strlen(digits) > MAX_DIGITS) {
  Branch (142:9): [True: 0, False: 334k]
143
0
        fail(r, "integer exceeds digit limit");
144
0
    }
145
334k
    free(digits);
146
334k
    return !r->error;
147
334k
}
decode.c:read_nat
Line
Count
Source
114
334k
{
115
334k
    uint16_t n;
116
334k
    const uint8_t *p;
117
334k
    pn_big_init(out);
118
334k
    if (!u16(r, &n)) {
  Branch (118:9): [True: 5, False: 334k]
119
5
        return false;
120
5
    }
121
334k
    size_t max_bytes = (MAX_DIGITS * 3322u + 7999u) / 8000u;
122
334k
    if (n > max_bytes) {
  Branch (122:9): [True: 34, False: 334k]
123
34
        fail(r, "integer exceeds digit limit");
124
34
        return false;
125
34
    }
126
334k
    if (!take(r, n, &p)) {
  Branch (126:9): [True: 70, False: 334k]
127
70
        return false;
128
70
    }
129
334k
    if (n && p[0] == 0) {
  Branch (129:9): [True: 205k, False: 128k]
  Branch (129:14): [True: 3, False: 205k]
130
3
        fail(r, "non-minimal integer");
131
3
        return false;
132
3
    }
133
334k
    if (!pn_big_from_bytes(out, p, n)) {
  Branch (133:9): [True: 10, False: 334k]
134
10
        fail(r, "out of memory");
135
10
        return false;
136
10
    }
137
334k
    char *digits = pn_big_string(out);
138
334k
    if (!digits) {
  Branch (138:9): [True: 2, False: 334k]
139
2
        fail(r, "out of memory");
140
2
        return false;
141
2
    }
142
334k
    if (strlen(digits) > MAX_DIGITS) {
  Branch (142:9): [True: 0, False: 334k]
143
0
        fail(r, "integer exceeds digit limit");
144
0
    }
145
334k
    free(digits);
146
334k
    return !r->error;
147
334k
}
148
149
static bool read_decimal(Reader *r, Constant *out)
150
7.07k
{
151
7.07k
    uint8_t sign;
152
7.07k
    uint16_t ex, digits;
153
7.07k
    const uint8_t *p;
154
7.07k
    if (!u8(r, &sign) || !u16(r, &ex) || !u16(r, &digits)) {
  Branch (154:9): [True: 1, False: 3.53k]
  Branch (154:26): [True: 3, False: 3.53k]
  Branch (154:42): [True: 2, False: 3.52k]
  Branch (154:9): [True: 1, False: 3.53k]
  Branch (154:26): [True: 3, False: 3.53k]
  Branch (154:42): [True: 2, False: 3.52k]
155
12
        return false;
156
12
    }
157
7.05k
    out->boolean = sign != 0;
158
7.05k
    out->exponent = (int16_t)ex;
159
7.05k
    if (sign > 1 || !digits || out->exponent > 4096 || out->exponent < -4096 ||
  Branch (159:9): [True: 11, False: 3.51k]
  Branch (159:21): [True: 1, False: 3.51k]
  Branch (159:32): [True: 2, False: 3.51k]
  Branch (159:56): [True: 4, False: 3.51k]
  Branch (159:9): [True: 11, False: 3.51k]
  Branch (159:21): [True: 1, False: 3.51k]
  Branch (159:32): [True: 2, False: 3.51k]
  Branch (159:56): [True: 4, False: 3.51k]
160
7.05k
        digits > MAX_DIGITS) {
  Branch (160:9): [True: 9, False: 3.50k]
  Branch (160:9): [True: 9, False: 3.50k]
161
54
        fail(r, "invalid decimal");
162
54
        return false;
163
54
    }
164
7.00k
    size_t bytes = (digits + 1) / 2;
165
7.00k
    if (!take(r, bytes, &p)) {
  Branch (165:9): [True: 10, False: 3.49k]
  Branch (165:9): [True: 10, False: 3.49k]
166
20
        return false;
167
20
    }
168
6.98k
    char *value = malloc(digits);
169
6.98k
    if (!value) {
  Branch (169:9): [True: 2, False: 3.49k]
  Branch (169:9): [True: 2, False: 3.49k]
170
4
        fail(r, "out of memory");
171
4
        return false;
172
4
    }
173
56.1k
    for (size_t i = 0; i < digits; i++) {
  Branch (173:24): [True: 24.6k, False: 3.46k]
  Branch (173:24): [True: 24.6k, False: 3.46k]
174
49.2k
        uint8_t d = (i & 1) ? p[i / 2] & 15 : p[i / 2] >> 4;
  Branch (174:21): [True: 10.5k, False: 14.0k]
  Branch (174:21): [True: 10.5k, False: 14.0k]
175
49.2k
        if (d > 9) {
  Branch (175:13): [True: 26, False: 24.5k]
  Branch (175:13): [True: 26, False: 24.5k]
176
52
            free(value);
177
52
            fail(r, "invalid decimal digit");
178
52
            return false;
179
52
        }
180
49.1k
        value[i] = (char)('0' + d);
181
49.1k
    }
182
6.92k
    if ((digits & 1) && (p[bytes - 1] & 15) != 15) {
  Branch (182:9): [True: 3.43k, False: 28]
  Branch (182:25): [True: 12, False: 3.42k]
  Branch (182:9): [True: 3.43k, False: 28]
  Branch (182:25): [True: 12, False: 3.42k]
183
24
        free(value);
184
24
        fail(r, "invalid decimal padding");
185
24
        return false;
186
24
    }
187
6.90k
    if (digits > 1 && value[0] == '0') {
  Branch (187:9): [True: 3.43k, False: 16]
  Branch (187:23): [True: 4, False: 3.43k]
  Branch (187:9): [True: 3.43k, False: 16]
  Branch (187:23): [True: 4, False: 3.43k]
188
8
        free(value);
189
8
        fail(r, "non-minimal decimal coefficient");
190
8
        return false;
191
8
    }
192
6.89k
    bool ok = pn_big_from_digits(&out->number, value, digits, sign ? -1 : 1);
  Branch (192:63): [True: 3.38k, False: 66]
  Branch (192:63): [True: 3.38k, False: 66]
193
6.89k
    free(value);
194
6.89k
    if (!ok) {
  Branch (194:9): [True: 2, False: 3.44k]
  Branch (194:9): [True: 2, False: 3.44k]
195
4
        fail(r, "out of memory");
196
4
    }
197
6.89k
    return ok;
198
6.90k
}
decode.c:read_decimal
Line
Count
Source
150
3.53k
{
151
3.53k
    uint8_t sign;
152
3.53k
    uint16_t ex, digits;
153
3.53k
    const uint8_t *p;
154
3.53k
    if (!u8(r, &sign) || !u16(r, &ex) || !u16(r, &digits)) {
  Branch (154:9): [True: 1, False: 3.53k]
  Branch (154:26): [True: 3, False: 3.53k]
  Branch (154:42): [True: 2, False: 3.52k]
155
6
        return false;
156
6
    }
157
3.52k
    out->boolean = sign != 0;
158
3.52k
    out->exponent = (int16_t)ex;
159
3.52k
    if (sign > 1 || !digits || out->exponent > 4096 || out->exponent < -4096 ||
  Branch (159:9): [True: 11, False: 3.51k]
  Branch (159:21): [True: 1, False: 3.51k]
  Branch (159:32): [True: 2, False: 3.51k]
  Branch (159:56): [True: 4, False: 3.51k]
160
3.52k
        digits > MAX_DIGITS) {
  Branch (160:9): [True: 9, False: 3.50k]
161
27
        fail(r, "invalid decimal");
162
27
        return false;
163
27
    }
164
3.50k
    size_t bytes = (digits + 1) / 2;
165
3.50k
    if (!take(r, bytes, &p)) {
  Branch (165:9): [True: 10, False: 3.49k]
166
10
        return false;
167
10
    }
168
3.49k
    char *value = malloc(digits);
169
3.49k
    if (!value) {
  Branch (169:9): [True: 2, False: 3.49k]
170
2
        fail(r, "out of memory");
171
2
        return false;
172
2
    }
173
28.0k
    for (size_t i = 0; i < digits; i++) {
  Branch (173:24): [True: 24.6k, False: 3.46k]
174
24.6k
        uint8_t d = (i & 1) ? p[i / 2] & 15 : p[i / 2] >> 4;
  Branch (174:21): [True: 10.5k, False: 14.0k]
175
24.6k
        if (d > 9) {
  Branch (175:13): [True: 26, False: 24.5k]
176
26
            free(value);
177
26
            fail(r, "invalid decimal digit");
178
26
            return false;
179
26
        }
180
24.5k
        value[i] = (char)('0' + d);
181
24.5k
    }
182
3.46k
    if ((digits & 1) && (p[bytes - 1] & 15) != 15) {
  Branch (182:9): [True: 3.43k, False: 28]
  Branch (182:25): [True: 12, False: 3.42k]
183
12
        free(value);
184
12
        fail(r, "invalid decimal padding");
185
12
        return false;
186
12
    }
187
3.45k
    if (digits > 1 && value[0] == '0') {
  Branch (187:9): [True: 3.43k, False: 16]
  Branch (187:23): [True: 4, False: 3.43k]
188
4
        free(value);
189
4
        fail(r, "non-minimal decimal coefficient");
190
4
        return false;
191
4
    }
192
3.44k
    bool ok = pn_big_from_digits(&out->number, value, digits, sign ? -1 : 1);
  Branch (192:63): [True: 3.38k, False: 66]
193
3.44k
    free(value);
194
3.44k
    if (!ok) {
  Branch (194:9): [True: 2, False: 3.44k]
195
2
        fail(r, "out of memory");
196
2
    }
197
3.44k
    return ok;
198
3.45k
}
decode.c:read_decimal
Line
Count
Source
150
3.53k
{
151
3.53k
    uint8_t sign;
152
3.53k
    uint16_t ex, digits;
153
3.53k
    const uint8_t *p;
154
3.53k
    if (!u8(r, &sign) || !u16(r, &ex) || !u16(r, &digits)) {
  Branch (154:9): [True: 1, False: 3.53k]
  Branch (154:26): [True: 3, False: 3.53k]
  Branch (154:42): [True: 2, False: 3.52k]
155
6
        return false;
156
6
    }
157
3.52k
    out->boolean = sign != 0;
158
3.52k
    out->exponent = (int16_t)ex;
159
3.52k
    if (sign > 1 || !digits || out->exponent > 4096 || out->exponent < -4096 ||
  Branch (159:9): [True: 11, False: 3.51k]
  Branch (159:21): [True: 1, False: 3.51k]
  Branch (159:32): [True: 2, False: 3.51k]
  Branch (159:56): [True: 4, False: 3.51k]
160
3.52k
        digits > MAX_DIGITS) {
  Branch (160:9): [True: 9, False: 3.50k]
161
27
        fail(r, "invalid decimal");
162
27
        return false;
163
27
    }
164
3.50k
    size_t bytes = (digits + 1) / 2;
165
3.50k
    if (!take(r, bytes, &p)) {
  Branch (165:9): [True: 10, False: 3.49k]
166
10
        return false;
167
10
    }
168
3.49k
    char *value = malloc(digits);
169
3.49k
    if (!value) {
  Branch (169:9): [True: 2, False: 3.49k]
170
2
        fail(r, "out of memory");
171
2
        return false;
172
2
    }
173
28.0k
    for (size_t i = 0; i < digits; i++) {
  Branch (173:24): [True: 24.6k, False: 3.46k]
174
24.6k
        uint8_t d = (i & 1) ? p[i / 2] & 15 : p[i / 2] >> 4;
  Branch (174:21): [True: 10.5k, False: 14.0k]
175
24.6k
        if (d > 9) {
  Branch (175:13): [True: 26, False: 24.5k]
176
26
            free(value);
177
26
            fail(r, "invalid decimal digit");
178
26
            return false;
179
26
        }
180
24.5k
        value[i] = (char)('0' + d);
181
24.5k
    }
182
3.46k
    if ((digits & 1) && (p[bytes - 1] & 15) != 15) {
  Branch (182:9): [True: 3.43k, False: 28]
  Branch (182:25): [True: 12, False: 3.42k]
183
12
        free(value);
184
12
        fail(r, "invalid decimal padding");
185
12
        return false;
186
12
    }
187
3.45k
    if (digits > 1 && value[0] == '0') {
  Branch (187:9): [True: 3.43k, False: 16]
  Branch (187:23): [True: 4, False: 3.43k]
188
4
        free(value);
189
4
        fail(r, "non-minimal decimal coefficient");
190
4
        return false;
191
4
    }
192
3.44k
    bool ok = pn_big_from_digits(&out->number, value, digits, sign ? -1 : 1);
  Branch (192:63): [True: 3.38k, False: 66]
193
3.44k
    free(value);
194
3.44k
    if (!ok) {
  Branch (194:9): [True: 2, False: 3.44k]
195
2
        fail(r, "out of memory");
196
2
    }
197
3.44k
    return ok;
198
3.45k
}
199
200
static bool read_constant(Reader *r, Constant *out)
201
2.06M
{
202
2.06M
    uint8_t sign;
203
2.06M
    memset(out, 0, sizeof(*out));
204
2.06M
    pn_big_init(&out->number);
205
2.06M
    if (!u8(r, &out->tag)) {
  Branch (205:9): [True: 9, False: 2.06M]
206
9
        return false;
207
9
    }
208
2.06M
    switch (out->tag) {
209
330k
    case 0:
  Branch (209:5): [True: 330k, False: 1.72M]
210
330k
        return read_nat(r, &out->number);
211
3.57k
    case 1:
  Branch (211:5): [True: 3.57k, False: 2.05M]
212
3.57k
        if (!u8(r, &sign) || sign > 1) {
  Branch (212:13): [True: 2, False: 3.56k]
  Branch (212:30): [True: 19, False: 3.54k]
213
21
            fail(r, "invalid integer sign");
214
21
            return false;
215
21
        }
216
3.54k
        if (!read_nat(r, &out->number)) {
  Branch (216:13): [True: 42, False: 3.50k]
217
42
            return false;
218
42
        }
219
3.50k
        if (sign && pn_big_is_zero(&out->number)) {
  Branch (219:13): [True: 3.50k, False: 4]
  Branch (219:21): [True: 2, False: 3.50k]
220
2
            fail(r, "negative zero integer");
221
2
            return false;
222
2
        }
223
3.50k
        if (sign) {
  Branch (223:13): [True: 3.50k, False: 4]
224
3.50k
            out->number.sign = -1;
225
3.50k
        }
226
3.50k
        return true;
227
3.53k
    case 2:
  Branch (227:5): [True: 3.53k, False: 2.05M]
228
3.53k
        return read_decimal(r, out);
229
700k
    case 3: {
  Branch (229:5): [True: 700k, False: 1.35M]
230
700k
        size_t start = r->at;
231
700k
        out->text = text(r, false);
232
700k
        if (out->text) {
  Branch (232:13): [True: 700k, False: 116]
233
700k
            out->text_length = r->at - start - 4;
234
700k
        }
235
700k
        return out->text != NULL;
236
3.50k
    }
237
500k
    case 4:
  Branch (237:5): [True: 500k, False: 1.56M]
238
500k
        if (!u8(r, &sign) || sign > 1) {
  Branch (238:13): [True: 1, False: 500k]
  Branch (238:30): [True: 47, False: 500k]
239
48
            fail(r, "invalid Bool constant");
240
48
            return false;
241
48
        }
242
500k
        out->boolean = sign != 0;
243
500k
        return true;
244
521k
    case 5:
  Branch (244:5): [True: 521k, False: 1.53M]
245
521k
        return true;
246
350
    default:
  Branch (246:5): [True: 350, False: 2.06M]
247
350
        fail(r, "unknown constant tag");
248
350
        return false;
249
2.06M
    }
250
2.06M
}
251
252
static char **strings(Reader *r, uint16_t count, bool names)
253
366k
{
254
366k
    char **items = calloc(count, sizeof(char *));
255
366k
    if (count && !items) {
  Branch (255:9): [True: 183k, False: 5]
  Branch (255:18): [True: 2, False: 183k]
  Branch (255:9): [True: 183k, False: 5]
  Branch (255:18): [True: 2, False: 183k]
256
4
        fail(r, "out of memory");
257
4
        return NULL;
258
4
    }
259
1.56M
    for (uint16_t i = 0; i < count && !r->error; i++) {
  Branch (259:26): [True: 598k, False: 183k]
  Branch (259:39): [True: 598k, False: 168]
  Branch (259:26): [True: 598k, False: 183k]
  Branch (259:39): [True: 598k, False: 168]
260
1.19M
        items[i] = text(r, names);
261
1.19M
    }
262
366k
    if (r->error) {
  Branch (262:9): [True: 285, False: 183k]
  Branch (262:9): [True: 285, False: 183k]
263
1.31M
        for (uint16_t i = 0; i < count; i++) {
  Branch (263:30): [True: 659k, False: 285]
  Branch (263:30): [True: 659k, False: 285]
264
1.31M
            free(items[i]);
265
1.31M
        }
266
570
        free(items);
267
570
        return NULL;
268
570
    }
269
366k
    return items;
270
366k
}
decode.c:strings
Line
Count
Source
253
183k
{
254
183k
    char **items = calloc(count, sizeof(char *));
255
183k
    if (count && !items) {
  Branch (255:9): [True: 183k, False: 5]
  Branch (255:18): [True: 2, False: 183k]
256
2
        fail(r, "out of memory");
257
2
        return NULL;
258
2
    }
259
782k
    for (uint16_t i = 0; i < count && !r->error; i++) {
  Branch (259:26): [True: 598k, False: 183k]
  Branch (259:39): [True: 598k, False: 168]
260
598k
        items[i] = text(r, names);
261
598k
    }
262
183k
    if (r->error) {
  Branch (262:9): [True: 285, False: 183k]
263
659k
        for (uint16_t i = 0; i < count; i++) {
  Branch (263:30): [True: 659k, False: 285]
264
659k
            free(items[i]);
265
659k
        }
266
285
        free(items);
267
285
        return NULL;
268
285
    }
269
183k
    return items;
270
183k
}
decode.c:strings
Line
Count
Source
253
183k
{
254
183k
    char **items = calloc(count, sizeof(char *));
255
183k
    if (count && !items) {
  Branch (255:9): [True: 183k, False: 5]
  Branch (255:18): [True: 2, False: 183k]
256
2
        fail(r, "out of memory");
257
2
        return NULL;
258
2
    }
259
782k
    for (uint16_t i = 0; i < count && !r->error; i++) {
  Branch (259:26): [True: 598k, False: 183k]
  Branch (259:39): [True: 598k, False: 168]
260
598k
        items[i] = text(r, names);
261
598k
    }
262
183k
    if (r->error) {
  Branch (262:9): [True: 285, False: 183k]
263
659k
        for (uint16_t i = 0; i < count; i++) {
  Branch (263:30): [True: 659k, False: 285]
264
659k
            free(items[i]);
265
659k
        }
266
285
        free(items);
267
285
        return NULL;
268
285
    }
269
183k
    return items;
270
183k
}
271
272
static bool instruction(Reader *r, Instruction *in)
273
13.4M
{
274
13.4M
    uint16_t count;
275
13.4M
    uint32_t ignored;
276
13.4M
    memset(in, 0, sizeof(*in));
277
13.4M
    if (!u8(r, &in->op)) {
  Branch (277:9): [True: 603, False: 13.4M]
278
603
        return false;
279
603
    }
280
13.4M
    switch (in->op) {
281
2.06M
    case OP_CONST:
  Branch (281:5): [True: 2.06M, False: 11.3M]
282
2.06M
        return read_constant(r, &in->constant);
283
3.44M
    case OP_LOAD:
  Branch (283:5): [True: 3.44M, False: 9.95M]
284
4.91M
    case OP_STORE:
  Branch (284:5): [True: 1.46M, False: 11.9M]
285
4.98M
    case OP_ITER_INIT:
  Branch (285:5): [True: 75.0k, False: 13.3M]
286
5.87M
    case OP_FIELD_GET:
  Branch (286:5): [True: 889k, False: 12.5M]
287
5.87M
        in->name = text(r, true);
288
5.87M
        return !r->error;
289
276k
    case OP_POP:
  Branch (289:5): [True: 276k, False: 13.1M]
290
284k
    case OP_MAKE_RANGE:
  Branch (290:5): [True: 7.95k, False: 13.4M]
291
372k
    case OP_INDEX_GET:
  Branch (291:5): [True: 87.5k, False: 13.3M]
292
477k
    case OP_MATCH_FAIL:
  Branch (292:5): [True: 105k, False: 13.3M]
293
614k
    case OP_RETURN:
  Branch (293:5): [True: 137k, False: 13.2M]
294
614k
        return true;
295
11
    case OP_AWAIT_VALUE:
  Branch (295:5): [True: 11, False: 13.4M]
296
2.28k
    case OP_CALL_VALUE:
  Branch (296:5): [True: 2.26k, False: 13.4M]
297
2.28k
        return u8(r, &in->arity);
298
39.4k
    case OP_UNARY:
  Branch (298:5): [True: 39.4k, False: 13.3M]
299
39.4k
        if (!u8(r, &in->code) || in->code > 1) {
  Branch (299:13): [True: 2, False: 39.4k]
  Branch (299:34): [True: 23, False: 39.4k]
300
25
            fail(r, "unknown unary operator");
301
25
            return false;
302
25
        }
303
39.4k
        return true;
304
779k
    case OP_BINARY:
  Branch (304:5): [True: 779k, False: 12.6M]
305
779k
        if (!u8(r, &in->code) || in->code > 12) {
  Branch (305:13): [True: 2, False: 779k]
  Branch (305:34): [True: 18, False: 779k]
306
20
            fail(r, "unknown binary operator");
307
20
            return false;
308
20
        }
309
779k
        return true;
310
144k
    case OP_MAKE_ARRAY:
  Branch (310:5): [True: 144k, False: 13.2M]
311
144k
        if (!u16(r, &count)) {
  Branch (311:13): [True: 3, False: 144k]
312
3
            return false;
313
3
        }
314
144k
        in->count = count;
315
144k
        return true;
316
35.3k
    case OP_INTERPOLATE:
  Branch (316:5): [True: 35.3k, False: 13.3M]
317
35.3k
        if (!u16(r, &count)) {
  Branch (317:13): [True: 3, False: 35.3k]
318
3
            return false;
319
3
        }
320
35.3k
        in->count = count;
321
35.3k
        in->items = strings(r, count, false);
322
35.3k
        return !r->error;
323
74.9k
    case OP_ITER_NEXT:
  Branch (323:5): [True: 74.9k, False: 13.3M]
324
74.9k
        in->name = text(r, true);
325
74.9k
        in->name2 = text(r, true);
326
74.9k
        return in->name && in->name2 && u32(r, &in->target);
  Branch (326:16): [True: 74.9k, False: 64]
  Branch (326:28): [True: 74.8k, False: 56]
  Branch (326:41): [True: 74.8k, False: 4]
327
148k
    case OP_MAKE_RECORD:
  Branch (327:5): [True: 148k, False: 13.2M]
328
148k
        in->name = text(r, true);
329
148k
        if (!in->name || !u16(r, &count)) {
  Branch (329:13): [True: 44, False: 148k]
  Branch (329:26): [True: 2, False: 148k]
330
46
            return false;
331
46
        }
332
148k
        in->count = count;
333
148k
        in->items = strings(r, count, true);
334
148k
        return !r->error;
335
307k
    case OP_MAKE_VARIANT:
  Branch (335:5): [True: 307k, False: 13.1M]
336
307k
        in->name = text(r, true);
337
307k
        in->name2 = text(r, true);
338
307k
        if (!in->name || !in->name2 || !u16(r, &count)) {
  Branch (338:13): [True: 55, False: 307k]
  Branch (338:26): [True: 50, False: 307k]
  Branch (338:40): [True: 2, False: 307k]
339
107
            return false;
340
107
        }
341
307k
        in->count = count;
342
307k
        return true;
343
450k
    case OP_MATCH_VARIANT:
  Branch (343:5): [True: 450k, False: 12.9M]
344
450k
        in->name = text(r, true);
345
450k
        return in->name && u32(r, &in->target);
  Branch (345:16): [True: 450k, False: 50]
  Branch (345:28): [True: 450k, False: 4]
346
412
    case OP_AWAIT_CALL:
  Branch (346:5): [True: 412, False: 13.4M]
347
973k
    case OP_CALL:
  Branch (347:5): [True: 972k, False: 12.4M]
348
973k
        in->name = text(r, true);
349
973k
        return in->name && u8(r, &in->arity);
  Branch (349:16): [True: 973k, False: 74]
  Branch (349:28): [True: 973k, False: 1]
350
737k
    case OP_JUMP_FALSE:
  Branch (350:5): [True: 737k, False: 12.6M]
351
1.90M
    case OP_JUMP:
  Branch (351:5): [True: 1.16M, False: 12.2M]
352
1.90M
        return u32(r, &in->target);
353
778
    default:
  Branch (353:5): [True: 778, False: 13.4M]
354
778
        (void)ignored;
355
778
        fail(r, "unknown bytecode opcode");
356
778
        return false;
357
13.4M
    }
358
13.4M
}
359
360
bool decode(const uint8_t *data, size_t length, Program *p, const char **error)
361
23.8k
{
362
23.8k
    memset(p, 0, sizeof(*p));
363
23.8k
    Reader r = {data, length, 0, NULL};
364
23.8k
    const uint8_t *magic;
365
23.8k
    uint16_t version, count;
366
23.8k
    if (length > MAX_ARTIFACT) {
  Branch (366:9): [True: 1, False: 11.9k]
  Branch (366:9): [True: 1, False: 11.9k]
367
2
        *error = "artifact exceeds size limit";
368
2
        return false;
369
2
    }
370
23.8k
    if (!take(&r, 9, &magic) || memcmp(magic, "PANACKBC\0", 9)) {
  Branch (370:9): [True: 18, False: 11.8k]
  Branch (370:33): [True: 2.42k, False: 9.46k]
  Branch (370:9): [True: 18, False: 11.8k]
  Branch (370:33): [True: 2.42k, False: 9.46k]
371
4.87k
        *error = "not a Panackelty bytecode file";
372
4.87k
        return false;
373
4.87k
    }
374
18.9k
    if (!u16(&r, &version)) {
  Branch (374:9): [True: 6, False: 9.46k]
  Branch (374:9): [True: 6, False: 9.46k]
375
12
        *error = r.error;
376
12
        return false;
377
12
    }
378
18.9k
    if (version != 9) {
  Branch (378:9): [True: 537, False: 8.92k]
  Branch (378:9): [True: 537, False: 8.92k]
379
1.07k
        *error = "unsupported bytecode version";
380
1.07k
        return false;
381
1.07k
    }
382
17.8k
    if (!u16(&r, &count)) {
  Branch (382:9): [True: 6, False: 8.91k]
  Branch (382:9): [True: 6, False: 8.91k]
383
12
        *error = r.error;
384
12
        return false;
385
12
    }
386
17.8k
    if (count > MAX_FUNCTIONS) {
  Branch (386:9): [True: 248, False: 8.67k]
  Branch (386:9): [True: 248, False: 8.67k]
387
496
        *error = "function count exceeds limit";
388
496
        return false;
389
496
    }
390
17.3k
    p->count = count;
391
17.3k
    p->functions = calloc(count, sizeof(Function));
392
17.3k
    if (count && !p->functions) {
  Branch (392:9): [True: 8.66k, False: 3]
  Branch (392:18): [True: 4, False: 8.66k]
  Branch (392:9): [True: 8.66k, False: 3]
  Branch (392:18): [True: 4, False: 8.66k]
393
8
        *error = "out of memory";
394
8
        return false;
395
8
    }
396
17.3k
    size_t total = 0;
397
296k
    for (size_t i = 0; i < count && !r.error; i++) {
  Branch (397:24): [True: 142k, False: 5.93k]
  Branch (397:37): [True: 142k, False: 136]
  Branch (397:24): [True: 142k, False: 5.93k]
  Branch (397:37): [True: 142k, False: 136]
398
284k
        Function *f = &p->functions[i];
399
284k
        uint8_t flags, params;
400
284k
        uint32_t ins;
401
284k
        f->name = text(&r, true);
402
284k
        if (!f->name || !u8(&r, &flags) || flags > 2 || !u8(&r, &params)) {
  Branch (402:13): [True: 1.42k, False: 140k]
  Branch (402:25): [True: 5, False: 140k]
  Branch (402:44): [True: 288, False: 140k]
  Branch (402:57): [True: 4, False: 140k]
  Branch (402:13): [True: 1.42k, False: 140k]
  Branch (402:25): [True: 5, False: 140k]
  Branch (402:44): [True: 288, False: 140k]
  Branch (402:57): [True: 4, False: 140k]
403
3.44k
            if (!r.error) {
  Branch (403:17): [True: 288, False: 1.43k]
  Branch (403:17): [True: 288, False: 1.43k]
404
576
                fail(&r, "invalid function flags");
405
576
            }
406
3.44k
            break;
407
3.44k
        }
408
281k
        f->pure = flags == 1;
409
281k
        f->is_async = flags == 2;
410
281k
        f->param_count = params;
411
281k
        f->params = calloc(params, sizeof(char *));
412
281k
        if (params && !f->params) {
  Branch (412:13): [True: 131k, False: 9.07k]
  Branch (412:23): [True: 2, False: 131k]
  Branch (412:13): [True: 131k, False: 9.07k]
  Branch (412:23): [True: 2, False: 131k]
413
4
            fail(&r, "out of memory");
414
4
            break;
415
4
        }
416
875k
        for (size_t j = 0; j < params && !r.error; j++) {
  Branch (416:28): [True: 297k, False: 140k]
  Branch (416:42): [True: 297k, False: 277]
  Branch (416:28): [True: 297k, False: 140k]
  Branch (416:42): [True: 297k, False: 277]
417
594k
            f->params[j] = text(&r, true);
418
594k
        }
419
281k
        if (!u32(&r, &ins)) {
  Branch (419:13): [True: 279, False: 140k]
  Branch (419:13): [True: 279, False: 140k]
420
558
            break;
421
558
        }
422
280k
        if (ins > 1000000u || total + ins > MAX_INSTRUCTIONS) {
  Branch (422:13): [True: 592, False: 139k]
  Branch (422:31): [True: 0, False: 139k]
  Branch (422:13): [True: 592, False: 139k]
  Branch (422:31): [True: 0, False: 139k]
423
1.18k
            fail(&r, "instruction count exceeds limit");
424
1.18k
            break;
425
1.18k
        }
426
279k
        total += ins;
427
279k
        f->ins_count = ins;
428
279k
        f->ins = calloc(ins, sizeof(Instruction));
429
279k
        if (ins && !f->ins) {
  Branch (429:13): [True: 139k, False: 5]
  Branch (429:20): [True: 6, False: 139k]
  Branch (429:13): [True: 139k, False: 5]
  Branch (429:20): [True: 6, False: 139k]
430
12
            fail(&r, "out of memory");
431
12
            break;
432
12
        }
433
27.0M
        for (size_t j = 0; j < ins && !r.error; j++) {
  Branch (433:28): [True: 13.4M, False: 136k]
  Branch (433:39): [True: 13.4M, False: 2.89k]
  Branch (433:28): [True: 13.4M, False: 136k]
  Branch (433:39): [True: 13.4M, False: 2.89k]
434
26.8M
            instruction(&r, &f->ins[j]);
435
26.8M
        }
436
279k
    }
437
17.3k
    if (!r.error && r.at != r.len) {
  Branch (437:9): [True: 2.87k, False: 5.79k]
  Branch (437:21): [True: 33, False: 2.83k]
  Branch (437:9): [True: 2.87k, False: 5.79k]
  Branch (437:21): [True: 33, False: 2.83k]
438
66
        fail(&r, "trailing data");
439
66
    }
440
17.3k
    if (r.error) {
  Branch (440:9): [True: 5.83k, False: 2.83k]
  Branch (440:9): [True: 5.83k, False: 2.83k]
441
11.6k
        *error = r.error;
442
11.6k
        free_program(p);
443
11.6k
        return false;
444
11.6k
    }
445
5.67k
    return true;
446
17.3k
}
decode
Line
Count
Source
361
11.9k
{
362
11.9k
    memset(p, 0, sizeof(*p));
363
11.9k
    Reader r = {data, length, 0, NULL};
364
11.9k
    const uint8_t *magic;
365
11.9k
    uint16_t version, count;
366
11.9k
    if (length > MAX_ARTIFACT) {
  Branch (366:9): [True: 1, False: 11.9k]
367
1
        *error = "artifact exceeds size limit";
368
1
        return false;
369
1
    }
370
11.9k
    if (!take(&r, 9, &magic) || memcmp(magic, "PANACKBC\0", 9)) {
  Branch (370:9): [True: 18, False: 11.8k]
  Branch (370:33): [True: 2.42k, False: 9.46k]
371
2.43k
        *error = "not a Panackelty bytecode file";
372
2.43k
        return false;
373
2.43k
    }
374
9.46k
    if (!u16(&r, &version)) {
  Branch (374:9): [True: 6, False: 9.46k]
375
6
        *error = r.error;
376
6
        return false;
377
6
    }
378
9.46k
    if (version != 9) {
  Branch (378:9): [True: 537, False: 8.92k]
379
537
        *error = "unsupported bytecode version";
380
537
        return false;
381
537
    }
382
8.92k
    if (!u16(&r, &count)) {
  Branch (382:9): [True: 6, False: 8.91k]
383
6
        *error = r.error;
384
6
        return false;
385
6
    }
386
8.91k
    if (count > MAX_FUNCTIONS) {
  Branch (386:9): [True: 248, False: 8.67k]
387
248
        *error = "function count exceeds limit";
388
248
        return false;
389
248
    }
390
8.67k
    p->count = count;
391
8.67k
    p->functions = calloc(count, sizeof(Function));
392
8.67k
    if (count && !p->functions) {
  Branch (392:9): [True: 8.66k, False: 3]
  Branch (392:18): [True: 4, False: 8.66k]
393
4
        *error = "out of memory";
394
4
        return false;
395
4
    }
396
8.66k
    size_t total = 0;
397
148k
    for (size_t i = 0; i < count && !r.error; i++) {
  Branch (397:24): [True: 142k, False: 5.93k]
  Branch (397:37): [True: 142k, False: 136]
398
142k
        Function *f = &p->functions[i];
399
142k
        uint8_t flags, params;
400
142k
        uint32_t ins;
401
142k
        f->name = text(&r, true);
402
142k
        if (!f->name || !u8(&r, &flags) || flags > 2 || !u8(&r, &params)) {
  Branch (402:13): [True: 1.42k, False: 140k]
  Branch (402:25): [True: 5, False: 140k]
  Branch (402:44): [True: 288, False: 140k]
  Branch (402:57): [True: 4, False: 140k]
403
1.72k
            if (!r.error) {
  Branch (403:17): [True: 288, False: 1.43k]
404
288
                fail(&r, "invalid function flags");
405
288
            }
406
1.72k
            break;
407
1.72k
        }
408
140k
        f->pure = flags == 1;
409
140k
        f->is_async = flags == 2;
410
140k
        f->param_count = params;
411
140k
        f->params = calloc(params, sizeof(char *));
412
140k
        if (params && !f->params) {
  Branch (412:13): [True: 131k, False: 9.07k]
  Branch (412:23): [True: 2, False: 131k]
413
2
            fail(&r, "out of memory");
414
2
            break;
415
2
        }
416
437k
        for (size_t j = 0; j < params && !r.error; j++) {
  Branch (416:28): [True: 297k, False: 140k]
  Branch (416:42): [True: 297k, False: 277]
417
297k
            f->params[j] = text(&r, true);
418
297k
        }
419
140k
        if (!u32(&r, &ins)) {
  Branch (419:13): [True: 279, False: 140k]
420
279
            break;
421
279
        }
422
140k
        if (ins > 1000000u || total + ins > MAX_INSTRUCTIONS) {
  Branch (422:13): [True: 592, False: 139k]
  Branch (422:31): [True: 0, False: 139k]
423
592
            fail(&r, "instruction count exceeds limit");
424
592
            break;
425
592
        }
426
139k
        total += ins;
427
139k
        f->ins_count = ins;
428
139k
        f->ins = calloc(ins, sizeof(Instruction));
429
139k
        if (ins && !f->ins) {
  Branch (429:13): [True: 139k, False: 5]
  Branch (429:20): [True: 6, False: 139k]
430
6
            fail(&r, "out of memory");
431
6
            break;
432
6
        }
433
13.5M
        for (size_t j = 0; j < ins && !r.error; j++) {
  Branch (433:28): [True: 13.4M, False: 136k]
  Branch (433:39): [True: 13.4M, False: 2.89k]
434
13.4M
            instruction(&r, &f->ins[j]);
435
13.4M
        }
436
139k
    }
437
8.66k
    if (!r.error && r.at != r.len) {
  Branch (437:9): [True: 2.87k, False: 5.79k]
  Branch (437:21): [True: 33, False: 2.83k]
438
33
        fail(&r, "trailing data");
439
33
    }
440
8.66k
    if (r.error) {
  Branch (440:9): [True: 5.83k, False: 2.83k]
441
5.83k
        *error = r.error;
442
5.83k
        free_program(p);
443
5.83k
        return false;
444
5.83k
    }
445
2.83k
    return true;
446
8.66k
}
decode
Line
Count
Source
361
11.9k
{
362
11.9k
    memset(p, 0, sizeof(*p));
363
11.9k
    Reader r = {data, length, 0, NULL};
364
11.9k
    const uint8_t *magic;
365
11.9k
    uint16_t version, count;
366
11.9k
    if (length > MAX_ARTIFACT) {
  Branch (366:9): [True: 1, False: 11.9k]
367
1
        *error = "artifact exceeds size limit";
368
1
        return false;
369
1
    }
370
11.9k
    if (!take(&r, 9, &magic) || memcmp(magic, "PANACKBC\0", 9)) {
  Branch (370:9): [True: 18, False: 11.8k]
  Branch (370:33): [True: 2.42k, False: 9.46k]
371
2.43k
        *error = "not a Panackelty bytecode file";
372
2.43k
        return false;
373
2.43k
    }
374
9.46k
    if (!u16(&r, &version)) {
  Branch (374:9): [True: 6, False: 9.46k]
375
6
        *error = r.error;
376
6
        return false;
377
6
    }
378
9.46k
    if (version != 9) {
  Branch (378:9): [True: 537, False: 8.92k]
379
537
        *error = "unsupported bytecode version";
380
537
        return false;
381
537
    }
382
8.92k
    if (!u16(&r, &count)) {
  Branch (382:9): [True: 6, False: 8.91k]
383
6
        *error = r.error;
384
6
        return false;
385
6
    }
386
8.91k
    if (count > MAX_FUNCTIONS) {
  Branch (386:9): [True: 248, False: 8.67k]
387
248
        *error = "function count exceeds limit";
388
248
        return false;
389
248
    }
390
8.67k
    p->count = count;
391
8.67k
    p->functions = calloc(count, sizeof(Function));
392
8.67k
    if (count && !p->functions) {
  Branch (392:9): [True: 8.66k, False: 3]
  Branch (392:18): [True: 4, False: 8.66k]
393
4
        *error = "out of memory";
394
4
        return false;
395
4
    }
396
8.66k
    size_t total = 0;
397
148k
    for (size_t i = 0; i < count && !r.error; i++) {
  Branch (397:24): [True: 142k, False: 5.93k]
  Branch (397:37): [True: 142k, False: 136]
398
142k
        Function *f = &p->functions[i];
399
142k
        uint8_t flags, params;
400
142k
        uint32_t ins;
401
142k
        f->name = text(&r, true);
402
142k
        if (!f->name || !u8(&r, &flags) || flags > 2 || !u8(&r, &params)) {
  Branch (402:13): [True: 1.42k, False: 140k]
  Branch (402:25): [True: 5, False: 140k]
  Branch (402:44): [True: 288, False: 140k]
  Branch (402:57): [True: 4, False: 140k]
403
1.72k
            if (!r.error) {
  Branch (403:17): [True: 288, False: 1.43k]
404
288
                fail(&r, "invalid function flags");
405
288
            }
406
1.72k
            break;
407
1.72k
        }
408
140k
        f->pure = flags == 1;
409
140k
        f->is_async = flags == 2;
410
140k
        f->param_count = params;
411
140k
        f->params = calloc(params, sizeof(char *));
412
140k
        if (params && !f->params) {
  Branch (412:13): [True: 131k, False: 9.07k]
  Branch (412:23): [True: 2, False: 131k]
413
2
            fail(&r, "out of memory");
414
2
            break;
415
2
        }
416
437k
        for (size_t j = 0; j < params && !r.error; j++) {
  Branch (416:28): [True: 297k, False: 140k]
  Branch (416:42): [True: 297k, False: 277]
417
297k
            f->params[j] = text(&r, true);
418
297k
        }
419
140k
        if (!u32(&r, &ins)) {
  Branch (419:13): [True: 279, False: 140k]
420
279
            break;
421
279
        }
422
140k
        if (ins > 1000000u || total + ins > MAX_INSTRUCTIONS) {
  Branch (422:13): [True: 592, False: 139k]
  Branch (422:31): [True: 0, False: 139k]
423
592
            fail(&r, "instruction count exceeds limit");
424
592
            break;
425
592
        }
426
139k
        total += ins;
427
139k
        f->ins_count = ins;
428
139k
        f->ins = calloc(ins, sizeof(Instruction));
429
139k
        if (ins && !f->ins) {
  Branch (429:13): [True: 139k, False: 5]
  Branch (429:20): [True: 6, False: 139k]
430
6
            fail(&r, "out of memory");
431
6
            break;
432
6
        }
433
13.5M
        for (size_t j = 0; j < ins && !r.error; j++) {
  Branch (433:28): [True: 13.4M, False: 136k]
  Branch (433:39): [True: 13.4M, False: 2.89k]
434
13.4M
            instruction(&r, &f->ins[j]);
435
13.4M
        }
436
139k
    }
437
8.66k
    if (!r.error && r.at != r.len) {
  Branch (437:9): [True: 2.87k, False: 5.79k]
  Branch (437:21): [True: 33, False: 2.83k]
438
33
        fail(&r, "trailing data");
439
33
    }
440
8.66k
    if (r.error) {
  Branch (440:9): [True: 5.83k, False: 2.83k]
441
5.83k
        *error = r.error;
442
5.83k
        free_program(p);
443
5.83k
        return false;
444
5.83k
    }
445
2.83k
    return true;
446
8.66k
}