[clang-tools-extra] [clang-tidy] Fix a typo (PR #112283)

2024-10-14 Thread Konstantin Bogdanov via cfe-commits

https://github.com/thevar1able created 
https://github.com/llvm/llvm-project/pull/112283

None

>From 31e4d362b6466b101cdf896003c6c264786b3f2a Mon Sep 17 00:00:00 2001
From: Konstantin Bogdanov 
Date: Tue, 15 Oct 2024 01:21:21 +0200
Subject: [PATCH] Fix a typo

---
 .../CrtpConstructorAccessibilityCheck.cpp|  2 +-
 .../bugprone/crtp-constructor-accessibility.cpp  | 16 
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 6175fcdfd229c3..8eaf54fe0088a4 100644
--- 
a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -165,7 +165,7 @@ void CrtpConstructorAccessibilityCheck::check(
 
 WithFriendHintIfNeeded(
 diag(Ctor->getLocation(),
- "%0 contructor allows the CRTP to be %select{inherited "
+ "%0 constructor allows the CRTP to be %select{inherited "
  "from|constructed}1 as a regular template class; consider making "
  "it private%select{| and declaring the derived class as friend}2")
 << Access << IsPublic << NeedsFriend
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
index cb41923df157cf..f33b8457cc8af5 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
@@ -26,7 +26,7 @@ template 
 class CRTP {
 public:
 CRTP() = default;
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = 
default;{{[[:space:]]*}}public:
 // CHECK-FIXES: friend T;
 };
@@ -39,7 +39,7 @@ template 
 class CRTP {
 public:
 CRTP(int) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
 // CHECK-FIXES: friend T;
 };
@@ -52,10 +52,10 @@ template 
 class CRTP {
 public:
 CRTP(int) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
 CRTP(float) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(float) 
{}{{[[:space:]]*}}public:
 
 // CHECK-FIXES: friend T;
@@ -70,13 +70,13 @@ template 
 class CRTP {
 protected:
 CRTP(int) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows 
the CRTP to be inherited from as a regular template class; consider making it 
private and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected constructor allows 
the CRTP to be inherited from as a regular template class; consider making it 
private and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) 

[clang-tools-extra] [clang-tidy] Fix a typo (PR #112283)

2024-10-14 Thread Konstantin Bogdanov via cfe-commits

https://github.com/thevar1able updated 
https://github.com/llvm/llvm-project/pull/112283

>From c9db5830f95727601900aa9a87c0970ecfbf5c76 Mon Sep 17 00:00:00 2001
From: Konstantin Bogdanov 
Date: Tue, 15 Oct 2024 03:06:46 +0200
Subject: [PATCH] Fix a typo

---
 .../CrtpConstructorAccessibilityCheck.cpp|  2 +-
 .../bugprone/crtp-constructor-accessibility.cpp  | 16 
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 6175fcdfd229c3..8eaf54fe0088a4 100644
--- 
a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -165,7 +165,7 @@ void CrtpConstructorAccessibilityCheck::check(
 
 WithFriendHintIfNeeded(
 diag(Ctor->getLocation(),
- "%0 contructor allows the CRTP to be %select{inherited "
+ "%0 constructor allows the CRTP to be %select{inherited "
  "from|constructed}1 as a regular template class; consider making "
  "it private%select{| and declaring the derived class as friend}2")
 << Access << IsPublic << NeedsFriend
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
index cb41923df157cf..f33b8457cc8af5 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/crtp-constructor-accessibility.cpp
@@ -26,7 +26,7 @@ template 
 class CRTP {
 public:
 CRTP() = default;
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP() = 
default;{{[[:space:]]*}}public:
 // CHECK-FIXES: friend T;
 };
@@ -39,7 +39,7 @@ template 
 class CRTP {
 public:
 CRTP(int) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
 // CHECK-FIXES: friend T;
 };
@@ -52,10 +52,10 @@ template 
 class CRTP {
 public:
 CRTP(int) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) {}{{[[:space:]]*}}public:
 CRTP(float) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public contructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: public constructor allows the 
CRTP to be constructed as a regular template class; consider making it private 
and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(float) 
{}{{[[:space:]]*}}public:
 
 // CHECK-FIXES: friend T;
@@ -70,13 +70,13 @@ template 
 class CRTP {
 protected:
 CRTP(int) {}
-// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected contructor allows 
the CRTP to be inherited from as a regular template class; consider making it 
private and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
+// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: protected constructor allows 
the CRTP to be inherited from as a regular template class; consider making it 
private and declaring the derived class as friend 
[bugprone-crtp-constructor-accessibility]
 // CHECK-FIXES: private:{{[[:space:]]*}}CRTP(int) 
{}{{[

[libunwind] [libunwind][X86-64] Handle Linux sigreturn trampoline when DWARF info is missing (PR #103473)

2025-02-22 Thread Konstantin Bogdanov via cfe-commits

https://github.com/thevar1able updated 
https://github.com/llvm/llvm-project/pull/103473

>From 3beb6fc40ccc0b5726b360156063ef35e2b1db3f Mon Sep 17 00:00:00 2001
From: Michael Kolupaev 
Date: Tue, 13 Aug 2024 21:16:08 +
Subject: [PATCH 1/2] [libunwind][X86-64] Handle Linux sigreturn trampoline
 when DWARF info is missing

---
 libunwind/src/UnwindCursor.hpp | 100 -
 1 file changed, 99 insertions(+), 1 deletion(-)

diff --git a/libunwind/src/UnwindCursor.hpp b/libunwind/src/UnwindCursor.hpp
index ca9927edc9990..b4ddcd2792bf2 100644
--- a/libunwind/src/UnwindCursor.hpp
+++ b/libunwind/src/UnwindCursor.hpp
@@ -33,7 +33,8 @@
 #if defined(_LIBUNWIND_TARGET_LINUX) &&
\
 (defined(_LIBUNWIND_TARGET_AARCH64) || 
\
  defined(_LIBUNWIND_TARGET_LOONGARCH) ||   
\
- defined(_LIBUNWIND_TARGET_RISCV) || defined(_LIBUNWIND_TARGET_S390X))
+ defined(_LIBUNWIND_TARGET_RISCV) || defined(_LIBUNWIND_TARGET_S390X) ||   
\
+ defined(_LIBUNWIND_TARGET_S390X) || defined(_LIBUNWIND_TARGET_X86_64))
 #include 
 #include 
 #include 
@@ -1008,6 +1009,10 @@ class UnwindCursor : public AbstractUnwindCursor{
 #if defined(_LIBUNWIND_TARGET_S390X)
   bool setInfoForSigReturn(Registers_s390x &);
   int stepThroughSigReturn(Registers_s390x &);
+#endif
+#if defined(_LIBUNWIND_TARGET_X86_64)
+  bool setInfoForSigReturn(Registers_x86_64 &);
+  int stepThroughSigReturn(Registers_x86_64 &);
 #endif
   template  bool setInfoForSigReturn(Registers &) {
 return false;
@@ -3032,6 +3037,99 @@ int UnwindCursor::stepThroughSigReturn(Registers_s390x &) {
 #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
// defined(_LIBUNWIND_TARGET_S390X)
 
+#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&   
\
+defined(_LIBUNWIND_TARGET_X86_64)
+template 
+bool UnwindCursor::setInfoForSigReturn(Registers_x86_64 &) {
+  // Look for the sigreturn trampoline. The trampoline's body is two
+  // specific instructions (see below). Typically the trampoline comes from the
+  // vDSO or from libc.
+  //
+  // This special code path is a fallback that is only used if the trampoline
+  // lacks proper (e.g. DWARF) unwind info.
+  const uint8_t amd64_linux_sigtramp_code[9] = {
+  0x48, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00, // mov rax, 15
+  0x0f, 0x05// syscall
+  };
+  const size_t code_size = sizeof(amd64_linux_sigtramp_code);
+
+  // The PC might contain an invalid address if the unwind info is bad, so
+  // directly accessing it could cause a SIGSEGV.
+  unw_word_t pc = static_cast(this->getReg(UNW_REG_IP));
+  if (!isReadableAddr(pc))
+return false;
+  // If near page boundary, check the next page too.
+  if (((pc + code_size - 1) & 4095) != (pc & 4095) &&
+  !isReadableAddr(pc + code_size - 1))
+return false;
+
+  const uint8_t *pc_ptr = reinterpret_cast(pc);
+  if (memcmp(pc_ptr, amd64_linux_sigtramp_code, code_size))
+return false;
+
+  _info = {};
+  _info.start_ip = pc;
+  _info.end_ip = pc + code_size;
+  _isSigReturn = true;
+  return true;
+}
+
+template 
+int UnwindCursor::stepThroughSigReturn(Registers_x86_64 &) {
+  // In the signal trampoline frame, sp points to ucontext:
+  //   struct ucontext {
+  // unsigned long uc_flags;
+  // struct ucontext  *uc_link;
+  // stack_t   uc_stack; // 24 bytes
+  // struct sigcontext uc_mcontext;
+  // ...
+  //   };
+  const pint_t kOffsetSpToSigcontext = (8 + 8 + 24);
+  pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
+
+  // UNW_X86_64_* -> field in struct sigcontext_64.
+  //   struct sigcontext_64 {
+  // __u64 r8;  // 0
+  // __u64 r9;  // 1
+  // __u64 r10; // 2
+  // __u64 r11; // 3
+  // __u64 r12; // 4
+  // __u64 r13; // 5
+  // __u64 r14; // 6
+  // __u64 r15; // 7
+  // __u64 di;  // 8
+  // __u64 si;  // 9
+  // __u64 bp;  // 10
+  // __u64 bx;  // 11
+  // __u64 dx;  // 12
+  // __u64 ax;  // 13
+  // __u64 cx;  // 14
+  // __u64 sp;  // 15
+  // __u64 ip;  // 16
+  // ...
+  //   };
+  const size_t idx_map[17] = {13, 12, 14, 11, 9, 8, 10, 15, 0,
+  1, 2, 3, 4, 5, 6, 7, 16};
+
+  for (int i = 0; i < 17; ++i) {
+uint64_t value = _addressSpace.get64(sigctx + idx_map[i] * 8);
+_registers.setRegister(i, value);
+  }
+
+  // The +1 story is the same as in DwarfInstructions::stepWithDwarf()
+  // (search for "returnAddress + cieInfo.isSignalFrame" or "Return address
+  // points to the next instruction"). This is probably not the right place for
+  // this because this function is not necessarily used with DWARF. Need to
+  // research whether the other unwind methods have the same +-1 situation or
+  // are off by one.
+  _registers.setIP(_registers.getIP() + 1);
+
+  _isSignalFrame = true;
+  return UNW_STE

[libunwind] [libunwind][X86-64] Handle Linux sigreturn trampoline when DWARF info is missing (PR #103473)

2025-02-22 Thread Konstantin Bogdanov via cfe-commits

https://github.com/thevar1able updated 
https://github.com/llvm/llvm-project/pull/103473

>From 3beb6fc40ccc0b5726b360156063ef35e2b1db3f Mon Sep 17 00:00:00 2001
From: Michael Kolupaev 
Date: Tue, 13 Aug 2024 21:16:08 +
Subject: [PATCH 1/2] [libunwind][X86-64] Handle Linux sigreturn trampoline
 when DWARF info is missing

---
 libunwind/src/UnwindCursor.hpp | 100 -
 1 file changed, 99 insertions(+), 1 deletion(-)

diff --git a/libunwind/src/UnwindCursor.hpp b/libunwind/src/UnwindCursor.hpp
index ca9927edc9990..b4ddcd2792bf2 100644
--- a/libunwind/src/UnwindCursor.hpp
+++ b/libunwind/src/UnwindCursor.hpp
@@ -33,7 +33,8 @@
 #if defined(_LIBUNWIND_TARGET_LINUX) &&
\
 (defined(_LIBUNWIND_TARGET_AARCH64) || 
\
  defined(_LIBUNWIND_TARGET_LOONGARCH) ||   
\
- defined(_LIBUNWIND_TARGET_RISCV) || defined(_LIBUNWIND_TARGET_S390X))
+ defined(_LIBUNWIND_TARGET_RISCV) || defined(_LIBUNWIND_TARGET_S390X) ||   
\
+ defined(_LIBUNWIND_TARGET_S390X) || defined(_LIBUNWIND_TARGET_X86_64))
 #include 
 #include 
 #include 
@@ -1008,6 +1009,10 @@ class UnwindCursor : public AbstractUnwindCursor{
 #if defined(_LIBUNWIND_TARGET_S390X)
   bool setInfoForSigReturn(Registers_s390x &);
   int stepThroughSigReturn(Registers_s390x &);
+#endif
+#if defined(_LIBUNWIND_TARGET_X86_64)
+  bool setInfoForSigReturn(Registers_x86_64 &);
+  int stepThroughSigReturn(Registers_x86_64 &);
 #endif
   template  bool setInfoForSigReturn(Registers &) {
 return false;
@@ -3032,6 +3037,99 @@ int UnwindCursor::stepThroughSigReturn(Registers_s390x &) {
 #endif // defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&
// defined(_LIBUNWIND_TARGET_S390X)
 
+#if defined(_LIBUNWIND_CHECK_LINUX_SIGRETURN) &&   
\
+defined(_LIBUNWIND_TARGET_X86_64)
+template 
+bool UnwindCursor::setInfoForSigReturn(Registers_x86_64 &) {
+  // Look for the sigreturn trampoline. The trampoline's body is two
+  // specific instructions (see below). Typically the trampoline comes from the
+  // vDSO or from libc.
+  //
+  // This special code path is a fallback that is only used if the trampoline
+  // lacks proper (e.g. DWARF) unwind info.
+  const uint8_t amd64_linux_sigtramp_code[9] = {
+  0x48, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00, // mov rax, 15
+  0x0f, 0x05// syscall
+  };
+  const size_t code_size = sizeof(amd64_linux_sigtramp_code);
+
+  // The PC might contain an invalid address if the unwind info is bad, so
+  // directly accessing it could cause a SIGSEGV.
+  unw_word_t pc = static_cast(this->getReg(UNW_REG_IP));
+  if (!isReadableAddr(pc))
+return false;
+  // If near page boundary, check the next page too.
+  if (((pc + code_size - 1) & 4095) != (pc & 4095) &&
+  !isReadableAddr(pc + code_size - 1))
+return false;
+
+  const uint8_t *pc_ptr = reinterpret_cast(pc);
+  if (memcmp(pc_ptr, amd64_linux_sigtramp_code, code_size))
+return false;
+
+  _info = {};
+  _info.start_ip = pc;
+  _info.end_ip = pc + code_size;
+  _isSigReturn = true;
+  return true;
+}
+
+template 
+int UnwindCursor::stepThroughSigReturn(Registers_x86_64 &) {
+  // In the signal trampoline frame, sp points to ucontext:
+  //   struct ucontext {
+  // unsigned long uc_flags;
+  // struct ucontext  *uc_link;
+  // stack_t   uc_stack; // 24 bytes
+  // struct sigcontext uc_mcontext;
+  // ...
+  //   };
+  const pint_t kOffsetSpToSigcontext = (8 + 8 + 24);
+  pint_t sigctx = _registers.getSP() + kOffsetSpToSigcontext;
+
+  // UNW_X86_64_* -> field in struct sigcontext_64.
+  //   struct sigcontext_64 {
+  // __u64 r8;  // 0
+  // __u64 r9;  // 1
+  // __u64 r10; // 2
+  // __u64 r11; // 3
+  // __u64 r12; // 4
+  // __u64 r13; // 5
+  // __u64 r14; // 6
+  // __u64 r15; // 7
+  // __u64 di;  // 8
+  // __u64 si;  // 9
+  // __u64 bp;  // 10
+  // __u64 bx;  // 11
+  // __u64 dx;  // 12
+  // __u64 ax;  // 13
+  // __u64 cx;  // 14
+  // __u64 sp;  // 15
+  // __u64 ip;  // 16
+  // ...
+  //   };
+  const size_t idx_map[17] = {13, 12, 14, 11, 9, 8, 10, 15, 0,
+  1, 2, 3, 4, 5, 6, 7, 16};
+
+  for (int i = 0; i < 17; ++i) {
+uint64_t value = _addressSpace.get64(sigctx + idx_map[i] * 8);
+_registers.setRegister(i, value);
+  }
+
+  // The +1 story is the same as in DwarfInstructions::stepWithDwarf()
+  // (search for "returnAddress + cieInfo.isSignalFrame" or "Return address
+  // points to the next instruction"). This is probably not the right place for
+  // this because this function is not necessarily used with DWARF. Need to
+  // research whether the other unwind methods have the same +-1 situation or
+  // are off by one.
+  _registers.setIP(_registers.getIP() + 1);
+
+  _isSignalFrame = true;
+  return UNW_STE

[libunwind] [libunwind][X86-64] Handle Linux sigreturn trampoline when DWARF info is missing (PR #103473)

2025-02-22 Thread Konstantin Bogdanov via cfe-commits

thevar1able wrote:

Ping

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