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

[POWERPC64*] llvm12 and later produces broken binaries on FreeBSD #50932

Closed
adalava mannequin opened this issue Aug 23, 2021 · 14 comments · Fixed by #86665
Closed

[POWERPC64*] llvm12 and later produces broken binaries on FreeBSD #50932

adalava mannequin opened this issue Aug 23, 2021 · 14 comments · Fixed by #86665
Assignees
Labels
bugzilla Issues migrated from bugzilla confirmed Verified by a second party llvm:codegen platform:freebsd regression

Comments

@adalava
Copy link
Mannequin

adalava mannequin commented Aug 23, 2021

Bugzilla Link 51590
Version unspecified
OS FreeBSD
Blocks #48661 #50580 #51489
CC @emaste,@luporl,@pkubaj,@zygoloid,@tstellar

Extended Description

LLVM12 and later (tested main branch at 2c5c06c) produces broken binaries on FreeBSD 13 for powerpc64 and powerpc64le. The binaries will crash with segmentation.

Steps to reproduce using FreeBSD 13 RELEASE ISO:

  1. Install llvm12 (apt install llvm12)
  2. Create echo_crash.c:

#include <capsicum_helpers.h>
#include <err.h>

int
main(int argc, char *argv[])
{

    if (caph_limit_stdio() < 0 || caph_enter() < 0)
            err(1, "capsicum");
    return 0;

}

  1. Compile with:

clang12 -O2 -g echo_crash.c -o echo_crash.good
clang12 -O2 -g -fstack-protector-strong echo_crash.c -o echo_crash.bad

  1. Execute the binaries produced.
    ./echo_crash.good: will produce no output and return normally.
    ./echo_crash.bad: Segmentation Fault
@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 23, 2021

assigned to @adalava

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 23, 2021

The same occurs with clang/LLVM built from sources (tested main branch at 2c5c06c).
When building from sources you should use llvm11, as this is the latest/best working compiler.

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 23, 2021

During my tests, a working binary is produced when -fstack-protector-strong is removed.
Strangely a working binary is also produced if -O[0|1|2|3] is removed while -fstack-protector-strong is kept

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 23, 2021

0x10010eb4 <caph_limit_stream> addis r2,r12,2
0x10010eb8 <caph_limit_stream+4> addi r2,r2,-31372
0x10010ebc <caph_limit_stream+8> mflr r0
0x10010ec0 <caph_limit_stream+12> std r27,-40(r1)
0x10010ec4 <caph_limit_stream+16> std r28,-32(r1)
0x10010ec8 <caph_limit_stream+20> std r29,-24(r1)
0x10010ecc <caph_limit_stream+24> std r30,-16(r1)
0x10010ed0 <caph_limit_stream+28> std r0,16(r1)
0x10010ed4 <caph_limit_stream+32> stdu r1,-160(r1)
0x10010ed8 <caph_limit_stream+36> mr r29,r3
0x10010edc <caph_limit_stream+40> li r3,129
0x10010ee0 <caph_limit_stream+44> addis r27,r2,-4099
0x10010ee4 <caph_limit_stream+48> rotldi r28,r3,57

0x10010ee8 <caph_limit_stream+52> ld r3,27608(r27) <--- segmentation fault
0x10010eec <caph_limit_stream+56> mr r30,r4
0x10010ef0 <caph_limit_stream+60> li r4,2049
0x10010ef4 <caph_limit_stream+64> addi r9,r28,11
0x10010ef8 <caph_limit_stream+68> std r3,112(r1)
0x10010efc <caph_limit_stream+72> lis r3,1024
0x10010f00 <caph_limit_stream+76> rotldi r5,r4,58

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 23, 2021

objdump output with clang11 and clang12
This contains the objdump output of same program compiled with both "-O2 and -fstack-protector-strong" on llvm11 (working) and llvm12(crashing)

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 23, 2021

  1. Install llvm12 (apt install llvm12)

I meant "pkg install llvm12" ;-)

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 25, 2021

An alternative test program that doesn't rely on FreeBSD libraries is:

int main(int argc, char *argv[])
{
char name[10];
if (ttyname_r(0, name, 10))
err(1, "capsicum"); return 0;
};

build parameters:
-target powerpc64-unknown-freebsd13 -O2 -g -fstack-protector-strong

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 26, 2021

Bisect points to commit 2518433
A quick workaround is under review on FreeBSD project. Comments are welcome: https://reviews.freebsd.org/D31698

@adalava
Copy link
Mannequin Author

adalava mannequin commented Aug 27, 2021

llvm12 FreeBSD in-tree fix
This is the fix/workaround commited to FreeBSD's in-tree LLVM

@tstellar
Copy link
Collaborator

tstellar commented Sep 1, 2021

Are you going to submit this fix via phabricator?

@adalava
Copy link
Mannequin Author

adalava mannequin commented Sep 1, 2021

Are you going to submit this fix via phabricator?

Just submitted it as https://reviews.llvm.org/D109090

I'm not sure who should review it. Feel free to edit it if you wish.

@adalava
Copy link
Mannequin Author

adalava mannequin commented Nov 8, 2021

The commit https://reviews.llvm.org/rG1cb9f37a17ab528e53d38c790c1bc44a087d2a50 fixed the problem on FreeBSD/powerpc64, however caused a regression on FreeBSD/amd64 freestanding objects used in kernel.

It seems like there's a problem somewhere else (check discussion on https://reviews.llvm.org/D109090).

To resolve the regression the patch should be amended to restrict the logic to PPC64 only, but more investigation is still required.

@adalava
Copy link
Mannequin Author

adalava mannequin commented Nov 9, 2021

To resolve the regression the patch should be amended to restrict the logic
to PPC64 only, but more investigation is still required.

Added https://reviews.llvm.org/D113443

@tstellar
Copy link
Collaborator

mentioned in issue #51489

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@llvmbot llvmbot added the confirmed Verified by a second party label Jan 26, 2022
justincady added a commit to justincady/llvm-project that referenced this issue Mar 26, 2024
Adjust logic of 1cb9f37 to match freebsd/freebsd-src@9a4d48a645a7a.

D113443 is the original attempt to bring this FreeBSD patch to llvm-project,
but it never landed. This change is required to build FreeBSD kernel modules
with -fstack-protector using a standard LLVM toolchain. The FreeBSD kernel
loader does not handle R_X86_64_REX_GOTPCRELX relocations.

Fixes llvm#50932.
justincady added a commit to justincady/llvm-project that referenced this issue Mar 27, 2024
Adjust logic of 1cb9f37 to match freebsd/freebsd-src@9a4d48a645a7a.

D113443 is the original attempt to bring this FreeBSD patch to llvm-project,
but it never landed. This change is required to build FreeBSD kernel modules
with -fstack-protector using a standard LLVM toolchain. The FreeBSD kernel
loader does not handle R_X86_64_REX_GOTPCRELX relocations.

Fixes llvm#50932.
justincady added a commit that referenced this issue Mar 27, 2024
Adjust logic of 1cb9f37 to match freebsd/freebsd-src@9a4d48a645a7a.

D113443 is the original attempt to bring this FreeBSD patch to
llvm-project,
but it never landed. This change is required to build FreeBSD kernel
modules
with -fstack-protector using a standard LLVM toolchain. The FreeBSD
kernel
loader does not handle R_X86_64_REX_GOTPCRELX relocations.

Fixes #50932.
@EugeneZelenko EugeneZelenko added llvm:codegen and removed clang Clang issues not falling into any other category labels Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla confirmed Verified by a second party llvm:codegen platform:freebsd regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants