2022-09-06 19:14:07 -04:00
|
|
|
// Copyright 2018 The Crashpad Authors
|
2018-02-22 11:06:35 -08: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.
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
#include "snapshot/fuchsia/process_reader_fuchsia.h"
|
2018-02-22 11:06:35 -08:00
|
|
|
|
2018-04-30 12:57:03 -07:00
|
|
|
#include <pthread.h>
|
2018-02-22 11:06:35 -08:00
|
|
|
#include <zircon/process.h>
|
|
|
|
#include <zircon/syscalls.h>
|
fuchsia: Work around lack of packaging in Fuchsia tree build
Packaged test running seems to be a ways off, but with a bit of path
fiddling in test_paths.cc we can actually use the paths where the tests
are copied, so do that instead to get all the tests re-enabled. The
setup in BUILD.gn should be mostly-useful once packaging is working as
all helper/data files will need to specified there anyway.
Also, attempted fix to flaky behaviour in
ProcessReaderFuchsia.ChildThreads exposed because the tests are now
being run. zx_object_wait_many() waits on *any* of the objects, not
*all* of them. Derp!
And finally, for the same test, work around some unintuitive behaviour
in zx_task_suspend(), in particular that the thread will not be
suspended for the purpose of reading registers right away, but instead
only "sometime later", which appears in pratice to be after the next
context switch. Have ScopedTaskSuspend block for a while to try to
ensure the registers become readble, and if they don't, at least fail
noisily at that point.
Bug: crashpad:196
Change-Id: I01fb3590ede96301c941c2a88eba47fdbfe74ea7
Reviewed-on: https://chromium-review.googlesource.com/1053797
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
2018-05-10 10:27:58 -07:00
|
|
|
#include <zircon/syscalls/port.h>
|
2018-07-02 13:22:13 -07:00
|
|
|
#include <zircon/types.h>
|
2018-02-22 11:06:35 -08:00
|
|
|
|
2022-02-28 20:57:19 -08:00
|
|
|
#include <iterator>
|
|
|
|
|
2022-06-13 14:44:24 -06:00
|
|
|
#include "base/strings/stringprintf.h"
|
2018-02-22 11:06:35 -08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "test/multiprocess_exec.h"
|
2022-06-13 14:44:24 -06:00
|
|
|
#include "test/scoped_set_thread_name.h"
|
2018-05-04 17:06:13 -07:00
|
|
|
#include "test/test_paths.h"
|
2018-04-30 12:57:03 -07:00
|
|
|
#include "util/fuchsia/scoped_task_suspend.h"
|
2018-02-22 11:06:35 -08:00
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
namespace test {
|
|
|
|
namespace {
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
TEST(ProcessReaderFuchsia, SelfBasic) {
|
2022-06-13 14:44:24 -06:00
|
|
|
const ScopedSetThreadName scoped_set_thread_name("SelfBasic");
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia process_reader;
|
2018-07-30 15:50:17 -07:00
|
|
|
ASSERT_TRUE(process_reader.Initialize(*zx::process::self()));
|
2018-02-22 11:06:35 -08:00
|
|
|
|
|
|
|
static constexpr char kTestMemory[] = "Some test memory";
|
2022-02-28 20:57:19 -08:00
|
|
|
char buffer[std::size(kTestMemory)];
|
2018-02-22 11:06:35 -08:00
|
|
|
ASSERT_TRUE(process_reader.Memory()->Read(
|
|
|
|
reinterpret_cast<zx_vaddr_t>(kTestMemory), sizeof(kTestMemory), &buffer));
|
|
|
|
EXPECT_STREQ(kTestMemory, buffer);
|
|
|
|
|
|
|
|
const auto& modules = process_reader.Modules();
|
2019-04-01 13:38:26 -07:00
|
|
|
// The process should have at least one module, the executable, and then some
|
|
|
|
// shared libraries, no loadable modules.
|
2018-02-22 11:06:35 -08:00
|
|
|
EXPECT_GT(modules.size(), 0u);
|
2019-04-01 13:38:26 -07:00
|
|
|
size_t num_executables = 0u;
|
|
|
|
size_t num_shared_libraries = 0u;
|
2018-02-22 11:06:35 -08:00
|
|
|
for (const auto& module : modules) {
|
|
|
|
EXPECT_FALSE(module.name.empty());
|
|
|
|
EXPECT_NE(module.type, ModuleSnapshot::kModuleTypeUnknown);
|
2019-04-01 13:38:26 -07:00
|
|
|
|
|
|
|
if (module.type == ModuleSnapshot::kModuleTypeExecutable) {
|
|
|
|
EXPECT_EQ(module.name, "<_>");
|
|
|
|
num_executables++;
|
|
|
|
} else if (module.type == ModuleSnapshot::kModuleTypeSharedLibrary) {
|
|
|
|
EXPECT_NE(module.name, "<_>");
|
|
|
|
num_shared_libraries++;
|
|
|
|
}
|
2018-02-22 11:06:35 -08:00
|
|
|
}
|
2019-04-01 13:38:26 -07:00
|
|
|
EXPECT_EQ(num_executables, 1u);
|
|
|
|
EXPECT_EQ(num_shared_libraries, modules.size() - num_executables);
|
2018-02-22 11:06:35 -08:00
|
|
|
|
|
|
|
const auto& threads = process_reader.Threads();
|
|
|
|
EXPECT_GT(threads.size(), 0u);
|
|
|
|
|
|
|
|
zx_info_handle_basic_t info;
|
|
|
|
ASSERT_EQ(zx_object_get_info(zx_thread_self(),
|
|
|
|
ZX_INFO_HANDLE_BASIC,
|
|
|
|
&info,
|
|
|
|
sizeof(info),
|
|
|
|
nullptr,
|
|
|
|
nullptr),
|
|
|
|
ZX_OK);
|
|
|
|
EXPECT_EQ(threads[0].id, info.koid);
|
|
|
|
EXPECT_EQ(threads[0].state, ZX_THREAD_STATE_RUNNING);
|
2022-06-13 14:44:24 -06:00
|
|
|
EXPECT_EQ(threads[0].name, "SelfBasic");
|
2018-02-22 11:06:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr char kTestMemory[] = "Read me from another process";
|
|
|
|
|
|
|
|
CRASHPAD_CHILD_TEST_MAIN(ProcessReaderBasicChildTestMain) {
|
2018-04-27 13:11:48 -07:00
|
|
|
zx_vaddr_t addr = reinterpret_cast<zx_vaddr_t>(kTestMemory);
|
|
|
|
CheckedWriteFile(
|
|
|
|
StdioFileHandle(StdioStream::kStandardOutput), &addr, sizeof(addr));
|
2018-02-22 11:06:35 -08:00
|
|
|
CheckedReadFileAtEOF(StdioFileHandle(StdioStream::kStandardInput));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class BasicChildTest : public MultiprocessExec {
|
|
|
|
public:
|
|
|
|
BasicChildTest() : MultiprocessExec() {
|
|
|
|
SetChildTestMainFunction("ProcessReaderBasicChildTestMain");
|
|
|
|
}
|
2021-09-20 12:55:12 -07:00
|
|
|
|
|
|
|
BasicChildTest(const BasicChildTest&) = delete;
|
|
|
|
BasicChildTest& operator=(const BasicChildTest&) = delete;
|
|
|
|
|
2018-02-22 11:06:35 -08:00
|
|
|
~BasicChildTest() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void MultiprocessParent() override {
|
2018-02-22 12:12:26 -08:00
|
|
|
ProcessReaderFuchsia process_reader;
|
2018-07-30 15:50:17 -07:00
|
|
|
ASSERT_TRUE(process_reader.Initialize(*ChildProcess()));
|
2018-04-27 13:11:48 -07:00
|
|
|
|
|
|
|
zx_vaddr_t addr;
|
|
|
|
ASSERT_TRUE(ReadFileExactly(ReadPipeHandle(), &addr, sizeof(addr)));
|
2018-02-22 11:06:35 -08:00
|
|
|
|
|
|
|
std::string read_string;
|
2018-04-27 13:11:48 -07:00
|
|
|
ASSERT_TRUE(process_reader.Memory()->ReadCString(addr, &read_string));
|
2018-02-22 11:06:35 -08:00
|
|
|
EXPECT_EQ(read_string, kTestMemory);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-22 12:12:26 -08:00
|
|
|
TEST(ProcessReaderFuchsia, ChildBasic) {
|
2018-02-22 11:06:35 -08:00
|
|
|
BasicChildTest test;
|
|
|
|
test.Run();
|
|
|
|
}
|
|
|
|
|
2022-06-13 14:44:24 -06:00
|
|
|
struct ThreadData {
|
|
|
|
zx_handle_t port;
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
2018-04-30 12:57:03 -07:00
|
|
|
void* SignalAndSleep(void* arg) {
|
2022-06-13 14:44:24 -06:00
|
|
|
const ThreadData* thread_data = reinterpret_cast<const ThreadData*>(arg);
|
|
|
|
const ScopedSetThreadName scoped_set_thread_name(thread_data->name);
|
fuchsia: Work around lack of packaging in Fuchsia tree build
Packaged test running seems to be a ways off, but with a bit of path
fiddling in test_paths.cc we can actually use the paths where the tests
are copied, so do that instead to get all the tests re-enabled. The
setup in BUILD.gn should be mostly-useful once packaging is working as
all helper/data files will need to specified there anyway.
Also, attempted fix to flaky behaviour in
ProcessReaderFuchsia.ChildThreads exposed because the tests are now
being run. zx_object_wait_many() waits on *any* of the objects, not
*all* of them. Derp!
And finally, for the same test, work around some unintuitive behaviour
in zx_task_suspend(), in particular that the thread will not be
suspended for the purpose of reading registers right away, but instead
only "sometime later", which appears in pratice to be after the next
context switch. Have ScopedTaskSuspend block for a while to try to
ensure the registers become readble, and if they don't, at least fail
noisily at that point.
Bug: crashpad:196
Change-Id: I01fb3590ede96301c941c2a88eba47fdbfe74ea7
Reviewed-on: https://chromium-review.googlesource.com/1053797
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
2018-05-10 10:27:58 -07:00
|
|
|
zx_port_packet_t packet = {};
|
|
|
|
packet.type = ZX_PKT_TYPE_USER;
|
2022-06-13 14:44:24 -06:00
|
|
|
zx_port_queue(thread_data->port, &packet);
|
2018-07-02 13:22:13 -07:00
|
|
|
zx_nanosleep(ZX_TIME_INFINITE);
|
2018-04-30 12:57:03 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRASHPAD_CHILD_TEST_MAIN(ProcessReaderChildThreadsTestMain) {
|
2022-06-13 14:44:24 -06:00
|
|
|
const ScopedSetThreadName scoped_set_thread_name(
|
|
|
|
"ProcessReaderChildThreadsTest-Main");
|
|
|
|
|
2018-04-30 12:57:03 -07:00
|
|
|
// Create 5 threads with stack sizes of 4096, 8192, ...
|
fuchsia: Work around lack of packaging in Fuchsia tree build
Packaged test running seems to be a ways off, but with a bit of path
fiddling in test_paths.cc we can actually use the paths where the tests
are copied, so do that instead to get all the tests re-enabled. The
setup in BUILD.gn should be mostly-useful once packaging is working as
all helper/data files will need to specified there anyway.
Also, attempted fix to flaky behaviour in
ProcessReaderFuchsia.ChildThreads exposed because the tests are now
being run. zx_object_wait_many() waits on *any* of the objects, not
*all* of them. Derp!
And finally, for the same test, work around some unintuitive behaviour
in zx_task_suspend(), in particular that the thread will not be
suspended for the purpose of reading registers right away, but instead
only "sometime later", which appears in pratice to be after the next
context switch. Have ScopedTaskSuspend block for a while to try to
ensure the registers become readble, and if they don't, at least fail
noisily at that point.
Bug: crashpad:196
Change-Id: I01fb3590ede96301c941c2a88eba47fdbfe74ea7
Reviewed-on: https://chromium-review.googlesource.com/1053797
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
2018-05-10 10:27:58 -07:00
|
|
|
zx_handle_t port;
|
|
|
|
zx_status_t status = zx_port_create(0, &port);
|
|
|
|
EXPECT_EQ(status, ZX_OK);
|
|
|
|
|
|
|
|
constexpr size_t kNumThreads = 5;
|
2022-06-13 15:49:56 -07:00
|
|
|
struct ThreadData thread_data[kNumThreads] = {{0, 0}};
|
2022-06-13 14:44:24 -06:00
|
|
|
|
fuchsia: Work around lack of packaging in Fuchsia tree build
Packaged test running seems to be a ways off, but with a bit of path
fiddling in test_paths.cc we can actually use the paths where the tests
are copied, so do that instead to get all the tests re-enabled. The
setup in BUILD.gn should be mostly-useful once packaging is working as
all helper/data files will need to specified there anyway.
Also, attempted fix to flaky behaviour in
ProcessReaderFuchsia.ChildThreads exposed because the tests are now
being run. zx_object_wait_many() waits on *any* of the objects, not
*all* of them. Derp!
And finally, for the same test, work around some unintuitive behaviour
in zx_task_suspend(), in particular that the thread will not be
suspended for the purpose of reading registers right away, but instead
only "sometime later", which appears in pratice to be after the next
context switch. Have ScopedTaskSuspend block for a while to try to
ensure the registers become readble, and if they don't, at least fail
noisily at that point.
Bug: crashpad:196
Change-Id: I01fb3590ede96301c941c2a88eba47fdbfe74ea7
Reviewed-on: https://chromium-review.googlesource.com/1053797
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
2018-05-10 10:27:58 -07:00
|
|
|
for (size_t i = 0; i < kNumThreads; ++i) {
|
2022-06-13 14:44:24 -06:00
|
|
|
thread_data[i] = {
|
|
|
|
.port = port,
|
|
|
|
.name = base::StringPrintf("ProcessReaderChildThreadsTest-%zu", i + 1),
|
|
|
|
};
|
2018-04-30 12:57:03 -07:00
|
|
|
pthread_attr_t attr;
|
|
|
|
EXPECT_EQ(pthread_attr_init(&attr), 0);
|
|
|
|
EXPECT_EQ(pthread_attr_setstacksize(&attr, (i + 1) * 4096), 0);
|
fuchsia: Work around lack of packaging in Fuchsia tree build
Packaged test running seems to be a ways off, but with a bit of path
fiddling in test_paths.cc we can actually use the paths where the tests
are copied, so do that instead to get all the tests re-enabled. The
setup in BUILD.gn should be mostly-useful once packaging is working as
all helper/data files will need to specified there anyway.
Also, attempted fix to flaky behaviour in
ProcessReaderFuchsia.ChildThreads exposed because the tests are now
being run. zx_object_wait_many() waits on *any* of the objects, not
*all* of them. Derp!
And finally, for the same test, work around some unintuitive behaviour
in zx_task_suspend(), in particular that the thread will not be
suspended for the purpose of reading registers right away, but instead
only "sometime later", which appears in pratice to be after the next
context switch. Have ScopedTaskSuspend block for a while to try to
ensure the registers become readble, and if they don't, at least fail
noisily at that point.
Bug: crashpad:196
Change-Id: I01fb3590ede96301c941c2a88eba47fdbfe74ea7
Reviewed-on: https://chromium-review.googlesource.com/1053797
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
2018-05-10 10:27:58 -07:00
|
|
|
pthread_t thread;
|
2022-06-13 14:44:24 -06:00
|
|
|
EXPECT_EQ(pthread_create(&thread, &attr, &SignalAndSleep, &thread_data[i]),
|
|
|
|
0);
|
2018-04-30 12:57:03 -07:00
|
|
|
}
|
|
|
|
|
fuchsia: Work around lack of packaging in Fuchsia tree build
Packaged test running seems to be a ways off, but with a bit of path
fiddling in test_paths.cc we can actually use the paths where the tests
are copied, so do that instead to get all the tests re-enabled. The
setup in BUILD.gn should be mostly-useful once packaging is working as
all helper/data files will need to specified there anyway.
Also, attempted fix to flaky behaviour in
ProcessReaderFuchsia.ChildThreads exposed because the tests are now
being run. zx_object_wait_many() waits on *any* of the objects, not
*all* of them. Derp!
And finally, for the same test, work around some unintuitive behaviour
in zx_task_suspend(), in particular that the thread will not be
suspended for the purpose of reading registers right away, but instead
only "sometime later", which appears in pratice to be after the next
context switch. Have ScopedTaskSuspend block for a while to try to
ensure the registers become readble, and if they don't, at least fail
noisily at that point.
Bug: crashpad:196
Change-Id: I01fb3590ede96301c941c2a88eba47fdbfe74ea7
Reviewed-on: https://chromium-review.googlesource.com/1053797
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
2018-05-10 10:27:58 -07:00
|
|
|
// Wait until all threads are ready.
|
|
|
|
for (size_t i = 0; i < kNumThreads; ++i) {
|
|
|
|
zx_port_packet_t packet;
|
2018-05-25 08:46:11 -07:00
|
|
|
zx_port_wait(port, ZX_TIME_INFINITE, &packet);
|
fuchsia: Work around lack of packaging in Fuchsia tree build
Packaged test running seems to be a ways off, but with a bit of path
fiddling in test_paths.cc we can actually use the paths where the tests
are copied, so do that instead to get all the tests re-enabled. The
setup in BUILD.gn should be mostly-useful once packaging is working as
all helper/data files will need to specified there anyway.
Also, attempted fix to flaky behaviour in
ProcessReaderFuchsia.ChildThreads exposed because the tests are now
being run. zx_object_wait_many() waits on *any* of the objects, not
*all* of them. Derp!
And finally, for the same test, work around some unintuitive behaviour
in zx_task_suspend(), in particular that the thread will not be
suspended for the purpose of reading registers right away, but instead
only "sometime later", which appears in pratice to be after the next
context switch. Have ScopedTaskSuspend block for a while to try to
ensure the registers become readble, and if they don't, at least fail
noisily at that point.
Bug: crashpad:196
Change-Id: I01fb3590ede96301c941c2a88eba47fdbfe74ea7
Reviewed-on: https://chromium-review.googlesource.com/1053797
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
Commit-Queue: Scott Graham <scottmg@chromium.org>
2018-05-10 10:27:58 -07:00
|
|
|
}
|
2018-04-30 12:57:03 -07:00
|
|
|
|
|
|
|
char c = ' ';
|
|
|
|
CheckedWriteFile(
|
|
|
|
StdioFileHandle(StdioStream::kStandardOutput), &c, sizeof(c));
|
|
|
|
CheckedReadFileAtEOF(StdioFileHandle(StdioStream::kStandardInput));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThreadsChildTest : public MultiprocessExec {
|
|
|
|
public:
|
|
|
|
ThreadsChildTest() : MultiprocessExec() {
|
|
|
|
SetChildTestMainFunction("ProcessReaderChildThreadsTestMain");
|
|
|
|
}
|
2021-09-20 12:55:12 -07:00
|
|
|
|
|
|
|
ThreadsChildTest(const ThreadsChildTest&) = delete;
|
|
|
|
ThreadsChildTest& operator=(const ThreadsChildTest&) = delete;
|
|
|
|
|
2018-04-30 12:57:03 -07:00
|
|
|
~ThreadsChildTest() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void MultiprocessParent() override {
|
|
|
|
char c;
|
|
|
|
ASSERT_TRUE(ReadFileExactly(ReadPipeHandle(), &c, 1));
|
|
|
|
ASSERT_EQ(c, ' ');
|
|
|
|
|
2018-07-30 15:50:17 -07:00
|
|
|
ScopedTaskSuspend suspend(*ChildProcess());
|
2018-04-30 12:57:03 -07:00
|
|
|
|
|
|
|
ProcessReaderFuchsia process_reader;
|
2018-07-30 15:50:17 -07:00
|
|
|
ASSERT_TRUE(process_reader.Initialize(*ChildProcess()));
|
2018-04-30 12:57:03 -07:00
|
|
|
|
|
|
|
const auto& threads = process_reader.Threads();
|
|
|
|
EXPECT_EQ(threads.size(), 6u);
|
|
|
|
|
2022-06-13 14:44:24 -06:00
|
|
|
EXPECT_EQ(threads[0].name, "ProcessReaderChildThreadsTest-main");
|
|
|
|
|
2018-04-30 12:57:03 -07:00
|
|
|
for (size_t i = 1; i < 6; ++i) {
|
|
|
|
ASSERT_GT(threads[i].stack_regions.size(), 0u);
|
2019-08-19 10:15:58 -07:00
|
|
|
EXPECT_GT(threads[i].stack_regions[0].size(), 0u);
|
|
|
|
EXPECT_LE(threads[i].stack_regions[0].size(), i * 4096u);
|
2022-06-13 14:44:24 -06:00
|
|
|
EXPECT_EQ(threads[i].name,
|
|
|
|
base::StringPrintf("ProcessReaderChildThreadsTest-%zu", i));
|
2018-04-30 12:57:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-08 17:02:20 -08:00
|
|
|
// TODO(scottmg): US-553. ScopedTaskSuspend fails sometimes, with a 50ms
|
|
|
|
// timeout. Currently unclear how to make that more reliable, so disable the
|
|
|
|
// test for now as otherwise it flakes.
|
|
|
|
TEST(ProcessReaderFuchsia, DISABLED_ChildThreads) {
|
2018-04-30 12:57:03 -07:00
|
|
|
ThreadsChildTest test;
|
|
|
|
test.Run();
|
|
|
|
}
|
|
|
|
|
2018-02-22 11:06:35 -08:00
|
|
|
} // namespace
|
|
|
|
} // namespace test
|
|
|
|
} // namespace crashpad
|