Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
kallsyms committed Nov 16, 2023
1 parent deb3431 commit ecdb358
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 99 deletions.
12 changes: 5 additions & 7 deletions Source/santad/ProcessTree/Annotations/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
load("//:helper.bzl", "santa_unit_test")

package(
default_visibility = ["//:santa_package_group"],
)

cc_library(
name = "base",
hdrs = ["base.h"],
deps = [
"//Source/santad/ProcessTree:process_tree_cc_proto",
],
name = "base",
hdrs = ["base.h"],
deps = [
"//Source/santad/ProcessTree:process_tree_cc_proto",
],
)
67 changes: 35 additions & 32 deletions Source/santad/ProcessTree/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,33 @@ package(
)

cc_library(
name = "process",
hdrs = ["process.h"],
deps = [
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/status:statusor",
"//Source/santad/ProcessTree/Annotations:base",
],
name = "process",
hdrs = ["process.h"],
deps = [
"//Source/santad/ProcessTree/Annotations:base",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/synchronization",
],
)

objc_library(
name = "process_tree",
srcs = ["tree.cc", "tree_macos.mm"],
hdrs = ["tree.h"],
deps = [
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/status",
":process",
"//Source/santad/ProcessTree/Annotations:base",
],
name = "process_tree",
srcs = [
"tree.cc",
"tree_macos.mm",
],
hdrs = ["tree.h"],
sdk_dylibs = [
"bsm",
],
deps = [
":process",
"//Source/santad/ProcessTree/Annotations:base",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status",
"@com_google_absl//absl/synchronization",
],
)

proto_library(
Expand All @@ -43,21 +46,21 @@ cc_proto_library(
)

objc_library(
name = "tree_test_helpers",
srcs = ["tree_test_helpers.mm"],
hdrs = ["tree_test_helpers.h"],
deps = [
":process_tree",
"@com_google_absl//absl/synchronization",
],
name = "tree_test_helpers",
srcs = ["tree_test_helpers.mm"],
hdrs = ["tree_test_helpers.h"],
deps = [
":process_tree",
"@com_google_absl//absl/synchronization",
],
)

santa_unit_test(
name = "process_tree_test",
srcs = ["tree_test.mm"],
deps = [
":process",
":tree_test_helpers",
"//Source/santad/ProcessTree/Annotations:base",
],
name = "process_tree_test",
srcs = ["tree_test.mm"],
deps = [
":process",
":tree_test_helpers",
"//Source/santad/ProcessTree/Annotations:base",
],
)
57 changes: 29 additions & 28 deletions Source/santad/ProcessTree/tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,45 +111,46 @@ void ProcessTree::BackfillInsertChildren(

void ProcessTree::HandleFork(uint64_t timestamp, const Process &parent,
const pid new_pid) {
if (Step(timestamp)) {
std::shared_ptr<Process> child;
{
absl::MutexLock lock(&mtx_);
child = std::make_shared<Process>(new_pid, parent.effective_cred_,
parent.program_, map_[parent.pid_]);
map_.emplace(new_pid, child);
}
for (const auto &annotator : annotators_) {
annotator->AnnotateFork(*this, parent, *child);
}
if (Step(timestamp)) {
std::shared_ptr<Process> child;
{
absl::MutexLock lock(&mtx_);
child = std::make_shared<Process>(new_pid, parent.effective_cred_,
parent.program_, map_[parent.pid_]);
map_.emplace(new_pid, child);
}
for (const auto &annotator : annotators_) {
annotator->AnnotateFork(*this, parent, *child);
}
}
}

void ProcessTree::HandleExec(uint64_t timestamp, const Process &p,
const pid new_pid, const program prog,
const cred c) {
if (Step(timestamp)) {
// TODO(nickmg): should struct pid be reworked and only pid_version be passed?
assert(new_pid.pid == p.pid_.pid);

auto new_proc = std::make_shared<Process>(
new_pid, c, std::make_shared<const program>(prog), p.parent_);
{
absl::MutexLock lock(&mtx_);
remove_at_.push_back({timestamp, p.pid_});
map_.emplace(new_proc->pid_, new_proc);
}
for (const auto &annotator : annotators_) {
annotator->AnnotateExec(*this, p, *new_proc);
if (Step(timestamp)) {
// TODO(nickmg): should struct pid be reworked and only pid_version be
// passed?
assert(new_pid.pid == p.pid_.pid);

auto new_proc = std::make_shared<Process>(
new_pid, c, std::make_shared<const program>(prog), p.parent_);
{
absl::MutexLock lock(&mtx_);
remove_at_.push_back({timestamp, p.pid_});
map_.emplace(new_proc->pid_, new_proc);
}
for (const auto &annotator : annotators_) {
annotator->AnnotateExec(*this, p, *new_proc);
}
}
}
}

void ProcessTree::HandleExit(uint64_t timestamp, const Process &p) {
if (Step(timestamp)) {
absl::MutexLock lock(&mtx_);
remove_at_.push_back({timestamp, p.pid_});
}
absl::MutexLock lock(&mtx_);
remove_at_.push_back({timestamp, p.pid_});
}
}

bool ProcessTree::Step(uint64_t timestamp) {
Expand Down
4 changes: 2 additions & 2 deletions Source/santad/ProcessTree/tree_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace process_tree {
absl::StatusOr<Process> LoadPID(pid_t pid) {
// TODO
return absl::UnimplementedError("LoadPID not implemented");
// TODO
return absl::UnimplementedError("LoadPID not implemented");
}
} // namespace process_tree
27 changes: 12 additions & 15 deletions Source/santad/ProcessTree/tree_test.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,28 @@
class TestAnnotator : public Annotator {
public:
TestAnnotator() {}
void AnnotateFork(ProcessTree &tree, const Process &parent,
const Process &child) override;
void AnnotateFork(ProcessTree &tree, const Process &parent, const Process &child) override;
void AnnotateExec(ProcessTree &tree, const Process &orig_process,
const Process &new_process) override;
std::optional<pb::Annotations> Proto() const override;
};

void TestAnnotator::AnnotateFork(ProcessTree &tree, const Process &parent,
const Process &child) {
void TestAnnotator::AnnotateFork(ProcessTree &tree, const Process &parent, const Process &child) {
// "Base case". Propagate existing annotations down to descendants.
if (auto annotation = tree.GetAnnotation<TestAnnotator>(parent)) {
tree.AnnotateProcess(child, std::move(*annotation));
}
}

void TestAnnotator::AnnotateExec(ProcessTree &tree,
const Process &orig_process,
const Process &new_process) {
void TestAnnotator::AnnotateExec(ProcessTree &tree, const Process &orig_process,
const Process &new_process) {
if (auto annotation = tree.GetAnnotation<TestAnnotator>(orig_process)) {
tree.AnnotateProcess(new_process, std::move(*annotation));
return;
}

if (new_process.program_->executable == kAnnotatedExecutable) {
tree.AnnotateProcess(
new_process,
std::make_shared<TestAnnotator>());
tree.AnnotateProcess(new_process, std::make_shared<TestAnnotator>());
}
}

Expand Down Expand Up @@ -96,8 +91,10 @@ - (void)testSimpleOps {

// PID 2.2: exec("/bin/bash") -> PID 2.3
const struct pid child_exec_pid = {.pid = 2, .pidversion = 3};
const struct program child_exec_prog = {.executable = "/bin/bash", .arguments = {"/bin/bash", "-i"}};
self.tree->HandleExec(event_id++, *child, child_exec_pid, child_exec_prog, child->effective_cred_);
const struct program child_exec_prog = {.executable = "/bin/bash",
.arguments = {"/bin/bash", "-i"}};
self.tree->HandleExec(event_id++, *child, child_exec_pid, child_exec_prog,
child->effective_cred_);

child_opt = self.tree->Get(child_exec_pid);
XCTAssertTrue(child_opt.has_value());
Expand All @@ -119,7 +116,8 @@ - (void)testAnnotation {

// PID 2.2: exec("/usr/bin/login") -> PID 2.3
const struct pid login_exec_pid = {.pid = 2, .pidversion = 3};
const struct program login_prog = {.executable = std::string(kAnnotatedExecutable), .arguments = {}};
const struct program login_prog = {.executable = std::string(kAnnotatedExecutable),
.arguments = {}};
auto login = *self.tree->Get(login_pid);
self.tree->HandleExec(event_id++, *login, login_exec_pid, login_prog, cred);

Expand Down Expand Up @@ -191,7 +189,7 @@ - (void)testRefcountCleanup {
// Even if we step far into the future, we should still be able to lookup
// the child.
for (int i = 0; i < 1000; i++) {
struct pid churn_pid = {.pid = 100+i, .pidversion = 100+i};
struct pid churn_pid = {.pid = 100 + i, .pidversion = 100 + i};
self.tree->HandleFork(event_id++, *self.init_proc, churn_pid);
auto child = self.tree->Get(child_pid);
XCTAssertTrue(child.has_value());
Expand All @@ -212,4 +210,3 @@ - (void)testRefcountCleanup {
}

@end

27 changes: 12 additions & 15 deletions Source/santad/ProcessTree/tree_test_helpers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,21 @@

namespace process_tree {
class ProcessTreeTestPeer : public ProcessTree {
public:
std::shared_ptr<const Process> InsertInit();
public:
std::shared_ptr<const Process> InsertInit();
};

std::shared_ptr<const Process> ProcessTreeTestPeer::InsertInit() {
absl::MutexLock lock(&mtx_);
struct pid initpid = {
.pid = 1,
.pidversion = 1,
};
auto proc = std::make_shared<Process>(
initpid, (cred){.uid = 0, .gid = 0},
std::make_shared<program>((program){.executable = "/init", .arguments = {"/init"}}),
nullptr
);
map_.emplace(initpid,
proc);
return proc;
absl::MutexLock lock(&mtx_);
struct pid initpid = {
.pid = 1,
.pidversion = 1,
};
auto proc = std::make_shared<Process>(
initpid, (cred){.uid = 0, .gid = 0},
std::make_shared<program>((program){.executable = "/init", .arguments = {"/init"}}), nullptr);
map_.emplace(initpid, proc);
return proc;
}

} // namespace process_tree

0 comments on commit ecdb358

Please sign in to comment.