ChibiOS/os/oslib/include/chmemchecks.h

169 lines
5.7 KiB
C

/*
ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
This file is part of ChibiOS.
ChibiOS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
ChibiOS is distributed in the hope that 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/>.
*/
/**
* @file oslib/include/chmemchecks.h
* @brief Memory areas and pointers validation macros and structures.
*
* @addtogroup oslib_memareas
* @{
*/
#ifndef CHMEMCHECKS_H
#define CHMEMCHECKS_H
/*===========================================================================*/
/* Module constants. */
/*===========================================================================*/
/*===========================================================================*/
/* Module pre-compile time settings. */
/*===========================================================================*/
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
/*===========================================================================*/
/* Module data structures and types. */
/*===========================================================================*/
/**
* @brief Type of a memory area.
*/
typedef struct {
/**
* @brief Memory area base.
* @note Value -1 is reserved as end-on-array marker.
*/
uint8_t *base;
/**
* @brief Memory area size.
* @note Value 0 represents the whole address space and is only valid
* when @p base is also zero.
*/
size_t size;
} memory_area_t;
/*===========================================================================*/
/* Module macros. */
/*===========================================================================*/
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#if !defined(__DOXYGEN__)
extern const memory_area_t __ch_mem_writable_areas[];
extern const memory_area_t __ch_mem_readable_areas[];
extern const memory_area_t __ch_mem_executable_areas[];
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if CH_CFG_USE_MEMCHECKS == TRUE
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 chMemIsAddressExecutableX(const void *p);
#endif
#ifdef __cplusplus
}
#endif
/*===========================================================================*/
/* Module inline functions. */
/*===========================================================================*/
/**
* @brief Memory area check.
* @details Checks if specified area 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
* @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.
*
* @xclass
*/
static inline bool chMemIsAreaWithinX(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;
chDbgAssert(mem_base <= mem_end, "invalid memory area");
return (bool)((base <= end) && (base >= 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.*/
bool chMemIsAreaWritableX(const void *p,
size_t size,
unsigned align) {
(void)p;
(void)size;
(void)align;
return true;
}
bool chMemIsAreaReadableX(const void *p,
size_t size,
unsigned align) {
(void)p;
(void)size;
(void)align;
return true;
}
bool chMemIsAddressExecutableX(const void *p) {
(void)p;
return true;
}
#endif
#endif /* CHMEMCHECKS_H */
/** @} */