From b2581196eb036bd1310afd39f440e348e0e1a580 Mon Sep 17 00:00:00 2001 From: Jinsong Ji Date: Fri, 28 May 2021 02:18:12 +0000 Subject: [PATCH] [AIX] Enable stackprotect feature AIX use `__ssp_canary_word` instead of `__stack_chk_guard`. This patch update the target hook to use correct symbol, so that the basic stackprotect feature can work. The traceback will be handled in follow up patch. Reviewed By: #powerpc, shchenz Differential Revision: https://reviews.llvm.org/D103100 --- llvm/lib/Target/PowerPC/PPCISelLowering.cpp | 16 +++++++++++++- llvm/lib/Target/PowerPC/PPCISelLowering.h | 1 + llvm/lib/Transforms/IPO/Internalize.cpp | 5 ++++- llvm/test/CodeGen/PowerPC/stack-guard-oob.ll | 29 ++++++++++++++++++++++++++ llvm/test/CodeGen/PowerPC/stack-protector.ll | 6 ++++++ llvm/test/Transforms/Internalize/stackguard.ll | 6 ++++++ 6 files changed, 61 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp index 2e681a4f..6de883d 100644 --- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp +++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp @@ -130,6 +130,8 @@ static bool isNByteElemShuffleMask(ShuffleVectorSDNode *, unsigned, int); static SDValue widenVec(SelectionDAG &DAG, SDValue Vec, const SDLoc &dl); +static const char AIXSSPCanaryWordName[] = "__ssp_canary_word"; + // FIXME: Remove this once the bug has been fixed! extern cl::opt ANDIGlueBug; @@ -16406,12 +16408,24 @@ bool PPCTargetLowering::useLoadStackGuardNode() const { return true; } -// Override to disable global variable loading on Linux. +// Override to disable global variable loading on Linux and insert AIX canary +// word declaration. void PPCTargetLowering::insertSSPDeclarations(Module &M) const { + if (Subtarget.isAIXABI()) { + M.getOrInsertGlobal(AIXSSPCanaryWordName, + Type::getInt8PtrTy(M.getContext())); + return; + } if (!Subtarget.isTargetLinux()) return TargetLowering::insertSSPDeclarations(M); } +Value *PPCTargetLowering::getSDagStackGuard(const Module &M) const { + if (Subtarget.isAIXABI()) + return M.getGlobalVariable(AIXSSPCanaryWordName); + return TargetLowering::getSDagStackGuard(M); +} + bool PPCTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT, bool ForCodeSize) const { if (!VT.isSimple() || !Subtarget.hasVSX()) diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.h b/llvm/lib/Target/PowerPC/PPCISelLowering.h index 94edce5..e6f331d 100644 --- a/llvm/lib/Target/PowerPC/PPCISelLowering.h +++ b/llvm/lib/Target/PowerPC/PPCISelLowering.h @@ -1073,6 +1073,7 @@ namespace llvm { /// Override to support customized stack guard loading. bool useLoadStackGuardNode() const override; void insertSSPDeclarations(Module &M) const override; + Value *getSDagStackGuard(const Module &M) const override; bool isFPImmLegal(const APFloat &Imm, EVT VT, bool ForCodeSize) const override; diff --git a/llvm/lib/Transforms/IPO/Internalize.cpp b/llvm/lib/Transforms/IPO/Internalize.cpp index 05c32d6..ce5429a 100644 --- a/llvm/lib/Transforms/IPO/Internalize.cpp +++ b/llvm/lib/Transforms/IPO/Internalize.cpp @@ -233,7 +233,10 @@ bool InternalizePass::internalizeModule(Module &M, CallGraph *CG) { // FIXME: We should probably add this (and the __stack_chk_guard) via some // type of call-back in CodeGen. AlwaysPreserved.insert("__stack_chk_fail"); - AlwaysPreserved.insert("__stack_chk_guard"); + if (Triple(M.getTargetTriple()).isOSAIX()) + AlwaysPreserved.insert("__ssp_canary_word"); + else + AlwaysPreserved.insert("__stack_chk_guard"); // Mark all global variables with initializers that are not in the api as // internal as well. diff --git a/llvm/test/CodeGen/PowerPC/stack-guard-oob.ll b/llvm/test/CodeGen/PowerPC/stack-guard-oob.ll index ba728fe..182d037 100644 --- a/llvm/test/CodeGen/PowerPC/stack-guard-oob.ll +++ b/llvm/test/CodeGen/PowerPC/stack-guard-oob.ll @@ -1,7 +1,10 @@ ; RUN: llc -mtriple=powerpc64le -O0 < %s | FileCheck %s +; RUN: llc -mtriple=powerpc64-ibm-aix-xcoff -O0 < %s | FileCheck %s --check-prefix=AIX +; RUN: llc -mtriple=powerpc-ibm-aix-xcoff -O0 < %s | FileCheck %s --check-prefix=AIX ; CHECK-LABEL: in_bounds: ; CHECK-NOT: __stack_chk_guard +; AIX-NOT: __ssp_canary_word define i32 @in_bounds() #0 { %var = alloca i32, align 4 store i32 0, i32* %var, align 4 @@ -12,6 +15,7 @@ define i32 @in_bounds() #0 { ; CHECK-LABEL: constant_out_of_bounds: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @constant_out_of_bounds() #0 { %var = alloca i32, align 4 store i32 0, i32* %var, align 4 @@ -22,6 +26,7 @@ define i32 @constant_out_of_bounds() #0 { ; CHECK-LABEL: nonconstant_out_of_bounds: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @nonconstant_out_of_bounds(i32 %n) #0 { %var = alloca i32, align 4 store i32 0, i32* %var, align 4 @@ -32,6 +37,7 @@ define i32 @nonconstant_out_of_bounds(i32 %n) #0 { ; CHECK-LABEL: phi_before_gep_in_bounds: ; CHECK-NOT: __stack_chk_guard +; AIX-NOT: __ssp_canary_word define i32 @phi_before_gep_in_bounds(i32 %k) #0 { entry: %var1 = alloca i32, align 4 @@ -53,6 +59,7 @@ then: ; CHECK-LABEL: phi_before_gep_constant_out_of_bounds: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @phi_before_gep_constant_out_of_bounds(i32 %k) #0 { entry: %var1 = alloca i32, align 4 @@ -74,6 +81,7 @@ then: ; CHECK-LABEL: phi_before_gep_nonconstant_out_of_bounds: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @phi_before_gep_nonconstant_out_of_bounds(i32 %k, i32 %n) #0 { entry: %var1 = alloca i32, align 4 @@ -95,6 +103,7 @@ then: ; CHECK-LABEL: phi_after_gep_in_bounds: ; CHECK-NOT: __stack_chk_guard +; AIX-NOT: __ssp_canary_word define i32 @phi_after_gep_in_bounds(i32 %k) #0 { entry: %var1 = alloca i32, align 4 @@ -120,6 +129,7 @@ then: ; CHECK-LABEL: phi_after_gep_constant_out_of_bounds_a: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @phi_after_gep_constant_out_of_bounds_a(i32 %k) #0 { entry: %var1 = alloca i32, align 4 @@ -145,6 +155,7 @@ then: ; CHECK-LABEL: phi_after_gep_constant_out_of_bounds_b: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @phi_after_gep_constant_out_of_bounds_b(i32 %k) #0 { entry: %var1 = alloca i32, align 4 @@ -170,6 +181,7 @@ then: ; CHECK-LABEL: phi_different_types_a: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i64 @phi_different_types_a(i32 %k) #0 { entry: %var1 = alloca i64, align 4 @@ -191,6 +203,7 @@ then: ; CHECK-LABEL: phi_different_types_b: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i64 @phi_different_types_b(i32 %k) #0 { entry: %var1 = alloca i32, align 4 @@ -212,6 +225,7 @@ then: ; CHECK-LABEL: phi_after_gep_nonconstant_out_of_bounds_a: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @phi_after_gep_nonconstant_out_of_bounds_a(i32 %k, i32 %n) #0 { entry: %var1 = alloca i32, align 4 @@ -237,6 +251,7 @@ then: ; CHECK-LABEL: phi_after_gep_nonconstant_out_of_bounds_b: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @phi_after_gep_nonconstant_out_of_bounds_b(i32 %k, i32 %n) #0 { entry: %var1 = alloca i32, align 4 @@ -265,6 +280,7 @@ then: ; CHECK-LABEL: struct_in_bounds: ; CHECK-NOT: __stack_chk_guard +; AIX-NOT: __ssp_canary_word define void @struct_in_bounds() #0 { %var = alloca %struct.outer, align 4 %outergep = getelementptr inbounds %struct.outer, %struct.outer* %var, i32 0, i32 1 @@ -275,6 +291,7 @@ define void @struct_in_bounds() #0 { ; CHECK-LABEL: struct_constant_out_of_bounds_a: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define void @struct_constant_out_of_bounds_a() #0 { %var = alloca %struct.outer, align 4 %outergep = getelementptr inbounds %struct.outer, %struct.outer* %var, i32 1, i32 0 @@ -287,6 +304,7 @@ define void @struct_constant_out_of_bounds_a() #0 { ; Here the offset is out-of-bounds of the addressed struct.inner member, but ; still within bounds of the outer struct so no stack guard is needed. ; CHECK-NOT: __stack_chk_guard +; AIX-NOT: __ssp_canary_word define void @struct_constant_out_of_bounds_b() #0 { %var = alloca %struct.outer, align 4 %outergep = getelementptr inbounds %struct.outer, %struct.outer* %var, i32 0, i32 0 @@ -298,6 +316,7 @@ define void @struct_constant_out_of_bounds_b() #0 { ; CHECK-LABEL: struct_constant_out_of_bounds_c: ; Here we are out-of-bounds of both the inner and outer struct. ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define void @struct_constant_out_of_bounds_c() #0 { %var = alloca %struct.outer, align 4 %outergep = getelementptr inbounds %struct.outer, %struct.outer* %var, i32 0, i32 1 @@ -308,6 +327,7 @@ define void @struct_constant_out_of_bounds_c() #0 { ; CHECK-LABEL: struct_nonconstant_out_of_bounds_a: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define void @struct_nonconstant_out_of_bounds_a(i32 %n) #0 { %var = alloca %struct.outer, align 4 %outergep = getelementptr inbounds %struct.outer, %struct.outer* %var, i32 %n, i32 0 @@ -318,6 +338,7 @@ define void @struct_nonconstant_out_of_bounds_a(i32 %n) #0 { ; CHECK-LABEL: struct_nonconstant_out_of_bounds_b: ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define void @struct_nonconstant_out_of_bounds_b(i32 %n) #0 { %var = alloca %struct.outer, align 4 %outergep = getelementptr inbounds %struct.outer, %struct.outer* %var, i32 0, i32 0 @@ -328,6 +349,7 @@ define void @struct_nonconstant_out_of_bounds_b(i32 %n) #0 { ; CHECK-LABEL: bitcast_smaller_load ; CHECK-NOT: __stack_chk_guard +; AIX-NOT: __ssp_canary_word define i32 @bitcast_smaller_load() #0 { %var = alloca i64, align 4 store i64 0, i64* %var, align 4 @@ -338,6 +360,7 @@ define i32 @bitcast_smaller_load() #0 { ; CHECK-LABEL: bitcast_same_size_load ; CHECK-NOT: __stack_chk_guard +; AIX-NOT: __ssp_canary_word define i32 @bitcast_same_size_load() #0 { %var = alloca i64, align 4 store i64 0, i64* %var, align 4 @@ -349,6 +372,7 @@ define i32 @bitcast_same_size_load() #0 { ; CHECK-LABEL: bitcast_larger_load ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i64 @bitcast_larger_load() #0 { %var = alloca i32, align 4 store i32 0, i32* %var, align 4 @@ -359,6 +383,7 @@ define i64 @bitcast_larger_load() #0 { ; CHECK-LABEL: bitcast_larger_store ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @bitcast_larger_store() #0 { %var = alloca i32, align 4 %bitcast = bitcast i32* %var to i64* @@ -369,6 +394,7 @@ define i32 @bitcast_larger_store() #0 { ; CHECK-LABEL: bitcast_larger_cmpxchg ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i64 @bitcast_larger_cmpxchg(i64 %desired, i64 %new) #0 { %var = alloca i32, align 4 %bitcast = bitcast i32* %var to i64* @@ -379,6 +405,7 @@ define i64 @bitcast_larger_cmpxchg(i64 %desired, i64 %new) #0 { ; CHECK-LABEL: bitcast_larger_atomic_rmw ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i64 @bitcast_larger_atomic_rmw() #0 { %var = alloca i32, align 4 %bitcast = bitcast i32* %var to i64* @@ -390,6 +417,7 @@ define i64 @bitcast_larger_atomic_rmw() #0 { ; CHECK-LABEL: bitcast_overlap ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @bitcast_overlap() #0 { %var = alloca i32, align 4 %bitcast = bitcast i32* %var to %struct.packed* @@ -402,6 +430,7 @@ define i32 @bitcast_overlap() #0 { ; CHECK-LABEL: multi_dimensional_array ; CHECK: __stack_chk_guard +; AIX: __ssp_canary_word define i32 @multi_dimensional_array() #0 { %var = alloca %struct.multi_dimensional, align 4 %gep1 = getelementptr inbounds %struct.multi_dimensional, %struct.multi_dimensional* %var, i32 0, i32 0 diff --git a/llvm/test/CodeGen/PowerPC/stack-protector.ll b/llvm/test/CodeGen/PowerPC/stack-protector.ll index 95985b9..1fcce55 100644 --- a/llvm/test/CodeGen/PowerPC/stack-protector.ll +++ b/llvm/test/CodeGen/PowerPC/stack-protector.ll @@ -1,12 +1,18 @@ ; RUN: llc -verify-machineinstrs -mtriple=powerpc-unknown-linux < %s | FileCheck -check-prefix=LINUX32 %s ; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-linux < %s | FileCheck -check-prefix=LINUX64 %s ; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-linux < %s | FileCheck -check-prefix=LINUX64 %s +; RUN: llc -verify-machineinstrs -mtriple=powerpc-ibm-aix-xcoff < %s | FileCheck -check-prefix=AIX32 %s +; RUN: llc -verify-machineinstrs -mtriple=powerpc64-ibm-aix-xcoff < %s | FileCheck -check-prefix=AIX64 %s ; LINUX32: lwz {{[0-9]+}}, -28680(2) ; LINUX64: ld {{[0-9]+}}, -28688(13) +; AIX32: lwz {{.*}}__ssp_canary_word +; AIX64: ld {{.*}}__ssp_canary_word ; LINUX32: __stack_chk_fail ; LINUX64: __stack_chk_fail +; AIX32: __stack_chk_fail +; AIX64: __stack_chk_fail @"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00" ; <[11 x i8]*> [#uses=1] diff --git a/llvm/test/Transforms/Internalize/stackguard.ll b/llvm/test/Transforms/Internalize/stackguard.ll index e9dc6cc..86fa3f8 100644 --- a/llvm/test/Transforms/Internalize/stackguard.ll +++ b/llvm/test/Transforms/Internalize/stackguard.ll @@ -1,9 +1,15 @@ ; __stack_chk_guard and __stack_chk_fail should not be internalized. ; RUN: opt < %s -internalize -S | FileCheck %s ; RUN: opt < %s -passes=internalize -S | FileCheck %s +; RUN: opt -mtriple=powerpc64-ibm-aix-xcoff < %s -passes=internalize -S | FileCheck %s --check-prefix=AIX ; CHECK: @__stack_chk_guard = hidden global [8 x i64] zeroinitializer, align 16 +; AIX: @__stack_chk_guard = internal global [8 x i64] zeroinitializer, align 16 @__stack_chk_guard = hidden global [8 x i64] zeroinitializer, align 16 ; CHECK: @__stack_chk_fail = hidden global [8 x i64] zeroinitializer, align 16 +; AIX: @__stack_chk_fail = hidden global [8 x i64] zeroinitializer, align 16 @__stack_chk_fail = hidden global [8 x i64] zeroinitializer, align 16 + +; AIX: @__ssp_canary_word = hidden global [8 x i64] zeroinitializer, align 16 +@__ssp_canary_word = hidden global [8 x i64] zeroinitializer, align 16 -- 2.7.4