Resource Checker
Overview
The resource checker reports a resource acquired in a function and not released on some path out of the function, a resource released a second time, a resource used after its release, an acquire or an assignment over an identifier that may still hold a resource, a loop that reaches an identifier’s declaration again while it may still hold one, a marker on a path where the resource may already be released, and a parameter released before a failure return that leaves it to the caller. It covers Gantry’s reference counted memory, events and Stow values, and common Zephyr resources such as IRQ locks, mutexes, spinlocks and net_buf. It runs automatically when Gantry is included as a module. The Stow sources that stow-gen writes for an application all come from one template, so they are checked once, by a test in tests/resource_check that renders the tests/stow schema, which has a field of every kind.
The checker is a Python tool built on tree-sitter. It reads the unexpanded source, so it needs no build, include paths or compile flags. The arms of an #if in a function body are checked as alternative branches, so a function must be clean in every configuration. An arm under #if 0 is never compiled, and an #if 1 arm is the only one that is. It lives in tools/resource_check as the standalone resource-check package and does not depend on Gantry, so it can be installed into other projects.
Usage
uv run --project tools/resource_check resource-check <path>... [--annotations <file-or-dir>]...
Each <path> is a .c file or a directory searched for .c files. Functions defined in the headers that annotations are read from, such as static inline functions, are checked too, once per run. The checker exits non-zero if it reports any of these, a policy violation, an annotation error or code it could not parse.
Annotations are used to determine what function acquire or release resources. The resource checker includes annotation for common Zephyr functions that allocate or release resources. Annotations are read from functions in the checked source, --annotations provides additional files to read annotations from.
Annotations
What a function acquires or releases is declared with tags in the doc comment directly above its declaration, definition or #define:
/** @acquires memory return on-non-null */
void* mem_alloc(size_t size);
/** @releases memory block */
void mem_unref(void* block);
@acquires <class> <target> [on-success | on-non-null]Calling the function acquires a resource of the given class.
@releases <class | *> <target> [on-success | on-non-null]Calling the function releases a resource. The class
*releases the identifier whatever class it was acquired as.
The class is a name for the kind of resource, and a release only matches an acquire of the same class. The target is a parameter name, or return for the variable that receives the result.
Most allocations only acquire when they succeed. on-success means success is a return value of 0 (SUCCESS), and on-non-null means a non-NULL return value. On the failure branch of a test of the result nothing is held, so returning there is not a leak:
void* buf = MEM_ALLOC(16);
if (buf == NULL)
{
return -ENOMEM; // fine, nothing was allocated
}
MEM_UNREF(buf);
A release with a condition happens only when the call succeeds. On the failure branch of a test of the result the resource is still held, so the caller releases it or tries again. The result of such a call must be tested by an if, or it is reported as a policy error:
if (MSGQ_PUT_OWNED(&queue, &msg, event) != 0)
{
EVENT_UNREF(event); // the queue did not take it
}
A function whose own tag promises the same conditional release of its parameter may instead pass the result on: return give(buf);, or return ret; on the failure branch of a test of int ret = give(buf);. Both leave the resource with the caller when the call fails. The failure branch must run straight to return ret;, without a branch or anything that may change ret before it; otherwise, or when return ret; follows the join of both branches, the return is not a failure return, and a resource the function still holds there is reported as a leak.
Ownership Transfer
A function that takes ownership of an argument carries @releases on that parameter, so its callers need no marker. A function that gives a resource to its caller carries @acquires on its own target, and handing the resource to that target needs no marker either: acquiring into the target, storing a held identifier through it (*block = new_block, block->field = new_block), or returning a held identifier from a function tagged @acquires <class> return. Such a function may also return a call’s result directly, as in return net_buf_alloc(pool, K_NO_WAIT);, when the callee’s return tag has the same class and condition as its own. After a hand-off the function may still read the identifier, but releasing it is reported as a second release. A message whose queue takes ownership of the resource it carries is put with MSGQ_PUT_OWNED, which names that resource’s identifier. When ownership leaves a function any other way, through a field or an untagged out parameter, mark the point with PASS_OWNERSHIP. A marker, a @releases macro that expands to nothing, ends the obligation on every path through it, so it belongs on the one path it describes: a marker reached on a path where the identifier may already be released or handed off is reported. The checker does not infer any other transfer from a value being stored in a field, returned, or passed to an unannotated function.
/** @acquires memory block on-success */
int custom_alloc(void** block)
{
void* new_block = mem_alloc(42);
if (new_block == NULL)
{
return -ENOMEM;
}
*block = new_block;
return SUCCESS;
}
Under on-success or on-non-null the caller owns nothing when the function fails, so a hand-off followed by a literal failure return, such as return -EIO; or return NULL;, is reported as a leak, and releasing the handed-off resource where every path then returns such a failure is not a second release. After a hand-off, returning a variable or the result of a call, such as return ret;, is trusted to pass the result through, except that the result variable of a conditional call, returned on the branch where that call failed, is a failure return.
A @releases parameter is held from the start of the function, and every path must release it or hand it off, except the arm where the parameter itself is tested equal to NULL. Under on-success or on-non-null the caller keeps it when the function fails, so a path that returns a literal failure must not release it. A function that implements its class with bookkeeping rather than another tagged call, such as mem_unref_with, marks the parameter with PASS_OWNERSHIP where the obligation ends.
Custom Resources
Applications describe their own resources by annotating their functions in place. Other files see a function’s tags through headers, so a function called from other files, such as custom_alloc above, carries its tag on its header declaration. The checker then requires every caller to release the block:
/** @acquires memory block on-success */
int custom_alloc(void** block);
Function Pointers
An interface implemented by several types, such as the Stow type interface, is a struct of function pointers. A tag in the doc comment directly above a field applies to every call through a field of that name, codec->get(...) or codec.get(...), which is then checked like a call to a tagged function:
struct stow_item_interface
{
/** @acquires memory out_value on-success */
int (*get)(void* src, data_value_t* out_value);
/** @releases memory value */
void (*release)(data_value_t* value);
};
The functions assigned to the field are the interface’s implementations and stay untagged. Each one defines what holding a value of its type means, so an int’s release that does nothing and a buffer’s release that unreferences a block are both correct. Fields of one name carry the same tags, with each target at the same parameter position.
A callback passed as a parameter or held in a local is declared with a function-pointer typedef. A tag in the typedef’s doc comment applies to every call through a parameter or local declared with that type name itself, not a pointer to it, such as write(frame, user_data) for a stow_cobs_write_t write. As with fields, the functions passed as the callback are not checked against it:
/** @releases net_buf frame on-success */
typedef int (*stow_cobs_write_t)(struct net_buf* frame, void* user_data);
Obligations
A resource class need not be memory. An obligation is something that must happen exactly once, and the checker enforces it the same way. A new kind of obligation only needs a new class name, such as lease, which makes every lease taken finish exactly once:
/** @acquires lease return on-non-null */
struct lease* lease_take(struct lease_pool* pool);
/** @releases lease lease */
void lease_finish(struct lease* lease, int result);
A function that takes a lease and returns without finishing it is reported, and so is one that finishes it twice.
Two rules keep code checkable:
Obligations live in locals. A resource held in a struct field is not tracked, so code that holds one across a function keeps it in a local and passes the local.
Tag the function pointer. A call through a function pointer is tracked only when its field is tagged, or the typedef that its parameter or local is declared with.
Limitations
The checker works on one function at a time. It does not track:
uses of a released identifier other than passing the identifier, its address or a cast of it to a call
a value copied to a second variable or into a message: release it through the original name
resources held in struct fields
A field tag applies to every call through a field of that name, in any struct, because the checker has no types. A call through an untagged field that shares a tagged field’s name is checked as if it were tagged.
An identifier the function never acquired, such as a parameter or a local copied from a field, may be released once, unless the function first stored it through its own target.
A second release and a use after release are only reported when every path to the call has already released the identifier. for, while and do loops are followed for two passes for an identifier the function acquires or takes as a parameter, so a release in a loop of something held before it is reported; a loop built with goto is followed for one. A loop whose condition is a literal 0 or false is not repeated: a do body runs once, and a while or for body never. Any other local is followed for one pass, because a loop may fill it where the checker can’t see, such as k_msgq_get(&queue, &event, K_FOREVER).
A local declared in more than one arm of an #if is a different identifier in each arm; declare it once before the #if and assign it in each arm.
A failed acquire is recognised only where an if tests its result. A retry tested by a loop condition, such as while ((buf = net_buf_alloc(pool, K_NO_WAIT)) == NULL) or a do/while loop, is reported as acquired while held; test the result with if instead.
See Memory Leak Detection for the memory rules.
API Reference
-
PASS_OWNERSHIP(data)
Marks a point where the function’s obligation for a resource ends without a tagged release.
Tells the resource checker that the value is not leaked on this path: another owner, such as the caller or the struct it was stored in, is now responsible for releasing it, or the function implements the resource’s class and has released it itself. It releases the identifier whatever resource class it was acquired as.
- Releases:
data
-
MSGQ_PUT_OWNED(msgq, data, held)
Put a message that carries a resource on a queue that takes ownership of it, without waiting.
For a queue whose receiver releases what it takes. A queue of borrowed pointers uses
k_msgq_put. The message must carryheld, the identifier the resource checker sees released. When the put fails, the caller still ownsheld.- Releases:
held on-success
- Parameters:
msgq – The queue
data – The message, which the queue copies
held – The resource the message carries
- Returns:
0 when the message is queued, otherwise the error of
k_msgq_put