[clang] [clang] Fix FnInfoOpts::operator&= and FnInfoOpts::operator|= not updating assigned operands (PR #107050)

2024-09-04 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/107050

>From e542003d82acfad40e1a3174ca23edf6e08a753f Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 19 Jun 2024 11:28:52 +0800
Subject: [PATCH] [clang] Fix bug of FnInfoOpts::operator&= and
 FnInfoOpts::operator|= not updating assigned operands

---
 clang/lib/CodeGen/CGCall.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.h b/clang/lib/CodeGen/CGCall.h
index 6fa65e1916183a..92e0cc43919ca4 100644
--- a/clang/lib/CodeGen/CGCall.h
+++ b/clang/lib/CodeGen/CGCall.h
@@ -450,12 +450,12 @@ inline FnInfoOpts operator&(FnInfoOpts A, FnInfoOpts B) {
  llvm::to_underlying(B));
 }
 
-inline FnInfoOpts operator|=(FnInfoOpts A, FnInfoOpts B) {
+inline FnInfoOpts &operator|=(FnInfoOpts &A, FnInfoOpts B) {
   A = A | B;
   return A;
 }
 
-inline FnInfoOpts operator&=(FnInfoOpts A, FnInfoOpts B) {
+inline FnInfoOpts &operator&=(FnInfoOpts &A, FnInfoOpts B) {
   A = A & B;
   return A;
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][RISCV] Introduce CodeGenModule::calcRISCVZicfilpFuncSigLabel() (PR #111661)

2024-10-09 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk created 
https://github.com/llvm/llvm-project/pull/111661

This method calculates a CFI label used in the RISC-V Zicfilp func-sig CFI 
scheme for a given function type/declaration. The scheme, according to psABI, 
encodes the label based on function signature, and the rules are modified from 
the Itanium C++ ABI mangling rule to allow functions (callees) that are called 
indirectly to have the expected label as indicated by the function pointer type 
seen at the call site (caller).

This method is a pre-requisite to enable RISC-V Zicfilp CFI with func-sig label 
scheme.

The implemented psABI scheme can be found at 
riscv-non-isa/riscv-elf-psabi-doc#434 .

>From ea2aa89cb3c886f1d9ebae05088f7f2401811c3b Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 31 May 2024 17:03:04 +0800
Subject: [PATCH] [clang][RISCV] Introduce
 CodeGenModule::calcRISCVZicfilpFuncSigLabel()

This method calculates a CFI label used in the RISC-V Zicfilp func-sig CFI
scheme for a given function type/declaration. The scheme, according to psABI,
encodes the label based on function signature, and the rules are modified from
the Itanium C++ ABI mangling rule to allow functions (callees) that are called
indirectly to have the expected label as indicated by the function pointer type
seen at the call site (caller).
---
 clang/include/clang/AST/ASTContext.h |   7 ++
 clang/include/clang/AST/Mangle.h |   5 +
 clang/lib/AST/ASTContext.cpp |   6 ++
 clang/lib/AST/ItaniumMangle.cpp  | 137 ++-
 clang/lib/CodeGen/CodeGenModule.cpp  |  50 ++
 clang/lib/CodeGen/CodeGenModule.h|   8 ++
 6 files changed, 211 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index a4d36f2eacd5d1..4812e0bac2cfc3 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -1876,6 +1876,9 @@ class ASTContext : public RefCountedBase {
   /// (struct/union/class/enum) decl.
   QualType getTagDeclType(const TagDecl *Decl) const;
 
+  /// Return the type for "void *"
+  QualType getVoidPtrType() const { return VoidPtrTy; }
+
   /// Return the unique type for "size_t" (C99 7.17), defined in
   /// .
   ///
@@ -1903,6 +1906,10 @@ class ASTContext : public RefCountedBase {
   /// defined in  as defined by the target.
   QualType getWideCharType() const { return WideCharTy; }
 
+  /// Return the type of wide characters in C context, no matter whether it's C
+  /// or C++ being compiled.
+  QualType getWCharTypeInC() const;
+
   /// Return the type of "signed wchar_t".
   ///
   /// Used when in C++, as a GCC extension.
diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h
index d5f6c0f6cc67df..16cbd802177ba4 100644
--- a/clang/include/clang/AST/Mangle.h
+++ b/clang/include/clang/AST/Mangle.h
@@ -212,6 +212,11 @@ class ItaniumMangleContext : public MangleContext {
 
   virtual void mangleModuleInitializer(const Module *Module, raw_ostream &) = 
0;
 
+  virtual void mangleForRISCVZicfilpFuncSigLabel(const FunctionType &FT,
+ const bool 
IsCXXInstanceMethod,
+ const bool IsCXXVirtualMethod,
+ raw_ostream &) = 0;
+
   // This has to live here, otherwise the CXXNameMangler won't have access to
   // it.
   virtual DiscriminatorOverrideTy getDiscriminatorOverride() const = 0;
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 034fbbe0bc7829..ce8688a489cb43 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -6442,6 +6442,12 @@ CanQualType ASTContext::getUIntMaxType() const {
   return getFromTargetType(Target->getUIntMaxType());
 }
 
+/// Return the type of wide characters in C context, no matter whether it's C
+/// or C++ being compiled.
+QualType ASTContext::getWCharTypeInC() const {
+  return getFromTargetType(Target->getWCharType());
+}
+
 /// getSignedWCharType - Return the type of "signed wchar_t".
 /// Used when in C++, as a GCC extension.
 QualType ASTContext::getSignedWCharType() const {
diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index 777cdca1a0c0d7..9eff94b0844092 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -43,6 +43,14 @@ using namespace clang;
 
 namespace {
 
+static bool mayBeCovariant(const Type &Ty) {
+  if (auto *const PT = Ty.getAs())
+return PT->getPointeeType()->isStructureOrClassType();
+  if (auto *const RT = Ty.getAs())
+return RT->getPointeeType()->isStructureOrClassType();
+  return false;
+}
+
 static bool isLocalContainerContext(const DeclContext *DC) {
   return isa(DC) || isa(DC) || 
isa(DC);
 }
@@ -136,6 +144,11 @@ class ItaniumMangleContextImpl : public 
ItaniumMangleContext {
 
   void mangleModuleInitializer(const Module *Module, raw_ostream &) override;

[clang] [llvm] [clang][RISCV] Introduce CodeGenModule::calcRISCVZicfilpFuncSigLabel() (PR #111661)

2024-10-11 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/111661

>From 9041af3df59b6f6f2f6c2ff335cc697dfd41ac73 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 31 May 2024 17:03:04 +0800
Subject: [PATCH] [clang][RISCV] Emit RISCV function-signature-based CFI label
 in llvm::Function metadata

This patch emits the RISC-V Zicfilp func-sig CFI label in the metadata of
generated `llvm::Function`s.

It introduces CodeGenModule::calcRISCVZicfilpFuncSigLabel(), which calculates a
CFI label used in the RISC-V Zicfilp func-sig CFI scheme for a given function
type/declaration. The scheme, according to psABI, encodes the label based on
function signature, and the rules are modified from the Itanium C++ ABI mangling
rule to allow functions (callees) that are called indirectly to have the
expected label as indicated by the function pointer type seen at the call site
(caller).
---
 clang/include/clang/AST/ASTContext.h  |   7 +
 clang/include/clang/AST/Mangle.h  |   5 +
 clang/lib/AST/ASTContext.cpp  |   6 +
 clang/lib/AST/ItaniumMangle.cpp   | 137 +-
 clang/lib/CodeGen/CodeGenModule.cpp   |  84 +++
 clang/lib/CodeGen/CodeGenModule.h |  19 +++
 .../mangle-class-covariant-virtual-method.cpp |  33 +
 .../mangle-class-destructor.cpp   |  17 +++
 ...angle-class-incovariant-virtual-method.cpp |  18 +++
 .../zicfilp-func-sig/mangle-class-method.cpp  |  18 +++
 .../mangle-class-static-method.cpp|  21 +++
 .../mangle-func-empty-param-list.c|  15 ++
 .../mangle-func-empty-param-list.cpp  |  15 ++
 .../mangle-ignore-exception-spec.cpp  |  15 ++
 .../RISCV/zicfilp-func-sig/mangle-main.cpp|  39 +
 .../RISCV/zicfilp-func-sig/mangle-wchar-t.cpp |  12 ++
 llvm/include/llvm/Support/RISCVISAUtils.h |   6 +-
 llvm/lib/Support/RISCVISAUtils.cpp|  22 ++-
 18 files changed, 485 insertions(+), 4 deletions(-)
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-covariant-virtual-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-destructor.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-incovariant-virtual-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-static-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-func-empty-param-list.c
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-func-empty-param-list.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-ignore-exception-spec.cpp
 create mode 100644 clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-main.cpp
 create mode 100644 clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-wchar-t.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index a4d36f2eacd5d1..4812e0bac2cfc3 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -1876,6 +1876,9 @@ class ASTContext : public RefCountedBase {
   /// (struct/union/class/enum) decl.
   QualType getTagDeclType(const TagDecl *Decl) const;
 
+  /// Return the type for "void *"
+  QualType getVoidPtrType() const { return VoidPtrTy; }
+
   /// Return the unique type for "size_t" (C99 7.17), defined in
   /// .
   ///
@@ -1903,6 +1906,10 @@ class ASTContext : public RefCountedBase {
   /// defined in  as defined by the target.
   QualType getWideCharType() const { return WideCharTy; }
 
+  /// Return the type of wide characters in C context, no matter whether it's C
+  /// or C++ being compiled.
+  QualType getWCharTypeInC() const;
+
   /// Return the type of "signed wchar_t".
   ///
   /// Used when in C++, as a GCC extension.
diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h
index d5f6c0f6cc67df..16cbd802177ba4 100644
--- a/clang/include/clang/AST/Mangle.h
+++ b/clang/include/clang/AST/Mangle.h
@@ -212,6 +212,11 @@ class ItaniumMangleContext : public MangleContext {
 
   virtual void mangleModuleInitializer(const Module *Module, raw_ostream &) = 
0;
 
+  virtual void mangleForRISCVZicfilpFuncSigLabel(const FunctionType &FT,
+ const bool 
IsCXXInstanceMethod,
+ const bool IsCXXVirtualMethod,
+ raw_ostream &) = 0;
+
   // This has to live here, otherwise the CXXNameMangler won't have access to
   // it.
   virtual DiscriminatorOverrideTy getDiscriminatorOverride() const = 0;
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 034fbbe0bc7829..ce8688a489cb43 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -6442,6 +6442,12 @@ CanQualType ASTContext::getUIntMaxType() c

[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function (PR #111661)

2024-10-11 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2024-10-11 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2024-10-11 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/111661

>From 0cc12aa82111ead067485c0e846313563e516e6c Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 31 May 2024 17:03:04 +0800
Subject: [PATCH] [clang][RISCV] Emit RISCV function-signature-based CFI label
 in llvm::Function metadata

This patch emits the RISC-V Zicfilp func-sig CFI label in the metadata of
generated `llvm::Function`s.

It introduces CodeGenModule::calcRISCVZicfilpFuncSigLabel(), which calculates a
CFI label used in the RISC-V Zicfilp func-sig CFI scheme for a given function
type/declaration. The scheme, according to psABI, encodes the label based on
function signature, and the rules are modified from the Itanium C++ ABI mangling
rule to allow functions (callees) that are called indirectly to have the
expected label as indicated by the function pointer type seen at the call site
(caller).
---
 clang/include/clang/AST/ASTContext.h  |   7 +
 clang/include/clang/AST/Mangle.h  |   5 +
 clang/lib/AST/ASTContext.cpp  |   6 +
 clang/lib/AST/ItaniumMangle.cpp   | 138 +-
 clang/lib/CodeGen/CodeGenModule.cpp   |  84 +++
 clang/lib/CodeGen/CodeGenModule.h |  19 +++
 .../mangle-class-covariant-virtual-method.cpp |  33 +
 .../mangle-class-destructor.cpp   |  17 +++
 ...angle-class-incovariant-virtual-method.cpp |  18 +++
 .../zicfilp-func-sig/mangle-class-method.cpp  |  18 +++
 .../mangle-class-static-method.cpp|  21 +++
 .../mangle-func-empty-param-list.c|  15 ++
 .../mangle-func-empty-param-list.cpp  |  15 ++
 .../mangle-ignore-exception-spec.cpp  |  15 ++
 .../RISCV/zicfilp-func-sig/mangle-main.cpp|  39 +
 .../RISCV/zicfilp-func-sig/mangle-wchar-t.cpp |  12 ++
 llvm/include/llvm/Support/RISCVISAUtils.h |   6 +-
 llvm/lib/Support/RISCVISAUtils.cpp|  22 ++-
 18 files changed, 486 insertions(+), 4 deletions(-)
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-covariant-virtual-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-destructor.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-incovariant-virtual-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-static-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-func-empty-param-list.c
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-func-empty-param-list.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-ignore-exception-spec.cpp
 create mode 100644 clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-main.cpp
 create mode 100644 clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-wchar-t.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index a4d36f2eacd5d1..4812e0bac2cfc3 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -1876,6 +1876,9 @@ class ASTContext : public RefCountedBase {
   /// (struct/union/class/enum) decl.
   QualType getTagDeclType(const TagDecl *Decl) const;
 
+  /// Return the type for "void *"
+  QualType getVoidPtrType() const { return VoidPtrTy; }
+
   /// Return the unique type for "size_t" (C99 7.17), defined in
   /// .
   ///
@@ -1903,6 +1906,10 @@ class ASTContext : public RefCountedBase {
   /// defined in  as defined by the target.
   QualType getWideCharType() const { return WideCharTy; }
 
+  /// Return the type of wide characters in C context, no matter whether it's C
+  /// or C++ being compiled.
+  QualType getWCharTypeInC() const;
+
   /// Return the type of "signed wchar_t".
   ///
   /// Used when in C++, as a GCC extension.
diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h
index d5f6c0f6cc67df..16cbd802177ba4 100644
--- a/clang/include/clang/AST/Mangle.h
+++ b/clang/include/clang/AST/Mangle.h
@@ -212,6 +212,11 @@ class ItaniumMangleContext : public MangleContext {
 
   virtual void mangleModuleInitializer(const Module *Module, raw_ostream &) = 
0;
 
+  virtual void mangleForRISCVZicfilpFuncSigLabel(const FunctionType &FT,
+ const bool 
IsCXXInstanceMethod,
+ const bool IsCXXVirtualMethod,
+ raw_ostream &) = 0;
+
   // This has to live here, otherwise the CXXNameMangler won't have access to
   // it.
   virtual DiscriminatorOverrideTy getDiscriminatorOverride() const = 0;
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 034fbbe0bc7829..ce8688a489cb43 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -6442,6 +6442,12 @@ CanQualType ASTContext::getUIntMaxType() c

[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2024-10-11 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Introduce CodeGenModule::calcRISCVZicfilpFuncSigLabel() (PR #111661)

2024-10-11 Thread Ming-Yi Lai via cfe-commits


@@ -2829,6 +2829,56 @@ void 
CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
   F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
 }
 
+uint32_t
+CodeGenModule::calcRISCVZicfilpFuncSigLabel(const FunctionType &FT,
+const bool IsCXXInstanceMethod,
+const bool IsCXXVirtualMethod) {

mylai-mtk wrote:

I moved just the hash part of this function to the llvm folder.
I suppose you're suggesting that the hash part needs to be moved to llvm to 
support implementing the `%lpad_label` asm operand modifier, which takes a 
mangled string and hash it into a 20-bit integer, so it's only the hashing part 
that needs to be re-located, not the whole mangling part.

Fixed.

https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Introduce CodeGenModule::calcRISCVZicfilpFuncSigLabel() (PR #111661)

2024-10-11 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Update:

* Rebase to `main`
* Address comments: Expand the scope of this PR to allow testing
* Address other comments

https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-21 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-21 Thread Ming-Yi Lai via cfe-commits


@@ -899,6 +899,10 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, 
QualType RetTy,
   if (CodeGenOpts.PointerAuth.IndirectGotos)
 Fn->addFnAttr("ptrauth-indirect-gotos");
 
+  // Add return control flow integrity attributes.
+  if (CodeGenOpts.CFProtectionReturn)
+Fn->addFnAttr("hw-shadow-stack");
+

mylai-mtk wrote:

This feel a bit weird: I thought using `-fcf-protection=return` to select 
hardware-based shadow stack is a RISC-V specific thing. Perhaps for now this 
should look more tailored for RISC-V to avoid misleading?

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-21 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-23 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

LGTM, but please wait for others. I'm too junior to this community that I'm not 
sure if my words are enough 😂

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-25 Thread Ming-Yi Lai via cfe-commits


@@ -607,6 +607,9 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
 auto *Fn = cast(GV);
 
 Fn->addFnAttr("interrupt", Kind);
+
+if (CGM.getCodeGenOpts().CFProtectionReturn)

mylai-mtk wrote:

The early exit above may render this line unreachable for normal functions.

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-24 Thread Ming-Yi Lai via cfe-commits


@@ -607,6 +607,9 @@ class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
 auto *Fn = cast(GV);
 
 Fn->addFnAttr("interrupt", Kind);
+
+if (CGM.getCodeGenOpts().CFProtectionReturn)

mylai-mtk wrote:

After tracing the code, I believe the above lines are for attaching interrupt 
attributes to LLVM functions based on the C `__attribute__((interrupt))` parsed 
by Clang. They're placed here since the interrupt attribute implementation is 
target-specific (ref. 
https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Function-Attributes.html#index-interrupt-handler-functions-2886).
 

The above lines
```c++
const auto *Attr = FD->getAttr();
if (!Attr)
  return;
```
mean that for C function declarations without the interrupt attribute, which is 
the common case for normal functions, the `setTargetAttributes` function would 
exit early, and this renders your attachment of the `hw-shadow-stack` attribute 
undone.

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-24 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s -fcf-protection=return | FileCheck 
-check-prefix=NOTIGNORELISTED %s
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s | FileCheck -check-prefix=IGNORELISTED %s
+
+int foo(int *a) { return *a; }
+
+// CHECK: define i32 @foo(ptr %a)

mylai-mtk wrote:

It looks like this line is not checked if you specify the `--check-prefix` 
option to FileCheck:

> --check-prefix prefix
> FileCheck searches the contents of match-filename for patterns to match. By 
> default, these patterns are prefixed with “CHECK:”. If you’d like to use a 
> different prefix (e.g. because the same input file is checking multiple 
> different tool or options), the 
> [--check-prefix](https://llvm.org/docs/CommandGuide/FileCheck.html#cmdoption-FileCheck-check-prefix)
>  argument allows you to specify (without the trailing “:”) one or more 
> prefixes to match. Multiple prefixes are useful for tests which might change 
> for different run options, but most lines remain the same.

(ref. https://llvm.org/docs/CommandGuide/FileCheck.html)

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-24 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s -fcf-protection=return | FileCheck 
-check-prefix=NOTIGNORELISTED %s
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s | FileCheck -check-prefix=IGNORELISTED %s
+
+int foo(int *a) { return *a; }
+
+// CHECK: define i32 @foo(ptr %a)
+
+// IGNORELISTED-NOT: attributes {{.*}}"hw-shadow-stack"{{.*}}
+// NOTIGNORELISTED: attributes {{.*}}"hw-shadow-stack"{{.*}}

mylai-mtk wrote:

What does the name 'ignore listed' mean?

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-28 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

LGTM. Thanks.

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-27 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s -fcf-protection=return | FileCheck %s
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s

mylai-mtk wrote:

The triple components are in the wrong order. They should be 
`riscv{64,32}-unknown-linux-gnu`.

> Configuration names are strings in the canonical form: 
> ARCHITECTURE-VENDOR-OPERATING_SYSTEM or 
> ARCHITECTURE-VENDOR-OPERATING_SYSTEM-ENVIRONMENT

(ref. https://llvm.org/doxygen/classllvm_1_1Triple.html#details)

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-27 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s -fcf-protection=return | FileCheck %s
+// RUN: %clang_cc1 -triple riscv64-linux-unknown -target-feature +zimop 
-emit-llvm -o - %s | FileCheck -check-prefix=NOSHADOWSTACK %s

mylai-mtk wrote:

Add riscv32 tests

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-24 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [KCFI][NFC] Rename the !kcfi_type Function metadata to !cfi_type (PR #109080)

2024-09-22 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Excuse me, @MaskRay  . I don't understand your stance: 

> I understand the intention to generalize naming, but I feel that there is 
> significance chance that the over-generalization may not work with the 
> alternative CFI schemes, and this change could turn out to be needed when the 
> other schemes go with different IR constructs.

I get that this renaming is an over-generalization, and more changes in the 
future may be required should different CFI schemes arise, so I guess you're 
suggesting maybe we should not do the renaming.

> I feel that we should make such renaming at this time.

But then you propose that we make the renaming now. (??)

I'm quite confused by the conflict of these two paragraphs. Can you talk more 
about your view?

https://github.com/llvm/llvm-project/pull/109080
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-22 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk created 
https://github.com/llvm/llvm-project/pull/109600

This patch adds preprocessor macros when Zicfilp CFI is enabled. The macros are 
proposed in riscv-non-isa/riscv-c-api-doc#76 , and the CLI flags are from 
riscv-non-isa/riscv-toolchain-conventions#54. 

>From 40ecff66e69ad520dd858d34be8b8f9a6da594fb Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH 1/3] [clang][RISCV] Accept -fcf-protection=branch when Zicfilp
 extension is on

-fcf-protection=branch turns on indirect branching protection on RISC-V targets
with Zicfilp extension.
---
 clang/lib/Basic/Targets/RISCV.h | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h
index b808ccc8e9cfe9..13d4b4c04ce8e0 100644
--- a/clang/lib/Basic/Targets/RISCV.h
+++ b/clang/lib/Basic/Targets/RISCV.h
@@ -131,6 +131,13 @@ class RISCVTargetInfo : public TargetInfo {
   bool supportsCpuInit() const override { return getTriple().isOSLinux(); }
   bool validateCpuSupports(StringRef Feature) const override;
   bool isValidFeatureName(StringRef Name) const override;
+
+  bool
+  checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const override {
+if (ISAInfo->hasExtension("zicfilp"))
+  return true;
+return TargetInfo::checkCFProtectionBranchSupported(Diags);
+  }
 };
 class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
 public:

>From 71de8dcc7d5a0038e705b552ba8092d7ae6868b7 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 6 Sep 2024 19:00:10 +0800
Subject: [PATCH 2/3] [Clang] Introduce
 -fcf-branch-label-scheme=unlabeled|func-sig

This flag controls the scheme of the labels of landing pad insns. Landing pad
insns are special insns that mark the targets of indirect jumps. In RISC-V
Zicfilp extension, its landing pad insn further encodes a field of `label',
which can be used to limit the sources of indirect jumps to those that set up a
matching label according to a `scheme' selected at compile time. This flag
controls the selection of that `scheme'. It's named `-fcf-branch-label-scheme`
since the generation of landing pad insns is turned on by
`-fcf-protection=branch`.
---
 .../include/clang/Basic/CFProtectionOptions.h | 22 +++
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 .../clang/Basic/DiagnosticDriverKinds.td  |  3 ++
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  4 ++
 clang/include/clang/Driver/Options.td |  4 ++
 clang/lib/Basic/TargetInfo.cpp|  5 +++
 clang/lib/Basic/Targets/RISCV.h   |  4 ++
 clang/lib/Basic/Targets/X86.h |  4 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  4 ++
 clang/lib/Frontend/CompilerInvocation.cpp | 39 +++
 13 files changed, 96 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h

diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..d97f8489c4631f
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,22 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind { Default, Unlabeled, FuncSig };
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..feda4260e320c2 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -fcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-c

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-23 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 3cae49e7cbff687c1303f8204f70c9c1105a9560 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH 1/3] [clang][RISCV] Accept -fcf-protection=branch when Zicfilp
 extension is on

-fcf-protection=branch turns on indirect branching protection on RISC-V targets
with Zicfilp extension.
---
 clang/lib/Basic/Targets/RISCV.h|  7 +++
 clang/test/CodeGen/RISCV/riscv-cf-protection.c | 18 ++
 2 files changed, 25 insertions(+)
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h
index b808ccc8e9cfe9..13d4b4c04ce8e0 100644
--- a/clang/lib/Basic/Targets/RISCV.h
+++ b/clang/lib/Basic/Targets/RISCV.h
@@ -131,6 +131,13 @@ class RISCVTargetInfo : public TargetInfo {
   bool supportsCpuInit() const override { return getTriple().isOSLinux(); }
   bool validateCpuSupports(StringRef Feature) const override;
   bool isValidFeatureName(StringRef Name) const override;
+
+  bool
+  checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const override {
+if (ISAInfo->hasExtension("zicfilp"))
+  return true;
+return TargetInfo::checkCFProtectionBranchSupported(Diags);
+  }
 };
 class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
 public:
diff --git a/clang/test/CodeGen/RISCV/riscv-cf-protection.c 
b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
new file mode 100644
index 00..56a3c48cde53a1
--- /dev/null
+++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
@@ -0,0 +1,18 @@
+// RUN: not %clang --target=riscv32 -fcf-protection=branch -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s
+
+// RUN: not %clang --target=riscv32 -fcf-protection=branch -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s
+
+// RUN: not %clang --target=riscv64 -fcf-protection=branch -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s
+
+// RUN: not %clang --target=riscv64 -fcf-protection=branch -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-BRANCH-PROT-INVALID %s
+
+// CHECK-BRANCH-PROT-INVALID: error: option 'cf-protection=branch' cannot be
+// CHECK-BRANCH-PROT-INVALID-SAME: specified on this target

>From 3ac3d29294232f6688d8e570aac8f57a5262df05 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 6 Sep 2024 19:00:10 +0800
Subject: [PATCH 2/3] [Clang] Introduce
 -fcf-branch-label-scheme=unlabeled|func-sig

This flag controls the scheme of the labels of landing pad insns. Landing pad
insns are special insns that mark the targets of indirect jumps. In RISC-V
Zicfilp extension, its landing pad insn further encodes a field of `label',
which can be used to limit the sources of indirect jumps to those that set up a
matching label according to a `scheme' selected at compile time. This flag
controls the selection of that `scheme'. It's named `-fcf-branch-label-scheme`
since the generation of landing pad insns is turned on by
`-fcf-protection=branch`.
---
 .../include/clang/Basic/CFProtectionOptions.h | 22 
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  4 ++
 clang/include/clang/Driver/Options.td |  4 ++
 clang/lib/Basic/TargetInfo.cpp|  5 ++
 clang/lib/Basic/Targets/RISCV.h   |  4 ++
 clang/lib/Basic/Targets/X86.h |  4 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  4 ++
 clang/lib/Frontend/CompilerInvocation.cpp | 37 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 52 ---
 13 files changed, 137 insertions(+), 6 deletions(-)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h

diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..d97f8489c4631f
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,22 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_B

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-23 Thread Ming-Yi Lai via cfe-commits


@@ -2022,6 +2035,22 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << 
Name;
   }
 
+  if (const Arg *const A = Args.getLastArg(OPT_fcf_branch_label_scheme_EQ)) {

mylai-mtk wrote:

Fixed

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-23 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Update: Address comments

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-23 Thread Ming-Yi Lai via cfe-commits


@@ -3952,6 +3981,16 @@ bool CompilerInvocation::ParseLangArgs(LangOptions 
&Opts, ArgList &Args,
 }
   }
 
+  if (const Arg *const A = Args.getLastArg(OPT_fcf_branch_label_scheme_EQ)) {

mylai-mtk wrote:

Fixed

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-23 Thread Ming-Yi Lai via cfe-commits


@@ -2841,6 +2841,10 @@ def fcf_protection : Flag<["-"], "fcf-protection">, 
Group,
   Visibility<[ClangOption, CLOption, CC1Option]>,
   Alias, AliasArgs<["full"]>,
   HelpText<"Enable cf-protection in 'full' mode">;
+def fcf_branch_label_scheme_EQ : Joined<["-"], "fcf-branch-label-scheme=">,

mylai-mtk wrote:

Fixed

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

> Could you update this PR? 
> https://github.com/llvm/llvm-project/commit/4579272e057e6ec77a2a660384080e1f57a17cf0
>  is generally LGTM, but I assume this should process within this PR :)

Sure, I'm just a few minutes behind you XD

https://github.com/llvm/llvm-project/pull/109784
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 4579272e057e6ec77a2a660384080e1f57a17cf0 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH 1/2] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../clang/Basic/CFProtectionOptions.def   | 15 
 .../include/clang/Basic/CFProtectionOptions.h | 38 
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  8 ++
 clang/include/clang/Driver/Options.td |  4 +
 clang/lib/Basic/TargetInfo.cpp| 15 
 clang/lib/Basic/Targets/RISCV.h   | 22 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 10 +++
 clang/lib/Driver/ToolChains/Clang.cpp |  4 +
 clang/lib/Frontend/CompilerInvocation.cpp | 40 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 86 +++
 14 files changed, 249 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.def
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.def 
b/clang/include/clang/Basic/CFProtectionOptions.def
new file mode 100644
index 00..b9df2de7f7eba0
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.def
@@ -0,0 +1,15 @@
+//===-- CFProtectionOptions.def - cf-protection options -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+
+#ifdef CF_BRANCH_LABEL_SCHEME
+CF_BRANCH_LABEL_SCHEME(Unlabeled, unlabeled)
+CF_BRANCH_LABEL_SCHEME(FuncSig, func-sig)
+
+#undef CF_BRANCH_LABEL_SCHEME
+#endif // #ifdef CF_BRANCH_LABEL_SCHEME
diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..13f46d4c13e7e7
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,38 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+#include "llvm/Support/ErrorHandling.h"
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind {
+  Default,
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal) Kind,
+#include "clang/Basic/CFProtectionOptions.def"
+};
+
+static inline const char *
+getCFBranchLabelSchemeFlagVal(const CFBranchLabelSchemeKind Scheme) {
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal)  
\
+  if (Scheme == CFBranchLabelSchemeKind::Kind) 
\
+return #FlagVal;
+#include "clang/Basic/CFProtectionOptions.def"
+
+  llvm::report_fatal_error("invalid scheme");
+}
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Update: Always support `-fcf-protection=branch` for RISC-V targets

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 1a0aed20421b58114332975ad7d7fff90a21014a Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH 1/2] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../clang/Basic/CFProtectionOptions.def   | 15 +++
 .../include/clang/Basic/CFProtectionOptions.h | 38 
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  8 ++
 clang/include/clang/Driver/Options.td |  4 +
 clang/lib/Basic/TargetInfo.cpp| 15 +++
 clang/lib/Basic/Targets/RISCV.h   | 22 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 10 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  4 +
 clang/lib/Frontend/CompilerInvocation.cpp | 40 
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 94 +++
 14 files changed, 257 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.def
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.def 
b/clang/include/clang/Basic/CFProtectionOptions.def
new file mode 100644
index 00..b9df2de7f7eba0
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.def
@@ -0,0 +1,15 @@
+//===-- CFProtectionOptions.def - cf-protection options -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+
+#ifdef CF_BRANCH_LABEL_SCHEME
+CF_BRANCH_LABEL_SCHEME(Unlabeled, unlabeled)
+CF_BRANCH_LABEL_SCHEME(FuncSig, func-sig)
+
+#undef CF_BRANCH_LABEL_SCHEME
+#endif // #ifdef CF_BRANCH_LABEL_SCHEME
diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..13f46d4c13e7e7
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,38 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+#include "llvm/Support/ErrorHandling.h"
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind {
+  Default,
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal) Kind,
+#include "clang/Basic/CFProtectionOptions.def"
+};
+
+static inline const char *
+getCFBranchLabelSchemeFlagVal(const CFBranchLabelSchemeKind Scheme) {
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal)  
\
+  if (Scheme == CFBranchLabelSchemeKind::Kind) 
\
+return #FlagVal;
+#include "clang/Basic/CFProtectionOptions.def"
+
+  llvm::report_fatal_error("invalid scheme");
+}
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix

[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-26 Thread Ming-Yi Lai via cfe-commits


@@ -198,6 +198,21 @@ 
TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const {
   return false;
 }
 
+CFBranchLabelSchemeKind TargetInfo::getDefaultCFBranchLabelScheme() const {
+  // if this hook is called, the target should override it to return a
+  // non-default scheme
+  llvm::report_fatal_error("not implemented");
+}
+
+bool TargetInfo::checkCFBranchLabelSchemeSupported(
+const CFBranchLabelSchemeKind Scheme, DiagnosticsEngine &Diags) const {
+  Diags.Report(diag::err_opt_not_valid_on_target)

mylai-mtk wrote:

Guard against `Default`

https://github.com/llvm/llvm-project/pull/109784
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-26 Thread Ming-Yi Lai via cfe-commits


@@ -134,6 +134,25 @@ class RISCVTargetInfo : public TargetInfo {
 
   bool validateGlobalRegisterVariable(StringRef RegName, unsigned RegSize,
   bool &HasSizeMismatch) const override;
+
+  bool checkCFProtectionBranchSupported() const override {
+return ISAInfo->hasExtension("zicfilp");

mylai-mtk wrote:

Fixed: Always return supported for `-fcf-protection=branch`.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-26 Thread Ming-Yi Lai via cfe-commits


@@ -224,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";

mylai-mtk wrote:

No longer relevant: Code removed.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-26 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,90 @@
+// Default cf-branch-label-scheme is func-sig
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// 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 -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 -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %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: 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: 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: %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 -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: %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: 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: 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: %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=func-sig -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s
+
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad_unlabeled 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad_func_sig 1{{$}}
+// 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:
+// CHECK-UNLABELED-SCHEME-UNUSED-SAME: '-mcf-branch-label-scheme=unlabeled'
+// CHECK-FUNC-SIG-SCHEME-UNUSED: warning: argument unused during compilation:
+// CHECK-FUNC-SIG-SCHEME-UNUSED-SAME: '-mcf-branch-label-scheme=func-sig'

mylai-mtk wrote:

Fixed: Since `-fcf-protection=branch` is always supported for RISC-V targets, 
landing pad macros are no longer absent in the cases of 'no zicfilp + 
`-fcf-protection=branch`', and this renders all landing pad

[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109784

>From 7aaeaef35f63665bc943efca7ef1b603137522eb Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../clang/Basic/CFProtectionOptions.def   | 15 +++
 .../include/clang/Basic/CFProtectionOptions.h | 38 
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  8 ++
 clang/include/clang/Driver/Options.td |  4 +
 clang/lib/Basic/TargetInfo.cpp| 15 +++
 clang/lib/Basic/Targets/RISCV.h   | 22 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 10 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  4 +
 clang/lib/Frontend/CompilerInvocation.cpp | 40 
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 94 +++
 14 files changed, 257 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.def
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.def 
b/clang/include/clang/Basic/CFProtectionOptions.def
new file mode 100644
index 00..b9df2de7f7eba0
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.def
@@ -0,0 +1,15 @@
+//===-- CFProtectionOptions.def - cf-protection options -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+
+#ifdef CF_BRANCH_LABEL_SCHEME
+CF_BRANCH_LABEL_SCHEME(Unlabeled, unlabeled)
+CF_BRANCH_LABEL_SCHEME(FuncSig, func-sig)
+
+#undef CF_BRANCH_LABEL_SCHEME
+#endif // #ifdef CF_BRANCH_LABEL_SCHEME
diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..13f46d4c13e7e7
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,38 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+#include "llvm/Support/ErrorHandling.h"
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind {
+  Default,
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal) Kind,
+#include "clang/Basic/CFProtectionOptions.def"
+};
+
+static inline const char *
+getCFBranchLabelSchemeFlagVal(const CFBranchLabelSchemeKind Scheme) {
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal)  
\
+  if (Scheme == CFBranchLabelSchemeKind::Kind) 
\
+return #FlagVal;
+#include "clang/Basic/CFProtectionOptions.def"
+
+  llvm::report_fatal_error("invalid scheme");
+}
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix
  //

[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109784

>From baa87159eb5b6966841bac0a8a089f4ce63d66e2 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../clang/Basic/CFProtectionOptions.def   | 15 +++
 .../include/clang/Basic/CFProtectionOptions.h | 38 
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  8 ++
 clang/include/clang/Driver/Options.td |  4 +
 clang/lib/Basic/TargetInfo.cpp| 16 
 clang/lib/Basic/Targets/RISCV.h   | 22 +
 clang/lib/CodeGen/CodeGenModule.cpp   | 10 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  4 +
 clang/lib/Frontend/CompilerInvocation.cpp | 40 
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 94 +++
 14 files changed, 258 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.def
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.def 
b/clang/include/clang/Basic/CFProtectionOptions.def
new file mode 100644
index 00..b9df2de7f7eba0
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.def
@@ -0,0 +1,15 @@
+//===-- CFProtectionOptions.def - cf-protection options -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+
+#ifdef CF_BRANCH_LABEL_SCHEME
+CF_BRANCH_LABEL_SCHEME(Unlabeled, unlabeled)
+CF_BRANCH_LABEL_SCHEME(FuncSig, func-sig)
+
+#undef CF_BRANCH_LABEL_SCHEME
+#endif // #ifdef CF_BRANCH_LABEL_SCHEME
diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..13f46d4c13e7e7
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,38 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+#include "llvm/Support/ErrorHandling.h"
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind {
+  Default,
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal) Kind,
+#include "clang/Basic/CFProtectionOptions.def"
+};
+
+static inline const char *
+getCFBranchLabelSchemeFlagVal(const CFBranchLabelSchemeKind Scheme) {
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal)  
\
+  if (Scheme == CFBranchLabelSchemeKind::Kind) 
\
+return #FlagVal;
+#include "clang/Basic/CFProtectionOptions.def"
+
+  llvm::report_fatal_error("invalid scheme");
+}
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix
  /

[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-26 Thread Ming-Yi Lai via cfe-commits


@@ -198,6 +198,21 @@ 
TargetInfo::checkCFProtectionBranchSupported(DiagnosticsEngine &Diags) const {
   return false;
 }
 
+CFBranchLabelSchemeKind TargetInfo::getDefaultCFBranchLabelScheme() const {
+  // if this hook is called, the target should override it to return a
+  // non-default scheme
+  llvm::report_fatal_error("not implemented");
+}
+
+bool TargetInfo::checkCFBranchLabelSchemeSupported(
+const CFBranchLabelSchemeKind Scheme, DiagnosticsEngine &Diags) const {
+  Diags.Report(diag::err_opt_not_valid_on_target)

mylai-mtk wrote:

Fixed

https://github.com/llvm/llvm-project/pull/109784
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-25 Thread Ming-Yi Lai via cfe-commits


@@ -224,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";

mylai-mtk wrote:

Yes, if it's `clang -c` or `clang -S`, I believe it would be diagnosed in 
`CodeGenModule`, but if it's `clang -E -dM`, it wouldn't be diagnosed, since 
code gen is not required.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-25 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

> It is useful to protect a binary with an -mcpu that doesn't support Zicfilp. 

Generally I would agree that it's a good idea to protect a binary with an 
`-mcpu` that doesn't support Zicfilp, but to lift the check of Zicfilp would be 
misleading to naive compiler/RISC-V users who don't know that only with a 
system fully aware of Zicfilp, the protection of `-fcf-protection=branch` can 
be expected. I think from this perspective, compilers should at least warn the 
user if he asks for a feature that is known to be broken with his environment.

@kito-cheng I need your input on this, since this would require a few updates 
on various PRs to the RISC-V specs.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Thanks :D

https://github.com/llvm/llvm-project/pull/109784
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk closed 
https://github.com/llvm/llvm-project/pull/109784
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-26 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 35532cf0a9cc855fe5f0a4eefd4c343687a595dc 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 | 19 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 79 +++
 2 files changed, 98 insertions(+)

diff --git a/clang/lib/Basic/Targets/RISCV.cpp 
b/clang/lib/Basic/Targets/RISCV.cpp
index b6ea4440507ea1..b9199e3bc77d34 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -224,6 +224,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+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");
+}
+  }
 }
 
 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 3a9855a3d2f011..db7b65061658c0 100644
--- a/clang/test/CodeGen/RISCV/riscv-cf-protection.c
+++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
@@ -1,71 +1,143 @@
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %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=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,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=NO-MACRO,FUNC-SIG-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,FUNC-SIG-SCHEME-UNUSED %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM -emit-llvm %s -o - | \
+// RUN: FileCheck --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %s
 
+// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -E -dM %s \
+// RUN: -o - 2>&1 | FileCheck \
+// RUN: --check-prefixes=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -mcf-branc

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits


@@ -2022,6 +2035,20 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << 
Name;
   }
 
+  if (const Arg *A = Args.getLastArg(OPT_mcf_branch_label_scheme_EQ)) {
+if (Opts.CFProtectionBranch) {
+  const StringRef Scheme = A->getValue();
+  if (Scheme == "unlabeled")
+Opts.setCFBranchLabelScheme(CFBranchLabelSchemeKind::Unlabeled);
+  else if (Scheme == "func-sig")
+Opts.setCFBranchLabelScheme(CFBranchLabelSchemeKind::FuncSig);
+  else
+Diags.Report(diag::err_drv_invalid_value)
+<< A->getAsString(Args) << Scheme;
+} else
+  Diags.Report(diag::warn_drv_unused_argument) << A->getAsString(Args);

mylai-mtk wrote:

Fixed in #109784.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-24 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk created 
https://github.com/llvm/llvm-project/pull/109784

This patch introduces the CLI flag specified in 
riscv-non-isa/riscv-toolchain-conventions#54 for RISC-V Zicfilp-backed 
forward-edge control flow integrity conditioning. The first user of these CLI 
flags is expected to be #109600 , which adds preprocessor macros when the CFI 
conditioning is enabled.

(This PR was split from #109600 to separate the commit of flag introduction 
from macro definition.)

>From acd2093abeca6f52c0f01527333b38cf4321e74e Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../include/clang/Basic/CFProtectionOptions.h | 22 +++
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  4 ++
 clang/include/clang/Driver/Options.td |  4 ++
 clang/lib/Basic/TargetInfo.cpp|  5 ++
 clang/lib/Basic/Targets/RISCV.h   | 11 
 clang/lib/Driver/ToolChains/Clang.cpp |  4 ++
 clang/lib/Frontend/CompilerInvocation.cpp | 36 
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 58 +++
 12 files changed, 151 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..d97f8489c4631f
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,22 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind { Default, Unlabeled, FuncSig };
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix
  ///< is set.
diff --git a/clang/include/clang/Basic/CodeGenOptions.h 
b/clang/include/clang/Basic/CodeGenOptions.h
index f2a707a8ba8d76..a6953c17a447ef 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 #define LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 
+#include "clang/Basic/CFProtectionOptions.h"
 #include "clang/Basic/PointerAuthOptions.h"
 #include "clang/Basic/Sanitizers.h"
 #include "clang/Basic/XRayInstr.h"
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index fd3346d29f26a3..68db400c22e6c1 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -364,6 +364,8 @@ 
BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
 LANGOPT(ObjCDisableDirectMethodsForTesting, 1, 0,
 "Disable recognition of objc_direct methods")
 LANGOPT(CFProtectionBranch , 1, 0, "Control-Flow Branch Protection enabled")
+ENUM_LANGOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2, 
CFBranchLabelSchemeKind

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits


@@ -2022,6 +2035,20 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions 
&Opts, ArgList &Args,
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << 
Name;
   }
 
+  if (const Arg *A = Args.getLastArg(OPT_mcf_branch_label_scheme_EQ)) {

mylai-mtk wrote:

I suppose the check can be performed, given that only the RISC-V target uses 
it, and checking this prevents the flag from being claimed erroneously to some 
extent. However, this doesn't solve the claim issue completely, since at this 
point I don't have a `TargetInfo` that allows me to check if `zicfilp` is 
enabled; I only have the `TargetOptions::FeatureAsWritten` to be ready to check 
for the existence of `+experimental-zicfilp` from command line, which is less 
than ideal, I'd say.

Fixed (Checked) in #109784.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From acd2093abeca6f52c0f01527333b38cf4321e74e Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH 1/2] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../include/clang/Basic/CFProtectionOptions.h | 22 +++
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  4 ++
 clang/include/clang/Driver/Options.td |  4 ++
 clang/lib/Basic/TargetInfo.cpp|  5 ++
 clang/lib/Basic/Targets/RISCV.h   | 11 
 clang/lib/Driver/ToolChains/Clang.cpp |  4 ++
 clang/lib/Frontend/CompilerInvocation.cpp | 36 
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 58 +++
 12 files changed, 151 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..d97f8489c4631f
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,22 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind { Default, Unlabeled, FuncSig };
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix
  ///< is set.
diff --git a/clang/include/clang/Basic/CodeGenOptions.h 
b/clang/include/clang/Basic/CodeGenOptions.h
index f2a707a8ba8d76..a6953c17a447ef 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 #define LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 
+#include "clang/Basic/CFProtectionOptions.h"
 #include "clang/Basic/PointerAuthOptions.h"
 #include "clang/Basic/Sanitizers.h"
 #include "clang/Basic/XRayInstr.h"
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index fd3346d29f26a3..68db400c22e6c1 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -364,6 +364,8 @@ 
BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
 LANGOPT(ObjCDisableDirectMethodsForTesting, 1, 0,
 "Disable recognition of objc_direct methods")
 LANGOPT(CFProtectionBranch , 1, 0, "Control-Flow Branch Protection enabled")
+ENUM_LANGOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2, 
CFBranchLabelSchemeKind::Default,
+ "Control-Flow Branch Protection Label Scheme")
 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")
diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,90 @@
+// Default cf-branch-label-scheme is func-sig
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// 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 -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 -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %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: 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: 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: %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 -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: %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: 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: 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: %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=func-sig -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s
+
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad_unlabeled 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad_func_sig 1{{$}}

mylai-mtk wrote:

Sorry, they're leftovers from restructuring. Removed.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits


@@ -224,6 +224,26 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (ISAInfo->hasExtension("zicfilp") && Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+if (Scheme == CFBranchLabelSchemeKind::Default)
+  Scheme = getDefaultCFBranchLabelScheme();
+
+switch (Scheme) {
+case CFBranchLabelSchemeKind::Unlabeled:
+  Builder.defineMacro("__riscv_landing_pad", "1");

mylai-mtk wrote:

Fixed.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,90 @@
+// Default cf-branch-label-scheme is func-sig
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// 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 -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 -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %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: 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: 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: %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 -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: %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: 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: 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: %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=func-sig -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s
+
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad_unlabeled 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad_func_sig 1{{$}}
+// 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:
+// CHECK-UNLABELED-SCHEME-UNUSED-SAME: '-mcf-branch-label-scheme=unlabeled'
+// CHECK-FUNC-SIG-SCHEME-UNUSED: warning: argument unused during compilation:
+// CHECK-FUNC-SIG-SCHEME-UNUSED-SAME: '-mcf-branch-label-scheme=func-sig'

mylai-mtk wrote:

Fixed partially.

I added these negative checks for most cases, but I can't have them checked in 
the cases of 'no zicfilp + `-fcf-protection=branch`', since the check of the 
validness of `-fcf-protection=bra

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Update: Address comments
Update: Split the CLI flag introduction to #109784 as per the suggestion from 
kito

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-24 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 0068ec08f28e2197a4982f876052f577988acd61 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH 1/2] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../include/clang/Basic/CFProtectionOptions.h | 22 +++
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  4 ++
 clang/include/clang/Driver/Options.td |  4 ++
 clang/lib/Basic/TargetInfo.cpp|  5 ++
 clang/lib/Basic/Targets/RISCV.h   | 11 
 clang/lib/Driver/ToolChains/Clang.cpp |  4 ++
 clang/lib/Frontend/CompilerInvocation.cpp | 36 
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 58 +++
 12 files changed, 151 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..d97f8489c4631f
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,22 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind { Default, Unlabeled, FuncSig };
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix
  ///< is set.
diff --git a/clang/include/clang/Basic/CodeGenOptions.h 
b/clang/include/clang/Basic/CodeGenOptions.h
index f2a707a8ba8d76..a6953c17a447ef 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 #define LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 
+#include "clang/Basic/CFProtectionOptions.h"
 #include "clang/Basic/PointerAuthOptions.h"
 #include "clang/Basic/Sanitizers.h"
 #include "clang/Basic/XRayInstr.h"
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index fd3346d29f26a3..68db400c22e6c1 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -364,6 +364,8 @@ 
BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
 LANGOPT(ObjCDisableDirectMethodsForTesting, 1, 0,
 "Disable recognition of objc_direct methods")
 LANGOPT(CFProtectionBranch , 1, 0, "Control-Flow Branch Protection enabled")
+ENUM_LANGOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2, 
CFBranchLabelSchemeKind::Default,
+ "Control-Flow Branch Protection Label Scheme")
 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")
diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang

[clang] [clang][RISCV] Introduce command line options for RISC-V Zicfilp CFI (PR #109784)

2024-09-24 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109784

>From 0068ec08f28e2197a4982f876052f577988acd61 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../include/clang/Basic/CFProtectionOptions.h | 22 +++
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  4 ++
 clang/include/clang/Driver/Options.td |  4 ++
 clang/lib/Basic/TargetInfo.cpp|  5 ++
 clang/lib/Basic/Targets/RISCV.h   | 11 
 clang/lib/Driver/ToolChains/Clang.cpp |  4 ++
 clang/lib/Frontend/CompilerInvocation.cpp | 36 
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 58 +++
 12 files changed, 151 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..d97f8489c4631f
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,22 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind { Default, Unlabeled, FuncSig };
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix
  ///< is set.
diff --git a/clang/include/clang/Basic/CodeGenOptions.h 
b/clang/include/clang/Basic/CodeGenOptions.h
index f2a707a8ba8d76..a6953c17a447ef 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 #define LLVM_CLANG_BASIC_CODEGENOPTIONS_H
 
+#include "clang/Basic/CFProtectionOptions.h"
 #include "clang/Basic/PointerAuthOptions.h"
 #include "clang/Basic/Sanitizers.h"
 #include "clang/Basic/XRayInstr.h"
diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index fd3346d29f26a3..68db400c22e6c1 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -364,6 +364,8 @@ 
BENIGN_LANGOPT(CompatibilityQualifiedIdBlockParamTypeChecking, 1, 0,
 LANGOPT(ObjCDisableDirectMethodsForTesting, 1, 0,
 "Disable recognition of objc_direct methods")
 LANGOPT(CFProtectionBranch , 1, 0, "Control-Flow Branch Protection enabled")
+ENUM_LANGOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2, 
CFBranchLabelSchemeKind::Default,
+ "Control-Flow Branch Protection Label Scheme")
 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")
diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/inc

[clang] [llvm] [KCFI][NFC] Rename the !kcfi_type Function metadata to !cfi_type (PR #109080)

2024-09-23 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk closed 
https://github.com/llvm/llvm-project/pull/109080
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [KCFI][NFC] Rename the !kcfi_type Function metadata to !cfi_type (PR #109080)

2024-09-23 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

@MaskRay Sure, I also agree that `!cfi_type` is a too general name and covers 
too little possibility for its name. I picked this approach instead of adding 
RISC-V-specific metadata nodes just to avoid adding RISC-V specifics to the 
conceptually backend-neutral LLVM IR. However, if this approach looks too 
general, maybe it's better to just use RISC-V-specific metadata nodes.
(I'm closing this PR then, since it's not a good idea.)

https://github.com/llvm/llvm-project/pull/109080
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-25 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 2eda754ee96aa416063ff65bddfc5e856b626454 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Wed, 4 Sep 2024 18:40:48 +0800
Subject: [PATCH 1/4] [clang][RISCV] Introduce command line options for
 Zicfilp-backed forward-edge CFI

This patch enables the following command line flags for RISC-V targets:

+ `-fcf-protection=branch` turns on forward-edge control-flow integrity 
conditioning on RISC-V targets with Zicfilp extension
+ `-mcf-branch-label-scheme=unlabeled|func-sig` selects the label scheme used 
in the forward-edge CFI conditioning
---
 .../clang/Basic/CFProtectionOptions.def   | 15 +
 .../include/clang/Basic/CFProtectionOptions.h | 38 
 clang/include/clang/Basic/CodeGenOptions.def  |  2 +
 clang/include/clang/Basic/CodeGenOptions.h|  1 +
 clang/include/clang/Basic/LangOptions.def |  2 +
 clang/include/clang/Basic/LangOptions.h   |  2 +
 clang/include/clang/Basic/TargetInfo.h|  7 +++
 clang/include/clang/Driver/Options.td |  4 ++
 clang/lib/Basic/TargetInfo.cpp| 10 
 clang/lib/Basic/Targets/RISCV.h   | 22 +++
 clang/lib/Driver/ToolChains/Clang.cpp |  4 ++
 clang/lib/Frontend/CompilerInvocation.cpp | 40 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 58 +++
 13 files changed, 205 insertions(+)
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.def
 create mode 100644 clang/include/clang/Basic/CFProtectionOptions.h
 create mode 100644 clang/test/CodeGen/RISCV/riscv-cf-protection.c

diff --git a/clang/include/clang/Basic/CFProtectionOptions.def 
b/clang/include/clang/Basic/CFProtectionOptions.def
new file mode 100644
index 00..b9df2de7f7eba0
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.def
@@ -0,0 +1,15 @@
+//===-- CFProtectionOptions.def - cf-protection options -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+
+#ifdef CF_BRANCH_LABEL_SCHEME
+CF_BRANCH_LABEL_SCHEME(Unlabeled, unlabeled)
+CF_BRANCH_LABEL_SCHEME(FuncSig, func-sig)
+
+#undef CF_BRANCH_LABEL_SCHEME
+#endif // #ifdef CF_BRANCH_LABEL_SCHEME
diff --git a/clang/include/clang/Basic/CFProtectionOptions.h 
b/clang/include/clang/Basic/CFProtectionOptions.h
new file mode 100644
index 00..13f46d4c13e7e7
--- /dev/null
+++ b/clang/include/clang/Basic/CFProtectionOptions.h
@@ -0,0 +1,38 @@
+//===--- CFProtectionOptions.h --*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+//  This file defines constants for -fcf-protection and other related flags.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+#define LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
+
+#include "llvm/Support/ErrorHandling.h"
+
+namespace clang {
+
+enum class CFBranchLabelSchemeKind {
+  Default,
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal) Kind,
+#include "clang/Basic/CFProtectionOptions.def"
+};
+
+static inline const char *
+getCFBranchLabelSchemeFlagVal(const CFBranchLabelSchemeKind Scheme) {
+#define CF_BRANCH_LABEL_SCHEME(Kind, FlagVal)  
\
+  if (Scheme == CFBranchLabelSchemeKind::Kind) 
\
+return #FlagVal;
+#include "clang/Basic/CFProtectionOptions.def"
+
+  llvm::report_fatal_error("invalid scheme");
+}
+
+} // namespace clang
+
+#endif // #ifndef LLVM_CLANG_BASIC_CFPROTECTIONOPTIONS_H
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index b600198998d85b..de7ae73f8a603b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -110,6 +110,8 @@ CODEGENOPT(CFProtectionReturn , 1, 0) ///< if 
-fcf-protection is
   ///< set to full or return.
 CODEGENOPT(CFProtectionBranch , 1, 0) ///< if -fcf-protection is
   ///< set to full or branch.
+ENUM_CODEGENOPT(CFBranchLabelScheme, CFBranchLabelSchemeKind, 2,
+CFBranchLabelSchemeKind::Default) ///< if -mcf-branch-label-scheme is set.
 CODEGENOPT(FunctionReturnThunks, 1, 0) ///< 
-mfunction-return={keep|thunk-extern}
 CODEGENOPT(IndirectBranchCSPrefix, 1, 0) ///< if -mindirect-branch-cs-prefix
  ///< is set.
diff --git a/clang/incl

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-09-25 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,90 @@
+// Default cf-branch-label-scheme is func-sig
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// 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 -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 -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-UNLABELED-SCHEME-UNUSED %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: 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: 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: %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 -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: %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: 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: 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: %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=func-sig -c %s \
+// RUN:   -o /dev/null 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-FUNC-SIG-SCHEME-UNUSED %s
+
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-UNLABELED-DAG: __riscv_landing_pad_unlabeled 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad 1{{$}}
+// CHECK-ZICFILP-FUNC-SIG-DAG: __riscv_landing_pad_func_sig 1{{$}}
+// 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:
+// CHECK-UNLABELED-SCHEME-UNUSED-SAME: '-mcf-branch-label-scheme=unlabeled'
+// CHECK-FUNC-SIG-SCHEME-UNUSED: warning: argument unused during compilation:
+// CHECK-FUNC-SIG-SCHEME-UNUSED-SAME: '-mcf-branch-label-scheme=func-sig'

mylai-mtk wrote:

To provide diagnostics at the preprocessor's get definition step, I supplied 
`DiagnosticsEngine` to `TargetInfo::getTargetDefines()` so diagnostics can be 
made in it. This is a change to a widely used interf

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2024-10-03 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Hello, is there anything more that I should amend about this PR?

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [Clang][RISCV] Support -fcf-protection=return for RISC-V (PR #112477)

2024-10-24 Thread Ming-Yi Lai via cfe-commits


@@ -0,0 +1,30 @@
+; ModuleID = '/home/jhuang4/workspace/test.c'

mylai-mtk wrote:

I guess the inclusion of this file is a mistake?

https://github.com/llvm/llvm-project/pull/112477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Remove forced-sw-shadow-stack (PR #115355)

2024-11-07 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

LGTM

https://github.com/llvm/llvm-project/pull/115355
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [KCFI][NFC] Rename the !kcfi_type Function metadata to !cfi_type (PR #109080)

2024-09-17 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk created 
https://github.com/llvm/llvm-project/pull/109080

According to the comments in , which 
introduces the KCFI sanitizer, the data structures of KCFI should avoid 
embedding the term "kcfi" in their names so that later/new CFI mechanisms can 
make use of the data structures, however, those "kcfi" were not removed 
completely. This patch further removes those "kcfi" terms from the Function 
metadata !kcfi_type so that the RISC-V Zicfilp extension code generation can 
reuse the same data path as the KCFI sanitizer to pass function-specific CFI 
labels.

>From b3f6c6edbbcc005f9ceb4dec5873b23fcc75989b Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 14 Jun 2024 15:28:11 +0800
Subject: [PATCH] [KCFI][NFC] Rename the !kcfi_type Function metadata to
 !cfi_type

According to the comments in , which
introduces the KCFI sanitizer, the data structures of KCFI should avoid
embedding the term "kcfi" in their names so that later/new CFI mechanisms can
make use of the data structures, however, those "kcfi" were not removed
completely. This patch further removes those "kcfi" terms from the Function
metadata !kcfi_type so that the RISC-V Zicfilp extension code generation can
reuse the same data path as the KCFI sanitizer to pass function-specific CFI
labels.
---
 clang/lib/CodeGen/CodeGenModule.cpp   | 16 ++---
 clang/lib/CodeGen/CodeGenModule.h |  6 ++---
 clang/test/CodeGen/kcfi-normalize.c   |  6 ++---
 clang/test/CodeGen/kcfi.c | 20 
 llvm/docs/LangRef.rst | 12 +-
 llvm/include/llvm/IR/FixedMetadataKinds.def   |  2 +-
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp|  2 +-
 llvm/lib/CodeGen/MachineFunction.cpp  |  3 ++-
 llvm/lib/IR/Verifier.cpp  | 23 +--
 llvm/lib/Target/X86/X86AsmPrinter.cpp |  2 +-
 llvm/lib/Transforms/IPO/MergeFunctions.cpp|  4 ++--
 .../InstCombine/InstCombineCalls.cpp  |  4 ++--
 llvm/lib/Transforms/Utils/ModuleUtils.cpp |  2 +-
 llvm/test/CodeGen/AArch64/kcfi-bti.ll |  4 ++--
 .../AArch64/kcfi-patchable-function-prefix.ll |  4 ++--
 llvm/test/CodeGen/AArch64/kcfi.ll |  2 +-
 llvm/test/CodeGen/ARM/kcfi.ll |  2 +-
 llvm/test/CodeGen/RISCV/kcfi-isel-mir.ll  |  2 +-
 llvm/test/CodeGen/RISCV/kcfi-mir.ll   |  2 +-
 .../RISCV/kcfi-patchable-function-prefix.ll   |  4 ++--
 llvm/test/CodeGen/RISCV/kcfi.ll   |  2 +-
 .../X86/kcfi-patchable-function-prefix.ll |  4 ++--
 llvm/test/CodeGen/X86/kcfi.ll |  6 ++---
 .../AddressSanitizer/kcfi-offset.ll   |  2 +-
 .../Instrumentation/AddressSanitizer/kcfi.ll  |  4 ++--
 .../GCOVProfiling/kcfi-normalize.ll   |  8 +++
 llvm/test/Transforms/GCOVProfiling/kcfi.ll|  8 +++
 .../Verifier/metadata-function-kcfi-type.ll   | 22 +-
 28 files changed, 89 insertions(+), 89 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 17b82b205063d4..561673ab960b31 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -910,7 +910,7 @@ void CodeGenModule::Release() {
 CodeGenFunction(*this).EmitCfiCheckStub();
   }
   if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
-finalizeKCFITypes();
+finalizeCFITypes();
   emitAtAvailableLinkGuard();
   if (Context.getTargetInfo().getTriple().isWasm())
 EmitMainVoidAlias();
@@ -2818,10 +2818,10 @@ void 
CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD,
   F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
 }
 
-void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) {
+void CodeGenModule::setCFIType(const FunctionDecl *FD, llvm::Function *F) {
   llvm::LLVMContext &Ctx = F->getContext();
   llvm::MDBuilder MDB(Ctx);
-  F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
+  F->setMetadata(llvm::LLVMContext::MD_cfi_type,
  llvm::MDNode::get(
  Ctx, 
MDB.createConstant(CreateKCFITypeId(FD->getType();
 }
@@ -2835,13 +2835,13 @@ static bool allowKCFIIdentifier(StringRef Name) {
   });
 }
 
-void CodeGenModule::finalizeKCFITypes() {
+void CodeGenModule::finalizeCFITypes() {
   llvm::Module &M = getModule();
   for (auto &F : M.functions()) {
-// Remove KCFI type metadata from non-address-taken local functions.
+// Remove CFI type metadata from non-address-taken local functions.
 bool AddressTaken = F.hasAddressTaken();
 if (!AddressTaken && F.hasLocalLinkage())
-  F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type);
+  F.eraseMetadata(llvm::LLVMContext::MD_cfi_type);
 
 // Generate a constant with the expected KCFI type identifier for all
 // address-taken function declarations to support annotating indirectly
@@ -2850,7

[libunwind] [libunwind][NFC] Remove the CET keyword in shadow stack-related stuffs (PR #126663)

2025-02-12 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/126663

>From 39731df5b3e48fe2623786b120dc0eae604f8de6 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Tue, 11 Feb 2025 11:20:42 +0800
Subject: [PATCH 1/3] [libunwind][NFC] Remove the CET keyword in shadow
 stack-related stuffs

libunwind currently supports shadow stack based on the Intel CET and AArch64
GCS technology, but throughout related codes, the Intel-specific keyword,
"CET", is used to refer to the generic concept of control-flow integrity/shadow
stack. This patch replaces such wordings with architecture-neutral term "shadow
stack" (abbr. "ss") to allow future implementation to avoid using the
Intel-specific "CET" term.
---
 libunwind/src/CMakeLists.txt  |  2 +-
 libunwind/src/Registers.hpp   |  8 +--
 libunwind/src/UnwindCursor.hpp|  4 +-
 libunwind/src/UnwindLevel1.c  | 63 ++-
 .../{cet_unwind.h => shadow_stack_unwind.h}   | 12 ++--
 5 files changed, 46 insertions(+), 43 deletions(-)
 rename libunwind/src/{cet_unwind.h => shadow_stack_unwind.h} (88%)

diff --git a/libunwind/src/CMakeLists.txt b/libunwind/src/CMakeLists.txt
index ecbd019bb29ea..3bbbc70fde79b 100644
--- a/libunwind/src/CMakeLists.txt
+++ b/libunwind/src/CMakeLists.txt
@@ -36,7 +36,7 @@ set(LIBUNWIND_HEADERS
 AddressSpace.hpp
 assembly.h
 CompactUnwinder.hpp
-cet_unwind.h
+shadow_stack_unwind.h
 config.h
 dwarf2.h
 DwarfInstructions.hpp
diff --git a/libunwind/src/Registers.hpp b/libunwind/src/Registers.hpp
index 861e6b5f6f2c5..df79f0439ae85 100644
--- a/libunwind/src/Registers.hpp
+++ b/libunwind/src/Registers.hpp
@@ -15,7 +15,7 @@
 #include 
 #include 
 
-#include "cet_unwind.h"
+#include "shadow_stack_unwind.h"
 #include "config.h"
 #include "libunwind.h"
 
@@ -48,7 +48,7 @@ class _LIBUNWIND_HIDDEN Registers_x86;
 extern "C" void __libunwind_Registers_x86_jumpto(Registers_x86 *);
 
 #if defined(_LIBUNWIND_USE_CET)
-extern "C" void *__libunwind_cet_get_jump_target() {
+extern "C" void *__libunwind_ss_get_jump_target() {
   return reinterpret_cast(&__libunwind_Registers_x86_jumpto);
 }
 #endif
@@ -268,7 +268,7 @@ class _LIBUNWIND_HIDDEN Registers_x86_64;
 extern "C" void __libunwind_Registers_x86_64_jumpto(Registers_x86_64 *);
 
 #if defined(_LIBUNWIND_USE_CET)
-extern "C" void *__libunwind_cet_get_jump_target() {
+extern "C" void *__libunwind_ss_get_jump_target() {
   return reinterpret_cast(&__libunwind_Registers_x86_64_jumpto);
 }
 #endif
@@ -1817,7 +1817,7 @@ class _LIBUNWIND_HIDDEN Registers_arm64;
 extern "C" void __libunwind_Registers_arm64_jumpto(Registers_arm64 *);
 
 #if defined(_LIBUNWIND_USE_GCS)
-extern "C" void *__libunwind_cet_get_jump_target() {
+extern "C" void *__libunwind_ss_get_jump_target() {
   return reinterpret_cast(&__libunwind_Registers_arm64_jumpto);
 }
 #endif
diff --git a/libunwind/src/UnwindCursor.hpp b/libunwind/src/UnwindCursor.hpp
index 0923052b1b588..5cb04b1f76820 100644
--- a/libunwind/src/UnwindCursor.hpp
+++ b/libunwind/src/UnwindCursor.hpp
@@ -11,7 +11,7 @@
 #ifndef __UNWINDCURSOR_HPP__
 #define __UNWINDCURSOR_HPP__
 
-#include "cet_unwind.h"
+#include "shadow_stack_unwind.h"
 #include 
 #include 
 #include 
@@ -3122,7 +3122,7 @@ bool UnwindCursor::isReadableAddr(const pint_t 
addr) const {
 #endif
 
 #if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
-extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
+extern "C" void *__libunwind_ss_get_registers(unw_cursor_t *cursor) {
   AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
   return co->get_registers();
 }
diff --git a/libunwind/src/UnwindLevel1.c b/libunwind/src/UnwindLevel1.c
index 7e785f4d31e71..2d471d5d65690 100644
--- a/libunwind/src/UnwindLevel1.c
+++ b/libunwind/src/UnwindLevel1.c
@@ -25,7 +25,7 @@
 #include 
 #include 
 
-#include "cet_unwind.h"
+#include "shadow_stack_unwind.h"
 #include "config.h"
 #include "libunwind.h"
 #include "libunwind_ext.h"
@@ -36,14 +36,17 @@
 
 #ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
 
-// When CET is enabled, each "call" instruction will push return address to
-// CET shadow stack, each "ret" instruction will pop current CET shadow stack
-// top and compare it with target address which program will return.
-// In exception handing, some stack frames will be skipped before jumping to
-// landing pad and we must adjust CET shadow stack accordingly.
-// _LIBUNWIND_POP_CET_SSP is used to adjust CET shadow stack pointer and we
-// directly jump to __libunwind_Registers_x86/x86_64_jumpto instead of using
-// a regular function call to avoid pushing to CET shadow stack again.
+// When shadow stack is enabled, a separate stack containing only return
+// addresses would be maintained. On function return, the return address would
+// be compared to the popped address from shadow stack to ensure the return
+// target is not tempered with. When unwinding, we're skipping the normal 
return

[libunwind] [libunwind][NFC] Remove the CET keyword in shadow stack-related stuffs (PR #126663)

2025-02-12 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Great!

---

Update: Use "shstk" instead of "ss" as the new name

https://github.com/llvm/llvm-project/pull/126663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][X86] Only define __CET__ macro for X86 targets (PR #127616)

2025-02-18 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

> Can you update the description to mention which PR makes RISCV targets 
> inspect -fcf-protection=?

Added PR links. Thanks!

https://github.com/llvm/llvm-project/pull/127616
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][X86] Only define __CET__ macro for X86 targets (PR #127616)

2025-02-18 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk created 
https://github.com/llvm/llvm-project/pull/127616

The `-fcf-protection` flag is now also used to enable CFI features for the 
RISC-V target, so it's not suitable to define `__CET__` solely based on the 
flag anymore. This patch moves the definition of the `__CET__` macro into X86 
target hook, so only X86 targets with the `-fcf-protection` flag would enable 
the `__CET__` macro.

>From 348e70f60f29e7fbc0f16b0f0125853cdd832b03 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Tue, 18 Feb 2025 18:30:35 +0800
Subject: [PATCH] [clang][X86] Only define __CET__ macro for X86 targets

The `-fcf-protection` flag is now also used to enable CFI features for the
RISC-V target, so it's not suitable to define `__CET__` solely based on the
flag anymore. This patch moves the definition of the `__CET__` macro into X86
target hook, so only X86 targets with the `-fcf-protection` flag would enable
the `__CET__` macro.
---
 clang/lib/Basic/Targets/X86.cpp  |  4 
 clang/lib/Frontend/CompilerInvocation.cpp| 11 ---
 clang/test/Preprocessor/riscv-cf-protection-return.c |  2 ++
 3 files changed, 6 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Basic/Targets/X86.cpp b/clang/lib/Basic/Targets/X86.cpp
index 84a05cec04e7f..e4d3ad04fe9de 100644
--- a/clang/lib/Basic/Targets/X86.cpp
+++ b/clang/lib/Basic/Targets/X86.cpp
@@ -1109,6 +1109,10 @@ void X86TargetInfo::getTargetDefines(const LangOptions 
&Opts,
 
   if (HasFloat128)
 Builder.defineMacro("__SIZEOF_FLOAT128__", "16");
+
+  if (Opts.CFProtectionReturn || Opts.CFProtectionBranch)
+Builder.defineMacro("__CET__", Twine{(Opts.CFProtectionReturn << 1) |
+ Opts.CFProtectionBranch});
 }
 
 bool X86TargetInfo::isValidFeatureName(StringRef Name) const {
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index b9a5c0589ebc4..4eb743acf327f 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -4776,17 +4776,6 @@ static bool ParsePreprocessorArgs(PreprocessorOptions 
&Opts, ArgList &Args,
 }
   }
 
-  // Add the __CET__ macro if a CFProtection option is set.
-  if (const Arg *A = Args.getLastArg(OPT_fcf_protection_EQ)) {
-StringRef Name = A->getValue();
-if (Name == "branch")
-  Opts.addMacroDef("__CET__=1");
-else if (Name == "return")
-  Opts.addMacroDef("__CET__=2");
-else if (Name == "full")
-  Opts.addMacroDef("__CET__=3");
-  }
-
   // Add macros from the command line.
   for (const auto *A : Args.filtered(OPT_D, OPT_U)) {
 if (A->getOption().matches(OPT_D))
diff --git a/clang/test/Preprocessor/riscv-cf-protection-return.c 
b/clang/test/Preprocessor/riscv-cf-protection-return.c
index 3a93a88fa6839..a4cbaa1edf68c 100644
--- a/clang/test/Preprocessor/riscv-cf-protection-return.c
+++ b/clang/test/Preprocessor/riscv-cf-protection-return.c
@@ -40,5 +40,7 @@
 // RUN: -menable-experimental-extensions -fcf-protection=full -E -dM %s -o - \
 // RUN: | FileCheck --check-prefixes=SHSTK-MACRO %s
 
+// SHSTK-MACRO-NOT: __CET__
 // SHSTK-MACRO: __riscv_shadow_stack 1{{$}}
+// SHSTK-MACRO-NOT: __CET__
 // NO-MACRO-NOT: __riscv_shadow_stack

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][X86] Only define __CET__ macro for X86 targets (PR #127616)

2025-02-18 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

See https://github.com/llvm/llvm-project/pull/109784 and 
https://github.com/llvm/llvm-project/pull/112477 for the adoption of 
`-fcf-protection` for RISC-V targets

https://github.com/llvm/llvm-project/pull/127616
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-02-18 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk closed 
https://github.com/llvm/llvm-project/pull/127592
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2025-02-18 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk created 
https://github.com/llvm/llvm-project/pull/127592

The `-fcf-protection=[full|return]` flag enables shadow stack implementation 
based on RISC-V Zicfiss extension. This patch adds the `__riscv_shadow_stack` 
predefined macro to preprocessing when such a shadow stack implementation is 
enabled.

>From 84ced6d0108a8250274b8d87b7b6ddba858e0dc8 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Tue, 11 Feb 2025 17:36:06 +0800
Subject: [PATCH] [clang][RISCV] Introduce preprocessor macro when
 Zicfiss-based shadow stack is enabled

The `-fcf-protection=[full|return]` flag enables shadow stack implementation
based on RISC-V Zicfiss extension. This patch adds the `__riscv_shadow_stack`
predefined macro to preprocessing when such a shadow stack implementation is
enabled.
---
 clang/include/clang/Basic/LangOptions.def |  1 +
 clang/lib/Basic/Targets/RISCV.cpp |  3 ++
 clang/lib/Frontend/CompilerInvocation.cpp |  7 ++-
 .../Preprocessor/riscv-cf-protection-return.c | 44 +++
 4 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Preprocessor/riscv-cf-protection-return.c

diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index bfab0baa089cf..383440ddbc0ea 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -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")
diff --git a/clang/lib/Basic/Targets/RISCV.cpp 
b/clang/lib/Basic/Targets/RISCV.cpp
index b4aa3206fcfab..dff990d15dd62 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -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 =
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 014e629c959e2..b9a5c0589ebc4 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -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;
 }
   }
 
diff --git a/clang/test/Preprocessor/riscv-cf-protection-return.c 
b/clang/test/Preprocessor/riscv-cf-protection-return.c
new file mode 100644
index 0..3a93a88fa6839
--- /dev/null
+++ b/clang/test/Preprocessor/riscv-cf-protection-return.c
@@ -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-pr

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-02-17 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 3bb0b659009ade3dcd04d7bbb88b57e7b072fac5 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 10 May 2024 14:16:59 +0800
Subject: [PATCH 1/6] [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 | 19 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 79 +++
 2 files changed, 98 insertions(+)

diff --git a/clang/lib/Basic/Targets/RISCV.cpp 
b/clang/lib/Basic/Targets/RISCV.cpp
index b4aa3206fcfab..f00fa6bf3904f 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -238,6 +238,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+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");
+}
+  }
 }
 
 static constexpr int NumRVVBuiltins =
diff --git a/clang/test/CodeGen/RISCV/riscv-cf-protection.c 
b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
index 3a9855a3d2f01..db7b65061658c 100644
--- a/clang/test/CodeGen/RISCV/riscv-cf-protection.c
+++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
@@ -1,71 +1,143 @@
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %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=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,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=NO-MACRO,FUNC-SIG-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,FUNC-SIG-SCHEME-UNUSED %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM -emit-llvm %s -o - | \
+// RUN: FileCheck --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %s
 
+// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -E -dM %s \
+// RUN: -o - 2>&1 | FileCheck \
+// RUN: --check-prefixes=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -mcf-branch-label-sch

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-02-16 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 3bb0b659009ade3dcd04d7bbb88b57e7b072fac5 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 10 May 2024 14:16:59 +0800
Subject: [PATCH 1/5] [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 | 19 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 79 +++
 2 files changed, 98 insertions(+)

diff --git a/clang/lib/Basic/Targets/RISCV.cpp 
b/clang/lib/Basic/Targets/RISCV.cpp
index b4aa3206fcfab..f00fa6bf3904f 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -238,6 +238,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+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");
+}
+  }
 }
 
 static constexpr int NumRVVBuiltins =
diff --git a/clang/test/CodeGen/RISCV/riscv-cf-protection.c 
b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
index 3a9855a3d2f01..db7b65061658c 100644
--- a/clang/test/CodeGen/RISCV/riscv-cf-protection.c
+++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
@@ -1,71 +1,143 @@
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %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=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,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=NO-MACRO,FUNC-SIG-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,FUNC-SIG-SCHEME-UNUSED %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM -emit-llvm %s -o - | \
+// RUN: FileCheck --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %s
 
+// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -E -dM %s \
+// RUN: -o - 2>&1 | FileCheck \
+// RUN: --check-prefixes=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -mcf-branch-label-sch

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-02-16 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 3bb0b659009ade3dcd04d7bbb88b57e7b072fac5 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 10 May 2024 14:16:59 +0800
Subject: [PATCH 1/4] [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 | 19 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 79 +++
 2 files changed, 98 insertions(+)

diff --git a/clang/lib/Basic/Targets/RISCV.cpp 
b/clang/lib/Basic/Targets/RISCV.cpp
index b4aa3206fcfab..f00fa6bf3904f 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -238,6 +238,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+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");
+}
+  }
 }
 
 static constexpr int NumRVVBuiltins =
diff --git a/clang/test/CodeGen/RISCV/riscv-cf-protection.c 
b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
index 3a9855a3d2f01..db7b65061658c 100644
--- a/clang/test/CodeGen/RISCV/riscv-cf-protection.c
+++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
@@ -1,71 +1,143 @@
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %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=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,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=NO-MACRO,FUNC-SIG-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,FUNC-SIG-SCHEME-UNUSED %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM -emit-llvm %s -o - | \
+// RUN: FileCheck --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %s
 
+// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -E -dM %s \
+// RUN: -o - 2>&1 | FileCheck \
+// RUN: --check-prefixes=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -mcf-branch-label-sch

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-02-16 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Update:

+ Rebase to latest `main`
+ Remove redundant macro contents
+ Move tests of macros from CodeGen dir to Preprocessor dir
+ Add tests for default branch label scheme
+ Add tests for `-fcf-protection=full`
+ Rewrite PR/commit message

---

It looks like some of the new macros are added in gcc already 
(https://github.com/gcc-mirror/gcc/commit/59a869d7196132ae5257fcb068508561d3526c7d).
 Is it possible that we restart processing this PR now?

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-02-16 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-02-17 Thread Ming-Yi Lai via cfe-commits


@@ -238,6 +238,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+if (Scheme == CFBranchLabelSchemeKind::Default)
+  Scheme = getDefaultCFBranchLabelScheme();
+
+Builder.defineMacro("__riscv_landing_pad");
+switch (Scheme) {
+case CFBranchLabelSchemeKind::Unlabeled:
+  Builder.defineMacro("__riscv_landing_pad_unlabeled");
+  break;
+case CFBranchLabelSchemeKind::FuncSig:
+  Builder.defineMacro("__riscv_landing_pad_func_sig");
+  break;

mylai-mtk wrote:

To drop this case I have 2 options, but I think neither is appropriate.

+ Option 1:

```c++
case CFBranchLabelSchemeKind::FuncSig:
  // TODO: Define macros after the func-sig scheme is implemented
  break;
```

Considering we already accepted `-mcf-branch-label-scheme=func-sig`, if the 
user uses the flag now, he would get a success program run, but incomplete 
compilation result, which is also misleading, so it's better to add an error to 
prevent the case, which leads to Option 2.

+ Option 2:

```c++
case CFBranchLabelSchemeKind::FuncSig:
  // TODO: Define macros after the func-sig scheme is implemented
  llvm::report_fatal_error("the func-sig scheme is not implemented yet!");
  break;
```

This prevents the misleading results, but it breaks tests in the `main` trunk, 
so existing tests for the func-sig scheme have to be removed, but I don't think 
it's worth it to undo the work just to merge this PR.

---

Perhaps we should still pause this PR? This is not really an urgent thing given 
the concern of misleading users.

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [libunwind][NFC] Remove the CET keyword in shadow stack-related stuffs (PR #126663)

2025-02-20 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk closed 
https://github.com/llvm/llvm-project/pull/126663
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-05-13 Thread Ming-Yi Lai via cfe-commits


@@ -238,6 +238,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+if (Scheme == CFBranchLabelSchemeKind::Default)
+  Scheme = getDefaultCFBranchLabelScheme();
+
+Builder.defineMacro("__riscv_landing_pad");
+switch (Scheme) {
+case CFBranchLabelSchemeKind::Unlabeled:
+  Builder.defineMacro("__riscv_landing_pad_unlabeled");
+  break;
+case CFBranchLabelSchemeKind::FuncSig:
+  Builder.defineMacro("__riscv_landing_pad_func_sig");
+  break;

mylai-mtk wrote:

Used Option 1 to drop the func-sig part for now

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-05-13 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 3bb0b659009ade3dcd04d7bbb88b57e7b072fac5 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 10 May 2024 14:16:59 +0800
Subject: [PATCH 1/7] [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 | 19 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 79 +++
 2 files changed, 98 insertions(+)

diff --git a/clang/lib/Basic/Targets/RISCV.cpp 
b/clang/lib/Basic/Targets/RISCV.cpp
index b4aa3206fcfab..f00fa6bf3904f 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -238,6 +238,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 else
   Builder.defineMacro("__riscv_32e");
   }
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+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");
+}
+  }
 }
 
 static constexpr int NumRVVBuiltins =
diff --git a/clang/test/CodeGen/RISCV/riscv-cf-protection.c 
b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
index 3a9855a3d2f01..db7b65061658c 100644
--- a/clang/test/CodeGen/RISCV/riscv-cf-protection.c
+++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
@@ -1,71 +1,143 @@
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %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=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,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=NO-MACRO,FUNC-SIG-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,FUNC-SIG-SCHEME-UNUSED %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM -emit-llvm %s -o - | \
+// RUN: FileCheck --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %s
 
+// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -E -dM %s \
+// RUN: -o - 2>&1 | FileCheck \
+// RUN: --check-prefixes=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -mcf-branch-label-sch

[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-05-13 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-05-19 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add Zicfilp CFI unlabeled scheme preprocessor macros (PR #109600)

2025-05-19 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add Zicfilp CFI unlabeled scheme preprocessor macros (PR #109600)

2025-05-19 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Update: Rebase to `main` trunk

https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang][RISCV] Add preprocessor macros for Zicfilp CFI scheme (PR #109600)

2025-05-19 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/109600

>From 93617b9aa7bdc9368de80b2595e37f0e8ed72c52 Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 10 May 2024 14:16:59 +0800
Subject: [PATCH 1/7] [clang][RISCV] Add Zicfilp CFI unlabeled scheme
 preprocessor macros

+ `#define __riscv_landing_pad 1` when any Zicfilp scheme is enabled
+ `#define __riscv_landing_pad_unlabeled 1` when the unlabeled Zicfilp scheme is
enabled
---
 clang/lib/Basic/Targets/RISCV.cpp | 19 +
 .../test/CodeGen/RISCV/riscv-cf-protection.c  | 79 +++
 2 files changed, 98 insertions(+)

diff --git a/clang/lib/Basic/Targets/RISCV.cpp 
b/clang/lib/Basic/Targets/RISCV.cpp
index a1a2437f288a0..85d0610df974b 100644
--- a/clang/lib/Basic/Targets/RISCV.cpp
+++ b/clang/lib/Basic/Targets/RISCV.cpp
@@ -241,6 +241,25 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions 
&Opts,
 
   if (Opts.CFProtectionReturn && ISAInfo->hasExtension("zicfiss"))
 Builder.defineMacro("__riscv_shadow_stack");
+
+  if (Opts.CFProtectionBranch) {
+auto Scheme = Opts.getCFBranchLabelScheme();
+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");
+}
+  }
 }
 
 static constexpr int NumRVVBuiltins =
diff --git a/clang/test/CodeGen/RISCV/riscv-cf-protection.c 
b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
index 3a9855a3d2f01..db7b65061658c 100644
--- a/clang/test/CodeGen/RISCV/riscv-cf-protection.c
+++ b/clang/test/CodeGen/RISCV/riscv-cf-protection.c
@@ -1,71 +1,143 @@
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -menable-experimental-extensions \
+// RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %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=NO-MACRO,UNLABELED-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=unlabeled -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,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=NO-MACRO,FUNC-SIG-SCHEME-UNUSED %s
+
 // RUN: %clang --target=riscv32 -menable-experimental-extensions \
 // RUN: -march=rv32i_zicfilp1p0 -mcf-branch-label-scheme=func-sig -S \
 // RUN: -emit-llvm %s -o - 2>&1 | FileCheck \
 // RUN: --check-prefixes=NO-FLAG,FUNC-SIG-SCHEME-UNUSED %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=unlabeled -E -dM -emit-llvm %s -o - | \
+// RUN: FileCheck --check-prefixes=LPAD-MACRO,UNLABELED-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=unlabeled -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,UNLABELED-FLAG %s
 
+// RUN: %clang --target=riscv32 -fcf-protection=branch \
+// RUN: -mcf-branch-label-scheme=func-sig -E -dM %s -o - | FileCheck \
+// RUN: --check-prefixes=LPAD-MACRO,FUNC-SIG-MACRO %s
+
 // RUN: %clang --target=riscv32 -fcf-protection=branch \
 // RUN: -mcf-branch-label-scheme=func-sig -S -emit-llvm %s -o - | FileCheck \
 // RUN: --check-prefixes=BRANCH-PROT-FLAG,FUNC-SIG-FLAG %s
 
+// RUN: %clang --target=riscv32 -mcf-branch-label-scheme=unlabeled -E -dM %s \
+// RUN: -o - 2>&1 | F

[clang] [Clang][RISCV] Add Zicfilp CFI unlabeled scheme preprocessor macros (PR #109600)

2025-05-19 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk closed 
https://github.com/llvm/llvm-project/pull/109600
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2025-05-25 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

> For missing prototypes, we can statically detect calls which pass arguments 
> to function without a prototype. Generating code we know will never work, 
> without a diagnostic, is a bad idea.

@efriedma-quic If what you need to see are diagnostics, I can add them in this 
PR. I already have this particular one in my internal branch since I need it to 
filter out those tests that are expected to fail due to passing arguments to 
unprototyped functions.

> For the type merging issue, we can't detect it completely reliably, but we 
> can detect function definitions which aren't compatible with the 
> corresponding function declaration, and diagnose that.

I think "function definitions which aren't compatible with the corresponding 
function declaration" is an error and should already be diagnosed?



https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV][Zicfilp] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2025-05-27 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk edited 
https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV][Zicfilp] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2025-05-28 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

> There are intentional tradeoffs here, for the ABI, which should be made 
> carefully; in some cases, it might be the right tradeoff to reject 
> standard-compliant code. If you've considered it, I guess I won't object. I 
> can't find any discussion of it, though.

The reduction of unprototyped function types to prototyped no parameter 
function types is proposed by me in 
,
 which is accepted without much discussion/objection. I also proposed treating 
such usages as errors, but it looks like this part had not been taken into the 
spec.

> With my psABI hat on, settling on an ABI that turns out to be wrong is my big 
> concern, but you can't find those corner cases until you try it at scale (and 
> that's speaking from experience where we've missed things in the past for 
> CHERI). This is probably the kind of thing that needs to be explicitly marked 
> as experimental and with no ABI forward-compatibility guarantees, but that 
> needs to be landed so that people can start actually using it and finding 
> where it falls down in the real world. (But that doesn't obviate proper code 
> review, the implementation should still be proper even if the ABI isn't 
> stable.)

Agree. I'm pretty sure that the mangling scheme is not perfect, especially when 
applied to ancient C projects. With regard to forward-compatibility management, 
I think we can introduce versioning to Zicfilp psABI as well? (cf. 
Tag_RISCV_atomic_abi in `.riscv.attributes` section) @kito-cheng 

> Given the comparison with the AArch64 PAuth work, how much is building on 
> that versus doing our own independent thing?

We did not refer to the AArch64 PtrAuth work when we devised the Zicfilp 
mangling scheme, and instead, our foundation was the Itanium C++ ABI. The time 
at which these 2 mangling schemes appeared were close, and I did not know that 
AArch64 was developing a similar thing back then, so adapting the Itanium C++ 
ABI looked like the most practical approach. 

> Is there any real-world deployment experience from that that we can make use 
> of?

Not that I know of... The AArch64 PtrAuth work is in the main trunk for only 
less than 1 year, and it may take multiple years for the feature to mature, so 
I would say that we can surely learn some valuable things from it if we observe 
it long enough, but for now, I think both schemes are in a similarly nascent 
status, with the AArch64 PtrAuth scheme being more explicit and controllable.

https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2025-05-21 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

> A high level suggestion is don't hash that until MC layer, so that we can 
> easier debug and observe that from the IR level, so that means we can drop 
> the hash part from this PR.

@kito-cheng I prefer to keep the function signature as an auxiliary metadata 
with the name `riscv_lpad_func_sig`, so future schemes and other language 
frontends can reuse the `riscv_lpad_label` metadata flow and have the 
opportunity of not being forced to adopt the idea of function signatures. 
Besides, this is in fact what we currently have in our internal branch, since a 
later development of the Zicfilp psABI spec introduced the lpad mapping symbol, 
which writes function signatures into ELF files, and thus the internal branch 
had already been modified to pass function signature as an extra metadata.

Regarding the testability issue, I think later I'll update this PR so clang 
attaches both the `riscv_lpad_label` and `riscv_lpad_func_sig` metadata to 
Functions, and unittests can be added specifically for the 
`zicfilpFuncSigHash()` function.

https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2025-05-23 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

Update: Rebased to `main` trunk; keep function signature in LLVM IR

https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2025-05-23 Thread Ming-Yi Lai via cfe-commits

https://github.com/mylai-mtk updated 
https://github.com/llvm/llvm-project/pull/111661

>From b3a7b9f4bd6d65f837b3cc9e4a0e0bd6fdede6ab Mon Sep 17 00:00:00 2001
From: Ming-Yi Lai 
Date: Fri, 31 May 2024 17:03:04 +0800
Subject: [PATCH] [clang][RISCV] Emit RISCV function-signature-based CFI label
 in llvm::Function metadata

---
 clang/include/clang/AST/ASTContext.h  |   7 +
 clang/include/clang/AST/Mangle.h  |   5 +
 clang/lib/AST/ASTContext.cpp  |   6 +
 clang/lib/AST/ItaniumMangle.cpp   | 138 +-
 clang/lib/CodeGen/CodeGenModule.cpp   | 124 
 clang/lib/CodeGen/CodeGenModule.h |  17 +++
 .../mangle-class-covariant-virtual-method.cpp |  39 +
 .../mangle-class-destructor.cpp   |  19 +++
 ...angle-class-incovariant-virtual-method.cpp |  20 +++
 .../zicfilp-func-sig/mangle-class-method.cpp  |  21 +++
 .../mangle-class-static-method.cpp|  23 +++
 .../mangle-func-empty-param-list.c|  17 +++
 .../mangle-func-empty-param-list.cpp  |  17 +++
 .../mangle-ignore-exception-spec.cpp  |  17 +++
 .../RISCV/zicfilp-func-sig/mangle-main.cpp|  41 ++
 .../RISCV/zicfilp-func-sig/mangle-wchar-t.cpp |  14 ++
 llvm/include/llvm/Support/RISCVISAUtils.h |   6 +-
 llvm/lib/Support/RISCVISAUtils.cpp|  22 ++-
 llvm/unittests/Support/CMakeLists.txt |   1 +
 llvm/unittests/Support/RISCVISAUtils.cpp  |  23 +++
 20 files changed, 573 insertions(+), 4 deletions(-)
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-covariant-virtual-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-destructor.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-incovariant-virtual-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-class-static-method.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-func-empty-param-list.c
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-func-empty-param-list.cpp
 create mode 100644 
clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-ignore-exception-spec.cpp
 create mode 100644 clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-main.cpp
 create mode 100644 clang/test/CodeGen/RISCV/zicfilp-func-sig/mangle-wchar-t.cpp
 create mode 100644 llvm/unittests/Support/RISCVISAUtils.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 1fdc488a76507..2349f3eab3950 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -1952,6 +1952,9 @@ class ASTContext : public RefCountedBase {
   /// (struct/union/class/enum) decl.
   QualType getTagDeclType(const TagDecl *Decl) const;
 
+  /// Return the type for "void *"
+  QualType getVoidPtrType() const { return VoidPtrTy; }
+
   /// Return the unique type for "size_t" (C99 7.17), defined in
   /// .
   ///
@@ -1979,6 +1982,10 @@ class ASTContext : public RefCountedBase {
   /// defined in  as defined by the target.
   QualType getWideCharType() const { return WideCharTy; }
 
+  /// Return the type of wide characters in C context, no matter whether it's C
+  /// or C++ being compiled.
+  QualType getWCharTypeInC() const;
+
   /// Return the type of "signed wchar_t".
   ///
   /// Used when in C++, as a GCC extension.
diff --git a/clang/include/clang/AST/Mangle.h b/clang/include/clang/AST/Mangle.h
index ca72dcfd4483d..579df6d6bfbb7 100644
--- a/clang/include/clang/AST/Mangle.h
+++ b/clang/include/clang/AST/Mangle.h
@@ -215,6 +215,11 @@ class ItaniumMangleContext : public MangleContext {
 
   virtual void mangleModuleInitializer(const Module *Module, raw_ostream &) = 
0;
 
+  virtual void mangleForRISCVZicfilpFuncSigLabel(const FunctionType &FT,
+ const bool 
IsCXXInstanceMethod,
+ const bool IsCXXVirtualMethod,
+ raw_ostream &) = 0;
+
   // This has to live here, otherwise the CXXNameMangler won't have access to
   // it.
   virtual DiscriminatorOverrideTy getDiscriminatorOverride() const = 0;
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index b5417fcf20ddd..a3e1a09157265 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -6712,6 +6712,12 @@ CanQualType ASTContext::getUIntMaxType() const {
   return getFromTargetType(Target->getUIntMaxType());
 }
 
+/// Return the type of wide characters in C context, no matter whether it's C
+/// or C++ being compiled.
+QualType ASTContext::getWCharTypeInC() const {
+  return getFromTargetType(Target->getWCharType());
+}
+
 /// getSignedWCharType - Return the type of "signed wchar_t".
 /// Used when in C++, as a GCC extension.
 QualType ASTContext

[clang] [llvm] [clang][RISCV] Emit RISCV function-signature-based CFI label in llvm::Function metadata (PR #111661)

2025-05-23 Thread Ming-Yi Lai via cfe-commits

mylai-mtk wrote:

@efriedma-quic Thanks for pointing out the potential type incompatibilities 
regarding the current mangling scheme. 

Regarding this issue, I believe there's currently no perfect solution given the 
complexity of C type compatibility. It looks like even in the AArch64 PtrAuth 
implementation you referred to 
(https://github.com/llvm/llvm-project/pull/96992), some known compatible cases 
still result in incompatible mangling results.

Given that the mangling scheme implemented here had already undergone extensive 
discussion in https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/434 , I 
would prefer to avoid spec-induced type compatibility issues and focus on 
implementation in this PR. 

If you have any concrete case which you believe should result in compatible 
mangling results but does not due to the draft spec, please raise the issue in 
the RISC-V psABI thread 
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/434 , so people there 
(me included, of course) would be able to incorporate the solution into the 
spec. This PR is just an implementation of the psABI spec.

For this PR, please comment on mangling issues only if it arises from 
mis-implementation of the draft spec. Thank you.

https://github.com/llvm/llvm-project/pull/111661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits