Skip to contentSkip to conversation

Propagate error from packet_seek in case of truncated packet

RFC PATCH New Jonathan Rajotte 2 participants 3 messages Feb 7, 2018 – Feb 20, 2018 2 messages not in the archive Library code

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

RFC PATCH 1/1 Propagate error from packet_seek in case of truncated packet

Report the error all the way up allowing users/scripts to perform error detection and act on it.

Print to stderr the truncated packet information for easier identification.

Introduce bt_packet_seek_error enum for specific error handling.

Use the ERANGE errno for error propagation inside bt_iter_next and ctf_read_event.

---
 converter/babeltrace.c               | 12 ++++++++--
 formats/ctf/ctf.c                    | 44 ++++++++++++++++++++++++++++--------
 formats/lttng-live/lttng-live-comm.c |  6 ++---
 include/babeltrace/error.h           |  5 ++++
 lib/iterator.c                       |  4 ++++
 5 files changed, 57 insertions(+), 14 deletions(-)
converter/babeltrace.c +10 -2
index f74384e..ef783ed 100644
@@ -669,6 +669,7 @@ int convert_trace(struct bt_trace_descriptor *td_write,
 	struct bt_iter_pos *begin_pos = NULL, *end_pos = NULL;
 	struct bt_ctf_event *ctf_event;
 	int ret;
+	int error_holder = 0;
 
 	sout = container_of(td_write, struct ctf_text_stream_pos,
 			trace_descriptor);
@@ -695,11 +696,18 @@ int convert_trace(struct bt_trace_descriptor *td_write,
 			goto end;
 		}
 		ret = bt_iter_next(bt_ctf_get_iter(iter));
-		if (ret < 0) {
+		if (ret == -ERANGE) {
+			/*
+			 * Remember that a range (truncated packet)
+			 * error occurred and continue.
+			 */
+			error_holder = 1;
+			continue;
+		} else if (ret < 0) {
 			goto end;
 		}
 	}
-	ret = 0;
+	ret = error_holder;
 
 end:
 	bt_ctf_iter_destroy(iter);
formats/ctf/ctf.c +35 -9

92 lines not on this page · raw message

formats/lttng-live/lttng-live-comm.c +3 -3
index cb871a1..055b1c3 100644
@@ -1223,7 +1223,7 @@ void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
 	ret = handle_seek_position(index, whence, viewer_stream, pos,
 			file_stream);
 	if (ret != 0) {
-		ret = -1;
+		ret = -BT_PACKET_SEEK_ERROR;
 		goto end;
 	}
 
@@ -1266,7 +1266,7 @@ retry:
 			if (!lttng_live_should_quit()) {
 				fprintf(stderr, "[error] get_next_index failed\n");
 			}
-			ret = -1;
+			ret = -BT_PACKET_SEEK_ERROR;
 			goto end;
 		}
 		printf_verbose("Index received : packet_size : %" PRIu64
@@ -1389,7 +1389,7 @@ retry:
 		pos->offset = EOF;
 		if (!lttng_live_should_quit()) {
 			fprintf(stderr, "[error] get_data_packet failed\n");
-			ret = -1;
+			ret = -BT_PACKET_SEEK_ERROR;
 		} else {
 			ret = 0;
 		}
include/babeltrace/error.h +5 -0
index 2f6a6d7..8b4f323 100644
@@ -30,6 +30,11 @@
  * SOFTWARE.
  */
 
+enum bt_packet_seek_error {
+	BT_PACKET_SEEK_ERROR = 1,
+	BT_PACKET_SEEK_ERROR_TRUNCATED_PACKET = 2,
+};
+
 /*
  * bt_packet_seek_get_error: get the return code of the last packet_seek use.
  *
lib/iterator.c +4 -0
index 30a4542..639a2d2 100644
@@ -864,6 +864,10 @@ int bt_iter_next(struct bt_iter *iter)
 		 */
 		ret = 0;
 		goto reinsert;
+	} else if (ret == -ERANGE) {
+		removed = bt_heap_remove(iter->stream_heap);
+		assert(removed == file_stream);
+		goto end;
 	} else if (ret) {
 		goto end;
 	}
Signature · 6 lines
2.7.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

A message that is not in the archive (sent off-list or not yet received): 1518043925-13229-1-git-send-email-jonathan.rajotte-julien@efficios.com

#3 lore raw

Merged with a couple of minor style fixes in master, stable-1.5, and stable-1.4.

Thanks!
Jérémie

Quoted 225 lines · Jonathan Rajotte

Report the error all the way up allowing users/scripts to perform error detection and act on it.

Print to stderr the truncated packet information for easier identification.

Introduce bt_packet_seek_error enum for specific error handling.

Use the ERANGE errno for error propagation inside bt_iter_next and ctf_read_event.

---
 converter/babeltrace.c               | 12 ++++++++--
 formats/ctf/ctf.c                    | 44 ++++++++++++++++++++++++++++--------
 formats/lttng-live/lttng-live-comm.c |  6 ++---
 include/babeltrace/error.h           |  5 ++++
 lib/iterator.c                       |  4 ++++
 5 files changed, 57 insertions(+), 14 deletions(-)
converter/babeltrace.c +10 -2
index f74384e..ef783ed 100644
@@ -669,6 +669,7 @@ int convert_trace(struct bt_trace_descriptor *td_write,
 	struct bt_iter_pos *begin_pos = NULL, *end_pos = NULL;
 	struct bt_ctf_event *ctf_event;
 	int ret;
+	int error_holder = 0;
 
 	sout = container_of(td_write, struct ctf_text_stream_pos,
 			trace_descriptor);
@@ -695,11 +696,18 @@ int convert_trace(struct bt_trace_descriptor *td_write,
 			goto end;
 		}
 		ret = bt_iter_next(bt_ctf_get_iter(iter));
-		if (ret < 0) {
+		if (ret == -ERANGE) {
+			/*
+			 * Remember that a range (truncated packet)
+			 * error occurred and continue.
+			 */
+			error_holder = 1;
+			continue;
+		} else if (ret < 0) {
 			goto end;
 		}
 	}
-	ret = 0;
+	ret = error_holder;
 
 end:
 	bt_ctf_iter_destroy(iter);
formats/ctf/ctf.c +35 -9
index b74cddd..9acaa2a 100644
@@ -476,6 +476,25 @@ void ctf_print_discarded_lost(FILE *fp, struct ctf_stream_definition *stream)
 		"buffers or with fewer events enabled.\n");
 	fflush(fp);
 }
+static
+void ctf_print_truncated_packet(FILE *fp, struct ctf_stream_definition *stream,
+		uint64_t packet_size, uint64_t remaining_file_size)
+{
+	fflush(stdout);
+	fprintf(fp, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits) ",
+			packet_size, remaining_file_size);
+	fprintf(fp, "in trace UUID ");
+	print_uuid(fp, stream->stream_class->trace->uuid);
+	if (stream->stream_class->trace->parent.path[0])
+		fprintf(fp, ", at path: \"%s\"",
+			stream->stream_class->trace->parent.path);
+
+	fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
+	if (stream->path[0])
+		fprintf(fp, ", at relative path: \"%s\"", stream->path);
+	fprintf(fp, ".\n");
+	fflush(fp);
+}
 
 static
 int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
@@ -491,8 +510,12 @@ int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *str
 	if (unlikely(pos->offset == EOF))
 		return EOF;
 
-	if (ctf_pos_get_event(pos))
+	ret = ctf_pos_get_event(pos);
+	if (ret == -BT_PACKET_SEEK_ERROR_TRUNCATED_PACKET) {
+		return -ERANGE;
+	} else if (ret) {
 		return EOF;
+	}
 
 	/* save the current position as a restore point */
 	pos->last_offset = pos->offset;
@@ -1059,7 +1082,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
 	case SEEK_SET:	/* Fall-through */
 		break;	/* OK */
 	default:
-		ret = -1;
+		ret = -BT_PACKET_SEEK_ERROR;
 		goto end;
 	}
 
@@ -1072,7 +1095,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
 		if (ret) {
 			fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
 				strerror(errno));
-			ret = -1;
+			ret = -BT_PACKET_SEEK_ERROR;
 			goto end;
 		}
 		pos->base_mma = NULL;
@@ -1093,7 +1116,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
 			pos->cur_index = 0;
 			break;
 		default:
-			ret = -1;
+			ret = -BT_PACKET_SEEK_ERROR;
 			goto end;
 		}
 		pos->content_size = -1U;	/* Unknown at this point */
@@ -1127,7 +1150,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
 			pos->cur_index = index;
 			break;
 		default:
-			ret = -1;
+			ret = -BT_PACKET_SEEK_ERROR;
 			goto end;
 		}
 
