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