NetBSD Problem Report #57158

From www@netbsd.org  Wed Jan  4 10:52:28 2023
Return-Path: <www@netbsd.org>
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 9B90B1A9239
	for <gnats-bugs@gnats.NetBSD.org>; Wed,  4 Jan 2023 10:52:28 +0000 (UTC)
Message-Id: <20230104105227.2102D1A923A@mollari.NetBSD.org>
Date: Wed,  4 Jan 2023 10:52:27 +0000 (UTC)
From: michael@emte.net.au
Reply-To: michael@emte.net.au
To: gnats-bugs@NetBSD.org
Subject: Erlang 25.2 fails to build in default pkg_comp sandbox that does not mount /var/shm
X-Send-Pr-Version: www-1.0

>Number:         57158
>Category:       pkg
>Synopsis:       Erlang 25.2 fails to build in default pkg_comp sandbox that does not mount /var/shm
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    pkg-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Jan 04 10:55:01 +0000 2023
>Last-Modified:  Wed Jan 04 21:30:02 +0000 2023
>Originator:     Michael Taylor
>Release:        NetBSD 9.2, pkgsrc HEAD 2023-01-04
>Organization:
>Environment:
NetBSD lenovo.home.lan 9.2 NetBSD 9.2 (GENERIC) #0: Wed May 11 16:53:21 AEST 2022  met@lenovo.home.lan:/usr/src/sys/arch/amd64/compile/obj/GENERIC amd64
>Description:
Erlang 24 introduced a JIT-compiler and uses that compiler during it's build process. The JIT-compiler implementation on NetBSD uses shm_open [1] & [2].

For erlang to build it needs a working shm_open setup, namely a correctly mounted /var/shm mount. This condition is not met by default in the sandbox used by pkg_comp [3].

A problem report is being submitted here rather than just with pkg_comp since this may affect other packages using shm_open during their build, and in other chroot based build systems. I assume this is the reason why the most recent binary packages [4] are all pre Erlang 24, because the build server is using a chroot based build system with /var/shm not mounted.

I am not sure where any fix/changes should be made but I will start with raising an issue with pkg_comp [5] to make /var/shm mounting an option rather than a custom hook.

[1] https://man.netbsd.org/NetBSD-9.2/shm_open.3
[2] https://github.com/erlang/otp/blob/OTP-25.2/erts/emulator/asmjit/core/virtmem.cpp#L353
[3] https://cdn.netbsd.org/pub/pkgsrc/current/pkgsrc/pkgtools/pkg_comp/index.html
[4] https://cdn.netbsd.org/pub/pkgsrc/current/pkgsrc/lang/erlang/index.html
[5] https://github.com/jmmv/pkg_comp/
>How-To-Repeat:
Build the lang/erlang (25.2) package inside a chroot that does not have /var/shm mounted. In my case that was running:

  # pkg_comp auto erlang

The ERLC steps will fail with the following error

---
beam/jit/beam_jit_main.cpp:177:pick_allocator(): Internal error: jit: Cannot allocate executable memory. Use the interpreter instead.
---

The core error is shm_open returning errno ENOTSUP since /var/shm is not mounted.

I used the following test to check when shm_open was working inside my chroot (linked with -lrt):

---
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

void main(void) {
    if (shm_open("/shm-test", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR) >= 0) {
        printf("OK\n");
    } else {
        printf("ERROR %s\n", strerror(errno));
    }
}
---
>Fix:
I have successfully built erlang-25.2 with pkg_comp by mounting /var/shm inside pkg_comp's pkg_comp_post_mount_hook.

My complete sandbox.conf is now:

---
SANDBOX_TYPE=netbsd-release
SANDBOX_ROOT=/var/pkg_comp/sandbox
NETBSD_RELEASE_RELEASEDIR="/usr/obj/releasedir/amd64"

pkg_comp_post_mount_hook() {
        mount -t tmpfs -o rw -o -m1777 -o -sram%10 tmpfs /var/pkg_comp/sandbox/var/shm
        true
}
---

>Audit-Trail:
From: "David H. Gutteridge" <david@gutteridge.ca>
To: Gnats Bugs <gnats-bugs@netbsd.org>
Cc: 
Subject: Re: pkg/57158: Erlang 25.2 fails to build in default pkg_comp sandbox
 that does not mount /var/shm
Date: Wed, 04 Jan 2023 16:26:37 -0500

 There's more than one (competing, so to speak) package for helping
 create sandboxed builds, so pkgtools/mksandbox would also need
 consideration here.

 Some official build environments didn't have /var/shm mounted as of a
 year or so ago. I'm not sure what their state is these days. (At the
 time, I'd worked around that differently with www/unit, which simply
 tests if /var/shm is usable at build time, but has no actual need to
 employ it during a build.)

 Dave

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.