Fix: avoid double-free in build_index_from_idx_file
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.c
index b4022c32f5804689e0e7886fab82001c97d34650..5f811214f917634a980854e1dd2cf3fe1f14adbc 100644 (file)
@@ -25,7 +25,7 @@
 #define BT_COMP_LOG_SELF_COMP (ds_file->self_comp)
 #define BT_LOG_OUTPUT_LEVEL (ds_file->log_level)
 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
-#include "plugins/comp-logging.h"
+#include "logging/comp-logging.h"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -48,6 +48,7 @@
 static inline
 size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
 {
+       BT_ASSERT(ds_file->mmap_len >= ds_file->request_offset);
        return ds_file->mmap_len - ds_file->request_offset;
 }
 
@@ -138,7 +139,10 @@ enum bt_msg_iter_medium_status medop_request_bytes(
                goto end;
        }
 
-       /* Check if we have at least one memory-mapped byte left */
+       /*
+        * Check if we have at least one memory-mapped byte left. If we don't,
+        * mmap the next file.
+        */
        if (remaining_mmap_bytes(ds_file) == 0) {
                /* Are we at the end of the file? */
                if (ds_file->mmap_offset >= ds_file->file->size) {
@@ -163,6 +167,7 @@ enum bt_msg_iter_medium_status medop_request_bytes(
        }
 
        *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
+       BT_ASSERT(ds_file->mmap_addr);
        *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
        ds_file->request_offset += *buffer_sz;
        goto end;
@@ -206,47 +211,56 @@ enum bt_msg_iter_medium_status medop_seek(enum bt_msg_iter_seek_whence whence,
        enum bt_msg_iter_medium_status ret =
                        BT_MSG_ITER_MEDIUM_STATUS_OK;
        struct ctf_fs_ds_file *ds_file = data;
-       off_t file_size = ds_file->file->size;
+       off_t offset_in_mapping, file_size = ds_file->file->size;
 
        if (whence != BT_MSG_ITER_SEEK_WHENCE_SET ||
                offset < 0 || offset > file_size) {
                BT_COMP_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
-                               "file-size=%jd", (int) whence, offset,
-                               file_size);
+                               "file-size=%jd", (int) whence, (intmax_t) offset,
+                               (intmax_t) file_size);
                ret = BT_MSG_ITER_MEDIUM_STATUS_INVAL;
                goto end;
        }
 
+       /* If there is no current mapping, map the right file directly. */
+       if (!ds_file->mmap_addr) {
+               goto map_requested_offset;
+       }
+
        /*
         * Determine whether or not the destination is contained within the
         * current mapping.
         */
-       if (ds_file->mmap_addr && (offset < ds_file->mmap_offset ||
-                       offset >= ds_file->mmap_offset + ds_file->mmap_len)) {
+       if (offset < ds_file->mmap_offset ||
+                       offset >= ds_file->mmap_offset + ds_file->mmap_len) {
                int unmap_ret;
-               off_t offset_in_mapping = offset %
-                       bt_common_get_page_size(ds_file->log_level);
-
                BT_COMP_LOGD("Medium seek request cannot be accomodated by the current "
                                "file mapping: offset=%jd, mmap-offset=%jd, "
-                               "mmap-len=%zu", offset, ds_file->mmap_offset,
+                               "mmap-len=%zu", (intmax_t) offset, (intmax_t) ds_file->mmap_offset,
                                ds_file->mmap_len);
                unmap_ret = ds_file_munmap(ds_file);
                if (unmap_ret) {
                        ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
                        goto end;
                }
-
-               ds_file->mmap_offset = offset - offset_in_mapping;
-               ds_file->request_offset = offset_in_mapping;
-               ret = ds_file_mmap_next(ds_file);
-               if (ret != BT_MSG_ITER_MEDIUM_STATUS_OK) {
-                       goto end;
-               }
+               goto map_requested_offset;
        } else {
                ds_file->request_offset = offset - ds_file->mmap_offset;
+               goto test_end;
        }
 
+map_requested_offset:
+       offset_in_mapping = offset %
+               bt_mmap_get_offset_align_size(ds_file->log_level);
+
+       ds_file->mmap_offset = offset - offset_in_mapping;
+       ds_file->request_offset = offset_in_mapping;
+       ret = ds_file_mmap_next(ds_file);
+       if (ret != BT_MSG_ITER_MEDIUM_STATUS_OK) {
+               goto end;
+       }
+
+test_end:
        ds_file->end_reached = (offset == file_size);
 end:
        return ret;
@@ -270,7 +284,8 @@ int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
 
 static
 struct ctf_fs_ds_index *build_index_from_idx_file(
-               struct ctf_fs_ds_file *ds_file)
+               struct ctf_fs_ds_file *ds_file,
+               struct ctf_fs_ds_file_info *file_info)
 {
        int ret;
        gchar *directory = NULL;
@@ -282,7 +297,7 @@ struct ctf_fs_ds_index *build_index_from_idx_file(
        const char *mmap_begin = NULL, *file_pos = NULL;
        const struct ctf_packet_index_file_hdr *header = NULL;
        struct ctf_fs_ds_index *index = NULL;
-       struct ctf_fs_ds_index_entry *index_entry = NULL;
+       struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL;
        uint64_t total_packets_size = 0;
        size_t file_index_entry_size;
        size_t file_entry_count;
@@ -389,15 +404,18 @@ struct ctf_fs_ds_index *build_index_from_idx_file(
                        goto error;
                }
 
+               /* Set path to stream file. */
+               index_entry->path = file_info->path->str;
+
                /* Convert size in bits to bytes. */
                packet_size /= CHAR_BIT;
                index_entry->packet_size = packet_size;
 
                index_entry->offset = be64toh(file_index->offset);
-               if (i != 0 && index_entry->offset < (index_entry - 1)->offset) {
+               if (i != 0 && index_entry->offset < prev_index_entry->offset) {
                        BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
                                "previous offset=%" PRIu64 ", current offset=%" PRIu64,
-                               (index_entry - 1)->offset, index_entry->offset);
+                               prev_index_entry->offset, index_entry->offset);
                        goto error;
                }
 
@@ -430,7 +448,11 @@ struct ctf_fs_ds_index *build_index_from_idx_file(
                total_packets_size += packet_size;
                file_pos += file_index_entry_size;
 
+               prev_index_entry = index_entry;
+
+               /* Give ownership of `index_entry` to `index->entries`. */
                g_ptr_array_add(index->entries, index_entry);
+               index_entry = NULL;
        }
 
        /* Validate that the index addresses the complete stream. */
@@ -476,6 +498,8 @@ int init_index_entry(struct ctf_fs_ds_index_entry *entry,
        entry->packet_size = packet_size;
 
        if (props->snapshots.beginning_clock != UINT64_C(-1)) {
+               entry->timestamp_begin = props->snapshots.beginning_clock;
+
                /* Convert the packet's bound to nanoseconds since Epoch. */
                ret = convert_cycles_to_ns(sc->default_clock_class,
                                           props->snapshots.beginning_clock,
@@ -485,10 +509,14 @@ int init_index_entry(struct ctf_fs_ds_index_entry *entry,
                        goto end;
                }
        } else {
+               entry->timestamp_begin = UINT64_C(-1);
                entry->timestamp_begin_ns = UINT64_C(-1);
        }
 
        if (props->snapshots.end_clock != UINT64_C(-1)) {
+               entry->timestamp_end = props->snapshots.end_clock;
+
+               /* Convert the packet's bound to nanoseconds since Epoch. */
                ret = convert_cycles_to_ns(sc->default_clock_class,
                                           props->snapshots.end_clock,
                                           &entry->timestamp_end_ns);
@@ -497,6 +525,7 @@ int init_index_entry(struct ctf_fs_ds_index_entry *entry,
                        goto end;
                }
        } else {
+               entry->timestamp_end = UINT64_C(-1);
                entry->timestamp_end_ns = UINT64_C(-1);
        }
 
@@ -506,7 +535,8 @@ end:
 
 static
 struct ctf_fs_ds_index *build_index_from_stream_file(
-               struct ctf_fs_ds_file *ds_file)
+               struct ctf_fs_ds_file *ds_file,
+               struct ctf_fs_ds_file_info *file_info)
 {
        int ret;
        struct ctf_fs_ds_index *index = NULL;
@@ -520,7 +550,7 @@ struct ctf_fs_ds_index *build_index_from_stream_file(
                goto error;
        }
 
-       do {
+       while (true) {
                off_t current_packet_size_bytes;
                struct ctf_fs_ds_index_entry *index_entry;
                struct bt_msg_iter_packet_properties props;
@@ -561,9 +591,9 @@ struct ctf_fs_ds_index *build_index_from_stream_file(
                                        "packet-offset=%jd, packet-size-bytes=%jd, "
                                        "file-size=%jd",
                                        ds_file->file->path->str,
-                                       current_packet_offset_bytes,
-                                       current_packet_size_bytes,
-                                       ds_file->file->size);
+                                       (intmax_t) current_packet_offset_bytes,
+                                       (intmax_t) current_packet_size_bytes,
+                                       (intmax_t) ds_file->file->size);
                        goto error;
                }
 
@@ -573,6 +603,9 @@ struct ctf_fs_ds_index *build_index_from_stream_file(
                        goto error;
                }
 
+               /* Set path to stream file. */
+               index_entry->path = file_info->path->str;
+
                ret = init_index_entry(index_entry, ds_file, &props,
                        current_packet_size_bytes, current_packet_offset_bytes);
                if (ret) {
@@ -585,13 +618,8 @@ struct ctf_fs_ds_index *build_index_from_stream_file(
                current_packet_offset_bytes += current_packet_size_bytes;
                BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
                        "next-packet-offset=%jd",
-                       current_packet_offset_bytes - current_packet_size_bytes,
-                       current_packet_offset_bytes);
-
-       } while (iter_status == BT_MSG_ITER_STATUS_OK);
-
-       if (iter_status != BT_MSG_ITER_STATUS_OK) {
-               goto error;
+                       (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes),
+                       (intmax_t) current_packet_offset_bytes);
        }
 
 end:
@@ -612,7 +640,7 @@ struct ctf_fs_ds_file *ctf_fs_ds_file_create(
                bt_logging_level log_level)
 {
        int ret;
-       const size_t page_size = bt_common_get_page_size(log_level);
+       const size_t offset_align = bt_mmap_get_offset_align_size(log_level);
        struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
 
        if (!ds_file) {
@@ -642,7 +670,7 @@ struct ctf_fs_ds_file *ctf_fs_ds_file_create(
                goto error;
        }
 
-       ds_file->mmap_max_len = page_size * 2048;
+       ds_file->mmap_max_len = offset_align * 2048;
 
        goto end;
 
@@ -657,18 +685,19 @@ end:
 
 BT_HIDDEN
 struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
-               struct ctf_fs_ds_file *ds_file)
+               struct ctf_fs_ds_file *ds_file,
+               struct ctf_fs_ds_file_info *file_info)
 {
        struct ctf_fs_ds_index *index;
 
-       index = build_index_from_idx_file(ds_file);
+       index = build_index_from_idx_file(ds_file, file_info);
        if (index) {
                goto end;
        }
 
        BT_COMP_LOGI("Failed to build index from .index file; "
                "falling back to stream indexing.");
-       index = build_index_from_stream_file(ds_file);
+       index = build_index_from_stream_file(ds_file, file_info);
 end:
        return index;
 }
This page took 0.028793 seconds and 4 git commands to generate.