All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0)
@ 2017-09-29 16:50 zhuoweizhang
  2017-09-29 19:14 ` Laurent Vivier
  0 siblings, 1 reply; 7+ messages in thread
From: zhuoweizhang @ 2017-09-29 16:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: riku.voipio, laurent, Zhuowei Zhang

From: Zhuowei Zhang <zhuoweizhang@yahoo.com>

Linux returns success for the special case of calling write with a zero-length
NULL buffer: compiling and running

```

int main() {
   ssize_t ret = write(STDOUT_FILENO, NULL, 0);
   fprintf(stderr, "write returned %ld\n", ret);
   return 0;
}
```
gives "write returned 0" when run directly, but "write returned -1" in QEMU.

This commit checks for this situation and returns success if found.

Signed-off-by: Zhuowei Zhang <zhuoweizhang@yahoo.com>
---
 linux-user/syscall.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 9b6364a..ecadf49 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7783,6 +7783,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         }
         break;
     case TARGET_NR_write:
+        if (arg2 == 0 && arg3 == 0) {
+            /* special case: write(fd, NULL, 0) returns success. */
+            ret = 0;
+            break;
+        }
         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
             goto efault;
         if (fd_trans_target_to_host_data(arg1)) {
-- 
1.9.1


.

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0)
  2017-09-29 16:50 [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0) zhuoweizhang
@ 2017-09-29 19:14 ` Laurent Vivier
  2017-09-29 19:49   ` Peter Maydell
  2017-09-29 23:33   ` Carlo Arenas
  0 siblings, 2 replies; 7+ messages in thread
From: Laurent Vivier @ 2017-09-29 19:14 UTC (permalink / raw)
  To: zhuoweizhang, qemu-devel; +Cc: riku.voipio

Le 29/09/2017 à 18:50, zhuoweizhang@yahoo.com a écrit :
> From: Zhuowei Zhang <zhuoweizhang@yahoo.com>
> 
> Linux returns success for the special case of calling write with a zero-length
> NULL buffer: compiling and running
> 
> ```
> 
> int main() {
>    ssize_t ret = write(STDOUT_FILENO, NULL, 0);
>    fprintf(stderr, "write returned %ld\n", ret);
>    return 0;
> }
> ```
> gives "write returned 0" when run directly, but "write returned -1" in QEMU.
> 
> This commit checks for this situation and returns success if found.
> 
> Signed-off-by: Zhuowei Zhang <zhuoweizhang@yahoo.com>
> ---
>  linux-user/syscall.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 9b6364a..ecadf49 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -7783,6 +7783,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
>          }
>          break;
>      case TARGET_NR_write:
> +        if (arg2 == 0 && arg3 == 0) {
> +            /* special case: write(fd, NULL, 0) returns success. */
> +            ret = 0;
> +            break;
> +        }
>          if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
>              goto efault;
>          if (fd_trans_target_to_host_data(arg1)) {
> 

I think we should keep the call to the kernel write() as the behavior
depends on the driver behind the syscall. Moreover, calling write() with
(NULL, 0) can triggers "something" at kernel level.

Thanks,
Laurent

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0)
  2017-09-29 19:14 ` Laurent Vivier
@ 2017-09-29 19:49   ` Peter Maydell
  2017-09-29 23:33   ` Carlo Arenas
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Maydell @ 2017-09-29 19:49 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: zhuoweizhang, QEMU Developers, Riku Voipio

On 29 September 2017 at 12:14, Laurent Vivier <laurent@vivier.eu> wrote:
> Le 29/09/2017 à 18:50, zhuoweizhang@yahoo.com a écrit :
>> From: Zhuowei Zhang <zhuoweizhang@yahoo.com>
>>
>> Linux returns success for the special case of calling write with a zero-length
>> NULL buffer: compiling and running
>>
>> ```
>>
>> int main() {
>>    ssize_t ret = write(STDOUT_FILENO, NULL, 0);
>>    fprintf(stderr, "write returned %ld\n", ret);
>>    return 0;
>> }
>> ```
>> gives "write returned 0" when run directly, but "write returned -1" in QEMU.
>>
>> This commit checks for this situation and returns success if found.
>>
>> Signed-off-by: Zhuowei Zhang <zhuoweizhang@yahoo.com>
>> ---
>>  linux-user/syscall.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
>> index 9b6364a..ecadf49 100644
>> --- a/linux-user/syscall.c
>> +++ b/linux-user/syscall.c
>> @@ -7783,6 +7783,11 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
>>          }
>>          break;
>>      case TARGET_NR_write:
>> +        if (arg2 == 0 && arg3 == 0) {
>> +            /* special case: write(fd, NULL, 0) returns success. */
>> +            ret = 0;
>> +            break;
>> +        }
>>          if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
>>              goto efault;
>>          if (fd_trans_target_to_host_data(arg1)) {
>>
>
> I think we should keep the call to the kernel write() as the behavior
> depends on the driver behind the syscall. Moreover, calling write() with
> (NULL, 0) can triggers "something" at kernel level.

I tend to agree. On the other hand our handling of NR_read
with a zero count just returns 0 without calling the syscall;
perhaps we should change that for consistency?

Do we also have the same issue with pread64/pwrite64 ?

thanks
-- PMM

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0)
  2017-09-29 19:14 ` Laurent Vivier
  2017-09-29 19:49   ` Peter Maydell
@ 2017-09-29 23:33   ` Carlo Arenas
  2017-09-29 23:38     ` Peter Maydell
  1 sibling, 1 reply; 7+ messages in thread
From: Carlo Arenas @ 2017-09-29 23:33 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: zhuoweizhang, qemu-devel, riku.voipio

a return of 0 is a valid response for write per POSIX and just indicates 0
bytes were written.

the use of NULL for the buffer is not a trigger for that behaviour, but the
fact that the third parameter asks for 0 bytes to be copied.

therefore there is no need to get logic in this handler to check for NULL
IMHO

Carlo

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0)
  2017-09-29 23:33   ` Carlo Arenas
