aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2023-04-13 13:51:52 +0300
committerbabenko <babenko@yandex-team.com>2023-04-13 13:51:52 +0300
commitc8b713637bb0419113b508b14594e36bda82cf0b (patch)
tree20a8af056ed16c67a1107cd0184a1931ce04b163 /library/cpp
parent62975dbd72f660e1a0e0583d4b1ebf1fdb4fc924 (diff)
downloadydb-c8b713637bb0419113b508b14594e36bda82cf0b.tar.gz
Refactor backtrace cursors
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/memory/CMakeLists.darwin-x86_64.txt1
-rw-r--r--library/cpp/yt/memory/CMakeLists.linux-aarch64.txt1
-rw-r--r--library/cpp/yt/memory/CMakeLists.linux-x86_64.txt1
-rw-r--r--library/cpp/yt/memory/CMakeLists.windows-x86_64.txt1
-rw-r--r--library/cpp/yt/memory/safe_memory_reader-inl.h19
-rw-r--r--library/cpp/yt/memory/safe_memory_reader.cpp45
-rw-r--r--library/cpp/yt/memory/safe_memory_reader.h38
-rw-r--r--library/cpp/yt/memory/unittests/safe_memory_reader_ut.cpp27
8 files changed, 133 insertions, 0 deletions
diff --git a/library/cpp/yt/memory/CMakeLists.darwin-x86_64.txt b/library/cpp/yt/memory/CMakeLists.darwin-x86_64.txt
index 30fdfbef100..fff3bb860b4 100644
--- a/library/cpp/yt/memory/CMakeLists.darwin-x86_64.txt
+++ b/library/cpp/yt/memory/CMakeLists.darwin-x86_64.txt
@@ -27,5 +27,6 @@ target_sources(cpp-yt-memory PRIVATE
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/chunked_output_stream.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref_tracked.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/safe_memory_reader.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/shared_range.cpp
)
diff --git a/library/cpp/yt/memory/CMakeLists.linux-aarch64.txt b/library/cpp/yt/memory/CMakeLists.linux-aarch64.txt
index 7325ab2c740..899e0b4d0dd 100644
--- a/library/cpp/yt/memory/CMakeLists.linux-aarch64.txt
+++ b/library/cpp/yt/memory/CMakeLists.linux-aarch64.txt
@@ -28,5 +28,6 @@ target_sources(cpp-yt-memory PRIVATE
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/chunked_output_stream.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref_tracked.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/safe_memory_reader.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/shared_range.cpp
)
diff --git a/library/cpp/yt/memory/CMakeLists.linux-x86_64.txt b/library/cpp/yt/memory/CMakeLists.linux-x86_64.txt
index 7325ab2c740..899e0b4d0dd 100644
--- a/library/cpp/yt/memory/CMakeLists.linux-x86_64.txt
+++ b/library/cpp/yt/memory/CMakeLists.linux-x86_64.txt
@@ -28,5 +28,6 @@ target_sources(cpp-yt-memory PRIVATE
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/chunked_output_stream.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref_tracked.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/safe_memory_reader.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/shared_range.cpp
)
diff --git a/library/cpp/yt/memory/CMakeLists.windows-x86_64.txt b/library/cpp/yt/memory/CMakeLists.windows-x86_64.txt
index da62073cb8c..c0ecdaaeb89 100644
--- a/library/cpp/yt/memory/CMakeLists.windows-x86_64.txt
+++ b/library/cpp/yt/memory/CMakeLists.windows-x86_64.txt
@@ -24,5 +24,6 @@ target_sources(cpp-yt-memory PRIVATE
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/chunked_output_stream.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/ref_tracked.cpp
+ ${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/safe_memory_reader.cpp
${CMAKE_SOURCE_DIR}/library/cpp/yt/memory/shared_range.cpp
)
diff --git a/library/cpp/yt/memory/safe_memory_reader-inl.h b/library/cpp/yt/memory/safe_memory_reader-inl.h
new file mode 100644
index 00000000000..69d8c1c9184
--- /dev/null
+++ b/library/cpp/yt/memory/safe_memory_reader-inl.h
@@ -0,0 +1,19 @@
+#ifndef SAFE_MEMORY_READER_INL_H_
+#error "Direct inclusion of this file is not allowed, include safe_memory_reader.h"
+// For the sake of sane code completion.
+#include "safe_memory_reader.h"
+#endif
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+template <class T>
+bool TSafeMemoryReader::Read(const void* addr, T* value)
+{
+ return ReadRaw(addr, value, sizeof(*value));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT
diff --git a/library/cpp/yt/memory/safe_memory_reader.cpp b/library/cpp/yt/memory/safe_memory_reader.cpp
new file mode 100644
index 00000000000..49d88b3ba1b
--- /dev/null
+++ b/library/cpp/yt/memory/safe_memory_reader.cpp
@@ -0,0 +1,45 @@
+#include "safe_memory_reader.h"
+
+#ifdef _linux_
+#include <fcntl.h>
+#include <unistd.h>
+#endif
+
+#include <library/cpp/yt/assert/assert.h>
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+TSafeMemoryReader::TSafeMemoryReader()
+{
+#ifdef _linux_
+ FD_ = ::open("/proc/self/mem", O_RDONLY);
+ YT_VERIFY(FD_ >= 0);
+#endif
+}
+
+TSafeMemoryReader::~TSafeMemoryReader()
+{
+#ifdef _linux_
+ ::close(FD_);
+#endif
+}
+
+bool TSafeMemoryReader::ReadRaw(const void* addr, void* ptr, size_t size)
+{
+#ifdef _linux_
+ int ret;
+ do {
+ ret = ::pread64(FD_, ptr, size, reinterpret_cast<uintptr_t>(addr));
+ } while (ret < 0 && errno == EINTR);
+ return ret == static_cast<int>(size);
+#else
+ Y_UNUSED(FD_, addr, ptr, size);
+ return false;
+#endif
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT
diff --git a/library/cpp/yt/memory/safe_memory_reader.h b/library/cpp/yt/memory/safe_memory_reader.h
new file mode 100644
index 00000000000..0ea87d32903
--- /dev/null
+++ b/library/cpp/yt/memory/safe_memory_reader.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include "public.h"
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+//! Enables safe read-only access to the address space of the current process.
+/*!
+ * Inaccessible memory locations will not cause any traps but rather report
+ * failure via return value.
+ */
+class TSafeMemoryReader
+{
+public:
+ TSafeMemoryReader();
+ TSafeMemoryReader(const TSafeMemoryReader&) = delete;
+ ~TSafeMemoryReader();
+
+ //! Attempts to read #value at address #addr.
+ //! Returns |true| on success, |false| on failure.
+ template <class T>
+ bool Read(const void* addr, T* value);
+
+private:
+ int FD_ = -1;
+
+ bool ReadRaw(const void* addr, void* ptr, size_t size);
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT
+
+#define SAFE_MEMORY_READER_INL_H_
+#include "safe_memory_reader-inl.h"
+#undef SAFE_MEMORY_READER_INL_H_
diff --git a/library/cpp/yt/memory/unittests/safe_memory_reader_ut.cpp b/library/cpp/yt/memory/unittests/safe_memory_reader_ut.cpp
new file mode 100644
index 00000000000..9873dd80bea
--- /dev/null
+++ b/library/cpp/yt/memory/unittests/safe_memory_reader_ut.cpp
@@ -0,0 +1,27 @@
+
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/yt/memory/safe_memory_reader.h>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(TSafeMemoryReaderTest, Simple)
+{
+ TSafeMemoryReader reader;
+
+ int i = 1;
+
+ int value;
+ ASSERT_TRUE(reader.Read(&i, &value));
+ ASSERT_EQ(value, 1);
+
+ ASSERT_FALSE(reader.Read(reinterpret_cast<void*>(0x1ee3beef), &value));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT