CTF writer: stream: handle automatic fields more securely
[babeltrace.git] / lib / ctf-writer / serialize.c
1 /*
2 * serialize.c
3 *
4 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
7 *
8 * The original author of the serialization functions for Babeltrace 1
9 * is Mathieu Desnoyers. Philippe Proulx modified the functions in 2017
10 * to use Babeltrace 2 objects.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * SOFTWARE.
29 */
30
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <babeltrace/ctf-ir/field-types.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/ctf-ir/fields.h>
36 #include <babeltrace/ctf-ir/fields-internal.h>
37 #include <babeltrace/ctf-writer/serialize-internal.h>
38 #include <babeltrace/align-internal.h>
39 #include <babeltrace/mmap-align-internal.h>
40 #include <babeltrace/endian-internal.h>
41 #include <babeltrace/bitfield-internal.h>
42 #include <babeltrace/compat/fcntl-internal.h>
43 #include <babeltrace/types.h>
44 #include <glib.h>
45
46 #if (FLT_RADIX != 2)
47 # error "Unsupported floating point radix"
48 #endif
49
50 union intval {
51 int64_t signd;
52 uint64_t unsignd;
53 };
54
55 /*
56 * The aligned read/write functions are expected to be faster than the
57 * bitfield variants. They will be enabled eventually as an
58 * optimisation.
59 */
60 static
61 int aligned_integer_write(struct bt_ctf_stream_pos *pos,
62 union intval value, unsigned int alignment, unsigned int size,
63 bt_bool is_signed, enum bt_ctf_byte_order byte_order)
64 {
65 bt_bool rbo = (byte_order != BT_CTF_MY_BYTE_ORDER); /* reverse byte order */
66
67 if (!bt_ctf_stream_pos_align(pos, alignment))
68 return -EFAULT;
69
70 if (!bt_ctf_stream_pos_access_ok(pos, size))
71 return -EFAULT;
72
73 assert(!(pos->offset % CHAR_BIT));
74 if (!is_signed) {
75 switch (size) {
76 case 8:
77 {
78 uint8_t v = value.unsignd;
79
80 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
81 break;
82 }
83 case 16:
84 {
85 uint16_t v = value.unsignd;
86
87 if (rbo)
88 v = GUINT16_SWAP_LE_BE(v);
89 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
90 break;
91 }
92 case 32:
93 {
94 uint32_t v = value.unsignd;
95
96 if (rbo)
97 v = GUINT32_SWAP_LE_BE(v);
98 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
99 break;
100 }
101 case 64:
102 {
103 uint64_t v = value.unsignd;
104
105 if (rbo)
106 v = GUINT64_SWAP_LE_BE(v);
107 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
108 break;
109 }
110 default:
111 assert(BT_FALSE);
112 }
113 } else {
114 switch (size) {
115 case 8:
116 {
117 uint8_t v = value.signd;
118
119 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
120 break;
121 }
122 case 16:
123 {
124 int16_t v = value.signd;
125
126 if (rbo)
127 v = GUINT16_SWAP_LE_BE(v);
128 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
129 break;
130 }
131 case 32:
132 {
133 int32_t v = value.signd;
134
135 if (rbo)
136 v = GUINT32_SWAP_LE_BE(v);
137 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
138 break;
139 }
140 case 64:
141 {
142 int64_t v = value.signd;
143
144 if (rbo)
145 v = GUINT64_SWAP_LE_BE(v);
146 memcpy(bt_ctf_stream_pos_get_addr(pos), &v, sizeof(v));
147 break;
148 }
149 default:
150 assert(BT_FALSE);
151 }
152 }
153
154 if (!bt_ctf_stream_pos_move(pos, size))
155 return -EFAULT;
156 return 0;
157 }
158
159 static
160 int integer_write(struct bt_ctf_stream_pos *pos, union intval value,
161 unsigned int alignment, unsigned int size, bt_bool is_signed,
162 enum bt_ctf_byte_order byte_order)
163 {
164 if (!(alignment % CHAR_BIT)
165 && !(size % CHAR_BIT)) {
166 return aligned_integer_write(pos, value, alignment,
167 size, is_signed, byte_order);
168 }
169
170 if (!bt_ctf_stream_pos_align(pos, alignment))
171 return -EFAULT;
172
173 if (!bt_ctf_stream_pos_access_ok(pos, size))
174 return -EFAULT;
175
176 if (!is_signed) {
177 if (byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN)
178 bt_bitfield_write_le(mmap_align_addr(pos->base_mma) +
179 pos->mmap_base_offset, unsigned char,
180 pos->offset, size, value.unsignd);
181 else
182 bt_bitfield_write_be(mmap_align_addr(pos->base_mma) +
183 pos->mmap_base_offset, unsigned char,
184 pos->offset, size, value.unsignd);
185 } else {
186 if (byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN)
187 bt_bitfield_write_le(mmap_align_addr(pos->base_mma) +
188 pos->mmap_base_offset, unsigned char,
189 pos->offset, size, value.signd);
190 else
191 bt_bitfield_write_be(mmap_align_addr(pos->base_mma) +
192 pos->mmap_base_offset, unsigned char,
193 pos->offset, size, value.signd);
194 }
195
196 if (!bt_ctf_stream_pos_move(pos, size))
197 return -EFAULT;
198 return 0;
199 }
200
201 BT_HIDDEN
202 int bt_ctf_field_integer_write(struct bt_ctf_field_integer *int_field,
203 struct bt_ctf_stream_pos *pos,
204 enum bt_ctf_byte_order native_byte_order)
205 {
206 struct bt_ctf_field_type *type = int_field->parent.type;
207 struct bt_ctf_field_type_integer *int_type = (void *) type;
208 enum bt_ctf_byte_order byte_order;
209 union intval value;
210
211 byte_order = int_type->user_byte_order;
212 if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
213 byte_order = native_byte_order;
214 }
215
216 value.signd = int_field->payload.signd;
217 value.unsignd = int_field->payload.unsignd;
218 return integer_write(pos, value, type->alignment,
219 int_type->size, int_type->is_signed,
220 byte_order);
221 }
222
223 BT_HIDDEN
224 int bt_ctf_field_floating_point_write(
225 struct bt_ctf_field_floating_point *flt_field,
226 struct bt_ctf_stream_pos *pos,
227 enum bt_ctf_byte_order native_byte_order)
228 {
229 struct bt_ctf_field_type *type = flt_field->parent.type;
230 struct bt_ctf_field_type_floating_point *flt_type = (void *) type;
231 enum bt_ctf_byte_order byte_order;
232 union intval value;
233 unsigned int size;
234
235 byte_order = flt_type->user_byte_order;
236 if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
237 byte_order = native_byte_order;
238 }
239
240 if (flt_type->mant_dig == FLT_MANT_DIG) {
241 union u32f {
242 uint32_t u;
243 float f;
244 } u32f;
245
246 u32f.f = (float) flt_field->payload;
247 value.unsignd = u32f.u;
248 size = 32;
249 } else if (flt_type->mant_dig == DBL_MANT_DIG) {
250 union u64d {
251 uint64_t u;
252 double d;
253 } u64d;
254
255 u64d.d = flt_field->payload;
256 value.unsignd = u64d.u;
257 size = 64;
258 } else {
259 return -EINVAL;
260 }
261
262 return integer_write(pos, value, type->alignment, size, BT_FALSE,
263 byte_order);
264 }
265
266 BT_HIDDEN
267 void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index,
268 int whence)
269 {
270 int ret;
271
272 assert(whence == SEEK_CUR && index == 0);
273
274 if (pos->base_mma) {
275 /* unmap old base */
276 ret = munmap_align(pos->base_mma);
277 if (ret) {
278 assert(BT_FALSE);
279 }
280 pos->base_mma = NULL;
281 }
282
283 /* The writer will add padding */
284 pos->mmap_offset += pos->packet_size / CHAR_BIT;
285 pos->packet_size = getpagesize() * 8 * CHAR_BIT;
286 do {
287 ret = bt_posix_fallocate(pos->fd, pos->mmap_offset,
288 pos->packet_size / CHAR_BIT);
289 } while (ret == EINTR);
290 assert(ret == 0);
291 pos->offset = 0;
292
293 /* map new base. Need mapping length from header. */
294 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
295 pos->flags, pos->fd, pos->mmap_offset);
296 if (pos->base_mma == MAP_FAILED) {
297 assert(BT_FALSE);
298 }
299 }
This page took 0.036615 seconds and 5 git commands to generate.