From owner-freebsd-hackers@freebsd.org Thu Dec 13 06:01:15 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id AE553130A77F for ; Thu, 13 Dec 2018 06:01:15 +0000 (UTC) (envelope-from cse.cem@gmail.com) Received: from mail-it1-f194.google.com (mail-it1-f194.google.com [209.85.166.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B871D6F4C8 for ; Thu, 13 Dec 2018 06:01:14 +0000 (UTC) (envelope-from cse.cem@gmail.com) Received: by mail-it1-f194.google.com with SMTP id a6so2138648itl.4 for ; Wed, 12 Dec 2018 22:01:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to:cc; bh=Q4QSXzafRS0EavJN6NDA4nZFBkss6/74FekA/NzOsAQ=; b=tQnpDBSVsK7NmTudy+A6UF0EhzGUSoqy0Sxv5XE36oq7pAXwi22/NXKiLpvRTLqssN 5JMkp0pNHn9wus9CRX6TOWoPzo+L1LwFrx+cFwPDRATr6Evhoq2gXyV84+sQp/nbPY2T rMdZxrja+JELaEaZTzwI9V5bKRR4lLJ5EG9c4uMcoPW3qOfdRv0KurQWGIYVDeMzwc0T jZGMHEMCqsxu2lq5pQLqei+FP+I83uCdMRIeMzj+svYQ31J4Qfbe3Lnc+D1DvHQbXgV1 MOfEN8uGWvYMp8UNOS5HgqKcYkMje4AuJFqtiKxM6LN1YqJwwRT52tZmgTeanQ7Vf1Vo P9bg== X-Gm-Message-State: AA+aEWbXrqWhsSssLQYOB5Fmtq346VdkpKyvAZX9YMRbKr7Vw/QD54WZ 0aeL7TiHRm/W2H4b1dlcy+44MhJ5 X-Google-Smtp-Source: AFSGD/XRE1+TLYni9K2OMjSyhB+thGSbYNAhzblas3HFJH5ckJz8sR6QvqTBblhuB9zfvyEJgUPSIQ== X-Received: by 2002:a05:660c:fd2:: with SMTP id m18mr8647172itn.1.1544680868005; Wed, 12 Dec 2018 22:01:08 -0800 (PST) Received: from mail-it1-f171.google.com (mail-it1-f171.google.com. [209.85.166.171]) by smtp.gmail.com with ESMTPSA id v202sm818744ita.13.2018.12.12.22.01.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Dec 2018 22:01:07 -0800 (PST) Received: by mail-it1-f171.google.com with SMTP id z7so2091273iti.0 for ; Wed, 12 Dec 2018 22:01:07 -0800 (PST) X-Received: by 2002:a24:5411:: with SMTP id t17mr8175337ita.32.1544680867147; Wed, 12 Dec 2018 22:01:07 -0800 (PST) MIME-Version: 1.0 References: <70c7214e-3619-115d-abca-09853a1729a6@ziglang.org> In-Reply-To: <70c7214e-3619-115d-abca-09853a1729a6@ziglang.org> Reply-To: cem@freebsd.org From: Conrad Meyer Date: Wed, 12 Dec 2018 22:00:56 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: raise() implementation in freebsd libc vs musl libc To: andrew@ziglang.org Cc: "freebsd-hackers@freebsd.org" Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: B871D6F4C8 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; spf=pass (mx1.freebsd.org: domain of csecem@gmail.com designates 209.85.166.194 as permitted sender) smtp.mailfrom=csecem@gmail.com X-Spamd-Result: default: False [-2.95 / 15.00]; TO_DN_EQ_ADDR_SOME(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; HAS_REPLYTO(0.00)[cem@freebsd.org]; R_SPF_ALLOW(-0.20)[+ip4:209.85.128.0/17]; REPLYTO_ADDR_EQ_FROM(0.00)[]; RCVD_COUNT_THREE(0.00)[4]; MX_GOOD(-0.01)[cached: alt3.gmail-smtp-in.l.google.com]; RCPT_COUNT_TWO(0.00)[2]; NEURAL_HAM_SHORT(-0.97)[-0.968,0]; FORGED_SENDER(0.30)[cem@freebsd.org,csecem@gmail.com]; IP_SCORE(-0.99)[ipnet: 209.85.128.0/17(-3.55), asn: 15169(-1.30), country: US(-0.09)]; R_DKIM_NA(0.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:209.85.128.0/17, country:US]; TAGGED_FROM(0.00)[]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.99)[-0.988,0]; FROM_NEQ_ENVFROM(0.00)[cem@freebsd.org,csecem@gmail.com]; FROM_HAS_DN(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; DMARC_NA(0.00)[freebsd.org]; MIME_TRACE(0.00)[0:+]; TO_MATCH_ENVRCPT_SOME(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[194.166.85.209.list.dnswl.org : 127.0.5.0]; RCVD_TLS_LAST(0.00)[]; RWL_MAILSPIKE_POSSIBLE(0.00)[194.166.85.209.rep.mailspike.net : 127.0.0.17] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Dec 2018 06:01:15 -0000 Hi Andrew, The musl signal blocking dates to this commit: https://git.musl-libc.org/cgit/musl/commit/?id=0bed7e0acfd34e3fb63ca0e4d99b7592571355a9 The concern raised in that commit was that raise(3) could theoretically be interrupted by a concurrently running signal handler which invokes fork() (also sighandler-safe), resulting in inconsistent/stale values from gettid(2)/getpid(2) on Linux (at the time). On both systems, gettid(2) / thr_self(2) returns a unique thread identifier that cannot be reused until the thread it identifies has exited. So, I don't know. I guess if fork happens between thr_self() and thr_kill(), the parent process may have already exited and had its tid recycled by the time the child process invokes thr_kill(). OTOH, that seems like a pretty byzantine / broken application? I'm not sure it's libc's job to prevent applications from shooting themselves in the foot. Forking in a signal handler is already somewhat dicey, and especially so if the child does not immediately exec() or _exit(2). Anyway, that's just my guess. I am not a libthr expert on either BSD nor Linux, so take this with a big grain of salt. Signals were a mistake ;-). Best, Conrad P.S., Zig looks quite promising, I am excited to see where it goes. On Wed, Dec 12, 2018 at 9:12 PM Andrew Kelley wrote: > > Howdy, > > I noticed that musl-libc blocks signals in raise() to prevent a race > condition, but freebsd libc does not. is there a reason it's necessary > on linux and not freebsd? > > musl > int raise(int sig) > { > sigset_t set; > __block_app_sigs(&set); > int ret = syscall(SYS_tkill, __pthread_self()->tid, sig); > __restore_sigs(&set); > return ret; > } > > freebsd > int > __raise(int s) > { > long id; > > if (__sys_thr_self(&id) == -1) > return (-1); > return (__sys_thr_kill(id, s)); > } > > Regards, > Andrew >