Skip to content

[GlobalOpt] Bail out on non-ConstExprs in isSimpleEnoughtToCommit. #143400

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 3 commits into from
Jun 11, 2025

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Jun 9, 2025

Bail out for non ConstantExpr constants in isSimpleEnoughValueToCommitHelper to prevent crash for non-ConstantExpr constants

@llvmbot
Copy link
Member

llvmbot commented Jun 9, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Florian Hahn (fhahn)

Changes

Bail out for ConstantPtrAuth constants in isSimpleEnoughValueToCommitHelper to prevent crash in cast<ConstantExpr> below.


Full diff: https://github.com/llvm/llvm-project/pull/143400.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/Evaluator.cpp (+3)
  • (added) llvm/test/Transforms/GlobalOpt/global-constructor-ptrauth-constant.ll (+27)
diff --git a/llvm/lib/Transforms/Utils/Evaluator.cpp b/llvm/lib/Transforms/Utils/Evaluator.cpp
index 2af447aadce22..ecdcdf55d5089 100644
--- a/llvm/lib/Transforms/Utils/Evaluator.cpp
+++ b/llvm/lib/Transforms/Utils/Evaluator.cpp
@@ -74,6 +74,9 @@ isSimpleEnoughValueToCommitHelper(Constant *C,
     return true;
   }
 
+  if (isa<ConstantPtrAuth>(C))
+    return false;
+
   // We don't know exactly what relocations are allowed in constant expressions,
   // so we allow &global+constantoffset, which is safe and uniformly supported
   // across targets.
diff --git a/llvm/test/Transforms/GlobalOpt/global-constructor-ptrauth-constant.ll b/llvm/test/Transforms/GlobalOpt/global-constructor-ptrauth-constant.ll
new file mode 100644
index 0000000000000..ef64b3eb1147c
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/global-constructor-ptrauth-constant.ll
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
+; RUN: opt -p globalopt -S %s | FileCheck %s
+
+@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @ctor, ptr null }]
+
+@foo = internal global ptr null
+
+declare void @user(ptr)
+
+;.
+; CHECK: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @ctor, ptr null }]
+; CHECK: @foo = internal global ptr null
+;.
+define void @ctor() {
+; CHECK-LABEL: define void @ctor() {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DST:%.*]] = alloca ptr, align 8
+; CHECK-NEXT:    store ptr ptrauth (ptr @foo, i32 0), ptr [[DST]], align 8
+; CHECK-NEXT:    call void @user(ptr [[DST]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %dst = alloca ptr, align 8
+  store ptr ptrauth (ptr @foo, i32 0), ptr %dst, align 8
+  call void @user(ptr %dst)
+  ret void
+}

@@ -74,6 +74,9 @@ isSimpleEnoughValueToCommitHelper(Constant *C,
return true;
}

if (isa<ConstantPtrAuth>(C))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably bail out on all non-ConstantExpr (use dyn_cast below)? There are a bunch of other special constants like dso_local_equivalent and no_cfi.

Copy link
Contributor

Choose a reason for hiding this comment

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

Obviously the early return works, but dyn_cast certainly seems like the robust solution - there doesn't seem to be any obvious indication that C is required to be a specific set of subclasses of Constant.

I don't know the impact of return false vs true - I don't know backend concepts so I don't know what it means to commit a helper here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use dyn_cast, thanks.

This checks if the constant can be lowered to something constant (or almost constant) in the backend. I'm not 100% sure how exactly the result is used, but presumably to decide whether it is worth propagating the constant to multiple places.

I think in general, ConstanPtrAuth will lowered to a set of instructions to materlize the address, followed by a ptrauth instruction, e.g. for the one in the example:

        adrp	x16, _foo@PAGE
	add	x16, x16, _foo@PAGEOFF
	paciza	x16

Copy link
Contributor

Choose a reason for hiding this comment

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

The check exists to determine whether the result is a relocatable constant that can be used as a global variable initializer.

Copy link
Contributor

Choose a reason for hiding this comment

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

@fhahn what is the [Obj-]C[++] code that led to this error? if it's a global constant we may want to see if it can be turned into a loader fixup (not something that needs to be fixed in this patch of course).

This LGTM, but would like to be sure @nikic agrees before it's merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests with the different constants

@fhahn fhahn force-pushed the globalopt-ptrauth-constant branch from f67b862 to fd20427 Compare June 10, 2025 08:11
Copy link
Contributor

@ojhunt ojhunt left a comment

Choose a reason for hiding this comment

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

Approved but I'd like @nikic to approve as well.

I wonder if you can also construct tests for the cases @nikic mentioned (dso_local_equivalent, etc)

fhahn added 2 commits June 11, 2025 18:20
Bail out for ConstantPtrAuth constants in isSimpleEnoughValueToCommitHelper
to prevent crash in cast<ConstantExpr> below.
@fhahn fhahn force-pushed the globalopt-ptrauth-constant branch from fd20427 to 956c7c0 Compare June 11, 2025 19:38
@fhahn fhahn force-pushed the globalopt-ptrauth-constant branch from 956c7c0 to bfd7b01 Compare June 11, 2025 19:38
@fhahn fhahn changed the title [GlobalOpt] Bail out for ConstantPtrAuth in isSimpleEnoughtToCommit. [GlobalOpt] Bail out on non-ConstExprs in isSimpleEnoughtToCommit. Jun 11, 2025
@ojhunt
Copy link
Contributor

ojhunt commented Jun 11, 2025

@fhahn did you verify the additional tests also fail without the fix (Oliver's test case paranoia :D)

@fhahn
Copy link
Contributor Author

fhahn commented Jun 11, 2025

@fhahn did you verify the additional tests also fail without the fix (Oliver's test case paranoia :D)

Yep

@fhahn fhahn merged commit c0c0f60 into llvm:main Jun 11, 2025
7 checks passed
@fhahn fhahn deleted the globalopt-ptrauth-constant branch June 11, 2025 21:09
@ojhunt
Copy link
Contributor

ojhunt commented Jun 11, 2025

@fhahn did you verify the additional tests also fail without the fix (Oliver's test case paranoia :D)

Yep

even more approved than before :D

llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 11, 2025
…oCommit. (#143400)

Bail out for non ConstantExpr constants in
isSimpleEnoughValueToCommitHelper to prevent crash for non-ConstantExpr
constants

PR: llvm/llvm-project#143400
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
…lvm#143400)

Bail out for non ConstantExpr constants in
isSimpleEnoughValueToCommitHelper to prevent crash for non-ConstantExpr
constants

PR: llvm#143400
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
…lvm#143400)

Bail out for non ConstantExpr constants in
isSimpleEnoughValueToCommitHelper to prevent crash for non-ConstantExpr
constants

PR: llvm#143400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants