llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[PowerPC/AIX] PPC32 prologue miscompile: mflr and mfcr assigned same register (r12), clobbering saved LR

Open matthew-wozniczka opened this issue 4 months ago • 9 comments

(This might not be the right place to report this, but I'm still trying to figure out if we have the right support contract to bother IBM about it...

And yeah, I had Claude generate the report, and help me debug the issue, as I have very little powerpc/dbx experience, but it does seem like a real compiler issue, whether it's coming from LLVM/Clang or IBM's modifications to it in XLClang)

Component: Backend - PowerPC

Target: powerpc-ibm-aix (32-bit XCOFF)

Severity: Crash - generates code that deterministically SEGFAULTs on return from affected functions


Summary

The PPC32 prologue codegen for certain non-leaf functions assigns the same register (r12) for both mflr (save link register) and mfcr (save condition register), with mfcr sequenced immediately after mflr and no intervening store. This destroys the saved return address. The function then stores CR value into both the CR save slot and the LR save slot. On return, the epilogue restores the CR value as LR, and blr branches to a garbage address, causing SIGSEGV.

Affected Function (observed)

sbicu_78::UnicodeString::findAndReplace(int, int, const UnicodeString&, int, int, const UnicodeString&, int, int) in ICU 78, compiled for powerpc-ibm-aix (32-bit).

A neighboring function in the same translation unit (UnicodeString::getTerminatedBuffer) is compiled correctly, using mflr r0 - the standard distinct register.

Disassembly Evidence

Buggy prologue (findAndReplace, at .text+0x5a88)

0x5a70:  lhz    r0,0x4(r3)          # early bogus-string check
0x5a74:  andi.  r11,r0,0x1
0x5a78:  bnelr                       # returns via LR (still intact) - survives
0x5a7c:  lhz    r11,0x4(r6)
0x5a80:  andi.  r12,r11,0x1
0x5a84:  bnelr                       # returns via LR (still intact) - survives
0x5a88:  mflr   r12                  # r12 = LR (correct return address)
0x5a8c:  mfcr   r12                  # r12 = CR - CLOBBERS LR value
0x5a90:  stw    r12,0x4(r1)          # caller_SP+4 (CR slot) <- CR  (accidentally correct)
0x5a94:  stwu   r1,-112(r1)          # allocate frame
0x5a98:  stw    r12,0x78(r1)         # caller_SP+8 (LR slot) <- CR  *** BUG ***
0x5a9c:  stmw   r18,0x38(r1)        # save GPRs (correct)

Epilogue (findAndReplace, at .text+0x5ce8)

0x5ce8:  lmw    r18,0x38(r1)         # restore GPRs
0x5cec:  addi   r1,0x70(r1)          # pop frame -> r1 = caller SP
0x5cf0:  lwz    r0,0x8(r1)           # "LR" <- caller_SP+8 = CR value (POISON)
0x5cf4:  lwz    r12,0x4(r1)          # "CR" <- caller_SP+4 = CR value
0x5cf8:  mtcrf  0x20,r12             # restore CR fields (correct)
0x5cfc:  mtcrf  0x10,r12
0x5d00:  mtcrf  0x08,r12
0x5d04:  mtlr   r0                   # LR = CR value (e.g. 0x24348a43)
0x5d08:  blr                         # branch to ~0x24348a40 -> SIGSEGV

The epilogue is correctly structured - it restores LR from [caller_SP+8]. The bug is entirely in the prologue register assignment.

Correct prologue (getTerminatedBuffer, same TU, for comparison)

mflr   r0                            # LR in r0 - distinct from CR register
...
stw    r0,0x58(r1)                    # LR saved correctly

Analysis

The standard AIX/PPC32 linkage convention uses r0 for mflr and r12 for mfcr. The prologue above uses r12 for both, with the mfcr immediately following mflr before a store, so the LR value is irrecoverably lost.

