Skip to content

Fix invalid target opline with jit->reuse_ip active #16440

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

Closed
wants to merge 1 commit into from

Conversation

bwoebi
Copy link
Member

@bwoebi bwoebi commented Oct 14, 2024

When the JIT jumps out of tracing while reuse_ip is set, it will jump directly to the reused pointer, which might be e.g. EX(call) instead of EX(opline).

I honestly don't know how to properly give an isolated reproducer, but the gist is that the tracing JIT was bailing out on a DO_UCALL opcode...

I'm not sure whether that fix is correct, but at least I could no longer reproduce it later.

When the JIT jumps out of tracing while reuse_ip is set, it will jump directly to the reused pointer, which might be e.g. EX(call) instead of EX(opline).

Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

I recently saw and fix the same problem, but I thought it's related to my new changes in #16365 (it's not merged yet).

I think your fix is right (it needs few minor tweaks).
It would be very useful to see the TRACE where the "invalid" zend_jit_trace_return() occurs.

@@ -16811,6 +16811,9 @@ static int zend_jit_trace_end_loop(zend_jit_ctx *jit, int loop_ref, const void *

static int zend_jit_trace_return(zend_jit_ctx *jit, bool original_handler, const zend_op *opline)
{
if (!original_handler && jit->reuse_ip) {
Copy link
Member

Choose a reason for hiding this comment

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

I think the check for original_handler is useless, but the check for jit->reuse_ip may be not enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

Wanted to leave minimal impact on codepaths, but maybe it's useless. I don't understand the code well enough.

}
} else if (p->stop == ZEND_JIT_TRACE_STOP_RETURN) {
zend_jit_trace_return(&ctx, 0, NULL);
zend_jit_trace_return(&ctx, 0, p->opline);
Copy link
Member

@dstogov dstogov Oct 14, 2024

Choose a reason for hiding this comment

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

This change is useless.

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 thought so, but wasn't sure.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

#16365 fixed the similar problem by adding the following code at the end of zend_jit_init_method_call()

+	if (trace
+	 && trace->op == ZEND_JIT_TRACE_END
+	 && trace->stop >= ZEND_JIT_TRACE_STOP_INTERPRETER) {
+		if (!zend_jit_set_ip(jit, opline + 1)) {
+			return 0;
+		}
+	}

@bwoebi
Copy link
Member Author

bwoebi commented Oct 15, 2024

@dstogov But that means that you would have to put this code into every single zend_jit_* function where IP reuse is active, right?
At least #16365 doesn't fix the issue I encountered (just tested it).

@bwoebi
Copy link
Member Author

bwoebi commented Oct 15, 2024

And I also have a self-contained reproducer now:

<?php

function gen() {
    yield 1;
    yield 2;
    return 3;
};

opcache_jit_blacklist(gen(...));

for ($i = 0; $i < 2; ++$i) {
    foreach (gen() as $val) {
        var_dump($val);
    }
}

with

./sapi/cli/php -d zend_extension=$(pwd)/modules/opcache.so -d opcache.jit=tracing -d opcache.jit_hot_loop=1 -d opcache.enable_cli=1

@dstogov
Copy link
Member

dstogov commented Oct 15, 2024

@dstogov But that means that you would have to put this code into every single zend_jit_* function where IP reuse is active, right? At least #16365 doesn't fix the issue I encountered (just tested it).

Right, this is the reason I liked to see the test case.
Now I can take care about a better fix.. (I'm going to do it on top of yours).

@dstogov
Copy link
Member

dstogov commented Oct 15, 2024

Unfortunately the fix for this problem becomes more complex than I thought.
p->opline of the last trace record for ZEND_JIT_TRACE_STOP_INTERPRETER doesn't necessary correspond to the next executed instruction (in many cases the next executed opline is unknown), so we can't use it as argument for zend_jit_set_ip().

Probably, I'll have to extend the solution from #16365 Anyway, I would prefer to merge it first.

@bwoebi
Copy link
Member Author

bwoebi commented Oct 15, 2024

I see. It was fine for fixing my issue, but probably not in the general case then.
I'd like to review your PR, but as you see, my understanding of assumptions you can and can't do in jit code is ... very limited.

dstogov added a commit to dstogov/php-src that referenced this pull request Oct 16, 2024
dstogov added a commit that referenced this pull request Oct 18, 2024
@bwoebi
Copy link
Member Author

bwoebi commented Oct 23, 2024

Closing as fixed by #16457.

@bwoebi bwoebi closed this Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants