Skip to contentSkip to conversation

plugins: correct docstring for write_register API

PATCH In review Florian Hofhammer 4 participants 10 messages Jan 16 – Jan 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 plugins: correct docstring for write_register API

The return value in the docstring did not match the return value in the code (see plugins/api.c).

---
 include/qemu/qemu-plugin.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/qemu-plugin.h +1 -1
index 1f25fb2b40..def6693a17 100644
@@ -1002,7 +1002,7 @@ int qemu_plugin_read_register(struct qemu_plugin_register *handle,
  * Attempting to write a register with @buf smaller than the register size
  * will result in a crash or other undesired behavior.
  *
- * Returns the number of bytes written. On failure returns 0.
+ * Returns the number of bytes written. On failure returns -1.
  */
 QEMU_PLUGIN_API
 int qemu_plugin_write_register(struct qemu_plugin_register *handle,
Signature · 1 line
2.51.0
#2 lore raw
In reply to Florian Hofhammer
Quoted 13 lines · Florian Hofhammer

The return value in the docstring did not match the return value in the code (see plugins/api.c).

---
  include/qemu/qemu-plugin.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/qemu/qemu-plugin.h +0 -0
index 1f25fb2b40..def6693a17 100644

include/qemu/qemu-plugin.h int qemu_plugin_read_register(struct qemu_plugin_register *handle, · line 1008

Quoted 8 lines
include/qemu/qemu-plugin.h +1 -1
@@ -1002,7 +1002,7 @@ int qemu_plugin_read_register(struct qemu_plugin_register *handle,
   * Attempting to write a register with @buf smaller than the register size
   * will result in a crash or other undesired behavior.
   *
- * Returns the number of bytes written. On failure returns 0.
+ * Returns the number of bytes written. On failure returns -1.
   */
  QEMU_PLUGIN_API
  int qemu_plugin_write_register(struct qemu_plugin_register *handle,

In practice, it may return anything else than 0 (see arm_cpu_gdb_write_register for instance). So the right (vague) description should be:
On success returns 0.

Regards,
Pierrick

#3 lore raw
Quoted 3 lines · Pierrick Bouvier

In practice, it may return anything else than 0 (see arm_cpu_gdb_write_register for instance). So the right (vague) description should be:
On success returns 0.

Hmm, it seems to me as if the code is a bit inconsistent here: the plugin API in plugins/api.c returns -1 if it detects an error directly, and the arm_cpu_gdb_write_register() (but it's similar for other archs, e.g., x86_cpu_gdb_write_register()) returns 0 if the register is unknown and the number of bytes written otherwise (in the arm example: 4 for the general-purpose registers).
That means that currently, both -1 and 0 as return value indicate an error.

Thanks for the catch, that made me dig into the actual gdbstub code a bit more!

In order to make this consistent, there are two options I see:
1) Change the plugin API function to return 0 on error (but then it's inconsistent with the qemu_plugin_read_register() function which returns -1 on error), or
2) Change the arch-specific gdbstub functions to return -1 on error instead of 0.

What do you think? I'd be happy to prepare a patch for either option.

Best regards,
Florian

#4 lore raw
Quoted 15 lines · Florian Hofhammer
Quoted 3 lines · Pierrick Bouvier

In practice, it may return anything else than 0 (see arm_cpu_gdb_write_register for instance). So the right (vague) description should be:
On success returns 0.

Hmm, it seems to me as if the code is a bit inconsistent here: the plugin API in plugins/api.c returns -1 if it detects an error directly, and the arm_cpu_gdb_write_register() (but it's similar for other archs, e.g., x86_cpu_gdb_write_register()) returns 0 if the register is unknown and the number of bytes written otherwise (in the arm example: 4 for the general-purpose registers).
That means that currently, both -1 and 0 as return value indicate an error.

Quoted 3 lines

Thanks for the catch, that made me dig into the actual gdbstub code a bit more!

Indeed, same for me. I've been reading too quick when answering through your first email, and missed the nuance.

Quoted 8 lines

In order to make this consistent, there are two options I see:
1) Change the plugin API function to return 0 on error (but then it's inconsistent with the qemu_plugin_read_register() function which returns -1 on error), or
2) Change the arch-specific gdbstub functions to return -1 on error instead of 0.

What do you think? I'd be happy to prepare a patch for either option.>

For sake of consistency, we should make this use the same interface than {read,write}_memory_vaddr, minus the len param.
bool qemu_plugin_read_memory_vaddr(uint64_t addr,

                                   GByteArray *data, size_t len);
bool qemu_plugin_write_memory_vaddr(uint64_t addr,
                                   GByteArray *data);

So it would be:
bool qemu_plugin_read_register(uint64_t addr, GByteArray *data); bool qemu_plugin_write_register(uint64_t addr, GByteArray *data);

This is better and unambiguous, as no one needs a documentation to know what a bool return is, and data already holds the size information.

As well, writing this, I realized that existing write_register is broken by design: we never check the size of data array (except > 1) and blindly an arbitrary amount of memory from it, which is wrong. Even though the doc mentions it, we should just fix it, detect when size < reg_size, and return false.

This comes from the fact gdb_write_register itself has no notion of size and trust the pointer. so it would need another refactor also. And while at it, change gdb_{read,write}_register definition to return bool also.

Quoted 2 lines

Best regards,
Florian

I think it pushes a lot of changes just for a simple comment change, so I would understand if you don't want to do this whole refactoring beyond plugins interface. I can pick it up, or let you work on it if you have time/interest. Feel free to let us know.

In all cases, thank you for pointing this.

Regards,
Pierrick

#5 lore raw
Quoted 52 lines · Pierrick Bouvier
Quoted 15 lines · Florian Hofhammer
Quoted 3 lines · Pierrick Bouvier

In practice, it may return anything else than 0 (see arm_cpu_gdb_write_register for instance). So the right (vague) description should be:
On success returns 0.

Hmm, it seems to me as if the code is a bit inconsistent here: the plugin API in plugins/api.c returns -1 if it detects an error directly, and the arm_cpu_gdb_write_register() (but it's similar for other archs, e.g., x86_cpu_gdb_write_register()) returns 0 if the register is unknown and the number of bytes written otherwise (in the arm example: 4 for the general-purpose registers).
That means that currently, both -1 and 0 as return value indicate an error.

Quoted 3 lines

Thanks for the catch, that made me dig into the actual gdbstub code a bit more!

Indeed, same for me. I've been reading too quick when answering through your first email, and missed the nuance.

Quoted 8 lines

In order to make this consistent, there are two options I see:
1) Change the plugin API function to return 0 on error (but then it's inconsistent with the qemu_plugin_read_register() function which returns -1 on error), or
2) Change the arch-specific gdbstub functions to return -1 on error instead of 0.

What do you think? I'd be happy to prepare a patch for either option.>

For sake of consistency, we should make this use the same interface than {read,write}_memory_vaddr, minus the len param.
bool qemu_plugin_read_memory_vaddr(uint64_t addr,

                                     GByteArray *data, size_t len);
bool qemu_plugin_write_memory_vaddr(uint64_t addr,
                                     GByteArray *data);

So it would be:
bool qemu_plugin_read_register(uint64_t addr, GByteArray *data); bool qemu_plugin_write_register(uint64_t addr, GByteArray *data);

This is better and unambiguous, as no one needs a documentation to know what a bool return is, and data already holds the size information.

As well, writing this, I realized that existing write_register is broken by design: we never check the size of data array (except > 1) and blindly an arbitrary amount of memory from it, which is wrong. Even though the doc mentions it, we should just fix it, detect when size < reg_size, and return false.

Quoted 4 lines

This comes from the fact gdb_write_register itself has no notion of size and trust the pointer. so it would need another refactor also. And while at it, change gdb_{read,write}_register definition to return bool also.

I reviewed that code by the way, so definitely my fault I let it through.

#6 lore raw
In reply to Pierrick Bouvier
Quoted 35 lines · Pierrick Bouvier
Quoted 15 lines · Florian Hofhammer
Quoted 3 lines · Pierrick Bouvier

In practice, it may return anything else than 0 (see arm_cpu_gdb_write_register for instance). So the right (vague) description should be:
On success returns 0.

Hmm, it seems to me as if the code is a bit inconsistent here: the plugin API in plugins/api.c returns -1 if it detects an error directly, and the arm_cpu_gdb_write_register() (but it's similar for other archs, e.g., x86_cpu_gdb_write_register()) returns 0 if the register is unknown and the number of bytes written otherwise (in the arm example: 4 for the general-purpose registers).
That means that currently, both -1 and 0 as return value indicate an error.

Quoted 3 lines

Thanks for the catch, that made me dig into the actual gdbstub code a bit more!

Indeed, same for me. I've been reading too quick when answering through your first email, and missed the nuance.

Quoted 8 lines

In order to make this consistent, there are two options I see:
1) Change the plugin API function to return 0 on error (but then it's inconsistent with the qemu_plugin_read_register() function which returns -1 on error), or
2) Change the arch-specific gdbstub functions to return -1 on error instead of 0.

What do you think? I'd be happy to prepare a patch for either option.>

For sake of consistency, we should make this use the same interface than {read,write}_memory_vaddr, minus the len param.
bool qemu_plugin_read_memory_vaddr(uint64_t addr,

Quoted 7 lines
                                     GByteArray *data, size_t len);
bool qemu_plugin_write_memory_vaddr(uint64_t addr,
                                     GByteArray *data);

So it would be:
bool qemu_plugin_read_register(uint64_t addr, GByteArray *data); bool qemu_plugin_write_register(uint64_t addr, GByteArray *data);

Meant:

bool qemu_plugin_read_register(struct qemu_plugin_register *handle,

                               GByteArray *data);
bool qemu_plugin_write_register(struct qemu_plugin_register *handle,
                                GByteArray *data);
Quoted 26 lines

This is better and unambiguous, as no one needs a documentation to know what a bool return is, and data already holds the size information.

As well, writing this, I realized that existing write_register is broken by design: we never check the size of data array (except > 1) and blindly an arbitrary amount of memory from it, which is wrong. Even though the doc mentions it, we should just fix it, detect when size < reg_size, and return false.

This comes from the fact gdb_write_register itself has no notion of size and trust the pointer. so it would need another refactor also. And while at it, change gdb_{read,write}_register definition to return bool also.

Quoted 2 lines

Best regards,
Florian

I think it pushes a lot of changes just for a simple comment change, so I would understand if you don't want to do this whole refactoring beyond plugins interface. I can pick it up, or let you work on it if you have time/interest. Feel free to let us know.

In all cases, thank you for pointing this.

Regards,
Pierrick

#7 lore raw
In reply to Pierrick Bouvier
Quoted 7 lines · Pierrick Bouvier

For sake of consistency, we should make this use the same interface than {read,write}_memory_vaddr, minus the len param.
bool qemu_plugin_read_memory_vaddr(uint64_t addr,

                                   GByteArray *data, size_t len);
bool qemu_plugin_write_memory_vaddr(uint64_t addr,
                                   GByteArray *data);
Quoted 5 lines

So it would be:
bool qemu_plugin_read_register(uint64_t addr, GByteArray *data); bool qemu_plugin_write_register(uint64_t addr, GByteArray *data);

This is better and unambiguous, as no one needs a documentation to know what a bool return is, and data already holds the size information.

Sounds like a good idea. This would break existing plugins though, is this acceptable? (Asking purely out of curiosity, as I'm not familiar yet with the processes and policies around breaking changes in QEMU development :))

Quoted 4 lines

As well, writing this, I realized that existing write_register is broken by design: we never check the size of data array (except > 1) and blindly an arbitrary amount of memory from it, which is wrong. Even though the doc mentions it, we should just fix it, detect when size < reg_size, and return false.

This comes from the fact gdb_write_register itself has no notion of size and trust the pointer. so it would need another refactor also. And while at it, change gdb_{read,write}_register definition to return bool also.

gdb_read_register already takes a GByteArray, it would make sense to also use GByteArray for gdb_write_register instead of a uint8_t* and use the size of the array for sanity checks.
However, I'm not sure changing the return value to bool for those internal functions makes sense, as other parts of the codebase rely on the size information. E.g., handle_write_all_regs() in gdbstub/gdbstub.c relies on the size returned by gdb_write_register() to know how far to offset into the packet data it received from GDB, as GDB does not send register size information when writing multiple registers via a 'G' packet (just a flat byte stream with the register values concatenated).

Quoted 1 line

I think it pushes a lot of changes just for a simple comment change, so I would understand if you don't want to do this whole refactoring beyond plugins interface. I can pick it up, or let you work on it if you have time/interest. Feel free to let us know.

I'd be happy to handle this!

Best regards,
Florian

#8 lore raw
Quoted 13 lines · Florian Hofhammer
Quoted 11 lines · Pierrick Bouvier

For sake of consistency, we should make this use the same interface than {read,write}_memory_vaddr, minus the len param.
bool qemu_plugin_read_memory_vaddr(uint64_t addr,

                                    GByteArray *data, size_t len);
bool qemu_plugin_write_memory_vaddr(uint64_t addr,
                                    GByteArray *data);

So it would be:
bool qemu_plugin_read_register(uint64_t addr, GByteArray *data); bool qemu_plugin_write_register(uint64_t addr, GByteArray *data);

Quoted 7 lines
Quoted 1 line

This is better and unambiguous, as no one needs a documentation to know what a bool return is, and data already holds the size information.

Sounds like a good idea. This would break existing plugins though, is this acceptable? (Asking purely out of curiosity, as I'm not familiar yet with the processes and policies around breaking changes in QEMU development :))

As long as we bump QEMU_PLUGIN_VERSION, this is acceptable.
Of course, the series will need to modify existing plugins to use the new signature.
It's a bit tricky as the bool can be implicitly converted to an integer, but it's worth fixing this for coherency sake with read/memory vaddr.

Quoted 5 lines
Quoted 4 lines

As well, writing this, I realized that existing write_register is broken by design: we never check the size of data array (except > 1) and blindly an arbitrary amount of memory from it, which is wrong. Even though the doc mentions it, we should just fix it, detect when size < reg_size, and return false.

This comes from the fact gdb_write_register itself has no notion of size and trust the pointer. so it would need another refactor also. And while at it, change gdb_{read,write}_register definition to return bool also.

Quoted 11 lines

gdb_read_register already takes a GByteArray, it would make sense to also use GByteArray for gdb_write_register instead of a uint8_t* and use the size of the array for sanity checks.
However, I'm not sure changing the return value to bool for those internal functions makes sense, as other parts of the codebase rely on the size information. E.g., handle_write_all_regs() in gdbstub/gdbstub.c relies on the size returned by gdb_write_register() to know how far to offset into the packet data it received from GDB, as GDB does not send register size information when writing multiple registers via a 'G' packet (just a flat byte stream with the register values concatenated).

I don't want to put the burden on you to cleanup all this kind of stuff, so you can simply let the gdbstub refactoring out of this, and fix API plugin only.

Quoted 4 lines
Quoted 1 line

I think it pushes a lot of changes just for a simple comment change, so I would understand if you don't want to do this whole refactoring beyond plugins interface. I can pick it up, or let you work on it if you have time/interest. Feel free to let us know.

I'd be happy to handle this!

Thanks for volunteering!

Quoted 2 lines

Best regards,
Florian

#9 lore raw
In reply to Florian Hofhammer
Quoted 28 lines · Florian Hofhammer
Quoted 7 lines · Pierrick Bouvier

For sake of consistency, we should make this use the same interface than {read,write}_memory_vaddr, minus the len param.
bool qemu_plugin_read_memory_vaddr(uint64_t addr,

                                   GByteArray *data, size_t len);
bool qemu_plugin_write_memory_vaddr(uint64_t addr,
                                   GByteArray *data);
Quoted 5 lines

So it would be:
bool qemu_plugin_read_register(uint64_t addr, GByteArray *data); bool qemu_plugin_write_register(uint64_t addr, GByteArray *data);

This is better and unambiguous, as no one needs a documentation to know what a bool return is, and data already holds the size information.

Sounds like a good idea. This would break existing plugins though, is this acceptable? (Asking purely out of curiosity, as I'm not familiar yet with the processes and policies around breaking changes in QEMU development :))

Quoted 7 lines

As well, writing this, I realized that existing write_register is broken by design: we never check the size of data array (except > 1) and blindly an arbitrary amount of memory from it, which is wrong. Even though the doc mentions it, we should just fix it, detect when size < reg_size, and return false.

This comes from the fact gdb_write_register itself has no notion of size and trust the pointer. so it would need another refactor also. And while at it, change gdb_{read,write}_register definition to return bool also.

Quoted 11 lines

gdb_read_register already takes a GByteArray, it would make sense to also use GByteArray for gdb_write_register instead of a uint8_t* and use the size of the array for sanity checks.
However, I'm not sure changing the return value to bool for those internal functions makes sense, as other parts of the codebase rely on the size information. E.g., handle_write_all_regs() in gdbstub/gdbstub.c relies on the size returned by gdb_write_register() to know how far to offset into the packet data it received from GDB, as GDB does not send register size information when writing multiple registers via a 'G' packet (just a flat byte stream with the register values concatenated).

We don't need to expose internal implementation details to the plugins. If we have length already as part of GByteArray then returning a bool for success/fail makes sense. Its the more modern pattern throughout QEMU anyway.

Quoted 10 lines
Quoted 5 lines

I think it pushes a lot of changes just for a simple comment change, so I would understand if you don't want to do this whole refactoring beyond plugins interface. I can pick it up, or let you work on it if you have time/interest. Feel free to let us know.

I'd be happy to handle this!

Best regards,
Florian

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro