From 98ea14e8804b847c3b89fd931370c200804d24ab Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Sat, 16 Jun 2018 17:09:23 +0200 Subject: [PATCH] fs.mitm: Simplify initialization --- .../fs_mitm/source/imitmserviceobject.hpp | 8 ++-- stratosphere/fs_mitm/source/mitm_session.hpp | 22 +++++----- .../include/stratosphere/ipcsession.hpp | 8 +--- .../include/stratosphere/isession.hpp | 41 ++++++------------- 4 files changed, 30 insertions(+), 49 deletions(-) diff --git a/stratosphere/fs_mitm/source/imitmserviceobject.hpp b/stratosphere/fs_mitm/source/imitmserviceobject.hpp index eea88363b..30cdd052e 100644 --- a/stratosphere/fs_mitm/source/imitmserviceobject.hpp +++ b/stratosphere/fs_mitm/source/imitmserviceobject.hpp @@ -9,10 +9,10 @@ class IMitMServiceObject : public IServiceObject { protected: Service *forward_service; - u64 process_id; - u64 title_id; + u64 process_id = 0; + u64 title_id = 0; public: - IMitMServiceObject(Service *s) : forward_service(s), process_id(0), title_id(0) { + IMitMServiceObject(Service *s) : forward_service(s) { } @@ -22,7 +22,7 @@ class IMitMServiceObject : public IServiceObject { virtual void clone_to(void *o) = 0; protected: - virtual ~IMitMServiceObject() { } + virtual ~IMitMServiceObject() = default; virtual Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0; virtual void postprocess(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) = 0; virtual Result handle_deferred() = 0; diff --git a/stratosphere/fs_mitm/source/mitm_session.hpp b/stratosphere/fs_mitm/source/mitm_session.hpp index 3e3a778cd..e1afef72e 100644 --- a/stratosphere/fs_mitm/source/mitm_session.hpp +++ b/stratosphere/fs_mitm/source/mitm_session.hpp @@ -19,33 +19,35 @@ class MitMSession final : public ISession { /* This will be for the actual session. */ Service forward_service; IpcParsedCommand cur_out_r; - u32 mitm_domain_id; + u32 mitm_domain_id = 0; bool got_first_message; public: - MitMSession(MitMServer *s, Handle s_h, Handle c_h, const char *srv) : ISession(s, s_h, c_h, NULL, 0), mitm_domain_id(0), got_first_message(false) { + MitMSession(MitMServer *s, Handle s_h, Handle c_h, const char *srv) : ISession(s, s_h, c_h, NULL, 0), got_first_message(false) { this->server = s; this->server_handle = s_h; this->client_handle = c_h; if (R_FAILED(smMitMGetService(&forward_service, srv))) { /* TODO: Panic. */ } - if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &this->pointer_buffer_size))) { + size_t pointer_buffer_size = 0; + if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) { /* TODO: Panic. */ } this->service_object = std::make_shared(&forward_service); - this->pointer_buffer = new char[this->pointer_buffer_size]; + this->pointer_buffer.resize(pointer_buffer_size); } - MitMSession(MitMServer *s, Handle s_h, Handle c_h, Handle f_h) : ISession(s, s_h, c_h, NULL, 0), mitm_domain_id(0), got_first_message(true) { + MitMSession(MitMServer *s, Handle s_h, Handle c_h, Handle f_h) : ISession(s, s_h, c_h, NULL, 0), got_first_message(true) { this->server = s; this->server_handle = s_h; this->client_handle = c_h; serviceCreate(&this->forward_service, f_h); - if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &this->pointer_buffer_size))) { + size_t pointer_buffer_size = 0; + if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) { /* TODO: Panic. */ } this->service_object = std::make_shared(&forward_service); - this->pointer_buffer = new char[this->pointer_buffer_size]; + this->pointer_buffer.resize(pointer_buffer_size); } virtual ~MitMSession() { @@ -92,7 +94,7 @@ class MitMSession final : public ISession { obj = this->service_object; } if (obj != nullptr) { - retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size); + retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size()); if (R_SUCCEEDED(retval)) { if (r.IsDomainMessage) { ipcParseForDomain(&cur_out_r); @@ -197,7 +199,7 @@ class MitMSession final : public ISession { if (this->active_object == this->service_object && (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext)) { IpcCommand c; ipcInitialize(&c); - this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size); + this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size()); } else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) { if (cmd_id == IpcCtrl_Cmd_ConvertCurrentObjectToDomain) { this->is_domain = true; @@ -226,4 +228,4 @@ class MitMSession final : public ISession { } } } -}; \ No newline at end of file +}; diff --git a/stratosphere/libstratosphere/include/stratosphere/ipcsession.hpp b/stratosphere/libstratosphere/include/stratosphere/ipcsession.hpp index e39677237..9983a1535 100644 --- a/stratosphere/libstratosphere/include/stratosphere/ipcsession.hpp +++ b/stratosphere/libstratosphere/include/stratosphere/ipcsession.hpp @@ -18,9 +18,7 @@ class IPCSession final : public ISession { fatalSimple(rc); } this->service_object = std::make_shared(); - this->pointer_buffer_size = pbs; - this->pointer_buffer = new char[this->pointer_buffer_size]; - this->is_domain = false; + this->pointer_buffer.resize(pbs); } IPCSession(std::shared_ptr so, size_t pbs = 0x400) : ISession(NULL, 0, 0, so, 0) { @@ -28,8 +26,6 @@ class IPCSession final : public ISession { if (R_FAILED((rc = svcCreateSession(&this->server_handle, &this->client_handle, 0, 0)))) { fatalSimple(rc); } - this->pointer_buffer_size = pbs; - this->pointer_buffer = new char[this->pointer_buffer_size]; - this->is_domain = false; + this->pointer_buffer.resize(pbs); } }; diff --git a/stratosphere/libstratosphere/include/stratosphere/isession.hpp b/stratosphere/libstratosphere/include/stratosphere/isession.hpp index 52df6c426..c0e44d0b2 100644 --- a/stratosphere/libstratosphere/include/stratosphere/isession.hpp +++ b/stratosphere/libstratosphere/include/stratosphere/isession.hpp @@ -17,7 +17,6 @@ enum IpcControlCommand { IpcCtrl_Cmd_CloneCurrentObjectEx = 4 }; -#define POINTER_BUFFER_SIZE_MAX 0xFFFF #define RESULT_DEFER_SESSION (0x6580A) @@ -34,39 +33,23 @@ class ISession : public IWaitable { IServer *server; Handle server_handle; Handle client_handle; - char *pointer_buffer; - size_t pointer_buffer_size; + std::vector pointer_buffer; - bool is_domain; + bool is_domain = false; std::shared_ptr domain; std::shared_ptr active_object; - static_assert(sizeof(pointer_buffer) <= POINTER_BUFFER_SIZE_MAX, "Incorrect Size for PointerBuffer!"); - public: - ISession(IServer *s, Handle s_h, Handle c_h, size_t pbs = 0x400) : server(s), server_handle(s_h), client_handle(c_h), pointer_buffer_size(pbs) { + ISession(IServer *s, Handle s_h, Handle c_h, size_t pbs = 0x400) : server(s), server_handle(s_h), client_handle(c_h), pointer_buffer(pbs) { this->service_object = std::make_shared(); - if (this->pointer_buffer_size) { - this->pointer_buffer = new char[this->pointer_buffer_size]; - } - this->is_domain = false; - this->domain.reset(); - this->active_object.reset(); } - ISession(IServer *s, Handle s_h, Handle c_h, std::shared_ptr so, size_t pbs = 0x400) : service_object(so), server(s), server_handle(s_h), client_handle(c_h), pointer_buffer_size(pbs) { - if (this->pointer_buffer_size) { - this->pointer_buffer = new char[this->pointer_buffer_size]; - } - this->is_domain = false; - this->domain.reset(); - this->active_object.reset(); + ISession(IServer *s, Handle s_h, Handle c_h, std::shared_ptr so, size_t pbs = 0x400) : service_object(so), server(s), server_handle(s_h), client_handle(c_h), pointer_buffer(pbs) { } ~ISession() override { - delete this->pointer_buffer; if (server_handle) { svcCloseHandle(server_handle); } @@ -153,7 +136,7 @@ class ISession : public IWaitable { break; case IpcCommandType_Request: case IpcCommandType_RequestWithContext: - retval = this->active_object->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer, this->pointer_buffer_size); + retval = this->active_object->dispatch(r, c, cmd_id, (u8 *)pointer_buffer.data(), pointer_buffer.size()); break; case IpcCommandType_Control: case IpcCommandType_ControlWithContext: @@ -185,7 +168,7 @@ class ISession : public IWaitable { /* Prepare pointer buffer... */ IpcCommand c_for_reply; ipcInitialize(&c_for_reply); - ipcAddRecvStatic(&c_for_reply, this->pointer_buffer, this->pointer_buffer_size, 0); + ipcAddRecvStatic(&c_for_reply, this->pointer_buffer.data(), this->pointer_buffer.size(), 0); ipcPrepareHeader(&c_for_reply, 0); if (R_SUCCEEDED(rc = svcReplyAndReceive(&handle_index, &this->server_handle, 1, 0, U64_MAX))) { @@ -247,19 +230,19 @@ class ISession : public IWaitable { /* TODO: Implement. */ switch ((IpcControlCommand)cmd_id) { case IpcCtrl_Cmd_ConvertCurrentObjectToDomain: - rc = WrapIpcCommandImpl<&ISession::ConvertCurrentObjectToDomain>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size); + rc = WrapIpcCommandImpl<&ISession::ConvertCurrentObjectToDomain>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size()); break; case IpcCtrl_Cmd_CopyFromCurrentDomain: - rc = WrapIpcCommandImpl<&ISession::CopyFromCurrentDomain>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size); + rc = WrapIpcCommandImpl<&ISession::CopyFromCurrentDomain>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size()); break; case IpcCtrl_Cmd_CloneCurrentObject: - rc = WrapIpcCommandImpl<&ISession::CloneCurrentObject>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size); + rc = WrapIpcCommandImpl<&ISession::CloneCurrentObject>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size()); break; case IpcCtrl_Cmd_QueryPointerBufferSize: - rc = WrapIpcCommandImpl<&ISession::QueryPointerBufferSize>(this, r, out_c, (u8 *)this->pointer_buffer, this->pointer_buffer_size); + rc = WrapIpcCommandImpl<&ISession::QueryPointerBufferSize>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size()); break; case IpcCtrl_Cmd_CloneCurrentObjectEx: - rc = WrapIpcCommandImpl<&ISession::CloneCurrentObjectEx>(this, r, out_c, (u8 *)this->pointer_buffer, sizeof(this->pointer_buffer)); + rc = WrapIpcCommandImpl<&ISession::CloneCurrentObjectEx>(this, r, out_c, (u8 *)this->pointer_buffer.data(), pointer_buffer.size()); break; default: break; @@ -282,7 +265,7 @@ class ISession : public IWaitable { return {0xF601}; } std::tuple QueryPointerBufferSize() { - return {0x0, (u32)this->pointer_buffer_size}; + return {0x0, (u32)this->pointer_buffer.size()}; } std::tuple CloneCurrentObjectEx() { /* TODO */