From bc444f6d26c6c5b2477654b59a503c81f20f879a Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Wed, 21 May 2025 23:09:44 +0200 Subject: [PATCH] nir: fix use-after-free on function parameter names Fixes: 3da8444be56 ("nir: add names to function parameters") Reviewed-by: Alyssa Rosenzweig Acked-by: Faith Ekstrand Part-of: --- src/compiler/nir/nir_serialize.c | 6 ++++-- src/compiler/nir/nir_sweep.c | 3 +++ src/compiler/spirv/vtn_opencl.c | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/compiler/nir/nir_serialize.c b/src/compiler/nir/nir_serialize.c index 49a07f4f0ef..4b5e77fcce1 100644 --- a/src/compiler/nir/nir_serialize.c +++ b/src/compiler/nir/nir_serialize.c @@ -2060,8 +2060,10 @@ read_function(read_ctx *ctx) for (unsigned i = 0; i < fxn->num_params; i++) { uint32_t val = blob_read_uint32(ctx->blob); bool has_name = (val & 0x10000); - if (has_name) - fxn->params[i].name = blob_read_string(ctx->blob); + if (has_name) { + char *name = blob_read_string(ctx->blob); + fxn->params[i].name = ralloc_strdup(ctx->nir, name); + } fxn->params[i].num_components = val & 0xff; fxn->params[i].bit_size = (val >> 8) & 0xff; diff --git a/src/compiler/nir/nir_sweep.c b/src/compiler/nir/nir_sweep.c index 29caf897193..2ed2e1fa32b 100644 --- a/src/compiler/nir/nir_sweep.c +++ b/src/compiler/nir/nir_sweep.c @@ -140,6 +140,9 @@ sweep_function(nir_shader *nir, nir_function *f) ralloc_steal(nir, f); ralloc_steal(nir, f->params); + for (unsigned i = 0; i < f->num_params; i++) + ralloc_steal(nir, (char *)f->params[i].name); + if (f->impl) sweep_impl(nir, f->impl); } diff --git a/src/compiler/spirv/vtn_opencl.c b/src/compiler/spirv/vtn_opencl.c index 72da06f8356..770064afd6b 100644 --- a/src/compiler/spirv/vtn_opencl.c +++ b/src/compiler/spirv/vtn_opencl.c @@ -154,6 +154,7 @@ static nir_function *mangle_and_find(struct vtn_builder *b, decl->params = ralloc_array(b->shader, nir_parameter, decl->num_params); for (unsigned i = 0; i < decl->num_params; i++) { decl->params[i] = found->params[i]; + decl->params[i].name = ralloc_strdup(b->shader, found->params[i].name); } found = decl; }