Fix: use of file pointer after fclose()
[babeltrace.git] / plugins / ctf / common / metadata / decoder.c
1 /*
2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 */
14
15 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-DECODER"
16 #include "logging.h"
17
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <inttypes.h>
23 #include <assert.h>
24 #include <babeltrace/compat/uuid-internal.h>
25 #include <babeltrace/compat/memstream-internal.h>
26 #include <babeltrace/ctf-ir/trace.h>
27 #include <glib.h>
28 #include <string.h>
29
30 #include "ast.h"
31 #include "decoder.h"
32 #include "scanner.h"
33
34 #define TSDL_MAGIC 0x75d11d57
35
36 extern
37 int yydebug;
38
39 struct ctf_metadata_decoder {
40 struct ctf_visitor_generate_ir *visitor;
41 uint8_t uuid[16];
42 bool is_uuid_set;
43 int bo;
44 struct ctf_metadata_decoder_config config;
45 };
46
47 struct packet_header {
48 uint32_t magic;
49 uint8_t uuid[16];
50 uint32_t checksum;
51 uint32_t content_size;
52 uint32_t packet_size;
53 uint8_t compression_scheme;
54 uint8_t encryption_scheme;
55 uint8_t checksum_scheme;
56 uint8_t major;
57 uint8_t minor;
58 } __attribute__((__packed__));
59
60 BT_HIDDEN
61 bool ctf_metadata_decoder_is_packetized(FILE *fp, int *byte_order)
62 {
63 uint32_t magic;
64 size_t len;
65 int ret = 0;
66
67 len = fread(&magic, sizeof(magic), 1, fp);
68 if (len != 1) {
69 BT_LOGD_STR("Cannot reade first metadata packet header: assuming the stream is not packetized.");
70 goto end;
71 }
72
73 if (byte_order) {
74 if (magic == TSDL_MAGIC) {
75 ret = 1;
76 *byte_order = BYTE_ORDER;
77 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
78 ret = 1;
79 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
80 LITTLE_ENDIAN : BIG_ENDIAN;
81 }
82 }
83
84 end:
85 rewind(fp);
86
87 return ret;
88 }
89
90 static
91 bool is_version_valid(unsigned int major, unsigned int minor)
92 {
93 return major == 1 && minor == 8;
94 }
95
96 static
97 int decode_packet(struct ctf_metadata_decoder *mdec, FILE *in_fp, FILE *out_fp,
98 int byte_order)
99 {
100 struct packet_header header;
101 size_t readlen, writelen, toread;
102 uint8_t buf[512 + 1]; /* + 1 for debug-mode \0 */
103 int ret = 0;
104 const long offset = ftell(in_fp);
105
106 BT_LOGV("Decoding metadata packet: mdec-addr=%p, offset=%ld",
107 mdec, offset);
108 readlen = fread(&header, sizeof(header), 1, in_fp);
109 if (feof(in_fp) != 0) {
110 BT_LOGV("Reached end of file: offset=%ld", ftell(in_fp));
111 goto end;
112 }
113 if (readlen < 1) {
114 BT_LOGV("Cannot decode metadata packet: offset=%ld", offset);
115 goto error;
116 }
117
118 if (byte_order != BYTE_ORDER) {
119 header.magic = GUINT32_SWAP_LE_BE(header.magic);
120 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
121 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
122 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
123 }
124
125 if (header.compression_scheme) {
126 BT_LOGE("Metadata packet compression is not supported as of this version: "
127 "compression-scheme=%u, offset=%ld",
128 (unsigned int) header.compression_scheme, offset);
129 goto error;
130 }
131
132 if (header.encryption_scheme) {
133 BT_LOGE("Metadata packet encryption is not supported as of this version: "
134 "encryption-scheme=%u, offset=%ld",
135 (unsigned int) header.encryption_scheme, offset);
136 goto error;
137 }
138
139 if (header.checksum || header.checksum_scheme) {
140 BT_LOGE("Metadata packet checksum verification is not supported as of this version: "
141 "checksum-scheme=%u, checksum=%x, offset=%ld",
142 (unsigned int) header.checksum_scheme, header.checksum,
143 offset);
144 goto error;
145 }
146
147 if (!is_version_valid(header.major, header.minor)) {
148 BT_LOGE("Invalid metadata packet version: "
149 "version=%u.%u, offset=%ld",
150 header.major, header.minor, offset);
151 goto error;
152 }
153
154 /* Set expected trace UUID if not set; otherwise validate it */
155 if (mdec) {
156 if (!mdec->is_uuid_set) {
157 memcpy(mdec->uuid, header.uuid, sizeof(header.uuid));
158 mdec->is_uuid_set = true;
159 } else if (bt_uuid_compare(header.uuid, mdec->uuid)) {
160 BT_LOGE("Metadata UUID mismatch between packets of the same stream: "
161 "packet-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
162 "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
163 "offset=%ld",
164 (unsigned int) header.uuid[0],
165 (unsigned int) header.uuid[1],
166 (unsigned int) header.uuid[2],
167 (unsigned int) header.uuid[3],
168 (unsigned int) header.uuid[4],
169 (unsigned int) header.uuid[5],
170 (unsigned int) header.uuid[6],
171 (unsigned int) header.uuid[7],
172 (unsigned int) header.uuid[8],
173 (unsigned int) header.uuid[9],
174 (unsigned int) header.uuid[10],
175 (unsigned int) header.uuid[11],
176 (unsigned int) header.uuid[12],
177 (unsigned int) header.uuid[13],
178 (unsigned int) header.uuid[14],
179 (unsigned int) header.uuid[15],
180 (unsigned int) mdec->uuid[0],
181 (unsigned int) mdec->uuid[1],
182 (unsigned int) mdec->uuid[2],
183 (unsigned int) mdec->uuid[3],
184 (unsigned int) mdec->uuid[4],
185 (unsigned int) mdec->uuid[5],
186 (unsigned int) mdec->uuid[6],
187 (unsigned int) mdec->uuid[7],
188 (unsigned int) mdec->uuid[8],
189 (unsigned int) mdec->uuid[9],
190 (unsigned int) mdec->uuid[10],
191 (unsigned int) mdec->uuid[11],
192 (unsigned int) mdec->uuid[12],
193 (unsigned int) mdec->uuid[13],
194 (unsigned int) mdec->uuid[14],
195 (unsigned int) mdec->uuid[15],
196 offset);
197 goto error;
198 }
199 }
200
201 if ((header.content_size / CHAR_BIT) < sizeof(header)) {
202 BT_LOGE("Bad metadata packet content size: content-size=%u, "
203 "offset=%ld", header.content_size, offset);
204 goto error;
205 }
206
207 toread = header.content_size / CHAR_BIT - sizeof(header);
208
209 for (;;) {
210 readlen = fread(buf, sizeof(uint8_t),
211 MIN(sizeof(buf) - 1, toread), in_fp);
212 if (ferror(in_fp)) {
213 BT_LOGE("Cannot read metadata packet buffer: "
214 "offset=%ld, read-size=%u",
215 ftell(in_fp), (unsigned int) readlen);
216 goto error;
217 }
218
219 writelen = fwrite(buf, sizeof(uint8_t), readlen, out_fp);
220 if (writelen < readlen || ferror(out_fp)) {
221 BT_LOGE("Cannot write decoded metadata text to buffer: "
222 "read-offset=%ld, write-size=%u",
223 ftell(in_fp), (unsigned int) readlen);
224 goto error;
225 }
226
227 toread -= readlen;
228 if (toread == 0) {
229 int fseek_ret;
230
231 /* Read leftover padding */
232 toread = (header.packet_size - header.content_size) /
233 CHAR_BIT;
234 fseek_ret = fseek(in_fp, toread, SEEK_CUR);
235 if (fseek_ret < 0) {
236 BT_LOGW_STR("Missing padding at the end of the metadata stream.");
237 }
238 break;
239 }
240 }
241
242 goto end;
243
244 error:
245 ret = -1;
246
247 end:
248 return ret;
249 }
250
251 static
252 int ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec(
253 struct ctf_metadata_decoder *mdec, FILE *fp,
254 char **buf, int byte_order)
255 {
256 FILE *out_fp;
257 size_t size;
258 int ret = 0;
259 int tret;
260 size_t packet_index = 0;
261
262 out_fp = bt_open_memstream(buf, &size);
263 if (out_fp == NULL) {
264 BT_LOGE("Cannot open memory stream: %s: mdec-addr=%p",
265 strerror(errno), mdec);
266 goto error;
267 }
268
269 for (;;) {
270 if (feof(fp) != 0) {
271 break;
272 }
273
274 tret = decode_packet(mdec, fp, out_fp, byte_order);
275 if (tret) {
276 BT_LOGE("Cannot decode packet: index=%zu, mdec-addr=%p",
277 packet_index, mdec);
278 goto error;
279 }
280
281 packet_index++;
282 }
283
284 /* Make sure the whole string ends with a null character */
285 tret = fputc('\0', out_fp);
286 if (tret == EOF) {
287 BT_LOGE("Cannot append '\\0' to the decoded metadata buffer: "
288 "mdec-addr=%p", mdec);
289 goto error;
290 }
291
292 /* Close stream, which also flushes the buffer */
293 ret = bt_close_memstream(buf, &size, out_fp);
294 /*
295 * See fclose(3). Further access to out_fp after both success
296 * and error, even through another bt_close_memstream(), results
297 * in undefined behavior. Nullify out_fp to ensure we don't
298 * fclose it twice on error.
299 */
300 out_fp = NULL;
301 if (ret < 0) {
302 BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p",
303 strerror(errno), mdec);
304 goto error;
305 }
306
307 goto end;
308
309 error:
310 ret = -1;
311
312 if (out_fp) {
313 if (bt_close_memstream(buf, &size, out_fp)) {
314 BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p",
315 strerror(errno), mdec);
316 }
317 }
318
319 if (*buf) {
320 free(*buf);
321 *buf = NULL;
322 }
323
324 end:
325 return ret;
326 }
327
328 BT_HIDDEN
329 int ctf_metadata_decoder_packetized_file_stream_to_buf(
330 FILE *fp, char **buf, int byte_order)
331 {
332 return ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec(
333 NULL, fp, buf, byte_order);
334 }
335
336 BT_HIDDEN
337 struct ctf_metadata_decoder *ctf_metadata_decoder_create(
338 const struct ctf_metadata_decoder_config *config,
339 const char *name)
340 {
341 struct ctf_metadata_decoder *mdec =
342 g_new0(struct ctf_metadata_decoder, 1);
343 struct ctf_metadata_decoder_config default_config = {
344 .clock_class_offset_s = 0,
345 .clock_class_offset_ns = 0,
346 };
347
348 if (!config) {
349 config = &default_config;
350 }
351
352 BT_LOGD("Creating CTF metadata decoder: "
353 "clock-class-offset-s=%" PRId64 ", "
354 "clock-class-offset-ns=%" PRId64 ", name=\"%s\"",
355 config->clock_class_offset_s, config->clock_class_offset_ns,
356 name);
357
358 if (!mdec) {
359 BT_LOGE_STR("Failed to allocate one CTF metadata decoder.");
360 goto end;
361 }
362
363 mdec->config = *config;
364 mdec->visitor = ctf_visitor_generate_ir_create(config, name);
365 if (!mdec->visitor) {
366 BT_LOGE("Failed to create a CTF IR metadata AST visitor: "
367 "mdec-addr=%p", mdec);
368 ctf_metadata_decoder_destroy(mdec);
369 mdec = NULL;
370 goto end;
371 }
372
373 BT_LOGD("Creating CTF metadata decoder: "
374 "clock-class-offset-s=%" PRId64 ", "
375 "clock-class-offset-ns=%" PRId64 ", "
376 "name=\"%s\", addr=%p",
377 config->clock_class_offset_s, config->clock_class_offset_ns,
378 name, mdec);
379
380 end:
381 return mdec;
382 }
383
384 BT_HIDDEN
385 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *mdec)
386 {
387 if (!mdec) {
388 return;
389 }
390
391 BT_LOGD("Destroying CTF metadata decoder: addr=%p", mdec);
392 ctf_visitor_generate_ir_destroy(mdec->visitor);
393 g_free(mdec);
394 }
395
396 BT_HIDDEN
397 enum ctf_metadata_decoder_status ctf_metadata_decoder_decode(
398 struct ctf_metadata_decoder *mdec, FILE *fp)
399 {
400 enum ctf_metadata_decoder_status status =
401 CTF_METADATA_DECODER_STATUS_OK;
402 int ret;
403 struct ctf_scanner *scanner = NULL;
404 char *buf = NULL;
405 bool close_fp = false;
406
407 assert(mdec);
408
409 if (ctf_metadata_decoder_is_packetized(fp, &mdec->bo)) {
410 BT_LOGD("Metadata stream is packetized: mdec-addr=%p", mdec);
411 ret = ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec(
412 mdec, fp, &buf, mdec->bo);
413 if (ret) {
414 BT_LOGE("Cannot decode packetized metadata packets to metadata text: "
415 "mdec-addr=%p, ret=%d", mdec, ret);
416 status = CTF_METADATA_DECODER_STATUS_ERROR;
417 goto end;
418 }
419
420 if (strlen(buf) == 0) {
421 /* An empty metadata packet is OK. */
422 goto end;
423 }
424
425 /* Convert the real file pointer to a memory file pointer */
426 fp = bt_fmemopen(buf, strlen(buf), "rb");
427 close_fp = true;
428 if (!fp) {
429 BT_LOGE("Cannot memory-open metadata buffer: %s: "
430 "mdec-addr=%p", strerror(errno), mdec);
431 status = CTF_METADATA_DECODER_STATUS_ERROR;
432 goto end;
433 }
434 } else {
435 unsigned int major, minor;
436 ssize_t nr_items;
437 const long init_pos = ftell(fp);
438
439 BT_LOGD("Metadata stream is plain text: mdec-addr=%p", mdec);
440
441 if (init_pos < 0) {
442 BT_LOGE_ERRNO("Failed to get current file position", ".");
443 status = CTF_METADATA_DECODER_STATUS_ERROR;
444 goto end;
445 }
446
447 /* Check text-only metadata header and version */
448 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
449 if (nr_items < 2) {
450 BT_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: "
451 "mdec-addr=%p", mdec);
452 }
453
454 BT_LOGD("Found metadata stream version in signature: version=%u.%u", major, minor);
455
456 if (!is_version_valid(major, minor)) {
457 BT_LOGE("Invalid metadata version found in plain text signature: "
458 "version=%u.%u, mdec-addr=%p", major, minor,
459 mdec);
460 status = CTF_METADATA_DECODER_STATUS_INVAL_VERSION;
461 goto end;
462 }
463
464 if (fseek(fp, init_pos, SEEK_SET)) {
465 BT_LOGE("Cannot seek metadata file stream to initial position: %s: "
466 "mdec-addr=%p", strerror(errno), mdec);
467 status = CTF_METADATA_DECODER_STATUS_ERROR;
468 goto end;
469 }
470 }
471
472 if (BT_LOG_ON_VERBOSE) {
473 yydebug = 1;
474 }
475
476 /* Allocate a scanner and append the metadata text content */
477 scanner = ctf_scanner_alloc();
478 if (!scanner) {
479 BT_LOGE("Cannot allocate a metadata lexical scanner: "
480 "mdec-addr=%p", mdec);
481 status = CTF_METADATA_DECODER_STATUS_ERROR;
482 goto end;
483 }
484
485 assert(fp);
486 ret = ctf_scanner_append_ast(scanner, fp);
487 if (ret) {
488 BT_LOGE("Cannot create the metadata AST out of the metadata text: "
489 "mdec-addr=%p", mdec);
490 status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
491 goto end;
492 }
493
494 ret = ctf_visitor_semantic_check(0, &scanner->ast->root);
495 if (ret) {
496 BT_LOGE("Validation of the metadata semantics failed: "
497 "mdec-addr=%p", mdec);
498 status = CTF_METADATA_DECODER_STATUS_ERROR;
499 goto end;
500 }
501
502 ret = ctf_visitor_generate_ir_visit_node(mdec->visitor,
503 &scanner->ast->root);
504 switch (ret) {
505 case 0:
506 /* Success */
507 break;
508 case -EINCOMPLETE:
509 BT_LOGD("While visiting metadata AST: incomplete data: "
510 "mdec-addr=%p", mdec);
511 status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
512 goto end;
513 default:
514 BT_LOGE("Failed to visit AST node to create CTF IR objects: "
515 "mdec-addr=%p, ret=%d", mdec, ret);
516 status = CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR;
517 goto end;
518 }
519
520 end:
521 if (scanner) {
522 ctf_scanner_free(scanner);
523 }
524
525 yydebug = 0;
526
527 if (fp && close_fp) {
528 if (fclose(fp)) {
529 BT_LOGE("Cannot close metadata file stream: "
530 "mdec-addr=%p", mdec);
531 }
532 }
533
534 if (buf) {
535 free(buf);
536 }
537
538 return status;
539 }
540
541 BT_HIDDEN
542 struct bt_ctf_trace *ctf_metadata_decoder_get_trace(
543 struct ctf_metadata_decoder *mdec)
544 {
545 return ctf_visitor_generate_ir_get_trace(mdec->visitor);
546 }
This page took 0.048186 seconds and 4 git commands to generate.