util/attempt.h: mark 'Attempt' types as nodiscard

This catches bugs early on. E.g., when leaving an 'Allocation'
unused, it gets immediately deallocated, which is most probably not
intended. For regular 'Attempt' objects, this change encourages
the proper propagation of errors, or at least the logging of unexpected
conditions.

Fixes #5513
This commit is contained in:
Norman Feske 2025-04-06 17:25:15 +02:00
parent e380d0da95
commit 84eb264786
49 changed files with 300 additions and 168 deletions

View File

@ -192,6 +192,11 @@ struct Region
{
return (((base + size) > start) && (base < end));
}
void print(Output &out) const
{
Genode::print(out, Hex_range(start, end - start));
}
};
@ -204,7 +209,8 @@ static inline void add_region(Region r, Range_allocator &alloc)
addr_t start = trunc_page(r.start);
addr_t end = round_page(r.end);
alloc.add_range(start, end - start);
if (alloc.add_range(start, end - start).failed())
warning("unable to add range to allocator: ", r);
}
@ -217,7 +223,8 @@ static inline void remove_region(Region r, Range_allocator &alloc)
addr_t start = trunc_page(r.start);
addr_t end = round_page(r.end);
alloc.remove_range(start, end - start);
if (alloc.remove_range(start, end - start).failed())
warning("unable to exclude range from allocator: ", r);
}
@ -297,7 +304,8 @@ void Core::Platform::_setup_mem_alloc()
void Core::Platform::_setup_irq_alloc()
{
_irq_alloc.add_range(0, 0x10);
if (_irq_alloc.add_range(0, 0x10).failed())
warning("unable to initialize IRQ allocator");
}
@ -371,15 +379,17 @@ void Core::Platform::_setup_basics()
/* configure applicable address space but never use page0 */
_vm_size = _vm_start == 0 ? _vm_size - L4_PAGESIZE : _vm_size;
_vm_start = _vm_start == 0 ? L4_PAGESIZE : _vm_start;
_region_alloc.add_range(_vm_start, _vm_size);
if (_region_alloc.add_range(_vm_start, _vm_size).failed())
warning("unable to initialize core's virtual-memory allocator");
/* preserve stack area in core's virtual address space */
_region_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size());
if (_region_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size()).failed())
warning("unable to mark stack-area as used virtual memory");
/* I/O memory could be the whole user address space */
/* FIXME if the kernel helps to find out max address - use info here */
_io_mem_alloc.add_range(0, ~0);
if (_io_mem_alloc.add_range(0, ~0).failed())
warning("unable to initialize I/O-memory allocator");
/* remove KIP area from region and IO_MEM allocator */
remove_region(Region((addr_t)kip, (addr_t)kip + L4_PAGESIZE), _region_alloc);
@ -450,7 +460,11 @@ Core::Platform::Platform()
addr_t const phys_addr = reinterpret_cast<addr_t>(phys.ptr);
void * const core_local_ptr = phys.ptr;
region_alloc().remove_range((addr_t)core_local_ptr, size);
if (region_alloc().remove_range((addr_t)core_local_ptr, size).failed()) {
warning("unable to mark ROM-module range for '",
rom_name, "' as used virtual memory");
return;
}
memset(core_local_ptr, 0, size);
content_fn(core_local_ptr, size);

View File

@ -46,9 +46,8 @@ void Platform::_setup_io_port_alloc()
&& fp.iofp.f == 0xf /* got IO ports */
&& fp.iofp.iosize == L4_WHOLE_IOADDRESS_SPACE
&& fp.iofp.iopage == 0)) /* got whole IO space */
panic("Received no I/O ports from sigma0");
panic("Received no I/O ports from sigma0");
/* setup allocator */
_io_port_alloc.add_range(0, 0x10000);
if (_io_port_alloc.add_range(0, 0x10000).failed())
warning("unable to initialize default I/O-port range");
}

View File

@ -188,6 +188,11 @@ struct Region
{
return (((base + size) > start) && (base < end));
}
void print(Output &out) const
{
Genode::print(out, Hex_range(start, end - start));
}
};
@ -200,7 +205,8 @@ static inline void add_region(Region r, Range_allocator &alloc)
addr_t const start = trunc_page(r.start);
addr_t const end = round_page(r.end);
alloc.add_range(start, end - start);
if (alloc.add_range(start, end - start).failed())
warning("unable to add allocator region: ", r);
}
@ -213,7 +219,8 @@ static inline void remove_region(Region r, Range_allocator &alloc)
addr_t const start = trunc_page(r.start);
addr_t const end = round_page(r.end);
alloc.remove_range(start, end - start);
if (alloc.remove_range(start, end - start).failed())
warning("unable to exclude allocator region: ", r);
}
@ -339,7 +346,8 @@ void Core::Platform::_setup_irq_alloc()
if (l4_error(res))
panic("could not determine number of IRQs");
_irq_alloc.add_range(0, info.nr_irqs);
if (_irq_alloc.add_range(0, info.nr_irqs).failed())
warning("unable to initialize IRQ allocator");
}
@ -458,18 +466,22 @@ void Core::Platform::_setup_basics()
_vm_start = VCPU_VIRT_EXT_END;
}
_region_alloc.add_range(_vm_start, _vm_size);
if (_region_alloc.add_range(_vm_start, _vm_size).failed())
warning("unable to initialize core's virtual memory");
/* preserve stack area in core's virtual address space */
_region_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size());
if (_region_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size()).failed())
warning("unable to exclude stack area from core-local allocator");
/* preserve utcb- area in core's virtual address space */
_region_alloc.remove_range((addr_t)l4_utcb(), L4_PAGESIZE * 16);
if (_region_alloc.remove_range((addr_t)l4_utcb(), L4_PAGESIZE * 16).failed())
warning("unable to exclude UTCB area from core-local allocator");
/* I/O memory could be the whole user address space */
/* FIXME if the kernel helps to find out max address - use info here */
_io_mem_alloc.add_range(0, ~0);
if (_io_mem_alloc.add_range(0, ~0).failed())
warning("unable to initialize I/O-memory allocator");
/* remove KIP and MBI area from region and IO_MEM allocator */
remove_region(Region((addr_t)&kip, (addr_t)&kip + L4_PAGESIZE), _region_alloc);
@ -485,8 +497,9 @@ void Core::Platform::_setup_basics()
add_region(Region(img_start, img_end), _core_address_ranges());
/* requested as I/O memory by the VESA driver and ACPI (rsdp search) */
_io_mem_alloc.add_range (0, 0x2000);
ram_alloc() .remove_range(0, 0x2000);
if (_io_mem_alloc.add_range (0, 0x2000).failed()
|| ram_alloc() .remove_range(0, 0x2000).failed())
warning("unable to register RSDP as memory-mapped I/O range");
}

