From 9addd946958d072ff5f21044461fde27fa626770 Mon Sep 17 00:00:00 2001 From: Ming-Yi Lai Date: Fri, 10 May 2024 14:16:59 +0800 Subject: [PATCH] [clang][RISCV] Add Zicfilp CFI scheme preprocessor macros These macros allow assembly files to know which CFI label to use when the target has Zicfilp enabled. --- clang/lib/Basic/Targets/RISCV.cpp | 28 ++++++ .../test/CodeGen/RISCV/riscv-cf-protection.c | 99 +++++++++++++------ 2 files changed, 95 insertions(+), 32 deletions(-) diff --git a/clang/lib/Basic/Targets/RISCV.cpp b/clang/lib/Basic/Targets/RISCV.cpp index d6f09ce70cbec6..32fb2e003a4a71 100644 --- a/clang/lib/Basic/Targets/RISCV.cpp +++ b/clang/lib/Basic/Targets/RISCV.cpp @@ -225,6 +225,34 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts, else Builder.defineMacro("__riscv_32e"); } + + if (Opts.CFProtectionBranch) { + if (checkCFProtectionBranchSupported()) { + auto Scheme = Opts.getCFBranchLabelScheme(); + if (checkCFBranchLabelSchemeSupported(Scheme)) { + if (Scheme == CFBranchLabelSchemeKind::Default) + Scheme = getDefaultCFBranchLabelScheme(); + + Builder.defineMacro("__riscv_landing_pad", "1"); + switch (Scheme) { + case CFBranchLabelSchemeKind::Unlabeled: + Builder.defineMacro("__riscv_landing_pad_unlabeled", "1"); + break; + case CFBranchLabelSchemeKind::FuncSig: + Builder.defineMacro("__riscv_landing_pad_func_sig", "1"); + break; + case CFBranchLabelSchemeKind::Default: + llvm_unreachable("default cf-branch-label scheme should already be " + "transformed to other scheme"); + } + } else + Diags.Report(diag::err_opt_not_valid_on_target) + << (Twine("-mcf-branch-label-scheme=") + + getCFBranchLabelSchemeFlagVal(Scheme)) + .str(); + } else + Diags.Report(diag::err_opt_not_valid_on_target) << "cf-protection=branch"; + } } static constexpr Builtin::Info BuiltinInfo[] = { diff --git a/clang/test/CodeGen/RISCV/riscv-cf-protection.c b/clang/test/CodeGen/RISCV/riscv-cf-protection.c index 02a22006b1638f..4de45e0c4ef416 100644 --- a/clang/test/CodeGen/RISCV/riscv-cf-protection.c +++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c @@ -1,55 +1,90 @@ +// Default cf-branch-label-scheme is func-sig // RUN: %clang --target=riscv32 -menable-experimental-extensions \ -// RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %s +// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s +// RUN: %clang --target=riscv64 -menable-experimental-extensions \ +// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s + +// RUN: %clang --target=riscv32 -menable-experimental-extensions \ +// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s // RUN: %clang --target=riscv32 -menable-experimental-extensions \ -// RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s +// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s + +// RUN: %clang --target=riscv32 -menable-experimental-extensions \ +// RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-UNLABELED-SCHEME-UNUSED %s + +// RUN: %clang --target=riscv32 -menable-experimental-extensions \ +// RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-FUNC-SIG-SCHEME-UNUSED %s // RUN: not %clang --target=riscv32 -fcf-protection=branch \ -// RUN: -mcf-branch-label-scheme=unlabeled -c %s -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s +// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-BRANCH-PROT-INVALID %s // RUN: not %clang --target=riscv32 -fcf-protection=branch \ -// RUN: -mcf-branch-label-scheme=func-sig -c %s -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s +// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-BRANCH-PROT-INVALID %s + +// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-UNLABELED-SCHEME-UNUSED %s -// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %s +// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=func-sig -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-FUNC-SIG-SCHEME-UNUSED %s -// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=func-sig -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s +// RUN: %clang --target=riscv64 -menable-experimental-extensions \ +// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-UNLABELED %s + +// RUN: %clang --target=riscv64 -menable-experimental-extensions \ +// RUN: -march=rv64i_zicfilp1p0 -fcf-protection=branch \ +// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - \ +// RUN: | FileCheck --check-prefix=CHECK-ZICFILP-FUNC-SIG %s // RUN: %clang --target=riscv64 -menable-experimental-extensions \ -// RUN: -march=rv64i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %s +// RUN: -march=rv64i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-UNLABELED-SCHEME-UNUSED %s // RUN: %clang --target=riscv64 -menable-experimental-extensions \ -// RUN: -march=rv64i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s +// RUN: -march=rv64i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-FUNC-SIG-SCHEME-UNUSED %s // RUN: not %clang --target=riscv64 -fcf-protection=branch \ -// RUN: -mcf-branch-label-scheme=unlabeled -c %s -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s +// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-BRANCH-PROT-INVALID %s // RUN: not %clang --target=riscv64 -fcf-protection=branch \ -// RUN: -mcf-branch-label-scheme=func-sig -c %s -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s +// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-BRANCH-PROT-INVALID %s -// RUN: %clang --target=riscv64 -mcf-branch-label-scheme=unlabeled -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %s +// RUN: %clang --target=riscv64 -mcf-branch-label-scheme=unlabeled -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-UNLABELED-SCHEME-UNUSED %s -// RUN: %clang --target=riscv64 -mcf-branch-label-scheme=func-sig -c %s \ -// RUN: -o /dev/null 2>&1 \ -// RUN: | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s +// RUN: %clang --target=riscv64 -mcf-branch-label-scheme=func-sig -E -dM %s \ +// RUN: -o - 2>&1 | FileCheck \ +// RUN: --check-prefixes=CHECK-NO-MACRO,CHECK-FUNC-SIG-SCHEME-UNUSED %s +// CHECK-ZICFILP-UNLABELED: __riscv_landing_pad 1{{$}} +// CHECK-ZICFILP-UNLABELED: __riscv_landing_pad_unlabeled 1{{$}} +// CHECK-ZICFILP-FUNC-SIG: __riscv_landing_pad 1{{$}} +// CHECK-ZICFILP-FUNC-SIG: __riscv_landing_pad_func_sig 1{{$}} +// CHECK-NO-MACRO-NOT: __riscv_landing_pad +// CHECK-NO-MACRO-NOT: __riscv_landing_pad_unlabeled +// CHECK-NO-MACRO-NOT: __riscv_landing_pad_func_sig // CHECK-BRANCH-PROT-INVALID: error: option 'cf-protection=branch' cannot be // CHECK-BRANCH-PROT-INVALID-SAME: specified on this target // CHECK-UNLABELED-SCHEME-UNUSED: warning: argument unused during compilation: