[Lldb-commits] [lldb] [lldb] Fix SIGSEGV in `GetPtraceScope()` in `Procfs.cpp` (PR #142224)
via lldb-commits
lldb-commits at lists.llvm.org
Fri May 30 15:55:57 PDT 2025
https://github.com/royitaqi updated https://github.com/llvm/llvm-project/pull/142224
>From 58b8a322a8cc6e3cce3d3e2d405fb915c338791a Mon Sep 17 00:00:00 2001
From: Roy Shi <royshi at meta.com>
Date: Fri, 30 May 2025 14:21:06 -0700
Subject: [PATCH 1/4] Fix crash and add test
---
lldb/source/Plugins/Process/Linux/Procfs.cpp | 2 +-
lldb/unittests/Process/Linux/ProcfsTests.cpp | 14 +++++++++++++-
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/lldb/source/Plugins/Process/Linux/Procfs.cpp b/lldb/source/Plugins/Process/Linux/Procfs.cpp
index d3bd396fbaeab..4349a2b1adb9d 100644
--- a/lldb/source/Plugins/Process/Linux/Procfs.cpp
+++ b/lldb/source/Plugins/Process/Linux/Procfs.cpp
@@ -74,7 +74,7 @@ lldb_private::process_linux::GetAvailableLogicalCoreIDs() {
llvm::Expected<int> lldb_private::process_linux::GetPtraceScope() {
ErrorOr<std::unique_ptr<MemoryBuffer>> ptrace_scope_file =
getProcFile("sys/kernel/yama/ptrace_scope");
- if (!*ptrace_scope_file)
+ if (!ptrace_scope_file)
return errorCodeToError(ptrace_scope_file.getError());
// The contents should be something like "1\n". Trim it so we get "1".
StringRef buffer = (*ptrace_scope_file)->getBuffer().trim();
diff --git a/lldb/unittests/Process/Linux/ProcfsTests.cpp b/lldb/unittests/Process/Linux/ProcfsTests.cpp
index e7af1f469c2bf..cbddccb89a215 100644
--- a/lldb/unittests/Process/Linux/ProcfsTests.cpp
+++ b/lldb/unittests/Process/Linux/ProcfsTests.cpp
@@ -104,7 +104,7 @@ TEST(Perf, RealLogicalCoreIDs) {
ASSERT_GT((int)cpu_ids->size(), 0) << "We must see at least one core";
}
-TEST(Perf, RealPtraceScope) {
+TEST(Perf, RealPtraceScopeWhenExist) {
// We first check we can read /proc/sys/kernel/yama/ptrace_scope
auto buffer_or_error =
errorOrToExpected(getProcFile("sys/kernel/yama/ptrace_scope"));
@@ -120,6 +120,18 @@ TEST(Perf, RealPtraceScope) {
<< "Sensible values of ptrace_scope are between 0 and 3";
}
+TEST(Perf, RealPtraceScopeWhenNotExist) {
+ // We first check we can NOT read /proc/sys/kernel/yama/ptrace_scope
+ auto buffer_or_error =
+ errorOrToExpected(getProcFile("sys/kernel/yama/ptrace_scope"));
+ if (buffer_or_error)
+ GTEST_SKIP() << "In order for this test to run, sys/kernel/yama/ptrace_scope should not exist";
+
+ // At this point we should fail parsing the ptrace_scope value.
+ Expected<int> ptrace_scope = GetPtraceScope();
+ ASSERT_FALSE((bool)ptrace_scope);
+}
+
#ifdef LLVM_ENABLE_THREADING
TEST(Support, getProcFile_Tid) {
auto BufferOrError = getProcFile(getpid(), llvm::get_threadid(), "comm");
>From 02a2ba2d67105b1fadec0a9e0c0ee7d914a24e72 Mon Sep 17 00:00:00 2001
From: Roy Shi <royshi at meta.com>
Date: Fri, 30 May 2025 14:39:46 -0700
Subject: [PATCH 2/4] Fix test
---
lldb/unittests/Process/Linux/ProcfsTests.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/lldb/unittests/Process/Linux/ProcfsTests.cpp b/lldb/unittests/Process/Linux/ProcfsTests.cpp
index cbddccb89a215..a795fa4e019c5 100644
--- a/lldb/unittests/Process/Linux/ProcfsTests.cpp
+++ b/lldb/unittests/Process/Linux/ProcfsTests.cpp
@@ -125,11 +125,13 @@ TEST(Perf, RealPtraceScopeWhenNotExist) {
auto buffer_or_error =
errorOrToExpected(getProcFile("sys/kernel/yama/ptrace_scope"));
if (buffer_or_error)
- GTEST_SKIP() << "In order for this test to run, sys/kernel/yama/ptrace_scope should not exist";
+ GTEST_SKIP() << "In order for this test to run, /proc/sys/kernel/yama/ptrace_scope should not exist";
+ consumeError(buffer_or_error.takeError());
// At this point we should fail parsing the ptrace_scope value.
Expected<int> ptrace_scope = GetPtraceScope();
ASSERT_FALSE((bool)ptrace_scope);
+ consumeError(ptrace_scope.takeError());
}
#ifdef LLVM_ENABLE_THREADING
>From 02e9d8b60dcd8797e29874b64fe1369bfb7821b6 Mon Sep 17 00:00:00 2001
From: Roy Shi <royshi at meta.com>
Date: Fri, 30 May 2025 15:55:06 -0700
Subject: [PATCH 3/4] Add if for unique_ptr
---
lldb/source/Plugins/Process/Linux/Procfs.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/lldb/source/Plugins/Process/Linux/Procfs.cpp b/lldb/source/Plugins/Process/Linux/Procfs.cpp
index 4349a2b1adb9d..7ef4f3a102c2c 100644
--- a/lldb/source/Plugins/Process/Linux/Procfs.cpp
+++ b/lldb/source/Plugins/Process/Linux/Procfs.cpp
@@ -76,6 +76,9 @@ llvm::Expected<int> lldb_private::process_linux::GetPtraceScope() {
getProcFile("sys/kernel/yama/ptrace_scope");
if (!ptrace_scope_file)
return errorCodeToError(ptrace_scope_file.getError());
+ if (!*ptrace_scope_file)
+ return createStringError(inconvertibleErrorCode(),
+ "ptrace_scope buffer is nullptr");
// The contents should be something like "1\n". Trim it so we get "1".
StringRef buffer = (*ptrace_scope_file)->getBuffer().trim();
int ptrace_scope_value;
>From e55bbbed66bb1b49bc23a7d24d70055865424ba5 Mon Sep 17 00:00:00 2001
From: Roy Shi <royshi at meta.com>
Date: Fri, 30 May 2025 15:55:46 -0700
Subject: [PATCH 4/4] Fix format
---
lldb/source/Plugins/Process/Linux/Procfs.cpp | 2 +-
lldb/unittests/Process/Linux/ProcfsTests.cpp | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/lldb/source/Plugins/Process/Linux/Procfs.cpp b/lldb/source/Plugins/Process/Linux/Procfs.cpp
index 7ef4f3a102c2c..b7a61bf810fff 100644
--- a/lldb/source/Plugins/Process/Linux/Procfs.cpp
+++ b/lldb/source/Plugins/Process/Linux/Procfs.cpp
@@ -78,7 +78,7 @@ llvm::Expected<int> lldb_private::process_linux::GetPtraceScope() {
return errorCodeToError(ptrace_scope_file.getError());
if (!*ptrace_scope_file)
return createStringError(inconvertibleErrorCode(),
- "ptrace_scope buffer is nullptr");
+ "ptrace_scope buffer is nullptr");
// The contents should be something like "1\n". Trim it so we get "1".
StringRef buffer = (*ptrace_scope_file)->getBuffer().trim();
int ptrace_scope_value;
diff --git a/lldb/unittests/Process/Linux/ProcfsTests.cpp b/lldb/unittests/Process/Linux/ProcfsTests.cpp
index a795fa4e019c5..534e3e132ec62 100644
--- a/lldb/unittests/Process/Linux/ProcfsTests.cpp
+++ b/lldb/unittests/Process/Linux/ProcfsTests.cpp
@@ -125,7 +125,8 @@ TEST(Perf, RealPtraceScopeWhenNotExist) {
auto buffer_or_error =
errorOrToExpected(getProcFile("sys/kernel/yama/ptrace_scope"));
if (buffer_or_error)
- GTEST_SKIP() << "In order for this test to run, /proc/sys/kernel/yama/ptrace_scope should not exist";
+ GTEST_SKIP() << "In order for this test to run, "
+ "/proc/sys/kernel/yama/ptrace_scope should not exist";
consumeError(buffer_or_error.takeError());
// At this point we should fail parsing the ptrace_scope value.
More information about the lldb-commits
mailing list