View File

@ -190,7 +190,8 @@ Cap_id_allocator::Cap_id_allocator(Allocator &alloc)
:
_id_alloc(&alloc)
{
_id_alloc.add_range(CAP_ID_OFFSET, unsigned(CAP_ID_RANGE) - unsigned(CAP_ID_OFFSET));
(void)_id_alloc.add_range(CAP_ID_OFFSET,
unsigned(CAP_ID_RANGE) - unsigned(CAP_ID_OFFSET));
}

View File

@ -45,7 +45,8 @@ void Platform::_setup_io_port_alloc()
panic("Received no I/O ports from sigma0");
/* setup allocator */
_io_port_alloc.add_range(0, 0x10000);
if (_io_port_alloc.add_range(0, 0x10000).failed())
warning("unable to register default I/O-port range");
}

View File

@ -113,8 +113,8 @@ Vm_session_component::Vm_session_component(Rpc_entrypoint &ep,
}
/* configure managed VM area */
_map.add_range(0, 0UL - 0x1000);
_map.add_range(0UL - 0x1000, 0x1000);
(void)_map.add_range(0, 0UL - 0x1000);
(void)_map.add_range(0UL - 0x1000, 0x1000);
caps.acknowledge();
}

View File

@ -45,12 +45,18 @@ void * Platform::Ram_allocator::alloc_aligned(size_t size, unsigned align)
}
void Platform::Ram_allocator::add(Memory_region const & region) {
add_range(region.base, region.size); }
void Platform::Ram_allocator::add(Memory_region const & region)
{
if (add_range(region.base, region.size).failed())
Genode::warning("bootstrap failed to register RAM: ", region);
}
void Platform::Ram_allocator::remove(Memory_region const & region) {
remove_range(region.base, region.size); }
void Platform::Ram_allocator::remove(Memory_region const & region)
{
if (remove_range(region.base, region.size).failed())
Genode::warning("bootstrap unable to exclude RAM: ", region);
}
/******************

View File

@ -222,7 +222,8 @@ class Core::Guest_memory
_sliced_heap(ram, region_map)
{
/* configure managed VM area */
_map.add_range(0UL, ~0UL);
if (_map.add_range(0UL, ~0UL).failed())
warning("unable to initialize guest-memory allocator");
}
~Guest_memory()

View File

@ -75,9 +75,12 @@ addr_t Platform::core_main_thread_phys_utcb()
void Platform::_init_io_mem_alloc()
{
/* add entire adress space minus the RAM memory regions */
_io_mem_alloc.add_range(0, ~0x0UL);
if (_io_mem_alloc.add_range(0, ~0x0UL).failed())
warning("unable to initialize I/O-memory allocator");
_boot_info().ram_regions.for_each([this] (unsigned, Hw::Memory_region const &r) {
_io_mem_alloc.remove_range(r.base, r.size); });
if (_io_mem_alloc.remove_range(r.base, r.size).failed())
warning("unable to exclude I/O range from RAM: ", r); });
};
@ -180,14 +183,21 @@ Platform::Platform()
{
struct Kernel_resource : Exception { };
_core_mem_alloc.virt_alloc().add_range(Hw::Mm::core_heap().base,
Hw::Mm::core_heap().size);
if (_core_mem_alloc.virt_alloc().add_range(Hw::Mm::core_heap().base,
Hw::Mm::core_heap().size).failed())
warning("unable to initialize core virtual-memory allocator");
_core_virt_regions().for_each([this] (unsigned, Hw::Memory_region const & r) {
_core_mem_alloc.virt_alloc().remove_range(r.base, r.size); });
if (_core_mem_alloc.virt_alloc().remove_range(r.base, r.size).failed())
warning("unable to exclude core from core's virtual memory"); });
_boot_info().elf_mappings.for_each([this] (unsigned, Hw::Mapping const & m) {
_core_mem_alloc.virt_alloc().remove_range(m.virt(), m.size()); });
if (_core_mem_alloc.virt_alloc().remove_range(m.virt(), m.size()).failed())
warning("unable to exclude ELF mapping from core's virtual memory"); });
_boot_info().ram_regions.for_each([this] (unsigned, Hw::Memory_region const & region) {
_core_mem_alloc.phys_alloc().add_range(region.base, region.size); });
if (_core_mem_alloc.phys_alloc().add_range(region.base, region.size).failed())
warning("unable to register RAM region ", region); });
_init_io_port_alloc();
@ -203,7 +213,8 @@ Platform::Platform()
if (kernel_resource) {
continue;
}
_irq_alloc.add_range(i, 1);
if (_irq_alloc.add_range(i, 1).failed())
warning("unable to register IRQ ", i);
}
_init_io_mem_alloc();

View File

@ -22,7 +22,8 @@ using namespace Core;
void Platform::_init_io_port_alloc()
{
_io_port_alloc.add_range(0, 0x10000);
if (_io_port_alloc.add_range(0, 0x10000).failed())
warning("unable to register default I/O-port range");
}

View File

@ -111,7 +111,8 @@ Core::Platform::Platform()
/* create resource directory under /tmp */
lx_mkdir(resource_path(), S_IRWXU);
_core_mem_alloc.add_range((addr_t)_core_mem, sizeof(_core_mem));
if (_core_mem_alloc.add_range((addr_t)_core_mem, sizeof(_core_mem)).failed())
warning("failed to initialize core memory allocator");
/*
* Occupy the socket handle that will be used to propagate the parent

View File

@ -23,14 +23,15 @@ using namespace Genode;
void Platform::_attach_stack_area()
{
pd._address_space.attach(pd._stack_area.dataspace(), Region_map::Attr {
if (pd._address_space.attach(pd._stack_area.dataspace(), Region_map::Attr {
.size = stack_area_virtual_size(),
.offset = { },
.use_at = true,
.at = stack_area_virtual_base(),
.executable = { },
.writeable = true
});
}).failed())
warning("unable to attach stack area to local address space");
env_stack_area_region_map = &pd._stack_area;
env_stack_area_ram_allocator = &ram;

View File

@ -260,9 +260,9 @@ Region_map_mmap::attach(Dataspace_capability ds, Attr const &attr)
* argument as the region was reserved by a PROT_NONE mapping.
*/
if (_is_attached())
_map_local(ds, region_size, attr.offset,
true, _base + attr.at,
attr.executable, true, attr.writeable);
if (_map_local(ds, region_size, attr.offset, true, _base + attr.at,
attr.executable, true, attr.writeable).failed())
return Attach_error::REGION_CONFLICT;
return Range { .start = attr.at, .num_bytes = region_size };
@ -317,9 +317,10 @@ Region_map_mmap::attach(Dataspace_capability ds, Attr const &attr)
* We have to enforce the mapping via the 'overmap' argument as
* the region was reserved by a PROT_NONE mapping.
*/
_map_local(region.dataspace(), region.size(), region.offset(),
true, rm->_base + region.start() + region.offset(),
attr.executable, true, attr.writeable);
if (_map_local(region.dataspace(), region.size(), region.offset(),
true, rm->_base + region.start() + region.offset(),
attr.executable, true, attr.writeable).failed())
return Attach_error::REGION_CONFLICT;
}
return Range { .start = rm->_base, .num_bytes = region_size };
@ -398,7 +399,8 @@ void Region_map_mmap::detach(addr_t at)
*/
if (_is_attached()) {
lx_munmap((void *)(at + _base), region.size());
_reserve_local(true, at + _base, region.size());
if (_reserve_local(true, at + _base, region.size()).failed())
warning(__PRETTY_FUNCTION__, ": _reserve_local unexpectedly failed");
}
} else {

View File

@ -405,7 +405,7 @@ Core::Platform::Platform()
/* define core's virtual address space */
addr_t virt_beg = _vm_base;
addr_t virt_end = _vm_size;
_core_mem_alloc.virt_alloc().add_range(virt_beg, virt_end - virt_beg);
(void)_core_mem_alloc.virt_alloc().add_range(virt_beg, virt_end - virt_beg);
/* exclude core image from core's virtual address allocator */
addr_t const core_virt_beg = trunc_page((addr_t)&_prog_img_beg);
@ -414,29 +414,33 @@ Core::Platform::Platform()
addr_t const binaries_end = round_page((addr_t)&_boot_modules_binaries_end);
size_t const core_size = binaries_beg - core_virt_beg;
region_alloc().remove_range(core_virt_beg, core_size);
auto exclude_from_core = [&] (addr_t start, size_t num_bytes)
{
if (region_alloc().remove_range(start, num_bytes).failed())
warning("unable to exclude core-local range ", Hex_range(start, num_bytes));
};
exclude_from_core(core_virt_beg, core_size);
/* ROM modules are un-used by core - de-detach region */
addr_t const binaries_size = binaries_end - binaries_beg;
unmap_local(*__main_thread_utcb, binaries_beg, binaries_size >> 12);
/* preserve Bios Data Area (BDA) in core's virtual address space */
region_alloc().remove_range(BDA_VIRT_ADDR, 0x1000);
exclude_from_core(BDA_VIRT_ADDR, 0x1000);
/* preserve stack area in core's virtual address space */
region_alloc().remove_range(stack_area_virtual_base(),
stack_area_virtual_size());
exclude_from_core(stack_area_virtual_base(), stack_area_virtual_size());
/* exclude utcb of core pager thread + empty guard pages before and after */
region_alloc().remove_range(CORE_PAGER_UTCB_ADDR - get_page_size(),
get_page_size() * 3);
exclude_from_core(CORE_PAGER_UTCB_ADDR - get_page_size(), get_page_size() * 3);
/* exclude utcb of main thread and hip + empty guard pages before and after */
region_alloc().remove_range((addr_t)__main_thread_utcb - get_page_size(),
get_page_size() * 4);
exclude_from_core((addr_t)__main_thread_utcb - get_page_size(), get_page_size() * 4);
/* exclude HIP */
region_alloc().remove_range(addr_t(&hip), _vm_base + _vm_size - addr_t(&hip));
exclude_from_core(addr_t(&hip), _vm_base + _vm_size - addr_t(&hip));
/* sanity checks */
addr_t check [] = {
@ -458,7 +462,7 @@ Core::Platform::Platform()
}
/* initialize core's physical-memory and I/O memory allocator */
_io_mem_alloc.add_range(0, ~0xfffUL);
(void)_io_mem_alloc.add_range(0, ~0xfffUL);
Hip::Mem_desc *mem_desc = (Hip::Mem_desc *)mem_desc_base;
Hip::Mem_desc *boot_fb = nullptr;
@ -495,10 +499,16 @@ Core::Platform::Platform()
else
size = trunc_page(mem_desc->addr + mem_desc->size) - base;
_io_mem_alloc.remove_range((addr_t)base, (size_t)size);
ram_alloc().add_range((addr_t)base, (size_t)size);
(void)_io_mem_alloc.remove_range((addr_t)base, (size_t)size);
(void)ram_alloc().add_range((addr_t)base, (size_t)size);
}
auto exclude_from_ram = [&] (addr_t start, size_t num_bytes)
{
if (ram_alloc().remove_range(start, num_bytes).failed())
warning("unable to exclude RAM range ", Hex_range(start, num_bytes));
};
addr_t hyp_log = 0;
size_t hyp_log_size = 0;
@ -542,14 +552,14 @@ Core::Platform::Platform()
/* make acpi regions as io_mem available to platform driver */
if (mem_desc->type == Hip::Mem_desc::ACPI_RECLAIM_MEMORY ||
mem_desc->type == Hip::Mem_desc::ACPI_NVS_MEMORY)
_io_mem_alloc.add_range((addr_t)base, (size_t)size);
(void)_io_mem_alloc.add_range((addr_t)base, (size_t)size);
ram_alloc().remove_range((addr_t)base, (size_t)size);
exclude_from_ram((addr_t)base, (size_t)size);
}
/* needed as I/O memory by the VESA driver */
_io_mem_alloc.add_range(0, 0x1000);
ram_alloc().remove_range(0, 0x1000);
(void)_io_mem_alloc.add_range(0, 0x1000);
exclude_from_ram(0, 0x1000);
/* exclude pages holding multi-boot command lines from core allocators */
mem_desc = (Hip::Mem_desc *)mem_desc_base;
@ -561,8 +571,8 @@ Core::Platform::Platform()
curr_cmd_line_page = mem_desc->aux >> get_page_size_log2();
if (curr_cmd_line_page == prev_cmd_line_page) continue;
ram_alloc().remove_range(curr_cmd_line_page << get_page_size_log2(),
get_page_size() * 2);
exclude_from_ram(curr_cmd_line_page << get_page_size_log2(),
get_page_size() * 2);
prev_cmd_line_page = curr_cmd_line_page;
}
@ -804,10 +814,10 @@ Core::Platform::Platform()
}
/* I/O port allocator (only meaningful for x86) */
_io_port_alloc.add_range(0, 0x10000);
(void)_io_port_alloc.add_range(0, 0x10000);
/* IRQ allocator */
_irq_alloc.add_range(0, hip.sel_gsi);
(void)_irq_alloc.add_range(0, hip.sel_gsi);
_gsi_base_sel = (hip.mem_desc_offset - hip.cpu_desc_offset) / hip.cpu_desc_size;
log(_rom_fs);

View File

@ -397,8 +397,8 @@ Vm_session_component::Vm_session_component(Rpc_entrypoint &ep,
* Configure managed VM area. The two ranges work around the size
* limitation to ULONG_MAX.
*/
_map.add_range(0, 0UL - 0x1000);
_map.add_range(0UL - 0x1000, 0x1000);
(void)_map.add_range(0, 0UL - 0x1000);
(void)_map.add_range(0UL - 0x1000, 0x1000);
}

View File

@ -56,8 +56,8 @@ int Core::Platform::bi_init_mem(Okl4::uintptr_t virt_base, Okl4::uintptr_t virt_
const Okl4::bi_user_data_t *data)
{
Platform &p = *(Platform *)data->user_data;
p._core_mem_alloc.phys_alloc().add_range(phys_base, phys_end - phys_base + 1);
p._core_mem_alloc.virt_alloc().add_range(virt_base, virt_end - virt_base + 1);
(void)p._core_mem_alloc.phys_alloc().add_range(phys_base, phys_end - phys_base + 1);
(void)p._core_mem_alloc.virt_alloc().add_range(virt_base, virt_end - virt_base + 1);
return 0;
}
@ -70,7 +70,7 @@ int Core::Platform::bi_add_virt_mem(Okl4::bi_name_t, Okl4::uintptr_t base,
return 0;
Platform &p = *(Platform *)data->user_data;
p._core_mem_alloc.virt_alloc().add_range(base, end - base + 1);
(void)p._core_mem_alloc.virt_alloc().add_range(base, end - base + 1);
return 0;
}
@ -80,7 +80,7 @@ int Core::Platform::bi_add_phys_mem(Okl4::bi_name_t pool, Okl4::uintptr_t base,
{
if (pool == 2) {
Platform &p = *(Platform *)data->user_data;
p._core_mem_alloc.phys_alloc().add_range(base, end - base + 1);
(void)p._core_mem_alloc.phys_alloc().add_range(base, end - base + 1);
}
return 0;
}
@ -149,19 +149,23 @@ Core::Platform::Platform()
Okl4::bootinfo_parse((void *)boot_info_addr, &callbacks, this);
/* initialize interrupt allocator */
_irq_alloc.add_range(0, 0x10);
if (_irq_alloc.add_range(0, 0x10).failed())
warning("unable to initialize IRQ allocator");
/* I/O memory could be the whole user address space */
_io_mem_alloc.add_range(0, ~0);
if (_io_mem_alloc.add_range(0, ~0).failed())
warning("unable to initialize I/O-memory allocator");
/* I/O port allocator (only meaningful for x86) */
_io_port_alloc.add_range(0, 0x10000);
if (_io_port_alloc.add_range(0, 0x10000).failed())
warning("unable to initialize I/O-port allocator");
_init_rom_modules();
/* preserve stack area in core's virtual address space */
_core_mem_alloc.virt_alloc().remove_range(stack_area_virtual_base(),
stack_area_virtual_size());
if (_core_mem_alloc.virt_alloc().remove_range(stack_area_virtual_base(),
stack_area_virtual_size()).failed())
warning("unable to mark stack area as preserved virtual memory");
_vm_start = 0x1000;
_vm_size = 0xc0000000 - _vm_start;

View File

@ -282,7 +282,8 @@ static inline void add_region(Region r, Range_allocator &alloc)
addr_t start = trunc_page(r.start);
addr_t end = round_page(r.end);
alloc.add_range(start, end - start);
if (alloc.add_range(start, end - start).failed())
warning("failed to add range to allocator: ", r);
}
@ -298,7 +299,8 @@ static inline void remove_region(Region r, Range_allocator &alloc)
addr_t start = trunc_page(r.start);
addr_t end = round_page(r.end);
alloc.remove_range(start, end - start);
if (alloc.remove_range(start, end - start).failed())
warning("failed to exclude range from allocator: ", r);
}
@ -451,7 +453,11 @@ void Core::Platform::_setup_mem_alloc()
}
void Core::Platform::_setup_irq_alloc() { _irq_alloc.add_range(0, 0x10); }
void Core::Platform::_setup_irq_alloc()
{
if (_irq_alloc.add_range(0, 0x10).failed())
warning("unable to initialize IRQ allocator");
}
void Core::Platform::_setup_preemption()
@ -496,7 +502,8 @@ void Core::Platform::_setup_basics()
Platform_pd::touch_utcb_space();
/* I/O memory could be the whole user address space */
_io_mem_alloc.add_range(0, ~0);
if (_io_mem_alloc.add_range(0, ~0).failed())
warning("unable to initialize I/O-memory allocator");
unsigned int kip_size = sizeof(L4_KernelInterfacePage_t);
@ -530,10 +537,15 @@ void Core::Platform::_setup_basics()
}
/* configure core's virtual memory, exclude KIP, stack area */
_region_alloc.add_range(_vm_start, _vm_size);
_region_alloc.remove_range((addr_t)kip, kip_size);
_region_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size());
if (_region_alloc.add_range(_vm_start, _vm_size).failed())
warning("unable to initialize core's virtual-memory allocator");
if (_region_alloc.remove_range((addr_t)kip, kip_size).failed())
warning("unable to mark KIP as preserved virtual memory");
if (_region_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size()).failed())
warning("unable to mark stack area as preserved virtual memory");
/* remove KIP area from region and IO_MEM allocator */
remove_region(Region((addr_t)kip, (addr_t)kip + kip_size), _region_alloc);
@ -608,7 +620,11 @@ Core::Platform::Platform()
addr_t const phys_addr = reinterpret_cast<addr_t>(phys.ptr);
void * const core_local_ptr = phys.ptr;
region_alloc().remove_range((addr_t)core_local_ptr, size);
if (region_alloc().remove_range((addr_t)core_local_ptr, size).failed()) {
warning("unable to mark ROM-module range for '",
rom_name, "' as preserved virtual memory");
return;
}
memset(core_local_ptr, 0, size);
content_fn(core_local_ptr, size);

View File

@ -26,5 +26,6 @@ void Platform::_setup_io_port_alloc()
{
/* setup allocator */
enum { IO_PORT_RANGE_SIZE = 0x10000 };
_io_port_alloc.add_range(0, IO_PORT_RANGE_SIZE);
if (_io_port_alloc.add_range(0, IO_PORT_RANGE_SIZE).failed())
warning("unable to initialize default I/O-port range");
}

View File

@ -88,14 +88,16 @@ bool Mapped_mem_allocator::_unmap_local(addr_t virt_addr, addr_t phys_addr, size
void Core::Platform::_init_unused_phys_alloc()
{
/* the lower physical ram is kept by the kernel and not usable to us */
_unused_phys_alloc.add_range(0x100000, 0UL - 0x100000);
if (_unused_phys_alloc.add_range(0x100000, 0UL - 0x100000).failed())
warning("unable to mark kernel-only memory as preserved");
}
void Core::Platform::_init_allocators()
{
/* interrupt allocator */
_irq_alloc.add_range(0, 256);
if (_irq_alloc.add_range(0, 256).failed())
warning("unable to initialize IRQ allocator");
/*
* XXX allocate intermediate CNodes for organizing the untyped pages here
@ -109,12 +111,17 @@ void Core::Platform::_init_allocators()
addr_t const phys_addr = trunc_page(phys);
size_t const phys_size = round_page(phys - phys_addr + size);
if (device_memory)
_io_mem_alloc.add_range(phys_addr, phys_size);
else
_core_mem_alloc.phys_alloc().add_range(phys_addr, phys_size);
Hex_range const range { phys_addr, phys_size };
_unused_phys_alloc.remove_range(phys_addr, phys_size);
if (device_memory) {
if (_io_mem_alloc.add_range(phys_addr, phys_size).failed())
warning("failed to register I/O range: ", range);
} else {
if (_core_mem_alloc.phys_alloc().add_range(phys_addr, phys_size).failed())
warning("failed to register RAM range: ", range);
}
if (_unused_phys_alloc.remove_range(phys_addr, phys_size).failed())
warning("failed to mark range as used: ", range);
return true; /* range used by this functor */
});
@ -125,7 +132,8 @@ void Core::Platform::_init_allocators()
*/
/* core's maximum virtual memory area */
_unused_virt_alloc.add_range(_vm_base, _vm_size);
if (_unused_virt_alloc.add_range(_vm_base, _vm_size).failed())
warning("unable to initialize core's virtual memory allocator");
/* remove core image from core's virtual address allocator */
addr_t const modules_start = reinterpret_cast<addr_t>(&_boot_modules_binaries_begin);
@ -133,23 +141,29 @@ void Core::Platform::_init_allocators()
core_virt_end = round_page((addr_t)&_prog_img_end);
addr_t const image_elf_size = core_virt_end - core_virt_beg;
_unused_virt_alloc.remove_range(core_virt_beg, image_elf_size);
_core_mem_alloc.virt_alloc().add_range(modules_start, core_virt_end - modules_start);
if (_unused_virt_alloc.remove_range(core_virt_beg, image_elf_size).failed())
warning("unable to mark virtual range of core image as used");
if (_core_mem_alloc.virt_alloc() .add_range(modules_start, core_virt_end - modules_start).failed())
warning("unable to register ROM-module range at core;s virtual memory");
/* remove initial IPC buffer from core's virtual address allocator */
seL4_BootInfo const &bi = sel4_boot_info();
addr_t const core_ipc_buffer = reinterpret_cast<addr_t>(bi.ipcBuffer);
addr_t const core_ipc_bsize = 4096;
_unused_virt_alloc.remove_range(core_ipc_buffer, core_ipc_bsize);
if (_unused_virt_alloc.remove_range(core_ipc_buffer, core_ipc_bsize).failed())
warning("unable to mark core's IPC buffer as preserved virtual memory");
/* remove sel4_boot_info page from core's virtual address allocator */
addr_t const boot_info_page = reinterpret_cast<addr_t>(&bi);
addr_t const boot_info_size = 4096 + bi.extraLen;
_unused_virt_alloc.remove_range(boot_info_page, boot_info_size);
if (_unused_virt_alloc.remove_range(boot_info_page, boot_info_size).failed())
warning("unable to mark seL4 boot-info page as preserved virtual memory");
/* preserve stack area in core's virtual address space */
_unused_virt_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size());
if (_unused_virt_alloc.remove_range(stack_area_virtual_base(),
stack_area_virtual_size()).failed())
warning("unable to mark stack area as preserved virtual memory");
if (verbose_boot_info) {
using Hex_range = Hex_range<addr_t>;
@ -590,7 +604,8 @@ Core::Platform::Platform()
addr_t const virt_addr = (addr_t)virt.ptr;
/* add to available virtual region of core */
_core_mem_alloc.virt_alloc().add_range(virt_addr, virt_size);
if (_core_mem_alloc.virt_alloc().add_range(virt_addr, virt_size).failed())
warning("unable to register virtual range for dynamic allocations");
/* back region by page tables */
_core_vm_space.unsynchronized_alloc_page_tables(virt_addr, virt_size);

View File

@ -91,8 +91,9 @@ void Platform::_init_core_page_table_registry()
if (device_memory)
return false;
phys_alloc_16k().add_range(phys, size);
_unused_phys_alloc.remove_range(phys, size);
if (phys_alloc_16k() .add_range (phys, size).failed()
|| _unused_phys_alloc.remove_range(phys, size).failed())
warning("unable to register range as RAM: ", Hex_range(phys, size));
return true;
},

