2022-09-06 19:14:07 -04:00
|
|
|
|
// Copyright 2017 The Crashpad Authors
|
2017-04-03 14:09:58 -07:00
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2017-09-26 12:52:41 -07:00
|
|
|
|
#include "util/process/process_memory.h"
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
#include "base/containers/heap_array.h"
|
2021-06-28 09:35:07 -04:00
|
|
|
|
#include "base/memory/page_size.h"
|
2018-12-20 14:42:17 -08:00
|
|
|
|
#include "build/build_config.h"
|
2017-04-03 14:09:58 -07:00
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include "test/errors.h"
|
|
|
|
|
#include "test/multiprocess.h"
|
2018-01-30 10:12:47 -08:00
|
|
|
|
#include "test/multiprocess_exec.h"
|
|
|
|
|
#include "test/process_type.h"
|
2018-11-05 13:14:20 -08:00
|
|
|
|
#include "test/scoped_guarded_page.h"
|
2017-04-03 14:09:58 -07:00
|
|
|
|
#include "util/file/file_io.h"
|
2017-04-27 15:08:17 -04:00
|
|
|
|
#include "util/misc/from_pointer_cast.h"
|
2018-01-30 10:12:47 -08:00
|
|
|
|
#include "util/process/process_memory_native.h"
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
#include "test/mac/mach_multiprocess.h"
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
|
2021-08-10 14:55:25 -07:00
|
|
|
|
#include "test/linux/fake_ptrace_connection.h"
|
|
|
|
|
#include "util/linux/direct_ptrace_connection.h"
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) ||
|
|
|
|
|
// BUILDFLAG(IS_CHROMEOS)
|
2021-08-10 14:55:25 -07:00
|
|
|
|
|
2017-04-03 14:09:58 -07:00
|
|
|
|
namespace crashpad {
|
|
|
|
|
namespace test {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2018-12-20 11:12:51 -08:00
|
|
|
|
// On macOS the ProcessMemoryTests require accessing the child process' task
|
|
|
|
|
// port which requires root or a code signing entitlement. To account for this
|
|
|
|
|
// we implement an adaptor class that wraps MachMultiprocess on macOS, because
|
|
|
|
|
// it shares the child's task port, and makes it behave like MultiprocessExec.
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_APPLE)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
class MultiprocessAdaptor : public MachMultiprocess {
|
|
|
|
|
public:
|
|
|
|
|
void SetChildTestMainFunction(const std::string& function_name) {
|
|
|
|
|
test_function_ = function_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProcessType ChildProcess() { return ChildTask(); }
|
|
|
|
|
|
|
|
|
|
// Helpers to get I/O handles in the child process
|
|
|
|
|
static FileHandle OutputHandle() {
|
|
|
|
|
CHECK_NE(write_pipe_handle_, -1);
|
|
|
|
|
return write_pipe_handle_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static FileHandle InputHandle() {
|
|
|
|
|
CHECK_NE(read_pipe_handle_, -1);
|
|
|
|
|
return read_pipe_handle_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual void Parent() = 0;
|
|
|
|
|
|
|
|
|
|
void MachMultiprocessParent() override { Parent(); }
|
|
|
|
|
|
|
|
|
|
void MachMultiprocessChild() override {
|
|
|
|
|
read_pipe_handle_ = ReadPipeHandle();
|
|
|
|
|
write_pipe_handle_ = WritePipeHandle();
|
|
|
|
|
internal::CheckedInvokeMultiprocessChild(test_function_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string test_function_;
|
|
|
|
|
|
|
|
|
|
static FileHandle read_pipe_handle_;
|
|
|
|
|
static FileHandle write_pipe_handle_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FileHandle MultiprocessAdaptor::read_pipe_handle_ = -1;
|
|
|
|
|
FileHandle MultiprocessAdaptor::write_pipe_handle_ = -1;
|
|
|
|
|
#else
|
|
|
|
|
class MultiprocessAdaptor : public MultiprocessExec {
|
|
|
|
|
public:
|
|
|
|
|
static FileHandle OutputHandle() {
|
|
|
|
|
return StdioFileHandle(StdioStream::kStandardOutput);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static FileHandle InputHandle() {
|
|
|
|
|
return StdioFileHandle(StdioStream::kStandardInput);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual void Parent() = 0;
|
|
|
|
|
|
|
|
|
|
void MultiprocessParent() override { Parent(); }
|
|
|
|
|
};
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_APPLE)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
base::HeapArray<char> DoChildReadTestSetup() {
|
|
|
|
|
auto region = base::HeapArray<char>::Uninit(4 * base::GetPageSize());
|
|
|
|
|
for (size_t index = 0; index < region.size(); ++index) {
|
|
|
|
|
region[index] = static_cast<char>(index % 256);
|
2018-01-30 10:12:47 -08:00
|
|
|
|
}
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
return region;
|
2018-01-30 10:12:47 -08:00
|
|
|
|
}
|
2018-01-16 14:46:29 -08:00
|
|
|
|
|
2018-01-30 10:12:47 -08:00
|
|
|
|
CRASHPAD_CHILD_TEST_MAIN(ReadTestChild) {
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
auto region = DoChildReadTestSetup();
|
|
|
|
|
auto region_size = region.size();
|
2018-12-20 11:12:51 -08:00
|
|
|
|
FileHandle out = MultiprocessAdaptor::OutputHandle();
|
2018-01-30 10:12:47 -08:00
|
|
|
|
CheckedWriteFile(out, ®ion_size, sizeof(region_size));
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
VMAddress address = FromPointerCast<VMAddress>(region.data());
|
2018-01-30 10:12:47 -08:00
|
|
|
|
CheckedWriteFile(out, &address, sizeof(address));
|
2018-12-20 11:12:51 -08:00
|
|
|
|
CheckedReadFileAtEOF(MultiprocessAdaptor::InputHandle());
|
2018-01-30 10:12:47 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-20 11:12:51 -08:00
|
|
|
|
class ReadTest : public MultiprocessAdaptor {
|
2017-04-03 14:09:58 -07:00
|
|
|
|
public:
|
2018-12-20 11:12:51 -08:00
|
|
|
|
ReadTest() : MultiprocessAdaptor() {
|
2018-01-30 10:12:47 -08:00
|
|
|
|
SetChildTestMainFunction("ReadTestChild");
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2021-09-20 12:55:12 -07:00
|
|
|
|
ReadTest(const ReadTest&) = delete;
|
|
|
|
|
ReadTest& operator=(const ReadTest&) = delete;
|
|
|
|
|
|
2018-01-30 10:12:47 -08:00
|
|
|
|
void RunAgainstSelf() {
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
auto region = DoChildReadTestSetup();
|
2018-01-30 10:12:47 -08:00
|
|
|
|
DoTest(GetSelfProcess(),
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
region.size(),
|
|
|
|
|
FromPointerCast<VMAddress>(region.data()));
|
2018-01-30 10:12:47 -08:00
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-01-30 10:12:47 -08:00
|
|
|
|
void RunAgainstChild() { Run(); }
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
private:
|
2018-12-20 11:12:51 -08:00
|
|
|
|
void Parent() override {
|
2018-01-30 10:12:47 -08:00
|
|
|
|
size_t region_size;
|
|
|
|
|
VMAddress region;
|
|
|
|
|
ASSERT_TRUE(
|
|
|
|
|
ReadFileExactly(ReadPipeHandle(), ®ion_size, sizeof(region_size)));
|
|
|
|
|
ASSERT_TRUE(ReadFileExactly(ReadPipeHandle(), ®ion, sizeof(region)));
|
|
|
|
|
DoTest(ChildProcess(), region_size, region);
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:12:47 -08:00
|
|
|
|
void DoTest(ProcessType process, size_t region_size, VMAddress address) {
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
|
2021-08-10 14:55:25 -07:00
|
|
|
|
FakePtraceConnection connection;
|
|
|
|
|
ASSERT_TRUE(connection.Initialize(process));
|
|
|
|
|
ProcessMemoryLinux memory(&connection);
|
|
|
|
|
#else
|
2018-01-30 10:12:47 -08:00
|
|
|
|
ProcessMemoryNative memory;
|
|
|
|
|
ASSERT_TRUE(memory.Initialize(process));
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) ||
|
|
|
|
|
// BUILDFLAG(IS_CHROMEOS)
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
auto result = base::HeapArray<char>::Uninit(region_size);
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
// Ensure that the entire region can be read.
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
ASSERT_TRUE(memory.Read(address, result.size(), result.data()));
|
|
|
|
|
for (size_t i = 0; i < result.size(); ++i) {
|
2018-01-30 10:12:47 -08:00
|
|
|
|
EXPECT_EQ(result[i], static_cast<char>(i % 256));
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
// Ensure that a read of length 0 succeeds and doesn’t touch the result.
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
memset(result.data(), '\0', result.size());
|
|
|
|
|
ASSERT_TRUE(memory.Read(address, 0, result.data()));
|
|
|
|
|
for (size_t i = 0; i < result.size(); ++i) {
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_EQ(result[i], 0);
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure that a read starting at an unaligned address works.
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
ASSERT_TRUE(memory.Read(address + 1, result.size() - 1, result.data()));
|
|
|
|
|
for (size_t i = 0; i < result.size() - 1; ++i) {
|
2018-01-30 10:12:47 -08:00
|
|
|
|
EXPECT_EQ(result[i], static_cast<char>((i + 1) % 256));
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
// Ensure that a read ending at an unaligned address works.
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
ASSERT_TRUE(memory.Read(address, result.size() - 1, result.data()));
|
|
|
|
|
for (size_t i = 0; i < result.size() - 1; ++i) {
|
2018-01-30 10:12:47 -08:00
|
|
|
|
EXPECT_EQ(result[i], static_cast<char>(i % 256));
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
// Ensure that a read starting and ending at unaligned addresses works.
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
ASSERT_TRUE(memory.Read(address + 1, result.size() - 2, result.data()));
|
|
|
|
|
for (size_t i = 0; i < result.size() - 2; ++i) {
|
2018-01-30 10:12:47 -08:00
|
|
|
|
EXPECT_EQ(result[i], static_cast<char>((i + 1) % 256));
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
// Ensure that a read of exactly one page works.
|
2018-11-05 13:14:20 -08:00
|
|
|
|
size_t page_size = base::GetPageSize();
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
ASSERT_GE(result.size(), page_size + page_size);
|
|
|
|
|
ASSERT_TRUE(memory.Read(address + page_size, page_size, result.data()));
|
2018-01-30 10:12:47 -08:00
|
|
|
|
for (size_t i = 0; i < page_size; ++i) {
|
|
|
|
|
EXPECT_EQ(result[i], static_cast<char>((i + page_size) % 256));
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
// Ensure that reading exactly a single byte works.
|
|
|
|
|
result[1] = 'J';
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
ASSERT_TRUE(memory.Read(address + 2, 1, result.data()));
|
2018-01-30 10:12:47 -08:00
|
|
|
|
EXPECT_EQ(result[0], 2);
|
test: Use (actual, [un]expected) in gtest {ASSERT,EXPECT}_{EQ,NE}
gtest used to require (expected, actual) ordering for arguments to
EXPECT_EQ and ASSERT_EQ, and in failed test assertions would identify
each side as “expected” or “actual.” Tests in Crashpad adhered to this
traditional ordering. After a gtest change in February 2016, it is now
agnostic with respect to the order of these arguments.
This change mechanically updates all uses of these macros to (actual,
expected) by reversing them. This provides consistency with our use of
the logging CHECK_EQ and DCHECK_EQ macros, and makes for better
readability by ordinary native speakers. The rough (but working!)
conversion tool is
https://chromium-review.googlesource.com/c/466727/1/rewrite_expectassert_eq.py,
and “git cl format” cleaned up its output.
EXPECT_NE and ASSERT_NE never had a preferred ordering. gtest never made
a judgment that one side or the other needed to provide an “unexpected”
value. Consequently, some code used (unexpected, actual) while other
code used (actual, unexpected). For consistency with the new EXPECT_EQ
and ASSERT_EQ usage, as well as consistency with CHECK_NE and DCHECK_NE,
this change also updates these use sites to (actual, unexpected) where
one side can be called “unexpected” as, for example, std::string::npos
can be. Unfortunately, this portion was a manual conversion.
References:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#binary-comparison
https://github.com/google/googletest/commit/77d6b173380332b1c1bc540532641f410ec82d65
https://github.com/google/googletest/pull/713
Change-Id: I978fef7c94183b8b1ef63f12f5ab4d6693626be3
Reviewed-on: https://chromium-review.googlesource.com/466727
Reviewed-by: Scott Graham <scottmg@chromium.org>
2017-04-04 00:35:21 -04:00
|
|
|
|
EXPECT_EQ(result[1], 'J');
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST(ProcessMemory, ReadSelf) {
|
|
|
|
|
ReadTest test;
|
|
|
|
|
test.RunAgainstSelf();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:12:47 -08:00
|
|
|
|
TEST(ProcessMemory, ReadChild) {
|
2017-04-03 14:09:58 -07:00
|
|
|
|
ReadTest test;
|
2018-01-30 10:12:47 -08:00
|
|
|
|
test.RunAgainstChild();
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
constexpr char kConstCharEmpty[] = "";
|
|
|
|
|
constexpr char kConstCharShort[] = "A short const char[]";
|
2018-01-30 10:12:47 -08:00
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
#define SHORT_LOCAL_STRING "A short local variable char[]"
|
2018-01-30 10:12:47 -08:00
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
std::string MakeLongString() {
|
|
|
|
|
std::string long_string;
|
2018-11-05 13:14:20 -08:00
|
|
|
|
const size_t kStringLongSize = 4 * base::GetPageSize();
|
2018-01-30 10:16:57 -08:00
|
|
|
|
for (size_t index = 0; index < kStringLongSize; ++index) {
|
|
|
|
|
long_string.push_back((index % 255) + 1);
|
|
|
|
|
}
|
|
|
|
|
EXPECT_EQ(long_string.size(), kStringLongSize);
|
|
|
|
|
return long_string;
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
void DoChildCStringReadTestSetup(const char** const_empty,
|
|
|
|
|
const char** const_short,
|
|
|
|
|
const char** local_empty,
|
|
|
|
|
const char** local_short,
|
|
|
|
|
std::string* long_string) {
|
|
|
|
|
*const_empty = kConstCharEmpty;
|
|
|
|
|
*const_short = kConstCharShort;
|
|
|
|
|
*local_empty = "";
|
|
|
|
|
*local_short = SHORT_LOCAL_STRING;
|
|
|
|
|
*long_string = MakeLongString();
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
CRASHPAD_CHILD_TEST_MAIN(ReadCStringTestChild) {
|
|
|
|
|
const char* const_empty;
|
|
|
|
|
const char* const_short;
|
|
|
|
|
const char* local_empty;
|
|
|
|
|
const char* local_short;
|
|
|
|
|
std::string long_string;
|
|
|
|
|
DoChildCStringReadTestSetup(
|
|
|
|
|
&const_empty, &const_short, &local_empty, &local_short, &long_string);
|
|
|
|
|
const auto write_address = [](const char* p) {
|
|
|
|
|
VMAddress address = FromPointerCast<VMAddress>(p);
|
2018-12-20 11:12:51 -08:00
|
|
|
|
CheckedWriteFile(
|
|
|
|
|
MultiprocessAdaptor::OutputHandle(), &address, sizeof(address));
|
2018-01-30 10:16:57 -08:00
|
|
|
|
};
|
|
|
|
|
write_address(const_empty);
|
|
|
|
|
write_address(const_short);
|
|
|
|
|
write_address(local_empty);
|
|
|
|
|
write_address(local_short);
|
|
|
|
|
write_address(long_string.c_str());
|
2018-12-20 11:12:51 -08:00
|
|
|
|
CheckedReadFileAtEOF(MultiprocessAdaptor::InputHandle());
|
2018-01-30 10:16:57 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-12-20 11:12:51 -08:00
|
|
|
|
class ReadCStringTest : public MultiprocessAdaptor {
|
2017-04-03 14:09:58 -07:00
|
|
|
|
public:
|
|
|
|
|
ReadCStringTest(bool limit_size)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
: MultiprocessAdaptor(), limit_size_(limit_size) {
|
2018-01-30 10:16:57 -08:00
|
|
|
|
SetChildTestMainFunction("ReadCStringTestChild");
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 12:55:12 -07:00
|
|
|
|
ReadCStringTest(const ReadCStringTest&) = delete;
|
|
|
|
|
ReadCStringTest& operator=(const ReadCStringTest&) = delete;
|
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
void RunAgainstSelf() {
|
|
|
|
|
const char* const_empty;
|
|
|
|
|
const char* const_short;
|
|
|
|
|
const char* local_empty;
|
|
|
|
|
const char* local_short;
|
|
|
|
|
std::string long_string;
|
|
|
|
|
DoChildCStringReadTestSetup(
|
|
|
|
|
&const_empty, &const_short, &local_empty, &local_short, &long_string);
|
|
|
|
|
DoTest(GetSelfProcess(),
|
|
|
|
|
FromPointerCast<VMAddress>(const_empty),
|
|
|
|
|
FromPointerCast<VMAddress>(const_short),
|
|
|
|
|
FromPointerCast<VMAddress>(local_empty),
|
|
|
|
|
FromPointerCast<VMAddress>(local_short),
|
|
|
|
|
FromPointerCast<VMAddress>(long_string.c_str()));
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
2018-01-30 10:16:57 -08:00
|
|
|
|
void RunAgainstChild() { Run(); }
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
private:
|
2018-12-20 11:12:51 -08:00
|
|
|
|
void Parent() override {
|
2018-01-30 10:16:57 -08:00
|
|
|
|
#define DECLARE_AND_READ_ADDRESS(name) \
|
|
|
|
|
VMAddress name; \
|
|
|
|
|
ASSERT_TRUE(ReadFileExactly(ReadPipeHandle(), &name, sizeof(name)));
|
|
|
|
|
DECLARE_AND_READ_ADDRESS(const_empty_address);
|
|
|
|
|
DECLARE_AND_READ_ADDRESS(const_short_address);
|
|
|
|
|
DECLARE_AND_READ_ADDRESS(local_empty_address);
|
|
|
|
|
DECLARE_AND_READ_ADDRESS(local_short_address);
|
|
|
|
|
DECLARE_AND_READ_ADDRESS(long_string_address);
|
|
|
|
|
#undef DECLARE_AND_READ_ADDRESS
|
|
|
|
|
|
|
|
|
|
DoTest(ChildProcess(),
|
|
|
|
|
const_empty_address,
|
|
|
|
|
const_short_address,
|
|
|
|
|
local_empty_address,
|
|
|
|
|
local_short_address,
|
|
|
|
|
long_string_address);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-20 11:57:49 -08:00
|
|
|
|
void Compare(ProcessMemory& memory, VMAddress address, const char* str) {
|
|
|
|
|
std::string result;
|
|
|
|
|
if (limit_size_) {
|
|
|
|
|
ASSERT_TRUE(
|
|
|
|
|
memory.ReadCStringSizeLimited(address, strlen(str) + 1, &result));
|
|
|
|
|
EXPECT_EQ(result, str);
|
|
|
|
|
ASSERT_TRUE(
|
|
|
|
|
memory.ReadCStringSizeLimited(address, strlen(str) + 2, &result));
|
|
|
|
|
EXPECT_EQ(result, str);
|
|
|
|
|
EXPECT_FALSE(
|
|
|
|
|
memory.ReadCStringSizeLimited(address, strlen(str), &result));
|
|
|
|
|
} else {
|
|
|
|
|
ASSERT_TRUE(memory.ReadCString(address, &result));
|
|
|
|
|
EXPECT_EQ(result, str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
void DoTest(ProcessType process,
|
|
|
|
|
VMAddress const_empty_address,
|
|
|
|
|
VMAddress const_short_address,
|
|
|
|
|
VMAddress local_empty_address,
|
|
|
|
|
VMAddress local_short_address,
|
|
|
|
|
VMAddress long_string_address) {
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
|
2021-08-10 14:55:25 -07:00
|
|
|
|
FakePtraceConnection connection;
|
|
|
|
|
ASSERT_TRUE(connection.Initialize(process));
|
|
|
|
|
ProcessMemoryLinux memory(&connection);
|
|
|
|
|
#else
|
2018-01-30 10:12:47 -08:00
|
|
|
|
ProcessMemoryNative memory;
|
2018-01-30 10:16:57 -08:00
|
|
|
|
ASSERT_TRUE(memory.Initialize(process));
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) ||
|
|
|
|
|
// BUILDFLAG(IS_CHROMEOS)
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-12-20 11:57:49 -08:00
|
|
|
|
Compare(memory, const_empty_address, kConstCharEmpty);
|
|
|
|
|
Compare(memory, const_short_address, kConstCharShort);
|
|
|
|
|
Compare(memory, local_empty_address, "");
|
|
|
|
|
Compare(memory, local_short_address, SHORT_LOCAL_STRING);
|
|
|
|
|
std::string long_string_for_comparison = MakeLongString();
|
|
|
|
|
Compare(memory, long_string_address, long_string_for_comparison.c_str());
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool limit_size_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST(ProcessMemory, ReadCStringSelf) {
|
|
|
|
|
ReadCStringTest test(/* limit_size= */ false);
|
|
|
|
|
test.RunAgainstSelf();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
TEST(ProcessMemory, ReadCStringChild) {
|
2017-04-03 14:09:58 -07:00
|
|
|
|
ReadCStringTest test(/* limit_size= */ false);
|
2018-01-30 10:16:57 -08:00
|
|
|
|
test.RunAgainstChild();
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(ProcessMemory, ReadCStringSizeLimitedSelf) {
|
|
|
|
|
ReadCStringTest test(/* limit_size= */ true);
|
|
|
|
|
test.RunAgainstSelf();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-30 10:16:57 -08:00
|
|
|
|
TEST(ProcessMemory, ReadCStringSizeLimitedChild) {
|
2017-04-03 14:09:58 -07:00
|
|
|
|
ReadCStringTest test(/* limit_size= */ true);
|
2018-01-30 10:16:57 -08:00
|
|
|
|
test.RunAgainstChild();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 13:14:20 -08:00
|
|
|
|
void DoReadUnmappedChildMainSetup(void* page) {
|
|
|
|
|
char* region = reinterpret_cast<char*>(page);
|
|
|
|
|
for (size_t index = 0; index < base::GetPageSize(); ++index) {
|
2021-04-16 21:29:33 +00:00
|
|
|
|
region[index] = static_cast<char>(index % 256);
|
2018-01-30 13:52:50 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CRASHPAD_CHILD_TEST_MAIN(ReadUnmappedChildMain) {
|
2018-11-05 13:14:20 -08:00
|
|
|
|
ScopedGuardedPage pages;
|
|
|
|
|
VMAddress address = reinterpret_cast<VMAddress>(pages.Pointer());
|
|
|
|
|
DoReadUnmappedChildMainSetup(pages.Pointer());
|
2018-12-20 11:12:51 -08:00
|
|
|
|
FileHandle out = MultiprocessAdaptor::OutputHandle();
|
2018-01-30 13:52:50 -08:00
|
|
|
|
CheckedWriteFile(out, &address, sizeof(address));
|
2018-12-20 11:12:51 -08:00
|
|
|
|
CheckedReadFileAtEOF(MultiprocessAdaptor::InputHandle());
|
2018-01-30 13:52:50 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 13:14:20 -08:00
|
|
|
|
// This test only supports running against a child process because
|
|
|
|
|
// ScopedGuardedPage is not thread-safe.
|
2018-12-20 11:12:51 -08:00
|
|
|
|
class ReadUnmappedTest : public MultiprocessAdaptor {
|
2018-01-30 13:52:50 -08:00
|
|
|
|
public:
|
2018-12-20 11:12:51 -08:00
|
|
|
|
ReadUnmappedTest() : MultiprocessAdaptor() {
|
2018-01-30 13:52:50 -08:00
|
|
|
|
SetChildTestMainFunction("ReadUnmappedChildMain");
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 12:55:12 -07:00
|
|
|
|
ReadUnmappedTest(const ReadUnmappedTest&) = delete;
|
|
|
|
|
ReadUnmappedTest& operator=(const ReadUnmappedTest&) = delete;
|
|
|
|
|
|
2018-01-30 13:52:50 -08:00
|
|
|
|
void RunAgainstChild() { Run(); }
|
|
|
|
|
|
|
|
|
|
private:
|
2018-12-20 11:12:51 -08:00
|
|
|
|
void Parent() override {
|
2018-04-16 10:23:21 -07:00
|
|
|
|
VMAddress address = 0;
|
2018-01-30 13:52:50 -08:00
|
|
|
|
ASSERT_TRUE(ReadFileExactly(ReadPipeHandle(), &address, sizeof(address)));
|
2018-11-05 13:14:20 -08:00
|
|
|
|
DoTest(ChildProcess(), address);
|
2018-01-30 13:52:50 -08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 13:14:20 -08:00
|
|
|
|
void DoTest(ProcessType process, VMAddress address) {
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
|
2021-08-10 14:55:25 -07:00
|
|
|
|
DirectPtraceConnection connection;
|
|
|
|
|
ASSERT_TRUE(connection.Initialize(process));
|
|
|
|
|
ProcessMemoryLinux memory(&connection);
|
|
|
|
|
#else
|
2018-01-30 13:52:50 -08:00
|
|
|
|
ProcessMemoryNative memory;
|
|
|
|
|
ASSERT_TRUE(memory.Initialize(process));
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) ||
|
|
|
|
|
// BUILDFLAG(IS_CHROMEOS)
|
2018-01-30 13:52:50 -08:00
|
|
|
|
|
|
|
|
|
VMAddress page_addr1 = address;
|
2018-11-05 13:14:20 -08:00
|
|
|
|
VMAddress page_addr2 = page_addr1 + base::GetPageSize();
|
2018-01-30 13:52:50 -08:00
|
|
|
|
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
auto result = base::HeapArray<char>::Uninit(base::GetPageSize() * 2);
|
|
|
|
|
EXPECT_TRUE(memory.Read(page_addr1, base::GetPageSize(), result.data()));
|
|
|
|
|
EXPECT_TRUE(memory.Read(page_addr2 - 1, 1, result.data()));
|
2018-01-30 13:52:50 -08:00
|
|
|
|
|
Replace std::unique_ptr<T> with HeapArray
Bug: crashpad: 326459659,326458942,326459376,326459390,326459417,326458979,326459333,326459016,326458338,326458738,326459156,326459512,326458694
Change-Id: I04724530cbef50a8d3c18f306d16c0bbf3b0815b
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5512394
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Arthur Wang <wuwang@chromium.org>
2024-05-09 12:39:17 -07:00
|
|
|
|
EXPECT_FALSE(memory.Read(page_addr1, result.size(), result.data()));
|
|
|
|
|
EXPECT_FALSE(memory.Read(page_addr2, base::GetPageSize(), result.data()));
|
|
|
|
|
EXPECT_FALSE(memory.Read(page_addr2 - 1, 2, result.data()));
|
2018-01-30 13:52:50 -08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST(ProcessMemory, ReadUnmappedChild) {
|
|
|
|
|
ReadUnmappedTest test;
|
|
|
|
|
ASSERT_FALSE(testing::Test::HasFailure());
|
|
|
|
|
test.RunAgainstChild();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
constexpr size_t kChildProcessStringLength = 10;
|
2018-01-30 10:16:57 -08:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
class StringDataInChildProcess {
|
2018-01-30 10:16:57 -08:00
|
|
|
|
public:
|
2018-01-31 14:34:38 -08:00
|
|
|
|
// This constructor only makes sense in the child process.
|
2018-11-05 13:14:20 -08:00
|
|
|
|
explicit StringDataInChildProcess(const char* cstring, bool valid)
|
2018-01-31 14:34:38 -08:00
|
|
|
|
: address_(FromPointerCast<VMAddress>(cstring)) {
|
2018-11-05 13:14:20 -08:00
|
|
|
|
if (valid) {
|
|
|
|
|
memcpy(expected_value_, cstring, kChildProcessStringLength + 1);
|
|
|
|
|
} else {
|
|
|
|
|
memset(expected_value_, 0xff, kChildProcessStringLength + 1);
|
|
|
|
|
}
|
2018-01-31 14:34:38 -08:00
|
|
|
|
}
|
2018-01-30 10:16:57 -08:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
void Write(FileHandle out) {
|
|
|
|
|
CheckedWriteFile(out, &address_, sizeof(address_));
|
|
|
|
|
CheckedWriteFile(out, &expected_value_, sizeof(expected_value_));
|
|
|
|
|
}
|
2018-01-30 10:16:57 -08:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
static StringDataInChildProcess Read(FileHandle in) {
|
|
|
|
|
StringDataInChildProcess str;
|
|
|
|
|
EXPECT_TRUE(ReadFileExactly(in, &str.address_, sizeof(str.address_)));
|
|
|
|
|
EXPECT_TRUE(
|
|
|
|
|
ReadFileExactly(in, &str.expected_value_, sizeof(str.expected_value_)));
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2018-01-30 10:16:57 -08:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
VMAddress address() const { return address_; }
|
|
|
|
|
std::string expected_value() const { return expected_value_; }
|
2018-01-30 10:16:57 -08:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
private:
|
|
|
|
|
StringDataInChildProcess() : address_(0), expected_value_() {}
|
2018-01-30 10:16:57 -08:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
VMAddress address_;
|
|
|
|
|
char expected_value_[kChildProcessStringLength + 1];
|
2018-01-30 10:16:57 -08:00
|
|
|
|
};
|
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
void DoCStringUnmappedTestSetup(
|
2018-11-05 13:14:20 -08:00
|
|
|
|
void* page,
|
2018-01-31 14:34:38 -08:00
|
|
|
|
std::vector<StringDataInChildProcess>* strings) {
|
2018-11-05 13:14:20 -08:00
|
|
|
|
char* region = reinterpret_cast<char*>(page);
|
|
|
|
|
for (size_t index = 0; index < base::GetPageSize(); ++index) {
|
2018-01-31 14:34:38 -08:00
|
|
|
|
region[index] = 1 + index % 255;
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
// A string at the start of the mapped region
|
|
|
|
|
char* string1 = region;
|
|
|
|
|
string1[kChildProcessStringLength] = '\0';
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
// A string near the end of the mapped region
|
2018-11-05 13:14:20 -08:00
|
|
|
|
char* string2 = region + base::GetPageSize() - kChildProcessStringLength * 2;
|
2018-01-31 14:34:38 -08:00
|
|
|
|
string2[kChildProcessStringLength] = '\0';
|
|
|
|
|
|
|
|
|
|
// A string that crosses from the mapped into the unmapped region
|
2018-11-05 13:14:20 -08:00
|
|
|
|
char* string3 = region + base::GetPageSize() - kChildProcessStringLength + 1;
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
// A string entirely in the unmapped region
|
2018-11-05 13:14:20 -08:00
|
|
|
|
char* string4 = region + base::GetPageSize() + 10;
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-11-05 13:14:20 -08:00
|
|
|
|
strings->push_back(StringDataInChildProcess(string1, true));
|
|
|
|
|
strings->push_back(StringDataInChildProcess(string2, true));
|
|
|
|
|
strings->push_back(StringDataInChildProcess(string3, false));
|
|
|
|
|
strings->push_back(StringDataInChildProcess(string4, false));
|
2018-01-31 14:34:38 -08:00
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
CRASHPAD_CHILD_TEST_MAIN(ReadCStringUnmappedChildMain) {
|
2018-11-05 13:14:20 -08:00
|
|
|
|
ScopedGuardedPage pages;
|
2018-01-31 14:34:38 -08:00
|
|
|
|
std::vector<StringDataInChildProcess> strings;
|
2018-11-05 13:14:20 -08:00
|
|
|
|
DoCStringUnmappedTestSetup(pages.Pointer(), &strings);
|
2018-12-20 11:12:51 -08:00
|
|
|
|
FileHandle out = MultiprocessAdaptor::OutputHandle();
|
2018-01-31 14:34:38 -08:00
|
|
|
|
strings[0].Write(out);
|
|
|
|
|
strings[1].Write(out);
|
|
|
|
|
strings[2].Write(out);
|
|
|
|
|
strings[3].Write(out);
|
2018-12-20 11:12:51 -08:00
|
|
|
|
CheckedReadFileAtEOF(MultiprocessAdaptor::InputHandle());
|
2018-01-31 14:34:38 -08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2018-11-05 13:14:20 -08:00
|
|
|
|
// This test only supports running against a child process because
|
|
|
|
|
// ScopedGuardedPage is not thread-safe.
|
2018-12-20 11:12:51 -08:00
|
|
|
|
class ReadCStringUnmappedTest : public MultiprocessAdaptor {
|
2018-01-31 14:34:38 -08:00
|
|
|
|
public:
|
|
|
|
|
ReadCStringUnmappedTest(bool limit_size)
|
2018-12-20 11:12:51 -08:00
|
|
|
|
: MultiprocessAdaptor(), limit_size_(limit_size) {
|
2018-01-31 14:34:38 -08:00
|
|
|
|
SetChildTestMainFunction("ReadCStringUnmappedChildMain");
|
|
|
|
|
}
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
2021-09-20 12:55:12 -07:00
|
|
|
|
ReadCStringUnmappedTest(const ReadCStringUnmappedTest&) = delete;
|
|
|
|
|
ReadCStringUnmappedTest& operator=(const ReadCStringUnmappedTest&) = delete;
|
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
void RunAgainstChild() { Run(); }
|
|
|
|
|
|
2017-04-03 14:09:58 -07:00
|
|
|
|
private:
|
2018-12-20 11:12:51 -08:00
|
|
|
|
void Parent() override {
|
2018-01-31 14:34:38 -08:00
|
|
|
|
std::vector<StringDataInChildProcess> strings;
|
|
|
|
|
strings.push_back(StringDataInChildProcess::Read(ReadPipeHandle()));
|
|
|
|
|
strings.push_back(StringDataInChildProcess::Read(ReadPipeHandle()));
|
|
|
|
|
strings.push_back(StringDataInChildProcess::Read(ReadPipeHandle()));
|
|
|
|
|
strings.push_back(StringDataInChildProcess::Read(ReadPipeHandle()));
|
2018-11-05 13:14:20 -08:00
|
|
|
|
ASSERT_NO_FATAL_FAILURE(DoTest(ChildProcess(), strings));
|
2018-01-31 14:34:38 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoTest(ProcessType process,
|
|
|
|
|
const std::vector<StringDataInChildProcess>& strings) {
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
|
2021-08-10 14:55:25 -07:00
|
|
|
|
DirectPtraceConnection connection;
|
|
|
|
|
ASSERT_TRUE(connection.Initialize(process));
|
|
|
|
|
ProcessMemoryLinux memory(&connection);
|
|
|
|
|
#else
|
2018-01-30 10:12:47 -08:00
|
|
|
|
ProcessMemoryNative memory;
|
2018-01-31 14:34:38 -08:00
|
|
|
|
ASSERT_TRUE(memory.Initialize(process));
|
2022-01-19 15:00:24 -05:00
|
|
|
|
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_LINUX) ||
|
|
|
|
|
// BUILDFLAG(IS_CHROMEOS)
|
2018-01-31 14:34:38 -08:00
|
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
result.reserve(kChildProcessStringLength + 1);
|
2017-04-03 14:09:58 -07:00
|
|
|
|
|
|
|
|
|
if (limit_size_) {
|
2018-01-31 14:34:38 -08:00
|
|
|
|
ASSERT_TRUE(memory.ReadCStringSizeLimited(
|
|
|
|
|
strings[0].address(), kChildProcessStringLength + 1, &result));
|
|
|
|
|
EXPECT_EQ(result, strings[0].expected_value());
|
|
|
|
|
ASSERT_TRUE(memory.ReadCStringSizeLimited(
|
|
|
|
|
strings[1].address(), kChildProcessStringLength + 1, &result));
|
|
|
|
|
EXPECT_EQ(result, strings[1].expected_value());
|
|
|
|
|
EXPECT_FALSE(memory.ReadCStringSizeLimited(
|
|
|
|
|
strings[2].address(), kChildProcessStringLength + 1, &result));
|
|
|
|
|
EXPECT_FALSE(memory.ReadCStringSizeLimited(
|
|
|
|
|
strings[3].address(), kChildProcessStringLength + 1, &result));
|
2017-04-03 14:09:58 -07:00
|
|
|
|
} else {
|
2018-01-31 14:34:38 -08:00
|
|
|
|
ASSERT_TRUE(memory.ReadCString(strings[0].address(), &result));
|
|
|
|
|
EXPECT_EQ(result, strings[0].expected_value());
|
|
|
|
|
ASSERT_TRUE(memory.ReadCString(strings[1].address(), &result));
|
|
|
|
|
EXPECT_EQ(result, strings[1].expected_value());
|
|
|
|
|
EXPECT_FALSE(memory.ReadCString(strings[2].address(), &result));
|
|
|
|
|
EXPECT_FALSE(memory.ReadCString(strings[3].address(), &result));
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool limit_size_;
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
TEST(ProcessMemory, ReadCStringUnmappedChild) {
|
2017-04-03 14:09:58 -07:00
|
|
|
|
ReadCStringUnmappedTest test(/* limit_size= */ false);
|
|
|
|
|
ASSERT_FALSE(testing::Test::HasFailure());
|
2018-01-31 14:34:38 -08:00
|
|
|
|
test.RunAgainstChild();
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 14:34:38 -08:00
|
|
|
|
TEST(ProcessMemory, ReadCStringSizeLimitedUnmappedChild) {
|
2017-04-03 14:09:58 -07:00
|
|
|
|
ReadCStringUnmappedTest test(/* limit_size= */ true);
|
|
|
|
|
ASSERT_FALSE(testing::Test::HasFailure());
|
2018-01-31 14:34:38 -08:00
|
|
|
|
test.RunAgainstChild();
|
2017-04-03 14:09:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace crashpad
|