Skip to content

Commit

Permalink
Fixed a crash with ' exec stack not zero' message.
Browse files Browse the repository at this point in the history
It was caused by a memory read from invalid (unmapped) address,
for example when gdb endianess does not match the target endianess.
  • Loading branch information
[email protected] committed Jul 12, 2013
1 parent 79a40ec commit de77fa4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
12 changes: 5 additions & 7 deletions adapter-mpsse.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ static void pracc_exec_write (mpsse_adapter_t *a, unsigned address)
} else if (address == PRACC_STACK)
{
/* save data onto our stack */
a->stack [a->stack_offset++] = data;
offset = a->stack_offset++;
a->stack [offset] = data;
} else
{
fprintf (stderr, "%s: error writing unexpected address %08x\n",
Expand All @@ -705,8 +706,9 @@ static void pracc_exec_write (mpsse_adapter_t *a, unsigned address)
* supplied via EJTAG block. Input and output data are mapped to
* special regions in debug memory segment. A separate stack region
* exists for temporary storage.
* Return zero, when failed (stack disbalanced).
*/
static void mpsse_exec (adapter_t *adapter, int stay_in_debug_mode,
static int mpsse_exec (adapter_t *adapter, int stay_in_debug_mode,
int code_len, const unsigned *code,
int num_param_in, unsigned *param_in,
int num_param_out, unsigned *param_out)
Expand Down Expand Up @@ -765,11 +767,7 @@ static void mpsse_exec (adapter_t *adapter, int stay_in_debug_mode,
}
done:
/* Stack sanity check */
if (a->stack_offset != 0) {
fprintf (stderr, "%s: exec stack not zero = %d\n",
a->adapter.name, a->stack_offset);
exit (-1);
}
return (a->stack_offset == 0);
}

/*
Expand Down
9 changes: 3 additions & 6 deletions adapter-pickit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,9 @@ static void pracc_exec_write (pickit_adapter_t *a, unsigned address)
* supplied via EJTAG block. Input and output data are mapped to
* special regions in debug memory segment. A separate stack region
* exists for temporary storage.
* Return zero, when failed (stack disbalanced).
*/
static void pickit_exec (adapter_t *adapter, int stay_in_debug_mode,
static int pickit_exec (adapter_t *adapter, int stay_in_debug_mode,
int code_len, const unsigned *code,
int num_param_in, unsigned *param_in,
int num_param_out, unsigned *param_out)
Expand Down Expand Up @@ -538,11 +539,7 @@ static void pickit_exec (adapter_t *adapter, int stay_in_debug_mode,
}
done:
/* Stack sanity check */
if (a->stack_offset != 0) {
fprintf (stderr, "%s: exec stack not zero = %d\n",
a->adapter.name, a->stack_offset);
exit (-1);
}
return (a->stack_offset == 0);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct _adapter_t {
int (*cpu_stopped) (adapter_t *a);
void (*reset_cpu) (adapter_t *a);
void (*stop_cpu) (adapter_t *a);
void (*exec) (adapter_t *a, int cycle,
int (*exec) (adapter_t *a, int cycle,
int num_code_words, const unsigned *code,
int num_param_in, unsigned *param_in,
int num_param_out, unsigned *param_out);
Expand Down
32 changes: 24 additions & 8 deletions target-ejtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,13 @@ unsigned target_read_word (target_t *t, unsigned addr)
};
unsigned word;

t->adapter->exec (t->adapter, 1,
ARRAY_SIZE(code), code, 1, &addr, 1, &word);
if (! t->adapter->exec (t->adapter, 1,
ARRAY_SIZE(code), code, 1, &addr, 1, &word))
{
/* Exception: bad address. */
fprintf (stderr, "ERROR: cannot read address %08x\n", addr);
return 0;
}
return word;
}

Expand Down Expand Up @@ -898,8 +903,13 @@ void target_read_block (target_t *t, unsigned addr,
param_in[0] = addr;
param_in[1] = n;

t->adapter->exec (t->adapter, 1, ARRAY_SIZE(code), code,
ARRAY_SIZE(param_in), param_in, n, &data[nread]);
if (! t->adapter->exec (t->adapter, 1, ARRAY_SIZE(code), code,
ARRAY_SIZE(param_in), param_in, n, &data[nread]))
{
/* Exception: bad address. */
fprintf (stderr, "ERROR: cannot read address %08x\n", addr);
memset (&data[nread], 0, 4*n);
}

nwords -= n;
addr += n;
Expand Down Expand Up @@ -935,8 +945,11 @@ void target_write_word (target_t *t, unsigned addr, unsigned word)

param_in[0] = addr;
param_in[1] = word;
t->adapter->exec (t->adapter, 1, ARRAY_SIZE(code), code,
ARRAY_SIZE(param_in), param_in, 0, 0);
if (! t->adapter->exec (t->adapter, 1, ARRAY_SIZE(code), code,
ARRAY_SIZE(param_in), param_in, 0, 0))
{
fprintf (stderr, "ERROR: cannot write %08x to address %08x\n", word, addr);
}
}

/*
Expand Down Expand Up @@ -985,8 +998,11 @@ void target_write_block (target_t *t, unsigned addr,
param_in[1] = addr + (nwords * sizeof(unsigned)); /* last address */
memcpy (&param_in[2], data, nwords * sizeof(unsigned));

t->adapter->exec (t->adapter, 1, ARRAY_SIZE(code), code,
nwords + 2, param_in, 0, 0);
if (! t->adapter->exec (t->adapter, 1, ARRAY_SIZE(code), code,
nwords + 2, param_in, 0, 0))
{
fprintf (stderr, "ERROR: cannot write %u words to address %08x\n", nwords, addr);
}
}

/*
Expand Down

0 comments on commit de77fa4

Please sign in to comment.