From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from magnesium.8pit.net (magnesium.8pit.net [45.76.88.171]) by sourceware.org (Postfix) with ESMTPS id C0D553858D39 for ; Sat, 20 Aug 2022 10:31:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C0D553858D39 Received: from localhost (p200300f5ff05aa009d746ddc22c1eda1.dip0.t-ipconnect.de [2003:f5:ff05:aa00:9d74:6ddc:22c1:eda1]) by magnesium.8pit.net (OpenSMTPD) with ESMTPSA id 1bdd8cc7 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:YES); Sat, 20 Aug 2022 12:31:32 +0200 (CEST) Date: Sat, 20 Aug 2022 12:31:27 +0200 To: Ian Lance Taylor Cc: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: [PATCH] libgo: Access to glibc-specific field in struct sigevent From: =?UTF-8?Q?S=C3=B6ren?= Tempel References: <3P4Q20UQY09GV.2ZECSRTOW6FRW@8pit.net> In-Reply-To: Message-Id: <2D7KNXY512R77.2ELCGV3AMTY0A@8pit.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.1 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 20 Aug 2022 10:31:37 -0000 Hi Ian, Thanks for your input! I am not familiar with Go runtime internals at all, but the patch below at least compiles for me. Let me know if this works for you too / if there is a better way to do this. The untyped uintptr_t seems to be necessary as otherwise I encountered errors about &sevp escaping to the heap. Sincerely, S=C3=B6ren diff --git a/libgo/go/runtime/os_linux.go b/libgo/go/runtime/os_linux.go index 96fb1788..6653d85e 100644 --- a/libgo/go/runtime/os_linux.go +++ b/libgo/go/runtime/os_linux.go @@ -22,6 +22,8 @@ type mOS struct { profileTimerValid uint32 } +func setProcID(uintptr, int32) + func getProcID() uint64 { return uint64(gettid()) } @@ -365,7 +367,7 @@ func setThreadCPUProfiler(hz int32) { var sevp _sigevent sevp.sigev_notify =3D _SIGEV_THREAD_ID sevp.sigev_signo =3D _SIGPROF - *((*int32)(unsafe.Pointer(&sevp._sigev_un))) =3D int32(mp.procid) + setProcID(uintptr(unsafe.Pointer(&sevp)), int32(mp.procid)) ret :=3D timer_create(_CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid) if ret !=3D 0 { // If we cannot create a timer for this M, leave profileTim= erValid false diff --git a/libgo/runtime/go-signal.c b/libgo/runtime/go-signal.c index 528d9b6d..347b24e2 100644 --- a/libgo/runtime/go-signal.c +++ b/libgo/runtime/go-signal.c @@ -183,6 +183,16 @@ setSigactionHandler(struct sigaction* sa, uintptr hand= ler) sa->sa_sigaction =3D (void*)(handler); } +void setProcID(uintptr_t, int32_t) + __asm__ (GOSYM_PREFIX "runtime.setProcID"); + +void +setProcID(uintptr_t ptr, int32_t v) +{ + struct sigevent *s =3D (void *)ptr; + s->sigev_notify_thread_id =3D v; +} + // C code to fetch values from the siginfo_t and ucontext_t pointers // passed to a signal handler. Ian Lance Taylor wrote: > On Fri, Aug 12, 2022 at 10:21 AM S=C3=B6ren Tempel wrote: > > > > This is one of the few remaining issues regarding musl compatibility fo= r > > gofrontend. Unfortunately, I am not sure how to best fix this one. Henc= e > > reporting it here. > > > > The setThreadCPUProfiler implementation in libgo/go/runtime/os_linux.go= > > contains the following code: > > > > var sevp _sigevent > > sevp.sigev_notify =3D _SIGEV_THREAD_ID > > sevp.sigev_signo =3D _SIGPROF > > *((*int32)(unsafe.Pointer(&sevp._sigev_un))) =3D int32(mp.proci= d) > > > > I am not entirely sure, but I assume the last line is supposed to acces= s > > the thread ID in struct sigevent. Unfortunately, it does so via the > > glibc-specific _sigev_un member which doesn't work on musl libc (the > > union is named __sev_fields on musl and hence causes a build failure). > > > > A portable way to access the thread ID in struct sigevent is via > > sigev_notify_thread_id which is documented in timer_create(2). This is = a > > macro provided in siginfo.h from linux-headers for glibc [1] and in > > signal.h for musl [2]. However, since it is macro it probably requires = a > > C wrapper function in order to be usable in libgo. Would be possible to= > > add that somehow to libgo or is there an alternative feasible solution > > for this issue? >=20 > I think you're right that a tiny C function is the way to go here. >=20 > Ian