From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by sourceware.org (Postfix) with ESMTPS id 4E39A3898528 for ; Thu, 30 Apr 2020 19:34:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 4E39A3898528 Received: by mail-pj1-x1043.google.com with SMTP id ms17so1279761pjb.0 for ; Thu, 30 Apr 2020 12:34:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=L5EJM6ukMwFbsSfM6BrErxkAmNQ6pApJu+kmliKsODY=; b=XJ656/A3vDUaqsPl5DmMIdktzCZWNfBh+hOJurh1KO3dJbxRJaY1p/O5rVjN1Zi5bI 1QsiH1z35ow4TwKmVCFyEqqfjpeDyyxdljSkbQ6QHECyXQTYZzUpvAsC0VxW56GJWy/K kaIMgSqatDJnASduhcrmkn6jZHBI6/I3Ax2atyLnbEZN2Z3y5ABxbutJkz4fJMsAaR4h XiN9E+NLn0HW3KYdxN3raPs+Mx3mN3ffjZ5RokPJDjO6YqfjDCj03upnTBqa1jPAzg75 E5uZJ7OwashWyVVD8ZhfC78xhdv0m60DqIJMOYVW/ZM8NPvaB3ievKeZeldjNV/roT/1 BZXg== X-Gm-Message-State: AGi0PuYCRSwmMz4qSNL8vGXWeyb+Vs0U18HLfekM7DosJOHfItII9Jfc bWASj58/sc6oFAn/4wnwfv9lN/1r X-Google-Smtp-Source: APiQypKixHVwcSrn1M6oaKSKryaPy7dBKcToTw4PxbO5gTb/8aYsjxWx5faDPqvkGRTcaf/8yhWreQ== X-Received: by 2002:a17:902:207:: with SMTP id 7mr516150plc.331.1588275266118; Thu, 30 Apr 2020 12:34:26 -0700 (PDT) Received: from gnu-cfl-2.localdomain (c-69-181-90-243.hsd1.ca.comcast.net. [69.181.90.243]) by smtp.gmail.com with ESMTPSA id j7sm506067pjy.9.2020.04.30.12.34.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Apr 2020 12:34:24 -0700 (PDT) Received: from gnu-cfl-2.localdomain (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id EA8D21A011C for ; Thu, 30 Apr 2020 12:34:23 -0700 (PDT) From: "H.J. Lu" To: libc-stable@sourceware.org Subject: [2.31/2.30] [PATCH 1/6] x32: Properly pass long to syscall [BZ #25810] Date: Thu, 30 Apr 2020 12:34:18 -0700 Message-Id: <20200430193423.807713-2-hjl.tools@gmail.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200430193423.807713-1-hjl.tools@gmail.com> References: <20200430193423.807713-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-24.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Apr 2020 19:34:29 -0000 X32 has 32-bit long and pointer with 64-bit off_t. Since x32 psABI requires that pointers passed in registers must be zero-extended to 64bit, x32 can share many syscall interfaces with LP64. When a LP64 syscall with long and unsigned long arguments is used for x32, these arguments must be properly extended to 64-bit. Otherwise if the upper 32 bits of the register have undefined value, such a syscall will be rejected by kernel. Enforce zero-extension for pointers and array system call arguments. For integer types, extend to int64_t (the full register) using a regular cast, resulting in zero or sign extension based on the signedness of the original type. For void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); we now generate 0: 41 f7 c1 ff 0f 00 00 test $0xfff,%r9d 7: 75 1f jne 28 <__mmap64+0x28> 9: 48 63 d2 movslq %edx,%rdx c: 89 f6 mov %esi,%esi e: 4d 63 c0 movslq %r8d,%r8 11: 4c 63 d1 movslq %ecx,%r10 14: b8 09 00 00 40 mov $0x40000009,%eax 19: 0f 05 syscall That is 1. addr is unchanged. 2. length is zero-extend to 64 bits. 3. prot is sign-extend to 64 bits. 4. flags is sign-extend to 64 bits. 5. fd is sign-extend to 64 bits. 6. offset is unchanged. For int arguments, since kernel uses only the lower 32 bits and ignores the upper 32 bits in 64-bit registers, these work correctly. Tested on x86-64 and x32. There are no code changes on x86-64. (cherry picked from commit df76ff3a446a787a95cf74cb15c285464d73a93d) --- sysdeps/unix/sysv/linux/x86_64/sysdep.h | 15 +++++++++------ sysdeps/unix/sysv/linux/x86_64/x32/sysdep.h | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h index c2eb37e575..7c60a3a134 100644 --- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h +++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h @@ -210,12 +210,15 @@ /* Registers clobbered by syscall. */ # define REGISTERS_CLOBBERED_BY_SYSCALL "cc", "r11", "cx" -/* Create a variable 'name' based on type 'X' to avoid explicit types. - This is mainly used set use 64-bits arguments in x32. */ -#define TYPEFY(X, name) __typeof__ ((X) - (X)) name -/* Explicit cast the argument to avoid integer from pointer warning on - x32. */ -#define ARGIFY(X) ((__typeof__ ((X) - (X))) (X)) +/* NB: This also works when X is an array. For an array X, type of + (X) - (X) is ptrdiff_t, which is signed, since size of ptrdiff_t + == size of pointer, cast is a NOP. */ +#define TYPEFY1(X) __typeof__ ((X) - (X)) +/* Explicit cast the argument. */ +#define ARGIFY(X) ((TYPEFY1 (X)) (X)) +/* Create a variable 'name' based on type of variable 'X' to avoid + explicit types. */ +#define TYPEFY(X, name) __typeof__ (ARGIFY (X)) name #undef INTERNAL_SYSCALL #define INTERNAL_SYSCALL(name, err, nr, args...) \ diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/x32/sysdep.h index 5bf9eed80b..a37d520f86 100644 --- a/sysdeps/unix/sysv/linux/x86_64/x32/sysdep.h +++ b/sysdeps/unix/sysv/linux/x86_64/x32/sysdep.h @@ -26,4 +26,20 @@ #undef LO_HI_LONG #define LO_HI_LONG(val) (val) +#ifndef __ASSEMBLER__ +# undef ARGIFY +/* Enforce zero-extension for pointers and array system call arguments. + For integer types, extend to int64_t (the full register) using a + regular cast, resulting in zero or sign extension based on the + signedness of the original type. */ +# define ARGIFY(X) \ + ({ \ + _Pragma ("GCC diagnostic push"); \ + _Pragma ("GCC diagnostic ignored \"-Wpointer-to-int-cast\""); \ + (__builtin_classify_type (X) == 5 \ + ? (uintptr_t) (X) : (int64_t) (X)); \ + _Pragma ("GCC diagnostic pop"); \ + }) +#endif /* __ASSEMBLER__ */ + #endif /* linux/x86_64/x32/sysdep.h */ -- 2.26.2