Linux SPARSE checker discussions
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: linux-sparse@vger.kernel.org
Cc: Bart Van Assche <bvanassche@acm.org>
Subject: [PATCH 17/20] llvm: Free leaked allocations and LLVM objects
Date: Mon, 19 Oct 2026 10:56:30 -0700	[thread overview]
Message-ID: <20261019175633.415493-18-bvanassche@acm.org> (raw)
In-Reply-To: <20261019175633.415493-1-bvanassche@acm.org>

Include <stdlib.h> in sparse-llvm.c, free arg_type after calling
LLVMFunctionType() in sym_func_type(), free args after emitting the call
instruction in output_op_call(), call LLVMDisposeBuilder() at the end of
output_fn(), and jump to a cleanup label when die_if_error is set in
main() so LLVMDisposeModule() executes before returning.

Prevent memory leaks of temporary argument arrays, per-function LLVM IR
builders, and the LLVM module during both normal compilation and error
exits under LeakSanitizer.

Fixes: 45a6fbff8250 ("sparse, llvm: Initial commit")
Fixes: 404705a0c86d ("sparse, llvm: implement OP_CALL")
Fixes: 064c4adfb5fd ("sparse, llvm: Function pointer code generation")
Fixes: 469e5d6ea335 ("sparse, llvm: die if error")
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 sparse-llvm.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/sparse-llvm.c b/sparse-llvm.c
index 1155c2341251..70e0ff6f58a4 100644
--- a/sparse-llvm.c
+++ b/sparse-llvm.c
@@ -10,6 +10,7 @@
 
 #include <stdbool.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <assert.h>
@@ -53,6 +54,7 @@ static LLVMTypeRef sym_func_type(struct symbol *sym)
 	int n_arg = symbol_list_size(sym->arguments);
 	LLVMTypeRef *arg_type = calloc(n_arg, sizeof(LLVMTypeRef));
 	LLVMTypeRef ret_type = func_return_type(sym);
+	LLVMTypeRef fn_type;
 	struct symbol *arg;
 	int idx = 0;
 
@@ -62,7 +64,9 @@ static LLVMTypeRef sym_func_type(struct symbol *sym)
 		arg_type[idx++] = symbol_type(arg_sym);
 	} END_FOR_EACH_PTR(arg);
 
-	return LLVMFunctionType(ret_type, arg_type, n_arg, sym->variadic);
+	fn_type = LLVMFunctionType(ret_type, arg_type, n_arg, sym->variadic);
+	free(arg_type);
+	return fn_type;
 }
 
 static LLVMTypeRef sym_array_type(struct symbol *sym)
@@ -858,6 +862,7 @@ static void output_op_call(struct function *fn, struct instruction *insn)
 	(void) fntype;
 	target = LLVMBuildCall(fn->builder, func, args, n_arg, name);
 #endif
+	free(args);
 
 	insn->target->priv = target;
 }
@@ -1223,6 +1228,8 @@ static void output_fn(LLVMModuleRef module, struct entrypoint *ep)
 			} END_FOR_EACH_PTR(phi);
 		} END_FOR_EACH_PTR(insn);
 	} END_FOR_EACH_PTR(bb);
+
+	LLVMDisposeBuilder(function.builder);
 }
 
 static LLVMValueRef output_data(LLVMModuleRef module, struct symbol *sym)
@@ -1372,6 +1379,7 @@ int main(int argc, char **argv)
 	struct string_list *filelist = NULL;
 	struct symbol_list *symlist;
 	LLVMModuleRef module;
+	int ret = 0;
 	char *file;
 
 	symlist = sparse_initialize(argc, argv, &filelist);
@@ -1383,8 +1391,10 @@ int main(int argc, char **argv)
 
 	FOR_EACH_PTR(filelist, file) {
 		symlist = sparse(file);
-		if (die_if_error)
-			return 1;
+		if (die_if_error) {
+			ret = 1;
+			goto out;
+		}
 		compile(module, symlist);
 	} END_FOR_EACH_PTR(file);
 
@@ -1392,8 +1402,9 @@ int main(int argc, char **argv)
 
 	LLVMWriteBitcodeToFD(module, STDOUT_FILENO, 0, 0);
 
+out:
 	LLVMDisposeModule(module);
 
 	report_stats();
-	return 0;
+	return ret;
 }

  parent reply	other threads:[~2026-10-19 17:56 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19 17:56 [PATCH 00/20] Sanitizer fixes, LLVM updates, and CI workflow Bart Van Assche
2026-10-19 17:56 ` [PATCH 01/20] doc: Update maintainer and mirror information Bart Van Assche
2026-10-19 17:56 ` [PATCH 02/20] ptrlist: Guard against null list in ptrlist operations Bart Van Assche
2026-10-19 17:56 ` [PATCH 03/20] bits: Guard against out-of-bounds bit shifts in bitmask helpers Bart Van Assche
2026-10-19 17:56 ` [PATCH 04/20] storage: Free hash table entries when freeing storage Bart Van Assche
2026-10-19 17:56 ` [PATCH 05/20] builtin: Bound argument array iteration in declare_builtin() Bart Van Assche
2026-10-19 17:56 ` [PATCH 06/20] c2xml: Use an unsigned shift in examine_modifiers() Bart Van Assche
2026-10-19 17:56 ` [PATCH 07/20] parse: Avoid incrementing rank on invalid long real specifiers Bart Van Assche
2026-10-19 17:56 ` [PATCH 08/20] pre-process: Avoid zero-length VLAs for parameterless macros Bart Van Assche
2026-10-19 17:56 ` [PATCH 09/20] pre-process: Avoid reading token->string on non-string tokens Bart Van Assche
2026-10-19 17:56 ` [PATCH 10/20] target: Avoid signed overflow in bit and byte conversion helpers Bart Van Assche
2026-10-19 17:56 ` [PATCH 11/20] evaluate: Use bitmask helpers in evaluate_ptr_add() Bart Van Assche
2026-10-19 17:56 ` [PATCH 12/20] linearize: Use bits_mask() in linearize_bitfield_insert() Bart Van Assche
2026-10-19 17:56 ` [PATCH 13/20] expand: Avoid undefined shifts and signed overflow in constant folding Bart Van Assche
2026-10-19 17:56 ` [PATCH 14/20] simplify: Avoid undefined shifts and signed overflow in simplification Bart Van Assche
2026-10-19 17:56 ` [PATCH 15/20] sparsec: Emit object files directly with llc -filetype=obj Bart Van Assche
2026-10-19 17:56 ` [PATCH 16/20] llvm: Stop calling LLVMConstGEP2() for string literals Bart Van Assche
2026-10-19 17:56 ` Bart Van Assche [this message]
2026-10-19 17:56 ` [PATCH 18/20] parse: Accept an empty attribute argument list Bart Van Assche
2026-10-19 17:56 ` [PATCH 19/20] llvm: Use explicit LLVMContextRef instead of global context Bart Van Assche
2026-10-19 17:56 ` [PATCH 20/20] ci: Add a GitHub Actions workflow and CI scripts Bart Van Assche

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261019175633.415493-18-bvanassche@acm.org \
    --to=bvanassche@acm.org \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox