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 <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16961>
This commit is contained in:
Boris Brezillon 2022-02-14 09:13:41 -08:00 committed by Marge Bot
parent 69339066fc
commit bd4c8f562c
2 changed files with 8 additions and 4 deletions

View File

@ -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);

View File

@ -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,