@@ -1176,15 +1199,18 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
 		if (packet_index->data_offset == -1) {
 			ret = find_data_offset(pos, file_stream, packet_index);
 			if (ret < 0) {
-				ret = -1;
+				ret = -BT_PACKET_SEEK_ERROR;
 				goto end;
 			}
 		}
 
 		if (packet_index->packet_size > ((uint64_t) pos->file_length - packet_index->offset) * CHAR_BIT) {
-			fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
-				packet_index->packet_size, ((uint64_t) pos->file_length - packet_index->offset) * CHAR_BIT);
-			ret = -1;
+			uint64_t remaining_file_size = ((uint64_t) pos->file_length - packet_index->offset) * CHAR_BIT;
+			ctf_print_truncated_packet(stderr, &file_stream->parent,
+					packet_index->packet_size,
+					remaining_file_size);
+			pos->offset = EOF;
+			ret = -BT_PACKET_SEEK_ERROR_TRUNCATED_PACKET;
 			goto end;
 		}
 
formats/lttng-live/lttng-live-comm.c +3 -3
index cb871a1..055b1c3 100644
@@ -1223,7 +1223,7 @@ void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
 	ret = handle_seek_position(index, whence, viewer_stream, pos,
 			file_stream);
 	if (ret != 0) {
-		ret = -1;
+		ret = -BT_PACKET_SEEK_ERROR;
 		goto end;
 	}
 
@@ -1266,7 +1266,7 @@ retry:
 			if (!lttng_live_should_quit()) {
 				fprintf(stderr, "[error] get_next_index failed\n");
 			}
-			ret = -1;
+			ret = -BT_PACKET_SEEK_ERROR;
 			goto end;
 		}
 		printf_verbose("Index received : packet_size : %" PRIu64
@@ -1389,7 +1389,7 @@ retry:
 		pos->offset = EOF;
 		if (!lttng_live_should_quit()) {
 			fprintf(stderr, "[error] get_data_packet failed\n");
-			ret = -1;
+			ret = -BT_PACKET_SEEK_ERROR;
 		} else {
 			ret = 0;
 		}
include/babeltrace/error.h +5 -0
index 2f6a6d7..8b4f323 100644
@@ -30,6 +30,11 @@
  * SOFTWARE.
  */
 
+enum bt_packet_seek_error {
+	BT_PACKET_SEEK_ERROR = 1,
+	BT_PACKET_SEEK_ERROR_TRUNCATED_PACKET = 2,
+};
+
 /*
  * bt_packet_seek_get_error: get the return code of the last packet_seek use.
  *
lib/iterator.c +4 -0
index 30a4542..639a2d2 100644
@@ -864,6 +864,10 @@ int bt_iter_next(struct bt_iter *iter)
 		 */
 		ret = 0;
 		goto reinsert;
+	} else if (ret == -ERANGE) {
+		removed = bt_heap_remove(iter->stream_heap);
+		assert(removed == file_stream);
+		goto end;
 	} else if (ret) {
 		goto end;
 	}
Signature · 6 lines
2.7.4

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

#4 lore raw

Re: [RFC PATCH babeltrace-1.5] Fix: report truncated files while reading (v2)

In reply to a message not in the archive

Merged in master, stable-1.5, and stable-1.4.

Thanks!
Jérémie

Quoted 167 lines · Mathieu Desnoyers

When index files are available, this ensures we report the issue properly before mapping a truncated packet rather than hitting a SIGBUS.

For traces without index files, remove the check that was done at index creation, and use the new check done when mapping the packet instead. This ensures babeltrace can print stream data prior to the truncated packet for this stream.

TODO: What is _not_ done in this patch is changing handling of truncated
packets: this should be treated as a warning rather than an error, so processing of other streams continue.

CC Jonathan Rajotte-Julien <[email protected]>
CC Jérémie Galarneau <[email protected]>
---
 formats/ctf/ctf.c              | 44 +++++++++++++++++++-----------------------
 include/babeltrace/ctf/types.h |  1 +
 2 files changed, 21 insertions(+), 24 deletions(-)
formats/ctf/ctf.c +20 -24
index ab98b288..b74cddd7 100644
@@ -1180,6 +1180,14 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
 				goto end;
 			}
 		}
+
+		if (packet_index->packet_size > ((uint64_t) pos->file_length - packet_index->offset) * CHAR_BIT) {
+			fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
+				packet_index->packet_size, ((uint64_t) pos->file_length - packet_index->offset) * CHAR_BIT);
+			ret = -1;
+			goto end;
+		}
+
 		pos->content_size = packet_index->content_size;
 		pos->packet_size = packet_index->packet_size;
 		pos->mmap_offset = packet_index->offset;
@@ -1708,8 +1716,7 @@ int stream_assign_class(struct ctf_trace *td,
 static
 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
 			struct ctf_trace *td,
-			struct ctf_file_stream *file_stream,
-			size_t filesize)
+			struct ctf_file_stream *file_stream)
 {
 	struct packet_index packet_index;
 	uint64_t stream_id = 0;
@@ -1724,8 +1731,8 @@ begin:
 		first_packet = 1;
 	}
 
-	if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
-		packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
+	if (pos->file_length - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
+		packet_map_len = (pos->file_length - pos->mmap_offset) << LOG2_CHAR_BIT;
 	}
 
 	if (pos->base_mma) {
@@ -1843,7 +1850,7 @@ begin:
 			packet_index.packet_size = bt_get_unsigned_int(field);
 		} else {
 			/* Use file size for packet size */
-			packet_index.packet_size = filesize * CHAR_BIT;
+			packet_index.packet_size = pos->file_length * CHAR_BIT;
 		}
 
 		/* read content size from header */
@@ -1855,7 +1862,7 @@ begin:
 			packet_index.content_size = bt_get_unsigned_int(field);
 		} else {
 			/* Use packet size if non-zero, else file size */
-			packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
+			packet_index.content_size = packet_index.packet_size ? : pos->file_length * CHAR_BIT;
 		}
 
 		/* read timestamp begin from header */
@@ -1912,9 +1919,9 @@ begin:
 		}
 	} else {
 		/* Use file size for packet size */
-		packet_index.packet_size = filesize * CHAR_BIT;
+		packet_index.packet_size = pos->file_length * CHAR_BIT;
 		/* Use packet size if non-zero, else file size */
-		packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
+		packet_index.content_size = packet_index.packet_size ? : pos->file_length * CHAR_BIT;
 	}
 
 	/* Validate content size and packet size values */
@@ -1924,12 +1931,6 @@ begin:
 		return -EINVAL;
 	}
 
-	if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
-		fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
-			packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
-		return -EINVAL;
-	}
-
 	if (packet_index.content_size < pos->offset) {
 		fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
 		return -EINVAL;
@@ -1952,7 +1953,7 @@ begin:
 
 	/* Retry with larger mapping */
 retry:
-	if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
+	if (packet_map_len == ((pos->file_length - pos->mmap_offset) << LOG2_CHAR_BIT)) {
 		/*
 		 * Reached EOF, but still expecting header/context data.
 		 */
@@ -1975,17 +1976,12 @@ int create_stream_packet_index(struct ctf_trace *td,
 			struct ctf_file_stream *file_stream)
 {
 	struct ctf_stream_pos *pos;
-	struct stat filestats;
 	int ret;
 
 	pos = &file_stream->pos;
 
-	ret = fstat(pos->fd, &filestats);
-	if (ret < 0)
-		return ret;
-
 	/* Deal with empty files */
-	if (!filestats.st_size) {
+	if (!pos->file_length) {
 		if (file_stream->parent.trace_packet_header
 				|| file_stream->parent.stream_packet_context) {
 			/*
@@ -2008,9 +2004,8 @@ int create_stream_packet_index(struct ctf_trace *td,
 		}
 	}
 
-	for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
-		ret = create_stream_one_packet_index(pos, td, file_stream,
-			filestats.st_size);
+	for (pos->mmap_offset = 0; pos->mmap_offset < pos->file_length; ) {
+		ret = create_stream_one_packet_index(pos, td, file_stream);
 		if (ret)
 			return ret;
 	}
@@ -2195,6 +2190,7 @@ int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
 	file_stream->pos.last_offset = LAST_OFFSET_POISON;
 	file_stream->pos.fd = -1;
 	file_stream->pos.index_fp = NULL;
+	file_stream->pos.file_length = statbuf.st_size;
 
 	strncpy(file_stream->parent.path, path, PATH_MAX);
 	file_stream->parent.path[PATH_MAX - 1] = '\0';
include/babeltrace/ctf/types.h +1 -0
index 59ce9981..0bc003c8 100644
@@ -92,6 +92,7 @@ struct ctf_stream_pos {
 
 	int dummy;		/* dummy position, for length calculation */
 	struct bt_stream_callbacks *cb;	/* Callbacks registered for iterator. */
+	size_t file_length;	/* length of backing file, in bytes. */
 	void *priv;
 };
 
Signature · 1 line
2.11.0

_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev