Commit Graph

161 Commits

Author SHA1 Message Date
Wim Taymans
1ae4374ccf Fix compilation with -Werror=float-conversion
Better make the conversions explicit so that we don't get any surprises.

Fixes #4065
2024-06-18 12:17:56 +02:00
Diego Viola
7410755c03 Fix typos
found them with codespell.

Signed-off-by: Diego Viola <diego.viola@gmail.com>
2024-05-22 09:19:34 +02:00
Pauli Virtanen
a63aa6329b spa: fix integer overflows etc. in spa_pod_compare_value
Add macro SPA_CMP to do 3-way comparisons safely, and use it to avoid
signed integer overflows.

Fix also float/double comparisons (previously 0.1 == 0.8 since cast to
return type int).

Fix Id/Bool comparisons so they can return negative value.
2024-05-20 09:11:25 +00:00
Pauli Virtanen
22ac5f85cc json: fix high surrogate escapes
Surrogate escapes must add not or 0x10000, as the specified bits go up
0xfffff.
2024-05-05 15:15:54 +03:00
Pauli Virtanen
eea18a8a8a test: fix loading json test data
Multiline results were not loaded correctly.
2024-05-05 09:32:43 +00:00
Barnabás Pőcze
d8da15c4ad test: add two minimal tests for pw_strv_parse() 2024-04-27 15:21:05 +02:00
Wim Taymans
9bb5780cc2 meta: add explicit sync metadata and data type
Change the GenericFd data type to SyncObj. It's probably better to
explicitly state the data type than to make something generic. Otherwise
we would need to transfer the specific fd type somewhere else and there
is no room for that in the buffer and the the metadata is not a good idea
either because it can be modified and corrupted at runtime.

Add the SyncTimeline metadata. This contains 2 points on two timelines
(SyncObj datas in the buffer). The buffer can be accessed when the
acquire_point is signaled on the timeline and when the buffer
can be released, the release_point on the timeline should be signaled.
2024-04-25 09:55:19 +02:00
Wim Taymans
20b52d2082 keys: add and use some loop keys 2024-04-23 11:49:12 +02:00
Wim Taymans
e8ac4e6a34 spa: add SPA_IO_AsyncBuffers
This structure has 2 io_buffers. Readers and writers operate on different
io_buffers to implement an asynchronous transfer.
2024-04-17 16:18:04 +02:00
Wim Taymans
67aafec8ab node: add an event to enumerate the peer port params
This can be used by nodes to filter or proxy the peer params.

Add test for new peer_enum_params event
2024-04-17 16:17:50 +02:00
Wim Taymans
f3fe20bdde buffer: add GenericFd memory type
Add a Generic fd type. These could be eventfd or timerfd, the meaning
can depend on extra metadata.
2024-04-02 12:28:09 +02:00
Wim Taymans
d4581755e6 spa: Improve JSON error reporting
Add struct spa_error_location that holds information about some parsing
context such as the line and column number, error and line fragment
with the error.

Make spa_json_get_error() fill in the spa_error_location instead. Add
some error codes to the error state and use this to add a parsing reason
to the location.

Add a debug function to log the error location in a nice way. Also
add a FILE based debug context to log to any FILE.

Replace pw_properties_check_string() with
pw_properties_update_string_checked() and add
pw_properties_new_string_checked(). The check string behaviour can still
be done by setting props to NULL but the main purpose is to be able to
avoid parsing the json file twice in the future.

When using the old pw_properties_update_string(), log a warning to the
log when we fail to parse the complete string.

Use the new checked functions and the debug functions to report about
parsing errors in the tools and conf parsing.

This gives errors like:

```
> pw-loopback --playback-props '{ foo =  [ f : g ] }'
error: syntax error in --playback-props: Invalid array separator
line:      1 | { foo =  [ f : g ] }
col:      14 |              ^
```
2024-03-27 15:42:29 +01:00
Wim Taymans
96fb63dfa1 test: fix some compiler warnings 2024-03-26 10:23:37 +01:00
Pauli Virtanen
3775e4e624 spa: json: check object key-value pair parity during parsing
Check each object key is associated with value.  Disallow object or
array valued keys.

Add flag tracking whether the parser is at global top-level or not, as
there we may either be in object context or in a single-value context.

Save depth=0 array flag bit in state, so that spa_json_next preserves
its complete state across calls. The higher-depth flag bits can be in
temporary stack as they are not needed across calls.
2024-03-25 20:49:25 +02:00
Pauli Virtanen
921c8b99db spa: json: allow only ascii in bare values
Control characters probably are an error. We also are not validating any
utf8 here, so disallow bare utf8 too --- one likely should use strings
for such content anyway as spaces are not allowed otherwise.
2024-03-25 20:49:25 +02:00
Pauli Virtanen
e63e8b8a37 test: add "strict" JSON tests
Adapt an existing JSON parsing test suite from
https://github.com/nst/JSONTestSuite for SPA JSON.
2024-03-25 20:49:25 +02:00
Pauli Virtanen
31e5823010 spa: json: more parsing errors & add spa_json_get_error
Disallow = and : as bare items in [] containers, as that likely is
"[ { foo = bar } ]" mistyped as "[ foo = bar ]".

Disallow nesting errors, eg. "[ foo bar" or "[ foo bar }".

Fix handling of ", \ and # in bare strings.

Fix ignoring trailing comments.

Add a fixed-size stack (128 levels) to the tokenizer, so that it can
check these at levels below its depth.

When the tokenizer encounters an error, make it and its parents enter
error state where no further input will be processed. This allows caller
to check for parse errors later as convenient.

The error state can be queried using spa_json_get_error, which also
looks up the error line/column position.
2024-03-25 20:49:25 +02:00
Pauli Virtanen
41d7762f8b spa: json: fix string function usage in spa_json_parse_float/int
spa_json_parse_float/int receive non nul-terminated string, so calling
string functions assuming nul-termination is invalid.

Fix by copying data to a buffer before doing parsing.
2024-03-25 19:22:57 +02:00
Wim Taymans
1b6370ace1 meson: fix compilation when spa-plugins is disabled
Fixes #3811
2024-02-06 11:50:54 +01:00
Pauli Virtanen
d2acad8f0e test: fix test error message check 2024-01-04 10:02:55 +00:00
Barnabás Pőcze
436787d6ba treewide: try to use const char * for string literals 2023-09-16 17:47:35 +02:00
Wim Taymans
41dcac0ecd Port: Add tag param
The tag param has a list of arbitrary key/value pairs. Like the Latency
param, it travels up and downstream. Mixers will append the info
dictionaries or do some more fancy merging.

The purpose is to transport arbirary metadata, out-of-band, through the
graph and it's used for stream metadata and other stream properties.
2023-08-29 14:22:13 +02:00
Wim Taymans
719e48a76d test: fix test 2023-07-19 11:35:03 +02:00
Barnabás Pőcze
de7883024b test: test-logger: fix systemd journal usage
`sd_journal_seek_tail()` is supposed to seek to the logical end of the journal,
i.e. (always) after the last entry. A call to `sd_journal_previous()` is needed
to seek to the last entry, so that `sd_journal_next()` can be called
successfully in `find_in_journal()`. Without it, the journal would always
stay at the end of the list of entries, so further `sd_journal_next()`
calls would fail as there are no entries after the last.

See:
 * https://github.com/systemd/systemd/issues/25369
 * https://github.com/systemd/systemd/pull/26577
2023-05-22 08:05:39 +00:00
Wim Taymans
efea7ad060 hooks: add and use _fast callback function
Add a _fast callback function that skips the version and method check.
We can use this in places where performance is critical when we do the
check out of the critical loops.

