mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 17:40:27 +00:00
Kernel: Use an ArmedScopeGuard to revert changes after failed mmap
This commit is contained in:
parent
a4e3ae0ee9
commit
790d620b39
Notes:
sideshowbarker
2024-07-17 17:41:55 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/790d620b39 Pull-request: https://github.com/SerenityOS/serenity/pull/12587 Reviewed-by: https://github.com/IdanHo Reviewed-by: https://github.com/awesomekling
1 changed files with 12 additions and 1 deletions
|
@ -5,6 +5,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/kmalloc.h>
|
||||
#include <Kernel/Arch/SmapDisabler.h>
|
||||
#include <Kernel/Arch/x86/MSR.h>
|
||||
#include <Kernel/Arch/x86/SafeMem.h>
|
||||
|
@ -184,8 +185,16 @@ ErrorOr<FlatPtr> Process::sys$mmap(Userspace<const Syscall::SC_mmap_params*> use
|
|||
return EINVAL;
|
||||
|
||||
Memory::Region* region = nullptr;
|
||||
Memory::VirtualRange range { {}, 0 };
|
||||
|
||||
auto range = TRY([&]() -> ErrorOr<Memory::VirtualRange> {
|
||||
ArmedScopeGuard scope_guard = [&] {
|
||||
if (region)
|
||||
address_space().deallocate_region(*region);
|
||||
else if (range.is_valid())
|
||||
address_space().page_directory().range_allocator().deallocate(range);
|
||||
};
|
||||
|
||||
range = TRY([&]() -> ErrorOr<Memory::VirtualRange> {
|
||||
if (map_randomized)
|
||||
return address_space().page_directory().range_allocator().try_allocate_randomized(rounded_size, alignment);
|
||||
|
||||
|
@ -248,6 +257,8 @@ ErrorOr<FlatPtr> Process::sys$mmap(Userspace<const Syscall::SC_mmap_params*> use
|
|||
|
||||
PerformanceManager::add_mmap_perf_event(*this, *region);
|
||||
|
||||
scope_guard.disarm();
|
||||
|
||||
return region->vaddr().get();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue