From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 0A67B3858C53 for ; Mon, 17 Apr 2023 15:35:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0A67B3858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681745710; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5X2KL2tKkhoaWEEFWzsMf3m66a6RCp341/NVUvlvNYo=; b=aOOS+7YMuGvIJahOUZVwl8P7UVHnhW8DFMnsVCEfNgDclVhuYdek33KJpiOjf7G34yOwfK w2WVUgxHA13y6CNi/WFvEaNLdw82jX/OwoQHA7eFht4YvLpiMZ+gxjhru5HO6hflN1LxwQ zojaGEsmhOgH2R4k5I89VMbSZQQPf5E= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-266-Mjv9B9enO82V7mlrBkV2Pw-1; Mon, 17 Apr 2023 11:35:09 -0400 X-MC-Unique: Mjv9B9enO82V7mlrBkV2Pw-1 Received: by mail-qv1-f70.google.com with SMTP id f17-20020a0cbed1000000b005d70160fbb0so11236648qvj.21 for ; Mon, 17 Apr 2023 08:35:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681745708; x=1684337708; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5X2KL2tKkhoaWEEFWzsMf3m66a6RCp341/NVUvlvNYo=; b=ZWGRbZNVJyXNDOJK8lmGaHBfaHrZyOz8cEXhc+A75r3nobzf6Hnuuw7MIyR40fW+G7 fSQU9wBdfy3rgg0A03dtnMpx81ZTtYDqgB/wmAZhv58GG0VA4CqlEJQYPPmqNMdRwt+c 21UfsYLw8HhiV4Tki4YGhLZzZ706Kb/3w3DekVzNenvlbXcHc1BPDTD54v0VvtV4hgwD XnAy9y1z0v6/y1Dp6r9tecoHp//L5HWaghhCQI4DEyTIfzydol3O3ydsoBFb6ZrEkrnJ 1zeYjVCRH/BCMI07mXhB+eG8V8wSH8zqPuLbn58EjSbKjnKu+DS/EXIE7pSH+7RuHvuD Qjfg== X-Gm-Message-State: AAQBX9cbz1baOA+uUURz3PQ250HW6u3+Dg5d4cVC03b5UXcp7buVBuKQ A+ldeQzarI29hpi3ji3W7AH1f4RsSQgRQu9qM6McHE6hJOHqqd+y20SFzeaLInVYM8fJrsNMroV vg0kaDevdlOY7Ho/lSiPANt4PsHaiE0eaucaO3t/hLiV9EvYL073PNm1K5lRpIspao03Ec1yMmS 05ic5/ X-Received: by 2002:a05:622a:28e:b0:3ef:2bae:d1e5 with SMTP id z14-20020a05622a028e00b003ef2baed1e5mr6339680qtw.24.1681745707919; Mon, 17 Apr 2023 08:35:07 -0700 (PDT) X-Google-Smtp-Source: AKy350aqQfaUf5V32EUfp4TPZkxDWK1UKaUAkc3CjLiAeJ/l1utsMJuRK9VHfOCEreBu5EAi9FZn6w== X-Received: by 2002:a05:622a:28e:b0:3ef:2bae:d1e5 with SMTP id z14-20020a05622a028e00b003ef2baed1e5mr6339643qtw.24.1681745707556; Mon, 17 Apr 2023 08:35:07 -0700 (PDT) Received: from oak.redhat.com (c-71-206-142-238.hsd1.va.comcast.net. [71.206.142.238]) by smtp.gmail.com with ESMTPSA id f20-20020a05620a20d400b0074d3233487dsm1577107qka.114.2023.04.17.08.35.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 08:35:06 -0700 (PDT) From: Joe Simmons-Talbott To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott Subject: [PATCH v3 1/2] x86_64: Set the syscall register right before doing the syscall. Date: Mon, 17 Apr 2023 11:34:50 -0400 Message-Id: <20230417153451.1450817-2-josimmon@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230417153451.1450817-1-josimmon@redhat.com> References: <20230411133004.2268170-1-josimmon@redhat.com> <20230417153451.1450817-1-josimmon@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: To make identifying syscalls easier during call tree analysis load the syscall number just before performing the syscall. Compiler optimizations can place quite a few instructions between the setting of the syscall number and the syscall instruction. During call tree analysis the number of instructions between the two can lead to more difficulty for both tools and humans in properly identifying the syscall number. Having the syscall number set in the prior instruction to the syscall instruction makes this task easier and less error prone. Being able to reliably identify syscalls made by a given API will make it easier to understand and verify the safety and security of glibc. --- sysdeps/unix/sysv/linux/x86_64/sysdep.h | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/sysdeps/unix/sysv/linux/x86_64/sysdep.h b/sysdeps/unix/sysv/linux/x86_64/sysdep.h index cfb51be8c5..800a56723f 100644 --- a/sysdeps/unix/sysv/linux/x86_64/sysdep.h +++ b/sysdeps/unix/sysv/linux/x86_64/sysdep.h @@ -250,12 +250,20 @@ (long int) resultvar; \ }) +#define MSTR_HELPER(x) #x +#define MSTR(x) MSTR_HELPER(x) + #undef internal_syscall1 #define internal_syscall1(number, arg1) \ ({ \ unsigned long int resultvar; \ TYPEFY (arg1, __arg1) = ARGIFY (arg1); \ register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ + if (__builtin_constant_p(number)) \ + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ + : /* no outputs */ \ + : "i" (number) \ + : "eax"); \ asm volatile ( \ "syscall\n\t" \ : "=a" (resultvar) \ @@ -272,6 +280,11 @@ TYPEFY (arg1, __arg1) = ARGIFY (arg1); \ register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ + if (__builtin_constant_p(number)) \ + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ + : /* no outputs */ \ + : "i" (number) \ + : "eax"); \ asm volatile ( \ "syscall\n\t" \ : "=a" (resultvar) \ @@ -290,6 +303,11 @@ register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ + if (__builtin_constant_p(number)) \ + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ + : /* no outputs */ \ + : "i" (number) \ + : "eax"); \ asm volatile ( \ "syscall\n\t" \ : "=a" (resultvar) \ @@ -310,6 +328,11 @@ register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ + if (__builtin_constant_p(number)) \ + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ + : /* no outputs */ \ + : "i" (number) \ + : "eax"); \ asm volatile ( \ "syscall\n\t" \ : "=a" (resultvar) \ @@ -332,6 +355,11 @@ register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ + if (__builtin_constant_p(number)) \ + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ + : /* no outputs */ \ + : "i" (number) \ + : "eax"); \ asm volatile ( \ "syscall\n\t" \ : "=a" (resultvar) \ @@ -357,6 +385,11 @@ register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \ register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \ register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \ + if (__builtin_constant_p(number)) \ + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ + : /* no outputs */ \ + : "i" (number) \ + : "eax"); \ asm volatile ( \ "syscall\n\t" \ : "=a" (resultvar) \ -- 2.39.2