From d6aefb583eac19a65eb7f03917d34dc9bac50933 Mon Sep 17 00:00:00 2001 From: Patrick Lerda Date: Sat, 24 Feb 2024 01:28:54 +0100 Subject: [PATCH] r300: fix memory leaks when register allocation fails For instance, this issue is triggered with "piglit/bin/ext_framebuffer_multisample-accuracy all_samples color depthstencil -auto -fbo": Direct leak of 1160 byte(s) in 1 object(s) allocated from: #0 0x7fbe8897d7ef in __interceptor_malloc (/usr/lib64/libasan.so.6+0xb17ef) #1 0x7fbe7e7abfcc in rc_constants_copy ../src/gallium/drivers/r300/compiler/radeon_code.c:47 #2 0x7fbe7e7ec902 in r3xx_compile_fragment_program ../src/gallium/drivers/r300/compiler/r3xx_fragprog.c:174 #3 0x7fbe7e7e1b22 in r300_translate_fragment_shader ../src/gallium/drivers/r300/r300_fs.c:516 #4 0x7fbe7e7e6373 in r300_pick_fragment_shader ../src/gallium/drivers/r300/r300_fs.c:591 #5 0x7fbe7e75456e in r300_create_fs_state ../src/gallium/drivers/r300/r300_state.c:1073 #6 0x7fbe7cd2ebe5 in st_create_fp_variant ../src/mesa/state_tracker/st_program.c:1070 #7 0x7fbe7cd374b5 in st_get_fp_variant ../src/mesa/state_tracker/st_program.c:1116 #8 0x7fbe7cd38273 in st_precompile_shader_variant ../src/mesa/state_tracker/st_program.c:1281 #9 0x7fbe7cd38273 in st_finalize_program ../src/mesa/state_tracker/st_program.c:1345 #10 0x7fbe7d798ca8 in st_link_glsl_to_nir ../src/mesa/state_tracker/st_glsl_to_nir.cpp:724 #11 0x7fbe7d798ca8 in st_link_shader ../src/mesa/state_tracker/st_glsl_to_nir.cpp:952 #12 0x7fbe7d6790d5 in link_program ../src/mesa/main/shaderapi.c:1336 #13 0x7fbe7d6790d5 in link_program_error ../src/mesa/main/shaderapi.c:1447 ... SUMMARY: AddressSanitizer: 2528456 byte(s) leaked in 1057 allocation(s). Fixes: 54f6e72b2715 ("r300: better register allocator for vertex shaders") Signed-off-by: Patrick Lerda Part-of: (cherry picked from commit 4d00edda00041de0492a5097353407f6d1781df3) --- .pick_status.json | 2 +- src/gallium/drivers/r300/compiler/r3xx_vertprog.c | 1 + src/gallium/drivers/r300/compiler/radeon_pair_regalloc.c | 1 + src/gallium/drivers/r300/r300_fs.c | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.pick_status.json b/.pick_status.json index dee7c490725..a27948a5eac 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -1684,7 +1684,7 @@ "description": "r300: fix memory leaks when register allocation fails", "nominated": true, "nomination_type": 1, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "54f6e72b27154219be237690453bb9c3b57cd3ec", "notes": null diff --git a/src/gallium/drivers/r300/compiler/r3xx_vertprog.c b/src/gallium/drivers/r300/compiler/r3xx_vertprog.c index a02147a8244..00b9928164a 100644 --- a/src/gallium/drivers/r300/compiler/r3xx_vertprog.c +++ b/src/gallium/drivers/r300/compiler/r3xx_vertprog.c @@ -689,6 +689,7 @@ static void allocate_temporary_registers(struct radeon_compiler *c, void *user) if (!ra_allocate(graph)) { rc_error(c, "Ran out of hardware temporaries\n"); + ralloc_free(graph); return; } diff --git a/src/gallium/drivers/r300/compiler/radeon_pair_regalloc.c b/src/gallium/drivers/r300/compiler/radeon_pair_regalloc.c index e7cabfa9d24..5f947678453 100644 --- a/src/gallium/drivers/r300/compiler/radeon_pair_regalloc.c +++ b/src/gallium/drivers/r300/compiler/radeon_pair_regalloc.c @@ -357,6 +357,7 @@ static void do_advanced_regalloc(struct regalloc_state * s) if (!ra_allocate(graph)) { rc_error(s->C, "Ran out of hardware temporaries\n"); + ralloc_free(graph); return; } diff --git a/src/gallium/drivers/r300/r300_fs.c b/src/gallium/drivers/r300/r300_fs.c index 5300c8bf047..6ce0eabdf92 100644 --- a/src/gallium/drivers/r300/r300_fs.c +++ b/src/gallium/drivers/r300/r300_fs.c @@ -525,6 +525,7 @@ static void r300_translate_fragment_shader( abort(); } + free(compiler.code->constants.Constants); rc_destroy(&compiler.Base); r300_dummy_fragment_shader(r300, shader); return;