libstrat: convert to experimental new (super-accurate) sf allocation semantics

This commit is contained in:
Michael Scire 2021-01-17 07:55:32 -08:00 committed by SciresM
parent 8314d015f3
commit f06de12bea
149 changed files with 2852 additions and 1746 deletions

View file

@ -31,7 +31,7 @@ namespace ams::sf::cmif {
}
}
void ServerDomainManager::Domain::DestroySelf() {
void ServerDomainManager::Domain::DisposeImpl() {
ServerDomainManager *manager = this->manager;
this->~Domain();
manager->FreeDomain(this);

View file

@ -16,16 +16,15 @@
#include <stratosphere.hpp>
#include "sf_hipc_mitm_query_api.hpp"
#define AMS_SF_HIPC_IMPL_I_MITM_QUERY_SERVICE_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 65000, void, ShouldMitm, (sf::Out<bool> out, const sm::MitmProcessInfo &client_info), (out, client_info))
AMS_SF_DEFINE_INTERFACE(ams::sf::hipc::impl, IMitmQueryService, AMS_SF_HIPC_IMPL_I_MITM_QUERY_SERVICE_INTERFACE_INFO)
namespace ams::sf::hipc::impl {
namespace {
#define AMS_SF_HIPC_IMPL_I_MITM_QUERY_SERVICE_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 65000, void, ShouldMitm, (sf::Out<bool> out, const sm::MitmProcessInfo &client_info))
AMS_SF_DEFINE_INTERFACE(IMitmQueryService, AMS_SF_HIPC_IMPL_I_MITM_QUERY_SERVICE_INTERFACE_INFO)
class MitmQueryService {
private:
ServerManagerBase::MitmQueryFunction query_function;
@ -33,23 +32,22 @@ namespace ams::sf::hipc::impl {
MitmQueryService(ServerManagerBase::MitmQueryFunction qf) : query_function(qf) { /* ... */ }
void ShouldMitm(sf::Out<bool> out, const sm::MitmProcessInfo &client_info) {
out.SetValue(this->query_function(client_info));
*out = this->query_function(client_info);
}
};
static_assert(IsIMitmQueryService<MitmQueryService>);
/* Globals. */
os::Mutex g_query_server_lock(false);
bool g_constructed_server = false;
bool g_registered_any = false;
constinit os::SdkMutex g_query_server_lock;
constinit bool g_constructed_server = false;
constinit bool g_registered_any = false;
void QueryServerProcessThreadMain(void *query_server) {
reinterpret_cast<ServerManagerBase *>(query_server)->LoopProcess();
}
constexpr size_t QueryServerProcessThreadStackSize = 0x4000;
alignas(os::ThreadStackAlignment) u8 g_server_process_thread_stack[QueryServerProcessThreadStackSize];
os::ThreadType g_query_server_process_thread;
alignas(os::ThreadStackAlignment) constinit u8 g_server_process_thread_stack[16_KB];
constinit os::ThreadType g_query_server_process_thread;
constexpr size_t MaxServers = 0;
TYPED_STORAGE(sf::hipc::ServerManager<MaxServers>) g_query_server_storage;
@ -59,13 +57,13 @@ namespace ams::sf::hipc::impl {
void RegisterMitmQueryHandle(Handle query_handle, ServerManagerBase::MitmQueryFunction query_func) {
std::scoped_lock lk(g_query_server_lock);
if (AMS_UNLIKELY(!g_constructed_server)) {
new (GetPointer(g_query_server_storage)) sf::hipc::ServerManager<MaxServers>();
g_constructed_server = true;
}
R_ABORT_UNLESS(GetPointer(g_query_server_storage)->RegisterSession(query_handle, cmif::ServiceObjectHolder(sf::MakeShared<IMitmQueryService, MitmQueryService>(query_func))));
/* TODO: Better object factory? */
R_ABORT_UNLESS(GetPointer(g_query_server_storage)->RegisterSession(query_handle, cmif::ServiceObjectHolder(sf::CreateSharedObjectEmplaced<IMitmQueryService, MitmQueryService>(query_func))));
if (AMS_UNLIKELY(!g_registered_any)) {
R_ABORT_UNLESS(os::CreateThread(std::addressof(g_query_server_process_thread), &QueryServerProcessThreadMain, GetPointer(g_query_server_storage), g_server_process_thread_stack, sizeof(g_server_process_thread_stack), AMS_GET_SYSTEM_THREAD_PRIORITY(mitm_sf, QueryServerProcessThread)));

View file

@ -15,20 +15,20 @@
*/
#include <stratosphere.hpp>
#define AMS_SF_HIPC_IMPL_I_HIPC_MANAGER_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 0, Result, ConvertCurrentObjectToDomain, (ams::sf::Out<ams::sf::cmif::DomainObjectId> out), (out)) \
AMS_SF_METHOD_INFO(C, H, 1, Result, CopyFromCurrentDomain, (ams::sf::OutMoveHandle out, ams::sf::cmif::DomainObjectId object_id), (out, object_id)) \
AMS_SF_METHOD_INFO(C, H, 2, Result, CloneCurrentObject, (ams::sf::OutMoveHandle out), (out)) \
AMS_SF_METHOD_INFO(C, H, 3, void, QueryPointerBufferSize, (ams::sf::Out<u16> out), (out)) \
AMS_SF_METHOD_INFO(C, H, 4, Result, CloneCurrentObjectEx, (ams::sf::OutMoveHandle out, u32 tag), (out, tag))
AMS_SF_DEFINE_INTERFACE(ams::sf::hipc::impl, IHipcManager, AMS_SF_HIPC_IMPL_I_HIPC_MANAGER_INTERFACE_INFO)
namespace ams::sf::hipc {
namespace impl {
#define AMS_SF_HIPC_IMPL_I_HIPC_MANAGER_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 0, Result, ConvertCurrentObjectToDomain, (ams::sf::Out<ams::sf::cmif::DomainObjectId> out)) \
AMS_SF_METHOD_INFO(C, H, 1, Result, CopyFromCurrentDomain, (ams::sf::OutMoveHandle out, ams::sf::cmif::DomainObjectId object_id)) \
AMS_SF_METHOD_INFO(C, H, 2, Result, CloneCurrentObject, (ams::sf::OutMoveHandle out)) \
AMS_SF_METHOD_INFO(C, H, 3, void, QueryPointerBufferSize, (ams::sf::Out<u16> out)) \
AMS_SF_METHOD_INFO(C, H, 4, Result, CloneCurrentObjectEx, (ams::sf::OutMoveHandle out, u32 tag))
AMS_SF_DEFINE_INTERFACE(IHipcManager, AMS_SF_HIPC_IMPL_I_HIPC_MANAGER_INTERFACE_INFO)
class HipcManager final {
class HipcManagerImpl {
private:
ServerDomainSessionManager *manager;
ServerSession *session;
@ -56,7 +56,7 @@ namespace ams::sf::hipc {
return ResultSuccess();
}
public:
explicit HipcManager(ServerDomainSessionManager *m, ServerSession *s) : manager(m), session(s), is_mitm_session(s->forward_service != nullptr) {
explicit HipcManagerImpl(ServerDomainSessionManager *m, ServerSession *s) : manager(m), session(s), is_mitm_session(s->forward_service != nullptr) {
/* ... */
}
@ -64,14 +64,14 @@ namespace ams::sf::hipc {
/* Allocate a domain. */
auto domain = this->manager->AllocateDomainServiceObject();
R_UNLESS(domain, sf::hipc::ResultOutOfDomains());
auto domain_guard = SCOPE_GUARD { cmif::ServerDomainManager::DestroyDomainServiceObject(static_cast<cmif::DomainServiceObject *>(domain)); };
/* Set up the new domain object. */
cmif::DomainObjectId object_id = cmif::InvalidDomainObjectId;
cmif::ServiceObjectHolder new_holder;
if (this->is_mitm_session) {
/* If we're a mitm session, we need to convert the remote session to domain. */
/* Make a new shared pointer to manage the allocated domain. */
SharedPointer<cmif::MitmDomainServiceObject> cmif_domain(static_cast<cmif::MitmDomainServiceObject *>(domain), false);
/* Convert the remote session to domain. */
AMS_ABORT_UNLESS(session->forward_service->own_handle);
R_TRY(serviceConvertToDomain(session->forward_service.get()));
@ -79,27 +79,28 @@ namespace ams::sf::hipc {
object_id = cmif::DomainObjectId{session->forward_service->object_id};
domain->ReserveSpecificIds(&object_id, 1);
/* Create new object. */
cmif::MitmDomainServiceObject *domain_ptr = static_cast<cmif::MitmDomainServiceObject *>(domain);
new_holder = cmif::ServiceObjectHolder(std::move(std::shared_ptr<cmif::MitmDomainServiceObject>(domain_ptr, cmif::ServerDomainManager::DestroyDomainServiceObject)));
/* Register the object. */
domain->RegisterObject(object_id, std::move(session->srv_obj_holder));
/* Set the new object holder. */
session->srv_obj_holder = cmif::ServiceObjectHolder(std::move(cmif_domain));
} else {
/* We're not a mitm session. Reserve a new object in the domain. */
/* Make a new shared pointer to manage the allocated domain. */
SharedPointer<cmif::DomainServiceObject> cmif_domain(domain, false);
/* Reserve a new object in the domain. */
R_TRY(domain->ReserveIds(&object_id, 1));
/* Create new object. */
cmif::DomainServiceObject *domain_ptr = static_cast<cmif::DomainServiceObject *>(domain);
new_holder = cmif::ServiceObjectHolder(std::move(std::shared_ptr<cmif::DomainServiceObject>(domain_ptr, cmif::ServerDomainManager::DestroyDomainServiceObject)));
/* Register the object. */
domain->RegisterObject(object_id, std::move(session->srv_obj_holder));
/* Set the new object holder. */
session->srv_obj_holder = cmif::ServiceObjectHolder(std::move(cmif_domain));
}
/* Return the allocated id. */
AMS_ABORT_UNLESS(object_id != cmif::InvalidDomainObjectId);
AMS_ABORT_UNLESS(static_cast<bool>(new_holder));
/* We succeeded! */
domain_guard.Cancel();
domain->RegisterObject(object_id, std::move(session->srv_obj_holder));
session->srv_obj_holder = std::move(new_holder);
out.SetValue(object_id);
*out = object_id;
return ResultSuccess();
}
@ -152,7 +153,7 @@ namespace ams::sf::hipc {
return this->CloneCurrentObjectImpl(out.GetHandlePointer(), this->manager->GetSessionManagerByTag(tag));
}
};
static_assert(IsIHipcManager<HipcManager>);
static_assert(IsIHipcManager<HipcManagerImpl>);
}
@ -160,8 +161,8 @@ namespace ams::sf::hipc {
/* Make a stack object, and pass a shared pointer to it to DispatchRequest. */
/* Note: This is safe, as no additional references to the hipc manager can ever be stored. */
/* The shared pointer to stack object is definitely gross, though. */
impl::HipcManager hipc_manager(this, session);
return this->DispatchRequest(cmif::ServiceObjectHolder(sf::GetSharedPointerTo<impl::IHipcManager>(hipc_manager)), session, in_message, out_message);
UnmanagedServiceObject<impl::IHipcManager, impl::HipcManagerImpl> hipc_manager(this, session);
return this->DispatchRequest(cmif::ServiceObjectHolder(hipc_manager.GetShared()), session, in_message, out_message);
}
}

View file

@ -18,8 +18,6 @@
namespace ams::sf::hipc {
ServerManagerBase::ServerBase::~ServerBase() { /* Pure virtual destructor, to prevent linker errors. */ }
Result ServerManagerBase::InstallMitmServerImpl(Handle *out_port_handle, sm::ServiceName service_name, ServerManagerBase::MitmQueryFunction query_func) {
/* Install the Mitm. */
Handle query_handle;
@ -89,37 +87,25 @@ namespace ams::sf::hipc {
Result ServerManagerBase::ProcessForServer(os::WaitableHolderType *holder) {
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetWaitableHolderUserData(holder)) == UserDataTag::Server);
ServerBase *server = static_cast<ServerBase *>(holder);
Server *server = static_cast<Server *>(holder);
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
/* Create resources for new session. */
cmif::ServiceObjectHolder obj;
std::shared_ptr<::Service> fsrv;
server->CreateSessionObjectHolder(&obj, &fsrv);
/* Not a mitm server, so we must have no forward service. */
AMS_ABORT_UNLESS(fsrv == nullptr);
/* Try to accept. */
return this->AcceptSession(server->port_handle, std::move(obj));
/* Create new session. */
if (server->static_object) {
return this->AcceptSession(server->port_handle, server->static_object.Clone());
} else {
return this->OnNeedsToAccept(server->index, server);
}
}
Result ServerManagerBase::ProcessForMitmServer(os::WaitableHolderType *holder) {
AMS_ABORT_UNLESS(static_cast<UserDataTag>(os::GetWaitableHolderUserData(holder)) == UserDataTag::MitmServer);
ServerBase *server = static_cast<ServerBase *>(holder);
Server *server = static_cast<Server *>(holder);
ON_SCOPE_EXIT { this->RegisterToWaitList(server); };
/* Create resources for new session. */
cmif::ServiceObjectHolder obj;
std::shared_ptr<::Service> fsrv;
server->CreateSessionObjectHolder(&obj, &fsrv);
/* Mitm server, so we must have forward service. */
AMS_ABORT_UNLESS(fsrv != nullptr);
/* Try to accept. */
return this->AcceptMitmSession(server->port_handle, std::move(obj), std::move(fsrv));
return this->OnNeedsToAccept(server->index, server);
}
Result ServerManagerBase::ProcessForSession(os::WaitableHolderType *holder) {

View file

@ -0,0 +1,161 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
namespace ams::sf {
namespace {
struct DefaultAllocatorImpl {
os::SdkMutexType tls_lock;
std::atomic_bool tls_allocated;
os::TlsSlot current_mr_tls_slot;
MemoryResource *default_mr;
void EnsureCurrentMemoryResourceTlsSlotInitialized() {
if (!tls_allocated.load(std::memory_order_acquire)) {
os::LockSdkMutex(std::addressof(tls_lock));
if (!tls_allocated.load(std::memory_order_relaxed)) {
R_ABORT_UNLESS(os::SdkAllocateTlsSlot(std::addressof(current_mr_tls_slot), nullptr));
tls_allocated.store(true, std::memory_order_release);
}
os::UnlockSdkMutex(std::addressof(tls_lock));
}
}
MemoryResource *GetDefaultMemoryResource() {
return default_mr;
}
MemoryResource *SetDefaultMemoryResource(MemoryResource *mr) {
return util::Exchange(std::addressof(default_mr), mr);
}
MemoryResource *GetCurrentMemoryResource() {
EnsureCurrentMemoryResourceTlsSlotInitialized();
return reinterpret_cast<MemoryResource *>(os::GetTlsValue(current_mr_tls_slot));
}
MemoryResource *SetCurrentMemoryResource(MemoryResource *mr) {
EnsureCurrentMemoryResourceTlsSlotInitialized();
auto ret = reinterpret_cast<MemoryResource *>(os::GetTlsValue(current_mr_tls_slot));
os::SetTlsValue(current_mr_tls_slot, reinterpret_cast<uintptr_t>(mr));
return ret;
}
MemoryResource *GetCurrentEffectiveMemoryResourceImpl() {
if (auto mr = GetCurrentMemoryResource(); mr != nullptr) {
return mr;
}
if (auto mr = GetGlobalDefaultMemoryResource(); mr != nullptr) {
return mr;
}
return nullptr;
}
};
constinit DefaultAllocatorImpl g_default_allocator_impl = {};
inline void *DefaultAllocate(size_t size, size_t align) {
return ::operator new(size, std::nothrow);
}
inline void DefaultDeallocate(void *ptr, size_t size, size_t align) {
return ::operator delete(ptr, std::nothrow);
}
class NewDeleteMemoryResource final : public MemoryResource {
private:
virtual void *AllocateImpl(size_t size, size_t alignment) override {
return DefaultAllocate(size, alignment);
}
virtual void DeallocateImpl(void *buffer, size_t size, size_t alignment) override {
return DefaultDeallocate(buffer, size, alignment);
}
virtual bool IsEqualImpl(const MemoryResource &resource) const {
return this == std::addressof(resource);
}
};
constinit NewDeleteMemoryResource g_new_delete_memory_resource;
}
namespace impl {
void *DefaultAllocateImpl(size_t size, size_t align, size_t offset) {
auto mr = g_default_allocator_impl.GetCurrentEffectiveMemoryResourceImpl();
auto h = mr != nullptr ? mr->allocate(size, align) : DefaultAllocate(size, align);
if (h == nullptr) {
return nullptr;
}
*static_cast<MemoryResource **>(h) = mr;
return static_cast<u8 *>(h) + offset;
}
void DefaultDeallocateImpl(void *ptr, size_t size, size_t align, size_t offset) {
if (ptr == nullptr) {
return;
}
auto h = static_cast<u8 *>(ptr) - offset;
if (auto mr = *reinterpret_cast<MemoryResource **>(h); mr != nullptr) {
return mr->deallocate(h, size, align);
} else {
return DefaultDeallocate(h, size, align);
}
}
}
MemoryResource *GetGlobalDefaultMemoryResource() {
return g_default_allocator_impl.GetDefaultMemoryResource();
}
MemoryResource *GetCurrentEffectiveMemoryResource() {
if (auto mr = g_default_allocator_impl.GetCurrentEffectiveMemoryResourceImpl(); mr != nullptr) {
return mr;
}
return GetNewDeleteMemoryResource();
}
MemoryResource *GetCurrentMemoryResource() {
return g_default_allocator_impl.GetCurrentMemoryResource();
}
MemoryResource *GetNewDeleteMemoryResource() {
return std::addressof(g_new_delete_memory_resource);
}
MemoryResource *SetGlobalDefaultMemoryResource(MemoryResource *mr) {
return g_default_allocator_impl.SetDefaultMemoryResource(mr);
}
MemoryResource *SetCurrentMemoryResource(MemoryResource *mr) {
return g_default_allocator_impl.SetCurrentMemoryResource(mr);
}
ScopedCurrentMemoryResourceSetter::ScopedCurrentMemoryResourceSetter(MemoryResource *mr) : m_prev(g_default_allocator_impl.GetCurrentMemoryResource()) {
os::SetTlsValue(g_default_allocator_impl.current_mr_tls_slot, reinterpret_cast<uintptr_t>(mr));
}
ScopedCurrentMemoryResourceSetter::~ScopedCurrentMemoryResourceSetter() {
os::SetTlsValue(g_default_allocator_impl.current_mr_tls_slot, reinterpret_cast<uintptr_t>(m_prev));
}
}