From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?=C3=93scar?= Fuentes Newsgroups: gmane.emacs.bugs Subject: bug#73444: 30.0.50; mingw-w64: Emacs uses CRT's `read` when _FORTIFY_SOURCE > 0 Date: Tue, 24 Sep 2024 14:55:56 +0200 Message-ID: <87ed59xlb7.fsf@telefonica.net> References: <87r09axbio.fsf@telefonica.net> <86setpe0gm.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10117"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 73444@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Sep 24 14:56:56 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1st568-0002Th-I6 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 24 Sep 2024 14:56:56 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1st55w-00074w-Tv; Tue, 24 Sep 2024 08:56:45 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1st55r-0006cj-K6 for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 08:56:39 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1st55q-0007av-Nf for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 08:56:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=V+4j5H/1Omz/QArCzarTqZvVRYzvgTmhWe7YzmUTmC4=; b=pHM1oYDay5NbPXdT9B8PtBDLg+C0FPqEg+hToGF14eJnAH3O3MXmL+zboyc4RgaTqxjPGj0dMK63ISivQtAcIgaL20AITiMAeyTj/s1uBfnZoombznS+NwJFk09pHnQmiXCq18a9XEYmz9Mj90nJXXA8TqicsAWMMsA0DUWPUiovMZghxlsnR/JS/SUfZi9hLL/xtg5TuhR+ID6XyODK2QyCPLs3bUzYZ6EGFvTrdST++2SwkNkXI7e6gnKzLrtg8ubHQg8STb8kLB6/3FqcJeDAyyslf0YiIGvNsuht0s7eKL121gLtjZVo1c9HpjaQaDhfqasxUXkDf+l8GhS1og==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1st56E-0007tB-Lm for bug-gnu-emacs@gnu.org; Tue, 24 Sep 2024 08:57:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?=C3=93scar?= Fuentes Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 24 Sep 2024 12:57:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 73444 X-GNU-PR-Package: emacs Original-Received: via spool by 73444-submit@debbugs.gnu.org id=B73444.172718259530285 (code B ref 73444); Tue, 24 Sep 2024 12:57:02 +0000 Original-Received: (at 73444) by debbugs.gnu.org; 24 Sep 2024 12:56:35 +0000 Original-Received: from localhost ([127.0.0.1]:45539 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1st55l-0007sO-Q7 for submit@debbugs.gnu.org; Tue, 24 Sep 2024 08:56:34 -0400 Original-Received: from relayout03.e.movistar.es ([86.109.101.203]:25505 helo=relayout03-redir.e.movistar.es) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1st55g-0007s3-95 for 73444@debbugs.gnu.org; Tue, 24 Sep 2024 08:56:32 -0400 Original-Received: from sky (132.red-81-39-22.dynamicip.rima-tde.net [81.39.22.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: 981711563@telefonica.net) by relayout03.e.movistar.es (Postfix) with ESMTPSA id 4XCfx940NgzMmb9; Tue, 24 Sep 2024 14:55:56 +0200 (CEST) In-Reply-To: <86setpe0gm.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 24 Sep 2024 14:48:57 +0300") X-TnetOut-Country: IP: 81.39.22.132 | Country: ES X-TnetOut-Information: AntiSPAM and AntiVIRUS on relayout03 X-TnetOut-MsgID: 4XCfx940NgzMmb9.A71B5 X-TnetOut-SpamCheck: no es spam, clean X-TnetOut-From: ofv@wanadoo.es X-TnetOut-Watermark: 1727787358.1044@owNSoYU+7cxvg9fMRdOQyw X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:292330 Archived-At: Eli Zaretskii writes: >> From: =C3=93scar Fuentes >> Date: Tue, 24 Sep 2024 00:15:11 +0200 >>=20 >>=20 >> On Onssee >> When the macro _FORTIFY_SOURCE > 0, mingw-64 provides an inline >> definition of `read` on io.h: >>=20 >> __mingw_bos_extern_ovr >> int read(int __fh, void * __dst, unsigned int __n) >> { >> return _read(__fh, __dst, __n); >> } > > Isn't that a bug in MinGW64's io.h? They should have used > > __mingw_bos_extern_ovr > int (read)(int __fh, void * __dst, unsigned int __n) > { > return _read(__fh, __dst, __n); > } > > Then we could modify the macro slightly as follows: > > #define read(h,d,n) sys_read(h,d,n) > > and avoid the problem. The above is how you protect your functions > from being interpreted as macro invocations. Well, AFAIK we are not supposed to #define names on the CRT namespace. > But I guess this is water under the bridge now? Yep, so discussing this is moot. >> A hack that avoids this consists on doing something like: >>=20 >> #define read dummy_read >> // etc >> #include >> // etc >> #undef read >> #define read sys_read >> int sys_read (int, char *, unsigned int); > > This indeed needs the prototype for sys_read, which is less desirable, > because we lose the ability to have the prototype exactly match io.h > without knowing what's in io.h. But I guess there's no better way, > sigh... > >> or simpler but untested: >>=20 >> #define _read sys_read >> // etc >> #include >> // etc > > That's simply wrong: we do NOT want to replace the Microsoft '_read', > we want to replace the Posix 'read' where it is used by Emacs. Ok. >> Either way it is necessary to condition the hack on the value of >> _FORTIFY_SOURCE. > > We could do that unconditionally, no? > > Does the MinGW64 build with _FORTIFY_SOURCE work, after taking > care of that? I tested that Emacs/MinGW64 + _FORTIFY_SOURCE works with the #define read dummy_read hack. Once we put the prototype for sys_read on ms-w32.h, maybe there is no need to put a conditional on _FORTIFY_SOURCE as well. I can check that. >> More generally, the way Emacs/NT overrides the CRT functions is >> susceptible to break anytime upstream does something like, this case, >> adding an inline definition, or some other unanticipated change. AFAIK >> the C standard says that precisely what Emacs is doing incurs on >> undefined behavior. >>=20 >> Any ideas about how to future-proof the solution for this problem? > > Not elegant ones, no. We are redirecting Posix functions to our > implementations where Emacs expects them to do something the MS > runtime doesn't, and we don't want to reproduce all the stuff in the > system headers that is related to those functions, including specific > data types, symbols, etc. > >> BTW, the initial bug report for this was in March 2023 and only today >> was succesfully analyzed (1) This gives an idea of how problematic this >> practice of redefining standard functions can be. > > Trying to make Emacs work well on MS-Windows is problematic in itself, > so we shouldn't be surprised it uses some "unconventional" techniques. Indeed. I was hoping for a trick from some of you C wizards. So, ok to install the workaround? On which branch? 1 file changed, 6 insertions(+), 1 deletion(-) nt/inc/ms-w32.h | 7 ++++++- modified nt/inc/ms-w32.h @@ -257,7 +257,7 @@ extern void w32_reset_stack_overflow_guard (void); #define link sys_link #define localtime sys_localtime #undef read -#define read sys_read +#define read unwanted_read // Override the CRT read, see #73444 #define rename sys_rename #define rmdir sys_rmdir #define select sys_select @@ -380,6 +380,11 @@ extern struct tm *localtime_r (time_t const * restrict= , struct tm * restrict); #define fileno _fileno #endif =20 +// Here we override CRT read with our own, see #73444 +#undef read +#define read sys_read +int sys_read (int, char *, unsigned int); + /* Defines that we need that aren't in the standard signal.h. */ #define SIGHUP 1 /* Hang up */ #define SIGQUIT 3 /* Quit process */