From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by sourceware.org (Postfix) with ESMTPS id E07853858C50 for ; Mon, 17 Apr 2023 15:54:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E07853858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yb1-xb2a.google.com with SMTP id l5so5583388ybe.7 for ; Mon, 17 Apr 2023 08:54:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681746893; x=1684338893; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=6afDOwc6tMpjkRRCPNMG0zUuTbgHX28hUs0YMQd3nxw=; b=SI7z5tEILn8dYIp29OVLze8eDNz+7sXJt+kUepjIkZHUDka+XCa+BozcqnwRGoJ97k AVnezcK0g9+5d1P8O4RJnxH0r2zPVxCh/8CEV5jD9EhWxeJ50vf52ZTc2TeJbcXLNx0Y uPSULprcshDuDXur1ZIvO4vE7HYF6nn1DvhFVpo1pHfheWBJmphvcNTpZVOpUhU3tv+f TF7F5O3B2MmPTw2uX0yhLY3tSpsnvMOydKQTrbnKTtdfScxRyUj3lCs3eI+Wz8uJEUOF /O/MhqdT39SxP2LddenJyluVCpH2OEfHTYU3iTGgUBnBrgQtNNQpAODu6hs7AKrL8ymn rrsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681746893; x=1684338893; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6afDOwc6tMpjkRRCPNMG0zUuTbgHX28hUs0YMQd3nxw=; b=VQRDxQy+eqy8ZMAzmL4pAm6K0e8xV6ERnMyOtKQILzkH7FqjXNR74T4fDAm/B1xgZE hFWZmDOOR7qq0aHAkw+RK7Vf4sT66pqz5Ej9u2ltohZ5SZ+/JEdZ89lWvK6zibpTTh18 /ZCLGmE8tjddIhBElFZc9xY+X6Jo84o6/rsDgce9t9D3BIRzeC1hct+NPo77QU/RwcrR FVyhedG8rsnVxL30k2FdSr13lcjFsFcpvYi3gBoP4Xp54cmKHRvs4dThw0KArUDTeo5X UaiF6wjWoGzYI+XJlHkInZL2hu3srBqV/UjcKVnIJw4ZwNqyoiU8dgGh2Y028RHyrDxF g4nQ== X-Gm-Message-State: AAQBX9eybMjvLO5e0z8/OUEDKCeWvgtCNIxUiNRqhH9qYHLhSvfwnNkX gu9BIcSL16LJayhaNMkzarr6Xn7ImJi/pHA6ztI= X-Google-Smtp-Source: AKy350by/lOfe+BoXs8zFrUN7tn7AsthlE0N/POEl6DQbwv/r7q46pF7fpDBnrHH3/PtE4ogdbhQDKhgETkhlAmxNzQ= X-Received: by 2002:a25:d08d:0:b0:b8f:5492:852f with SMTP id h135-20020a25d08d000000b00b8f5492852fmr9584683ybg.2.1681746893280; Mon, 17 Apr 2023 08:54:53 -0700 (PDT) MIME-Version: 1.0 References: <20230411133004.2268170-1-josimmon@redhat.com> <20230417153451.1450817-1-josimmon@redhat.com> <20230417153451.1450817-2-josimmon@redhat.com> In-Reply-To: <20230417153451.1450817-2-josimmon@redhat.com> From: "H.J. Lu" Date: Mon, 17 Apr 2023 08:54:17 -0700 Message-ID: Subject: Re: [PATCH v3 1/2] x86_64: Set the syscall register right before doing the syscall. To: Joe Simmons-Talbott Cc: libc-alpha@sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3022.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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: On Mon, Apr 17, 2023 at 8:35=E2=80=AFAM Joe Simmons-Talbott via Libc-alpha wrote: > > 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) =3D ARGIFY (arg1); = \ > register TYPEFY (arg1, _a1) asm ("rdi") =3D __arg1; = \ > + if (__builtin_constant_p(number)) \ > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > + : /* no outputs */ \ > + : "i" (number) \ > + : "eax"); \ > asm volatile ( \ > "syscall\n\t" \ > : "=3Da" (resultvar) = \ > @@ -272,6 +280,11 @@ > TYPEFY (arg1, __arg1) =3D ARGIFY (arg1); = \ > register TYPEFY (arg2, _a2) asm ("rsi") =3D __arg2; = \ > register TYPEFY (arg1, _a1) asm ("rdi") =3D __arg1; = \ > + if (__builtin_constant_p(number)) \ > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > + : /* no outputs */ \ > + : "i" (number) \ > + : "eax"); \ > asm volatile ( \ > "syscall\n\t" \ > : "=3Da" (resultvar) = \ > @@ -290,6 +303,11 @@ > register TYPEFY (arg3, _a3) asm ("rdx") =3D __arg3; = \ > register TYPEFY (arg2, _a2) asm ("rsi") =3D __arg2; = \ > register TYPEFY (arg1, _a1) asm ("rdi") =3D __arg1; = \ > + if (__builtin_constant_p(number)) \ > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > + : /* no outputs */ \ > + : "i" (number) \ > + : "eax"); \ > asm volatile ( \ > "syscall\n\t" \ > : "=3Da" (resultvar) = \ > @@ -310,6 +328,11 @@ > register TYPEFY (arg3, _a3) asm ("rdx") =3D __arg3; = \ > register TYPEFY (arg2, _a2) asm ("rsi") =3D __arg2; = \ > register TYPEFY (arg1, _a1) asm ("rdi") =3D __arg1; = \ > + if (__builtin_constant_p(number)) \ > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > + : /* no outputs */ \ > + : "i" (number) \ > + : "eax"); \ > asm volatile ( \ > "syscall\n\t" \ > : "=3Da" (resultvar) = \ > @@ -332,6 +355,11 @@ > register TYPEFY (arg3, _a3) asm ("rdx") =3D __arg3; = \ > register TYPEFY (arg2, _a2) asm ("rsi") =3D __arg2; = \ > register TYPEFY (arg1, _a1) asm ("rdi") =3D __arg1; = \ > + if (__builtin_constant_p(number)) \ > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > + : /* no outputs */ \ > + : "i" (number) \ > + : "eax"); \ > asm volatile ( \ > "syscall\n\t" \ > : "=3Da" (resultvar) = \ > @@ -357,6 +385,11 @@ > register TYPEFY (arg3, _a3) asm ("rdx") =3D __arg3; = \ > register TYPEFY (arg2, _a2) asm ("rsi") =3D __arg2; = \ > register TYPEFY (arg1, _a1) asm ("rdi") =3D __arg1; = \ > + if (__builtin_constant_p(number)) \ > + asm volatile ("movl $" MSTR(number) ", %%eax\n\t" \ > + : /* no outputs */ \ > + : "i" (number) \ > + : "eax"); \ > asm volatile ( \ > "syscall\n\t" \ > : "=3Da" (resultvar) = \ > -- > 2.39.2 > Won't the compiler load EAX twice when number is a constant? --=20 H.J.