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

__functor causes infinite loop when it returns itself #5515

Closed
andir opened this issue Nov 7, 2021 · 0 comments · Fixed by #5578
Closed

__functor causes infinite loop when it returns itself #5515

andir opened this issue Nov 7, 2021 · 0 comments · Fixed by #5578
Labels

Comments

@andir
Copy link
Member

andir commented Nov 7, 2021

Describe the bug
As of 81e7c40 a functor that doesn't take two arguments but one leads to an inifite callFunction loop. Previously this lead to a stack exhaustion and did terminate eventually.

Example expression:

let a = { __functor = x: x; }; in a {}

This will cause the functor that resides within the attribute set a to return itself. Since __functor requires 2 (or more?) arguments, the language then tries to call the attribute set again and that results in the __functor function to be called again... This now loops until you manually terminate it.

The recent code changes seem to have encouraged the compiler to apply tail call optimization, and thus the loop never consumes the entire stack(?).

Steps To Reproduce

  1. nix-instantiate --eval -E '({ __functor = args@{ ... }: args; }) {}'
  2. Observe that it never terminates.
  3. Attach gdb to the process and see that it loops in callFunction but never actually adds another stack frame.

Expected behavior

I'd expect the evaluation to realize that this is an infinite loop and terminate with an error.

Stack overflow on 2.3.16
Infinite loop on 81e7c40 (and newer).

Additional context

I encountered this while writing some additional libexpr unit tests for #5377 .

@andir andir added the bug label Nov 7, 2021
@andir andir changed the title __functor __functor causes infinite loop when it returns itself Nov 7, 2021
edolstra added a commit to edolstra/nix that referenced this issue Nov 16, 2021
This is not really useful on its own, but it does recover the
'infinite recursion' error message for '{ __functor = x: x; } 1', and
is more efficient in conjunction with NixOS#3718.

Fixes NixOS#5515.
dramforever pushed a commit to dramforever/nix that referenced this issue Nov 27, 2021
This is not really useful on its own, but it does recover the
'infinite recursion' error message for '{ __functor = x: x; } 1', and
is more efficient in conjunction with NixOS#3718.

Fixes NixOS#5515.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant