Skip to content

[clang][RISCV] Introduce preprocessor macro when Zicfiss-based shadow stack is enabled #127592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ LANGOPT(ObjCDisableDirectMethodsForTesting, 1, 0,
LANGOPT(CFProtectionBranch , 1, 0, "Control-Flow Branch Protection enabled")
ENUM_LANGOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2, CFBranchLabelSchemeKind::Default,
"Control-Flow Branch Protection Label Scheme")
LANGOPT(CFProtectionReturn, 1, 0, "Control-Flow Return Protection enabled")
LANGOPT(FakeAddressSpaceMap , 1, 0, "OpenCL fake address space map")
ENUM_LANGOPT(AddressSpaceMapMangling , AddrSpaceMapMangling, 2, ASMM_Target, "OpenCL address space map mangling mode")
LANGOPT(IncludeDefaultHeader, 1, 0, "Include default header file for OpenCL")
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Basic/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
else
Builder.defineMacro("__riscv_32e");
}

if (Opts.CFProtectionReturn && ISAInfo->hasExtension("zicfiss"))
Builder.defineMacro("__riscv_shadow_stack");
}

static constexpr int NumRVVBuiltins =
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4048,8 +4048,13 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,

if (const Arg *A = Args.getLastArg(OPT_fcf_protection_EQ)) {
StringRef Name = A->getValue();
if (Name == "full" || Name == "branch") {
if (Name == "full") {
Opts.CFProtectionBranch = 1;
Opts.CFProtectionReturn = 1;
} else if (Name == "branch") {
Opts.CFProtectionBranch = 1;
} else if (Name == "return") {
Opts.CFProtectionReturn = 1;
}
}

Expand Down
44 changes: 44 additions & 0 deletions clang/test/Preprocessor/riscv-cf-protection-return.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: %clang --target=riscv32 -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv32 -fcf-protection=return -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv32 -fcf-protection=full -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv32 -march=rv32i_zicfiss1p0 \
// RUN: -menable-experimental-extensions -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv32 -march=rv32i_zicfiss1p0 \
// RUN: -menable-experimental-extensions -fcf-protection=return -E -dM %s \
// RUN: -o - | FileCheck --check-prefixes=SHSTK-MACRO %s

// RUN: %clang --target=riscv32 -march=rv32i_zicfiss1p0 \
// RUN: -menable-experimental-extensions -fcf-protection=full -E -dM %s -o - \
// RUN: | FileCheck --check-prefixes=SHSTK-MACRO %s

// RUN: %clang --target=riscv64 -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv64 -fcf-protection=return -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv64 -fcf-protection=full -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv64 -march=rv64i_zicfiss1p0 \
// RUN: -menable-experimental-extensions -E -dM %s -o - | \
// RUN: FileCheck --check-prefixes=NO-MACRO %s

// RUN: %clang --target=riscv64 -march=rv64i_zicfiss1p0 \
// RUN: -menable-experimental-extensions -fcf-protection=return -E -dM %s \
// RUN: -o - | FileCheck --check-prefixes=SHSTK-MACRO %s

// RUN: %clang --target=riscv64 -march=rv64i_zicfiss1p0 \
// RUN: -menable-experimental-extensions -fcf-protection=full -E -dM %s -o - \
// RUN: | FileCheck --check-prefixes=SHSTK-MACRO %s

// SHSTK-MACRO: __riscv_shadow_stack 1{{$}}
// NO-MACRO-NOT: __riscv_shadow_stack