Skip to content

Commit

Permalink
aux/draw: fix memory leak related to ureg_get_tokens()
Browse files Browse the repository at this point in the history
Indeed, the function nir_to_tgsi() returns an ureg_get_tokens() allocated
object which is assigned locally. The ureg_get_tokens() allocated object
should be freed.

For instance, this issue is triggered with a llvm enabled lima,
"piglit/bin/gl-1.0-rendermode-feedback -auto -fbo":
Direct leak of 512 byte(s) in 1 object(s) allocated from:
    #0 0x7faeaa4500 in __interceptor_realloc (/usr/lib64/libasan.so.6+0xa4500)
    android-rpi#1 0x7fa4a88f1c in tokens_expand ../src/gallium/auxiliary/tgsi/tgsi_ureg.c:239
    android-rpi#2 0x7fa4a88f1c in get_tokens ../src/gallium/auxiliary/tgsi/tgsi_ureg.c:262
    android-rpi#3 0x7fa4a900f4 in copy_instructions ../src/gallium/auxiliary/tgsi/tgsi_ureg.c:2079
    #4 0x7fa4a900f4 in ureg_finalize ../src/gallium/auxiliary/tgsi/tgsi_ureg.c:2129
    #5 0x7fa4a91dfc in ureg_get_tokens ../src/gallium/auxiliary/tgsi/tgsi_ureg.c:2206
    #6 0x7fa4b20a2c in nir_to_tgsi_options ../src/gallium/auxiliary/nir/nir_to_tgsi.c:4011
    #7 0x7fa4a0c914 in draw_create_vertex_shader ../src/gallium/auxiliary/draw/draw_vs.c:77

Fixes: b5e782f ("aux/draw: use nir_to_tgsi for draw shader in llvm path")
Signed-off-by: Patrick Lerda <[email protected]>
Reviewed-by: Emma Anholt <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21924>
(cherry picked from commit 6a8e671)
  • Loading branch information
Patrick Lerda authored and dcbaker-intel committed May 17, 2023
1 parent ba474af commit 6dd2a7a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pick_status.json
Original file line number Diff line number Diff line change
Expand Up @@ -10294,7 +10294,7 @@
"description": "aux/draw: fix memory leak related to ureg_get_tokens()",
"nominated": true,
"nomination_type": 1,
"resolution": 0,
"resolution": 1,
"main_sha": null,
"because_sha": "b5e782f5f43185076acbbea1e42000fd1fb48aa0",
"notes": null
Expand Down
9 changes: 9 additions & 0 deletions src/gallium/auxiliary/draw/draw_vs.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#include "tgsi/tgsi_dump.h"
#include "tgsi/tgsi_exec.h"
#include "tgsi/tgsi_ureg.h"

#include "nir/nir_to_tgsi.h"

Expand All @@ -65,6 +66,7 @@ draw_create_vertex_shader(struct draw_context *draw,
}

#ifdef DRAW_LLVM_AVAILABLE
bool is_allocated = false;
if (draw->pt.middle.llvm) {
struct pipe_screen *screen = draw->pipe->screen;
if (shader->type == PIPE_SHADER_IR_NIR &&
Expand All @@ -75,6 +77,7 @@ draw_create_vertex_shader(struct draw_context *draw,
PIPE_SHADER_IR_TGSI))) {
state.type = PIPE_SHADER_IR_TGSI;
state.tokens = nir_to_tgsi(shader->ir.nir, screen);
is_allocated = true;
}
vs = draw_create_vs_llvm(draw, &state);
}
Expand All @@ -84,6 +87,12 @@ draw_create_vertex_shader(struct draw_context *draw,
vs = draw_create_vs_exec(draw, &state);
}

#ifdef DRAW_LLVM_AVAILABLE
if (is_allocated) {
ureg_free_tokens(state.tokens);
}
#endif

if (vs) {
bool found_clipvertex = FALSE;
vs->position_output = -1;
Expand Down

0 comments on commit 6dd2a7a

Please sign in to comment.