mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

C++ Flags, Versions, and Runtime Errors #550

Closed Rydial closed 2 years ago

Rydial commented 2 years ago

Currently using GCC 11.1.0, Ubuntu 20.04 LTS. I've taken the high-level api example from https://github.com/mackron/miniaudio and tried compiling and running it as a .cpp file, with the addition of defining the MA_DEBUG_OUTPUT macro before the include statement.

When I use the following compile command, there are no error outputs, and the MA_DEBUG_OUTPUT macro works when I run the program. g++ -Ilib/miniaudio test.cpp -ldl -lpthread -lm -o test.exe 2> test.log

MA_DEBUG_OUTPUT:

DEBUG: Failed to initialize WASAPI backend.
DEBUG: Failed to initialize DirectSound backend.
DEBUG: Failed to initialize WinMM backend.
DEBUG: Failed to initialize Core Audio backend.
DEBUG: Failed to initialize sndio backend.
DEBUG: Failed to initialize audio(4) backend.
DEBUG: Failed to initialize OSS backend.
DEBUG: Attempting to initialize PulseAudio backend...
DEBUG: Loading library: libpulse.so
DEBUG: Loading symbol: pa_mainloop_new
DEBUG: Loading symbol: pa_mainloop_free
DEBUG: Loading symbol: pa_mainloop_quit
DEBUG: Loading symbol: pa_mainloop_get_api
DEBUG: Loading symbol: pa_mainloop_iterate
DEBUG: Loading symbol: pa_mainloop_wakeup
DEBUG: Loading symbol: pa_threaded_mainloop_new
DEBUG: Loading symbol: pa_threaded_mainloop_free
DEBUG: Loading symbol: pa_threaded_mainloop_start
DEBUG: Loading symbol: pa_threaded_mainloop_stop
DEBUG: Loading symbol: pa_threaded_mainloop_lock
DEBUG: Loading symbol: pa_threaded_mainloop_unlock
DEBUG: Loading symbol: pa_threaded_mainloop_wait
DEBUG: Loading symbol: pa_threaded_mainloop_signal
DEBUG: Loading symbol: pa_threaded_mainloop_accept
DEBUG: Loading symbol: pa_threaded_mainloop_get_retval
DEBUG: Loading symbol: pa_threaded_mainloop_get_api
DEBUG: Loading symbol: pa_threaded_mainloop_in_thread
DEBUG: Loading symbol: pa_threaded_mainloop_set_name
DEBUG: Loading symbol: pa_context_new
DEBUG: Loading symbol: pa_context_unref
DEBUG: Loading symbol: pa_context_connect
DEBUG: Loading symbol: pa_context_disconnect
DEBUG: Loading symbol: pa_context_set_state_callback
DEBUG: Loading symbol: pa_context_get_state
DEBUG: Loading symbol: pa_context_get_sink_info_list
DEBUG: Loading symbol: pa_context_get_source_info_list
DEBUG: Loading symbol: pa_context_get_sink_info_by_name
DEBUG: Loading symbol: pa_context_get_source_info_by_name
DEBUG: Loading symbol: pa_operation_unref
DEBUG: Loading symbol: pa_operation_get_state
DEBUG: Loading symbol: pa_channel_map_init_extend
DEBUG: Loading symbol: pa_channel_map_valid
DEBUG: Loading symbol: pa_channel_map_compatible
DEBUG: Loading symbol: pa_stream_new
DEBUG: Loading symbol: pa_stream_unref
DEBUG: Loading symbol: pa_stream_connect_playback
DEBUG: Loading symbol: pa_stream_connect_record
DEBUG: Loading symbol: pa_stream_disconnect
DEBUG: Loading symbol: pa_stream_get_state
DEBUG: Loading symbol: pa_stream_get_sample_spec
DEBUG: Loading symbol: pa_stream_get_channel_map
DEBUG: Loading symbol: pa_stream_get_buffer_attr
DEBUG: Loading symbol: pa_stream_set_buffer_attr
DEBUG: Loading symbol: pa_stream_get_device_name
DEBUG: Loading symbol: pa_stream_set_write_callback
DEBUG: Loading symbol: pa_stream_set_read_callback
DEBUG: Loading symbol: pa_stream_set_suspended_callback
DEBUG: Loading symbol: pa_stream_set_moved_callback
DEBUG: Loading symbol: pa_stream_is_suspended
DEBUG: Loading symbol: pa_stream_flush
DEBUG: Loading symbol: pa_stream_drain
DEBUG: Loading symbol: pa_stream_is_corked
DEBUG: Loading symbol: pa_stream_cork
DEBUG: Loading symbol: pa_stream_trigger
DEBUG: Loading symbol: pa_stream_begin_write
DEBUG: Loading symbol: pa_stream_write
DEBUG: Loading symbol: pa_stream_peek
DEBUG: Loading symbol: pa_stream_drop
DEBUG: Loading symbol: pa_stream_writable_size
DEBUG: Loading symbol: pa_stream_readable_size
DEBUG: System Architecture:
DEBUG:   Endian: LE
DEBUG:   SSE2:   YES
DEBUG:   AVX2:   NO
DEBUG:   NEON:   NO
INFO: [PulseAudio] Playback attr: maxlength=13224, tlength=4408, prebuf=-1, minreq=-1, fragsize=4408; periodSizeInFrames=1102
INFO: [PulseAudio] Playback sample spec: format=16-bit Signed Integer, channels=2, rate=44100
INFO: [PulseAudio] Playback actual attr: maxlength=13224, tlength=3304, prebuf=2208, minreq=1100, fragsize=4408; internalPeriodSizeInFrames=826
INFO: [PulseAudio]
INFO:   GP106 High Definition Audio Controller Digital Stereo (HDMI 2) (Playback)
INFO:     Format:      32-bit IEEE Floating Point -> 16-bit Signed Integer
INFO:     Channels:    2 -> 2
INFO:     Sample Rate: 44100 -> 44100
INFO:     Buffer Size: 826*4 (3304)
INFO:     Conversion:
INFO:       Pre Format Conversion:  NO
INFO:       Post Format Conversion: YES
INFO:       Channel Routing:        NO
INFO:       Resampling:             NO
INFO:       Passthrough:            NO

