Skip to content

Commit

Permalink
debugger: Fix use of invalid pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Jun 4, 2023
1 parent 2e4bf9e commit 948ce12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
60 changes: 47 additions & 13 deletions rpcs3/rpcs3qt/debugger_frame.cpp
Expand Up @@ -767,21 +767,51 @@ cpu_thread* debugger_frame::get_cpu()
return m_rsx;
}

std::function<cpu_thread*()> debugger_frame::make_check_cpu(cpu_thread* cpu)
std::function<cpu_thread*()> debugger_frame::make_check_cpu(cpu_thread* cpu, bool unlocked)
{
const u32 id = cpu ? cpu->id : umax;
const u32 type = id >> 24;

std::shared_ptr<cpu_thread> shared = type == 1 ? static_cast<std::shared_ptr<cpu_thread>>(idm::get<named_thread<ppu_thread>>(id)) :
type == 2 ? idm::get<named_thread<spu_thread>>(id) : nullptr;
std::shared_ptr<cpu_thread> shared;

if (g_fxo->try_get<id_manager::id_map<named_thread<ppu_thread>>>() && g_fxo->try_get<id_manager::id_map<named_thread<spu_thread>>>())
{
if (unlocked)
{
if (type == 1)
{
shared = idm::get_unlocked<named_thread<ppu_thread>>(id);
}
else if (type == 2)
{
shared = idm::get_unlocked<named_thread<spu_thread>>(id);
}
}
else
{
if (type == 1)
{
shared = idm::get<named_thread<ppu_thread>>(id);
}
else if (type == 2)
{
shared = idm::get<named_thread<spu_thread>>(id);
}
}
}

if (shared.get() != cpu)
{
shared.reset();
}

return [&rsx = m_rsx, cpu, type, shared = std::move(shared)]() -> cpu_thread*
return [&rsx = m_rsx, cpu, type, shared = std::move(shared), emu_course = Emu.ProcureCurrentEmulationCourseInformation()]() -> cpu_thread*
{
if (emu_course != Emu.ProcureCurrentEmulationCourseInformation())
{
return nullptr;
}

if (type == 1 || type == 2)
{
// SPU and PPU
Expand Down Expand Up @@ -874,7 +904,7 @@ void debugger_frame::UpdateUI()
m_ui_update_ctr++;
}

using data_type = std::pair<cpu_thread*, u32>;
using data_type = std::function<cpu_thread*()>;

Q_DECLARE_METATYPE(data_type);

Expand All @@ -896,21 +926,18 @@ void debugger_frame::UpdateUnitList()
return;
}

//const int old_size = m_choice_units->count();
QVariant old_cpu = m_choice_units->currentData();

bool reselected = false;

const auto on_select = [&](u32 id, cpu_thread& cpu)
{
if (emu_state == system_state::stopped) return;

const QVariant var_cpu = QVariant::fromValue<data_type>(std::make_pair(&cpu, id));
const QVariant var_cpu = QVariant::fromValue<data_type>(make_check_cpu(std::addressof(cpu), true));

// Space at the end is to pad a gap on the right
m_choice_units->addItem(qstr((id >> 24 == 0x55 ? "RSX[0x55555555]" : cpu.get_name()) + ' '), var_cpu);

if (!reselected && old_cpu == var_cpu)
if (!reselected && old_cpu.canConvert<data_type>() && old_cpu.value<data_type>()() == std::addressof(cpu))
{
m_choice_units->setCurrentIndex(m_choice_units->count() - 1);
reselected = true;
Expand All @@ -923,8 +950,11 @@ void debugger_frame::UpdateUnitList()
m_choice_units->clear();
m_choice_units->addItem(NoThreadString);

idm::select<named_thread<ppu_thread>>(on_select);
idm::select<named_thread<spu_thread>>(on_select);
if (emu_state != system_state::stopped)
{
idm::select<named_thread<ppu_thread>>(on_select);
idm::select<named_thread<spu_thread>>(on_select);
}

if (const auto render = g_fxo->try_get<rsx::thread>(); emu_state != system_state::stopped && render && render->ctrl)
{
Expand Down Expand Up @@ -953,7 +983,9 @@ void debugger_frame::UpdateUnitList()

void debugger_frame::OnSelectUnit()
{
auto [selected, cpu_id] = m_choice_units->currentData().value<data_type>();
const QVariant data = m_choice_units->currentData();

cpu_thread* selected = data.canConvert<data_type>() ? data.value<data_type>()() : nullptr;

if (m_emu_state != system_state::stopped)
{
Expand Down Expand Up @@ -984,6 +1016,8 @@ void debugger_frame::OnSelectUnit()

if (selected)
{
const u32 cpu_id = selected->id;

switch (cpu_id >> 24)
{
case 1:
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/debugger_frame.h
Expand Up @@ -76,7 +76,7 @@ class debugger_frame : public custom_dock_widget
std::shared_ptr<gui_settings> m_gui_settings;

cpu_thread* get_cpu();
std::function<cpu_thread*()> make_check_cpu(cpu_thread* cpu);
std::function<cpu_thread*()> make_check_cpu(cpu_thread* cpu, bool unlocked = false);
void open_breakpoints_settings();

public:
Expand Down

0 comments on commit 948ce12

Please sign in to comment.