summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-06 12:58:03 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-06 13:06:05 +0200
commit6e3381ac32e5d251c124cc6147c4598dd2bac505 (patch)
tree108e6a04d89d5f9c483df0361079caea2424362c /Kernel
parente3a716ceffc0fc1f5c4f2ee35dee02e987731621 (diff)
downloadserenity-6e3381ac32e5d251c124cc6147c4598dd2bac505.zip
Kernel: Use KResultOr and TRY() for {Shared,Private}InodeVMObject
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/FileSystem/InodeFile.cpp6
-rw-r--r--Kernel/Memory/PrivateInodeVMObject.cpp4
-rw-r--r--Kernel/Memory/PrivateInodeVMObject.h2
-rw-r--r--Kernel/Memory/SharedInodeVMObject.cpp6
-rw-r--r--Kernel/Memory/SharedInodeVMObject.h2
-rw-r--r--Kernel/Syscalls/execve.cpp12
-rw-r--r--Kernel/Syscalls/mmap.cpp7
-rw-r--r--Kernel/Syscalls/ptrace.cpp6
8 files changed, 14 insertions, 31 deletions
diff --git a/Kernel/FileSystem/InodeFile.cpp b/Kernel/FileSystem/InodeFile.cpp
index 5cf971a471..47373cebf5 100644
--- a/Kernel/FileSystem/InodeFile.cpp
+++ b/Kernel/FileSystem/InodeFile.cpp
@@ -86,11 +86,9 @@ KResultOr<Memory::Region*> InodeFile::mmap(Process& process, FileDescription& de
// FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec.
RefPtr<Memory::InodeVMObject> vmobject;
if (shared)
- vmobject = Memory::SharedInodeVMObject::try_create_with_inode(inode());
+ vmobject = TRY(Memory::SharedInodeVMObject::try_create_with_inode(inode()));
else
- vmobject = Memory::PrivateInodeVMObject::try_create_with_inode(inode());
- if (!vmobject)
- return ENOMEM;
+ vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode(inode()));
return process.address_space().allocate_region_with_vmobject(range, vmobject.release_nonnull(), offset, description.absolute_path(), prot, shared);
}
diff --git a/Kernel/Memory/PrivateInodeVMObject.cpp b/Kernel/Memory/PrivateInodeVMObject.cpp
index 0c3aa9f981..25b1b6cbad 100644
--- a/Kernel/Memory/PrivateInodeVMObject.cpp
+++ b/Kernel/Memory/PrivateInodeVMObject.cpp
@@ -9,9 +9,9 @@
namespace Kernel::Memory {
-RefPtr<PrivateInodeVMObject> PrivateInodeVMObject::try_create_with_inode(Inode& inode)
+KResultOr<NonnullRefPtr<PrivateInodeVMObject>> PrivateInodeVMObject::try_create_with_inode(Inode& inode)
{
- return adopt_ref_if_nonnull(new (nothrow) PrivateInodeVMObject(inode, inode.size()));
+ return adopt_nonnull_ref_or_enomem(new (nothrow) PrivateInodeVMObject(inode, inode.size()));
}
KResultOr<NonnullRefPtr<VMObject>> PrivateInodeVMObject::try_clone()
diff --git a/Kernel/Memory/PrivateInodeVMObject.h b/Kernel/Memory/PrivateInodeVMObject.h
index a664c219a8..96a2f67fb7 100644
--- a/Kernel/Memory/PrivateInodeVMObject.h
+++ b/Kernel/Memory/PrivateInodeVMObject.h
@@ -18,7 +18,7 @@ class PrivateInodeVMObject final : public InodeVMObject {
public:
virtual ~PrivateInodeVMObject() override;
- static RefPtr<PrivateInodeVMObject> try_create_with_inode(Inode&);
+ static KResultOr<NonnullRefPtr<PrivateInodeVMObject>> try_create_with_inode(Inode&);
virtual KResultOr<NonnullRefPtr<VMObject>> try_clone() override;
private:
diff --git a/Kernel/Memory/SharedInodeVMObject.cpp b/Kernel/Memory/SharedInodeVMObject.cpp
index cfeda69841..7dacfd6e66 100644
--- a/Kernel/Memory/SharedInodeVMObject.cpp
+++ b/Kernel/Memory/SharedInodeVMObject.cpp
@@ -9,14 +9,12 @@
namespace Kernel::Memory {
-RefPtr<SharedInodeVMObject> SharedInodeVMObject::try_create_with_inode(Inode& inode)
+KResultOr<NonnullRefPtr<SharedInodeVMObject>> SharedInodeVMObject::try_create_with_inode(Inode& inode)
{
size_t size = inode.size();
if (auto shared_vmobject = inode.shared_vmobject())
return shared_vmobject.release_nonnull();
- auto vmobject = adopt_ref_if_nonnull(new (nothrow) SharedInodeVMObject(inode, size));
- if (!vmobject)
- return nullptr;
+ auto vmobject = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SharedInodeVMObject(inode, size)));
vmobject->inode().set_shared_vmobject(*vmobject);
return vmobject;
}
diff --git a/Kernel/Memory/SharedInodeVMObject.h b/Kernel/Memory/SharedInodeVMObject.h
index 6690c3a2d7..5b91785c55 100644
--- a/Kernel/Memory/SharedInodeVMObject.h
+++ b/Kernel/Memory/SharedInodeVMObject.h
@@ -16,7 +16,7 @@ class SharedInodeVMObject final : public InodeVMObject {
AK_MAKE_NONMOVABLE(SharedInodeVMObject);
public:
- static RefPtr<SharedInodeVMObject> try_create_with_inode(Inode&);
+ static KResultOr<NonnullRefPtr<SharedInodeVMObject>> try_create_with_inode(Inode&);
virtual KResultOr<NonnullRefPtr<VMObject>> try_clone() override;
private:
diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp
index be34bcced4..5e83ef9dec 100644
--- a/Kernel/Syscalls/execve.cpp
+++ b/Kernel/Syscalls/execve.cpp
@@ -165,11 +165,7 @@ struct RequiredLoadRange {
static KResultOr<RequiredLoadRange> get_required_load_range(FileDescription& program_description)
{
auto& inode = *(program_description.inode());
- auto vmobject = Memory::SharedInodeVMObject::try_create_with_inode(inode);
- if (!vmobject) {
- dbgln("get_required_load_range: Unable to allocate SharedInodeVMObject");
- return ENOMEM;
- }
+ auto vmobject = TRY(Memory::SharedInodeVMObject::try_create_with_inode(inode));
size_t executable_size = inode.size();
@@ -265,11 +261,7 @@ static KResultOr<LoadResult> load_elf_object(NonnullOwnPtr<Memory::AddressSpace>
FlatPtr load_offset, ShouldAllocateTls should_allocate_tls, ShouldAllowSyscalls should_allow_syscalls)
{
auto& inode = *(object_description.inode());
- auto vmobject = Memory::SharedInodeVMObject::try_create_with_inode(inode);
- if (!vmobject) {
- dbgln("load_elf_object: Unable to allocate SharedInodeVMObject");
- return ENOMEM;
- }
+ auto vmobject = TRY(Memory::SharedInodeVMObject::try_create_with_inode(inode));
if (vmobject->writable_mappings()) {
dbgln("Refusing to execute a write-mapped program");
diff --git a/Kernel/Syscalls/mmap.cpp b/Kernel/Syscalls/mmap.cpp
index 0616829628..28bec9aa54 100644
--- a/Kernel/Syscalls/mmap.cpp
+++ b/Kernel/Syscalls/mmap.cpp
@@ -497,17 +497,14 @@ KResultOr<FlatPtr> Process::sys$mremap(Userspace<const Syscall::SC_mremap_params
auto old_offset = old_region->offset_in_vmobject();
NonnullRefPtr inode = static_cast<Memory::SharedInodeVMObject&>(old_region->vmobject()).inode();
- auto new_vmobject = Memory::PrivateInodeVMObject::try_create_with_inode(inode);
- if (!new_vmobject)
- return ENOMEM;
-
+ auto new_vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode(inode));
auto old_name = old_region->take_name();
// Unmap without deallocating the VM range since we're going to reuse it.
old_region->unmap(Memory::Region::ShouldDeallocateVirtualRange::No);
address_space().deallocate_region(*old_region);
- auto new_region = TRY(address_space().allocate_region_with_vmobject(range, new_vmobject.release_nonnull(), old_offset, old_name->view(), old_prot, false));
+ auto new_region = TRY(address_space().allocate_region_with_vmobject(range, move(new_vmobject), old_offset, old_name->view(), old_prot, false));
new_region->set_mmap(true);
return new_region->vaddr().get();
}
diff --git a/Kernel/Syscalls/ptrace.cpp b/Kernel/Syscalls/ptrace.cpp
index 77854351ee..210bae36c1 100644
--- a/Kernel/Syscalls/ptrace.cpp
+++ b/Kernel/Syscalls/ptrace.cpp
@@ -185,10 +185,8 @@ KResult Process::poke_user_data(Userspace<u32*> address, u32 data)
// If the region is shared, we change its vmobject to a PrivateInodeVMObject
// to prevent the write operation from changing any shared inode data
VERIFY(region->vmobject().is_shared_inode());
- auto vmobject = Memory::PrivateInodeVMObject::try_create_with_inode(static_cast<Memory::SharedInodeVMObject&>(region->vmobject()).inode());
- if (!vmobject)
- return ENOMEM;
- region->set_vmobject(vmobject.release_nonnull());
+ auto vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode(static_cast<Memory::SharedInodeVMObject&>(region->vmobject()).inode()));
+ region->set_vmobject(move(vmobject));
region->set_shared(false);
}
const bool was_writable = region->is_writable();