From bd4c8f562c94ee17c1873b066c6a43ed19f28c97 Mon Sep 17 00:00:00 2001 From: Boris Brezillon Date: Mon, 14 Feb 2022 09:13:41 -0800 Subject: [PATCH] microsoft/compiler: Truncate function names when needed DXIL metadata strings and function names have a limited size. Truncate the name when they don't fit. This is a quick&dirty workaround since it doesn't address the problem for all kind of strings, and doesn't ensure there's no collision in the function names after the truncation. That's not an issue right now because I don't think we have implementations keeping more than one function (the entrypoint), but it might be a problem at some point. Acked-by: Jesse Natalie Part-of: --- src/microsoft/compiler/dxil_module.c | 7 ++++--- src/microsoft/compiler/nir_to_dxil.c | 5 ++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/microsoft/compiler/dxil_module.c b/src/microsoft/compiler/dxil_module.c index 6b3089442b4..8909df99d04 100644 --- a/src/microsoft/compiler/dxil_module.c +++ b/src/microsoft/compiler/dxil_module.c @@ -1852,7 +1852,8 @@ add_function(struct dxil_module *m, const char *name, if (!func) return NULL; - func->name = ralloc_strdup(func, name); + /* Truncate function name to make emit_symtab_entry() happy. */ + func->name = ralloc_strndup(func, name, 253); if (!func->name) { return NULL; } @@ -2218,7 +2219,7 @@ emit_symtab_entry(struct dxil_module *m, unsigned value, const char *name) temp[0] = VST_CODE_ENTRY; temp[1] = value; for (int i = 0; i < strlen(name); ++i) - temp[i + 2] = name[i]; + temp[i + 2] = (uint8_t)(name[i]); enum value_symtab_abbrev_id abbrev = VST_ABBREV_ENTRY_8; if (is_char6_string(name)) @@ -2492,7 +2493,7 @@ emit_metadata_string(struct dxil_module *m, const char *str) assert(strlen(str) < ARRAY_SIZE(data) - 1); data[0] = METADATA_STRING; for (size_t i = 0; i < strlen(str); ++i) - data[i + 1] = str[i]; + data[i + 1] = (uint8_t)(str[i]); return emit_metadata_abbrev_record(m, METADATA_ABBREV_STRING, data, strlen(str) + 1); diff --git a/src/microsoft/compiler/nir_to_dxil.c b/src/microsoft/compiler/nir_to_dxil.c index c8a17c4ef9e..5b4b6c91f41 100644 --- a/src/microsoft/compiler/nir_to_dxil.c +++ b/src/microsoft/compiler/nir_to_dxil.c @@ -1471,8 +1471,11 @@ emit_entrypoint(struct ntd_context *ctx, const struct dxil_mdnode *resources, const struct dxil_mdnode *shader_props) { + char truncated_name[254] = { 0 }; + strncpy(truncated_name, name, ARRAY_SIZE(truncated_name) - 1); + const struct dxil_mdnode *func_md = dxil_get_metadata_func(&ctx->mod, func); - const struct dxil_mdnode *name_md = dxil_get_metadata_string(&ctx->mod, name); + const struct dxil_mdnode *name_md = dxil_get_metadata_string(&ctx->mod, truncated_name); const struct dxil_mdnode *nodes[] = { func_md, name_md,