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

Detect and abort tracing if "early return" occurs. #1513

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

ltratt
Copy link
Contributor

@ltratt ltratt commented Dec 14, 2024

If we start tracing a loop and the function-with-a-control-point returns whilst tracing is ongoing, we are in dangerous territory. For quite a while we assumed that "dangerous territory" meant (gongs of doom!) "UB". Actually, we just hadn't created very good examples: when I did, it turns out that an unwrap in trace_builder fails if early return happens. We could, therefore, detect early returns there, but that means that we've done a lot of work before realising this.

This commit instead has mt.rs detect early returns, and abort tracing, as soon as we reasonably can. [Note, however, that if a user annotates returns from the function, they could stop things even earlier. Is this worth it? It is quite complicated to explain and, as we'll see in a bit, it's not a very common case]

All that said, it turns out early returns aren't that common: you have to start tracing on the same iteration that you "early return" from. This is definitely possible -- indeed the two C tests in the PR do that, one "normally" and one when side-tracing -- but it seems unlikely that it will happen very often. So although we could force the user to help us detect this even sooner, I'm not sure it's worth burdening them or us with that responsibility right now: the cases where it would save serious resources are borderline pathological. Indeed, the only such case I can think of is: if you start tracing from the "root" control point call frame, and then return to the main program (i.e. you will never go back to the interpreter), then tracing is never turned off. One day we can fix this, but I don't think we will encounter it very soon.

ykrt/src/mt.rs Show resolved Hide resolved
@ptersilie
Copy link
Contributor

Looks good to me.

@ptersilie ptersilie added this pull request to the merge queue Dec 16, 2024
ykrt/src/mt.rs Outdated
};
});
}

fn expect_stop_tracing(mt: &Arc<MT>, loc: &Location) {
let TransitionControlPoint::StopTracing = mt.transition_control_point(loc) else {
let TransitionControlPoint::StopTracing =
mt.transition_control_point(loc, 0 as *mut c_void)
Copy link
Contributor

Choose a reason for hiding this comment

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

Clippy will probably say to use ptr::null_mut().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are no warnings from Clippy. Note this is a c_void.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should work since null_mut<T>() parametrised. Fewer magic numbers, right?

#[allow(dead_code)]
GrowsToHigherAddress,
/// The CPU stack grows "upwards" i.e. growth changes it to a higher address.
#[allow(dead_code)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this second variant dead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now, yes. But having it there helps explain the other one IMHO.

@vext01 vext01 removed this pull request from the merge queue due to a manual request Dec 16, 2024
@vext01
Copy link
Contributor

vext01 commented Dec 16, 2024

Do I understand correctly that this will superceed #1481? Because we would no longer require the user to do manual annotations for early returns?

@ltratt
Copy link
Contributor Author

ltratt commented Dec 16, 2024

Do I understand correctly that this will superceed #1481? Because we would no longer require the user to do manual annotations for early returns?

Yes and yes.

@ltratt
Copy link
Contributor Author

ltratt commented Dec 16, 2024

Force pushed the null_mut change.

@vext01
Copy link
Contributor

vext01 commented Dec 16, 2024

Surprised the casts were needed. Doesn't it just coerce?

If we start tracing a loop and the function-with-a-control-point returns
whilst tracing is ongoing, we are in dangerous territory. For quite a
while we assumed that "dangerous territory" meant (gongs of doom!) "UB".
Actually, we just hadn't created very good examples: when I did, it
turns out that an `unwrap` in trace_builder fails if early return
happens. We *could*, therefore, detect early returns there, but that
means that we've done a lot of work before realising this.

This commit instead has `mt.rs` detect early returns, and abort tracing,
as soon as we reasonably can. [Note, however, that if a user annotates
`return`s from the function, they could stop things even earlier. Is
this worth it? It is quite complicated to explain and, as we'll see in a
bit, it's not a very common case]

All that said, it turns out early returns aren't that common: you have
to start tracing on the same iteration that you "early return" from.
This is definitely possible -- indeed the two C tests in the PR do that,
one "normally" and one when side-tracing -- but it seems unlikely that
it will happen very often. So although we could force the user to help
us detect this even sooner, I'm not sure it's worth burdening them or us
with that responsibility right now: the cases where it would save
serious resources are borderline pathological. Indeed, the only such
case I can think of is: if you start tracing from the "root" control
point call frame, and then return to the main program (i.e. you will
never go back to the interpreter), then tracing is never turned off. One
day we can fix this, but I don't think we will encounter it very soon.
@ltratt
Copy link
Contributor Author

ltratt commented Dec 16, 2024

You're right. Force pushed the simplification.

@vext01
Copy link
Contributor

vext01 commented Dec 16, 2024

Thanks!

@vext01 vext01 added this pull request to the merge queue Dec 16, 2024
Merged via the queue into ykjit:master with commit 190e051 Dec 16, 2024
2 checks passed
@ltratt ltratt deleted the early_return branch December 16, 2024 11: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