Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RB_GC_GUARD to numo_pocketfft_fft to fix crash #4

Merged

Conversation

mike-bourgeous
Copy link
Contributor

In #3 I describe a crash I have been experiencing with numo-pocketfft. When running RSpec tests in the mb-sound project, my FFT tests crash frequently, and occasionally return incorrect results. These errors only happen at the -O3 optimization level. It appears this is due to Ruby's GC freeing either x_val or z_val before the function has finished, and other data being written in its place.

This fix uses the RB_GC_GUARD macro to make sure gcc preserves the x_val and z_val VALUEs in stack or registers so that the GC knows they are still in use.

See https://ruby-doc.org/core-2.7.1/doc/extension_rdoc.html#label-Appendix+E.+RB_GC_GUARD+to+protect+from+premature+GC

Testing

To test the fix I ran rspec in a loop in the fft branch of the mb-sound project, as well as the reproducing script in #3. Before this change, tests would crash frequently. After this change, tests ran without issue for many iterations.

In yoshoku#3 I describe a crash I have been experiencing with numo-pocketfft.  When running RSpec tests in the mb-sound project, my FFT tests crash frequently, and occasionally return incorrect results.  These errors only happen at the `-O3` optimization level.  It appears this is due to Ruby's GC freeing either `x_val` or `z_val` before the function has finished, and other data being written in its place.

This fix uses the RB_GC_GUARD macro to make sure that gcc preserves the `x_val` and `z_val` `VALUE`s in stack or registers so that the GC knows they are still in use.

## Testing

To test the fix I ran `rspec` in a loop in the `fft` branch of the `mb-sound` project, as well as the reproducing script in yoshoku#3.  Before this change, tests would crash frequently.  After this change, tests ran without issue for many iterations.
mike-bourgeous added a commit to mike-bourgeous/mb-sound that referenced this pull request Jan 16, 2021
@yoshoku
Copy link
Owner

yoshoku commented Jan 16, 2021

Thank you for your contribution 👍

@yoshoku yoshoku merged commit c792bb5 into yoshoku:main Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants