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

Segfault when building with gcc 7 #60

Open
shlevy opened this issue Feb 25, 2018 · 13 comments
Open

Segfault when building with gcc 7 #60

shlevy opened this issue Feb 25, 2018 · 13 comments

Comments

@shlevy
Copy link

shlevy commented Feb 25, 2018

We recently switched nixpkgs to gcc 7, and now our perl cross-builds fail with:

./miniperl_top make_patchnum.pl
Attempt to free unreferenced scalar: SV 0x7c38a0.
Attempt to free unreferenced scalar: SV 0x7a4338 at /build/perl-5.24.3/lib/strict.pm line 38.
make: *** [Makefile:202: lib/Config_git.pl] Segmentation fault

@shlevy
Copy link
Author

shlevy commented Feb 25, 2018

Note that switching back to gcc 6 for miniperl itself fixes it

@arsv
Copy link
Owner

arsv commented Feb 25, 2018

Can you post xconfig.sh and maybe also config.log from this build somewhere?

No segfaults for me on x86_64 host with gcc 7.2.1, and valgrind reports nothing. make_patchnum.pl is the first script it runs with miniperl and that line must be the first attempt to free anything.

@shlevy
Copy link
Author

shlevy commented Feb 25, 2018

Sure, here's a build log, xconfig.sh, and config.log https://gist.github.com/shlevy/ade184d2b0f2ff59f17e55e74e97ccfb. It's extra verbose to show all the extra flags we pass to our compiler.

This is for riscv64-unknown-linux-gnu, but I got a similar result for aarch64-unknown-linux-gnu.

@arsv
Copy link
Owner

arsv commented Feb 26, 2018

Ok I think I got it. GCC 4.9 and above needs -fwrapv to build perl. The fix wasn't applied properly to gcc-7.x, and perl-cross failed to detect gcc version in your hardened setup anyway. This only matters for -O2 and above, in particular -Os builds work just fine.

If possible, please try this branch: https://github.com/arsv/perl-cross/tree/nix
(or just apply 07208bc on top of the version you're using)

The underlying issue: https://rt.perl.org/Public/Bug/Display.html?id=121505

@shlevy
Copy link
Author

shlevy commented Feb 26, 2018

Great, that worked! Do we need that patch for non-cross-compiled perl? NixOS/nixpkgs@b45fb43

@arsv
Copy link
Owner

arsv commented Feb 26, 2018

Probably not. Unless you use perl-cross for native builds, which doesn't seem to be the case.

Mainline Configure from perl-5.24 should be able to handle gcc-7, no changes are needed there.
https://github.com/Perl/perl5/blob/f12a20dd7713aa2d7a302b66ce8be168e5ab4fec/Configure#L4741-L4755

If unsure, check config.sh for -fwrapv in ccflags.

@fsparrow
Copy link

I'm still seeing this with perl 5.24.1 cross compiled with perl-cross 1.9 via a gentoo ebuild. Are there any other possible fixes for this? "-fwrapv" is in my cflags.

@arsv
Copy link
Owner

arsv commented Nov 13, 2018

Same exact segfault? Can you post xconfig.sh and config.log somewhere?

There's also -fno-strict-aliasing, but if -fwrapv is there the other one should be as well. Check also for -O3 or any standalone -f-something, especially if the target is unusual. Maybe try building it with -O0 or -Os.

dongsupark pushed a commit to flatcar-archive/coreos-overlay that referenced this issue Feb 26, 2019
In developer containers or SDK, there has been a bug in cross-compiled
perl that it crashes with segmentation fault. It has two different
reasons:

The first issue is that perl depends on perl-cross 1.1.4, which has a
bug with gcc 7.x or newer, so that the necessary CFLAGS `-fwrapv` was
not appended. This issue was fixed in perl-cross 1.1.9 or newer. So we
just need to upgrade perl-cross to 1.2.2, the newest release.

```
  CROSS_VER=1.2.2
```

The other issue is that the CFLAGS `-O2` was given when building
cross-compiled perl. We should replace the optimization flag with `-O0`,
when it's built by the cross compiler. Without that, the segfault will
not be fixed at all.

```
  if tc-is-cross-compiler; then
      replace-flags -O? -O0
  fi
```

How to test inside the Flatcar SDK environment:

```
LD_PRELOAD=/build/amd64-usr/usr/lib/libperl.so.5.24.1 /build/amd64-usr/usr/bin/perl -V
```

See also coreos/bugs#2369
coreos/bugs#2545
arsv/perl-cross#60
dongsupark pushed a commit to flatcar-archive/coreos-overlay that referenced this issue Feb 26, 2019
In developer containers or SDK, there has been a bug in cross-compiled
perl that it crashes with segmentation fault. It has two different
reasons:

The first issue is that perl depends on perl-cross 1.1.4, which has a
bug with gcc 7.x or newer, so that the necessary CFLAGS `-fwrapv` was
not appended. This issue was fixed in perl-cross 1.1.9 or newer. So we
just need to upgrade perl-cross to 1.2.2, the newest release.

```
  CROSS_VER=1.2.2
```

The other issue is that the CFLAGS `-O2` was given when building
cross-compiled perl. We should replace the optimization flag with `-O0`,
when it's built by the cross compiler. Without that, the segfault will
not be fixed at all.

```
  if tc-is-cross-compiler; then
      replace-flags -O? -O0
  fi
```

How to test inside the Flatcar SDK environment:

```
LD_PRELOAD=/build/amd64-usr/usr/lib/libperl.so.5.24.1 /build/amd64-usr/usr/bin/perl -V
```

See also coreos/bugs#2369
coreos/bugs#2545
arsv/perl-cross#60
dongsupark pushed a commit to kinvolk/gentoo that referenced this issue Feb 27, 2019
There has been a bug that cross-compiled perl crashes with segmentation
fault. It has two different reasons: first, perl-cross didn't append
CFLAGS `-fwrapv` in case of gcc 7.x or newer, and the other is that the
CFLAGS still contained `-O2`.

The first issue was recently fixed by setting `CROSS_VER` to `1.2.2`.
On the other hand, the second issue is not fixed yet, so perl still
segfaults in case of a cross-compiled version. So let's fix it by
replacing `-O?` with `-O2`.

For example, how to test inside a cross-compilation SDK environment:

```
LD_PRELOAD=/build/amd64-usr/usr/lib/libperl.so.5.24.1 /build/amd64-usr/usr/bin/perl -V
```

See also coreos/bugs#2369
coreos/bugs#2545
arsv/perl-cross#60

Signed-off-by: Dongsu Park <[email protected]>
@chewi
Copy link
Contributor

chewi commented Dec 3, 2019

I had this problem back in February. I think I stumbled across this report, worked around it, then forgot about it until just now when I ran into it again. I can confirm that I usually use -O3 but -fwrapv and -O2 doesn't work either. -O0 does. This is for m68k-linux-gnu, which is relatively obscure.

@arsv
Copy link
Owner

arsv commented Dec 4, 2019

Current defaults are -fwrapv -fno-strict-aliasing. I skimmed over the mainline Configure, it does not look like they are passing anything else either. If you did not pass -fno-strict-aliasing, try adding it, aliasing issues can easily cause crashes.

@chewi
Copy link
Contributor

chewi commented Dec 4, 2019

-O3 -fwrapv -fno-strict-aliasing does indeed work. I can't remember if I ever checked the detection mechanism. I'll let you know if I find anything.

@kentfredric
Copy link

Oh hai @chewi, fancy seeing you here.

Just reporting that under cross compile, the built perl still segfaults this way w/ perl 5.30.3 w/ cross 1.3.4 & GCC 10.

This caused a significant amount of "fun" for building a scratch VM for m68k as it means autoconf and friends like to fail :)

Fortunately, with enough brute force, natively building perl under emulation was achievable, and didn't suffer this problem.

@arsv
Copy link
Owner

arsv commented Oct 19, 2020

For issues like this, it would be really nice to have config.sh from the failing cross build, and also the one from a successful build if available. It's very difficult to tell what's going on otherwise.

Given the rest of this issue, the first thing I would be looking for is which compiler flags were actually used for either build.

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

No branches or pull requests

5 participants