Skip to content

[Clang][CodeGen] Add workaround for old glibc __PTR_ALIGN macro #137851

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 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,16 @@ bool BinaryOperator::isNullPointerArithmeticExtension(ASTContext &Ctx,
return false;
}

// Workaround for old glibc's __PTR_ALIGN macro
if (auto *Select =
dyn_cast<ConditionalOperator>(PExp->IgnoreParenNoopCasts(Ctx))) {
// If the condition can be constant evaluated, we check the selected arm.
bool EvalResult;
if (!Select->getCond()->EvaluateAsBooleanCondition(EvalResult, Ctx))
return false;
PExp = EvalResult ? Select->getTrueExpr() : Select->getFalseExpr();
}

// Check that the pointer is a nullptr.
if (!PExp->IgnoreParenCasts()
->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
Expand Down
62 changes: 62 additions & 0 deletions clang/test/CodeGen/glibc_ptr_align.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -O3 -o - -emit-llvm %s | FileCheck %s

// Make sure that we do not set inbounds flag if the base pointer may be a constant null.

// CHECK-LABEL: define dso_local ptr @glibc_ptr_align(
// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
// CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
// CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[AND]] to ptr
// CHECK-NEXT: ret ptr [[TMP0]]
//
char *glibc_ptr_align(char *base, char *pointer, long align_mask) {
return (sizeof(long int) < sizeof(void *) ? (base) : (char *)0) +
(((pointer) -
(sizeof(long int) < sizeof(void *) ? (base) : (char *)0) +
(align_mask)) &
~(align_mask));
}

// CHECK-LABEL: define dso_local ptr @glibc_ptr_align_commuted(
// CHECK-SAME: ptr noundef readnone captures(none) [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]]) local_unnamed_addr #[[ATTR0]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
// CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
// CHECK-NEXT: [[TMP0:%.*]] = inttoptr i64 [[AND]] to ptr
// CHECK-NEXT: ret ptr [[TMP0]]
//
char *glibc_ptr_align_commuted(char *base, char *pointer, long align_mask) {
return (sizeof(long int) >= sizeof(void *) ? (char *)0 : (base)) +
(((pointer) -
(sizeof(long int) >= sizeof(void *) ? (char *)0 : (base)) +
(align_mask)) &
~(align_mask));
}

// CHECK-LABEL: define dso_local ptr @glibc_ptr_align_non_constexpr(
// CHECK-SAME: ptr noundef [[BASE:%.*]], ptr noundef [[POINTER:%.*]], i64 noundef [[ALIGN_MASK:%.*]], i32 noundef [[COND:%.*]]) local_unnamed_addr #[[ATTR0]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[COND]], 0
// CHECK-NEXT: [[COND1:%.*]] = select i1 [[TOBOOL_NOT]], ptr null, ptr [[BASE]]
// CHECK-NEXT: [[SUB_PTR_LHS_CAST:%.*]] = ptrtoint ptr [[POINTER]] to i64
// CHECK-NEXT: [[SUB_PTR_RHS_CAST:%.*]] = ptrtoint ptr [[COND1]] to i64
// CHECK-NEXT: [[SUB_PTR_SUB:%.*]] = add i64 [[ALIGN_MASK]], [[SUB_PTR_LHS_CAST]]
// CHECK-NEXT: [[ADD:%.*]] = sub i64 [[SUB_PTR_SUB]], [[SUB_PTR_RHS_CAST]]
// CHECK-NEXT: [[NOT:%.*]] = xor i64 [[ALIGN_MASK]], -1
// CHECK-NEXT: [[AND:%.*]] = and i64 [[ADD]], [[NOT]]
// CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr [[COND1]], i64 [[AND]]
// CHECK-NEXT: ret ptr [[ADD_PTR]]
//
char *glibc_ptr_align_non_constexpr(char *base, char *pointer, long align_mask, int cond) {
return (cond ? (base) : (char *)0) +
(((pointer) -
(cond ? (base) : (char *)0) +
(align_mask)) &
~(align_mask));
}