Message ID | 20221129031659.2263453-1-lixing@loongson.cn |
---|---|
State | Superseded |
Headers |
Return-Path: <libc-alpha-bounces+patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B807D3852C63 for <patchwork@sourceware.org>; Tue, 29 Nov 2022 03:17:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id A6A243858C20 for <libc-alpha@sourceware.org>; Tue, 29 Nov 2022 03:17:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A6A243858C20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.5]) by gateway (Coremail) with SMTP id _____8CxKemteYVj3OYBAA--.1341S3; Tue, 29 Nov 2022 11:17:02 +0800 (CST) Received: from 5.5.5 (unknown [10.2.5.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cxb+KseYVj0OQdAA--.10461S4; Tue, 29 Nov 2022 11:17:00 +0800 (CST) From: XingLi <lixing@loongson.cn> To: adhemerval.zanella@linaro.org Cc: libc-alpha@sourceware.org, i.swmail@xen0n.name, xry111@xry111.site, caiyinyu@loongson.cn, wanghongliang@loongson.cn, hejinyang@loongson.cn, Xing Li <lixing@loongson.cn> Subject: [PATCH] linux: Change syscall return value to long int Date: Tue, 29 Nov 2022 11:16:59 +0800 Message-Id: <20221129031659.2263453-1-lixing@loongson.cn> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8Cxb+KseYVj0OQdAA--.10461S4 X-CM-SenderInfo: pol0x03j6o00pqjv00gofq/ X-Coremail-Antispam: 1U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VWUJVW8JbIYCTnIWIevJa73UjIFyTuY vjxU74lkDUUUU X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list <libc-alpha.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/libc-alpha/> List-Post: <mailto:libc-alpha@sourceware.org> List-Help: <mailto:libc-alpha-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=subscribe> Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" <libc-alpha-bounces+patchwork=sourceware.org@sourceware.org> |
Series |
linux: Change syscall return value to long int
|
|
Commit Message
lixing
Nov. 29, 2022, 3:16 a.m. UTC
From: Xing Li <lixing@loongson.cn>
The kernel syscall return is long value.
The generic syscall interface return value
is int, which may lead to incorrect return value.
The following test is syscall with mmap executed on LoongArch,
only 32bits and sign extension value returned leading to mmap failure,
which should be with 47bits address returned.
Testcase:
#include <sys/syscall.h>
#include <sys/mman.h>
#include <stdio.h>
void main()
{
long int ret;
ret = syscall(SYS_mmap, NULL, 0x801000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
printf("map address is %lx\n",ret);
}
Result:
[lixing@Sunhaiyong test]$ ./mmap
map address is fffffffff008c000
---
sysdeps/unix/sysv/linux/syscall.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Comments
The 11/29/2022 11:16, XingLi wrote: > From: Xing Li <lixing@loongson.cn> > > The kernel syscall return is long value. > The generic syscall interface return value > is int, which may lead to incorrect return value. it's not clear what you mean here, the generic syscall function returns long (according to unistd.h). > > The following test is syscall with mmap executed on LoongArch, > only 32bits and sign extension value returned leading to mmap failure, > which should be with 47bits address returned. > > Testcase: > > #include <sys/syscall.h> > #include <sys/mman.h> > #include <stdio.h> > > void main() > { > long int ret; > ret = syscall(SYS_mmap, NULL, 0x801000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); Note: there are many reasons why direct calls to syscall may not work. syscall is a variadic argument function that takes long arguments, but you pass ints that may *not* be sign/zero extended on the caller site so e.g. the top 32bits of size and offset can be arbitrary on a 64bit system. (you have to cast args to long to make the example valid). and some systems use SYS_mmap2. > printf("map address is %lx\n",ret); > } > > Result: > [lixing@Sunhaiyong test]$ ./mmap > map address is fffffffff008c000 > --- > sysdeps/unix/sysv/linux/syscall.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/sysdeps/unix/sysv/linux/syscall.c b/sysdeps/unix/sysv/linux/syscall.c > index 7303ba7188..8cb0b66b1c 100644 > --- a/sysdeps/unix/sysv/linux/syscall.c > +++ b/sysdeps/unix/sysv/linux/syscall.c > @@ -33,7 +33,7 @@ syscall (long int number, ...) > long int a5 = va_arg (args, long int); > va_end (args); > > - int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, a4, a5); > + long int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, a4, a5); > if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (r))) this change looks reasonable to me. > { > __set_errno (-r); > -- > 2.31.1 >
Xuerui: can you help to rewrite the commit message? The code change seems obvious but I'm not sure how to describe the rationale precisely either. On Tue, 2022-11-29 at 08:55 +0000, Szabolcs Nagy wrote: > The 11/29/2022 11:16, XingLi wrote: > > From: Xing Li <lixing@loongson.cn> > > > > The kernel syscall return is long value. > > The generic syscall interface return value > > is int, which may lead to incorrect return value. > > it's not clear what you mean here, the generic syscall > function returns long (according to unistd.h). > > > > > The following test is syscall with mmap executed on LoongArch, > > only 32bits and sign extension value returned leading to mmap > > failure, > > which should be with 47bits address returned. > > > > Testcase: > > > > #include <sys/syscall.h> > > #include <sys/mman.h> > > #include <stdio.h> > > > > void main() > > { > > long int ret; > > ret = syscall(SYS_mmap, NULL, 0x801000, PROT_READ | > > PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); > > Note: there are many reasons why direct calls to syscall > may not work. > > syscall is a variadic argument function that takes long > arguments, but you pass ints that may *not* be sign/zero > extended on the caller site so e.g. the top 32bits of > size and offset can be arbitrary on a 64bit system. > (you have to cast args to long to make the example valid). > > and some systems use SYS_mmap2. > > > printf("map address is %lx\n",ret); > > } > > > > Result: > > [lixing@Sunhaiyong test]$ ./mmap > > map address is fffffffff008c000 > > --- > > sysdeps/unix/sysv/linux/syscall.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/sysdeps/unix/sysv/linux/syscall.c > > b/sysdeps/unix/sysv/linux/syscall.c > > index 7303ba7188..8cb0b66b1c 100644 > > --- a/sysdeps/unix/sysv/linux/syscall.c > > +++ b/sysdeps/unix/sysv/linux/syscall.c > > @@ -33,7 +33,7 @@ syscall (long int number, ...) > > long int a5 = va_arg (args, long int); > > va_end (args); > > > > - int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, a4, > > a5); > > + long int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, > > a4, a5); > > if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (r))) > > this change looks reasonable to me. > > > { > > __set_errno (-r); > > -- > > 2.31.1 > >
The 11/29/2022 17:12, Xi Ruoyao wrote: > Xuerui: can you help to rewrite the commit message? The code change > seems obvious but I'm not sure how to describe the rationale precisely > either. what about: linux: Use long int for syscall return value The linux syscall ABI returns long, so the generic syscall code for linux should use long for the return value. This fixes the truncation of the return value of the syscall function when that does not fit into an int.
在 2022/11/29 下午5:37, Szabolcs Nagy 写道: > The 11/29/2022 17:12, Xi Ruoyao wrote: >> Xuerui: can you help to rewrite the commit message? The code change >> seems obvious but I'm not sure how to describe the rationale precisely >> either. > > what about: > > linux: Use long int for syscall return value > > The linux syscall ABI returns long, so the generic syscall code for > linux should use long for the return value. > > This fixes the truncation of the return value of the syscall function > when that does not fit into an int. > thanks
diff --git a/sysdeps/unix/sysv/linux/syscall.c b/sysdeps/unix/sysv/linux/syscall.c index 7303ba7188..8cb0b66b1c 100644 --- a/sysdeps/unix/sysv/linux/syscall.c +++ b/sysdeps/unix/sysv/linux/syscall.c @@ -33,7 +33,7 @@ syscall (long int number, ...) long int a5 = va_arg (args, long int); va_end (args); - int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, a4, a5); + long int r = INTERNAL_SYSCALL_NCS_CALL (number, a0, a1, a2, a3, a4, a5); if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (r))) { __set_errno (-r);