This appears to be a register allocation issue in PPCFrameLowering (or equivalent) where the single-scratch-register path assigns the same physical register to both the LR and CR save sequences without interleaving a store. The fix would be one of:

  1. Use r0 for mflr (the default/expected behavior) - investigate why r0 was not available
  2. Interleave a store between mflr r12 and mfcr r12 if a single scratch register is intentional
  3. Add a conflict/anti-dependency between the mflr and mfcr pseudos to prevent same-register assignment

Observable Behavior

  • Deterministic SIGSEGV on every call that reaches the frame setup (past the two bnelr early-exit checks)
  • Crash address is always ($cr & ~3) - the CR register value interpreted as a code pointer
  • The crash address tracks the allocator: plain runtime -> 0x24348a43; MALLOCDEBUG=catch_overflow -> 0x24242224 (different CR state)
  • Early-exit paths (bnelr at +0x08, +0x14) that return before the prologue are unaffected

Environment

  • Compiler: IBM Open XL C/C++ for AIX
  • Compiler version: 17.1.3 (5725-C72, 5765-J18), version 17.1.3.3, clang version 19.1.7 (build ed4274b ed4274b44e03622e37eb1d0fb3f4ec1a51cff4db)
  • Compile command: /opt/IBM/openxlC/17.1.3/bin/ibm-clang++_r -O2 -fPIC -fexceptions -m32 -D_REENTRANT -DUCLN_NO_AUTO_CLEANUP=0 -DU_HAVE_LIB_SUFFIX=1 -DU_LIB_SUFFIX_C_NAME=__sb32 -I. "-DDEFAULT_ICU_PLUGINS="/home/simba/jk/packagey/modules/icu/buildmodule/vcpkg_installed/build-configuration/lib/icu" " -DU_ALL_IMPLEMENTATION -DU_ATTRIBUTE_DEPRECATED= -DU_COMMON_IMPLEMENTATION -D_REENTRANT -O2 -fPIC -fexceptions -frtti -m32 -O3 -std=c++17 -D__STDC_FORMAT_MACROS -W -Wall -pedantic -Wpointer-arith -Wwrite-strings -Wno-long-long -c -o unistr.o unistr.cpp
  • Source: icu4c/source/common/unistr.cpp(UnicodeString::findAndReplace) from ICU 78.1
  • OS: System Model: IBM,9105-22A Machine Serial Number: 78C9421 Processor Type: PowerPC_POWER10 Processor Implementation Mode: POWER 9 Processor Version: PV_9_Compat Number Of Processors: 3 Processor Clock Speed: 2450 MHz CPU Type: 64-bit Kernel Type: 64-bit LPAR Info: 25 vm-89796347-144283407 Memory Size: 8192 MB Good Memory Size: 8192 MB Platform Firmware level: ML1060_107 Firmware Version: IBM,FW1060.30 (ML1060_107) Console Login: enable Auto Restart: true Full Core: false NX Crypto Acceleration: Capable and Enabled In-Core Crypto Acceleration: Capable, but not Enabled

matthew-wozniczka avatar Jul 14 '26 00:07 matthew-wozniczka

@llvm/issue-subscribers-backend-powerpc

Author: Matthew Wozniczka (matthew-wozniczka)

(This might not be the right place to report this, but I'm still trying to figure out if we have the right support contract to bother IBM about it...

And yeah, I had Claude generate the report, and help me debug the issue, as I have very little powerpc/dbx experience, but it does seem like a real compiler issue, whether it's coming from LLVM/Clang or IBM's modifications to it in XLClang)

Component: Backend - PowerPC

Target: powerpc-ibm-aix (32-bit XCOFF)

Severity: Crash - generates code that deterministically SEGFAULTs on return from affected functions


Summary

The PPC32 prologue codegen for certain non-leaf functions assigns the same register (r12) for both mflr (save link register) and mfcr (save condition register), with mfcr sequenced immediately after mflr and no intervening store. This destroys the saved return address. The function then stores CR value into both the CR save slot and the LR save slot. On return, the epilogue restores the CR value as LR, and blr branches to a garbage address, causing SIGSEGV.

Affected Function (observed)

sbicu_78::UnicodeString::findAndReplace(int, int, const UnicodeString&, int, int, const UnicodeString&, int, int) in ICU 78, compiled for powerpc-ibm-aix (32-bit).

A neighboring function in the same translation unit (UnicodeString::getTerminatedBuffer) is compiled correctly, using mflr r0 - the standard distinct register.

Disassembly Evidence

Buggy prologue (findAndReplace, at .text+0x5a88)

0x5a70:  lhz    r0,0x4(r3)          # early bogus-string check
0x5a74:  andi.  r11,r0,0x1
0x5a78:  bnelr                       # returns via LR (still intact) - survives
0x5a7c:  lhz    r11,0x4(r6)
0x5a80:  andi.  r12,r11,0x1
0x5a84:  bnelr                       # returns via LR (still intact) - survives
0x5a88:  mflr   r12                  # r12 = LR (correct return address)
0x5a8c:  mfcr   r12                  # r12 = CR - CLOBBERS LR value
0x5a90:  stw    r12,0x4(r1)          # caller_SP+4 (CR slot) &lt;- CR  (accidentally correct)
0x5a94:  stwu   r1,-112(r1)          # allocate frame
0x5a98:  stw    r12,0x78(r1)         # caller_SP+8 (LR slot) &lt;- CR  *** BUG ***
0x5a9c:  stmw   r18,0x38(r1)        # save GPRs (correct)

Epilogue (findAndReplace, at .text+0x5ce8)

0x5ce8:  lmw    r18,0x38(r1)         # restore GPRs
0x5cec:  addi   r1,0x70(r1)          # pop frame -&gt; r1 = caller SP
0x5cf0:  lwz    r0,0x8(r1)           # "LR" &lt;- caller_SP+8 = CR value (POISON)
0x5cf4:  lwz    r12,0x4(r1)          # "CR" &lt;- caller_SP+4 = CR value
0x5cf8:  mtcrf  0x20,r12             # restore CR fields (correct)
0x5cfc:  mtcrf  0x10,r12
0x5d00:  mtcrf  0x08,r12
0x5d04:  mtlr   r0                   # LR = CR value (e.g. 0x24348a43)
0x5d08:  blr                         # branch to ~0x24348a40 -&gt; SIGSEGV

The epilogue is correctly structured - it restores LR from [caller_SP+8]. The bug is entirely in the prologue register assignment.

Correct prologue (getTerminatedBuffer, same TU, for comparison)

mflr   r0                            # LR in r0 - distinct from CR register
...
stw    r0,0x58(r1)                    # LR saved correctly

Analysis

The standard AIX/PPC32 linkage convention uses r0 for mflr and r12 for mfcr. The prologue above uses r12 for both, with the mfcr immediately following mflr before a store, so the LR value is irrecoverably lost.

This appears to be a register allocation issue in PPCFrameLowering (or equivalent) where the single-scratch-register path assigns the same physical register to both the LR and CR save sequences without interleaving a store. The fix would be one of:

  1. Use r0 for mflr (the default/expected behavior) - investigate why r0 was not available
  2. Interleave a store between mflr r12 and mfcr r12 if a single scratch register is intentional
  3. Add a conflict/anti-dependency between the mflr and mfcr pseudos to prevent same-register assignment

Observable Behavior

  • Deterministic SIGSEGV on every call that reaches the frame setup (past the two bnelr early-exit checks)
  • Crash address is always ($cr & ~3) - the CR register value interpreted as a code pointer
  • The crash address tracks the allocator: plain runtime -> 0x24348a43; MALLOCDEBUG=catch_overflow -> 0x24242224 (different CR state)
  • Early-exit paths (bnelr at +0x08, +0x14) that return before the prologue are unaffected

