Commit | Line | Data |
---|---|---|
a248254b PP |
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_COMP_LOG_SELF_COMP self_comp | |
16 | #define BT_LOG_OUTPUT_LEVEL log_level | |
17 | #define BT_LOG_TAG "PLUGIN/CTF/META/DECODER-DECODE-PACKET" | |
18 | #include "plugins/comp-logging.h" | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <stdbool.h> | |
22 | #include <stdint.h> | |
23 | #include <stdlib.h> | |
24 | #include <inttypes.h> | |
25 | #include "common/assert.h" | |
d126826c | 26 | #include "common/uuid.h" |
a248254b PP |
27 | #include "compat/memstream.h" |
28 | #include <babeltrace2/babeltrace.h> | |
29 | #include <glib.h> | |
30 | #include <string.h> | |
31 | ||
32 | #include "ast.h" | |
33 | #include "decoder.h" | |
34 | #include "scanner.h" | |
35 | #include "logging.h" | |
36 | ||
37 | #define TSDL_MAGIC 0x75d11d57 | |
38 | ||
39 | extern | |
40 | int yydebug; | |
41 | ||
42 | struct ctf_metadata_decoder { | |
43 | struct ctf_visitor_generate_ir *visitor; | |
d126826c | 44 | bt_uuid_t uuid; |
a248254b PP |
45 | bool is_uuid_set; |
46 | int bo; | |
47 | struct ctf_metadata_decoder_config config; | |
48 | }; | |
49 | ||
50 | struct packet_header { | |
51 | uint32_t magic; | |
d126826c | 52 | bt_uuid_t uuid; |
a248254b PP |
53 | uint32_t checksum; |
54 | uint32_t content_size; | |
55 | uint32_t packet_size; | |
56 | uint8_t compression_scheme; | |
57 | uint8_t encryption_scheme; | |
58 | uint8_t checksum_scheme; | |
59 | uint8_t major; | |
60 | uint8_t minor; | |
61 | } __attribute__((__packed__)); | |
62 | ||
63 | static | |
64 | int decode_packet(FILE *in_fp, FILE *out_fp, | |
65 | int byte_order, bool *is_uuid_set, uint8_t *uuid, | |
66 | bt_logging_level log_level, bt_self_component *self_comp) | |
67 | { | |
68 | struct packet_header header; | |
69 | size_t readlen, writelen, toread; | |
70 | uint8_t buf[512 + 1]; /* + 1 for debug-mode \0 */ | |
71 | int ret = 0; | |
72 | const long offset = ftell(in_fp); | |
73 | ||
74 | if (offset < 0) { | |
75 | BT_COMP_LOGE_ERRNO("Failed to get current metadata file position", | |
76 | "."); | |
77 | goto error; | |
78 | } | |
79 | BT_COMP_LOGD("Decoding metadata packet: offset=%ld", offset); | |
80 | readlen = fread(&header, sizeof(header), 1, in_fp); | |
81 | if (feof(in_fp) != 0) { | |
82 | BT_COMP_LOGI("Reached end of file: offset=%ld", ftell(in_fp)); | |
83 | goto end; | |
84 | } | |
85 | if (readlen < 1) { | |
86 | BT_COMP_LOGE("Cannot decode metadata packet: offset=%ld", offset); | |
87 | goto error; | |
88 | } | |
89 | ||
90 | if (byte_order != BYTE_ORDER) { | |
91 | header.magic = GUINT32_SWAP_LE_BE(header.magic); | |
92 | header.checksum = GUINT32_SWAP_LE_BE(header.checksum); | |
93 | header.content_size = GUINT32_SWAP_LE_BE(header.content_size); | |
94 | header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size); | |
95 | } | |
96 | ||
97 | if (header.compression_scheme) { | |
98 | BT_COMP_LOGE("Metadata packet compression is not supported as of this version: " | |
99 | "compression-scheme=%u, offset=%ld", | |
100 | (unsigned int) header.compression_scheme, offset); | |
101 | goto error; | |
102 | } | |
103 | ||
104 | if (header.encryption_scheme) { | |
105 | BT_COMP_LOGE("Metadata packet encryption is not supported as of this version: " | |
106 | "encryption-scheme=%u, offset=%ld", | |
107 | (unsigned int) header.encryption_scheme, offset); | |
108 | goto error; | |
109 | } | |
110 | ||
111 | if (header.checksum || header.checksum_scheme) { | |
112 | BT_COMP_LOGE("Metadata packet checksum verification is not supported as of this version: " | |
113 | "checksum-scheme=%u, checksum=%x, offset=%ld", | |
114 | (unsigned int) header.checksum_scheme, header.checksum, | |
115 | offset); | |
116 | goto error; | |
117 | } | |
118 | ||
119 | if (!ctf_metadata_decoder_is_packet_version_valid(header.major, | |
120 | header.minor)) { | |
121 | BT_COMP_LOGE("Invalid metadata packet version: " | |
122 | "version=%u.%u, offset=%ld", | |
123 | header.major, header.minor, offset); | |
124 | goto error; | |
125 | } | |
126 | ||
127 | /* Set expected trace UUID if not set; otherwise validate it */ | |
128 | if (is_uuid_set) { | |
129 | if (!*is_uuid_set) { | |
d126826c | 130 | bt_uuid_copy(uuid, header.uuid); |
a248254b PP |
131 | *is_uuid_set = true; |
132 | } else if (bt_uuid_compare(header.uuid, uuid)) { | |
133 | BT_COMP_LOGE("Metadata UUID mismatch between packets of the same stream: " | |
d126826c MJ |
134 | "packet-uuid=\"" BT_UUID_FMT "\", " |
135 | "expected-uuid=\"" BT_UUID_FMT "\", " | |
a248254b | 136 | "offset=%ld", |
d126826c MJ |
137 | BT_UUID_FMT_VALUES(header.uuid), |
138 | BT_UUID_FMT_VALUES(uuid), | |
a248254b PP |
139 | offset); |
140 | goto error; | |
141 | } | |
142 | } | |
143 | ||
144 | if ((header.content_size / CHAR_BIT) < sizeof(header)) { | |
145 | BT_COMP_LOGE("Bad metadata packet content size: content-size=%u, " | |
146 | "offset=%ld", header.content_size, offset); | |
147 | goto error; | |
148 | } | |
149 | ||
150 | toread = header.content_size / CHAR_BIT - sizeof(header); | |
151 | ||
152 | for (;;) { | |
153 | size_t loop_read; | |
154 | ||
155 | loop_read = MIN(sizeof(buf) - 1, toread); | |
156 | readlen = fread(buf, sizeof(uint8_t), loop_read, in_fp); | |
157 | if (ferror(in_fp)) { | |
158 | BT_COMP_LOGE("Cannot read metadata packet buffer: " | |
159 | "offset=%ld, read-size=%zu", | |
160 | ftell(in_fp), loop_read); | |
161 | goto error; | |
162 | } | |
163 | if (readlen > loop_read) { | |
164 | BT_COMP_LOGE("fread returned more byte than expected: " | |
165 | "read-size-asked=%zu, read-size-returned=%zu", | |
166 | loop_read, readlen); | |
167 | goto error; | |
168 | } | |
169 | ||
170 | writelen = fwrite(buf, sizeof(uint8_t), readlen, out_fp); | |
171 | if (writelen < readlen || ferror(out_fp)) { | |
172 | BT_COMP_LOGE("Cannot write decoded metadata text to buffer: " | |
173 | "read-offset=%ld, write-size=%zu", | |
174 | ftell(in_fp), readlen); | |
175 | goto error; | |
176 | } | |
177 | ||
178 | toread -= readlen; | |
179 | if (toread == 0) { | |
180 | int fseek_ret; | |
181 | ||
182 | /* Read leftover padding */ | |
183 | toread = (header.packet_size - header.content_size) / | |
184 | CHAR_BIT; | |
185 | fseek_ret = fseek(in_fp, toread, SEEK_CUR); | |
186 | if (fseek_ret < 0) { | |
187 | BT_COMP_LOGW_STR("Missing padding at the end of the metadata stream."); | |
188 | } | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | goto end; | |
194 | ||
195 | error: | |
196 | ret = -1; | |
197 | ||
198 | end: | |
199 | return ret; | |
200 | } | |
201 | ||
202 | BT_HIDDEN | |
203 | int ctf_metadata_decoder_packetized_file_stream_to_buf(FILE *fp, | |
204 | char **buf, int byte_order, bool *is_uuid_set, | |
205 | uint8_t *uuid, bt_logging_level log_level, | |
206 | bt_self_component *self_comp) | |
207 | { | |
208 | FILE *out_fp; | |
209 | size_t size; | |
210 | int ret = 0; | |
211 | int tret; | |
212 | size_t packet_index = 0; | |
213 | ||
214 | out_fp = bt_open_memstream(buf, &size); | |
8e01f2d9 | 215 | if (!out_fp) { |
a248254b PP |
216 | BT_COMP_LOGE("Cannot open memory stream: %s.", |
217 | strerror(errno)); | |
218 | goto error; | |
219 | } | |
220 | ||
221 | for (;;) { | |
222 | if (feof(fp) != 0) { | |
223 | break; | |
224 | } | |
225 | ||
226 | tret = decode_packet(fp, out_fp, byte_order, is_uuid_set, | |
227 | uuid, log_level, self_comp); | |
228 | if (tret) { | |
229 | BT_COMP_LOGE("Cannot decode packet: index=%zu", | |
230 | packet_index); | |
231 | goto error; | |
232 | } | |
233 | ||
234 | packet_index++; | |
235 | } | |
236 | ||
237 | /* Make sure the whole string ends with a null character */ | |
238 | tret = fputc('\0', out_fp); | |
239 | if (tret == EOF) { | |
240 | BT_COMP_LOGE_STR( | |
241 | "Cannot append '\\0' to the decoded metadata buffer."); | |
242 | goto error; | |
243 | } | |
244 | ||
245 | /* Close stream, which also flushes the buffer */ | |
246 | ret = bt_close_memstream(buf, &size, out_fp); | |
247 | /* | |
248 | * See fclose(3). Further access to out_fp after both success | |
249 | * and error, even through another bt_close_memstream(), results | |
250 | * in undefined behavior. Nullify out_fp to ensure we don't | |
251 | * fclose it twice on error. | |
252 | */ | |
253 | out_fp = NULL; | |
254 | if (ret < 0) { | |
255 | BT_COMP_LOGE_ERRNO("Cannot close memory stream", "."); | |
256 | goto error; | |
257 | } | |
258 | ||
259 | goto end; | |
260 | ||
261 | error: | |
262 | ret = -1; | |
263 | ||
264 | if (out_fp) { | |
265 | if (bt_close_memstream(buf, &size, out_fp)) { | |
266 | BT_COMP_LOGE_ERRNO("Cannot close memory stream", "."); | |
267 | } | |
268 | } | |
269 | ||
270 | if (*buf) { | |
271 | free(*buf); | |
272 | *buf = NULL; | |
273 | } | |
274 | ||
275 | end: | |
276 | return ret; | |
277 | } |