Skip to content

Commit

Permalink
Fixes crash
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantComm committed Oct 8, 2024
1 parent 0f45e05 commit 98c1a03
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dive_core/shader_disassembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::string DisassembleA3XX(const uint8_t* data,
{
disasm_a3xx_set_debug(debug);
#ifdef _MSC_VER
FILE* disasm_file;
FILE* disasm_file = NULL;
errno_t err = tmpfile_s(&disasm_file);
DIVE_ASSERT(err == 0);
DIVE_ASSERT(disasm_file != NULL);
Expand Down
60 changes: 12 additions & 48 deletions third_party/mesa/src/compiler/isaspec/isaspec_decode_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,13 @@ isa_print(struct isa_print_state *state, const char *fmt, ...)
char *buffer;
va_list args;
int ret;

va_start(args, fmt);
#ifdef _MSC_VER
// MSVC doesn't have vasprintf, so we need a workaround
size_t buffer_size = 1024; // Initial buffer size
size_t buffer_size = 1024;
buffer = (char *)malloc(buffer_size);
if (buffer == NULL) {
va_end(args);
// Handle allocation error
fprintf(stderr, "Error: Memory allocation failed in isa_print\n"); // Error handling
return;
}

Expand All @@ -201,7 +199,6 @@ isa_print(struct isa_print_state *state, const char *fmt, ...)
if (new_buffer == NULL) {
va_end(args);
free(buffer); // Free the old buffer
// Handle allocation error
return;
}
buffer = new_buffer;
Expand All @@ -210,14 +207,11 @@ isa_print(struct isa_print_state *state, const char *fmt, ...)
ret = vasprintf(&buffer, fmt, args);
#endif
va_end(args);

if (ret != -1) {
const size_t len = strlen(buffer);

for (size_t i = 0; i < len; i++) {
const char c = buffer[i];

assert(state->out != NULL);
fputc(c, state->out);
state->line_column++;

Expand Down Expand Up @@ -266,7 +260,7 @@ decode_error(struct decode_state *state, const char *fmt, ...)
return;
}

_vsprintf_p(state->errors[state->num_errors++], len + 1, fmt, ap);
_vsprintf_p(state->errors[state->num_errors++], len , fmt, ap);
#else
vasprintf(&state->errors[state->num_errors++], fmt, ap);
#endif
Expand Down Expand Up @@ -310,11 +304,7 @@ pop_expr(struct decode_state *state)
static struct decode_scope *
push_scope(struct decode_state *state, const struct isa_bitset *bitset, bitmask_t val)
{
#ifdef _MSC_VER
struct decode_scope *scope = calloc(1, sizeof(*scope));
#else
struct decode_scope *scope = rzalloc_size(state, sizeof(*scope));
#endif

BITSET_COPY(scope->val.bitset, val.bitset);
scope->bitset = bitset;
Expand All @@ -332,11 +322,7 @@ pop_scope(struct decode_scope *scope)
assert(scope->state->scope == scope); /* must be top of stack */

scope->state->scope = scope->parent;
#ifdef _MSC_VER
free(scope);
#else
ralloc_free(scope);
#endif
ralloc_free(scope);
}

/**
Expand All @@ -360,7 +346,6 @@ evaluate_expr(struct decode_scope *scope, isa_expr_t expr)
uint64_t ret = expr(scope);

pop_expr(scope->state);

uint64_t *retp = ralloc_size(scope->cache, sizeof(*retp));
*retp = ret;
_mesa_hash_table_insert(scope->cache, expr, retp);
Expand Down Expand Up @@ -645,15 +630,15 @@ display_field(struct decode_scope *scope, const char *field_name)

/* Special case 'NAME' maps to instruction/bitset name: */
if (!strncmp("NAME", field_name, field_name_len)) {
if (options) {
if (options->field_cb) {
options->field_cb(options->cbdata, field_name, &(struct isa_decode_value){
.str = scope->bitset->name,
});
}

}
while (scope->state->print.line_column < num_align)
isa_print(print, " ");

isa_print(print, "%s", scope->bitset->name);

return;
Expand All @@ -667,12 +652,13 @@ display_field(struct decode_scope *scope, const char *field_name)
}

uint64_t val = bitmask_to_uint64_t(v);

if (options) {
if (options->field_cb) {
options->field_cb(options->cbdata, field_name, &(struct isa_decode_value){
.num = val,
});
}
}

unsigned width = 1 + field->high - field->low;

Expand Down Expand Up @@ -790,17 +776,14 @@ display(struct decode_scope *scope)
char *field_name = (char *)malloc(e - p + 1);
strncpy(field_name, p, e - p);
field_name[e - p] = '\0';
display_field(scope, field_name);
free(field_name);
#else
char *field_name = strndup(p, e - p);
display_field(scope, field_name);
free(field_name);
#endif
display_field(scope, field_name);
free(field_name);

p = e;
} else {
assert(scope->state->print.out != NULL);
fputc(*p, scope->state->print.out);
scope->state->print.line_column++;
}
Expand Down Expand Up @@ -886,7 +869,6 @@ disasm(struct decode_state *state, void *bin, int sz)
}

struct decode_scope *scope = push_scope(state, b, instr);
assert(scope->state->print.out != NULL);
display(scope);
if (flush_errors(state)) {
errors++;
Expand Down Expand Up @@ -1045,33 +1027,22 @@ isa_disasm(void *bin, int sz, FILE *out, const struct isa_decode_options *option

if (!options)
options = &default_options;

#ifdef _MSC_VER
state = calloc(1, sizeof(*state));
#else
state = rzalloc_size(NULL, sizeof(*state));
#endif
state->options = options;
state->num_instr = sz / (BITMASK_WORDS * sizeof(BITSET_WORD));

if (state->options->branch_labels) {
#ifdef _MSC_VER
state->branch_targets = calloc(BITSET_WORDS(state->num_instr), sizeof(BITSET_WORD));
state->call_targets = calloc(BITSET_WORDS(state->num_instr), sizeof(BITSET_WORD));
#else
state->branch_targets = rzalloc_size(state,
sizeof(BITSET_WORD) * BITSET_WORDS(state->num_instr));
state->call_targets = rzalloc_size(state,
sizeof(BITSET_WORD) * BITSET_WORDS(state->num_instr));
#endif

/* Do a pre-pass to find all the branch targets: */
#ifdef _MSC_VER
state->print.out = fopen("NUL", "w");
#else
state->print.out = fopen("/dev/null", "w");
#endif
assert(state->print.out != NULL);
state->options = &default_options; /* skip hooks for prepass */
disasm(state, bin, sz);
fclose(state->print.out);
Expand Down Expand Up @@ -1101,11 +1072,8 @@ isa_disasm(void *bin, int sz, FILE *out, const struct isa_decode_options *option
state->print.out = out;

disasm(state, bin, sz);
#ifdef _MSC_VER
free(state);
#else

ralloc_free(state);
#endif
}

bool
Expand All @@ -1120,10 +1088,6 @@ isa_decode(void *out, void *bin, const struct isa_decode_options *options)
return false;
}

#ifdef _MSC_VER
free(state);
#else
ralloc_free(state);
#endif
ralloc_free(state);
return result;
}
1 change: 0 additions & 1 deletion third_party/mesa/src/freedreno/ir3/disasm-a3xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ disasm_a3xx_stat(uint32_t *dwords, int sizedwords, int level, FILE *out,
memset(stats, 0, sizeof(*stats));

decode_options.cbdata = &ctx;

isa_disasm(dwords, sizedwords * 4, out, &decode_options);

disasm_handle_last(&ctx);
Expand Down

0 comments on commit 98c1a03

Please sign in to comment.