View File

@ -138,8 +138,8 @@ try
}
/* configure managed VM area */
_map.add_range(0, 0UL - 0x1000);
_map.add_range(0UL - 0x1000, 0x1000);
(void)_map.add_range(0, 0UL - 0x1000);
(void)_map.add_range(0UL - 0x1000, 0x1000);
caps.acknowledge();
ram.acknowledge();

View File

@ -87,8 +87,9 @@ void Platform::_init_core_page_table_registry()
if (device_memory)
return false;
phys_alloc_16k().add_range(phys, size);
_unused_phys_alloc.remove_range(phys, size);
if (phys_alloc_16k() .add_range (phys, size).failed()
|| _unused_phys_alloc.remove_range(phys, size).failed())
warning("unable to register range as RAM: ", Hex_range(phys, size));
return true;
},
@ -103,7 +104,8 @@ void Platform::_init_io_ports()
enum { PORTS = 0x10000, PORT_FIRST = 0, PORT_LAST = PORTS - 1 };
/* I/O port allocator (only meaningful for x86) */
_io_port_alloc.add_range(PORT_FIRST, PORTS);
if (_io_port_alloc.add_range(PORT_FIRST, PORTS).failed())
warning("unable to register default I/O-port range");
/* create I/O port capability used by io_port_session_support.cc */
auto const root = _core_cnode.sel().value();

View File

@ -17,8 +17,8 @@
#include <util/reconstructible.h>
namespace Genode {
template <typename, typename> struct Attempt;
template <typename, typename> struct Unique_attempt;
template <typename, typename> struct [[nodiscard]] Attempt;
template <typename, typename> struct [[nodiscard]] Unique_attempt;
/**
* Type used for results with no return value but error conditions
@ -86,8 +86,8 @@ class Genode::Attempt
bool operator == (RESULT const &rhs) const {
return ok() && (_result == rhs); }
bool ok() const { return _ok; }
bool failed() const { return !_ok; }
[[nodiscard]] bool ok() const { return _ok; }
[[nodiscard]] bool failed() const { return !_ok; }
void print(Output &out) const
{
@ -155,8 +155,8 @@ class Genode::Unique_attempt : Noncopyable
bool operator == (ERROR const &rhs) const {
return failed() && (_error == rhs); }
bool ok() const { return _result.constructed(); }
bool failed() const { return !_result.constructed(); }
[[nodiscard]] bool ok() const { return _result.constructed(); }
[[nodiscard]] bool failed() const { return !_result.constructed(); }
void print(auto &out) const
{

View File

@ -538,7 +538,8 @@ Region_map_component::Region_map_component(Rpc_entrypoint &ep,
_ds_cap(_type_deduction_helper(_ds_ep.manage(&_ds)))
{
/* configure managed VM area */
_map.add_range(vm_start, align_addr(vm_size, get_page_size_log2()));
if (_map.add_range(vm_start, align_addr(vm_size, get_page_size_log2())).failed())
warning("unable to initialize region-map allocator");
Capability<Region_map> cap = ep.manage(this);
_ds.sub_rm(cap);

View File

@ -35,14 +35,15 @@ struct Genode::Attached_stack_area : Expanding_region_map_client
{
Region_map_client local_rm(Pd_session_client(pd).address_space());
local_rm.attach(Expanding_region_map_client::dataspace(), Region_map::Attr {
if (local_rm.attach(Expanding_region_map_client::dataspace(), Region_map::Attr {
.size = stack_area_virtual_size(),
.offset = { },
.use_at = true,
.at = stack_area_virtual_base(),
.executable = false,
.writeable = true
});
}).failed())
warning("unable to attach stack area to local address space");
}
};

View File

@ -388,7 +388,8 @@ void Allocator_avl_base::free(void *addr)
_destroy_block(*b);
add_range(new_addr, new_size);
add_range(new_addr, new_size).with_error([&] (Alloc_error e) {
error(__PRETTY_FUNCTION__, ": unable to release freed range: ", e); });
}

View File

