Skip to content

Commit 133a6c9

Browse files
committed
[Clang] Fix crash with implicit int-to-pointer conversion
If an integer is passed to the pointer argument of the __atomic_test_and_set or __atomic_clear builtins with the int-conversion error disabled or downgraded, we crashed in codegen due to assuming that the type is always a pointer after skip[ping past implicit casts. Fixes #111293.
1 parent 667deb6 commit 133a6c9

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4928,8 +4928,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
49284928
// Look at the argument type to determine whether this is a volatile
49294929
// operation. The parameter type is always volatile.
49304930
QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
4931+
QualType PointeeTy = PtrTy->getPointeeType();
49314932
bool Volatile =
4932-
PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
4933+
PointeeTy.isNull() ? false : PointeeTy.isVolatileQualified();
49334934

49344935
Address Ptr =
49354936
EmitPointerWithAlignment(E->getArg(0)).withElementType(Int8Ty);
@@ -5011,8 +5012,9 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
50115012

50125013
case Builtin::BI__atomic_clear: {
50135014
QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
5015+
QualType PointeeTy = PtrTy->getPointeeType();
50145016
bool Volatile =
5015-
PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
5017+
PointeeTy.isNull() ? false : PointeeTy.isVolatileQualified();
50165018

50175019
Address Ptr = EmitPointerWithAlignment(E->getArg(0));
50185020
Ptr = Ptr.withElementType(Int8Ty);

clang/test/CodeGen/atomic-ops.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %clang_cc1 %s -emit-llvm -o - -ffreestanding -ffake-address-space-map -triple=i686-apple-darwin9 | FileCheck %s
1+
// RUN: %clang_cc1 %s -emit-llvm -o - -ffreestanding -ffake-address-space-map -triple=i686-apple-darwin9 -Wno-error=int-conversion | FileCheck %s
22
// REQUIRES: x86-registered-target
33

44
// Also test serialization of atomic operations here, to avoid duplicating the
55
// test.
6-
// RUN: %clang_cc1 %s -emit-pch -o %t -ffreestanding -ffake-address-space-map -triple=i686-apple-darwin9
7-
// RUN: %clang_cc1 %s -include-pch %t -ffreestanding -ffake-address-space-map -triple=i686-apple-darwin9 -emit-llvm -o - | FileCheck %s
6+
// RUN: %clang_cc1 %s -emit-pch -o %t -ffreestanding -ffake-address-space-map -triple=i686-apple-darwin9 -Wno-error=int-conversion
7+
// RUN: %clang_cc1 %s -include-pch %t -ffreestanding -ffake-address-space-map -triple=i686-apple-darwin9 -Wno-error=int-conversion -emit-llvm -o - | FileCheck %s
88
#ifndef ALREADY_INCLUDED
99
#define ALREADY_INCLUDED
1010

@@ -310,10 +310,14 @@ void test_and_set(void) {
310310
__atomic_test_and_set(&flag1, memory_order_seq_cst);
311311
// CHECK: atomicrmw volatile xchg ptr @flag2, i8 1 acquire, align 1
312312
__atomic_test_and_set(&flag2, memory_order_acquire);
313+
// CHECK: atomicrmw xchg ptr inttoptr (i32 32768 to ptr), i8 1 acquire, align 1
314+
__atomic_test_and_set(0x8000, memory_order_acquire);
313315
// CHECK: store atomic volatile i8 0, ptr @flag2 release, align 1
314316
__atomic_clear(&flag2, memory_order_release);
315317
// CHECK: store atomic i8 0, ptr @flag1 seq_cst, align 1
316318
__atomic_clear(&flag1, memory_order_seq_cst);
319+
// CHECK: store atomic i8 0, ptr inttoptr (i32 32768 to ptr) seq_cst, align 1
320+
__atomic_clear(0x8000, memory_order_seq_cst);
317321
}
318322

319323
struct Sixteen {

0 commit comments

Comments
 (0)