plugins: fix syscall filter return value type
cover plugins: fix syscall filter return value type
Hi,
The syscall filter plugin interface was introduced by me earlier. Its sysret value is declared as uint64_t, but it is semantically signed -- negative values encode errno codes -- so it should be int64_t. This also makes it consistent with the ret parameter of qemu_plugin_vcpu_syscall_ret(), which is already int64_t.
I did not bump QEMU_PLUGIN_VERSION: this is only a signedness fix, and a version bump is better left for the next significant plugin API update.
Thanks for your review.
Ziyang Zhang (1):
plugins: use int64_t for the syscall filter return value
include/plugins/qemu-plugin.h | 2 +- include/qemu/plugin.h | 4 ++-- linux-user/syscall.c | 2 +- plugins/core.c | 2 +- tests/tcg/plugins/setpc.c | 2 +- tests/tcg/plugins/syscall.c | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/1 plugins: use int64_t for the syscall filter return value
The syscall return value passed back through the syscall filter callback is semantically signed: negative values encode errno codes. Declaring the sysret pointer as uint64_t * is therefore misleading and forces callers to launder the value through an unsigned temporary.
Change the sysret pointer to int64_t * across the public plugin API typedef (qemu_plugin_vcpu_syscall_filter_cb_t), the internal qemu_plugin_vcpu_syscall_filter() prototypes and stub, its implementation in plugins/core.c, the linux-user caller, and the in-tree example plugins.
--- include/plugins/qemu-plugin.h | 2 +- include/qemu/plugin.h | 4 ++-- linux-user/syscall.c | 2 +- plugins/core.c | 2 +- tests/tcg/plugins/setpc.c | 2 +- tests/tcg/plugins/syscall.c | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-)
include/plugins/qemu-plugin.h +1 -1
@@ -870,7 +870,7 @@ typedef bool int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, uint64_t a8, - uint64_t *sysret); + int64_t *sysret);/** * typedef qemu_plugin_vcpu_syscall_ret_cb_t - vCPU syscall return callback
include/qemu/plugin.h +2 -2
@@ -174,7 +174,7 @@ bool qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, - uint64_t a8, uint64_t *sysret); + uint64_t a8, int64_t *sysret); void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, uint64_t value_low,
@@ -290,7 +290,7 @@ static inline bool qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, - uint64_t a8, uint64_t *sysret) + uint64_t a8, int64_t *sysret) {return false; }
linux-user/syscall.c +1 -1
@@ -14378,7 +14378,7 @@ static bool send_through_syscall_filters(CPUState *cpu, int num, abi_long arg7, abi_long arg8, abi_long *sysret) { - uint64_t sysret64 = 0; + int64_t sysret64 = 0;bool filtered =qemu_plugin_vcpu_syscall_filter (cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, &sysret64);
plugins/core.c +1 -1
@@ -596,7 +596,7 @@ bool qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, - uint64_t a8, uint64_t *sysret) + uint64_t a8, int64_t *sysret) {struct qemu_plugin_cb *cb, *next;enum qemu_plugin_event ev =QEMU_PLUGIN_EV_VCPU_SYSCALL_FILTER ;
tests/tcg/plugins/setpc.c +1 -1
@@ -27,7 +27,7 @@ static bool vcpu_syscall_filter(qemu_plugin_id_t id, unsigned int vcpu_index, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, uint64_t a8, - uint64_t *sysret) + int64_t *sysret) { if (num == MAGIC_SYSCALL) { if (a1 == SETPC) {
tests/tcg/plugins/syscall.c +1 -1
@@ -174,7 +174,7 @@ static bool vcpu_syscall_filter(qemu_plugin_id_t id, unsigned int vcpu_index, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, uint64_t a8, - uint64_t *sysret) + int64_t *sysret) { /* Special syscall to test the filter functionality. */ if (num == 4096 && a1 == 0x66CCFF) {
Signature · 1 line
2.34.1
Re: [PATCH 1/1] plugins: use int64_t for the syscall filter return value
Quoted 69 lines · Ziyang Zhang
The syscall return value passed back through the syscall filter callback is semantically signed: negative values encode errno codes. Declaring the sysret pointer as
uint64_t* is therefore misleading and forces callers to launder the value through an unsigned temporary.Change the sysret pointer to
int64_t* across the public plugin API typedef (qemu_plugin_vcpu_syscall_filter_cb_t), the internalqemu_plugin_vcpu_syscall_filter()prototypes and stub, its implementation inplugins/core.c, the linux-user caller, and the in-tree example plugins.Signed-off-by Ziyang Zhang <[email protected]>--- include/plugins/qemu-plugin.h | 2 +- include/qemu/plugin.h | 4 ++-- linux-user/syscall.c | 2 +- plugins/core.c | 2 +- tests/tcg/plugins/setpc.c | 2 +- tests/tcg/plugins/syscall.c | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-)include/plugins/qemu-plugin.h +1
-1@@ -870,7 +870,7 @@ typedef bool int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, uint64_t a8, - uint64_t *sysret); + int64_t *sysret);/** * typedef qemu_plugin_vcpu_syscall_ret_cb_t - vCPU syscall return callback include/qemu/plugin.h +2
-2@@ -174,7 +174,7 @@ bool qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, - uint64_t a8, uint64_t *sysret); + uint64_t a8, int64_t *sysret); void qemu_plugin_vcpu_mem_cb(CPUState *cpu, uint64_t vaddr, uint64_t value_low,@@ -290,7 +290,7 @@ static inline bool qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, - uint64_t a8, uint64_t *sysret) + uint64_t a8, int64_t *sysret) {return false; }linux-user/syscall.c +1
-1@@ -14378,7 +14378,7 @@ static bool send_through_syscall_filters(CPUState *cpu, int num, abi_long arg7, abi_long arg8, abi_long *sysret) { - uint64_t sysret64 = 0; + int64_t sysret64 = 0; bool filtered = qemu_plugin_vcpu_syscall_filter(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8,
Quoted 1 line
&sysret64);
All of the arguments here are abi_long which go to int32_t or target_long->int64_t so perhaps we should be using that for all args to ensure signedness is correct?
Quoted 39 lines
plugins/core.c +1
-1@@ -596,7 +596,7 @@ bool qemu_plugin_vcpu_syscall_filter(CPUState *cpu, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, - uint64_t a8, uint64_t *sysret) + uint64_t a8, int64_t *sysret) {struct qemu_plugin_cb *cb, *next;enum qemu_plugin_event ev =QEMU_PLUGIN_EV_VCPU_SYSCALL_FILTER ;tests/tcg/plugins/setpc.c +1
-1@@ -27,7 +27,7 @@ static bool vcpu_syscall_filter(qemu_plugin_id_t id, unsigned int vcpu_index, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, uint64_t a8, - uint64_t *sysret) + int64_t *sysret) { if (num == MAGIC_SYSCALL) { if (a1 == SETPC) {tests/tcg/plugins/syscall.c +1
-1@@ -174,7 +174,7 @@ static bool vcpu_syscall_filter(qemu_plugin_id_t id, unsigned int vcpu_index, int64_t num, uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4, uint64_t a5, uint64_t a6, uint64_t a7, uint64_t a8, - uint64_t *sysret) + int64_t *sysret) { /* Special syscall to test the filter functionality. */ if (num == 4096 && a1 == 0x66CCFF) {
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [PATCH 1/1] plugins: use int64_t for the syscall filter return value
Quoted 3 lines · Alex Bennée
All of the arguments here are
abi_longwhich go toint32_tortarget_long->int64_tso perhaps we should be using that for all args to ensure signedness is correct?
You're right that abi_long is signed. I only changed the return value to keep this callback consistent with the two syscall callbacks that existed from the first version of the plugin syscall API:qemu_plugin_vcpu_syscall_cb_t and qemu_plugin_vcpu_syscall_ret_cb_t. Both use int64_t for num and int64_t for the return value, while the entry callback passes a1..a8 as uint64_t. This patch matches that.
I'd prefer to keep the arguments as uint64_t. They don't share a single
signedness: pointers and size_t are unsigned, while offsets and fds are signed. At this boundary they are just register-width words that the handler casts per syscall, so the correct extension is per-argument-type (glibc handles them that way too [1]), and forcing them all to int64_t would be wrong for pointer arguments in particular.
Changing the whole argument vector would also touch several APIs and all their callers, which is well beyond the scope of this small signedness fix. So I'd rather keep it to the return value, the one part of this interface with a well-defined signed meaning.
[1] https://github.com/bminor/glibc/blob/master/sysdeps/unix/sysv/linux/x86_64/x32/sysdep.h#L50
Thanks,
Ziyang Zhang
Re: [PATCH 1/1] plugins: use int64_t for the syscall filter return value
Hi Ziyang,
Quoted 23 lines · Ziyang Zhang
Quoted 3 lines · Alex Bennée
All of the arguments here are
abi_longwhich go toint32_tortarget_long->int64_tso perhaps we should be using that for all args to ensure signedness is correct?You're right that
abi_longis signed. I only changed the return value to keep this callback consistent with the two syscall callbacks that existed from the first version of the plugin syscall API:qemu_plugin_vcpu_syscall_cb_tandqemu_plugin_vcpu_syscall_ret_cb_t. Both useint64_tfor num andint64_tfor the return value, while the entry callback passes a1..a8 asuint64_t. This patch matches that.I'd prefer to keep the arguments as
uint64_t. They don't share a single
signedness: pointers andsize_tare unsigned, while offsets and fds are signed. At this boundary they are just register-width words that the handler casts per syscall, so the correct extension is per-argument-type (glibc handles them that way too [1]), and forcing them all toint64_twould be wrong for pointer arguments in particular.Changing the whole argument vector would also touch several APIs and all their callers, which is well beyond the scope of this small signedness fix. So I'd rather keep it to the return value, the one part of this interface with a well-defined signed meaning.
Quoted 6 lines
[1] https://github.com/bminor/glibc/blob/master/sysdeps/unix/sysv/linux/
x86_64/x32/sysdep.h#L50Thanks,
Ziyang Zhang
We can't expose target dependent types, so having unsigned types for args and ret was the correct thing to do. This way, a plugin can decide to use any bit pattern, and upper part is discarded if needed.
For args, we explicitly clamp values depending on target:
https://gitlab.com/qemu-project/qemu/-/blob/master/plugins/core.c?ref_type=heads#L554
For ret, the type conversion is handled by writing ret to a temporary variable, before sending it back:
https://gitlab.com/qemu-project/qemu/-/blob/master/linux-user/syscall.c?ref_type=heads#L14622
Indeed, there is an inconsistency between syscall_ret and syscall_filter on that. However, they do different things:
- syscall_ret will read the value, so having a signed is mandatory. if ret < 0 needs to work.
- syscall_filter will only set it, so having a signed in not mandatory. *ret = -1 still does what is expected.
From that, I'm not sure any change is needed, but feel free to point a use case I missed where this matter for syscall filter.
Regards,
Pierrick
Re: [PATCH 1/1] plugins: use int64_t for the syscall filter return value
Hi Pierrick,
Quoted 18 lines · Pierrick Bouvier
We can't expose target dependent types, so having unsigned types for args and ret was the correct thing to do. This way, a plugin can decide to use any bit pattern, and upper part is discarded if needed.
For args, we explicitly clamp values depending on target:
https://gitlab.com/qemu-project/qemu/-/blob/master/plugins/core.c?ref_type=heads#L554For ret, the type conversion is handled by writing ret to a temporary variable, before sending it back:
https://gitlab.com/qemu-project/qemu/-/blob/master/linux-user/syscall.c?ref_type=heads#L14622Indeed, there is an inconsistency between
syscall_retandsyscall_filteron that. However, they do different things:
-syscall_retwill read the value, so having a signed is mandatory.if ret < 0needs to work.
-syscall_filterwill only set it, so having a signed in not mandatory.*ret = -1still does what is expected.
Quoted 5 lines
From that, I'm not sure any change is needed, but feel free to point a
use case I missed where this matter for syscall filter.Regards,
Pierrick
You're right, and thanks for the detailed explanation. There is no correctness issue here: the filter only sets sysret, and *sysret = -1 behaves the same whether the pointer is signed or unsigned, so nothing is functionally broken.
My concern is purely about consistency. The syscall return value appears in both callbacks: qemu_plugin_vcpu_syscall_filter_cb_t passes it as a writable sysret and qemu_plugin_vcpu_syscall_ret_cb_t passes it as a readable value, but in both it is the same kind of thing, a syscall return value, whose semantics are inherently signed (the negative range encodes errno). Having the write side use uint64_t while the read side uses int64_t looks surprising to a plugin user, since it is the same signed quantity in both places. Keeping the two consistent makes the intent clearer, and as a bonus lets a filter write *sysret = -EINVAL without an implicit signed to unsigned conversion.
As you say this is not mandatory, so I am happy to drop it if you would rather leave it as is. It just seemed like a small readability improvement given the two callbacks describe the same value.
Regards,
Ziyang Zhang
Re: [PATCH 1/1] plugins: use int64_t for the syscall filter return value
Quoted 45 lines · Ziyang Zhang
Hi Pierrick,
Quoted 20 lines · Pierrick Bouvier
We can't expose target dependent types, so having unsigned types for args and ret was the correct thing to do. This way, a plugin can decide to use any bit pattern, and upper part is discarded if needed.
For args, we explicitly clamp values depending on target:
https://gitlab.com/qemu-project/qemu/-/blob/master/plugins/core.c?
ref_type=heads#L554For ret, the type conversion is handled by writing ret to a temporary variable, before sending it back:
https://gitlab.com/qemu-project/qemu/-/blob/master/linux-user/
syscall.c?ref_type=heads#L14622Indeed, there is an inconsistency between
syscall_retandsyscall_filteron that. However, they do different things:
-syscall_retwill read the value, so having a signed is mandatory.if ret < 0needs to work.
-syscall_filterwill only set it, so having a signed in not mandatory.*ret = -1still does what is expected.Quoted 5 lines
From that, I'm not sure any change is needed, but feel free to point a use case I missed where this matter for syscall filter.
Regards,
PierrickYou're right, and thanks for the detailed explanation. There is no correctness issue here: the filter only sets sysret, and *sysret = -1 behaves the same whether the pointer is signed or unsigned, so nothing is functionally broken.
My concern is purely about consistency. The syscall return value appears in both callbacks:
qemu_plugin_vcpu_syscall_filter_cb_tpasses it as a writable sysret andqemu_plugin_vcpu_syscall_ret_cb_tpasses it as a readable value, but in both it is the same kind of thing, a syscall return value, whose semantics are inherently signed (the negative range encodes errno). Having the write side useuint64_twhile the read side usesint64_tlooks surprising to a plugin user, since it is the same signed quantity in both places. Keeping the two consistent makes the intent clearer, and as a bonus lets a filter write *sysret =-EINVALwithout an implicit signed to unsigned conversion.
Quoted 4 lines
As you say this is not mandatory, so I am happy to drop it if you would rather leave it as is. It just seemed like a small readability improvement given the two callbacks describe the same value.
That's convincing enough for me, and yes, consistency matters :). I'll pull today this other series:
https://lore.kernel.org/qemu-devel/[email protected]/
Would that be ok for you to wait for it to come down (hopefully before end of the week), and then publish a new version of yours rebased on top? I can ping you once it's merged if you want.
Quoted 3 lines
Regards,
Ziyang Zhang
Regards,
Pierrick
Re: [PATCH 1/1] plugins: use int64_t for the syscall filter return value
Quoted 3 lines · Pierrick Bouvier
Would that be ok for you to wait for it to come down (hopefully before end of the week), and then publish a new version of yours rebased on top? I can ping you once it's merged if you want.
Thanks. I will send a new patch later.
Quoted 3 lines · Ziyang Zhang
Hi,
The syscall filter plugin interface was introduced by me earlier.
Syscall filter PATCH reference:
https://lore.kernel.org/qemu-devel/[email protected]/