From de4e5db3300cc77694ff154cf3e8a3ba9c9eaf78 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 24 Feb 2023 12:29:55 -0500 Subject: [PATCH 1/2] hid: avoid direct pointer access of transfer memory objects --- .../hid/controllers/console_sixaxis.cpp | 14 +++++----- .../service/hid/controllers/console_sixaxis.h | 13 ++++++--- .../hle/service/hid/controllers/palma.cpp | 2 +- src/core/hle/service/hid/controllers/palma.h | 2 +- src/core/hle/service/hid/hid.cpp | 5 ++-- src/core/hle/service/hid/hid.h | 10 +++++-- src/core/hle/service/hid/hidbus.cpp | 2 +- src/core/hle/service/hid/hidbus.h | 3 +-- .../hle/service/hid/hidbus/hidbus_base.cpp | 7 +++-- src/core/hle/service/hid/hidbus/hidbus_base.h | 12 ++++++--- src/core/hle/service/hid/hidbus/ringcon.cpp | 13 +++++---- src/core/hle/service/hid/hidbus/ringcon.h | 3 +-- src/core/hle/service/hid/hidbus/starlink.cpp | 6 ++--- src/core/hle/service/hid/hidbus/starlink.h | 3 +-- src/core/hle/service/hid/hidbus/stubbed.cpp | 7 +++-- src/core/hle/service/hid/hidbus/stubbed.h | 3 +-- src/core/hle/service/hid/irs.cpp | 8 ++---- src/core/hle/service/hid/irs.h | 8 +++++- .../hid/irsensor/image_transfer_processor.cpp | 27 +++++++++++-------- .../hid/irsensor/image_transfer_processor.h | 12 ++++++--- 20 files changed, 91 insertions(+), 69 deletions(-) diff --git a/src/core/hle/service/hid/controllers/console_sixaxis.cpp b/src/core/hle/service/hid/controllers/console_sixaxis.cpp index bb3cba910..478d38590 100644 --- a/src/core/hle/service/hid/controllers/console_sixaxis.cpp +++ b/src/core/hle/service/hid/controllers/console_sixaxis.cpp @@ -1,17 +1,18 @@ // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "core/core.h" #include "core/core_timing.h" #include "core/hid/emulated_console.h" #include "core/hid/hid_core.h" #include "core/hle/service/hid/controllers/console_sixaxis.h" +#include "core/memory.h" namespace Service::HID { constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3C200; -Controller_ConsoleSixAxis::Controller_ConsoleSixAxis(Core::HID::HIDCore& hid_core_, - u8* raw_shared_memory_) - : ControllerBase{hid_core_} { +Controller_ConsoleSixAxis::Controller_ConsoleSixAxis(Core::System& system_, u8* raw_shared_memory_) + : ControllerBase{system_.HIDCore()}, system{system_} { console = hid_core.GetEmulatedConsole(); static_assert(SHARED_MEMORY_OFFSET + sizeof(ConsoleSharedMemory) < shared_memory_size, "ConsoleSharedMemory is bigger than the shared memory"); @@ -26,7 +27,7 @@ void Controller_ConsoleSixAxis::OnInit() {} void Controller_ConsoleSixAxis::OnRelease() {} void Controller_ConsoleSixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing) { - if (!IsControllerActivated() || !is_transfer_memory_set) { + if (!IsControllerActivated() || transfer_memory == 0) { seven_sixaxis_lifo.buffer_count = 0; seven_sixaxis_lifo.buffer_tail = 0; return; @@ -59,11 +60,10 @@ void Controller_ConsoleSixAxis::OnUpdate(const Core::Timing::CoreTiming& core_ti // Update seven six axis transfer memory seven_sixaxis_lifo.WriteNextEntry(next_seven_sixaxis_state); - std::memcpy(transfer_memory, &seven_sixaxis_lifo, sizeof(seven_sixaxis_lifo)); + system.Memory().WriteBlock(transfer_memory, &seven_sixaxis_lifo, sizeof(seven_sixaxis_lifo)); } -void Controller_ConsoleSixAxis::SetTransferMemoryPointer(u8* t_mem) { - is_transfer_memory_set = true; +void Controller_ConsoleSixAxis::SetTransferMemoryAddress(VAddr t_mem) { transfer_memory = t_mem; } diff --git a/src/core/hle/service/hid/controllers/console_sixaxis.h b/src/core/hle/service/hid/controllers/console_sixaxis.h index 2fd11538f..8d3e4081b 100644 --- a/src/core/hle/service/hid/controllers/console_sixaxis.h +++ b/src/core/hle/service/hid/controllers/console_sixaxis.h @@ -10,6 +10,10 @@ #include "core/hle/service/hid/controllers/controller_base.h" #include "core/hle/service/hid/ring_lifo.h" +namespace Core { +class System; +} // namespace Core + namespace Core::HID { class EmulatedConsole; } // namespace Core::HID @@ -17,7 +21,7 @@ class EmulatedConsole; namespace Service::HID { class Controller_ConsoleSixAxis final : public ControllerBase { public: - explicit Controller_ConsoleSixAxis(Core::HID::HIDCore& hid_core_, u8* raw_shared_memory_); + explicit Controller_ConsoleSixAxis(Core::System& system_, u8* raw_shared_memory_); ~Controller_ConsoleSixAxis() override; // Called when the controller is initialized @@ -30,7 +34,7 @@ public: void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; // Called on InitializeSevenSixAxisSensor - void SetTransferMemoryPointer(u8* t_mem); + void SetTransferMemoryAddress(VAddr t_mem); // Called on ResetSevenSixAxisSensorTimestamp void ResetTimestamp(); @@ -62,12 +66,13 @@ private: static_assert(sizeof(seven_sixaxis_lifo) == 0xA70, "SevenSixAxisState is an invalid size"); SevenSixAxisState next_seven_sixaxis_state{}; - u8* transfer_memory = nullptr; + VAddr transfer_memory{}; ConsoleSharedMemory* shared_memory = nullptr; Core::HID::EmulatedConsole* console = nullptr; - bool is_transfer_memory_set = false; u64 last_saved_timestamp{}; u64 last_global_timestamp{}; + + Core::System& system; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/palma.cpp b/src/core/hle/service/hid/controllers/palma.cpp index 4564ea1e2..bce51285c 100644 --- a/src/core/hle/service/hid/controllers/palma.cpp +++ b/src/core/hle/service/hid/controllers/palma.cpp @@ -152,7 +152,7 @@ Result Controller_Palma::WritePalmaRgbLedPatternEntry(const PalmaConnectionHandl } Result Controller_Palma::WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave, - u8* t_mem, u64 size) { + VAddr t_mem, u64 size) { if (handle.npad_id != active_handle.npad_id) { return InvalidPalmaHandle; } diff --git a/src/core/hle/service/hid/controllers/palma.h b/src/core/hle/service/hid/controllers/palma.h index 1d7fc94e1..cf62f0dbc 100644 --- a/src/core/hle/service/hid/controllers/palma.h +++ b/src/core/hle/service/hid/controllers/palma.h @@ -125,7 +125,7 @@ public: Result ReadPalmaUniqueCode(const PalmaConnectionHandle& handle); Result SetPalmaUniqueCodeInvalid(const PalmaConnectionHandle& handle); Result WritePalmaRgbLedPatternEntry(const PalmaConnectionHandle& handle, u64 unknown); - Result WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave, u8* t_mem, + Result WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave, VAddr t_mem, u64 size); Result SetPalmaDataBaseIdentificationVersion(const PalmaConnectionHandle& handle, s32 database_id_version_); diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index eb3c45a58..48f7bbf95 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -1858,7 +1858,7 @@ void Hid::InitializeSevenSixAxisSensor(Kernel::HLERequestContext& ctx) { .ActivateController(); applet_resource->GetController(HidController::ConsoleSixAxisSensor) - .SetTransferMemoryPointer(system.Memory().GetPointer(t_mem_1->GetSourceAddress())); + .SetTransferMemoryAddress(t_mem_1->GetSourceAddress()); LOG_WARNING(Service_HID, "called, t_mem_1_handle=0x{:08X}, t_mem_2_handle=0x{:08X}, " @@ -2145,8 +2145,7 @@ void Hid::WritePalmaWaveEntry(Kernel::HLERequestContext& ctx) { connection_handle.npad_id, wave_set, unknown, t_mem_handle, t_mem_size, size); applet_resource->GetController(HidController::Palma) - .WritePalmaWaveEntry(connection_handle, wave_set, - system.Memory().GetPointer(t_mem->GetSourceAddress()), t_mem_size); + .WritePalmaWaveEntry(connection_handle, wave_set, t_mem->GetSourceAddress(), t_mem_size); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultSuccess); diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h index b7c2a23ef..a397012a5 100644 --- a/src/core/hle/service/hid/hid.h +++ b/src/core/hle/service/hid/hid.h @@ -60,9 +60,15 @@ public: private: template void MakeController(HidController controller, u8* shared_memory) { - controllers[static_cast(controller)] = - std::make_unique(system.HIDCore(), shared_memory); + if constexpr (std::is_constructible_v) { + controllers[static_cast(controller)] = + std::make_unique(system, shared_memory); + } else { + controllers[static_cast(controller)] = + std::make_unique(system.HIDCore(), shared_memory); + } } + template void MakeControllerWithServiceContext(HidController controller, u8* shared_memory) { controllers[static_cast(controller)] = diff --git a/src/core/hle/service/hid/hidbus.cpp b/src/core/hle/service/hid/hidbus.cpp index bd94e8f3d..da1c8415c 100644 --- a/src/core/hle/service/hid/hidbus.cpp +++ b/src/core/hle/service/hid/hidbus.cpp @@ -472,7 +472,7 @@ void HidBus::EnableJoyPollingReceiveMode(Kernel::HLERequestContext& ctx) { if (device_index) { auto& device = devices[device_index.value()].device; device->SetPollingMode(polling_mode_); - device->SetTransferMemoryPointer(system.Memory().GetPointer(t_mem->GetSourceAddress())); + device->SetTransferMemoryAddress(t_mem->GetSourceAddress()); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ResultSuccess); diff --git a/src/core/hle/service/hid/hidbus.h b/src/core/hle/service/hid/hidbus.h index 8c687f678..9a4702021 100644 --- a/src/core/hle/service/hid/hidbus.h +++ b/src/core/hle/service/hid/hidbus.h @@ -115,8 +115,7 @@ private: void MakeDevice(BusHandle handle) { const auto device_index = GetDeviceIndexFromHandle(handle); if (device_index) { - devices[device_index.value()].device = - std::make_unique(system.HIDCore(), service_context); + devices[device_index.value()].device = std::make_unique(system, service_context); } } diff --git a/src/core/hle/service/hid/hidbus/hidbus_base.cpp b/src/core/hle/service/hid/hidbus/hidbus_base.cpp index b569b3c20..dfd23ec04 100644 --- a/src/core/hle/service/hid/hidbus/hidbus_base.cpp +++ b/src/core/hle/service/hid/hidbus/hidbus_base.cpp @@ -9,8 +9,8 @@ namespace Service::HID { -HidbusBase::HidbusBase(KernelHelpers::ServiceContext& service_context_) - : service_context(service_context_) { +HidbusBase::HidbusBase(Core::System& system_, KernelHelpers::ServiceContext& service_context_) + : system(system_), service_context(service_context_) { send_command_async_event = service_context.CreateEvent("hidbus:SendCommandAsyncEvent"); } HidbusBase::~HidbusBase() = default; @@ -59,8 +59,7 @@ void HidbusBase::DisablePollingMode() { polling_mode_enabled = false; } -void HidbusBase::SetTransferMemoryPointer(u8* t_mem) { - is_transfer_memory_set = true; +void HidbusBase::SetTransferMemoryAddress(VAddr t_mem) { transfer_memory = t_mem; } diff --git a/src/core/hle/service/hid/hidbus/hidbus_base.h b/src/core/hle/service/hid/hidbus/hidbus_base.h index 65e301137..26313264d 100644 --- a/src/core/hle/service/hid/hidbus/hidbus_base.h +++ b/src/core/hle/service/hid/hidbus/hidbus_base.h @@ -8,6 +8,10 @@ #include "common/common_types.h" #include "core/hle/result.h" +namespace Core { +class System; +} + namespace Kernel { class KEvent; class KReadableEvent; @@ -106,7 +110,7 @@ static_assert(sizeof(ButtonOnlyPollingDataAccessor) == 0x2F0, class HidbusBase { public: - explicit HidbusBase(KernelHelpers::ServiceContext& service_context_); + explicit HidbusBase(Core::System& system_, KernelHelpers::ServiceContext& service_context_); virtual ~HidbusBase(); void ActivateDevice(); @@ -134,7 +138,7 @@ public: void DisablePollingMode(); // Called on EnableJoyPollingReceiveMode - void SetTransferMemoryPointer(u8* t_mem); + void SetTransferMemoryAddress(VAddr t_mem); Kernel::KReadableEvent& GetSendCommandAsycEvent() const; @@ -170,9 +174,9 @@ protected: JoyEnableSixAxisDataAccessor enable_sixaxis_data{}; ButtonOnlyPollingDataAccessor button_only_data{}; - u8* transfer_memory{nullptr}; - bool is_transfer_memory_set{}; + VAddr transfer_memory{}; + Core::System& system; Kernel::KEvent* send_command_async_event; KernelHelpers::ServiceContext& service_context; }; diff --git a/src/core/hle/service/hid/hidbus/ringcon.cpp b/src/core/hle/service/hid/hidbus/ringcon.cpp index 35847cbdd..65a2dd521 100644 --- a/src/core/hle/service/hid/hidbus/ringcon.cpp +++ b/src/core/hle/service/hid/hidbus/ringcon.cpp @@ -1,18 +1,20 @@ // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include "core/core.h" #include "core/hid/emulated_controller.h" #include "core/hid/hid_core.h" #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_readable_event.h" #include "core/hle/service/hid/hidbus/ringcon.h" +#include "core/memory.h" namespace Service::HID { -RingController::RingController(Core::HID::HIDCore& hid_core_, +RingController::RingController(Core::System& system_, KernelHelpers::ServiceContext& service_context_) - : HidbusBase(service_context_) { - input = hid_core_.GetEmulatedController(Core::HID::NpadIdType::Player1); + : HidbusBase(system_, service_context_) { + input = system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Player1); } RingController::~RingController() = default; @@ -38,7 +40,7 @@ void RingController::OnUpdate() { return; } - if (!polling_mode_enabled || !is_transfer_memory_set) { + if (!polling_mode_enabled || transfer_memory == 0) { return; } @@ -62,7 +64,8 @@ void RingController::OnUpdate() { curr_entry.polling_data.out_size = sizeof(ringcon_value); std::memcpy(curr_entry.polling_data.data.data(), &ringcon_value, sizeof(ringcon_value)); - std::memcpy(transfer_memory, &enable_sixaxis_data, sizeof(enable_sixaxis_data)); + system.Memory().WriteBlock(transfer_memory, &enable_sixaxis_data, + sizeof(enable_sixaxis_data)); break; } default: diff --git a/src/core/hle/service/hid/hidbus/ringcon.h b/src/core/hle/service/hid/hidbus/ringcon.h index c2fb386b1..f42f3ea41 100644 --- a/src/core/hle/service/hid/hidbus/ringcon.h +++ b/src/core/hle/service/hid/hidbus/ringcon.h @@ -17,8 +17,7 @@ namespace Service::HID { class RingController final : public HidbusBase { public: - explicit RingController(Core::HID::HIDCore& hid_core_, - KernelHelpers::ServiceContext& service_context_); + explicit RingController(Core::System& system_, KernelHelpers::ServiceContext& service_context_); ~RingController() override; void OnInit() override; diff --git a/src/core/hle/service/hid/hidbus/starlink.cpp b/src/core/hle/service/hid/hidbus/starlink.cpp index d0e760314..36573274e 100644 --- a/src/core/hle/service/hid/hidbus/starlink.cpp +++ b/src/core/hle/service/hid/hidbus/starlink.cpp @@ -8,8 +8,8 @@ namespace Service::HID { constexpr u8 DEVICE_ID = 0x28; -Starlink::Starlink(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_) - : HidbusBase(service_context_) {} +Starlink::Starlink(Core::System& system_, KernelHelpers::ServiceContext& service_context_) + : HidbusBase(system_, service_context_) {} Starlink::~Starlink() = default; void Starlink::OnInit() { @@ -27,7 +27,7 @@ void Starlink::OnUpdate() { if (!device_enabled) { return; } - if (!polling_mode_enabled || !is_transfer_memory_set) { + if (!polling_mode_enabled || transfer_memory == 0) { return; } diff --git a/src/core/hle/service/hid/hidbus/starlink.h b/src/core/hle/service/hid/hidbus/starlink.h index 07c800e6e..a276aa88f 100644 --- a/src/core/hle/service/hid/hidbus/starlink.h +++ b/src/core/hle/service/hid/hidbus/starlink.h @@ -14,8 +14,7 @@ namespace Service::HID { class Starlink final : public HidbusBase { public: - explicit Starlink(Core::HID::HIDCore& hid_core_, - KernelHelpers::ServiceContext& service_context_); + explicit Starlink(Core::System& system_, KernelHelpers::ServiceContext& service_context_); ~Starlink() override; void OnInit() override; diff --git a/src/core/hle/service/hid/hidbus/stubbed.cpp b/src/core/hle/service/hid/hidbus/stubbed.cpp index 07632c872..8160b7218 100644 --- a/src/core/hle/service/hid/hidbus/stubbed.cpp +++ b/src/core/hle/service/hid/hidbus/stubbed.cpp @@ -8,9 +8,8 @@ namespace Service::HID { constexpr u8 DEVICE_ID = 0xFF; -HidbusStubbed::HidbusStubbed(Core::HID::HIDCore& hid_core_, - KernelHelpers::ServiceContext& service_context_) - : HidbusBase(service_context_) {} +HidbusStubbed::HidbusStubbed(Core::System& system_, KernelHelpers::ServiceContext& service_context_) + : HidbusBase(system_, service_context_) {} HidbusStubbed::~HidbusStubbed() = default; void HidbusStubbed::OnInit() { @@ -28,7 +27,7 @@ void HidbusStubbed::OnUpdate() { if (!device_enabled) { return; } - if (!polling_mode_enabled || !is_transfer_memory_set) { + if (!polling_mode_enabled || transfer_memory == 0) { return; } diff --git a/src/core/hle/service/hid/hidbus/stubbed.h b/src/core/hle/service/hid/hidbus/stubbed.h index 38eaa0ecc..2e58d42fc 100644 --- a/src/core/hle/service/hid/hidbus/stubbed.h +++ b/src/core/hle/service/hid/hidbus/stubbed.h @@ -14,8 +14,7 @@ namespace Service::HID { class HidbusStubbed final : public HidbusBase { public: - explicit HidbusStubbed(Core::HID::HIDCore& hid_core_, - KernelHelpers::ServiceContext& service_context_); + explicit HidbusStubbed(Core::System& system_, KernelHelpers::ServiceContext& service_context_); ~HidbusStubbed() override; void OnInit() override; diff --git a/src/core/hle/service/hid/irs.cpp b/src/core/hle/service/hid/irs.cpp index 3bd418e92..a40f61fde 100644 --- a/src/core/hle/service/hid/irs.cpp +++ b/src/core/hle/service/hid/irs.cpp @@ -208,8 +208,6 @@ void IRS::RunImageTransferProcessor(Kernel::HLERequestContext& ctx) { ASSERT_MSG(t_mem->GetSize() == parameters.transfer_memory_size, "t_mem has incorrect size"); - u8* transfer_memory = system.Memory().GetPointer(t_mem->GetSourceAddress()); - LOG_INFO(Service_IRS, "called, npad_type={}, npad_id={}, transfer_memory_size={}, transfer_memory_size={}, " "applet_resource_user_id={}", @@ -224,7 +222,7 @@ void IRS::RunImageTransferProcessor(Kernel::HLERequestContext& ctx) { auto& image_transfer_processor = GetProcessor(parameters.camera_handle); image_transfer_processor.SetConfig(parameters.processor_config); - image_transfer_processor.SetTransferMemoryPointer(transfer_memory); + image_transfer_processor.SetTransferMemoryAddress(t_mem->GetSourceAddress()); npad_device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex, Common::Input::PollingMode::IR); } @@ -448,8 +446,6 @@ void IRS::RunImageTransferExProcessor(Kernel::HLERequestContext& ctx) { auto t_mem = system.ApplicationProcess()->GetHandleTable().GetObject( t_mem_handle); - u8* transfer_memory = system.Memory().GetPointer(t_mem->GetSourceAddress()); - LOG_INFO(Service_IRS, "called, npad_type={}, npad_id={}, transfer_memory_size={}, " "applet_resource_user_id={}", @@ -464,7 +460,7 @@ void IRS::RunImageTransferExProcessor(Kernel::HLERequestContext& ctx) { auto& image_transfer_processor = GetProcessor(parameters.camera_handle); image_transfer_processor.SetConfig(parameters.processor_config); - image_transfer_processor.SetTransferMemoryPointer(transfer_memory); + image_transfer_processor.SetTransferMemoryAddress(t_mem->GetSourceAddress()); npad_device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex, Common::Input::PollingMode::IR); } diff --git a/src/core/hle/service/hid/irs.h b/src/core/hle/service/hid/irs.h index 2e6115c73..b76ad7854 100644 --- a/src/core/hle/service/hid/irs.h +++ b/src/core/hle/service/hid/irs.h @@ -80,7 +80,13 @@ private: LOG_CRITICAL(Service_IRS, "Invalid index {}", index); return; } - processors[index] = std::make_unique(system.HIDCore(), device_state, index); + + if constexpr (std::is_constructible_v) { + processors[index] = std::make_unique(system, device_state, index); + } else { + processors[index] = std::make_unique(system.HIDCore(), device_state, index); + } } template diff --git a/src/core/hle/service/hid/irsensor/image_transfer_processor.cpp b/src/core/hle/service/hid/irsensor/image_transfer_processor.cpp index 98f0c579d..bc896a1e3 100644 --- a/src/core/hle/service/hid/irsensor/image_transfer_processor.cpp +++ b/src/core/hle/service/hid/irsensor/image_transfer_processor.cpp @@ -1,16 +1,18 @@ // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project // SPDX-License-Identifier: GPL-3.0-or-later +#include "core/core.h" #include "core/hid/emulated_controller.h" #include "core/hid/hid_core.h" #include "core/hle/service/hid/irsensor/image_transfer_processor.h" +#include "core/memory.h" namespace Service::IRS { -ImageTransferProcessor::ImageTransferProcessor(Core::HID::HIDCore& hid_core_, +ImageTransferProcessor::ImageTransferProcessor(Core::System& system_, Core::IrSensor::DeviceFormat& device_format, std::size_t npad_index) - : device{device_format} { - npad_device = hid_core_.GetEmulatedControllerByIndex(npad_index); + : device{device_format}, system{system_} { + npad_device = system.HIDCore().GetEmulatedControllerByIndex(npad_index); Core::HID::ControllerUpdateCallback engine_callback{ .on_change = [this](Core::HID::ControllerTriggerType type) { OnControllerUpdate(type); }, @@ -43,7 +45,7 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType if (type != Core::HID::ControllerTriggerType::IrSensor) { return; } - if (!is_transfer_memory_set) { + if (transfer_memory == 0) { return; } @@ -56,14 +58,16 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType if (camera_data.format != current_config.origin_format) { LOG_WARNING(Service_IRS, "Wrong Input format {} expected {}", camera_data.format, current_config.origin_format); - memset(transfer_memory, 0, GetDataSize(current_config.trimming_format)); + system.Memory().ZeroBlock(*system.ApplicationProcess(), transfer_memory, + GetDataSize(current_config.trimming_format)); return; } if (current_config.origin_format > current_config.trimming_format) { LOG_WARNING(Service_IRS, "Origin format {} is smaller than trimming format {}", current_config.origin_format, current_config.trimming_format); - memset(transfer_memory, 0, GetDataSize(current_config.trimming_format)); + system.Memory().ZeroBlock(*system.ApplicationProcess(), transfer_memory, + GetDataSize(current_config.trimming_format)); return; } @@ -80,7 +84,8 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType "Trimming area ({}, {}, {}, {}) is outside of origin area ({}, {})", current_config.trimming_start_x, current_config.trimming_start_y, trimming_width, trimming_height, origin_width, origin_height); - memset(transfer_memory, 0, GetDataSize(current_config.trimming_format)); + system.Memory().ZeroBlock(*system.ApplicationProcess(), transfer_memory, + GetDataSize(current_config.trimming_format)); return; } @@ -94,7 +99,8 @@ void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType } } - memcpy(transfer_memory, window_data.data(), GetDataSize(current_config.trimming_format)); + system.Memory().WriteBlock(transfer_memory, window_data.data(), + GetDataSize(current_config.trimming_format)); if (!IsProcessorActive()) { StartProcessor(); @@ -134,8 +140,7 @@ void ImageTransferProcessor::SetConfig( npad_device->SetCameraFormat(current_config.origin_format); } -void ImageTransferProcessor::SetTransferMemoryPointer(u8* t_mem) { - is_transfer_memory_set = true; +void ImageTransferProcessor::SetTransferMemoryAddress(VAddr t_mem) { transfer_memory = t_mem; } @@ -143,7 +148,7 @@ Core::IrSensor::ImageTransferProcessorState ImageTransferProcessor::GetState( std::vector& data) const { const auto size = GetDataSize(current_config.trimming_format); data.resize(size); - memcpy(data.data(), transfer_memory, size); + system.Memory().ReadBlock(transfer_memory, data.data(), size); return processor_state; } diff --git a/src/core/hle/service/hid/irsensor/image_transfer_processor.h b/src/core/hle/service/hid/irsensor/image_transfer_processor.h index 393df492d..7cfe04c8c 100644 --- a/src/core/hle/service/hid/irsensor/image_transfer_processor.h +++ b/src/core/hle/service/hid/irsensor/image_transfer_processor.h @@ -7,6 +7,10 @@ #include "core/hid/irs_types.h" #include "core/hle/service/hid/irsensor/processor_base.h" +namespace Core { +class System; +} + namespace Core::HID { class EmulatedController; } // namespace Core::HID @@ -14,7 +18,7 @@ class EmulatedController; namespace Service::IRS { class ImageTransferProcessor final : public ProcessorBase { public: - explicit ImageTransferProcessor(Core::HID::HIDCore& hid_core_, + explicit ImageTransferProcessor(Core::System& system_, Core::IrSensor::DeviceFormat& device_format, std::size_t npad_index); ~ImageTransferProcessor() override; @@ -33,7 +37,7 @@ public: void SetConfig(Core::IrSensor::PackedImageTransferProcessorExConfig config); // Transfer memory where the image data will be stored - void SetTransferMemoryPointer(u8* t_mem); + void SetTransferMemoryAddress(VAddr t_mem); Core::IrSensor::ImageTransferProcessorState GetState(std::vector& data) const; @@ -67,7 +71,7 @@ private: Core::HID::EmulatedController* npad_device; int callback_key{}; - u8* transfer_memory = nullptr; - bool is_transfer_memory_set = false; + Core::System& system; + VAddr transfer_memory{}; }; } // namespace Service::IRS From 975186ad4d7c67b22f580986979d0520530b41c3 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 24 Feb 2023 12:50:54 -0500 Subject: [PATCH 2/2] am: avoid direct pointer access of transfer memory objects --- src/core/hle/service/am/am.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index beb2da06e..adb482941 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1260,9 +1260,8 @@ void ILibraryAppletCreator::CreateTransferMemoryStorage(Kernel::HLERequestContex return; } - const u8* const mem_begin = system.Memory().GetPointer(transfer_mem->GetSourceAddress()); - const u8* const mem_end = mem_begin + transfer_mem->GetSize(); - std::vector memory{mem_begin, mem_end}; + std::vector memory(transfer_mem->GetSize()); + system.Memory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(), memory.size()); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess); @@ -1294,9 +1293,8 @@ void ILibraryAppletCreator::CreateHandleStorage(Kernel::HLERequestContext& ctx) return; } - const u8* const mem_begin = system.Memory().GetPointer(transfer_mem->GetSourceAddress()); - const u8* const mem_end = mem_begin + transfer_mem->GetSize(); - std::vector memory{mem_begin, mem_end}; + std::vector memory(transfer_mem->GetSize()); + system.Memory().ReadBlock(transfer_mem->GetSourceAddress(), memory.data(), memory.size()); IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(ResultSuccess);