Skip to content

Fibers are broken on alpine x86 with clang #10398

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

Closed
arnaud-lb opened this issue Jan 21, 2023 · 7 comments
Closed

Fibers are broken on alpine x86 with clang #10398

arnaud-lb opened this issue Jan 21, 2023 · 7 comments

Comments

@arnaud-lb
Copy link
Member

Description

Found while looking at #9735 (comment)

Most fibers tests fail when building with clang on alpine x86 in non-debug.

This Dockerfile allows to reproduce the issue:

FROM i386/alpine:3.17.1
RUN apk add gcc clang autoconf make bison re2c git musl-dev
RUN git clone  https://github.com/php/php-src --branch master --depth 1
WORKDIR php-src
RUN ./buildconf --force && \
    CC=clang \
    CXX=clang++ \
    ./configure --disable-all --with-pic --build i586-alpine-linux-musl && \
    make -j$(nproc)
RUN TESTS=Zend/tests/fibers make test || true
CMD bash

Also happens in an alpine VM:

CC=clang CXX=clang++ ./configure --disable-all
TESTS=Zend/tests/fibers make test

Tests pass when building with gcc, or with -O0.

PHP Version

8.1 f673449

Operating System

Alpine 3.17.1 x86

@arnaud-lb
Copy link
Member Author

May be related: #9566

@andypost
Copy link
Contributor

Thank you for filing issue. Sadly I have no idea how to tackle it

@andypost
Copy link
Contributor

FYI I'm building only PHP 8.2 with clang, php81 package using gcc

@nielsdos
Copy link
Member

nielsdos commented Jan 21, 2023

I'm taking a brief look at this, it's a segfault in zend_fibers.c:350:

zend_fiber_transfer transfer = *data.transfer;

@nielsdos
Copy link
Member

The problem seems to be in how the pointers (that are in the argument's struct) are passed from the assembly to the function.

@nielsdos
Copy link
Member

nielsdos commented Jan 21, 2023

The following patch fixes it for me. I guess more platforms than just Alpine are actually affected by this:

diff --git a/Zend/asm/make_i386_sysv_elf_gas.S b/Zend/asm/make_i386_sysv_elf_gas.S
index b76de260..5568f57b 100644
--- a/Zend/asm/make_i386_sysv_elf_gas.S
+++ b/Zend/asm/make_i386_sysv_elf_gas.S
@@ -82,8 +82,8 @@ make_fcontext:
 
 trampoline:
     /* move transport_t for entering context-function */
-    movl  %edi, (%esp)
-    movl  %esi, 0x4(%esp)
+    pushl %esi
+    pushl %edi
     pushl %ebp
     /* jump to context-function */
     jmp *%ebx

Can someone else please also verify if this fixes it for them? Thanks :)

Note: idk why the stack overwriting was used instead of pushing, I guess to save stack space? If that's the case we can do that too but the solution becomes a little more complicated.

@nielsdos
Copy link
Member

nielsdos commented Jan 21, 2023

I don't have a Linux 32-bit install handy, and I want to check whether it breaks other platforms or not. So I'm going to create a draft PR so that CI can run through this.

EDIT: oh also, I guess if this fix works then we can get rid of the fiber tests in that ignorelist you mentioned here: #9735 (comment) ?

nielsdos added a commit to nielsdos/php-src that referenced this issue Jan 21, 2023
nielsdos added a commit to nielsdos/php-src that referenced this issue Jan 21, 2023
The arguments were passed incorrectly. This uses the regular approach to
pass arguments on the SYSV 32-bit ABI.
nielsdos added a commit to nielsdos/php-src that referenced this issue Jan 28, 2023
Fixes phpGH-10398

The stack was misaligned upon entering the trampoline function [1], this
causes a CPU trap when the SSE instruction is executed to copy data from
the stack. This was fixed upstream [2]. This commit syncs all upstream
changes from the boost/context assembly files to our copy.

[1] php#10407 (comment)
[2] boostorg/context#219
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.

4 participants