diff --git a/clang/include/clang/Basic/BuiltinsPPC.def b/clang/include/clang/Basic/BuiltinsPPC.def --- a/clang/include/clang/Basic/BuiltinsPPC.def +++ b/clang/include/clang/Basic/BuiltinsPPC.def @@ -165,6 +165,8 @@ BUILTIN(__builtin_ppc_get_timebase, "ULLi", "n") +BUILTIN(__builtin_ppc_kill_canary, "v", "") + // This is just a placeholder, the types and attributes are wrong. BUILTIN(__builtin_altivec_vaddcuw, "V4UiV4UiV4Ui", "") diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -122,6 +122,7 @@ Builder.defineMacro("__fetch_and_orlp", "__builtin_ppc_fetch_and_orlp"); Builder.defineMacro("__fetch_and_swap", "__builtin_ppc_fetch_and_swap"); Builder.defineMacro("__fetch_and_swaplp", "__builtin_ppc_fetch_and_swaplp"); + Builder.defineMacro("__kill_canary", "__builtin_ppc_kill_canary"); Builder.defineMacro("__ldarx", "__builtin_ppc_ldarx"); Builder.defineMacro("__lwarx", "__builtin_ppc_lwarx"); Builder.defineMacro("__lharx", "__builtin_ppc_lharx"); diff --git a/clang/test/CodeGen/PowerPC/builtins-ppc-stackprotect.c b/clang/test/CodeGen/PowerPC/builtins-ppc-stackprotect.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/PowerPC/builtins-ppc-stackprotect.c @@ -0,0 +1,23 @@ +// REQUIRES: powerpc-registered-target +// RUN: %clang_cc1 -O2 -triple powerpc64-unknown-linux-gnu \ +// RUN: -emit-llvm %s -o - -target-cpu pwr7 | \ +// RUN: FileCheck %s +// RUN: %clang_cc1 -O2 -triple powerpc64le-unknown-linux-gnu \ +// RUN: -emit-llvm %s -o - -target-cpu pwr8 | \ +// RUN: FileCheck %s +// RUN: %clang_cc1 -O2 -triple powerpc-unknown-aix \ +// RUN: -emit-llvm %s -o - -target-cpu pwr7 | \ +// RUN: FileCheck %s +// RUN: %clang_cc1 -O2 -triple powerpc64-unknown-aix \ +// RUN: -emit-llvm %s -o - -target-cpu pwr7 | \ +// RUN: FileCheck %s + +void test_builtin_ppc_kill_canary() { + // CHECK: call void @llvm.ppc.kill.canary() + __builtin_ppc_kill_canary(); +} + +void test_kill_canary() { + // CHECK: call void @llvm.ppc.kill.canary() + __kill_canary(); +} diff --git a/clang/test/CodeGen/PowerPC/builtins-ppc.c b/clang/test/CodeGen/PowerPC/builtins-ppc.c --- a/clang/test/CodeGen/PowerPC/builtins-ppc.c +++ b/clang/test/CodeGen/PowerPC/builtins-ppc.c @@ -74,3 +74,13 @@ // CHECK: call float (float, float, float, ...) @llvm.ppc.minfs(float %5, float %6, float %7, float %8) res = __builtin_ppc_minfs(a, b, c, d); } +/* +void test_builtin_ppc_kill_canary() { + // CHECK: call void @llvm.ppc.kill.canary() + __builtin_ppc_kill_canary(); +} + +void test_kill_canary() { + // CHECK: call void @llvm.ppc.kill.canary() + __kill_canary(); +}*/ diff --git a/final.patch b/final.patch new file mode 100644 --- /dev/null +++ b/final.patch @@ -0,0 +1,14 @@ +commit f11c8790cad5554c4dad7a14c0d177bacd99eb22 +Author: Paul Scoropan +Date: Tue Jun 21 13:57:35 2022 -0400 + + finished + +diff --git a/AIX-OS-headers b/AIX-OS-headers +deleted file mode 120000 +index c2adefef9991..000000000000 +--- a/AIX-OS-headers ++++ /dev/null +@@ -1 +0,0 @@ +-/home/pauls/llvm/dev_3105/AIX-OS-headers +\ No newline at end of file diff --git a/llvm/include/llvm/IR/IntrinsicsPowerPC.td b/llvm/include/llvm/IR/IntrinsicsPowerPC.td --- a/llvm/include/llvm/IR/IntrinsicsPowerPC.td +++ b/llvm/include/llvm/IR/IntrinsicsPowerPC.td @@ -217,6 +217,11 @@ : Intrinsic<[llvm_float_ty], [llvm_float_ty, llvm_float_ty, llvm_float_ty, llvm_vararg_ty], [IntrNoMem]>; + def int_ppc_kill_canary + : GCCBuiltin<"__builtin_ppc_kill_canary">, + Intrinsic<[], + [], + [IntrWriteMem, IntrHasSideEffects]>; } let TargetPrefix = "ppc" in { // All PPC intrinsics start with "llvm.ppc.". diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp --- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp +++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp @@ -11127,6 +11127,38 @@ } break; } + case Intrinsic::ppc_kill_canary: { + MachineFunction &MF = DAG.getMachineFunction(); + if (MF.getFunction().hasFnAttribute(Attribute::SafeStack)) { + break; + } + MachineFrameInfo &MFI = MF.getFrameInfo(); + int SPI = MFI.getStackProtectorIndex(); // should return -1 + + PPCFunctionInfo *FuncInfo = MF.getInfo(); + EVT PtrVT = getPointerTy(MF.getDataLayout()); + + MFI.CreateStackObject(16, Align(16), false); + SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); // frame index + + unsigned int deadBird = 0x4C6C566D; // replaces canary word + + SDValue Store = DAG.getStore( // create store instr, stores (deadBird + 0) into addr (frame index + stack protector) + Op->getOperand(0), + DL, + DAG.getConstant(deadBird, DL, MVT::i32), + DAG.getNode( // add frame index, stack protector index, return node result + ISD::ADD, + DL, + PtrVT, + FIN, + DAG.getConstant(SPI, DL, PtrVT) + ), + MachinePointerInfo() + ); + return Store; + break; + } default: break; } diff --git a/llvm/test/CodeGen/PowerPC/kill-canary-intrinsic.ll b/llvm/test/CodeGen/PowerPC/kill-canary-intrinsic.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/PowerPC/kill-canary-intrinsic.ll @@ -0,0 +1,20 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-linux-gnu < %s | \ +; RUN: FileCheck %s + + +declare void @llvm.ppc.kill.canary() +define dso_local void @test_kill_canary() { +; CHECK-LABEL: test_kill_canary: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: lis 3, 19564 +; CHECK-NEXT: ori 3, 3, 22125 +; CHECK-NEXT: stw 3, -17(1) +; CHECK-NEXT: blr +entry: + call void @llvm.ppc.kill.canary() + ret void +} + + + diff --git a/mypatch.patch b/mypatch.patch new file mode 100644 --- /dev/null +++ b/mypatch.patch @@ -0,0 +1,14 @@ +commit f11c8790cad5554c4dad7a14c0d177bacd99eb22 +Author: Paul Scoropan +Date: Tue Jun 21 13:57:35 2022 -0400 + + finished + +diff --git a/AIX-OS-headers b/AIX-OS-headers +deleted file mode 120000 +index c2adefef9991..000000000000 +--- a/AIX-OS-headers ++++ /dev/null +@@ -1 +0,0 @@ +-/home/pauls/llvm/dev_3105/AIX-OS-headers +\ No newline at end of file