include/punk_observe/po_segio.h

filter: Statementstmt 99.2% 125 / 126 Branchbran 56.9% 41 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.8% 166 / 198 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_manifest.h ↑ Summary xs/svg.xs »
1 /* po_segio.h - the three signals, into and out of a segment.
2 *
3 * THE JOIN THAT PHASES 5, 6 AND 7 DEFERRED.
4 *
5 * Each of those phases built its structure in memory and stopped there,
6 * because a segment could not yet carry it. This is where they land on disk:
7 * metric chunks, log blocks and span arrays become regions, and a reader gets
8 * them back without copying - the arrays are read straight out of the
9 * mapping.
10 *
11 * Every region is length-prefixed and bounds-checked on the way back in. A
12 * region is untrusted input the moment it has been on a disk: a bit flip, a
13 * partial write a CRC somehow survived, or a file from a newer version all
14 * arrive here, and the answer to each is a refusal rather than a pointer into
15 * somebody else's memory.
16 */
17 #ifndef PO_SEGIO_H
18 #define PO_SEGIO_H
19  
20 #include "punk_observe/po_compat.h"
21 #include "punk_observe/po_seg.h"
22 #include "punk_observe/po_metric.h"
23 #include "punk_observe/po_log.h"
24 #include "punk_observe/po_span.h"
25 #include "punk_observe/po_traceidx.h"
26 #include "punk_observe/po_tattr.h"
27  
28 /* A tiny append-only buffer, so each serialiser reads the same way. */
29 typedef struct { char *b; size_t n, cap; int err; } po_buf;
30  
31 4 static int po_buf_init(po_buf *b) {
32 4 memset(b, 0, sizeof(*b));
33 4 b->cap = 256;
34 4 b->b = (char *)malloc(b->cap);
35 4 return b->b != NULL;
36 }
37 0 static void po_buf_free(po_buf *b) { free(b->b); b->b = NULL; }
38  
39 80 static void po_buf_put(po_buf *b, const void *p, size_t n) {
40 80 ▶ if (b->err) return;
Branch: 1 branch
BranchTrueFalseTotal
if (b->err) return; 0 80 80
41 80 ▶ if (b->n + n > b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->n + n > b->cap) { 2 78 80
42 2 size_t want = b->cap * 2;
43 char *nb;
44 2 ▶ while (want < b->n + n) want *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (want < b->n + n) want *= 2; 0 2 2
45 2 nb = (char *)realloc(b->b, want);
46 2 ▶ if (!nb) { b->err = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nb) { b->err = 1; return; } 0 2 2
47 2 b->b = nb; b->cap = want;
48 }
49 80 ▶ if (n) memcpy(b->b + b->n, p, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) memcpy(b->b + b->n, p, n); 80 0 80
50 80 b->n += n;
51 }
52 44 static void po_buf_u32(po_buf *b, uint32_t v) { v = po_le32(v); po_buf_put(b, &v, 4); }
53 24 static void po_buf_u64(po_buf *b, po_u64 v) { v = po_le64(v); po_buf_put(b, &v, 8); }
54  
55 /* A bounds-checked reader over a region. */
56 typedef struct { const char *b; size_t n, i; int err; } po_rd;
57  
58 1238 static void po_rd_init(po_rd *r, const char *p, size_t n) {
59 1238 r->b = p; r->n = n; r->i = 0; r->err = 0;
60 1238 }
61 13618 static uint32_t po_rd_u32(po_rd *r) {
62 uint32_t v;
63 13618 ▶ if (r->err || r->n - r->i < 4) { r->err = 1; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (r->err || r->n - r->i < 4) { r->err = 1; return 0; } 13618 0 13618
if (r->err || r->n - r->i < 4) { r->err = 1; return 0; } 0 13618 13618
64 13618 memcpy(&v, r->b + r->i, 4); r->i += 4;
65 13618 return po_le32(v);
66 }
67 7428 static po_u64 po_rd_u64(po_rd *r) {
68 po_u64 v;
69 7428 ▶ if (r->err || r->n - r->i < 8) { r->err = 1; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (r->err || r->n - r->i < 8) { r->err = 1; return 0; } 7428 0 7428
if (r->err || r->n - r->i < 8) { r->err = 1; return 0; } 0 7428 7428
70 7428 memcpy(&v, r->b + r->i, 8); r->i += 8;
71 7428 return po_le64(v);
72 }
73 /* Borrows into the region: zero copy, and valid while the mapping is. */
74 3714 static const char *po_rd_bytes(po_rd *r, size_t n) {
75 const char *p;
76 3714 ▶ if (r->err || r->n - r->i < n) { r->err = 1; return NULL; }
Branch: 2 branches
BranchTrueFalseTotal
if (r->err || r->n - r->i < n) { r->err = 1; return NULL; } 3714 0 3714
if (r->err || r->n - r->i < n) { r->err = 1; return NULL; } 0 3714 3714
77 3714 p = r->b + r->i; r->i += n;
78 3714 return p;
79 }
80  
81 /* ---- metrics --------------------------------------------------------------
82 *
83 * [count] ( [series][t_first][t_last][count][bits][flags][stream bytes] )*
84 */
85 2 static char *po_metric_serialise(const po_chunk_hdr *h, const char *const *bits,
86 const po_u64 *series, uint32_t n, size_t *out) {
87 po_buf b;
88 uint32_t i;
89 2 ▶ if (!po_buf_init(&b)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_buf_init(&b)) return NULL; 0 2 2
90 2 po_buf_u32(&b, n);
91 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
92 4 size_t bytes = (h[i].bits + 7) / 8;
93 4 po_buf_u64(&b, series[i]);
94 4 po_buf_u64(&b, h[i].t_first);
95 4 po_buf_u64(&b, h[i].t_last);
96 4 po_buf_u32(&b, h[i].count);
97 4 po_buf_u32(&b, h[i].bits);
98 4 po_buf_u32(&b, (uint32_t)h[i].flags);
99 4 po_buf_u32(&b, (uint32_t)bytes);
100 4 po_buf_put(&b, bits[i], bytes);
101 }
102 2 ▶ if (b.err) { po_buf_free(&b); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (b.err) { po_buf_free(&b); return NULL; } 0 2 2
103 2 *out = b.n;
104 2 return b.b;
105 }
106  
107 typedef struct {
108 po_u64 series;
109 po_chunk_hdr h;
110 const char *bits;
111 } po_mchunk_ref;
112  
113 /* Returns the chunk count, or -1. `into` must hold at least that many. */
114 619 static int po_metric_open(const char *p, size_t len, po_mchunk_ref *into,
115 uint32_t max) {
116 po_rd r;
117 uint32_t n, i;
118 619 po_rd_init(&r, p, len);
119 619 n = po_rd_u32(&r);
120 619 ▶ if (r.err || n > max) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (r.err || n > max) return -1; 619 0 619
if (r.err || n > max) return -1; 0 619 619
121 1857 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1238 619 1857
122 uint32_t bytes;
123 1238 into[i].series = po_rd_u64(&r);
124 1238 into[i].h.t_first = po_rd_u64(&r);
125 1238 into[i].h.t_last = po_rd_u64(&r);
126 1238 into[i].h.count = po_rd_u32(&r);
127 1238 into[i].h.bits = po_rd_u32(&r);
128 1238 into[i].h.flags = (uint16_t)po_rd_u32(&r);
129 1238 bytes = po_rd_u32(&r);
130 /* The declared bit count and the byte count must agree, or a reader
131 * walks off the end of a bit stream that claims to be longer than it
132 * is. Checked here rather than trusted. */
133 1238 ▶ if (r.err || bytes != (into[i].h.bits + 7) / 8) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (r.err || bytes != (into[i].h.bits + 7) / 8) return -1; 1238 0 1238
if (r.err || bytes != (into[i].h.bits + 7) / 8) return -1; 0 1238 1238
134 1238 into[i].bits = po_rd_bytes(&r, bytes);
135 1238 ▶ if (r.err) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r.err) return -1; 0 1238 1238
136 }
137 619 return (int)n;
138 }
139  
140 /* ---- logs -----------------------------------------------------------------
141 *
142 * [count] ( [hdr fields][bloom bytes][compressed bytes] )*
143 */
144 2 static char *po_log_serialise(const po_block_hdr *h, const char *const *comp,
145 const unsigned char *const *bloom,
146 uint32_t n, size_t *out) {
147 po_buf b;
148 uint32_t i;
149 2 ▶ if (!po_buf_init(&b)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_buf_init(&b)) return NULL; 0 2 2
150 2 po_buf_u32(&b, n);
151 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
152 4 uint32_t bloom_bytes = (h[i].bloom_bits + 7) / 8;
153 4 po_buf_u64(&b, h[i].stream);
154 4 po_buf_u64(&b, h[i].t_min);
155 4 po_buf_u64(&b, h[i].t_max);
156 4 po_buf_u32(&b, h[i].lines);
157 4 po_buf_u32(&b, h[i].raw_len);
158 4 po_buf_u32(&b, h[i].comp_len);
159 4 po_buf_u32(&b, h[i].crc);
160 4 po_buf_u32(&b, h[i].bloom_bits);
161 4 po_buf_u32(&b, (uint32_t)h[i].flags);
162 4 po_buf_put(&b, bloom[i], bloom_bytes);
163 4 po_buf_put(&b, comp[i], h[i].comp_len);
164 }
165 2 ▶ if (b.err) { po_buf_free(&b); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (b.err) { po_buf_free(&b); return NULL; } 0 2 2
166 2 *out = b.n;
167 2 return b.b;
168 }
169  
170 typedef struct {
171 po_block_hdr h;
172 const unsigned char *bloom;
173 const char *comp;
174 } po_lblock_ref;
175  
176 619 static int po_log_open(const char *p, size_t len, po_lblock_ref *into,
177 uint32_t max) {
178 po_rd r;
179 uint32_t n, i;
180 619 po_rd_init(&r, p, len);
181 619 n = po_rd_u32(&r);
182 619 ▶ if (r.err || n > max) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (r.err || n > max) return -1; 619 0 619
if (r.err || n > max) return -1; 0 619 619
183 1857 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1238 619 1857
184 uint32_t bloom_bytes;
185 1238 into[i].h.stream = po_rd_u64(&r);
186 1238 into[i].h.t_min = po_rd_u64(&r);
187 1238 into[i].h.t_max = po_rd_u64(&r);
188 1238 into[i].h.lines = po_rd_u32(&r);
189 1238 into[i].h.raw_len = po_rd_u32(&r);
190 1238 into[i].h.comp_len = po_rd_u32(&r);
191 1238 into[i].h.crc = po_rd_u32(&r);
192 1238 into[i].h.bloom_bits = po_rd_u32(&r);
193 1238 into[i].h.flags = (uint16_t)po_rd_u32(&r);
194 1238 ▶ if (r.err) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r.err) return -1; 0 1238 1238
195 1238 bloom_bytes = (into[i].h.bloom_bits + 7) / 8;
196 1238 into[i].bloom = (const unsigned char *)po_rd_bytes(&r, bloom_bytes);
197 1238 into[i].comp = po_rd_bytes(&r, into[i].h.comp_len);
198 1238 ▶ if (r.err) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (r.err) return -1; 0 1238 1238
199 }
200 619 return (int)n;
201 }
202  
203 /* ---- spans ----------------------------------------------------------------
204 *
205 * The span array is written verbatim: it is already a flat array of fixed
206 * 64-byte structs sorted by (trace, start), so serialising it is a memcpy and
207 * reading it back is a cast. That is the payoff for making po_span a plain
208 * struct with no pointers in it.
209 */
210 3 static char *po_span_serialise(const po_span *s, uint32_t n, size_t *out) {
211 3 size_t bytes = (size_t)n * sizeof(po_span);
212 3 ▶ char *b = (char *)malloc(bytes ? bytes : 1);
Branch: 1 branch
BranchTrueFalseTotal
char *b = (char *)malloc(bytes ? bytes : 1); 3 0 3
213 3 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 3 3
214 3 ▶ if (bytes) memcpy(b, s, bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (bytes) memcpy(b, s, bytes); 3 0 3
215 3 *out = bytes;
216 3 return b;
217 }
218  
219 2 static const po_span *po_span_open(const char *p, size_t len, uint32_t *n) {
220 2 ▶ if (len % sizeof(po_span)) { *n = 0; return NULL; } /* not a span array */
Branch: 1 branch
BranchTrueFalseTotal
if (len % sizeof(po_span)) { *n = 0; return NULL; } /* not a span array */ 0 2 2
221 2 *n = (uint32_t)(len / sizeof(po_span));
222 2 return (const po_span *)p;
223 }
224  
225 /* ---- trace summaries ------------------------------------------------------ */
226  
227 3 static char *po_tsum_serialise(const po_tsummary *t, uint32_t n, size_t *out) {
228 3 size_t bytes = (size_t)n * sizeof(po_tsummary);
229 3 ▶ char *b = (char *)malloc(bytes ? bytes : 1);
Branch: 1 branch
BranchTrueFalseTotal
char *b = (char *)malloc(bytes ? bytes : 1); 3 0 3
230 3 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 3 3
231 3 ▶ if (bytes) memcpy(b, t, bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (bytes) memcpy(b, t, bytes); 3 0 3
232 3 *out = bytes;
233 3 return b;
234 }
235  
236 620 static const po_tsummary *po_tsum_open(const char *p, size_t len, uint32_t *n) {
237 620 ▶ if (len % sizeof(po_tsummary)) { *n = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (len % sizeof(po_tsummary)) { *n = 0; return NULL; } 0 620 620
238 620 *n = (uint32_t)(len / sizeof(po_tsummary));
239 620 return (const po_tsummary *)p;
240 }
241  
242 #endif /* PO_SEGIO_H */
« include/punk_observe/po_manifest.h ↑ Summary xs/svg.xs »