@ -328,7 +328,8 @@ Heap::Heap(Ram_allocator *ram_alloc,
_chunk_size(MIN_CHUNK_SIZE)
{
if (static_addr)
_alloc->add_range((addr_t)static_addr, static_size);
if (_alloc->add_range((addr_t)static_addr, static_size).failed())
warning("unable to add static range at heap-construction time");
}

View File

@ -219,10 +219,7 @@ void Thread::free_secondary_stack(void* stack_addr)
Thread::Stack_size_result Thread::stack_size(size_t const size)
{
return _stack.convert<Stack_size_result>(
[&] (Stack *stack) {
stack->size(size);
return stack->top() - stack->base();
},
[&] (Stack *stack) { return stack->size(size); },
[&] (Stack_error e) { return e; });
}

View File

@ -130,7 +130,8 @@ struct Linker::Elf_file : File
bool const binary = (start != 0);
if (binary) {
Region_map::r()->alloc_region_at(size, start);
if (Region_map::r()->alloc_region_at(size, start).failed())
warning("unable to allocate linker-area region for ", name);;
reloc_base = 0;
return;
}

View File

@ -62,7 +62,8 @@ class Linker::Region_map
.writeable = true
}).with_result(
[&] (Genode::Region_map::Range) {
_range.add_range(base, Pd_session::LINKER_AREA_SIZE);
if (_range.add_range(base, Pd_session::LINKER_AREA_SIZE).failed())
warning("failed to initialize linker-area range allocator");
if (Linker::verbose)
log(" ", Hex(base),

View File

@ -424,7 +424,8 @@ struct Linker::Binary : private Root_object, public Elf_object
size_t const stack_size = ((size_t(*)())addr)();
/* expand stack according to the component's needs */
Thread::myself()->stack_size(stack_size);
if (Thread::myself()->stack_size(stack_size).failed())
warning("unable to set initial stack size to ", stack_size);
}
/* call 'Component::construct' function if present */

View File

@ -38,7 +38,8 @@ void Component::construct(Genode::Env &env)
ram_2.free(ds);
log("try to attach dataspace to see if it still exists");
env.rm().attach(ds, { });
if (env.rm().attach(ds, { }).failed())
warning("dataspace unexpectedly vanished");
log("attach operation succeeded");

View File

@ -61,7 +61,11 @@ class Local_fault_handler : public Entrypoint
_region_map.attach(ds, {
.size = { }, .offset = { },
.use_at = true, .at = fault.addr & ~(PAGE_SIZE - 1),
.executable = { }, .writeable = true });
.executable = { }, .writeable = true })
.with_result(
[&] (Region_map::Range) { },
[&] (Region_map::Attach_error) { warning("attach to sub rm failed"); }
);
log("returning from handle_fault");
}

View File

@ -135,8 +135,11 @@ void Component::construct(Env &env)
.use_at = true, .at = local_attach_addr,
.executable = { }, .writeable = true
}).convert<char *>(
[&] (Region_map::Range const range) { return (char *)range.start; },
[&] (Region_map::Attach_error) { return nullptr; }
[&] (Env::Local_rm::Attachment &a) {
a.deallocate = false;
return (char *)a.ptr;
},
[&] (Env::Local_rm::Error) { return nullptr; }
);
log("validate pattern in sub rm");
@ -225,7 +228,10 @@ void Component::construct(Env &env)
.size = { }, .offset = 4096,
.use_at = true, .at = DS_SUB_OFFSET_3,
.executable = { }, .writeable = true
});
}).with_result(
[&] (Region_map::Range) { },
[&] (Region_map::Attach_error) { fail("RAM attachment with offset"); }
);
validate_pattern_at(test_pattern_2(), sub_rm_base + DS_SUB_OFFSET_3);
/*
@ -238,7 +244,10 @@ void Component::construct(Env &env)
.size = 2*4096, .offset = 4096,
.use_at = true, .at = DS_SUB_OFFSET_4,
.executable = { }, .writeable = true
});
}).with_result(
[&] (Region_map::Range) { },
[&] (Region_map::Attach_error) { fail("RAM attachment with offset and size"); }
);
validate_pattern_at(test_pattern_2(), sub_rm_base + DS_SUB_OFFSET_4);
/*

View File

@ -38,10 +38,12 @@ static void test_out_of_bounds_access(Env &env)
Attached_ram_dataspace buf_ds(env.ram(), env.rm(), BUF_SIZE);
/* attach buffer at start of managed dataspace, leave 2nd page as guard */
sub_rm.attach(buf_ds.cap(), {
if (sub_rm.attach(buf_ds.cap(), {
.size = { }, .offset = { },
.use_at = true, .at = 0,
.executable = { }, .writeable = true });
.executable = { }, .writeable = true
}).failed())
warning("test_out_of_bounds_access: sub_rm.attach failed");
/* locally attach managed dataspace */
char * const buf_ptr = env.rm().attach(sub_rm.dataspace(), {

View File

@ -303,7 +303,7 @@ struct Backing_store
Backing_store (Genode::Allocator &alloc) : _avl(&alloc)
{
Range r = pci_drv().dma();
_avl.add_range(r.start, r.size);
(void)_avl.add_range(r.start, r.size);
}
};

View File

@ -94,7 +94,8 @@ void * Lx_kit::Mem_allocator::alloc(size_t const size, size_t const align,
*/
Buffer & buffer = alloc_buffer(max(size + 1, min_buffer_size));
_mem.add_range(buffer.virt_addr(), buffer.size() - 1);
if (_mem.add_range(buffer.virt_addr(), buffer.size() - 1).failed())
warning("Lx_kit::Mem_allocator unable to extend virtual allocator");
/* re-try allocation */
void * const virt_addr = _mem.alloc_aligned(size, (unsigned)log2(align)).convert<void *>(

View File

@ -39,7 +39,7 @@ void Writer::start_iteration(Directory &root,
_interface_registry.clear();
_buffer.clear();
_buffer.append<Section_header_block>();
(void)_buffer.append<Section_header_block>(); /* header always fits in */
_empty_section = true;
}
catch (Append_file::Create_failed) {

View File

@ -94,7 +94,8 @@ int Libc::Mem_alloc_impl::Dataspace_pool::expand(size_t size, Range_allocator *a
}
/* add new local address range to our local allocator */
alloc->add_range(range.start, range.num_bytes);
if (alloc->add_range(range.start, range.num_bytes).failed())
warning("libc is unable to extend range allocator of dataspace pool");
/* now that we have new backing store, allocate Dataspace structure */
return alloc->alloc_aligned(sizeof(Dataspace), 2).convert<int>(

View File

@ -229,7 +229,8 @@ struct Gpu::Vram
_cap(gpu.alloc_vram(_elem.id(), size)),
_alloc(&md_alloc)
{
_alloc.add_range(0, Genode::Dataspace_client(_cap).size());
if (_alloc.add_range(0, Genode::Dataspace_client(_cap).size()).failed())
Genode::warning("unable to initialize Gpu::Vram allocator");
}
struct Allocation

View File

@ -303,7 +303,8 @@ class Lima::Call
* The end of range correspondes to LIMA_VA_RESERVE_START
* in Linux minus the page we omit at the start.
*/
_alloc.add_range(0x1000, 0xfff00000ul - 0x1000);
if(_alloc.add_range(0x1000, 0xfff00000ul - 0x1000).failed())
Genode::warning("unable to initialize Gpu::Vram allocator");
}
Gpu::Virtual_address alloc(uint32_t size)

View File

@ -648,14 +648,14 @@ class Genode::Packet_stream_source : private Packet_stream_base
Ack_queue::CONSUMER))
{
/* initialize packet allocator */
_packet_alloc.add_range(_bulk_buffer_offset,
_bulk_buffer_size);
if (_packet_alloc.add_range(_bulk_buffer_offset, _bulk_buffer_size).failed())
warning("unable to initialize packet-stream source allocator");
}
~Packet_stream_source()
{
_packet_alloc.remove_range(_bulk_buffer_offset,
_bulk_buffer_size);
if (_packet_alloc.remove_range(_bulk_buffer_offset, _bulk_buffer_size).failed())
warning("packet-stream source allocator in bad state at destruction time");
}
using Packet_stream_base::packet_valid;

View File

@ -181,7 +181,9 @@ class Main
{
policy.id.with_result(
[&] (Trace::Policy_id const policy_id) {
_trace.trace(id, policy_id, buffer_size); },
if (_trace.trace(id, policy_id, buffer_size).failed())
warning("failed to enable tracing with policy '", policy_name, "'");
},
[&] (auto) {
warning("skip tracing because of invalid policy '", policy_name, "'");
});

View File

@ -110,7 +110,8 @@ class Acpi::Memory
Memory(Env &env, Allocator &heap) : _env(env), _heap(heap)
{
_range.add_range(0, ~0UL);
if (_range.add_range(0, ~0UL).failed())
warning("unable to initialize Memory::_range");
}
addr_t map_region(addr_t const req_base, addr_t const req_size)
@ -182,20 +183,23 @@ class Acpi::Memory
_range.construct_metadata((void *)loop_region.base(), _env, loop_region);
Io_mem &io_mem = *_range.metadata((void *)loop_region.base());
/*
* We attach the I/O memory dataspace into a virtual-memory window,
* which starts at _io_region.base(). Therefore, the attachment
* address is the offset of loop_region.base() from
* _io_region.base().
*/
_acpi_window.attach(_range.metadata((void *)loop_region.base())->connection->dataspace(), {
if (_acpi_window.attach(io_mem.connection->dataspace(), {
.size = loop_region.size(),
.offset = { },
.use_at = true,
.at = loop_region.base() - _io_region->base(),
.executable = { },
.writeable = { }
});
}).failed())
warning("unable to attach io_mem to ACPI window: ", io_mem.region);
return _acpi_ptr(req_base);
}

View File

@ -121,7 +121,8 @@ Dma_allocator::Dma_allocator(Allocator & md_alloc,
{
/* 0x1000 - 4GB */
enum { DMA_SIZE = 0xffffe000 };
_dma_alloc.add_range(0x1000, DMA_SIZE);
if (_dma_alloc.add_range(0x1000, DMA_SIZE).failed())
warning("unable to add 0x1000 range to DMA allocator");
/*
* Interrupt address range is special handled and in general not
@ -130,7 +131,8 @@ Dma_allocator::Dma_allocator(Allocator & md_alloc,
* (March 2023, Revision 4.1)
*/
enum { IRQ_RANGE_BASE = 0xfee00000u, IRQ_RANGE_SIZE = 0x100000 };
_dma_alloc.remove_range(IRQ_RANGE_BASE, IRQ_RANGE_SIZE);
if (_dma_alloc.remove_range(IRQ_RANGE_BASE, IRQ_RANGE_SIZE).failed())
warning("unable to exclude IRQ range from DMA allocator");
}

View File

@ -59,12 +59,8 @@ class Cpu::Trace
_trace.destruct();
_trace.construct(_env, _ram_quota, _arg_quota);
/*
* Explicitly re-trigger import of subjects. Otherwise
* stored trace ids are not valid if used with subject_info(id)
* and we get exception thrown about unknown ids.
*/
_trace->_retry<Genode::Trace::Session::Alloc_rpc_error>([&] {
/* explicitly re-trigger import of subjects */
(void)_trace->_retry<Genode::Trace::Session::Alloc_rpc_error>([&] {
return _trace->call<Genode::Trace::Session::Rpc_subjects>(); });
_subject_id_reread ++;

View File

@ -120,8 +120,11 @@ class Vmm::Config
public:
Config(Heap & heap) : _heap(heap) {
_mmio_alloc.add_range(VIRTIO_MMIO_START, VIRTIO_MMIO_SIZE); }
Config(Heap & heap) : _heap(heap)
{
if (_mmio_alloc.add_range(VIRTIO_MMIO_START, VIRTIO_MMIO_SIZE).failed())
warning("failed to add virtio MMIO range");
}
bool initrd() const { return _initrd_name.valid(); }

View File

@ -75,11 +75,11 @@ class Driver::Expanding_page_table_allocator
_virt_addr((addr_t)_dataspace.local_addr<void>()),
_phys_addr(pd.dma_addr(_dataspace.cap()))
{
_range_alloc.add_range(_phys_addr, size);
(void)_range_alloc.add_range(_phys_addr, size);
}
~Element() {
_range_alloc.remove_range(_phys_addr, _dataspace.size()); }
(void)_range_alloc.remove_range(_phys_addr, _dataspace.size()); }
bool matches(addr_t pa)
{

View File

@ -61,7 +61,8 @@ void Sup::Gmm::_add_one_slice()
_slices[_slice_index(Offset{attach_base})] = ds;
_alloc.add_range(attach_base, slice_size);
if (_alloc.add_range(attach_base, slice_size).failed())
warning("unable to to add Gmm slice to range allocator");
/* update allocation size */
_size_pages = { (attach_base + slice_size) >> PAGE_SHIFT };