Commit graph

179 commits

Author SHA1 Message Date
William Wilgus
ea33e66021 [BugFix] Buffering.c NULL src for memcpy is UB -- ASAN
No clue if our implementation would suffer the same fate
but the C standard states:
Where an argument declared as size_t n specifies
the length of the array for a function, n can have
the value zero […] pointer arguments on such a call
shall still have valid values, as described in 7.1.4.

Change-Id: Iee0dd9c948c6ad4b0d96309053127ab11111f04c
2023-01-04 20:20:08 -05:00
William Wilgus
f6c719d7ec replace strlcpy with strmemccpy
replace applicable calls to strlcpy with calls to strmemccpy
which null terminates on truncation

in theory the strmemccpy calls should be slightly faster since they
don't traverse the rest of the source string on truncation
but I seriously doubt there is too much of that going on in the code base

Change-Id: Ia0251514e36a6242bbf3f03c5e0df123aba60ed2
2022-11-14 23:56:16 -05:00
Aidan MacDonald
9e93796407 buffering: remove bufgettail/bufcuttail
These operations can only be used in limited circumstances and have
exactly one user. bufgettail especially seems of dubious value; how
often do you need to read N bytes from the end of a file without
changing the file position?

strip_tags() was the only function using them, to strip off ID3v1
and APE tags off the end of buffered tracks. This would save only
32-192 bytes per track -- if the container format uses APE/ID3v1.
It hardly seems worth the effort.

Change-Id: I8fc3c1408517eda6126e75e76d76daea904b50eb
2022-04-20 16:51:49 -04:00
Aidan MacDonald
32f1418c5a buffering: fix buffer overflows with bitmap loading
In some circumstances it was possible for a bitmap to overflow its
buffer and overwrite the next handle. The easiest way to trigger it
is with a highly compressed JPEG that is decoded to a large bitmap.
Because the JPEG file size is used to determine how much to allocate
this would cause an obvious buffer overflow when the JPEG is smaller
than the decoded bitmap. Fix this by using the decoded bitmap size as
the allocation size. Some overhead must be added to deal with JPEGs,
but it will be freed once the image is loaded.

A less obvious possibility is the fact that add_handle() will allow
a handle to be added even if there's not enough space for the entire
allocation. This is generally beneficial because it allows the first
part of a file to be loaded while waiting for space to free up, but
for bitmaps it is not valid because the whole image is loaded at once.
Hence if there is not actually enough space in the buffer, the bitmap
load can again overflow the actual free space and overwrite the next
handle.

The buffering code supports an H_ALLOCALL flag for allocations that
need the free space available immediately, so use it for bitmaps to
avoid that bug.

load_image() had a sketchy-looking check for free space which stopped
me from triggering the bug with simple tests, but since guessing the
free space is obviously a bad idea when the caller *knows* how much
free space there really is, remove that guess and let the caller tell
load_image() the real deal.

Change-Id: If62a58759705d83c16ee5b50f26bcbccc3f6c01f
2022-04-09 15:20:57 +01:00
Aidan MacDonald
7718b24401 buffering: fix signed overflow in next_handle_id()
Not sure what the comment is talking about - signed overflow
is undefined behavior and we don't use -fwrapv or other flags
to make it defined. I can't see how a compiler could abuse it
here, but the overflow is nonetheless easily avoided.

Change-Id: Ibed6d7c0d841db2aa86b9d8ba4c6a0d08c413354
2022-04-09 15:20:57 +01:00
William Wilgus
a6eafc86f8 Remove execute bit from file permissions from recent commit
Change-Id: I415cf235d2b179ae8c58b0427308103be3e00130
2021-03-02 02:10:53 +00:00
Michael Giacomelli
ca09f91f64 Fix deadlocks when trying to buffer large album art.
Internally, buffering tries to load the entire album art file into the
audio buffer, which will fail if the file is larger than the buffer.
Playback.c interprets a file failing to buffer to mean that the buffer
is full, so it waits for more space and tries again.  This results in a
deadlock since the file will never fit.

Change bufopen to return a new error condition when an image file will
not fit on the buffer because it is too large:  ERR_BITMAP_TOO_LARGE.
Note that we arbitrarily set "too large" to be within 64KB of the
entire buffer size or larger, this could be adjusted if needed.

