]> git.sesse.net Git - vlc/blob - src/misc/block.c
Fixed a potential integer overflow in block_Alloc().
[vlc] / src / misc / block.c
1 /*****************************************************************************
2  * block.c: Data blocks management functions
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VLC authors and VideoLAN
5  * Copyright (C) 2007-2009 RĂ©mi Denis-Courmont
6  *
7  * Authors: Laurent Aimar <fenrir@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <sys/stat.h>
33 #include <assert.h>
34 #include <errno.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38
39 #include "vlc_block.h"
40
41 /**
42  * @section Block handling functions.
43  */
44
45 /**
46  * Internal state for heap block.
47   */
48 struct block_sys_t
49 {
50     block_t     self;
51     size_t      i_allocated_buffer;
52     uint8_t     p_allocated_buffer[];
53 };
54
55 #ifndef NDEBUG
56 static void BlockNoRelease( block_t *b )
57 {
58     fprintf( stderr, "block %p has no release callback! This is a bug!\n", b );
59     abort();
60 }
61 #endif
62
63 void block_Init( block_t *restrict b, void *buf, size_t size )
64 {
65     /* Fill all fields to their default */
66     b->p_next = NULL;
67     b->p_buffer = buf;
68     b->i_buffer = size;
69     b->i_flags = 0;
70     b->i_nb_samples = 0;
71     b->i_pts =
72     b->i_dts = VLC_TS_INVALID;
73     b->i_length = 0;
74 #ifndef NDEBUG
75     b->pf_release = BlockNoRelease;
76 #endif
77 }
78
79 static void BlockRelease( block_t *p_block )
80 {
81     free( p_block );
82 }
83
84 static void BlockMetaCopy( block_t *restrict out, const block_t *in )
85 {
86     out->p_next    = in->p_next;
87     out->i_nb_samples = in->i_nb_samples;
88     out->i_dts     = in->i_dts;
89     out->i_pts     = in->i_pts;
90     out->i_flags   = in->i_flags;
91     out->i_length  = in->i_length;
92 }
93
94 /* Memory alignment (must be a multiple of sizeof(void*) and a power of two) */
95 #define BLOCK_ALIGN        16
96 /* Initial reserved header and footer size (must be multiple of alignment) */
97 #define BLOCK_PADDING      32
98 /* Maximum size of reserved footer before we release with realloc() */
99 #define BLOCK_WASTE_SIZE   2048
100
101 block_t *block_Alloc( size_t i_size )
102 {
103     /* We do only one malloc
104      * TODO: bench if doing 2 malloc but keeping a pool of buffer is better
105      * 2 * BLOCK_PADDING -> pre + post padding
106      */
107     block_sys_t *p_sys;
108     uint8_t *buf;
109 #define ALIGN(x) (((x) + BLOCK_ALIGN - 1) & ~(BLOCK_ALIGN - 1))
110 #if 0 /*def HAVE_POSIX_MEMALIGN */
111     /* posix_memalign(,16,) is much slower than malloc() on glibc.
112      * -- Courmisch, September 2009, glibc 2.5 & 2.9 */
113     const size_t i_alloc = ALIGN(sizeof(*p_sys)) + (2 * BLOCK_PADDING)
114                          + ALIGN(i_size);
115     if( unlikely(i_alloc <= i_size) )
116         return NULL;
117     void *ptr;
118
119     if( posix_memalign( &ptr, BLOCK_ALIGN, i_alloc ) )
120         return NULL;
121
122     p_sys = ptr;
123     buf = p_sys->p_allocated_buffer + (-sizeof(*p_sys) & (BLOCK_ALIGN - 1));
124
125 #else
126     const size_t i_alloc = sizeof(*p_sys) + BLOCK_ALIGN + (2 * BLOCK_PADDING)
127                          + ALIGN(i_size);
128     if( unlikely(i_alloc <= i_size) )
129         return NULL;
130
131     p_sys = malloc( i_alloc );
132     if( p_sys == NULL )
133         return NULL;
134
135     buf = (void *)ALIGN((uintptr_t)p_sys->p_allocated_buffer);
136
137 #endif
138     buf += BLOCK_PADDING;
139
140     block_Init( &p_sys->self, buf, i_size );
141     p_sys->self.pf_release    = BlockRelease;
142     /* Fill opaque data */
143     p_sys->i_allocated_buffer = i_alloc - sizeof(*p_sys);
144
145     return &p_sys->self;
146 }
147
148 block_t *block_Realloc( block_t *p_block, ssize_t i_prebody, size_t i_body )
149 {
150     size_t requested = i_prebody + i_body;
151
152     /* Corner case: empty block requested */
153     if( i_prebody <= 0 && i_body <= (size_t)(-i_prebody) )
154     {
155         block_Release( p_block );
156         return NULL;
157     }
158
159     if( p_block->pf_release != BlockRelease )
160     {
161         /* Special case when pf_release if overloaded
162          * TODO if used one day, then implement it in a smarter way */
163         block_t *p_dup = block_Duplicate( p_block );
164         block_Release( p_block );
165         if( !p_dup )
166             return NULL;
167
168         p_block = p_dup;
169     }
170
171     block_sys_t *p_sys = (block_sys_t *)p_block;
172     uint8_t *p_start = p_sys->p_allocated_buffer;
173     uint8_t *p_end = p_sys->p_allocated_buffer + p_sys->i_allocated_buffer;
174
175     assert( p_block->p_buffer + p_block->i_buffer <= p_end );
176     assert( p_block->p_buffer >= p_start );
177
178     /* Corner case: the current payload is discarded completely */
179     if( i_prebody <= 0 && p_block->i_buffer <= (size_t)-i_prebody )
180          p_block->i_buffer = 0; /* discard current payload */
181     if( p_block->i_buffer == 0 )
182     {
183         size_t available = p_end - p_start;
184
185         if( requested <= available )
186         {   /* Enough room: recycle buffer */
187             size_t extra = available - requested;
188
189             p_block->p_buffer = p_start + (extra / 2);
190             p_block->i_buffer = requested;
191             return p_block;
192         }
193         /* Not enough room: allocate a new buffer */
194         block_t *p_rea = block_Alloc( requested );
195         if( p_rea )
196             BlockMetaCopy( p_rea, p_block );
197         block_Release( p_block );
198         return p_rea;
199     }
200
201     /* First, shrink payload */
202
203     /* Pull payload start */
204     if( i_prebody < 0 )
205     {
206         assert( p_block->i_buffer >= (size_t)-i_prebody );
207         p_block->p_buffer -= i_prebody;
208         p_block->i_buffer += i_prebody;
209         i_body += i_prebody;
210         i_prebody = 0;
211     }
212
213     /* Trim payload end */
214     if( p_block->i_buffer > i_body )
215         p_block->i_buffer = i_body;
216
217     /* Second, reallocate the buffer if we lack space. This is done now to
218      * minimize the payload size for memory copy. */
219     assert( i_prebody >= 0 );
220     if( (size_t)(p_block->p_buffer - p_start) < (size_t)i_prebody
221      || (size_t)(p_end - p_block->p_buffer) < i_body )
222     {
223         block_t *p_rea = block_Alloc( requested );
224         if( p_rea )
225         {
226             BlockMetaCopy( p_rea, p_block );
227             p_rea->p_buffer += i_prebody;
228             p_rea->i_buffer -= i_prebody;
229             memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
230         }
231         block_Release( p_block );
232         if( p_rea == NULL )
233             return NULL;
234         p_block = p_rea;
235     }
236     else
237     /* We have a very large reserved footer now? Release some of it.
238      * XXX it might not preserve the alignment of p_buffer */
239     if( p_end - (p_block->p_buffer + i_body) > BLOCK_WASTE_SIZE )
240     {
241         block_t *p_rea = block_Alloc( requested );
242         if( p_rea )
243         {
244             BlockMetaCopy( p_rea, p_block );
245             p_rea->p_buffer += i_prebody;
246             p_rea->i_buffer -= i_prebody;
247             memcpy( p_rea->p_buffer, p_block->p_buffer, p_block->i_buffer );
248             block_Release( p_block );
249             p_block = p_rea;
250         }
251     }
252
253     /* NOTE: p_start and p_end are corrupted from this point */
254
255     /* Third, expand payload */
256
257     /* Push payload start */
258     if( i_prebody > 0 )
259     {
260         p_block->p_buffer -= i_prebody;
261         p_block->i_buffer += i_prebody;
262         i_body += i_prebody;
263         i_prebody = 0;
264     }
265
266     /* Expand payload to requested size */
267     p_block->i_buffer = i_body;
268
269     return p_block;
270 }
271
272
273 typedef struct
274 {
275     block_t  self;
276     void    *mem;
277 } block_heap_t;
278
279 static void block_heap_Release (block_t *self)
280 {
281     block_heap_t *block = (block_heap_t *)self;
282
283     free (block->mem);
284     free (block);
285 }
286
287 /**
288  * Creates a block from a heap allocation.
289  * This is provided by LibVLC so that manually heap-allocated blocks can safely
290  * be deallocated even after the origin plugin has been unloaded from memory.
291  *
292  * When block_Release() is called, VLC will free() the specified pointer.
293  *
294  * @param ptr base address of the heap allocation (will be free()'d)
295  * @param addr base address of the useful buffer data
296  * @param length bytes length of the useful buffer data
297  * @return NULL in case of error (ptr free()'d in that case), or a valid
298  * block_t pointer.
299  */
300 block_t *block_heap_Alloc (void *ptr, void *addr, size_t length)
301 {
302     block_heap_t *block = malloc (sizeof (*block));
303     if (block == NULL)
304     {
305         free (addr);
306         return NULL;
307     }
308
309     block_Init (&block->self, (uint8_t *)addr, length);
310     block->self.pf_release = block_heap_Release;
311     block->mem = ptr;
312     return &block->self;
313 }
314
315 #ifdef HAVE_MMAP
316 # include <sys/mman.h>
317
318 typedef struct block_mmap_t
319 {
320     block_t     self;
321     void       *base_addr;
322     size_t      length;
323 } block_mmap_t;
324
325 static void block_mmap_Release (block_t *block)
326 {
327     block_mmap_t *p_sys = (block_mmap_t *)block;
328
329     munmap (p_sys->base_addr, p_sys->length);
330     free (p_sys);
331 }
332
333 /**
334  * Creates a block from a virtual address memory mapping (mmap).
335  * This is provided by LibVLC so that mmap blocks can safely be deallocated
336  * even after the allocating plugin has been unloaded from memory.
337  *
338  * @param addr base address of the mapping (as returned by mmap)
339  * @param length length (bytes) of the mapping (as passed to mmap)
340  * @return NULL if addr is MAP_FAILED, or an error occurred (in the later
341  * case, munmap(addr, length) is invoked before returning).
342  */
343 block_t *block_mmap_Alloc (void *addr, size_t length)
344 {
345     if (addr == MAP_FAILED)
346         return NULL;
347
348     block_mmap_t *block = malloc (sizeof (*block));
349     if (block == NULL)
350     {
351         munmap (addr, length);
352         return NULL;
353     }
354
355     block_Init (&block->self, (uint8_t *)addr, length);
356     block->self.pf_release = block_mmap_Release;
357     block->base_addr = addr;
358     block->length = length;
359     return &block->self;
360 }
361 #else
362 block_t *block_mmap_Alloc (void *addr, size_t length)
363 {
364     (void)addr; (void)length; return NULL;
365 }
366 #endif
367
368
369 #ifdef WIN32
370 # include <io.h>
371 # ifdef UNDER_CE
372 #  define _get_osfhandle(a) ((long) (a))
373 # endif
374
375 static
376 ssize_t pread (int fd, void *buf, size_t count, off_t offset)
377 {
378     HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
379     if (handle == INVALID_HANDLE_VALUE)
380         return -1;
381
382     OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32);
383     DWORD written;
384     /* This braindead API will override the file pointer even if we specify
385      * an explicit read offset... So do not expect this to mix well with
386      * regular read() calls. */
387     if (ReadFile (handle, buf, count, &written, &olap))
388         return written;
389     return -1;
390 }
391 #elif !defined( HAVE_PREAD )
392 static
393 ssize_t pread(int fd, const void * buf, size_t size, off_t offset) {
394     off_t offs0;
395     ssize_t rd;
396     if ((offs0 = lseek(fd, 0, SEEK_CUR)) == (off_t)-1) return -1;
397     if (lseek(fd, offset, SEEK_SET) == (off_t)-1) return -1;
398     rd = read(fd, (void *)buf, size);
399     if (lseek(fd, offs0, SEEK_SET) == (off_t)-1) return -1;
400     return rd;
401 }
402
403 static
404 ssize_t pwrite(int fd, const void * buf, size_t size, off_t offset) {
405     off_t offs0;
406     ssize_t wr;
407     if ((offs0 = lseek(fd, 0, SEEK_CUR)) == (off_t)-1) return -1;
408     if (lseek(fd, offset, SEEK_SET) == (off_t)-1) return -1;
409     wr = write(fd, (void *)buf, size);
410     if (lseek(fd, offs0, SEEK_SET) == (off_t)-1) return -1;
411     return wr;
412 }
413 #endif
414
415 /**
416  * Loads a file into a block of memory. If possible a private file mapping is
417  * created. Otherwise, the file is read normally. On 32-bits platforms, this
418  * function will not work for very large files, due to memory space
419  * constraints. Cancellation point.
420  *
421  * @param fd file descriptor to load from
422  * @return a new block with the file content at p_buffer, and file length at
423  * i_buffer (release it with block_Release()), or NULL upon error (see errno).
424  */
425 block_t *block_File (int fd)
426 {
427     size_t length;
428     struct stat st;
429
430     /* First, get the file size */
431     if (fstat (fd, &st))
432         return NULL;
433
434     /* st_size is meaningful for regular files, shared memory and typed memory.
435      * It's also meaning for symlinks, but that's not possible with fstat().
436      * In other cases, it's undefined, and we should really not go further. */
437 #ifndef S_TYPEISSHM
438 # define S_TYPEISSHM( buf ) (0)
439 #endif
440     if (S_ISDIR (st.st_mode))
441     {
442         errno = EISDIR;
443         return NULL;
444     }
445     if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
446     {
447         errno = ESPIPE;
448         return NULL;
449     }
450
451     /* Prevent an integer overflow in mmap() and malloc() */
452     if (st.st_size >= SIZE_MAX)
453     {
454         errno = ENOMEM;
455         return NULL;
456     }
457     length = (size_t)st.st_size;
458
459 #ifdef HAVE_MMAP
460     if (length > 0)
461     {
462         void *addr;
463
464         addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
465         if (addr != MAP_FAILED)
466             return block_mmap_Alloc (addr, length);
467     }
468 #endif
469
470     /* If mmap() is not implemented by the OS _or_ the filesystem... */
471     block_t *block = block_Alloc (length);
472     if (block == NULL)
473         return NULL;
474     block_cleanup_push (block);
475
476     for (size_t i = 0; i < length;)
477     {
478         ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
479         if (len == -1)
480         {
481             block_Release (block);
482             block = NULL;
483             break;
484         }
485         i += len;
486     }
487     vlc_cleanup_pop ();
488     return block;
489 }
490
491 /**
492  * @section Thread-safe block queue functions
493  */
494
495 /**
496  * Internal state for block queues
497  */
498 struct block_fifo_t
499 {
500     vlc_mutex_t         lock;                         /* fifo data lock */
501     vlc_cond_t          wait;      /**< Wait for data */
502     vlc_cond_t          wait_room; /**< Wait for queue depth to shrink */
503
504     block_t             *p_first;
505     block_t             **pp_last;
506     size_t              i_depth;
507     size_t              i_size;
508     bool          b_force_wake;
509 };
510
511 block_fifo_t *block_FifoNew( void )
512 {
513     block_fifo_t *p_fifo = malloc( sizeof( block_fifo_t ) );
514     if( !p_fifo )
515         return NULL;
516
517     vlc_mutex_init( &p_fifo->lock );
518     vlc_cond_init( &p_fifo->wait );
519     vlc_cond_init( &p_fifo->wait_room );
520     p_fifo->p_first = NULL;
521     p_fifo->pp_last = &p_fifo->p_first;
522     p_fifo->i_depth = p_fifo->i_size = 0;
523     p_fifo->b_force_wake = false;
524
525     return p_fifo;
526 }
527
528 void block_FifoRelease( block_fifo_t *p_fifo )
529 {
530     block_FifoEmpty( p_fifo );
531     vlc_cond_destroy( &p_fifo->wait_room );
532     vlc_cond_destroy( &p_fifo->wait );
533     vlc_mutex_destroy( &p_fifo->lock );
534     free( p_fifo );
535 }
536
537 void block_FifoEmpty( block_fifo_t *p_fifo )
538 {
539     block_t *block;
540
541     vlc_mutex_lock( &p_fifo->lock );
542     block = p_fifo->p_first;
543     if (block != NULL)
544     {
545         p_fifo->i_depth = p_fifo->i_size = 0;
546         p_fifo->p_first = NULL;
547         p_fifo->pp_last = &p_fifo->p_first;
548     }
549     vlc_cond_broadcast( &p_fifo->wait_room );
550     vlc_mutex_unlock( &p_fifo->lock );
551
552     while (block != NULL)
553     {
554         block_t *buf;
555
556         buf = block->p_next;
557         block_Release (block);
558         block = buf;
559     }
560 }
561
562 /**
563  * Wait until the FIFO gets below a certain size (if needed).
564  *
565  * Note that if more than one thread writes to the FIFO, you cannot assume that
566  * the FIFO is actually below the requested size upon return (since another
567  * thread could have refilled it already). This is typically not an issue, as
568  * this function is meant for (relaxed) congestion control.
569  *
570  * This function may be a cancellation point and it is cancel-safe.
571  *
572  * @param fifo queue to wait on
573  * @param max_depth wait until the queue has no more than this many blocks
574  *                  (use SIZE_MAX to ignore this constraint)
575  * @param max_size wait until the queue has no more than this many bytes
576  *                  (use SIZE_MAX to ignore this constraint)
577  * @return nothing.
578  */
579 void block_FifoPace (block_fifo_t *fifo, size_t max_depth, size_t max_size)
580 {
581     vlc_testcancel ();
582
583     vlc_mutex_lock (&fifo->lock);
584     while ((fifo->i_depth > max_depth) || (fifo->i_size > max_size))
585     {
586          mutex_cleanup_push (&fifo->lock);
587          vlc_cond_wait (&fifo->wait_room, &fifo->lock);
588          vlc_cleanup_pop ();
589     }
590     vlc_mutex_unlock (&fifo->lock);
591 }
592
593 /**
594  * Immediately queue one block at the end of a FIFO.
595  * @param fifo queue
596  * @param block head of a block list to queue (may be NULL)
597  * @return total number of bytes appended to the queue
598  */
599 size_t block_FifoPut( block_fifo_t *p_fifo, block_t *p_block )
600 {
601     size_t i_size = 0, i_depth = 0;
602     block_t *p_last;
603
604     if (p_block == NULL)
605         return 0;
606     for (p_last = p_block; ; p_last = p_last->p_next)
607     {
608         i_size += p_last->i_buffer;
609         i_depth++;
610         if (!p_last->p_next)
611             break;
612     }
613
614     vlc_mutex_lock (&p_fifo->lock);
615     *p_fifo->pp_last = p_block;
616     p_fifo->pp_last = &p_last->p_next;
617     p_fifo->i_depth += i_depth;
618     p_fifo->i_size += i_size;
619     /* We queued at least one block: wake up one read-waiting thread */
620     vlc_cond_signal( &p_fifo->wait );
621     vlc_mutex_unlock( &p_fifo->lock );
622
623     return i_size;
624 }
625
626 void block_FifoWake( block_fifo_t *p_fifo )
627 {
628     vlc_mutex_lock( &p_fifo->lock );
629     if( p_fifo->p_first == NULL )
630         p_fifo->b_force_wake = true;
631     vlc_cond_broadcast( &p_fifo->wait );
632     vlc_mutex_unlock( &p_fifo->lock );
633 }
634
635 /**
636  * Dequeue the first block from the FIFO. If necessary, wait until there is
637  * one block in the queue. This function is (always) cancellation point.
638  *
639  * @return a valid block, or NULL if block_FifoWake() was called.
640  */
641 block_t *block_FifoGet( block_fifo_t *p_fifo )
642 {
643     block_t *b;
644
645     vlc_testcancel( );
646
647     vlc_mutex_lock( &p_fifo->lock );
648     mutex_cleanup_push( &p_fifo->lock );
649
650     /* Remember vlc_cond_wait() may cause spurious wakeups
651      * (on both Win32 and POSIX) */
652     while( ( p_fifo->p_first == NULL ) && !p_fifo->b_force_wake )
653         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
654
655     vlc_cleanup_pop();
656     b = p_fifo->p_first;
657
658     p_fifo->b_force_wake = false;
659     if( b == NULL )
660     {
661         /* Forced wakeup */
662         vlc_mutex_unlock( &p_fifo->lock );
663         return NULL;
664     }
665
666     p_fifo->p_first = b->p_next;
667     p_fifo->i_depth--;
668     p_fifo->i_size -= b->i_buffer;
669
670     if( p_fifo->p_first == NULL )
671     {
672         p_fifo->pp_last = &p_fifo->p_first;
673     }
674
675     /* We don't know how many threads can queue new packets now. */
676     vlc_cond_broadcast( &p_fifo->wait_room );
677     vlc_mutex_unlock( &p_fifo->lock );
678
679     b->p_next = NULL;
680     return b;
681 }
682
683 /**
684  * Peeks the first block in the FIFO.
685  * If necessary, wait until there is one block.
686  * This function is (always) a cancellation point.
687  *
688  * @warning This function leaves the block in the FIFO.
689  * You need to protect against concurrent threads who could dequeue the block.
690  * Preferrably, there should be only one thread reading from the FIFO.
691  *
692  * @return a valid block.
693  */
694 block_t *block_FifoShow( block_fifo_t *p_fifo )
695 {
696     block_t *b;
697
698     vlc_testcancel( );
699
700     vlc_mutex_lock( &p_fifo->lock );
701     mutex_cleanup_push( &p_fifo->lock );
702
703     while( p_fifo->p_first == NULL )
704         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
705
706     b = p_fifo->p_first;
707
708     vlc_cleanup_run ();
709     return b;
710 }
711
712 /* FIXME: not thread-safe */
713 size_t block_FifoSize( const block_fifo_t *p_fifo )
714 {
715     return p_fifo->i_size;
716 }
717
718 /* FIXME: not thread-safe */
719 size_t block_FifoCount( const block_fifo_t *p_fifo )
720 {
721     return p_fifo->i_depth;
722 }