Environment

  • Compiler: IBM Open XL C/C++ for AIX
  • Compiler version: 17.1.3 (5725-C72, 5765-J18), version 17.1.3.3, clang version 19.1.7 (build ed4274b ed4274b44e03622e37eb1d0fb3f4ec1a51cff4db)
  • Compile command TODO Will fill in later
  • Source: ICU 78 (icu4c/source/common/unistr.cpp, UnicodeString::findAndReplace)
  • OS: System Model: IBM,9105-22A Machine Serial Number: 78C9421 Processor Type: PowerPC_POWER10 Processor Implementation Mode: POWER 9 Processor Version: PV_9_Compat Number Of Processors: 3 Processor Clock Speed: 2450 MHz CPU Type: 64-bit Kernel Type: 64-bit LPAR Info: 25 vm-89796347-144283407 Memory Size: 8192 MB Good Memory Size: 8192 MB Platform Firmware level: ML1060_107 Firmware Version: IBM,FW1060.30 (ML1060_107) Console Login: enable Auto Restart: true Full Core: false NX Crypto Acceleration: Capable and Enabled In-Core Crypto Acceleration: Capable, but not Enabled

Having some trouble getting the logs containing the actual compile commands, but I did find the following so far (the vcpkg logs; we use a custom port of ICU, patching several files before compiling it to address various platform issues, but nothing in the affected file):

[2026-07-14T21:52:14.526Z] -- Using source at /home/simba/jk/packagey/vcpkg/buildtrees/icu/src/lease-78.1-faba850dd5.clean
[2026-07-14T21:52:14.526Z] -- Common Options: --enable-auto-cleanup;--disable-layoutex;--verbose;--enable-static;--enable-shared;--with-library-suffix=_sb32
[2026-07-14T21:52:14.526Z] -- Options: --disable-64bit-libs;--prefix=/home/simba/jk/packagey/modules/icu/buildmodule/vcpkg_installed/build-configuration
[2026-07-14T21:52:14.526Z] -- Installing: /home/simba/jk/packagey/vcpkg/buildtrees/icu/LICENSE
[2026-07-14T21:52:14.526Z] -- Using working directory: /home/simba/jk/packagey/vcpkg/buildtrees/icu/build-configuration-build-configuration-rel-aix
[2026-07-14T21:52:14.526Z] -- Copying "/home/simba/jk/packagey/vcpkg/buildtrees/icu/src/lease-78.1-faba850dd5.clean/icu4c/source/" to "/home/simba/jk/packagey/vcpkg/buildtrees/icu/build-configuration-build-configuration-rel-aix"...
[2026-07-14T21:52:25.018Z] -- With environment variables:
[2026-07-14T21:52:25.018Z] -- CC=/opt/IBM/openxlC/17.1.3/bin/ibm-clang_r
[2026-07-14T21:52:25.018Z] -- CXX=/opt/IBM/openxlC/17.1.3/bin/ibm-clang++_r
[2026-07-14T21:52:25.018Z] -- CFLAGS=-O2 -fPIC -fexceptions -m32 -O3 -std=c17
[2026-07-14T21:52:25.018Z] -- CXXFLAGS=-D_REENTRANT -O2 -fPIC -fexceptions -frtti -m32 -O3 -std=c++17 -D__STDC_FORMAT_MACROS
[2026-07-14T21:52:25.018Z] -- CPPFLAGS=-O2 -fPIC -fexceptions -m32 -D_REENTRANT
[2026-07-14T21:52:25.018Z] -- LDFLAGS=-O2 -Wl,-bbigtoc -brtl -m32
[2026-07-14T21:52:25.018Z] -- AR=ar -X32
[2026-07-14T21:52:25.018Z] -- Parsing command: bash -c "V=1 /home/simba/jk/packagey/vcpkg/buildtrees/icu/build-configuration-build-configuration-rel-aix/configure --enable-auto-cleanup --disable-layoutex --verbose --enable-static --enable-shared --with-library-suffix=_sb32 --disable-64bit-libs --prefix=/home/simba/jk/packagey/modules/icu/buildmodule/vcpkg_installed/build-configuration"
[2026-07-14T21:52:25.019Z] -- Running command: V=1 /home/simba/jk/packagey/vcpkg/buildtrees/icu/build-configuration-build-configuration-rel-aix/configure --enable-auto-cleanup --disable-layoutex --verbose --enable-static --enable-shared --with-library-suffix=_sb32 --disable-64bit-libs --prefix=/home/simba/jk/packagey/modules/icu/buildmodule/vcpkg_installed/build-configuration
[2026-07-14T21:52:25.019Z] -- Using working directory: /home/simba/jk/packagey/vcpkg/buildtrees/icu/build-configuration-build-configuration-rel-aix
[2026-07-14T21:52:42.846Z] -- With environment variables:
[2026-07-14T21:52:42.847Z] -- CC=/opt/IBM/openxlC/17.1.3/bin/ibm-clang_r
[2026-07-14T21:52:42.847Z] -- CXX=/opt/IBM/openxlC/17.1.3/bin/ibm-clang++_r
[2026-07-14T21:52:42.847Z] -- CFLAGS=-O2 -fPIC -fexceptions -m32 -O3 -std=c17
[2026-07-14T21:52:42.847Z] -- CXXFLAGS=-D_REENTRANT -O2 -fPIC -fexceptions -frtti -m32 -O3 -std=c++17 -D__STDC_FORMAT_MACROS
[2026-07-14T21:52:42.847Z] -- CPPFLAGS=-O2 -fPIC -fexceptions -m32 -D_REENTRANT
[2026-07-14T21:52:42.847Z] -- LDFLAGS=-O2 -Wl,-bbigtoc -brtl -m32
[2026-07-14T21:52:42.847Z] -- AR=ar -X32
[2026-07-14T21:52:42.847Z] -- OPTIONS=
[2026-07-14T21:52:42.847Z] -- OPTIONS_RELEASE=
[2026-07-14T21:52:42.847Z] -- OPTIONS_DEBUG=
[2026-07-14T21:52:42.847Z] -- Parsing command: gmake
[2026-07-14T21:52:42.847Z] -- Running command: gmake
[2026-07-14T21:52:42.847Z] -- Using working directory: /home/simba/jk/packagey/vcpkg/buildtrees/icu/build-configuration-build-configuration-rel-aix
[2026-07-14T22:10:41.263Z] -- With environment variables:
[2026-07-14T22:10:41.263Z] -- CC=/opt/IBM/openxlC/17.1.3/bin/ibm-clang_r
[2026-07-14T22:10:41.263Z] -- CXX=/opt/IBM/openxlC/17.1.3/bin/ibm-clang++_r
[2026-07-14T22:10:41.263Z] -- CFLAGS=-O2 -fPIC -fexceptions -m32 -O3 -std=c17
[2026-07-14T22:10:41.263Z] -- CXXFLAGS=-D_REENTRANT -O2 -fPIC -fexceptions -frtti -m32 -O3 -std=c++17 -D__STDC_FORMAT_MACROS
[2026-07-14T22:10:41.263Z] -- CPPFLAGS=-O2 -fPIC -fexceptions -m32 -D_REENTRANT
[2026-07-14T22:10:41.263Z] -- LDFLAGS=-O2 -Wl,-bbigtoc -brtl -m32
[2026-07-14T22:10:41.263Z] -- AR=ar -X32
[2026-07-14T22:10:41.264Z] -- OPTIONS=install
[2026-07-14T22:10:41.264Z] -- OPTIONS_RELEASE=
[2026-07-14T22:10:41.264Z] -- OPTIONS_DEBUG=
[2026-07-14T22:10:41.264Z] -- Parsing command: gmake install
[2026-07-14T22:10:41.264Z] -- Running command: gmake install
[2026-07-14T22:10:41.264Z] -- Using working directory: /home/simba/jk/packagey/vcpkg/buildtrees/icu/build-configuration-build-configuration-rel-aix

matthew-wozniczka avatar Jul 14 '26 23:07 matthew-wozniczka

I've updated the initial comment w/ the compile command.

Let me know if there's anything else that would be useful.

matthew-wozniczka avatar Jul 15 '26 18:07 matthew-wozniczka

Also, I don't have the details right now, but we get a crash in what seems to be the same place in the 64-bit build as well, so the issue probably isn't bitness-specific.

I was able to confirm in a unit test that the crash still occurs when using literals, so that rules out initialization issues w/ some global objects which were part of it before, and other corruption caused by various initialization done in the 'production' binary which the crash originally occurred in.

matthew-wozniczka avatar Jul 15 '26 20:07 matthew-wozniczka

So, it looks like disabling optimizations, via the following patch against unistr.cpp resolves the crash (at least in the unit test on 64-bit, 32-bit & rest of tests still running):

@@ -1243,9 +1243,10 @@
+__attribute__((optnone))
 UnicodeString::findAndReplace(int32_t start,
                   int32_t length,
                   const UnicodeString& oldText,
                   int32_t oldStart,
                   int32_t oldLength,
                   const UnicodeString& newText,
                   int32_t newStart,
                   int32_t newLength)
 {

matthew-wozniczka avatar Jul 16 '26 19:07 matthew-wozniczka

OK, 32-bit unit tests succeeded as well, so I think that disabling optimizations in the function will be a viable workaround for us, but would still be nice to get this fixed, wherever it's broken (I'm not familiar enough w/ XLClang to know whether that's likely to be there or here upstream)

matthew-wozniczka avatar Jul 16 '26 20:07 matthew-wozniczka

I have reproduce the wrong prologue with IR.

declare void @ext(i32, i32, i32, i32, i32, i32, i32, i32)

define i32 @foo(i32 %a0, i32 %a1, i32 %a2, i32 %a3,
                i32 %a4, i32 %a5, i32 %a6, i32 %a7) {
entry:
  ; Compute the early-exit condition
  %lsb = and i32 %a0, 1
  %cmp = icmp eq i32 %lsb, 0

  ; Define two values pinned to r0 and r11 HERE in the entry block.
  ; These must flow live into %slow so they appear in its liveins.
  ; "={r0}"  forces the output into physical r0
  ; "={r11}" forces the output into physical r11
  %v0  = call i32 asm sideeffect "li ${0}, 0", "={r0}"()
  %v11 = call i32 asm sideeffect "li ${0}, 0", "={r11}"()

  br i1 %cmp, label %slow, label %fast_return

fast_return:
  ret i32 %a0

slow:
  ; Consume %v0 and %v11 as INPUTS at the very top of this block.
  ; This forces r0 and r11 into the liveins of %slow.
  ; "r"(%v0)  → r0  must be live-in here
  ; "r"(%v11) → r11 must be live-in here
  call void asm sideeffect "", "r,r"(i32 %v0, i32 %v11)

  ; Force MustSaveCR: CR2 is callee-saved on AIX
  call void asm sideeffect "", "~{cr2}"()

  ; Force MustSaveLR: non-leaf call
  call void @ext(i32 %a0, i32 %a1, i32 %a2, i32 %a3,
                 i32 %a4, i32 %a5, i32 %a6, i32 %a7)

  %sum    = add i32 %a0, %a1
  %heavy  = mul i32 %sum,    %a2
  %heavy2 = add i32 %heavy,  %a3
  %heavy3 = mul i32 %heavy2, %a4
  %heavy4 = add i32 %heavy3, %a5
  %heavy5 = mul i32 %heavy4, %a6
  %result = add i32 %heavy5, %a7
  ret i32 %result
}

I will have fix for it.

diggerlin avatar Jul 20 '26 15:07 diggerlin

Thank you for reporting this issue.  I have fixed it in our upcoming 17.1.5 release (based on LLVM 23) later this year.  If you need the fix in service releases, please open a case with IBM Support.

diggerlin avatar Jul 22 '26 15:07 diggerlin

@diggerlin: Was fix merged into LLVM code base? If not, issue should be open until this happens.

EugeneZelenko avatar Jul 23 '26 14:07 EugeneZelenko