diff --git a/os/oslib/include/chmemchecks.h b/os/oslib/include/chmemchecks.h index b27cf3b13..039732b44 100644 --- a/os/oslib/include/chmemchecks.h +++ b/os/oslib/include/chmemchecks.h @@ -83,15 +83,15 @@ extern "C" { bool chMemIsStringWithinX(const memory_area_t *map, const char *s, size_t n); - bool chMemIsAreaContainedX(const memory_area_t areas[], - const void *p, - size_t size); - bool chMemIsAreaWritableX(void *p, - size_t size, - unsigned align); - bool chMemIsAreaReadableX(const void *p, - size_t size, - unsigned align); + bool chMemIsSpaceContainedX(const memory_area_t areas[], + const void *p, + size_t size); + bool chMemIsSpaceWritableX(void *p, + size_t size, + unsigned align); + bool chMemIsSpaceReadableX(const void *p, + size_t size, + unsigned align); bool chMemIsAddressExecutableX(const void *p); #endif #ifdef __cplusplus @@ -103,23 +103,22 @@ extern "C" { /*===========================================================================*/ /** - * @brief Memory area check. - * @details Checks if specified area belongs to the specified memory area. + * @brief Memory space inclusion check. + * @details Checks if specified space belongs to the specified memory area. * * @param[in] map pointer to a @p memory_area_t structure - * @param[in] p pointer to the area to be checked - * @param[in] size size of the area to be checked, zero is considered - * the whole address space + * @param[in] p pointer to the memory space to be checked + * @param[in] size size of the memory space to be checked, zero is + * considered the whole address space * @return The test result. - * @retval true if the area is entirely contained within one of the - * specified areas. - * @retval false if the area check failed. + * @retval true if the memory space is entirely contained. + * @retval false if the memory space is not entirely contained. * * @xclass */ -static inline bool chMemIsAreaWithinX(const memory_area_t *map, - const void *p, - size_t size) { +static inline bool chMemIsSpaceWithinX(const memory_area_t *map, + const void *p, + size_t size) { const uint8_t *mem_base = (const uint8_t *)map->base; const uint8_t *mem_end = mem_base + map->size - (size_t)1; const uint8_t *base = (const uint8_t *)p; @@ -130,6 +129,33 @@ static inline bool chMemIsAreaWithinX(const memory_area_t *map, return (bool)((base <= end) && (base >= mem_base) && (end <= mem_end)); } +/** + * @brief Memory space intersection check. + * @details Checks if specified memory space intersects the specified memory + * area. + * + * @param[in] map pointer to a @p memory_area_t structure + * @param[in] p pointer to the memory space to be checked + * @param[in] size size of the memory space to be checked, zero is + * considered the whole address space + * @return The test result. + * @retval true if the memory space is intersecting. + * @retval false if the memory space is not intersecting. + * + * @xclass + */ +static inline bool chMemIsSpaceIntersectingX(const memory_area_t *map, + const void *p, + size_t size) { + const uint8_t *mem_base = (const uint8_t *)map->base; + const uint8_t *mem_end = mem_base + map->size - (size_t)1; + const uint8_t *base = (const uint8_t *)p; + const uint8_t *end = base + size - (size_t)1; + + return (bool)(((base >= mem_base) && (base <= mem_end)) || + ((end >= mem_base) && (end <= mem_end))); +} + #if CH_CFG_USE_MEMCHECKS == FALSE /* Stub implementations for when the functionality is disabled, areas are always reported as valid.*/ diff --git a/os/oslib/src/chmemchecks.c b/os/oslib/src/chmemchecks.c index 853f6a8c6..3fcc94623 100644 --- a/os/oslib/src/chmemchecks.c +++ b/os/oslib/src/chmemchecks.c @@ -121,31 +121,32 @@ bool chMemIsStringWithinX(const memory_area_t *map, const char *s, size_t n) { } /** - * @brief Memory area check. - * @details Checks if specified area belongs to one of the specified areas. + * @brief Memory space check. + * @details Checks if specified memory space belongs to one of the specified + * areas. * * @param[in] map array of valid areas terminated with an end * marker (base=-1) - * @param[in] p pointer to the area to be checked - * @param[in] size size of the area to be checked, zero is considered - * the whole address space + * @param[in] p pointer to the memory space to be checked + * @param[in] size size of the memory space to be checked, zero is + * considered the whole address space * @return The test result. - * @retval true if the area is entirely contained within one of the - * specified areas. - * @retval false if the area check failed. + * @retval true if the memory space is entirely contained within one + * of the specified areas. + * @retval false if the memory space check failed. * * @xclass */ -bool chMemIsAreaContainedX(const memory_area_t areas[], - const void *p, - size_t size) { +bool chMemIsSpaceContainedX(const memory_area_t areas[], + const void *p, + size_t size) { const memory_area_t *map = &areas[0]; chDbgCheck(p != NULL); /* Scanning the array of the valid areas for a mismatch.*/ while (map->base != (uint8_t *)-1) { - if (chMemIsAreaWithinX(map, p, size)) { + if (chMemIsSpaceWithinX(map, p, size)) { return true; } map++; @@ -157,27 +158,27 @@ bool chMemIsAreaContainedX(const memory_area_t areas[], #if (CH_CFG_USE_MEMCHECKS == TRUE) || defined(__DOXYGEN__) /** - * @brief Memory writable area check. - * @details Checks if specified pointer belongs to one of the system-defined - * writable areas and is aligned as specified. + * @brief Writable memory space check. + * @details Checks if the specified memory space belongs to one of the + * system-defined writable areas and is aligned as specified. * @note @p __ch_mem_writable_areas must be the name of a global * @p memory_area_t array terminated with an end marker (-1, 0). * - * @param[in] p pointer to the area to be checked - * @param[in] size size of the area to be checked, zero is considered - * the whole address space + * @param[in] p pointer to the memory space to be checked + * @param[in] size size of the memory space to be checked, zero is + * considered the whole address space * @param[in] align required pointer alignment to be checked, must be * a power of two * @return The test result. - * @retval true if the area is entirely contained within one of the - * system-defined writable areas. - * @retval false if the area check failed. + * @retval true if the memory space is entirely contained within one + * memory space system-defined writable areas. + * @retval false if the memory space check failed. * * @xclass */ -bool chMemIsAreaWritableX(void *p, - size_t size, - unsigned align) { +bool chMemIsSpaceWritableX(void *p, + size_t size, + unsigned align) { chDbgCheck((align & (align - 1U)) == 0U); @@ -185,31 +186,31 @@ bool chMemIsAreaWritableX(void *p, return false; } - return chMemIsAreaContainedX(__ch_mem_writable_areas, p, size); + return chMemIsSpaceContainedX(__ch_mem_writable_areas, p, size); } /** - * @brief Memory readable area check. - * @details Checks if specified pointer belongs to one of the system-defined - * readable areas and is aligned as specified. + * @brief Readable memory space check. + * @details Checks if specified memory space belongs to one of the + * system-defined readable areas and is aligned as specified. * @note @p __ch_mem_readable_areas must be the name of a global * @p memory_area_t array terminated with an end marker (-1, 0). * - * @param[in] p pointer to the area to be checked - * @param[in] size size of the area to be checked, zero is considered - * the whole address space + * @param[in] p pointer to the memory space to be checked + * @param[in] size size of the memory space to be checked, zero is + * considered the whole address space * @param[in] align required pointer alignment to be checked, must be * a power of two * @return The test result. - * @retval true if the area is entirely contained within one of the - * system-defined readable areas. - * @retval false if the area check failed. + * @retval true if the memory space is entirely contained within one + * of the system-defined readable areas. + * @retval false if the memory space check failed. * * @xclass */ -bool chMemIsAreaReadableX(const void *p, - size_t size, - unsigned align) { +bool chMemIsSpaceReadableX(const void *p, + size_t size, + unsigned align) { chDbgCheck((align & (align - 1U)) == 0U); @@ -217,7 +218,7 @@ bool chMemIsAreaReadableX(const void *p, return false; } - return chMemIsAreaContainedX(__ch_mem_readable_areas, p, size); + return chMemIsSpaceContainedX(__ch_mem_readable_areas, p, size); } /** @@ -243,7 +244,7 @@ bool chMemIsAddressExecutableX(const void *p) { return false; } - return chMemIsAreaContainedX(__ch_mem_executable_areas, p, 1); + return chMemIsSpaceContainedX(__ch_mem_executable_areas, p, 1); } #endif /* CH_CFG_USE_MEMCHECKS == TRUE */ diff --git a/os/oslib/src/chmemheaps.c b/os/oslib/src/chmemheaps.c index bcd4ce5b8..7a0205d7e 100644 --- a/os/oslib/src/chmemheaps.c +++ b/os/oslib/src/chmemheaps.c @@ -447,9 +447,9 @@ bool chHeapIntegrityCheck(memory_heap_t *heapp) { } /* Validating the found free block.*/ - if (!chMemIsAreaWithinX(&heapp->area, - (void *)hp, - H_FREE_FULLSIZE(hp))) { + if (!chMemIsSpaceWithinX(&heapp->area, + (void *)hp, + H_FREE_FULLSIZE(hp))) { result = true; break; } diff --git a/os/sb/host/sbelf.c b/os/sb/host/sbelf.c index 4f32cf089..360861445 100644 --- a/os/sb/host/sbelf.c +++ b/os/sb/host/sbelf.c @@ -206,9 +206,9 @@ static msg_t allocate_section(elf_load_context_t *ctxp, /* Checking if the section can fit into the destination memory area.*/ load_address = ctxp->map->base + shp->sh_addr; - if (!chMemIsAreaWithinX(ctxp->map, - (const void *)load_address, - (size_t)shp->sh_size)) { + if (!chMemIsSpaceWithinX(ctxp->map, + (const void *)load_address, + (size_t)shp->sh_size)) { return CH_RET_ENOMEM; } @@ -290,9 +290,9 @@ static msg_t reloc_entry(elf_load_context_t *ctxp, relocation_address = (uint32_t)ctxp->map->base + lip->address + rp->r_offset; - if (!chMemIsAreaWithinX(ctxp->map, - (const void *)relocation_address, - sizeof (uint32_t))) { + if (!chMemIsSpaceWithinX(ctxp->map, + (const void *)relocation_address, + sizeof (uint32_t))) { return CH_RET_ENOMEM; } @@ -372,7 +372,7 @@ static msg_t load_relocate_section(elf_load_context_t *ctxp, /* Checking if the section can fit into the destination memory area.*/ load_address = ctxp->map->base + lip->address; - if (!chMemIsAreaWithinX(ctxp->map, + if (!chMemIsSpaceWithinX(ctxp->map, (const void *)load_address, lip->bits_size)) { return CH_RET_ENOMEM; diff --git a/os/sb/host/sbhost.c b/os/sb/host/sbhost.c index be7b46ce0..b351dc8ec 100644 --- a/os/sb/host/sbhost.c +++ b/os/sb/host/sbhost.c @@ -61,7 +61,7 @@ bool sb_is_valid_read_range(sb_class_t *sbcp, const void *start, size_t size) { const sb_memory_region_t *rp = &sbcp->config->regions[0]; do { - if (chMemIsAreaContainedX(&rp->area, start, size)) { + if (chMemIsSpaceWithinX(&rp->area, start, size)) { return true; } rp++; @@ -74,7 +74,7 @@ bool sb_is_valid_write_range(sb_class_t *sbcp, void *start, size_t size) { const sb_memory_region_t *rp = &sbcp->config->regions[0]; do { - if (chMemIsAreaWithinX(&rp->area, start, size)) { + if (chMemIsSpaceWithinX(&rp->area, start, size)) { return rp->writeable; } rp++;