[PATCH smatch 2/4] smatch: record allocation callsites for assigned struct members
Harshit Mogalapalli <[email protected]> Wed, 22 Apr 2026 07:32:17 -0700
| Newsgroups | org.kernel.vger.smatch |
|---|---|
| Message-ID | <[email protected]> |
When allocation results are stored into struct members, we track assignment flow but do not persist which allocator function was used for each member in function_type_info. Add smatch_allocations_locations and wire it into the checker list and build. The checker hooks allocation handling, filters to real assignment expressions, extracts the destination struct type/member, and stores: - type: ALLOC - struct: destination struct type - member: destination member - value: allocator function name This extends DB coverage for allocation provenance and makes ALLOC queries for member assignments directly available to tooling (for example via smdb type_info). Assisted-by: Codex:gpt-5.3-codex Co-developed-by: Dan Carpenter <[email protected]> Signed-off-by: Harshit Mogalapalli <[email protected]> --- Makefile | 1 + check_list.h | 1 + smatch_allocations_locations.c | 57 ++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 smatch_allocations_locations.c diff --git a/Makefile b/Makefile index f5e49295e7ab..1b8bd34ffd92 100644 --- a/Makefile +++ b/Makefile @@ -258,6 +258,7 @@ SMATCH_OBJS += avl.o SMATCH_OBJS += smatch_about_fn_ptr_arg.o SMATCH_OBJS += smatch_address.o SMATCH_OBJS += smatch_allocations.o +SMATCH_OBJS += smatch_allocations_locations.o SMATCH_OBJS += smatch_annotate.o SMATCH_OBJS += smatch_array_values.o SMATCH_OBJS += smatch_assigned_expr.o diff --git a/check_list.h b/check_list.h index 77d596a7c350..330d43a12fb4 100644 --- a/check_list.h +++ b/check_list.h @@ -86,6 +86,7 @@ CK(register_unconstant_macros) CK(register_state_assigned) CK(register_points_to_container) CK(register_allocations) +CK(register_allocations_locations) CK(register_units) CK(register_goto_tracker) CK(register_refcount) diff --git a/smatch_allocations_locations.c b/smatch_allocations_locations.c new file mode 100644 index 000000000000..82daac18fc73 --- /dev/null +++ b/smatch_allocations_locations.c @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2026 Oracle. + * + * This program 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; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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/copyleft/gpl.txt + */ + +#include "smatch.h" + +static void match_allocation(struct expression *expr, + const char *name, struct symbol *sym, + struct allocation_info *info) +{ + struct expression *left; + struct symbol *type; + char *type_str; + char *member; + + if (__in_fake_assign) + return; + + if (!expr || expr->type != EXPR_ASSIGNMENT || expr->op != '=') + return; + + left = strip_expr(expr->left); + if (!left || left->type != EXPR_DEREF || !left->member) + return; + + type = get_type(left->deref); + if (!type || !type->ident) + return; + + type_str = type_to_str(type); + if (!type_str) + return; + + member = get_member_name_no_prefix(left); + if (!member) + return; + + sql_insert_function_type_info(ALLOC, type_str, member, info->fn_name); +} + +void register_allocations_locations(int id) +{ + add_allocation_hook(&match_allocation); +} -- 2.47.3