@ 2017-09-29 23:38     ` Peter Maydell
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Maydell @ 2017-09-29 23:38 UTC (permalink / raw)
  To: Carlo Arenas; +Cc: Laurent Vivier, Riku Voipio, zhuoweizhang, QEMU Developers

On 29 September 2017 at 16:33, Carlo Arenas <carenas@gmail.com> wrote:
> a return of 0 is a valid response for write per POSIX and just indicates 0
> bytes were written.

The interface we are implementing here is not the POSIX write()
function but the Linux write syscall, whose semantics are not
necessarily identical, though perhaps they may be in this case.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0)
  2018-02-14 23:33 Oliver Smith
@ 2018-02-15  8:50 ` Laurent Vivier
  0 siblings, 0 replies; 7+ messages in thread
From: Laurent Vivier @ 2018-02-15  8:50 UTC (permalink / raw)
  To: Oliver Smith, qemu-devel, peter.maydell; +Cc: zhuoweizhang

Le 15/02/2018 à 00:33, Oliver Smith a écrit :
> Hello there,
> 
> I'm a little late to the party. But what is necessary to get this
> upstreamed, and how can I help?
> 
> PS: Sorry if I picked the wrong e-mail addresses, I wasn't subscribed to
> the ML at that point and used the addresses I could find for the people
> who answered to the original thread here:
> 
> https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg08073.html

According to comments in the ML thread, you need to:

- update the patch to call write() with NULL and 0, something like:

--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7912,6 +7912,10 @@ abi_long do_syscall(void *cpu_env, int num,
abi_long arg1,
         }
         break;
     case TARGET_NR_write:
+        if (arg2 == 0 && arg3 == 0) {
+            ret = get_errno(safe_write(arg1, NULL, 0));
+            break;
+        }
         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
             goto efault;
         if (fd_trans_target_to_host_data(arg1)) {

- change TARGET_NR_read to do the same

- check if we need to do the same for pread64/pwrite64

Thanks,
Laurent

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0)
@ 2018-02-14 23:33 Oliver Smith
  2018-02-15  8:50 ` Laurent Vivier
  0 siblings, 1 reply; 7+ messages in thread
From: Oliver Smith @ 2018-02-14 23:33 UTC (permalink / raw)
  To: qemu-devel, carenas, laurent, peter.maydell; +Cc: zhuoweizhang

Hello there,

I'm a little late to the party. But what is necessary to get this
upstreamed, and how can I help?

PS: Sorry if I picked the wrong e-mail addresses, I wasn't subscribed to
the ML at that point and used the addresses I could find for the people
who answered to the original thread here:

https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg08073.html

Thanks,
Oliver


From: Zhuowei Zhang <address@hidden>
> Linux returns success for the special case of calling write with a
> zero-length NULL buffer: compiling and running
>
> ```
>
> int main() {
>    ssize_t ret = write(STDOUT_FILENO, NULL, 0);
>    fprintf(stderr, "write returned %ld\n", ret);
>    return 0;
> }
> ```
> gives "write returned 0" when run directly, but "write returned -1" in
> QEMU.
>
> This commit checks for this situation and returns success if found.
>
> Signed-off-by: Zhuowei Zhang <address@hidden>
> ---
>  linux-user/syscall.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 9b6364a..ecadf49 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -7783,6 +7783,11 @@ abi_long do_syscall(void *cpu_env, int num,
abi_long
> arg1,
>          }
>          break;
>      case TARGET_NR_write:
> +        if (arg2 == 0 && arg3 == 0) {
> +            /* special case: write(fd, NULL, 0) returns success. */
> +            ret = 0;
> +            break;
> +        }
>          if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
>              goto efault;
>          if (fd_trans_target_to_host_data(arg1)) {
> --
> 1.9.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-02-15  8:50 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-29 16:50 [Qemu-devel] [PATCH] syscall: fix special case of write(fd, NULL, 0) zhuoweizhang
2017-09-29 19:14 ` Laurent Vivier
2017-09-29 19:49   ` Peter Maydell
2017-09-29 23:33   ` Carlo Arenas
2017-09-29 23:38     ` Peter Maydell
2018-02-14 23:33 Oliver Smith
2018-02-15  8:50 ` Laurent Vivier

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.