When I add the compile flag -std=c++20, there are no error outputs, but the MA_DEBUG_OUTPUT macro stops working when I run the program. g++ -std=c++20 -Ilib/miniaudio test.cpp -ldl -lpthread -lm -o test.exe 2> test.log

When I add the compile flag -fsanitize=undefined, the following runtime error is outputted when the program is run:

lib/miniaudio/miniaudio.h:69031:5: runtime error: null pointer passed as argument 1, which is declared to never be null
lib/miniaudio/miniaudio.h:64082:24: runtime error: load of misaligned address 0x7ffcf8f89306 for type 'const ma_uint32', which requires 4 byte alignment
0x7ffcf8f89306: note: pointer points here
 65 78 65 00 72 65  73 2f 50 61 63 6d 61 6e  5f 43 4c 2f 61 75 64 69  6f 2f 67 61 6d 65 5f 73  74 61
             ^ 

g++ -fsanitize=undefined -Ilib/miniaudio test.cpp -ldl -lpthread -lm -o test.exe 2> test.log

Is there a way to get rid of the errors without having to remove the compile flags?

mackron commented 2 years ago

Can you try the dev branch? I know I fixed an issue with C++ and MA_DEBUG_OUTPUT, but I can't remember when I did it. I may not have released it yet.

Rydial commented 2 years ago

The dev branch fixed the MA_DEBUG_OUTPUT problem, but the runtime error still remains.

I know that removing the compile flag -fsanitize=undefined gets rid of the error, but I'd like to eliminate any runtime errors without needing to remove any compile flags if possible.

mackron commented 2 years ago

Thanks for checking the dev branch. Those sanitization errors should definitely be addressed. Do you have updated line numbers for those errors from the dev branch?

Rydial commented 2 years ago

No. The errors and line numbers from the dev branch are exactly the same as the ones previously shown above:

lib/miniaudio/miniaudio.h:69031:5: runtime error: null pointer passed as argument 1, which is declared to never be null
lib/miniaudio/miniaudio.h:64082:24: runtime error: load of misaligned address 0x7ffcf8f89306 for type 'const ma_uint32', which requires 4 byte alignment
0x7ffcf8f89306: note: pointer points here
 65 78 65 00 72 65  73 2f 50 61 63 6d 61 6e  5f 43 4c 2f 61 75 64 69  6f 2f 67 61 6d 65 5f 73  74 61
             ^ 

[EDIT] Apologies, the errors remain the same but the line numbers have been updated.

lib/miniaudio/miniaudio.h:69631:5: runtime error: null pointer passed as argument 1, which is declared to never be null
lib/miniaudio/miniaudio.h:64682:24: runtime error: load of misaligned address 0x7ffe22241306 for type 'const ma_uint32', which requires 4 byte alignment
0x7ffe22241306: note: pointer points here
 65 78 65 00 72 65  73 2f 50 61 63 6d 61 6e  5f 43 4c 2f 61 75 64 69  6f 2f 67 61 6d 65 5f 73  74 61
             ^ 
mackron commented 2 years ago

Thanks. I think those sanitization errors should now be fixed. Are you able to try the dev branch again?

The first one was not an error that would have resulted in a crash in practice, but still good to get that cleaned up regardless. The other was from MurmurHash3 and would be fine for x86/64, but for other architectures where unaligned access is a problem, this would have resulting in issues so good to get that one cleaned up as well.

Thanks for reporting these.

Rydial commented 2 years ago

The sanitization errors have now been fixed with the dev branch.

Thank you for spending the time to fix this!