Skip to content

Commit

Permalink
Kernel: Make Process::start_tracing_from API OOM safe
Browse files Browse the repository at this point in the history
Modify the API so it's possible to propagate error on OOM failure.
NonnullOwnPtr<T> is not appropriate for the ThreadTracer::create() API,
so switch to OwnPtr<T>, use adopt_own_if_nonnull() to handle creation.
  • Loading branch information
bgianfo authored and awesomekling committed May 13, 2021
1 parent dba261f commit 956314f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,13 @@ void Process::set_tty(TTY* tty)
m_tty = tty;
}

void Process::start_tracing_from(ProcessID tracer)
KResult Process::start_tracing_from(ProcessID tracer)
{
m_tracer = ThreadTracer::create(tracer);
auto thread_tracer = ThreadTracer::create(tracer);
if (!thread_tracer)
return ENOMEM;
m_tracer = move(thread_tracer);
return KSuccess;
}

void Process::stop_tracing()
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class Process

ThreadTracer* tracer() { return m_tracer.ptr(); }
bool is_traced() const { return !!m_tracer; }
void start_tracing_from(ProcessID tracer);
KResult start_tracing_from(ProcessID tracer);
void stop_tracing();
void tracer_trap(Thread&, const RegisterState&);

Expand Down
4 changes: 3 additions & 1 deletion Kernel/Syscalls/ptrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ static KResultOr<u32> handle_ptrace(const Kernel::Syscall::SC_ptrace_params& par
if (peer_process.tracer()) {
return EBUSY;
}
peer_process.start_tracing_from(caller.pid());
auto result = peer_process.start_tracing_from(caller.pid());
if (result.is_error())
return result.error();
ScopedSpinLock lock(peer->get_lock());
if (peer->state() != Thread::State::Stopped) {
peer->send_signal(SIGSTOP, &caller);
Expand Down
4 changes: 2 additions & 2 deletions Kernel/ThreadTracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

#pragma once

#include <AK/NonnullOwnPtr.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <Kernel/UnixTypes.h>
#include <LibC/sys/arch/i386/regs.h>

namespace Kernel {

class ThreadTracer {
public:
static NonnullOwnPtr<ThreadTracer> create(ProcessID tracer) { return make<ThreadTracer>(tracer); }
static OwnPtr<ThreadTracer> create(ProcessID tracer) { return adopt_own_if_nonnull(new ThreadTracer(tracer)); }

ProcessID tracer_pid() const { return m_tracer_pid; }
bool has_pending_signal(u32 signal) const { return m_pending_signals & (1 << (signal - 1)); }
Expand Down

0 comments on commit 956314f

Please sign in to comment.