1 /*************************************************************************** 2 * Copyright (c) 2024 Microsoft Corporation 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the MIT License which is available at 6 * https://opensource.org/licenses/MIT. 7 * 8 * SPDX-License-Identifier: MIT 9 **************************************************************************/ 10 11 12 /**************************************************************************/ 13 /**************************************************************************/ 14 /** */ 15 /** ThreadX Component */ 16 /** */ 17 /** Module Manager */ 18 /** */ 19 /**************************************************************************/ 20 /**************************************************************************/ 21 22 23 /**************************************************************************/ 24 /* */ 25 /* COMPONENT DEFINITION RELEASE */ 26 /* */ 27 /* txm_module_manager_util.h PORTABLE C */ 28 /* 6.3.0 */ 29 /* AUTHOR */ 30 /* */ 31 /* Scott Larson, Microsoft Corporation */ 32 /* */ 33 /* DESCRIPTION */ 34 /* */ 35 /* This file declares prototypes of utility functions used by the */ 36 /* module manager. */ 37 /* */ 38 /* RELEASE HISTORY */ 39 /* */ 40 /* DATE NAME DESCRIPTION */ 41 /* */ 42 /* 09-30-2020 Scott Larson Initial Version 6.1 */ 43 /* 04-02-2021 Scott Larson Modified comment(s) and */ 44 /* optimized object checks, */ 45 /* resulting in version 6.1.6 */ 46 /* 10-31-2023 Tiejun Zhou Modified comment(s) and */ 47 /* improved object check, */ 48 /* resulting in version 6.3.0 */ 49 /* */ 50 /**************************************************************************/ 51 52 #ifndef TXM_MODULE_MANAGER_UTIL_H 53 #define TXM_MODULE_MANAGER_UTIL_H 54 55 /* Define check macros for modules. */ 56 57 #define TXM_MODULE_MANAGER_CHECK_OUTSIDE_DATA(module_instance, obj_ptr, obj_size) \ 58 (!(TXM_MODULE_MANAGER_CHECK_INSIDE_DATA(module_instance, obj_ptr, obj_size))) 59 60 #define TXM_MODULE_MANAGER_CHECK_INSIDE_CODE(module_instance, obj_ptr, obj_size) \ 61 (((obj_ptr) < ((obj_ptr) + (obj_size))) && \ 62 ((obj_ptr) >= (ALIGN_TYPE) module_instance -> txm_module_instance_code_start) && \ 63 (((obj_ptr) + (obj_size)) <= ((ALIGN_TYPE) module_instance -> txm_module_instance_code_end + 1))) 64 65 #define TXM_MODULE_MANAGER_CHECK_OUTSIDE_CODE(module_instance, obj_ptr, obj_size) \ 66 (!(TXM_MODULE_MANAGER_CHECK_INSIDE_CODE(module_instance, obj_ptr, obj_size))) 67 68 /* Add sizeof(TXM_MODULE_ALLOCATED_OBJECT) to pool start because the object can't exist before that. */ 69 #define TXM_MODULE_MANAGER_CHECK_INSIDE_OBJ_POOL(module_instance, obj_ptr, obj_size) \ 70 ((_txm_module_manager_object_pool_created == TX_TRUE) && \ 71 ((obj_ptr) < ((obj_ptr) + (obj_size))) && \ 72 (((obj_ptr) >= ((ALIGN_TYPE) _txm_module_manager_object_pool.tx_byte_pool_start + sizeof(TXM_MODULE_ALLOCATED_OBJECT))) && \ 73 (((obj_ptr) + (obj_size)) <= (ALIGN_TYPE) (_txm_module_manager_object_pool.tx_byte_pool_start + _txm_module_manager_object_pool.tx_byte_pool_size)))) 74 75 /* Define macros for module. */ 76 77 #define TXM_MODULE_MANAGER_ENSURE_INSIDE_MODULE(module_instance, obj_ptr, obj_size) \ 78 (TXM_MODULE_MANAGER_CHECK_INSIDE_DATA(module_instance, obj_ptr, obj_size) || \ 79 TXM_MODULE_MANAGER_CHECK_INSIDE_CODE(module_instance, obj_ptr, obj_size)) 80 81 #define TXM_MODULE_MANAGER_ENSURE_INSIDE_MODULE_DATA(module_instance, obj_ptr, obj_size) \ 82 TXM_MODULE_MANAGER_CHECK_INSIDE_DATA(module_instance, obj_ptr, obj_size) 83 84 #define TXM_MODULE_MANAGER_ENSURE_OUTSIDE_MODULE(module_instance, obj_ptr, obj_size) \ 85 (TXM_MODULE_MANAGER_CHECK_OUTSIDE_DATA(module_instance, obj_ptr, obj_size) && \ 86 TXM_MODULE_MANAGER_CHECK_OUTSIDE_CODE(module_instance, obj_ptr, obj_size)) 87 88 #define TXM_MODULE_MANAGER_ENSURE_INSIDE_OBJ_POOL(module_instance, obj_ptr, obj_size) \ 89 TXM_MODULE_MANAGER_CHECK_INSIDE_OBJ_POOL(module_instance, obj_ptr, obj_size) 90 91 /* Define macros for parameter types. */ 92 93 /* Buffers we read from can be in RW/RO/Shared areas. */ 94 #define TXM_MODULE_MANAGER_PARAM_CHECK_BUFFER_READ(module_instance, buffer_ptr, buffer_size) \ 95 ((TXM_MODULE_MANAGER_ENSURE_INSIDE_MODULE(module_instance, buffer_ptr, buffer_size)) || \ 96 ((void *) (buffer_ptr) == TX_NULL)) 97 98 /* Buffers we write to can only be in RW/Shared areas. */ 99 #define TXM_MODULE_MANAGER_PARAM_CHECK_BUFFER_WRITE(module_instance, buffer_ptr, buffer_size) \ 100 ((TXM_MODULE_MANAGER_ENSURE_INSIDE_MODULE_DATA(module_instance, buffer_ptr, buffer_size)) || \ 101 ((void *) (buffer_ptr) == TX_NULL)) 102 103 /* Kernel objects should be outside the module at the very least. */ 104 #define TXM_MODULE_MANAGER_PARAM_CHECK_OBJECT_FOR_USE(module_instance, obj_ptr, obj_size) \ 105 (TXM_MODULE_MANAGER_ENSURE_OUTSIDE_MODULE(module_instance, obj_ptr, obj_size) || \ 106 (_txm_module_manager_created_object_check(module_instance, (void *)obj_ptr) == TX_FALSE) || \ 107 ((void *) (obj_ptr) == TX_NULL)) 108 109 /* When creating an object, the object must be inside the object pool. */ 110 #define TXM_MODULE_MANAGER_PARAM_CHECK_OBJECT_FOR_CREATION(module_instance, obj_ptr, obj_size) \ 111 ((TXM_MODULE_MANAGER_ENSURE_INSIDE_OBJ_POOL(module_instance, obj_ptr, obj_size) && \ 112 (_txm_module_manager_object_size_check(obj_ptr, obj_size) == TX_SUCCESS)) || \ 113 (_txm_module_manager_created_object_check(module_instance, (void *)obj_ptr) == TX_FALSE) || \ 114 ((void *) (obj_ptr) == TX_NULL)) 115 116 /* Strings we dereference can be in RW/RO/Shared areas. */ 117 #define TXM_MODULE_MANAGER_PARAM_CHECK_DEREFERENCE_STRING(module_instance, string_ptr) \ 118 ((TXM_MODULE_MANAGER_ENSURE_INSIDE_MODULE(module_instance, string_ptr, 1)) || \ 119 ((void *) (string_ptr) == TX_NULL)) 120 121 #define TXM_MODULE_MANAGER_UTIL_MAX_VALUE_OF_TYPE_UNSIGNED(type) ((1ULL << (sizeof(type) * 8)) - 1) 122 123 #define TXM_MODULE_MANAGER_UTIL_MATH_ADD_ULONG(augend, addend, result) \ 124 if ((ULONG)-1 - (augend) < (addend)) \ 125 { \ 126 return(TXM_MODULE_MATH_OVERFLOW); \ 127 } \ 128 else \ 129 { \ 130 (result) = (augend) + (addend); \ 131 } 132 133 /* Define utility functions. */ 134 135 UINT _txm_module_manager_object_memory_check(TXM_MODULE_INSTANCE *module_instance, ALIGN_TYPE object_ptr, ULONG object_size); 136 UINT _txm_module_manager_object_size_check(ALIGN_TYPE object_ptr, ULONG object_size); 137 UINT _txm_module_manager_object_name_compare(CHAR *object_name1, UINT object_name1_length, CHAR *object_name2); 138 UCHAR _txm_module_manager_created_object_check(TXM_MODULE_INSTANCE *module_instance, void *object_ptr); 139 UINT _txm_module_manager_util_code_allocation_size_and_alignment_get(TXM_MODULE_PREAMBLE *module_preamble, ULONG *code_alignment_dest, ULONG *code_allocation_size_dest); 140 141 #endif 142