/* magic */
ctf_dummy_pos(pos, &dummy);
- ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
- ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+ if (!ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
+ goto error;
+ if (!ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT))
+ goto error;
assert(!ctf_pos_packet(&dummy));
- ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+ if (!ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT))
+ goto error;
*(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
- ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+ if (!ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT))
+ goto error;
/* uuid */
ctf_dummy_pos(pos, &dummy);
- ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
- ctf_move_pos(&dummy, 16 * CHAR_BIT);
+ if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
+ goto error;
+ if (!ctf_move_pos(&dummy, 16 * CHAR_BIT))
+ goto error;
assert(!ctf_pos_packet(&dummy));
- ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
+ if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
+ goto error;
memcpy(ctf_get_pos_addr(pos), uuid, BABELTRACE_UUID_LEN);
- ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT);
+ if (!ctf_move_pos(pos, BABELTRACE_UUID_LEN * CHAR_BIT))
+ goto error;
+ return;
+
+error:
+ fprintf(stderr, "[error] Out of packet bounds when writing packet header\n");
+ abort();
}
static
/* content_size */
ctf_dummy_pos(pos, &dummy);
- ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
- ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
+ if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
assert(!ctf_pos_packet(&dummy));
- ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
*(uint64_t *) ctf_get_pos_addr(pos) = ~0ULL; /* Not known yet */
pos->content_size_loc = (uint64_t *) ctf_get_pos_addr(pos);
- ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
/* packet_size */
ctf_dummy_pos(pos, &dummy);
- ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
- ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_align_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
+ if (!ctf_move_pos(&dummy, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
assert(!ctf_pos_packet(&dummy));
- ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
*(uint64_t *) ctf_get_pos_addr(pos) = pos->packet_size;
- ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
+ return;
+
+error:
+ fprintf(stderr, "[error] Out of packet bounds when writing packet context\n");
+ abort();
}
static
}
}
/* timestamp */
- ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_align_pos(pos, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
if (!pos->dummy)
*(uint64_t *) ctf_get_pos_addr(pos) = *ts;
- ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT);
+ if (!ctf_move_pos(pos, sizeof(uint64_t) * CHAR_BIT))
+ goto error;
+ return;
+
+error:
+ fprintf(stderr, "[error] Out of packet bounds when writing event header\n");
+ abort();
}
static
for (;;) {
ctf_dummy_pos(pos, &dummy);
write_event_header(&dummy, line, &tline, len, &tlen, &ts);
- ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
- ctf_move_pos(&dummy, tlen * CHAR_BIT);
+ if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
+ goto error;
+ if (!ctf_move_pos(&dummy, tlen * CHAR_BIT))
+ goto error;
if (ctf_pos_packet(&dummy)) {
ctf_pos_pad_packet(pos);
write_packet_header(pos, s_uuid);
}
write_event_header(pos, line, &tline, len, &tlen, &ts);
- ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
+ if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
+ goto error;
memcpy(ctf_get_pos_addr(pos), tline, tlen);
- ctf_move_pos(pos, tlen * CHAR_BIT);
+ if (!ctf_move_pos(pos, tlen * CHAR_BIT))
+ goto error;
+ return;
+
+error:
+ fprintf(stderr, "[error] Out of packet bounds when writing event payload\n");
+ abort();
}
static
if (integer_declaration->len == CHAR_BIT
&& integer_declaration->p.alignment == CHAR_BIT) {
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, array_declaration->len * CHAR_BIT))
return -EFAULT;
g_string_insert_len(array_definition->string,
0, (char *) ctf_get_pos_addr(pos),
array_declaration->len);
- ctf_move_pos(pos, array_declaration->len * CHAR_BIT);
+ if (!ctf_move_pos(pos, array_declaration->len * CHAR_BIT))
+ return -EFAULT;
return 0;
}
}
if (integer_declaration->len == CHAR_BIT
&& integer_declaration->p.alignment == CHAR_BIT) {
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, array_declaration->len * CHAR_BIT))
return -EFAULT;
memcpy((char *) ctf_get_pos_addr(pos),
array_definition->string->str,
array_declaration->len);
- ctf_move_pos(pos, array_declaration->len * CHAR_BIT);
+ if (!ctf_move_pos(pos, array_declaration->len * CHAR_BIT))
+ return -EFAULT;
return 0;
}
}
mmap_align_set_addr(&mma, (char *) u.bits);
destp.base_mma = &mma;
destp.packet_size = sizeof(u) * CHAR_BIT;
- ctf_align_pos(pos, float_declaration->p.alignment);
+ if (!ctf_align_pos(pos, float_declaration->p.alignment)) {
+ ret = -EFAULT;
+ goto end_unref;
+ }
ret = _ctf_float_copy(&destp.parent, tmpfloat, ppos, float_definition);
switch (float_declaration->mantissa->len + 1) {
case FLT_MANT_DIG:
ret = -EINVAL;
goto end_unref;
}
- ctf_align_pos(pos, float_declaration->p.alignment);
+ if (!ctf_align_pos(pos, float_declaration->p.alignment)) {
+ ret = -EFAULT;
+ goto end_unref;
+ }
ret = _ctf_float_copy(ppos, float_definition, &srcp.parent, tmpfloat);
end_unref:
struct ctf_stream_pos *pos = ctf_pos(ppos);
int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, integer_declaration->len))
return -EFAULT;
assert(0);
}
}
- ctf_move_pos(pos, integer_declaration->len);
+ if (!ctf_move_pos(pos, integer_declaration->len))
+ return -EFAULT;
return 0;
}
struct ctf_stream_pos *pos = ctf_pos(ppos);
int rbo = (integer_declaration->byte_order != BYTE_ORDER); /* reverse byte order */
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, integer_declaration->len))
return -EFAULT;
}
}
end:
- ctf_move_pos(pos, integer_declaration->len);
+ if (!ctf_move_pos(pos, integer_declaration->len))
+ return -EFAULT;
return 0;
}
return _aligned_integer_read(ppos, definition);
}
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, integer_declaration->len))
return -EFAULT;
pos->offset, integer_declaration->len,
&integer_definition->value._signed);
}
- ctf_move_pos(pos, integer_declaration->len);
+ if (!ctf_move_pos(pos, integer_declaration->len))
+ return -EFAULT;
return 0;
}
return _aligned_integer_write(ppos, definition);
}
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, integer_declaration->len))
return -EFAULT;
integer_definition->value._signed);
}
end:
- ctf_move_pos(pos, integer_declaration->len);
+ if (!ctf_move_pos(pos, integer_declaration->len))
+ return -EFAULT;
return 0;
}
&& integer_declaration->p.alignment == CHAR_BIT) {
uint64_t len = bt_sequence_len(sequence_definition);
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, len * CHAR_BIT))
return -EFAULT;
g_string_assign(sequence_definition->string, "");
g_string_insert_len(sequence_definition->string,
0, (char *) ctf_get_pos_addr(pos), len);
- ctf_move_pos(pos, len * CHAR_BIT);
+ if (!ctf_move_pos(pos, len * CHAR_BIT))
+ return -EFAULT;
return 0;
}
}
&& integer_declaration->p.alignment == CHAR_BIT) {
uint64_t len = bt_sequence_len(sequence_definition);
- ctf_align_pos(pos, integer_declaration->p.alignment);
+ if (!ctf_align_pos(pos, integer_declaration->p.alignment))
+ return -EFAULT;
if (!ctf_pos_access_ok(pos, len * CHAR_BIT))
return -EFAULT;
memcpy((char *) ctf_get_pos_addr(pos),
sequence_definition->string->str, len);
- ctf_move_pos(pos, len * CHAR_BIT);
+ if (!ctf_move_pos(pos, len * CHAR_BIT))
+ return -EFAULT;
return 0;
}
}
ssize_t max_len;
char *srcaddr;
- ctf_align_pos(pos, string_declaration->p.alignment);
+ if (!ctf_align_pos(pos, string_declaration->p.alignment))
+ return -EFAULT;
srcaddr = ctf_get_pos_addr(pos);
if (pos->offset == EOF)
printf_debug("CTF string read %s\n", srcaddr);
memcpy(string_definition->value, srcaddr, len);
string_definition->len = len;
- ctf_move_pos(pos, len * CHAR_BIT);
+ if (!ctf_move_pos(pos, len * CHAR_BIT))
+ return -EFAULT;
return 0;
}
size_t len;
char *destaddr;
- ctf_align_pos(pos, string_declaration->p.alignment);
+ if (!ctf_align_pos(pos, string_declaration->p.alignment))
+ return -EFAULT;
assert(string_definition->value != NULL);
len = string_definition->len;
destaddr = ctf_get_pos_addr(pos);
memcpy(destaddr, string_definition->value, len);
end:
- ctf_move_pos(pos, len * CHAR_BIT);
+ if (!ctf_move_pos(pos, len * CHAR_BIT))
+ return -EFAULT;
return 0;
}
struct bt_declaration *declaration = definition->declaration;
struct ctf_stream_pos *pos = ctf_pos(ppos);
- ctf_align_pos(pos, declaration->alignment);
+ if (!ctf_align_pos(pos, declaration->alignment))
+ return -EFAULT;
return bt_struct_rw(ppos, definition);
}
struct bt_declaration *declaration = definition->declaration;
struct ctf_stream_pos *pos = ctf_pos(ppos);
- ctf_align_pos(pos, declaration->alignment);
+ if (!ctf_align_pos(pos, declaration->alignment))
+ return -EFAULT;
return bt_variant_rw(ppos, definition);
}
}
}
- ctf_align_pos(pos, field->type->declaration->alignment);
+ if (!ctf_align_pos(pos, field->type->declaration->alignment)) {
+ ret = -1;
+ goto end;
+ }
for (i = 0; i < structure->fields->len; i++) {
struct bt_ctf_field *field = g_ptr_array_index(
/*
* move_pos - move position of a relative bit offset
*
+ * Return 1 if OK, 0 if out-of-bound.
+ *
* TODO: allow larger files by updating base too.
*/
static inline
-void ctf_move_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
+int ctf_move_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
{
+ uint64_t max_len;
+
printf_debug("ctf_move_pos test EOF: %" PRId64 "\n", pos->offset);
if (unlikely(pos->offset == EOF))
- return;
+ return 0;
+ if (pos->flags & PROT_READ)
+ max_len = pos->content_size;
+ else
+ max_len = pos->packet_size;
+ if (unlikely(pos->offset + bit_offset > max_len))
+ return 0;
pos->offset += bit_offset;
printf_debug("ctf_move_pos after increment: %" PRId64 "\n", pos->offset);
+ return 1;
}
/*
* align_pos - align position on a bit offset (> 0)
*
+ * Return 1 if OK, 0 if out-of-bound.
+ *
* TODO: allow larger files by updating base too.
*/
static inline
-void ctf_align_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
+int ctf_align_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
{
- ctf_move_pos(pos, offset_align(pos->offset, bit_offset));
+ return ctf_move_pos(pos, offset_align(pos->offset, bit_offset));
}
static inline
static inline
int ctf_pos_access_ok(struct ctf_stream_pos *pos, uint64_t bit_len)
{
+ uint64_t max_len;
+
if (unlikely(pos->offset == EOF))
return 0;
- if (unlikely(pos->offset + bit_len > pos->packet_size))
+ if (pos->flags & PROT_READ)
+ max_len = pos->content_size;
+ else
+ max_len = pos->packet_size;
+ if (unlikely(pos->offset + bit_len > max_len))
return 0;
return 1;
}