Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on exit with uninitialized target #1015

Open
wants to merge 1 commit into
base: riscv
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/target/riscv/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,12 @@ static void riscv_deinit_target(struct target *target)
LOG_TARGET_DEBUG(target, "riscv_deinit_target()");

struct riscv_info *info = target->arch_info;
struct target_type *tt = get_target_type(target);
if (!tt)
LOG_TARGET_ERROR(target, "Could not identify target type.");
struct target_type *tt = NULL;
if (info->dtm_version != DTM_DTMCS_VERSION_UNKNOWN) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not particularly safe. Please, consider a target that was not properly initialized. target->arch_info can be still zero or point to uninitialized memory.
The condition should at least be modified to:

if (info && info->dtm_version != DTM_DTMCS_VERSION_UNKNOWN) {

However, under such condition get_target_type() will never print an error, and IMHO, if a target was never initialized but riscv_deinit_target() is called -- an error should be printed.

In general, I would like to leave the messaging here.
In case examine() failed, the state of the target is not the same as if the target was just initialized. Moreover, it is not documented either. This is an issue. I am preparing some patches aimed at addressing this. BTW, your patch with riscv013_dm_free() is a great help in this regard, thanks!

If you still find the message too annoying -- please, consider changing it to LOG_TARGET_DEBUG().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition should at least be modified to:

if (info && info->dtm_version != DTM_DTMCS_VERSION_UNKNOWN) {

Good point, will fix if we go on with this patch.

In case examine() failed, the state of the target is not the same as if the target was just initialized. Moreover, it is not documented either. This is an issue. I am preparing some patches aimed at addressing this.

Oh I think I see at least one issue:
If tt->init_target() succeds
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L1740
and the following version specific examination fails
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L1744-L1746
dtm_version gets reset to unknown
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L1750-L1752
and later
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L533
will not recognize target type and therefore will not call
https://github.com/riscv/riscv-openocd/blob/5d4fa0001e2f6bd23caf10fd02f980053adb2b3b/src/target/riscv/riscv.c#L545-L546

Hmm, much worse than an annoying message. Giving up for now

tt = get_target_type(target);
if (!tt)
LOG_TARGET_ERROR(target, "Could not identify target type.");
}

if (riscv_flush_registers(target) != ERROR_OK)
LOG_TARGET_ERROR(target, "Failed to flush registers. Ignoring this error.");
Expand Down
Loading