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 proper error for attempted use of undef slot #50556

Merged
merged 2 commits into from
Jul 16, 2023
Merged

Add proper error for attempted use of undef slot #50556

merged 2 commits into from
Jul 16, 2023

Conversation

Keno
Copy link
Member

@Keno Keno commented Jul 14, 2023

Fixes the segfault in #50518 and turns it into a proper error at both the syntax level (to catch lowering generating bad slot references) as well as at the codegen level (to catch e.g. bad generated functions and opaque closures). However, note that the latter case is technically undefined behavior, because we do not model the possibility that an otherwise-defined argument could throw at access time. Of course, throwing an error is allowable as undefined behavior and preferable to a segfault.

Fixes the segfault in #50518 and turns it into a proper error at
both the syntax level (to catch lowering generating bad slot references)
as well as at the codegen level (to catch e.g. bad generated functions
and opaque closures). However, note that the latter case is technically
undefined behavior, because we do not model the possibility that an
otherwise-defined argument could throw at access time. Of course,
throwing an error is allowable as undefined behavior and preferable
to a segfault.
Keno added a commit that referenced this pull request Jul 14, 2023
Fixes the case from #50518, but we actually have two test cases in
the tests that also hit this (e.g. this one:
```
f40964(xs::Int...=1; k = 2) = (xs, k)
```), but just happened not to hit the bad codegen path. #50556,
once merged would have complained on those definitions as well,
without this fix.
@Keno
Copy link
Member Author

Keno commented Jul 14, 2023

#50559 needs to be merged first for this not to error, since we actually had a test that triggered the bad lowering.

Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

LGTM. Though I would much rather it not be UB to access any arguments (since we might like to look at their types and type parameters to improve optimization / inlining, and it seems really awkward if that is illegal as a special case)

@@ -296,7 +296,7 @@
(if (eq? n '|#self#|) (gensy) n))
arg-names))))
(let ((body (insert-after-meta body ;; don't specialize on generator arguments
`((meta nospecialize ,@arg-names)))))
`((meta nospecialize ,@(map (lambda (idx) `(slot ,(+ idx 1))) (iota (length arg-names))))))))
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

IIRC, there was previously a bug (and open issue) that vararg arguments didn't get nospecialize'd for macros. Any idea if this fixed it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so. This is for generated functions.

github-merge-queue bot pushed a commit that referenced this pull request Jul 15, 2023
Fixes the case from #50518, but we actually have two test cases in the
tests that also hit this (e.g. this one:
```
f40964(xs::Int...=1; k = 2) = (xs, k)
```
), but just happened not to hit the bad codegen path. #50556, once
merged would have complained on those definitions as well, without this
fix.
@Keno
Copy link
Member Author

Keno commented Jul 15, 2023

Though I would much rather it not be UB to access any arguments

I agree with this, but that's a pre-existing issue. I wouldn't really have a problem with ripping out the whole special #undef# codegen handling completely, but until then, this at least makes is slightly safer.

@Keno Keno added this pull request to the merge queue Jul 16, 2023
Merged via the queue into master with commit 024edd6 Jul 16, 2023
1 check passed
@Keno Keno deleted the kf/unusederror branch July 16, 2023 16:31
Keno added a commit that referenced this pull request Jul 17, 2023
I had an off-by-one in #50556, since the argument slots actually start
at 2 and `iota` starts at `0`. This was breaking StaticArrays precompiles,
which attempts to precompile a generator with its abstract signature and
without the nospecialize, those signatures are not compileable.
KristofferC pushed a commit that referenced this pull request Jul 17, 2023
Fixes the case from #50518, but we actually have two test cases in
the tests that also hit this (e.g. this one:
```
f40964(xs::Int...=1; k = 2) = (xs, k)
```), but just happened not to hit the bad codegen path. #50556,
once merged would have complained on those definitions as well,
without this fix.

(cherry picked from commit c272236)
Keno added a commit that referenced this pull request Jul 18, 2023
I had an off-by-one in #50556, since the argument slots actually start
at 2 and `iota` starts at `0`. This was breaking StaticArrays
precompiles, which attempts to precompile a generator with its abstract
signature and without the nospecialize, those signatures are not
compileable.

Co-authored-by: Oscar Smith <oscardssmith@gmail.com>
KristofferC pushed a commit that referenced this pull request Aug 10, 2023
Fixes the case from #50518, but we actually have two test cases in
the tests that also hit this (e.g. this one:
```
f40964(xs::Int...=1; k = 2) = (xs, k)
```), but just happened not to hit the bad codegen path. #50556,
once merged would have complained on those definitions as well,
without this fix.

(cherry picked from commit c272236)
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.

2 participants