Skip to content

Prevent clobbering of TAILCALL op2 in epilogue #115

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

Merged
merged 4 commits into from
Jul 7, 2025

Conversation

arnaud-lb
Copy link
Contributor

TAILCALL op2 may be overridden when it is assigned to a preserved register, as the epilogue is emitted before the call:

{
	l_1 = START(l_4);
	uintptr_t f = PARAM(l_1, "p", 1);
	l_2 = CALL(l_1, f);
	l_3 = TAILCALL(l_2, f);
	l_4 = UNREACHABLE(l_3);
}
test:
	pushq %rbx
	movq %rdi, %rbx
	callq *%rbx
	popq %rbx
	jmpq *%rbx

This can not be trivially solved in RA, as the set of used preserved regs is not known yet.

Here I move op2 to a tmp reg in ir_emit_tailcall() when op2 is in used_preserved_regs.

@arnaud-lb arnaud-lb marked this pull request as ready for review July 1, 2025 10:15
Copy link
Owner

@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.

Thanks for the attempt to fix this and for the new test cases.
I think, the idea should work, but I see few problems.
See comments...

We probably should also check for %rsp and %rbp in the fused memory load.

Comment on lines -4961 to -4964
if (IR_REG_SPILLED(op2_reg)) {
op2_reg = IR_REG_NUM(op2_reg);
ir_emit_load(ctx, IR_ADDR, op2_reg, insn->op2);
}
Copy link
Owner

Choose a reason for hiding this comment

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

Can we have a spilled op2_reg that not interference with ctx->used_preserved_reg?
I think you lost this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I missed this case. I've updated the PR and added tests for that.

ir_x86.dasc Outdated
op2_reg = IR_REG_RAX;

ir_type type = ctx->ir_base[insn->op2].type;
| ASM_REG_MEM_OP, mov, type, op2_reg, mem
Copy link
Owner

Choose a reason for hiding this comment

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

Use ir_emit_load_mem_int()

Comment on lines 21 to 26
movq %rbx, (%rsp)
callq *%rbx
movq %rbx, %rdi
addq $0x10, %rsp
popq %rbx
jmpq *(%rsp)
Copy link
Owner

Choose a reason for hiding this comment

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

The code looks wrong. %rsp is changed between movq %rbx, (%rsp) and jmpq *(%rsp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, I didn't notice that

@dstogov dstogov merged commit af6dc83 into dstogov:master Jul 7, 2025
13 of 14 checks passed
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