Make all system methods _fast calls. We expect them to exist and have
the right version. If we add new versions we can make them slow.
2023-05-06 00:27:12 +02:00
Barnabás Pőcze
934ab3036e treewide: use SPDX tags to specify copyright information
SPDX tags make the licensing information easy to understand and clear,
and they are machine parseable.

See https://spdx.dev for more information.
2023-02-16 10:54:48 +00:00
Wim Taymans
9b3b6e2401 pipewire: support global log level in PIPEWIRE_DEBUG
Add support for PIPEWIRE_DEBUG=3,foo.bar:5 to set a global log level in
addition to specific topics.

Previously it would have to be done with *:3,foo.bar:5, which would not
really set a global level but all topics to the custom level of 3.
2022-12-14 18:00:54 +01:00
Wim Taymans
da3273891c tests: add another pw_split_strv test
To check if an empty string after a delimiter is counted.
2022-12-14 16:56:17 +01:00
columbarius
01b2552b71 spa: Add buffer meta information VideoTransform
This metadata can be used to signal that a buffer is transformed.
The values are intentionally choosen to coincide with
wl_output::transform from the wayland windowsystem.
2022-11-24 10:28:01 +00:00
Demi Marie Obenour
e8f3450a58 Fix various compiler warnings
These caused build failures with -Werror.
2022-10-06 12:12:35 -04:00
Wim Taymans
d22feab92a spa: add macro to simplify array iterations some more
uint32_t i;
	for (i = 0; i < SPA_N_ELEMENTS(some_array); i++)
		.. stuff with some_array[i].foo ...

   becomes:

	SPA_FOR_EACH_ELEMENT_VAR(some_array, p)
		.. stuff with p->foo ..
2022-09-30 16:24:26 +02:00
Demi Marie Obenour
bb4f274ae0 Make all fopen() calls use O_CLOEXEC
by adding "e" to the mode strings.
2022-09-23 15:19:01 +00:00
Wim Taymans
7bf84fa5e2 pod: only call the overflow callback when still needed
When we are already past the size of the buffer, don't bother calling
the overflow callback anymore, the buffer is already corrupted.

Otherwise it would be possible to have the overflow callback fail the
first time around, some data will be skipped, and then the next
overflow callback would succeed, giving the impression that all is
fine.

Add a unit test for this.
2022-09-20 16:59:25 +02:00
Thomas Weißschuh
2394413ec3 hook: don't remove from unitialized list
A lot of code calls spa_hook_remove() from error paths where the hook
and therefore the list may not have been initialized.
This leads to null-derefences.
2022-08-30 11:52:42 +00:00
Wim Taymans
dedbf39ab5 tests: whitespace is not allowed in number either 2022-07-11 12:15:40 +02:00
Wim Taymans
302bad96a0 tests: add json float check unit test 2022-07-11 12:05:42 +02:00
Wim Taymans
cbbc4baa3f json: avoid converting random string to floats
Only allow the json float characters when converting to float.
This avoids NAN, INF, 0xffe4 etc to be seen as a json float.
2022-07-11 11:50:58 +02:00
Barnabás Pőcze
479896279e spa: support: loop: handle cancellation better
Register a pthread cleanup handler to guarantee
that `spa_source::{priv, rmask}` are cleared even
if the thread is cancelled while the loop is dispatching.

This is necessary, otherwise `spa_source::priv` could point
to the stack of the cancelled thread, which will lead to
problems like this later:

  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  0x00007f846b025be2 in detach_source (source=0x7f845f435f60) at ../spa/plugins/support/loop.c:144
  144      e->data = NULL;
  [Current thread is 1 (LWP 5274)]
  (gdb) p e
  $1 = (struct spa_poll_event *) 0x7f845e297820
  (gdb) bt
  #0  0x00007f846b025be2 in detach_source (source=0x7f845f435f60) at ../spa/plugins/support/loop.c:144
  #1  0x00007f846b0276ad in free_source (s=0x7f845f435f60) at ../spa/plugins/support/loop.c:359
  #2  0x00007f846b02a453 in loop_destroy_source (object=0x7f845f3af478, source=0x7f845f435f60) at ../spa/plugins/support/loop.c:786
  #3  0x00007f846b02a886 in impl_clear (handle=0x7f845f3af478) at ../spa/plugins/support/loop.c:859
  #4  0x00007f846b172f40 in unref_handle (handle=0x7f845f3af450) at ../src/pipewire/pipewire.c:211
  #5  0x00007f846b173579 in pw_unload_spa_handle (handle=0x7f845f3af478) at ../src/pipewire/pipewire.c:346
  #6  0x00007f846b15a761 in pw_loop_destroy (loop=0x7f845f434e30) at ../src/pipewire/loop.c:159
  #7  0x00007f846b135d8e in pw_data_loop_destroy (loop=0x7f845f434cb0) at ../src/pipewire/data-loop.c:166
  #8  0x00007f846b12c31c in pw_context_destroy (context=0x7f845f41c690) at ../src/pipewire/context.c:485
  #9  0x00007f846b3ddf9e in jack_client_close (client=0x7f845f3c1030) at ../pipewire-jack/src/pipewire-jack.c:3481
  ...
2022-06-02 00:24:24 +02:00
Wim Taymans
92e2b7dd0d json: improve unicode parsing
Make sure don't overread the strings.
Don't use sscanf but use a custom hex read function.

Fixes #2340
2022-04-27 08:37:32 +02:00
Wim Taymans
5137015c33 json: convert utf16 to utf8
We work with utf8 string so convert the unicode escape sequences to
utf8.

Fixes #2337
2022-04-26 15:38:22 +02:00
Wim Taymans
f2cdb15e0c json: fix \uXXXX parsing
We need exactly 4 hex characters, everything else is refused. We
also copy those characters directly to the output string without
assuming any encoding.

See #2337
2022-04-26 11:01:33 +02:00
Barnabás Pőcze
e28a052105 treewide: retain sections to prevent linker garbage collection
The linker may remove sections that are actually used when
"--gc-sections" and "-z start-stop-gc" is set. Add the `retain`
attribute to prevent that.

Furthermore, fix the alignment for `pwtest_suite_decl` objects.

See: #2292
See: https://lld.llvm.org/ELF/start-stop-gc.html
See: https://github.com/systemd/systemd/issues/21847
See: https://github.com/systemd/systemd/pull/21855
2022-04-15 23:58:05 +02:00
Wim Taymans
2f5bba112f spa: Improve PropInfo fields
The PropInfo either has a registered id (and then also a name from the
type-info) or a custom name as a string.
In all cases, the description contains a free form text that clarifies
the property.

Use the description in the stream controls name.
2022-03-30 17:09:08 +02:00
Wim Taymans
b225fe1d54 pipewire: improve pw_init/pw_deinit
Keep a counter for the amount of times pw_init() was called and
only clear everything when pw_deinit() was called an equal amount of
times.
Also ensure that pw_init() can be called again after pw_deinit().

Fixes #2238
2022-03-26 21:20:07 +01:00
Wim Taymans
2b16df4e89 spa: add locale independent spa_dtoa
And use this in spa_json_format_float() where we also avoid invalid
json floats.
Use json float format in some places where we serialize json floats.
Add a unit test.

See #2223
2022-03-21 10:41:44 +01:00
Wim Taymans
93b5d440bc json: add locale independent parse and format functions
Use them in pw-dump.
Add some unit tests.

See #2223
2022-03-20 21:04:33 +01:00
Wim Taymans
947d15a60a json: empty string should not give a valid number 2022-03-20 20:37:26 +01:00
Wim Taymans
b868ec36c5 SPA_DATA_DIR is the source root when uninstalled
Add some more error logging to bluez5 quirk loading.
2022-03-18 21:33:02 +01:00
Barnabás Pőcze
6e705da101 test: loop: add helper functions to simplify eventfd use
Add the `read_eventfd()` and `write_eventfd()` functions.
2022-03-06 18:40:43 +00:00
Barnabás Pőcze
529b6fd1b8 test: loop: add test which destroys managed source before reentering
Add a test which triggers two event sources in the loop's "before"
control hook, and destroys the second source in the loop's "after"
control hook, and then reenters the loop in the event handler of
the first source. At the moment, this test triggers a use-after-free.

==2973914==ERROR: AddressSanitizer: heap-use-after-free on address 0x608000000440 [...]
READ of size 4 at 0x608000000440 thread T0
    #0 0x7fa97f60c6b7 in loop_iterate ../spa/plugins/support/loop.c:376
    #1 0x7fa98472c1eb in pw_main_loop_run ../src/pipewire/main-loop.c:148
    #2 0x559995af7a76 in destroy_managed_source_before_dispatch_recurse ../test/test-loop.c:355
    #3 0x559995b02678 in start_test_nofork ../test/pwtest.c:882
    #4 0x559995b06191 in run_test ../test/pwtest.c:1087
    #5 0x559995b0948a in run_tests ../test/pwtest.c:1283
    #6 0x559995b0aea4 in main ../test/pwtest.c:1482
    #7 0x7fa98360130f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)
    #8 0x7fa9836013c0 in __libc_start_main@GLIBC_2.2.5 (/usr/lib/libc.so.6+0x2d3c0)
    #9 0x559995aed754 in _start (/home/pb/temp/src/pipewire/build/test/test-loop+0x26754)

0x608000000440 is located 32 bytes inside of 96-byte region [0x608000000420,0x608000000480)
freed by thread T0 here:
    #0 0x7fa984ffda79 in __interceptor_free /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:127
    #1 0x7fa97f60b03a in process_destroy ../spa/plugins/support/loop.c:344
    #2 0x7fa97f60cbf8 in loop_iterate ../spa/plugins/support/loop.c:387
    #3 0x559995af5b62 in dmsbd_recurse_on_event ../test/test-loop.c:298
    #4 0x7fa97f60d826 in source_io_func ../spa/plugins/support/loop.c:396
    #5 0x7fa97f60c7e7 in loop_iterate ../spa/plugins/support/loop.c:377
    #6 0x7fa98472c1eb in pw_main_loop_run ../src/pipewire/main-loop.c:148
    #7 0x559995af7a76 in destroy_managed_source_before_dispatch_recurse ../test/test-loop.c:355
    #8 0x559995b02678 in start_test_nofork ../test/pwtest.c:882
    #9 0x559995b06191 in run_test ../test/pwtest.c:1087
    #10 0x559995b0948a in run_tests ../test/pwtest.c:1283
    #11 0x559995b0aea4 in main ../test/pwtest.c:1482
    #12 0x7fa98360130f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)

previously allocated by thread T0 here:
    #0 0x7fa984ffdfb9 in __interceptor_calloc /usr/src/debug/gcc/libsanitizer/asan/asan_malloc_linux.cpp:154
    #1 0x7fa97f60d883 in loop_add_io ../spa/plugins/support/loop.c:408
    #2 0x559995af75de in destroy_managed_source_before_dispatch_recurse ../test/test-loop.c:349
    #3 0x559995b02678 in start_test_nofork ../test/pwtest.c:882
    #4 0x559995b06191 in run_test ../test/pwtest.c:1087
    #5 0x559995b0948a in run_tests ../test/pwtest.c:1283
    #6 0x559995b0aea4 in main ../test/pwtest.c:1482
    #7 0x7fa98360130f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)

SUMMARY: AddressSanitizer: heap-use-after-free ../spa/plugins/support/loop.c:376 in loop_iterate
2022-03-06 18:40:43 +00:00