Skip to content

Commit

Permalink
[clang][RISCV] Add Zicfilp CFI scheme preprocessor macros
Browse files Browse the repository at this point in the history
These macros allow assembly files to know which CFI label to use when the target
has Zicfilp enabled.
  • Loading branch information
mylai-mtk committed Sep 25, 2024
1 parent 60a6830 commit 9addd94
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 32 deletions.
28 changes: 28 additions & 0 deletions clang/lib/Basic/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down
99 changes: 67 additions & 32 deletions clang/test/CodeGen/RISCV/riscv-cf-protection.c
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 9addd94

Please sign in to comment.