[llvm] [BOLT][AArch64] Avoid UB due to shift of negative value. (PR #174994)

Harald van Dijk via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 8 07:34:22 PST 2026


https://github.com/hvdijk created https://github.com/llvm/llvm-project/pull/174994

A build with LLVM_USE_SANITIZER=Undefined showed:

  bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp:2277:60:
  runtime error: left shift of negative value -32768

This showed up in bolt/test/AArch64/veneer-lite-mode.s.

It is valid for ADRP's operand to be negative, and not valid to shift it like that. To perform this shift reliably, cast the value to unsigned.

>From 8b65930bf746f8a2b50981ac1da84d8fbeedf524 Mon Sep 17 00:00:00 2001
From: Harald van Dijk <hdijk at accesssoftek.com>
Date: Thu, 8 Jan 2026 15:31:07 +0000
Subject: [PATCH] [BOLT][AArch64] Avoid UB due to shift of negative value.

A build with LLVM_USE_SANITIZER=Undefined showed:

  bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp:2277:60:
  runtime error: left shift of negative value -32768

This showed up in bolt/test/AArch64/veneer-lite-mode.s.

It is valid for ADRP's operand to be negative, and not valid to shift it
like that. To perform this shift reliably, cast the value to unsigned.
---
 bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp b/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp
index 5c030aed26190..7c7515b5ed222 100644
--- a/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp
+++ b/bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp
@@ -2274,7 +2274,7 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
         !I->getOperand(1).isImm() || I->getOperand(0).getReg() != AArch64::X16)
       return 0;
     TargetHiBits = &*I;
-    Addr |= (Address + ((int64_t)I->getOperand(1).getImm() << 12)) &
+    Addr |= (Address + ((uint64_t)I->getOperand(1).getImm() << 12)) &
             0xFFFFFFFFFFFFF000ULL;
     Target = Addr;
     return 3;



More information about the llvm-commits mailing list