summaryrefslogtreecommitdiff
path: root/common_modules/module_manager/src/txm_module_manager_util.c
diff options
context:
space:
mode:
authorFrédéric Desbiens <[email protected]>2026-09-28 10:13:22 -0400
committerGitHub <[email protected]>2026-09-28 10:13:22 -0400
commit2930618cfeb77af872c7b3e13bf584c85c2ac153 (patch)
treee2ec67d7314921beb97c258d6a5a09e07b089e24 /common_modules/module_manager/src/txm_module_manager_util.c
parent3276b0efd57d5e74f2edb1cec15add6e24085611 (diff)
Merge commit from fork
* Refused to give back the memory of a live kernel object A module allocates the control blocks of its kernel objects from the Module Manager's object pool and can ask for that memory back by address. The manager released it whatever was in it, including a control block the kernel was still using. Deallocation is not deletion: the object stays on the created list for its type, a thread stays wherever it was on the ready or suspension lists and stays schedulable, and an active timer stays on the timer list. Nothing on those paths consults a control block ID, so nothing about the memory having been freed stops the kernel from using it -- a created list walk reads a name pointer out of it and follows that pointer, a create or delete of another object of the same type writes through the created links in it, the scheduler switches the stack pointer to the word at offset 8 of it and pops a saved processor state, and timer expiration calls the function pointer in it. Meanwhile the byte pool is free to hand those same bytes to the next allocation, so what the kernel goes on reading as a control block becomes whatever the next owner of the memory puts there, through ordinary create services. The manager now refuses to release memory that holds an object which is still created, and returns TX_DELETE_ERROR without touching the allocation, the object or the memory. Deleting the object first is what makes its memory releasable, which is the sequence the delete dispatchers already follow and the one module authors are told to follow. The question is answered from the kernel's created lists, not from the control block. A control block ID is not evidence that an object is there: an allocation that was never created can be carrying the value of an ID, and refusing on that would strand memory a module is entitled to have back. Deallocation is also given an address and nothing else, so unlike a typed service it cannot be told which list to search, and each of the eight lists is searched in turn. The search is not narrowed by the size of the allocation, because that would be sound only if every object had been created through a size-checked path, and a module running without memory protection creates objects through no such path -- a queue at the start of a thread-sized allocation is a case the tests here cover. Each list is searched in its own interrupts-disabled window, bounded by the count the kernel keeps beside it, so the longest window is the length of one type's list and a list whose links have been damaged cannot make the search run on. The whole search is one pass over the objects the system has created, paid once per object deallocation. Storage that is not an object is released exactly as before, which is what keeps cleanup after a create that failed or was abandoned working, and what makes the release each delete dispatcher performs after a successful delete go through. The address the request arrives with is now checked before the manager's private header in front of it is read, rather than partly after. The size of the allocation comes from that header, so there is nothing to validate a size against until the header has been read, and the previous order established only where the header started: a header that began inside the pool and ended past it had its size word read from outside the pool. That check has moved out of the dispatcher into _txm_module_manager_param_check_object_for_deallocation, alongside the other parameter checks the dispatch table uses and where a test can reach it, and it now also refuses a size that would carry the end of the allocation past the top of the address space instead of wrapping it, since a wrapped end compares as though the allocation were inside the pool. The 301 expectations in the new test drive all eight object types through allocate, create, a refused deallocation, delete, and a deallocation that succeeds, and assert after the refusal that nothing reached the pool, that the allocation is still on the module's list at the head of it, that the control block still carries its ID and that the object is still live. They cover storage that was never created, storage carrying nothing but a plausible ID for each of the eight types, an object at the start of an oversized allocation, every aligned interior offset of a live object with that object's own ID planted at it, an application-owned object outside the pool, another module's allocations both live and raw, releasing the head of a list of several and releasing the same address twice, a type that has no created list, the bounds on the search against a list longer than its count and against a count larger than its list for every type, the boundary addresses at both ends of the pool, a crafted size, and an object pool that was never created. Removing the refusal fails 43 of them; removing the size wrap guard fails one. Line and branch coverage of the three new functions and of the changed _txm_module_manager_object_deallocate is 100%, with one exception that is test scaffolding rather than product code: the host shim's stand-in for TX_RESTORE has an underflow guard, and the test asserts that branch is never taken. All 99 tests pass in each of the five configurations the tree builds with GCC 14. Assisted-by: Claude Code (Opus 5) <[email protected]> * Removed the object deallocation bounds check superseded by the search _txm_module_manager_param_check_object_for_deallocation() bounded the private header in front of a caller's address before the deallocator read it. The deallocator no longer reads that header: it finds the allocation by searching the module's own allocation list, which never dereferences the address, so the bounds check now guards a read that does not happen. The function, its prototype, its macro and its one call site in the txm_module_object_deallocate dispatcher are removed, along with the twelve expectations that covered it and three declarations left unused by their removal. The search proves more than the check did: the bounds test established only that the header lay inside the object pool, while the search establishes that the address is the exact start of one of this module's allocations. The live object deallocation test holds 289 expectations and passes. Reverting the live-object guard still fails 51 of them, the same 51 as before the deletion, so nothing the removed expectations covered was load-bearing. The full suite passes 108/108 in default_build_coverage, with no warnings. Assisted-by: Claude Code (Opus 5) <[email protected]>
Diffstat (limited to 'common_modules/module_manager/src/txm_module_manager_util.c')
-rw-r--r--common_modules/module_manager/src/txm_module_manager_util.c110
1 files changed, 103 insertions, 7 deletions
diff --git a/common_modules/module_manager/src/txm_module_manager_util.c b/common_modules/module_manager/src/txm_module_manager_util.c
index ba7e5a29..756120fa 100644
--- a/common_modules/module_manager/src/txm_module_manager_util.c
+++ b/common_modules/module_manager/src/txm_module_manager_util.c
@@ -1021,13 +1021,18 @@ UINT exact_object;
/* This function determines whether an address is the exact address */
/* of an object on the kernel's created list for a module object type. */
/* */
-/* This is how an object the application created and shared with a */
-/* module is authenticated. Such an object is not in the manager's */
-/* object pool and the manager has no allocation record of it, so the */
-/* kernel's own created list is the only record of it that a module */
-/* cannot influence. Being on that list establishes at once that the */
-/* address is an object start, that the object is of this type, and */
-/* that it is created. */
+/* The created list is the record the create and delete services */
+/* maintain, so being on it establishes at once that the address is an */
+/* object start rather than an address inside an object, that the */
+/* object is of this type, and that it has not been deleted. It is */
+/* also the only record of an object the application created and */
+/* shared with a module, since the manager allocated no such object. */
+/* */
+/* Nothing the module can influence is consulted. In particular the */
+/* control block ID is not: a module can arrange for the value of an */
+/* ID to appear inside memory it owns, and distinct object types of */
+/* equal size exist, so an ID is not evidence that an object is there */
+/* nor of what type it is. */
/* */
/* INPUT */
/* */
@@ -1045,6 +1050,7 @@ UINT exact_object;
/* */
/* CALLED BY */
/* */
+/* _txm_module_manager_live_object_check Module object liveness check */
/* _txm_module_manager_param_check_typed_object_for_use */
/* Module object authentication */
/* */
@@ -1406,6 +1412,96 @@ UINT status;
/* */
/* FUNCTION RELEASE */
/* */
+/* _txm_module_manager_live_object_check PORTABLE C */
+/* 6.4.3 */
+/* AUTHOR */
+/* */
+/* Eclipse ThreadX contributors */
+/* */
+/* DESCRIPTION */
+/* */
+/* This function determines whether an address is the exact address */
+/* of a live kernel object of any type the Module Manager knows. */
+/* */
+/* It answers the question object deallocation has to ask before it */
+/* gives memory back: is the kernel still going to use these bytes as */
+/* a control block. Deallocation is told an address and nothing else, */
+/* so unlike the checks a typed service makes it cannot be given the */
+/* type to look for, and every type has to be looked for in turn. */
+/* */
+/* The eight created lists are searched whatever the size of the */
+/* allocation at the address. Skipping a type whose control block is */
+/* larger than the allocation would be sound only if every object had */
+/* been created through a size-checked path, and a module running */
+/* without memory protection creates objects without one. */
+/* */
+/* Each list is searched in its own interrupts-disabled window rather */
+/* than all of them in one, so the longest window is bounded by the */
+/* number of objects of a single type. The whole search costs one pass */
+/* over the objects the system has created, and is paid once per */
+/* object deallocation. */
+/* */
+/* INPUT */
+/* */
+/* object_ptr Address of object memory area */
+/* */
+/* OUTPUT */
+/* */
+/* TX_TRUE A live kernel object is there */
+/* TX_FALSE Anything else */
+/* */
+/* CALLS */
+/* */
+/* _txm_module_manager_created_object_type_check */
+/* Check one kernel created list */
+/* */
+/* CALLED BY */
+/* */
+/* _txm_module_manager_object_deallocate Deallocate object memory */
+/* */
+/* RELEASE HISTORY */
+/* */
+/* DATE NAME DESCRIPTION */
+/* */
+/* xx-xx-2026 Eclipse ThreadX Initial Version 6.4.3 */
+/* contributors */
+/* */
+/**************************************************************************/
+UINT _txm_module_manager_live_object_check(ALIGN_TYPE object_ptr)
+{
+
+UINT object_type;
+UINT status;
+
+
+ /* Assume no live object is there. */
+ status = TX_FALSE;
+
+ /* The eight module object types that have a kernel created list occupy a
+ contiguous range, so each is asked in turn by walking the range. A value in
+ the range that the per-type check does not recognise answers TX_FALSE, so a
+ type the manager stops knowing does not silently pass this search. */
+ for (object_type = ((UINT) TXM_BLOCK_POOL_OBJECT); object_type <= ((UINT) TXM_TIMER_OBJECT); object_type++)
+ {
+
+ if (_txm_module_manager_created_object_type_check(object_ptr, object_type) == TX_TRUE)
+ {
+
+ /* An address is the start of at most one object, so there is nothing
+ further to look for. */
+ status = TX_TRUE;
+ break;
+ }
+ }
+
+ return(status);
+}
+
+
+/**************************************************************************/
+/* */
+/* FUNCTION RELEASE */
+/* */
/* _txm_module_manager_util_code_allocation_size_and_alignment_get */
/* PORTABLE C */
/* 6.1 */