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

Fix for Float Overflow resulting in VM Crash #837

Merged
merged 1 commit into from
Jan 24, 2024

Conversation

lkarthee
Copy link
Member

@lkarthee lkarthee commented Jan 23, 2024

Before

Series.from_list([1_055_028_234_663_852_885_981_170_418_348_451_692_544.0], dtype: {:f, 32})
thread '<unnamed>' panicked at src/series.rs:86:1:
called `Result::unwrap()` on an `Err` value: {error, badarg}
stack backtrace:
libunwind: stepWithCompactEncoding - invalid compact unwind encoding
Abort trap: 6

After

Explorer.Series.from_list([1_055_028_234_663_852_885_981_170_418_348_451_692_544.0], dtype: {:f, 32})
** (ArgumentError) argument error
    (explorer 0.9.0-dev) Explorer.PolarsBackend.Native.s_from_list_f32("", [1.0550282346638529e39])
    (explorer 0.9.0-dev) lib/explorer/polars_backend/series.ex:24: Explorer.PolarsBackend.Series.from_list/2
    (explorer 0.9.0-dev) lib/explorer/series.ex:417: Explorer.Series.from_list/2
    iex:1: (file)

@lkarthee lkarthee changed the title Float Overflow results in VM Crash Fix for Float Overflow results in VM Crash Jan 23, 2024
@lkarthee lkarthee changed the title Fix for Float Overflow results in VM Crash Fix for Float Overflow resulting in VM Crash Jan 23, 2024
Copy link
Contributor

@philss philss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question, but LGTM!

@@ -53,32 +53,38 @@ from_list!(s_from_list_str, String);
macro_rules! from_list_float {
($name:ident, $type:ty, $module:ident) => {
#[rustler::nif(schedule = "DirtyCpu")]
pub fn $name(name: &str, val: Term) -> ExSeries {
pub fn $name(name: &str, val: Term) -> NifResult<ExSeries> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to use Rustler's NifResult here? Do you know if the Result<ExSeries, ExplorerError> could be used instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons:

  • Result<ExSeries, ExplorerError> will unwrap to {:ok, series} or {:error, _} instead of series. This will break our existing code as others return ex_series.
  • NifResult<ExSeries> will unwrap to series or raises ArgumentError/ErlangError. NifResult<T> is more than type alias of Result<T, Error>. It is special as it can Return(T) or Raise(T) (an ErlangError) or BadArg (an ArgumentError). ArgumentError is what other from_list raise - will help us in writing a wrapper for descriptive errors (Jose's suggestion in the other pr).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks!

}),
term_type => {
let message = format!("from_list/2 not implemented for {term_type:?}");
Err(Error::RaiseTerm(Box::new(message)))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be panic or ErlangError with message ? Let me know if I have to revert to panic!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter much honestly. As long as it comes out properly when we show it to the user.

Copy link
Member Author

@lkarthee lkarthee Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other places in series.rs file where panic! is being used - won't it crash VM ? Is it the case that validations are done in Elixir, so these panic clause won't ever trigger ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't it crash VM?

It won´t. Rustler catches the panics for us :)

Is it the case that validations are done in Elixir, so these panic clause won't ever trigger ?

yeah, normally we add validations so it won´t trigger the panics.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

Copy link
Member Author

@lkarthee lkarthee Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philss one last question, does rustler handle nifpanics differently in dev vs release builds? Looks like title of PR is valid only for dev/test environments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lkarthee no, it shouldn´t be different. I guess the problem is related to the lib that catches the panics.

libunwind: stepWithCompactEncoding - invalid compact unwind encoding

I'm not sure, but looks like this is some encoding problem there.

@@ -53,32 +53,38 @@ from_list!(s_from_list_str, String);
macro_rules! from_list_float {
($name:ident, $type:ty, $module:ident) => {
#[rustler::nif(schedule = "DirtyCpu")]
pub fn $name(name: &str, val: Term) -> ExSeries {
pub fn $name(name: &str, val: Term) -> NifResult<ExSeries> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons:

  • Result<ExSeries, ExplorerError> will unwrap to {:ok, series} or {:error, _} instead of series. This will break our existing code as others return ex_series.
  • NifResult<ExSeries> will unwrap to series or raises ArgumentError/ErlangError. NifResult<T> is more than type alias of Result<T, Error>. It is special as it can Return(T) or Raise(T) (an ErlangError) or BadArg (an ArgumentError). ArgumentError is what other from_list raise - will help us in writing a wrapper for descriptive errors (Jose's suggestion in the other pr).

@josevalim josevalim merged commit 4c181ca into elixir-explorer:main Jan 24, 2024
4 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@lkarthee lkarthee deleted the f32_crash_fix branch January 24, 2024 16:48
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.

3 participants