Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perl misdetects architecture on FreeBSD #19791

Closed
pkubaj opened this issue May 30, 2022 · 7 comments · Fixed by #19843
Closed

perl misdetects architecture on FreeBSD #19791

pkubaj opened this issue May 30, 2022 · 7 comments · Fixed by #19843
Assignees

Comments

@pkubaj
Copy link

pkubaj commented May 30, 2022

Module:
POSIX

Description
POSIX.xs at https://github.com/Perl/perl5/blob/blead/ext/POSIX/POSIX.xs#L3350= uses uname(3) for detecting architecture on FreeBSD. However, uname -m will print "powerpc" on all powerpc architectures (powerpc, powerpcspe, powerpc64 and powerpc64le), "riscv" on riscv architectures (riscv64, riscv64sf) and "arm" on 32-bit arm (armv6, armv7). For uname binary, -p switch is available that will print a correct name, but it's name available in uname C function.
This is in particular wrong on 64-bit architectures, since bitness is misdetected.

Steps to Reproduce

root@powerpc64-131-default:~ # perl5.35.11  -v

This is perl 5, version 35, subversion 11 (v5.35.11 (66f51c878ffb217ed5ec7a4563fa1167ca9e7ca6)) built for **powerpc-freebsd-thread-multi**

Copyright 1987-2022, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.

The above is on powerpc64 host.

Expected behavior

root@powerpc64-131-default:~ # perl5.35.11  -v

This is perl 5, version 35, subversion 11 (v5.35.11 (66f51c878ffb217ed5ec7a4563fa1167ca9e7ca6)) built for **powerpc64-freebsd-thread-multi**

Copyright 1987-2022, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.

The correct way would be to use sysctl hw.machine_arch output, like this:

int mib[] = { CTL_HW, HW_MACHINE_ARCH };
static char buf[48];
size_t len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == 0)
return buf;

Perl configuration

root@powerpc64-131-default:~ # perl5.35.11 -V
Summary of my perl5 (revision 5 version 35 subversion 11) configuration:
  Snapshot of: 66f51c878ffb217ed5ec7a4563fa1167ca9e7ca6
  Platform:
    osname=freebsd
    osvers=13.1-release
    archname=powerpc-freebsd-thread-multi
    uname='freebsd powerpc64-131-default 13.1-release freebsd 13.1-release 1301000 powerpc '
    config_args='-Accflags=-DUSE_THREAD_SAFE_LOCALE -Darchlib=/usr/local/lib/perl5/5.35/mach -Dcc=cc -Dcf_by=mat -Dcf_email=mat@FreeBSD.org -Dcf_time=Mon Apr 11 06:56:21 UTC 2022 -Dinc_version_list=none -Dlibperl=libperl.so.5.35.10.86 -Dman1dir=/usr/local/lib/perl5/5.35/perl/man/man1 -Dman3dir=/usr/local/lib/perl5/5.35/perl/man/man3 -Dprefix=/usr/local -Dprivlib=/usr/local/lib/perl5/5.35 -Dscriptdir=/usr/local/bin -Dsitearch=/usr/local/lib/perl5/site_perl/mach/5.35 -Dsitelib=/usr/local/lib/perl5/site_perl -Dsiteman1dir=/usr/local/lib/perl5/site_perl/man/man1 -Dsiteman3dir=/usr/local/lib/perl5/site_perl/man/man3 -Dusenm=n -Duseshrplib -sde -Ui_iconv -Ui_malloc -Uinstallusrbinperl -Alddlflags=-L/wrkdirs/usr/ports/lang/perl5-devel/work/perl5-5.35.10-86-g66f51c878f -L/usr/local/lib/perl5/5.35/mach/CORE -lperl -Dshrpldflags=$(LDDLFLAGS:N-L/wrkdirs/usr/ports/lang/perl5-devel/work/perl5-5.35.10-86-g66f51c878f:N-L/usr/local/lib/perl5/5.35/mach/CORE:N-lperl) -Wl,-soname,$(LIBPERL) -Dusedevel -Uversiononly -Doptimize=-O2 -pipe  -fstack-protector-strong -fno-strict-aliasing  -Dusedtrace -Ui_gdbm -Dusemultiplicity=y -Duse64bitint -Dusemymalloc=n -Dusethreads=y -Dversiononly'
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=define
    usemultiplicity=define
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='cc'
    ccflags ='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -DUSE_THREAD_SAFE_LOCALE -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    optimize='-O2 -pipe -fstack-protector-strong -fno-strict-aliasing '
    cppflags='-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -DUSE_THREAD_SAFE_LOCALE -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion=''
    gccversion='FreeBSD Clang 13.0.0 (git@github.com:llvm/llvm-project.git llvmorg-13.0.0-0-gd7b669b3a303)'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=87654321
    doublekind=4
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=8
    longdblkind=0
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags ='-pthread -Wl,-E  -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/lib /usr/local/lib /usr/lib/clang/13.0.0/lib
    libs=-lpthread -lm -lcrypt -lutil
    perllibs=-lpthread -lm -lcrypt -lutil
    libc=
    so=so
    useshrplib=true
    libperl=libperl.so.5.35.10.86
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='  -Wl,-R/usr/local/lib/perl5/5.35/mach/CORE'
    cccdlflags='-DPIC -fPIC'
    lddlflags='-shared  -L/usr/local/lib/perl5/5.35/mach/CORE -lperl -L/usr/local/lib -fstack-protector-strong'


Characteristics of this binary (from libperl): 
  Compile-time options:
    HAS_TIMES
    MULTIPLICITY
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    PERL_USE_DEVEL
    USE_64_BIT_ALL
    USE_64_BIT_INT
    USE_ITHREADS
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
    USE_REENTRANT_API
    USE_THREAD_SAFE_LOCALE
  Built under freebsd
  @INC:
    /usr/local/lib/perl5/site_perl/mach/5.35
    /usr/local/lib/perl5/site_perl
    /usr/local/lib/perl5/5.35/mach
    /usr/local/lib/perl5/5.35

@rwp0
Copy link
Contributor

rwp0 commented May 30, 2022

basically it says powerpc-freebsd-thread-multi instead of powerpc64-freebsd-thread-multi

tonycoz added a commit to tonycoz/perl5 that referenced this issue Jun 9, 2022
On FreeBSD C<uname -m> produces "powerpc" for all of 32-bit power pc, 32-powerpc little endian,
64-bit powerpc and 64-bit powerpc little endian, which means we produce the same archname
for all 4 of those incompatible architectures.

To avoid that, if no -Darchname has been supplied, the FreeBSD hints now creates an archname.cbu
to postprocess archname replacing a leading "`uname -m`-" with "`uname -p`-" which does
distinguish between the different architectures.

This change isn't suitable for all non-FreeBSD systems, on Linux at least, C<uname -p> is
"unknown" on my x86_64 machine.

Fixes Perl#19791
@jkeenan
Copy link
Contributor

jkeenan commented Jun 9, 2022

In order to evaluate Tony's p.r. on this, I'm trying to wrap my head around the original complaint here. Is the misdetection found solely on powerpc, riscv and arm architectures? Is there any current problem with amd64 architectures?

Here, for example, is output from the FreeBSD machine I use every day:

$ perl -v | head -2 | tail -1
This is perl 5, version 32, subversion 1 (v5.32.1) built for amd64-freebsd-thread-multi

Is there any thing wrong with that?

@mat813
Copy link
Contributor

mat813 commented Jun 11, 2022

On arm it seems like it is ok:

$ perl -v
This is perl 5, version 32, subversion 1 (v5.32.1) built for arm64-freebsd-thread-multi

@pkubaj
Copy link
Author

pkubaj commented Jun 13, 2022

It looks ok, because you check on arm64. On arm64, hw.machine is arm64, but the hw.machine_arch is aarch64, which is what should be displayed. It's just that both mean the same on 64-bit ARM.
If you check on 32-bits, arm will be displayed in place of armv6 or armv7. While the more detailed naming would be preferred, having it simply display arm is also ok (just not as good as more specific naming).

The situation is worse in POWER / RISC-V land.
Having powerpc displayed everywhere may confuse bitness on 64-bit host. On powerpc64le, some software may also confuse endianness (since 32-bit POWER is strictly big-endian). OpenSSL`s configuration script does exactly that.

It's similar with RISC-V. hw.machine has just riscv. FreeBSD doesn't even support 32-bit RISC-V.

It's not an issue on amd64 and i386, because hw.machine and hw.machine_arch are the same on both (amd64 for amd64 and i386 for i386). So the incorrect checking doesn't really affect those.

@tonycoz
Copy link
Contributor

tonycoz commented Jun 14, 2022

If you check on 32-bits, arm will be displayed in place of armv6 or armv7. While the more detailed naming would be preferred, having it simply display arm is also ok (just not as good as more specific naming).

If you can supply a better PR that provides better results I'm more than happy to drop this one.

I don't have ready access to FreeBSD on anything but an amd64 VM, and the painfully slow powerpc emulation.

jkeenan pushed a commit that referenced this issue Jul 9, 2022
On FreeBSD C<uname -m> produces "powerpc" for all of 32-bit power pc, 32-powerpc little endian,
64-bit powerpc and 64-bit powerpc little endian, which means we produce the same archname
for all 4 of those incompatible architectures.

To avoid that, if no -Darchname has been supplied, the FreeBSD hints now creates an archname.cbu
to postprocess archname replacing a leading "`uname -m`-" with "`uname -p`-" which does
distinguish between the different architectures.

This change isn't suitable for all non-FreeBSD systems, on Linux at least, C<uname -p> is
"unknown" on my x86_64 machine.

Fixes #19791
@jkeenan
Copy link
Contributor

jkeenan commented Jul 9, 2022

Re-opening and self-assigning for the purpose of monitoring for a week or two.

@jkeenan jkeenan reopened this Jul 9, 2022
@jkeenan jkeenan self-assigned this Jul 9, 2022
@jkeenan
Copy link
Contributor

jkeenan commented Jul 26, 2022

Re-opening and self-assigning for the purpose of monitoring for a week or two.

Re-closing as per schedule.

@jkeenan jkeenan closed this as completed Jul 26, 2022
scottchiefbaker pushed a commit to scottchiefbaker/perl5 that referenced this issue Nov 3, 2022
On FreeBSD C<uname -m> produces "powerpc" for all of 32-bit power pc, 32-powerpc little endian,
64-bit powerpc and 64-bit powerpc little endian, which means we produce the same archname
for all 4 of those incompatible architectures.

To avoid that, if no -Darchname has been supplied, the FreeBSD hints now creates an archname.cbu
to postprocess archname replacing a leading "`uname -m`-" with "`uname -p`-" which does
distinguish between the different architectures.

This change isn't suitable for all non-FreeBSD systems, on Linux at least, C<uname -p> is
"unknown" on my x86_64 machine.

Fixes Perl#19791
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants