NetBSD Problem Report #56771

From martin@duskware.de  Sat Mar 26 17:46:56 2022
Return-Path: <martin@duskware.de>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id D6B141A9239
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 26 Mar 2022 17:46:55 +0000 (UTC)
From: martin@NetBSD.org
Reply-To: martin@NetBSD.org
To: gnats-bugs@NetBSD.org
Subject: profiled programs crash in __mcount
X-Send-Pr-Version: 3.95

>Number:         56771
>Category:       toolchain
>Synopsis:       profiled programs crash in __mcount
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    toolchain-manager
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Mar 26 17:50:00 +0000 2022
>Closed-Date:    Fri May 06 04:52:59 +0000 2022
>Last-Modified:  Fri May 06 04:52:59 +0000 2022
>Originator:     Martin Husemann
>Release:        NetBSD 9.99.95
>Organization:
The NetBSD Foundation, Inc.
>Environment:
System: NetBSD thirdstage.duskware.de 9.99.95 NetBSD 9.99.95 (MODULAR) #536: Sat Mar 26 09:19:27 CET 2022 martin@thirdstage.duskware.de:/usr/src/sys/arch/sparc64/compile/MODULAR sparc64
Architecture: sparc64
Machine: sparc64
>Description:

On sparc64 PIE programs compiled with profiling crash in mcount.
Example from the usr.bin/c++/t_call_once test:

> cat test.cc 
#include <cstdio>
#include <thread>
#include <mutex>
std::once_flag flag;
int main(void) {
        std::call_once(flag, [](){ printf("hello, world!\n"); });
        return 0;
}
> c++ -pg -o call_once test.cc -pthread
gdb ./call_once
(gdb) run
Starting program: /tmp/call_once 

Program received signal SIGBUS, Bus error.
__mcount (frompc=<optimized out>, selfpc=0)
    at /usr/src/lib/libc/../../common/lib/libc/gmon/mcount.c:204
204                     top->selfpc = selfpc;


>How-To-Repeat:
s/a

>Fix:
n/a

>Release-Note:

>Audit-Trail:
From: Martin Husemann <martin@duskware.de>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: toolchain/56771: profiled programs crash in __mcount
Date: Mon, 28 Mar 2022 14:37:51 +0200

 Rumours say that we need -pg to go with -static, but that does not fix
 the crash here - but gives a more interesting backtrace:

  > c++ -pg -static -g -o call_once test.cc -pthread
  > ./call_once
  > gdb call_once call_once.core 
 Core was generated by `call_once'.
 Program terminated with signal SIGBUS, Bus error.
 #0  __mcount (frompc=<optimized out>, selfpc=1252584)
     at /usr/src/lib/libc/../../common/lib/libc/gmon/mcount.c:204
 204                     top->selfpc = selfpc;
 (gdb) bt
 #0  __mcount (frompc=<optimized out>, selfpc=1252584)
     at /usr/src/lib/libc/../../common/lib/libc/gmon/mcount.c:204
 #1  0x0000000000131ce8 in atexit ()
 #2  0x000000000010064c in ___start ()


 Martin

From: Rin Okuyama <rokuyama.rk@gmail.com>
To: gnats-bugs@netbsd.org, toolchain-manager@netbsd.org,
 gnats-admin@netbsd.org, netbsd-bugs@netbsd.org,
 Martin Husemann <martin@NetBSD.org>
Cc: 
Subject: Re: toolchain/56771: profiled programs crash in __mcount
Date: Sat, 30 Apr 2022 06:12:41 +0900

 Can you try

 https://gist.github.com/rokuyama/98b3d33222781337dc80e4e9a75da121

 in PR lib/56395?

 Thanks,
 rin

From: Martin Husemann <martin@duskware.de>
To: Rin Okuyama <rokuyama.rk@gmail.com>
Cc: gnats-bugs@netbsd.org
Subject: Re: toolchain/56771: profiled programs crash in __mcount
Date: Sat, 30 Apr 2022 16:15:17 +0200

 On Sat, Apr 30, 2022 at 06:12:41AM +0900, Rin Okuyama wrote:
 > Can you try
 > 
 > https://gist.github.com/rokuyama/98b3d33222781337dc80e4e9a75da121
 > 
 > in PR lib/56395?

 That patch fixes the issue for me.

 Martin

From: "Rin Okuyama" <rin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56771 CVS commit: src/lib/libc/gmon
Date: Fri, 6 May 2022 04:49:13 +0000

 Module Name:	src
 Committed By:	rin
 Date:		Fri May  6 04:49:13 UTC 2022

 Modified Files:
 	src/lib/libc/gmon: gmon.c

 Log Message:
 PR lib/56395
 PR toolchain/56771

 Fix profiling on CPUs that do not support unaligned memory access;
 Allocate objects (referenced from struct gmonparam) with proper
 alignments.

 For monstartup(), objects are allocated on heap. Break is not
 guaranteed to be aligned at all, unlike stack pointer.

 For _m_gmon_alloc(), objects are allocated on anonymous memory.
 p->tos is not aligned properly in general.

 This fixes quasi-random crashes for *_profile tests, at least on
 SH-4 and PowerPC 403 [1]. Also, no regression is observed for
 others as far as I can see.

 This change does not cause any ABI breakage, as long as application
 uses proper pointers; use p->tos instead of evil pointer arithmetic
 like (struct tostruct *)((char *)p->froms + p->fromssize) [2].

 [1] Timeout should be increased for some tests. "pic" variants
 still fail as expected. Dynamically-linked binaries also crash in
 rtld for SH-4, but this seems different problem...

 [2] This example did not work even before, since the order of
 froms[] and tos[] is reversed depending on which of monstartup() or
 _m_gmon_alloc() is used for allocation.


 To generate a diff of this commit:
 cvs rdiff -u -r1.36 -r1.37 src/lib/libc/gmon/gmon.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->closed
State-Changed-By: rin@NetBSD.org
State-Changed-When: Fri, 06 May 2022 04:52:59 +0000
State-Changed-Why:
Fixed. Thanks for quick response. Please feel free to end pull-up requests
if necessary.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.