-
Notifications
You must be signed in to change notification settings - Fork 883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alias settings_client internals to see it in a memory dump. #26654
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_CHROMIUM_SRC_BASE_DEBUG_ALIAS_H_ | ||
#define BRAVE_CHROMIUM_SRC_BASE_DEBUG_ALIAS_H_ | ||
|
||
#include "base/containers/span.h" | ||
#include "base/memory/raw_ptr_exclusion.h" | ||
#include "base/memory/stack_allocated.h" | ||
#include "base/ranges/algorithm.h" | ||
|
||
#include "src/base/debug/alias.h" // IWYU pragma: export | ||
|
||
namespace base::debug { | ||
|
||
// StackObjectCopy creates a byte-for-byte copy of an object on the stack, | ||
// allowing the object to be inspected in crash dumps even if the original | ||
// object is optimized away by the compiler or is stored in protected memory. | ||
// The copy maintains proper alignment and can be viewed as the original type in | ||
// a debugger. | ||
template <typename T> | ||
class StackObjectCopy { | ||
STACK_ALLOCATED(); | ||
|
||
public: | ||
explicit StackObjectCopy(const T* original) { | ||
if (original) { | ||
// SAFETY: `original` is a valid pointer to a T object, and `kSize` is the | ||
// size of T. | ||
// | ||
// Can't use `byte_span_from_ref` because `T` might be an abstract class. | ||
auto original_object_memory = UNSAFE_BUFFERS( | ||
make_span(reinterpret_cast<const uint8_t*>(original), kSize)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reported by reviewdog 🐶 |
||
span(buffer_).copy_from(std::move(original_object_memory)); | ||
} else { | ||
ranges::fill(buffer_, 0); | ||
} | ||
} | ||
|
||
~StackObjectCopy() { | ||
// Ensure the class isn't optimized away before destruction. | ||
Alias(buffer_); | ||
} | ||
|
||
StackObjectCopy(const StackObjectCopy&) = delete; | ||
StackObjectCopy& operator=(const StackObjectCopy&) = delete; | ||
|
||
private: | ||
constexpr static size_t kSize = sizeof(T); | ||
constexpr static size_t kMinSize = 128; | ||
|
||
alignas(T) uint8_t buffer_[kSize]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would that be enough to extract and store just vtable pointer? |
||
RAW_PTR_EXCLUSION const T* const typed_view_ = | ||
reinterpret_cast<const T*>(buffer_); | ||
// Prevent the class from being optimized down to a register value. | ||
const uint8_t padding_[kSize >= kMinSize ? 0 : kMinSize - kSize] = {}; | ||
}; | ||
|
||
} // namespace base::debug | ||
|
||
// DEBUG_ALIAS_FOR_OBJECT creates a stack copy of an object and ensures it | ||
// remains in memory for crash dumps. This is useful when you need to ensure an | ||
// object's state is captured in crash reports, especially when the object might | ||
// otherwise be optimized away or is not directly accessible. | ||
// | ||
// Usage: DEBUG_ALIAS_FOR_OBJECT(alias_name, pointer_to_object); | ||
#define DEBUG_ALIAS_FOR_OBJECT(var_name, object) \ | ||
const ::base::debug::StackObjectCopy var_name(object); \ | ||
::base::debug::Alias(&var_name) | ||
|
||
#endif // BRAVE_CHROMIUM_SRC_BASE_DEBUG_ALIAS_H_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reported by reviewdog 🐶
[semgrep] Potentially unsafe C++ construct detected
Source: https://github.com/brave/security-action/blob/main/assets/semgrep_rules/client/unsafe-cpp-constructs.yaml
Cc @stoletheminerals @thypon @cdesouza-chromium
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need
make_span
there. It can only bespan
I suspect.