Fix: SIGBUS error on reading past a file's end in mmap'ed region
[babeltrace.git] / plugins / ctf / fs / data-stream.c
1 /*
2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <glib.h>
30 #include <inttypes.h>
31 #include <sys/mman.h>
32 #include <babeltrace/ctf-ir/stream.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include "file.h"
35 #include "metadata.h"
36 #include "../common/notif-iter/notif-iter.h"
37 #include <assert.h>
38
39 #define PRINT_ERR_STREAM ctf_fs->error_fp
40 #define PRINT_PREFIX "ctf-fs-data-stream"
41 #include "print.h"
42
43 BT_HIDDEN
44 void ctf_fs_stream_destroy(struct ctf_fs_stream *stream)
45 {
46 if (stream->file) {
47 ctf_fs_file_destroy(stream->file);
48 }
49
50 if (stream->stream) {
51 BT_PUT(stream->stream);
52 }
53
54 if (stream->notif_iter) {
55 bt_ctf_notif_iter_destroy(stream->notif_iter);
56 }
57
58 g_free(stream);
59 }
60
61 static size_t remaining_mmap_bytes(struct ctf_fs_stream *stream)
62 {
63 return stream->mmap_valid_len - stream->request_offset;
64 }
65
66 static int stream_munmap(struct ctf_fs_stream *stream)
67 {
68 int ret = 0;
69 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
70
71 if (munmap(stream->mmap_addr, stream->mmap_len)) {
72 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
73 stream->mmap_addr, stream->mmap_len,
74 stream->file->path->str, stream->file->fp,
75 strerror(errno));
76 ret = -1;
77 goto end;
78 }
79 end:
80 return ret;
81 }
82
83 static int mmap_next(struct ctf_fs_stream *stream)
84 {
85 int ret = 0;
86 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
87
88 /* Unmap old region */
89 if (stream->mmap_addr) {
90 if (stream_munmap(stream)) {
91 goto error;
92 }
93
94 stream->mmap_offset += stream->mmap_valid_len;
95 stream->request_offset = 0;
96 }
97
98 stream->mmap_valid_len = MIN(stream->file->size - stream->mmap_offset,
99 stream->mmap_max_len);
100 /* Round up to next page, assuming page size being a power of 2. */
101 stream->mmap_len = (stream->mmap_valid_len + ctf_fs->page_size - 1)
102 & ~(ctf_fs->page_size - 1);
103 /* Map new region */
104 stream->mmap_addr = mmap((void *) 0, stream->mmap_len,
105 PROT_READ, MAP_PRIVATE, fileno(stream->file->fp),
106 stream->mmap_offset);
107 if (stream->mmap_addr == MAP_FAILED) {
108 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
109 stream->mmap_len, stream->file->path->str,
110 stream->file->fp, stream->mmap_offset,
111 strerror(errno));
112 goto error;
113 }
114
115 goto end;
116 error:
117 stream_munmap(stream);
118 ret = -1;
119 end:
120 return ret;
121 }
122
123 static enum bt_ctf_notif_iter_medium_status medop_request_bytes(
124 size_t request_sz, uint8_t **buffer_addr,
125 size_t *buffer_sz, void *data)
126 {
127 enum bt_ctf_notif_iter_medium_status status =
128 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
129 struct ctf_fs_stream *stream = data;
130 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
131
132 if (request_sz == 0) {
133 goto end;
134 }
135
136 /* Check if we need an initial memory map */
137 if (!stream->mmap_addr) {
138 if (mmap_next(stream)) {
139 PERR("Cannot memory-map initial region of file \"%s\" (%p)\n",
140 stream->file->path->str, stream->file->fp);
141 goto error;
142 }
143 }
144
145 /* Check if we have at least one memory-mapped byte left */
146 if (remaining_mmap_bytes(stream) == 0) {
147 /* Are we at the end of the file? */
148 if (stream->request_offset >= stream->file->size) {
149 PDBG("Reached end of file \"%s\" (%p)\n",
150 stream->file->path->str, stream->file->fp);
151 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
152 goto end;
153 }
154
155 if (mmap_next(stream)) {
156 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
157 stream->file->path->str, stream->file->fp);
158 goto error;
159 }
160 }
161
162 *buffer_sz = MIN(remaining_mmap_bytes(stream), request_sz);
163 *buffer_addr = ((uint8_t *) stream->mmap_addr) + stream->request_offset;
164 stream->request_offset += *buffer_sz;
165 goto end;
166
167 error:
168 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
169
170 end:
171 return status;
172 }
173
174 static struct bt_ctf_stream *medop_get_stream(
175 struct bt_ctf_stream_class *stream_class, void *data)
176 {
177 struct ctf_fs_stream *fs_stream = data;
178 struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs;
179
180 if (!fs_stream->stream) {
181 int64_t id = bt_ctf_stream_class_get_id(stream_class);
182
183 PDBG("Creating stream out of stream class %" PRId64 "\n", id);
184 fs_stream->stream = bt_ctf_stream_create(stream_class,
185 fs_stream->file->path->str);
186 if (!fs_stream->stream) {
187 PERR("Cannot create stream (stream class %" PRId64 ")\n",
188 id);
189 }
190 }
191
192 return fs_stream->stream;
193 }
194
195 static struct bt_ctf_notif_iter_medium_ops medops = {
196 .request_bytes = medop_request_bytes,
197 .get_stream = medop_get_stream,
198 };
199
200 BT_HIDDEN
201 struct ctf_fs_stream *ctf_fs_stream_create(
202 struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file)
203 {
204 struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1);
205
206 if (!stream) {
207 goto error;
208 }
209
210 stream->file = file;
211 stream->notif_iter = bt_ctf_notif_iter_create(ctf_fs->metadata.trace,
212 ctf_fs->page_size, medops, stream, ctf_fs->error_fp);
213 if (!stream->notif_iter) {
214 goto error;
215 }
216 stream->mmap_max_len = ctf_fs->page_size * 2048;
217 goto end;
218 error:
219 /* Do not touch "borrowed" file. */
220 stream->file = NULL;
221 ctf_fs_stream_destroy(stream);
222 stream = NULL;
223 end:
224 return stream;
225 }
226
227 enum bt_notification_iterator_status ctf_fs_data_stream_get_next_notification(
228 struct ctf_fs_component *ctf_fs,
229 struct bt_notification **notification,
230 size_t stream_id)
231 {
232 enum bt_ctf_notif_iter_status status;
233 enum bt_notification_iterator_status ret;
234 /* FIXME, only iterating on one stream for the moment. */
235 struct ctf_fs_stream *stream = g_ptr_array_index(ctf_fs->streams,
236 stream_id);
237
238 if (stream->end_reached) {
239 status = BT_CTF_NOTIF_ITER_STATUS_EOF;
240 goto end;
241 }
242
243 status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter,
244 notification);
245 if (status != BT_CTF_NOTIF_ITER_STATUS_OK &&
246 status != BT_CTF_NOTIF_ITER_STATUS_EOF) {
247 goto end;
248 }
249
250 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
251 if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
252 *notification = bt_notification_stream_end_create(
253 stream->stream);
254 if (!*notification) {
255 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
256 }
257 status = BT_CTF_NOTIF_ITER_STATUS_OK;
258 stream->end_reached = true;
259 }
260 end:
261 switch (status) {
262 case BT_CTF_NOTIF_ITER_STATUS_EOF:
263 ret = BT_NOTIFICATION_ITERATOR_STATUS_END;
264 break;
265 case BT_CTF_NOTIF_ITER_STATUS_OK:
266 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
267 break;
268 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
269 /*
270 * Should not make it this far as this is medium-specific;
271 * there is nothing for the user to do and it should have been
272 * handled upstream.
273 */
274 assert(0);
275 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
276 /* No argument provided by the user, so don't return INVAL. */
277 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
278 default:
279 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
280 break;
281 }
282 return ret;
283 }
This page took 0.035305 seconds and 4 git commands to generate.