Skip to content

x86 decoder: REP/segment prefix of one instruction sticks to the following ones; tzcnt/lzcnt decoded as REP bsf/bsr #2622

Open

Description

@bbiiggppiigg

Fixed by #2617 (draft). Independent of the other decoder and jump-table work.

How we found it

While measuring the jump-table fix for #2621 on TensorFlow 2.20's libtensorflow_cc.so.2, the jump at 0x279ce557 stayed unresolved. Its block is tzcnt %edx,%eax; lea table(%rip),%rdx; movslq (%rdx,%rax,4),%rax; add %rdx,%rax; jmp *%rax, and the parse log showed the add decoded as REP add %rdx,%rax. The jump-table analysis does not accept a REP-prefixed add, so the table is never read.

What is wrong

Two decoder bugs:

  1. Since 1f8ac59 (Move operand accumulation into decoder for x86 #2171, 2026-04-21), InstructionDecoder_x86 keeps the REP/REPNZ prefix and the segment override in members, and assigns them only when an instruction has prefixes. After any F3/F2 instruction, every later instruction from the same decoder is reported as REP/REPNZ and reads rcx, df (and zf). A stale fs/gs override likewise adds an fs/gs read to later instructions that have no prefix at all.
  2. f3 0f bc (tzcnt) and f3 0f bd (lzcnt) are decoded as REP bsf / REP bsr, because the sse_prefix table does not mark BC/BD as taking F3 as part of the opcode. tzcnt occurs 5149 times in libtensorflow_framework.so.2 and 126 times in RHEL 8 libc.

Fix status

#2617 (draft) resets the prefix state for each instruction and decodes tzcnt/lzcnt. It has unit tests for both, each shown to fail without the fix. With the #2621 prototype and #2620, it resolves the jump at 0x279ce557 and 22 jumps in libtensorflow_cc in total that were unresolved before.

Related:

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions