diff options
-rw-r--r-- | src/target/riscv/batch.c | 33 | ||||
-rw-r--r-- | src/target/riscv/riscv-011.c | 25 | ||||
-rw-r--r-- | src/target/riscv/riscv-013.c | 28 | ||||
-rw-r--r-- | src/target/riscv/riscv.c | 19 |
4 files changed, 96 insertions, 9 deletions
diff --git a/src/target/riscv/batch.c b/src/target/riscv/batch.c index b2c7e83..96144b6 100644 --- a/src/target/riscv/batch.c +++ b/src/target/riscv/batch.c @@ -21,17 +21,43 @@ struct riscv_batch *riscv_batch_alloc(struct target *target, size_t scans, size_ { scans += 4; struct riscv_batch *out = calloc(1, sizeof(*out)); + if (!out) + goto error0; out->target = target; out->allocated_scans = scans; out->idle_count = idle; out->data_out = malloc(sizeof(*out->data_out) * (scans) * DMI_SCAN_BUF_SIZE); - out->data_in = malloc(sizeof(*out->data_in) * (scans) * DMI_SCAN_BUF_SIZE); + if (!out->data_out) + goto error1; + out->data_in = malloc(sizeof(*out->data_in) * (scans) * DMI_SCAN_BUF_SIZE); + if (!out->data_in) + goto error2; out->fields = malloc(sizeof(*out->fields) * (scans)); - if (bscan_tunnel_ir_width != 0) + if (!out->fields) + goto error3; + if (bscan_tunnel_ir_width != 0) { out->bscan_ctxt = malloc(sizeof(*out->bscan_ctxt) * (scans)); + if (!out->bscan_ctxt) + goto error4; + } out->last_scan = RISCV_SCAN_TYPE_INVALID; out->read_keys = malloc(sizeof(*out->read_keys) * (scans)); + if (!out->read_keys) + goto error5; return out; + +error5: + free(out->bscan_ctxt); +error4: + free(out->fields); +error3: + free(out->data_in); +error2: + free(out->data_out); +error1: + free(out); +error0: + return NULL; } void riscv_batch_free(struct riscv_batch *batch) @@ -39,8 +65,7 @@ void riscv_batch_free(struct riscv_batch *batch) free(batch->data_in); free(batch->data_out); free(batch->fields); - if (batch->bscan_ctxt) - free(batch->bscan_ctxt); + free(batch->bscan_ctxt); free(batch->read_keys); free(batch); } diff --git a/src/target/riscv/riscv-011.c b/src/target/riscv/riscv-011.c index c49e925..9b5f749 100644 --- a/src/target/riscv/riscv-011.c +++ b/src/target/riscv/riscv-011.c @@ -520,6 +520,8 @@ typedef struct { static scans_t *scans_new(struct target *target, unsigned int scan_count) { scans_t *scans = malloc(sizeof(scans_t)); + if (!scans) + goto error0; scans->scan_count = scan_count; /* This code also gets called before xlen is detected. */ if (riscv_xlen(target)) @@ -528,10 +530,25 @@ static scans_t *scans_new(struct target *target, unsigned int scan_count) scans->scan_size = 2 + 128 / 8; scans->next_scan = 0; scans->in = calloc(scans->scan_size, scans->scan_count); + if (!scans->in) + goto error1; scans->out = calloc(scans->scan_size, scans->scan_count); + if (!scans->out) + goto error2; scans->field = calloc(scans->scan_count, sizeof(struct scan_field)); + if (!scans->field) + goto error3; scans->target = target; return scans; + +error3: + free(scans->out); +error2: + free(scans->in); +error1: + free(scans); +error0: + return NULL; } static scans_t *scans_delete(scans_t *scans) @@ -845,6 +862,8 @@ static int cache_write(struct target *target, unsigned int address, bool run) LOG_DEBUG("enter"); riscv011_info_t *info = get_info(target); scans_t *scans = scans_new(target, info->dramsize + 2); + if (!scans) + return ERROR_FAIL; unsigned int last = info->dramsize; for (unsigned int i = 0; i < info->dramsize; i++) { @@ -1584,6 +1603,8 @@ static riscv_error_t handle_halt_routine(struct target *target) riscv011_info_t *info = get_info(target); scans_t *scans = scans_new(target, 256); + if (!scans) + return RE_FAIL; /* Read all GPRs as fast as we can, because gdb is going to ask for them * anyway. Reading them one at a time is much slower. */ @@ -1996,6 +2017,8 @@ static int read_memory(struct target *target, target_addr_t address, riscv011_info_t *info = get_info(target); const unsigned max_batch_size = 256; scans_t *scans = scans_new(target, max_batch_size); + if (!scans) + return ERROR_FAIL; uint32_t result_value = 0x777; uint32_t i = 0; @@ -2152,6 +2175,8 @@ static int write_memory(struct target *target, target_addr_t address, const unsigned max_batch_size = 256; scans_t *scans = scans_new(target, max_batch_size); + if (!scans) + return ERROR_FAIL; uint32_t result_value = 0x777; uint32_t i = 0; diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 6a6507f..493880e 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -265,6 +265,8 @@ dm013_info_t *get_dm(struct target *target) if (!dm) { LOG_DEBUG("[%d] Allocating new DM", target->coreid); dm = calloc(1, sizeof(dm013_info_t)); + if (!dm) + return NULL; dm->abs_chain_position = abs_chain_position; dm->current_hartid = -1; dm->hart_count = -1; @@ -279,6 +281,10 @@ dm013_info_t *get_dm(struct target *target) return dm; } target_entry = calloc(1, sizeof(*target_entry)); + if (!target_entry) { + info->dm = NULL; + return NULL; + } target_entry->target = target; list_add(&target_entry->list, &dm->target_list); @@ -1592,6 +1598,8 @@ static int examine(struct target *target) /* Reset the Debug Module. */ dm013_info_t *dm = get_dm(target); + if (!dm) + return ERROR_FAIL; if (!dm->was_reset) { dmi_write(target, DM_DMCONTROL, 0); dmi_write(target, DM_DMCONTROL, DM_DMCONTROL_DMACTIVE); @@ -1814,6 +1822,8 @@ int riscv013_authdata_write(struct target *target, uint32_t value) LOG_INFO("authdata_write resulted in successful authentication"); int result = ERROR_OK; dm013_info_t *dm = get_dm(target); + if (!dm) + return ERROR_FAIL; target_list_t *entry; list_for_each_entry(entry, &dm->target_list, list) { if (examine(entry->target) != ERROR_OK) @@ -1828,6 +1838,7 @@ int riscv013_authdata_write(struct target *target, uint32_t value) static int riscv013_hart_count(struct target *target) { dm013_info_t *dm = get_dm(target); + assert(dm); return dm->hart_count; } @@ -2103,6 +2114,8 @@ static int assert_reset(struct target *target) target->state = TARGET_RESET; dm013_info_t *dm = get_dm(target); + if (!dm) + return ERROR_FAIL; /* The DM might have gotten reset if OpenOCD called us in some reset that * involves SRST being toggled. So clear our cache which may be out of @@ -2846,6 +2859,8 @@ static int read_memory_progbuf_inner(struct target *target, target_addr_t addres struct riscv_batch *batch = riscv_batch_alloc(target, 32, info->dmi_busy_delay + info->ac_busy_delay); + if (!batch) + return ERROR_FAIL; unsigned reads = 0; for (unsigned j = index; j < count; j++) { @@ -3357,6 +3372,8 @@ static int write_memory_bus_v1(struct target *target, target_addr_t address, target, 32, info->dmi_busy_delay + info->bus_master_write_delay); + if (!batch) + return ERROR_FAIL; for (uint32_t i = (next_address - address) / size; i < count; i++) { const uint8_t *p = buffer + i * size; @@ -3528,6 +3545,8 @@ static int write_memory_progbuf(struct target *target, target_addr_t address, target, 32, info->dmi_busy_delay + info->ac_busy_delay); + if (!batch) + goto error; /* To write another word, we put it in S1 and execute the program. */ unsigned start = (cur_addr - address) / size; @@ -3755,6 +3774,8 @@ static int riscv013_select_current_hart(struct target *target) RISCV_INFO(r); dm013_info_t *dm = get_dm(target); + if (!dm) + return ERROR_FAIL; if (r->current_hartid == dm->current_hartid) return ERROR_OK; @@ -3773,6 +3794,8 @@ static int riscv013_select_current_hart(struct target *target) static int select_prepped_harts(struct target *target, bool *use_hasel) { dm013_info_t *dm = get_dm(target); + if (!dm) + return ERROR_FAIL; if (!dm->hasel_supported) { RISCV_INFO(r); r->prepped = false; @@ -3865,6 +3888,8 @@ static int riscv013_halt_go(struct target *target) if (use_hasel) { target_list_t *entry; dm013_info_t *dm = get_dm(target); + if (!dm) + return ERROR_FAIL; list_for_each_entry(entry, &dm->target_list, list) { struct target *t = entry->target; t->state = TARGET_HALTED; @@ -3970,6 +3995,8 @@ static enum riscv_halt_reason riscv013_halt_reason(struct target *target) int riscv013_write_debug_buffer(struct target *target, unsigned index, riscv_insn_t data) { dm013_info_t *dm = get_dm(target); + if (!dm) + return ERROR_FAIL; if (dm->progbuf_cache[index] != data) { if (dmi_write(target, DM_PROGBUF0 + index, data) != ERROR_OK) return ERROR_FAIL; @@ -4094,6 +4121,7 @@ static int riscv013_test_sba_config_reg(struct target *target, } uint32_t num_sbdata_regs = get_num_sbdata_regs(target); + assert(num_sbdata_regs); uint32_t rd_buf[num_sbdata_regs]; diff --git a/src/target/riscv/riscv.c b/src/target/riscv/riscv.c index f847016..410454d 100644 --- a/src/target/riscv/riscv.c +++ b/src/target/riscv/riscv.c @@ -2371,9 +2371,10 @@ int parse_ranges(range_t **ranges, const char **argv) } if (pass == 0) { - if (*ranges) - free(*ranges); + free(*ranges); *ranges = calloc(range + 2, sizeof(range_t)); + if (!*ranges) + return ERROR_FAIL; } else { (*ranges)[range].low = 1; (*ranges)[range].high = 0; @@ -3713,6 +3714,8 @@ int riscv_init_registers(struct target *target) riscv_free_registers(target); target->reg_cache = calloc(1, sizeof(*target->reg_cache)); + if (!target->reg_cache) + return ERROR_FAIL; target->reg_cache->name = "RISC-V Registers"; target->reg_cache->num_regs = GDB_REGNO_COUNT; @@ -3730,12 +3733,15 @@ int riscv_init_registers(struct target *target) target->reg_cache->reg_list = calloc(target->reg_cache->num_regs, sizeof(struct reg)); + if (!target->reg_cache->reg_list) + return ERROR_FAIL; const unsigned int max_reg_name_len = 12; - if (info->reg_names) - free(info->reg_names); + free(info->reg_names); info->reg_names = calloc(target->reg_cache->num_regs, max_reg_name_len); + if (!info->reg_names) + return ERROR_FAIL; char *reg_name = info->reg_names; int hartid = riscv_current_hartid(target); @@ -3883,6 +3889,8 @@ int riscv_init_registers(struct target *target) int custom_within_range = 0; riscv_reg_info_t *shared_reg_info = calloc(1, sizeof(riscv_reg_info_t)); + if (!shared_reg_info) + return ERROR_FAIL; shared_reg_info->target = target; /* When gdb requests register N, gdb_get_register_packet() assumes that this @@ -4292,7 +4300,8 @@ int riscv_init_registers(struct target *target) r->group = "custom"; r->feature = &feature_custom; r->arch_info = calloc(1, sizeof(riscv_reg_info_t)); - assert(r->arch_info); + if (!r->arch_info) + return ERROR_FAIL; ((riscv_reg_info_t *) r->arch_info)->target = target; ((riscv_reg_info_t *) r->arch_info)->custom_number = custom_number; sprintf(reg_name, "custom%d", custom_number); |