Change audio_load_albumart to pass through error messages from bufopen.

In playback.c, check to see why audio_load_albumart fails.  If it fails
because the file is too large to buffer, simply ignore the file.  If it
fails because the file would fit but the buffer is full, try again
later.

Change-Id: I66799ae26f124b495e1522fce7285332f4cf986f
2020-12-25 17:47:19 +00:00
Michael Sevakis
d35a18f6b4 Buffering: Missed converting one case assuming const handle size.
Must now be h->size, not sizeof (type).

Change-Id: Ia0b1b552a486ddbc28b80542cfa76bed9e7cfdb3
2017-12-19 14:19:39 -05:00
Michael Sevakis
786fbbfa20 Buffering: Get rid of disabled code I have no intention of using
Change-Id: I0e5a20e042291180391b0b0059e44705c256d3e5
2017-12-17 18:49:24 -05:00
Michael Sevakis
dfff938dff Get rid of useless playlist probing and fix up some data types.
Playback checked the files' presence before attempting to buffer
the track. Just get rid of that and save an extra open/close call.
It will find out if the path is bad when the metadata fails.

Fix some size_t/off_t conflation. No need to update plugin version
because no plugin actually uses bufopen().

Change-Id: I3db112449dc0b2eeb91c546f308880ac82494fc7
2017-12-17 16:33:50 -05:00
Michael Sevakis
cd3ea086ec Buffering: Remove statically-sized path buffer from handle struct
Paths are stored after the structure at their actual length plus
any aligment padding. In principle, any type of auxilliary data
could go there.

Change-Id: Ic5487dc4089781b5cc52414d1691ba6d9dc1893c
2017-12-10 14:51:43 -05:00
Michael Sevakis
6ee3b6feee buffering.c: Fix oopses with caching handle pointer
The location of the handle cannot be kept across calls to
shrink_handle() since it may move the structure. The error was
there in one place at the inception, corrected, then reintroduced.

Make shrink_handle() return the new location and use it, which
makes the side effects of the function clearer.

Change-Id: Icae6a0ad6f7bb0d6645b044cccfa4aef88db42ad
2017-12-09 23:45:53 -05:00
Michael Sevakis
8be40746b8 Remove recursion from shrink_buffer()
There's no need for it any longer since the list is now doubly-
linked. As a bonus, stack limits pose no barrier to the length of
the list.

Change-Id: I41c567f946b640ef1e3c2d93da2f5aef9a763c66
2017-12-09 21:57:01 -05:00
Michael Sevakis
c1a01beded Playback: Move internal track list onto buffer
Does away the statically-allocated track list which frees quite
a fair amount of in-RAM size.

There's no compile-time hard track limit.

Recommended TODO (but not right away): Have data small enough use
the handle structure as its buffer data area. Almost the entire
handle structure is unused for simple allocations without any
associated filesystem path.

Change-Id: I74a4561e5a837e049811ac421722ec00dadc0d50
2017-12-09 17:05:59 -05:00
Michael Sevakis
65c6a14e5f Buffering: Switch to a more general handle caching type
It sort of implemented an MRU cache but just kept track of the most
recent access and checked the one after it, otherwise searching from
the beginning.

Implement a true MRU list of all open handles. Handles of the current
track will tend to stay up front for faster access.

Switch to common linked list functions

Use double-linked lists to have insert, remove and move_handle
operations in O(1)-- no more searching to find the previous handle,
which is very often required.

Smaller code too. :)

Change-Id: I9ae8f3f96d225a5d54b94133f499268007274784
2017-05-08 06:24:02 -04:00
Michael Sevakis
eefc7c73e2 Fix some problems with playback crashing
I'm not sure all the situations it affects, to be honest. The fix
aimed to address the strange symptom here:
http://forums.rockbox.org/index.php/topic,50793.0.html

It turns out that ringbuf_add_cross was used when handles were
butted up against one another with the first parameter equal to
the last, which it interprets as being an empty case when it should
be interpreted as full in the context it was used. To fix this,
introduce full/empty variants of ringbuf_add_cross and ringbuf_sub
and use them at the appropriate time.

The other way to address the problem is ensure there's always at
least a space byte between the end of one handle and the start of
another but this make the code a bit trickier to reason about than
using additional function variants.

bufopen() may yield after creating a handle and so do some more
locking so that the buffering thread doesn't mess things up by
moving anything or not seeing the yet-to-be linked-in allocation.

Add alignof() macro to use proper method to get alignment of
struct memory_handle. That should be useful in general anyway.
It's merely defined as __alignof__ but looks nicer.

Change-Id: If21739eaa33a4f6c084a28ee5b3c8fceecfd87ce
2017-04-08 18:32:54 -04:00
Mihail Zenkov
47d6d268c0 Cleanup unused function
Change-Id: I10aac94906607a74f05a687cb3d0029cb6faea6e
2016-04-04 11:06:29 +02:00
Michael Sevakis
5b08f1a5b9 Remove I/O priority. It is harmful when used with the new file code.
HAVE_IO_PRIORITY was defined for native targets with dircache.

It is already effectively disabled for the most part since dircache no
longer lowers its thread's I/O priority. It existed primarily for the
aforementioned configuration.

Change-Id: Ia04935305397ba14df34647c8ea29c2acaea92aa
2014-08-30 14:01:21 -04:00
Michael Sevakis
8375b691e6 buffering.c: Patch up some straggling strlcpy warnings
Originating from 3661581

Some build clients finding their "standard" string.h's that don't declare
strlcpy?

Change-Id: I50d19c7cecf5ae96ee1855f77d3c2e1f42620108
2014-04-03 18:49:16 -04:00
Michael Sevakis
f9d60e14c9 Apparently some builds still need string.h in buffering.c
Change-Id: I99b90ab7e5b7d074b1d2d1de72267f9f2eea975b
2014-04-02 21:07:30 -04:00
Michael Sevakis
36615815bf Buffering: Remove buf_ridx and buf_widx; these data are verbose.
It is trivial to obtain all required information from the allocated
handles without maintaining global indexes. In fact, it is less
complicated and increases general thread safety.

Other miscellaneous changes (some are nice to do at this time due to
required alterations, with some particularly more relevant than others):
* Handle value 0 will no longer be returned as a valid handle but all
failures will still return a negative value. Creates consistency with
buflib and removes the need to explicitly initialize them.

* Linking a new handle is delayed until explicitly
added by the code that called add_handle, keeping it invisible
until every operation succeeds, which is safer thread-wise. If anything
fails, the handle itself may just be abandoned rather than reqiring it
be freed.

* Dump the special handling to slow buffering when the PCM buffer
is low that calls PCM buffer functions. It doesn't seem to help much
of anything these days and it's a bit of a nasty hack to directly
tie those bits together. It can of course be put back (again!) if
there really is a need for it.

* Make data waiters ping the buffering thread more than just once if
the request is taking too long. Somehow I figured out how the requests
could get forgotten about but can't remember why months later after
making the change in my branch. :-)

* Neaten up some code by using (inline) functions and packing down
parameters; remember handle allocation and movement attributes in the
handle itself rather than figuring it out each time they're needed.

Change-Id: Ibf863370da3dd805132fc135e0ad104953365183
Reviewed-on: http://gerrit.rockbox.org/764
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested: Michael Sevakis <jethead71@rockbox.org>
2014-04-03 02:24:03 +02:00
Michael Sevakis
9b990bdab1 SWCODEC Audio: Add some INIT_ATTR's to get a few bytes back.
Change-Id: Ie7b04ecf3b3535e0ed45a6e0e8d81af89e38378e
2013-06-29 22:29:23 -04:00
Michael Sevakis
89b05af496 Fix whitespace
Change-Id: I2072c355f05b6e709a5c179512bc2b71756163a5
2013-06-29 22:29:03 -04:00
Michael Sevakis
0ebfb937aa Fix some lockup caused by handles not being initialized to < 0...
...by default where they would be interpreted as valid but not actually
be which would cause calls to buffering while it was not initialized.

Add BUFFER_EVENT_BUFFER_RESET to inform users of buffering that the
buffer is being reinitialized. Basically, this wraps all the
functionality being provided by three events (...START_PLAYBACK,
RECORDING_EVENT_START, RECORDING_EVENT_STOP) into one for radioart.c,
the only user of those events (perhaps remove them?) and closes some
loopholes.

Change-Id: I99ec46b9b5fb4e36605db5944c60ed986163db3a
2012-05-21 02:28:13 -04:00
Michael Sevakis
5a8f5b8330 Provide a reasonable fix for FS#12093 - Playback hanging after codec/playback rework. Also, get rid of an impossible buffering case (BUF_USED is always less than buffer_len) and remove a buffering API that is not used anywhere and shouldn't be needed (plugin API has to be incompatible).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29849 a1c6a512-1295-4272-9138-f99709370657
2011-05-09 21:19:11 +00:00
Michael Sevakis
c537d5958e Commit FS#12069 - Playback rework - first stages. Gives as thorough as possible a treatment of codec management, track change and metadata logic as possible while maintaining fairly narrow focus and not rewriting everything all at once. Please see the rockbox-dev mail archive on 2011-04-25 (Playback engine rework) for a more thorough manifest of what was addressed. Plugins and codecs become incompatible.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29785 a1c6a512-1295-4272-9138-f99709370657
2011-04-27 03:08:23 +00:00
Michael Sevakis
4823b2b1c0 Buffering should truncate if read() returns 0 since it's not a valid return there as there should be data left to read. The loop wouldn't break until there was a message in the queue. I just experienced the case with crosslinked files and read stopped making progress, returning 0 each time it was called.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29626 a1c6a512-1295-4272-9138-f99709370657
2011-03-21 15:57:07 +00:00
Michael Sevakis
56dd75d204 Purge buffer and codec APIs existing exclusively in support of mpa.codec and fix that to not require them: buf_get_offset and ci.advance_buffer_loc. Sort APIs; everything must become incompatible. :(
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29595 a1c6a512-1295-4272-9138-f99709370657
2011-03-16 05:38:37 +00:00
Michael Sevakis
7cac18f94b Use ringbuf_add in buffering when incrementing for initial allocation of non-wrapping data. The result of the shortcut would have been wrong if the handle used space exactly to the end of the buffer since buf_widx wouldn't have been properly wrapped to index 0.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29578 a1c6a512-1295-4272-9138-f99709370657
2011-03-13 11:56:51 +00:00
Michael Sevakis
05e180a130 Do the ridx > widx check where it should be done. A small rebuffering request must completely succeed before buffer can later shrink a handle or else it must reset the handle.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29491 a1c6a512-1295-4272-9138-f99709370657
2011-03-02 06:24:50 +00:00
Michael Sevakis
64647f3403 buffering: Unusual cases when a handle ridx is briefly seeked ahead of widx need to be handled properly. In the best case, buffer useful would be wrong and in the worst, a packet audio move_handle delta would be quite incorrect, causing the handle to be moved too far.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29490 a1c6a512-1295-4272-9138-f99709370657
2011-03-02 04:41:29 +00:00
Michael Sevakis
b15aa47c56 All kernel objects in code shared amongs targets (core, plugins, codecs) should be declared SHAREDBSS_ATTR as any core could potentially touch them even though they seem only to involve threads on one core. The exception is target code for particular CPUs where proper allocation is fixed. playlist.c was a little odd too-- use one mutex for the current playlist and a separate one for created playlists (still pondering the necessity of more than one).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29305 a1c6a512-1295-4272-9138-f99709370657
2011-02-14 11:27:45 +00:00
Michael Sevakis
8f14357064 Code police buffering.c a little - use already predominant style - shorted lines over 80 cols.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29304 a1c6a512-1295-4272-9138-f99709370657
2011-02-14 09:18:58 +00:00
Michael Sevakis
6938255b6b Buffering: tin cup. Update threading structure and handle rebuffer more reliably on buffer thread using a single message send.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29303 a1c6a512-1295-4272-9138-f99709370657
2011-02-14 08:36:29 +00:00
Michael Sevakis
0fde635fb0 Leave a gap between all handles because ringbuf_add_cross interprets equal pointers as empty, corruption guard check could fail to detect overlap if buffering ran right up to the next handle and it gets asked to buffer again before freeing the following handles (adds a byte on average). Storage alignment on handle reset must at times avoid alignment increments if after a stopped rebuffer, the handle was shrunk too close to the next one or the reading position in a prior rebuffer.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29302 a1c6a512-1295-4272-9138-f99709370657
2011-02-14 02:14:26 +00:00
Michael Sevakis
efdf49668e Needed to do a few more things to have r29291 correct.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29294 a1c6a512-1295-4272-9138-f99709370657
2011-02-13 11:02:37 +00:00
Michael Sevakis
b474d0d98f Change add_handle to never have side effects on the buffer if it fails. It actually seems ok and I'm not sure if's responsible for anything, but it's more sane and keeps buffer_handle from regressing buf_widx later if buffering cur_handle.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29291 a1c6a512-1295-4272-9138-f99709370657
2011-02-13 10:44:13 +00:00
Michael Sevakis
50c727ce7a Oops. Put back some changes to go only with others.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29289 a1c6a512-1295-4272-9138-f99709370657
2011-02-12 12:33:43 +00:00
Michael Sevakis
3c81bf0c13 Fix move_handle in buffering. Calculating wraps by buffer_len - 1 is incorrect. Switch handle movement to memmove calls exclusively.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29288 a1c6a512-1295-4272-9138-f99709370657
2011-02-12 12:18:09 +00:00
Michael Sevakis
88d91faba5 buffering: Don't execute move-handle-ony case if handle is of metadata type (atomic) and must be kept fully buffered. Manage handle corruption guard and handle buffering with one set of logic which allows reading of the maximum amount of data without overflow. 'FIXME' regarding handle corruption guard is really part of expected operation when thread that does the handle closing hasn't yet performed the delegated task before rebuffering starts.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29270 a1c6a512-1295-4272-9138-f99709370657
2011-02-10 05:56:21 +00:00
Thomas Martitz
86cab2e27a Disable buffering codecs (and code generally) on RaaA.
It's not useful to do it since you need to write back the code to disk to be able to load it from memory, it also requires writing to an executable directory.
Keep it for the simulator for the sake of simulating.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29261 a1c6a512-1295-4272-9138-f99709370657
2011-02-09 20:27:23 +00:00
Thomas Martitz
f577a6a22c Embedded album art support in MP3/ID3v2 tags.
- Support is limited to non-desync jpeg in id3v2 tags. Other formats (hopefully) follow in the future.
- Embedded album art takes precedence over files in album art files.
- No additional buffers are used, the jpeg is read directly from the audio file.

Flyspray: FS#11216
Author: Yoshihisa Uchida and I

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29259 a1c6a512-1295-4272-9138-f99709370657
2011-02-09 20:13:13 +00:00
Michael Sevakis
0d902c8c54 Buffering should align itself and not rely on buffering_reset parameters when storage alignment matters so that wrapped reads maintain alignment.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29258 a1c6a512-1295-4272-9138-f99709370657
2011-02-09 09:30:09 +00:00
Michael Sevakis
19ea72ff63 buffering: Fix a case that could allow widx to fully wrap to ridx and overflow the ringbuffer.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29257 a1c6a512-1295-4272-9138-f99709370657
2011-02-09 08:44:37 +00:00
Andree Buschmann
6b476e7bec Roll back unintentionally submitted file.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29200 a1c6a512-1295-4272-9138-f99709370657
2011-02-03 08:36:34 +00:00
Andree Buschmann
7345ac124e Submit FS#11918: Add 2 more codec types to be able to differentiate between AAC / AAC-HE and MPC SV7 / SV8. Additionally handle ATARI soundfiles in get_codec_base_type() as intended.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29199 a1c6a512-1295-4272-9138-f99709370657
2011-02-03 08:28:23 +00:00
Andree Buschmann
5d849a963e Clean up multiple definitions of RAM size. Remove -DMEM (make) and MEM (code), use the already defined MEMORYSIZE instead.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29189 a1c6a512-1295-4272-9138-f99709370657
2011-02-02 17:43:32 +00:00
Andree Buschmann
9c00d1a643 Fix FS#11586. Corrects rebuffering behaviour which did not allow to play several m4a files. Thanks to Magnus Holmgren.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27950 a1c6a512-1295-4272-9138-f99709370657
2010-08-30 22:35:32 +00:00
Thomas Martitz
ed033c0a2a Oops, committed before finishing the removal of "#include "memory.h""
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27790 a1c6a512-1295-4272-9138-f99709370657
2010-08-12 14:01:28 +00:00
Bertrik Sikken
5565a28c7d Remove some redundant #include's
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27183 a1c6a512-1295-4272-9138-f99709370657
2010-06-29 20:13:22 +00:00