This is an archive of the discontinued LLVM Phabricator instance.

[X86][CodeGen] Fix crash in hotpatch
ClosedPublic

Authored by saudi on Nov 8 2022, 6:56 AM.

Details

Summary

This patch fixes crashes (asserts) encountered in some cases while activating hotpatch using -fms-hotpatch, or clang-cl /HOTPATCH.

PatchableFunction pass emits a TargetOpcode::PATCHABLE_OP at the first LLVM-IR instruction that generates machine code. The search for that instruction was done with a local helper that hasn't been updated along with the addition of new LLVM-IR instructions.

Example crash: https://godbolt.org/z/8M9vGajs9
In this example, DBG_INSTR_REF is selected for TargetOpcode::PATCHABLE_OP; lowering that instruction asserts as the metadata-typed operands are unexpected.

Also, the search for the first instruction was assuming that one would be found, asserting otherwise.
Such case may happen in the case of an empty function that is unreachable ( in C/C++, a simple example is int f() {} )

Diff Detail

Event Timeline

saudi created this revision.Nov 8 2022, 6:56 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 8 2022, 6:56 AM
saudi requested review of this revision.Nov 8 2022, 6:56 AM
saudi updated this revision to Diff 473998.Nov 8 2022, 7:15 AM

Update: cleanup lambda (remove capture, fix const correctness)

saudi planned changes to this revision.Nov 10 2022, 2:12 PM

I found new related crashes, I will investigate.

rnk added a subscriber: dblaikie.Nov 17 2022, 1:10 PM

I was going to approve it as is, since it looks like a correct simplification.

Relatedly, @dblaikie was considering establishing the invariant that all empty functions have at least one trap instruction, although for this code the entry BB could conceivably be empty, and you should handle that.

saudi added a comment.Nov 18 2022, 1:03 PM

I was going to approve it as is, since it looks like a correct simplification.

Relatedly, @dblaikie was considering establishing the invariant that all empty functions have at least one trap instruction, although for this code the entry BB could conceivably be empty, and you should handle that.

I will update this patch to better handle the case of an empty entry BB.

Also, I encountered another issue with -fms-hotpatch, which looks more involved: currently, lowering PATCHABLE_OP may not lower the wrapped instruction properly.
I created an issue for it: https://github.com/llvm/llvm-project/issues/59039
I could keep that for another patch, but as it is pretty related it may be worth combining into a patch that completely fixes the feature.
WDYT?

saudi updated this revision to Diff 476960.Nov 21 2022, 11:43 AM

Added support for empty MBB: ensures 2-byte nop is inserted, to make the function patchable.
This should also support the requirement for the functions not to jump to the first instruction.

The 2-byte nop instruction insertion is done by creating a PATCHABLE_OP instance with a PATCHABLE_OP opcode as wrapped instruction.

Also, I removed the support for 16 bit, which would assert when trying to emit nops (happens with the new test cases)
It could be handled instead if necessary; I removed it because I didn't think hotpatching would be ever used on such a platform.

Herald added a project: Restricted Project. · View Herald TranscriptNov 21 2022, 11:43 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
saudi updated this revision to Diff 477182.Nov 22 2022, 7:21 AM

Small update: improve comments, remove empty line modification

rnk accepted this revision.Nov 28 2022, 10:14 AM

lgtm

This revision is now accepted and ready to land.Nov 28 2022, 10:14 AM
This revision was landed with ongoing or failed builds.Nov 30 2022, 4:30 AM
This revision was automatically updated to reflect the committed changes.