From 7a3eec2b4d9dbb9533acfb271dbe91afa0727c8e Mon Sep 17 00:00:00 2001 From: Tomas Vanek Date: Tue, 18 Dec 2018 17:17:33 +0100 Subject: [PATCH] target algo: do not write reg_param if direction is PARAM_IN Without this change xxx_start_algorithm() writes all register parameters no matter of their direction. It usually results in writing of uninitialized reg_params[].value - possibly reported by valgrind. While on it fix the wrong parameter direction in kinetis_disable_wdog_algo(). This bug did not have any impact because of unconditional write of reg_params. Change-Id: Ia9c6a7b37f77d5eb6e5f5463012dddd50471742b Signed-off-by: Tomas Vanek Reviewed-on: http://openocd.zylin.com/4813 Tested-by: jenkins Reviewed-by: Antonio Borneo Reviewed-by: Matthias Welwarsky --- src/flash/nor/kinetis.c | 2 +- src/target/armv4_5.c | 3 +++ src/target/armv7m.c | 3 +++ src/target/dsp563xx.c | 3 +++ src/target/mips32.c | 3 +++ src/target/riscv/riscv.c | 3 +++ src/target/stm8.c | 3 +++ 7 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/flash/nor/kinetis.c b/src/flash/nor/kinetis.c index 86fad7215..eca74a8b8 100644 --- a/src/flash/nor/kinetis.c +++ b/src/flash/nor/kinetis.c @@ -1035,7 +1035,7 @@ static int kinetis_disable_wdog_algo(struct target *target, size_t code_size, co armv7m_info.common_magic = ARMV7M_COMMON_MAGIC; armv7m_info.core_mode = ARM_MODE_THREAD; - init_reg_param(®_params[0], "r0", 32, PARAM_IN); + init_reg_param(®_params[0], "r0", 32, PARAM_OUT); buf_set_u32(reg_params[0].value, 0, 32, wdog_base); retval = target_run_algorithm(target, 0, NULL, 1, reg_params, diff --git a/src/target/armv4_5.c b/src/target/armv4_5.c index b3bee8d88..6c30acc46 100644 --- a/src/target/armv4_5.c +++ b/src/target/armv4_5.c @@ -1362,6 +1362,9 @@ int armv4_5_run_algorithm_inner(struct target *target, } for (i = 0; i < num_reg_params; i++) { + if (reg_params[i].direction == PARAM_IN) + continue; + struct reg *reg = register_get_by_name(arm->core_cache, reg_params[i].reg_name, 0); if (!reg) { LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name); diff --git a/src/target/armv7m.c b/src/target/armv7m.c index a1962fefc..ecca0e752 100644 --- a/src/target/armv7m.c +++ b/src/target/armv7m.c @@ -388,6 +388,9 @@ int armv7m_start_algorithm(struct target *target, } for (int i = 0; i < num_reg_params; i++) { + if (reg_params[i].direction == PARAM_IN) + continue; + struct reg *reg = register_get_by_name(armv7m->arm.core_cache, reg_params[i].reg_name, 0); /* uint32_t regvalue; */ diff --git a/src/target/dsp563xx.c b/src/target/dsp563xx.c index 1d728dff8..e7306d2e4 100644 --- a/src/target/dsp563xx.c +++ b/src/target/dsp563xx.c @@ -1394,6 +1394,9 @@ static int dsp563xx_run_algorithm(struct target *target, } for (i = 0; i < num_reg_params; i++) { + if (reg_params[i].direction == PARAM_IN) + continue; + struct reg *reg = register_get_by_name(dsp563xx->core_cache, reg_params[i].reg_name, 0); diff --git a/src/target/mips32.c b/src/target/mips32.c index b5dbea312..abb425596 100644 --- a/src/target/mips32.c +++ b/src/target/mips32.c @@ -468,6 +468,9 @@ int mips32_run_algorithm(struct target *target, int num_mem_params, } for (int i = 0; i < num_reg_params; i++) { + if (reg_params[i].direction == PARAM_IN) + continue; + struct reg *reg = register_get_by_name(mips32->core_cache, reg_params[i].reg_name, 0); if (!reg) { diff --git a/src/target/riscv/riscv.c b/src/target/riscv/riscv.c index 8d76c4aa4..02ba38055 100644 --- a/src/target/riscv/riscv.c +++ b/src/target/riscv/riscv.c @@ -853,6 +853,9 @@ static int riscv_run_algorithm(struct target *target, int num_mem_params, uint64_t saved_regs[32]; for (int i = 0; i < num_reg_params; i++) { + if (mem_params[i].direction == PARAM_IN) + continue; + LOG_DEBUG("save %s", reg_params[i].reg_name); struct reg *r = register_get_by_name(target->reg_cache, reg_params[i].reg_name, 0); if (!r) { diff --git a/src/target/stm8.c b/src/target/stm8.c index 39fbb5059..f5df2481a 100644 --- a/src/target/stm8.c +++ b/src/target/stm8.c @@ -1897,6 +1897,9 @@ static int stm8_run_algorithm(struct target *target, int num_mem_params, } for (int i = 0; i < num_reg_params; i++) { + if (reg_params[i].direction == PARAM_IN) + continue; + struct reg *reg = register_get_by_name(stm8->core_cache, reg_params[i].reg_name, 0);