Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

float_to_list/2 test fails with exception or segfault when digits range >= 9 #512

Open
KronicDeth opened this issue Aug 14, 2020 · 2 comments
Assignees
Labels
bug Something isn't working compiler Issues pertaining to the compiler with no specific tag help wanted We'd love your help getting this one done! runtime/bifs runtime
Milestone

Comments

@KronicDeth
Copy link
Collaborator

Failing

The following file fails to produce all output

init.erl

-module(init).
-export([start/0]).
-import(erlang, [append_element/2, display/1, float_to_list/2, make_tuple/2]).

start() ->
  lists().

lists() ->
   lists(0, 16).

lists(MaxDigits, MaxDigits) ->
  list(MaxDigits);
lists(Digits, MaxDigits) ->
  list(Digits),
  lists(Digits + 1, MaxDigits).

list(Digits) ->
  Decimals = pair(decimals, Digits),
  Options = [Decimals],
  display(float_to_list(12345.6789, Options)).

%% FIXME work around tuple lowering bug
pair(Key, Value) ->
  Empty = make_tuple(0, []),
  KeyTuple = append_element(Empty, Key),
  append_element(KeyTuple, Value).

Output

---- erlang::float_to_list_2::with_float::with_proper_list_options::with_decimals::without_compact::trailing_zeros_are_not_truncated stdout ----
thread 'erlang::float_to_list_2::with_float::with_proper_list_options::with_decimals::without_compact::trailing_zeros_are_not_truncated' panicked at 'assertion failed: `(left == right)`
  left: `"\"12346\"\n\"12345.7\"\n\"12345.68\"\n\"12345.679\"\n\"12345.6789\"\n\"12345.67890\"\n\"12345.678900\"\n\"12345.6789000\"\n\"12345.67890000\"\n\"12345.678900000\"\n\"12345.6789000000\"\nNoneValue { backtrace: <disabled> }\n"`,
 right: `"\"12346\"\n\"12345.7\"\n\"12345.68\"\n\"12345.679\"\n\"12345.6789\"\n\"12345.67890\"\n\"12345.678900\"\n\"12345.6789000\"\n\"12345.67890000\"\n\"12345.678900000\"\n\"12345.6789000000\"\n\"12345.67890000000\"\n\"12345.678900000001\"\n\"12345.6789000000008\"\n\"12345.67890000000079\"\n\"12345.678900000000795\"\n\"12345.6789000000007945\"\n"`: 
stdout = "12346"
"12345.7"
"12345.68"
"12345.679"
"12345.6789"
"12345.67890"
"12345.678900"
"12345.6789000"
"12345.67890000"
"12345.678900000"
"12345.6789000000"
NoneValue { backtrace: <disabled> }

stderr = thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NoneValue { backtrace: <disabled> }', liblumen_alloc/src/erts/term/convert.rs:365:1

Work-arounds

If the range of digits is changed from 0 to 16 to 0 to 10, no exception occurs. It also is not a problem

init.erl 0 to 10

-module(init).
-export([start/0]).
-import(erlang, [append_element/2, display/1, float_to_list/2, make_tuple/2]).

start() ->
  lists().

lists() ->
   lists(0, 10).

lists(MaxDigits, MaxDigits) ->
  list(MaxDigits);
lists(Digits, MaxDigits) ->
  list(Digits),
  lists(Digits + 1, MaxDigits).

list(Digits) ->
  Decimals = pair(decimals, Digits),
  Options = [Decimals],
  display(float_to_list(12345.6789, Options)).

%% FIXME work around tuple lowering bug
pair(Key, Value) ->
  Empty = make_tuple(0, []),
  KeyTuple = append_element(Empty, Key),
  append_element(KeyTuple, Value).

init.erl 11 to 16

-module(init).
-export([start/0]).
-import(erlang, [append_element/2, display/1, float_to_list/2, make_tuple/2]).

start() ->
  lists().

lists() ->
   lists(11, 16).

lists(MaxDigits, MaxDigits) ->
  list(MaxDigits);
lists(Digits, MaxDigits) ->
  list(Digits),
  lists(Digits + 1, MaxDigits).

list(Digits) ->
  Decimals = pair(decimals, Digits),
  Options = [Decimals],
  display(float_to_list(12345.6789, Options)).

%% FIXME work around tuple lowering bug
pair(Key, Value) ->
  Empty = make_tuple(0, []),
  KeyTuple = append_element(Empty, Key),
  append_element(KeyTuple, Value).

Segfault

The range 9..16 actually causes a segfault instead of the exception.

init.erl

-module(init).
-export([start/0]).
-import(erlang, [append_element/2, display/1, float_to_list/2, make_tuple/2]).

start() ->
  lists().

lists() ->
   lists(8, 16).

lists(MaxDigits, MaxDigits) ->
  list(MaxDigits);
lists(Digits, MaxDigits) ->
  list(Digits),
  lists(Digits + 1, MaxDigits).

list(Digits) ->
  Decimals = pair(decimals, Digits),
  Options = [Decimals],
  display(float_to_list(12345.6789, Options)).

%% FIXME work around tuple lowering bug
pair(Key, Value) ->
  Empty = make_tuple(0, []),
  KeyTuple = append_element(Empty, Key),
  append_element(KeyTuple, Value).

Output

---- erlang::float_to_list_2::with_float::with_proper_list_options::with_decimals::without_compact::trailing_zeros_are_not_truncated stdout ----
thread 'erlang::float_to_list_2::with_float::with_proper_list_options::with_decimals::without_compact::trailing_zeros_are_not_truncated' panicked at 'assertion failed: `(left == right)`
  left: `"\"12345.67890000\"\n\"12345.678900000\"\n\"12345.6789000000\"\n\"12345.67890000000\"\n\"12345.678900000001\"\n\"12345.6789000000008\"\n\"12345.67890000000079\"\n\"12345.678900000000795\"\n"`,
 right: `"\"12346\"\n\"12345.7\"\n\"12345.68\"\n\"12345.679\"\n\"12345.6789\"\n\"12345.67890\"\n\"12345.678900\"\n\"12345.6789000\"\n\"12345.67890000\"\n\"12345.678900000\"\n\"12345.6789000000\"\n\"12345.67890000000\"\n\"12345.678900000001\"\n\"12345.6789000000008\"\n\"12345.67890000000079\"\n\"12345.678900000000795\"\n\"12345.6789000000007945\"\n"`: 
stdout = "12345.67890000"
"12345.678900000"
"12345.6789000000"
"12345.67890000000"
"12345.678900000001"
"12345.6789000000008"
"12345.67890000000079"
"12345.678900000000795"

stderr = 
status_code = 
signal = segmentation fault (invalid address)', native_implemented/otp/tests/lib/erlang/float_to_list_2/with_float/with_proper_list_options/with_decimals/without_compact.rs:1:1
@KronicDeth KronicDeth added bug Something isn't working compiler Issues pertaining to the compiler with no specific tag runtime runtime/bifs labels Aug 14, 2020
@KronicDeth KronicDeth added this to the ElixirConf 2020 milestone Aug 14, 2020
@bitwalker
Copy link
Collaborator

The blocking issue I'm hitting is that the process heap runs out of space and the runtime functions are returning None rather than raising erlang exceptions; so the None term gets passed around and depending on what hits it first, you either get the invalid term error or a segfault (segfaults for when loads/stores are performed against the None when it is expected to be a tuple).

@KronicDeth
Copy link
Collaborator Author

@bitwalker we can revisited after I do #473 after you merge your pending local changes.

@KronicDeth KronicDeth modified the milestones: ElixirConf 2020, In 2020 Sep 3, 2020
@bcardarella bcardarella modified the milestones: In 2020, In 2021 Dec 9, 2020
@bcardarella bcardarella added the help wanted We'd love your help getting this one done! label Apr 7, 2021
@bitwalker bitwalker modified the milestones: In 2021, 2023 Mar 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working compiler Issues pertaining to the compiler with no specific tag help wanted We'd love your help getting this one done! runtime/bifs runtime
Projects
None yet
Development

No branches or pull requests

3 participants