Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Some missing parts for musl #2071

Merged
merged 11 commits into from
Feb 10, 2018
Merged

Some missing parts for musl #2071

merged 11 commits into from
Feb 10, 2018

Conversation

yshui
Copy link
Contributor

@yshui yshui commented Jan 31, 2018

No description provided.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @yshui! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.

Some tips to help speed things up:

  • smaller, focused PRs are easier to review than big ones

  • try not to mix up refactoring or style changes with bug fixes or feature enhancements

  • provide helpful commit messages explaining the rationale behind each change

Bear in mind that large or tricky changes may require multiple rounds of review and revision.

Please see CONTRIBUTING.md for more information.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Copy link
Contributor

@joakim-noah joakim-noah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is scoped to Musl, so shouldn't affect anything else.

int sigev_notify;
void function(sigval) sigev_notify_function;
pthread_attr_t *sigev_notify_attributes;
char[56-3*long.sizeof] __pad;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, spaces between operators. Otherwise OK.

@joakim-noah
Copy link
Contributor

joakim-noah commented Feb 3, 2018

After all your work on using the Musl libc, I thought I'd try it out, so I whipped up an Alpine VPS at my host. I got dmd master built by bootstrapping through ldc ltsmaster and then druntime master built fine, after adding this patch and commenting out some backtrace declarations that aren't there on Alpine.

However, the druntime test runner always segfaults on me with the tests I tried, any idea what's going on? Are you modifying dmd in some way for Musl, other than having it set CRuntime_Musl instead of CRuntime_Glibc like I am? Never mind, there was an issue with invoking the druntime tests as a shared library, compounded by the fact that the makefile ignores SHARED=0 when building the tests. Modifying the makefile directly worked around those issues, am able to run the druntime tests now. Building and running a simple D program works fine for me, tried this one from the tests after modifying it to only use puts/printf from druntime.

@joakim-noah
Copy link
Contributor

Alright, finally got all the druntime tests to run on Alpine with Musl, by applying this pull and making a few tweaks. Two test blocks from core.thread that call GC.collect manually had to be disabled, and the test runner still segfaulted on exit after running and passing all other unit tests in core.thread.

When I made the test runner skip core.thread, I only had to disable this assert in core.sync.mutex, just like on Bionic, all the other druntime modules' tests ran fine. When running the additional tests, I only had to disable thread_join from init_fini in release mode, everything else worked after the two changes mentioned above: SHARED turned off and the backtrace declarations commented out.

@wilzbach
Copy link
Member

wilzbach commented Feb 3, 2018

@yshui Once the issues that @joakim-noah has pointed out, have been addressed, how about making adding a changelog entry and announcement in the NG?

so I whipped up an Alpine VPS at my host.

As Alpine is getting more and more popular, how about doing this in a Dockerfile which can then be used by everyone who wants to use D applications with Alpine?

The GoLang people do this: https://hub.docker.com/_/golang/
There's already DMD in Docker and compiling an application for alpine could be as simple as:

docker run --rm -ti -v $(pwd):/src dlang-alpine/dmd dmd -run hello_alpine.d

Someone already missed it here: lindt/docker-dmd#1

@yshui
Copy link
Contributor Author

yshui commented Feb 4, 2018

@joakim-noah Yes, shared library doesn't work yet. I would need some time to look into that.

@ibuclaw
Copy link
Member

ibuclaw commented Feb 4, 2018

Has gdc and ldc compilers been tested with these bindings? That would separate binding problems from just dmd problems.

@joakim-noah
Copy link
Contributor

Why would you suspect dmd?

@ibuclaw
Copy link
Member

ibuclaw commented Feb 4, 2018

Experience. GDC (and maybe LDC) just integrates into the environment if you compile it for that platform.

@yshui
Copy link
Contributor Author

yshui commented Feb 5, 2018

@ibuclaw @joakim-noah I think it's a problem with musl.

@@ -1294,11 +1294,11 @@ else version (CRuntime_Musl)
timespec st_atim;
timespec st_mtim;
timespec st_ctim;
extern(D)
extern(D) @safe @property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit this too, wonder why @safe is only needed with this C runtime.

Copy link
Contributor Author

@yshui yshui Feb 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Other C runtimes don't use this definition. e.g. in CRuntime_Glibc this is behind a (__USE_MISC || __USE_XOPEN2KB) check, which is false.

@yshui
Copy link
Contributor Author

yshui commented Feb 5, 2018

Erm, at src/rt/sections_elf_shared.d:675, doing the relocation seems to fix the crash.

I'll push the change and see what the buildbot has to say.

Edit: looks like the buildbot doesn't like it :(

@yshui
Copy link
Contributor Author

yshui commented Feb 5, 2018

Looks like the glibc dynamic linker also relocates the entries in the dynamic section, while musl's doesn't. So I scoped the change to musl.

@yshui
Copy link
Contributor Author

yshui commented Feb 5, 2018

test/shared/link assumes library constructors are always run before main constructors, which seems to not be true with musl.

A couple other shared tests also make assumptions that are not true in musl. Otherwise all tests pass.

@yshui
Copy link
Contributor Author

yshui commented Feb 7, 2018

@joakim-noah Can you test again?

@joakim-noah
Copy link
Contributor

Ran the druntime tests again with this latest pull, which fixes most of the problems. I still get a segfault in init_fini/thread_join and shared/finalize errors out, when running the tests in release mode. In debug mode, asserts start tripping up in the additional tests for shared libraries. Didn't actually need to remove the backtrace declarations, that was only needed for the bootstrap ldc, which I didn't modify to use CRuntime_Musl.

I went ahead and started running the Phobos tests too, most of them passed with these additions to druntime:

diff --git a/src/core/stdc/math.d b/src/core/stdc/math.d
index 488b9d0a..8a7000bc 100644
--- a/src/core/stdc/math.d
+++ b/src/core/stdc/math.d
@@ -626,7 +626,7 @@ else version( CRuntime_Musl )
         ///
         FP_FAST_FMAL = 0,
     }
-
+pure{
     int __fpclassifyf(float x);
     int __fpclassify(double x);
     int __fpclassifyl(real x);
@@ -730,6 +730,7 @@ else version( CRuntime_Musl )
             : __signbitl(x);
     }
   }
+    }
 }
 else version( MinGW )
 {
diff --git a/src/core/sys/posix/pthread.d b/src/core/sys/posix/pthread.d
index 23df7895..9b76e9db 100644
--- a/src/core/sys/posix/pthread.d
+++ b/src/core/sys/posix/pthread.d
@@ -384,6 +384,7 @@ else version( CRuntime_Bionic )
 else version( CRuntime_Musl )
 {
 
+    enum PTHREAD_MUTEX_INITIALIZER = pthread_mutex_t.init;
 }
 else
 {
diff --git a/src/core/sys/posix/stdlib.d b/src/core/sys/posix/stdlib.d
index 70794da8..186633c6 100644
--- a/src/core/sys/posix/stdlib.d
+++ b/src/core/sys/posix/stdlib.d
@@ -125,6 +125,10 @@ else version( CRuntime_Bionic )
     // Added since Lollipop
     int posix_memalign(void**, size_t, size_t);
 }
+else version( CRuntime_Musl )
+{
+    int posix_memalign(void**, size_t, size_t);
+}
 
 //
 // C Extension (CX)

That first pure block is needed because of a recent change in git master for druntime. This is the patch I used to get most Phobos tests passing:

diff --git a/std/datetime/timezone.d b/std/datetime/timezone.d
index 61416cf9e..4de1fd7c3 100644
--- a/std/datetime/timezone.d
+++ b/std/datetime/timezone.d
@@ -758,7 +758,7 @@ public:
 
     @safe unittest
     {
-        assert(LocalTime().dstName !is null);
+        //assert(LocalTime().dstName !is null);
 
         version(Posix)
         {
diff --git a/std/experimental/allocator/building_blocks/region.d b/std/experimental/allocator/building_blocks/region.d
index 82e550e3c..dfbd6641d 100644
--- a/std/experimental/allocator/building_blocks/region.d
+++ b/std/experimental/allocator/building_blocks/region.d
@@ -830,7 +830,7 @@ version(Posix) struct SbrkRegion(uint minAlign = platformAlignment)
     }
 }
 
-version(Posix) @system unittest
+version(rosix) @system unittest
 {
     // Let's test the assumption that sbrk(n) returns the old address
     const p1 = sbrk(0);
@@ -842,7 +842,7 @@ version(Posix) @system unittest
     sbrk(-4096);
 }
 
-version(Posix) @system unittest
+version(rosix) @system unittest
 {
     import std.typecons : Ternary;
     import std.algorithm.comparison : min;
diff --git a/std/file.d b/std/file.d
index 2b042de5b..9dea2c164 100644
--- a/std/file.d
+++ b/std/file.d
@@ -1346,7 +1346,7 @@ if (isConvertibleToString!R)
     }
 
     testTimes(0);
-    version (linux)
+    version (rinux)
         testTimes(123_456_7);
 
     rmdirRecurse(newdir);
diff --git a/std/socket.d b/std/socket.d
index 6a5e5ff2b..97167ea0a 100644
--- a/std/socket.d
+++ b/std/socket.d
@@ -203,7 +203,7 @@ string formatSocketError(int err) @trusted
             else
                 return "Socket error " ~ to!string(err);
         }
-        else version (CRuntime_Bionic)
+        else version (CRuntime_Musl)
         {
             auto errs = strerror_r(err, buf.ptr, buf.length);
             if (errs == 0)
@@ -2063,7 +2063,7 @@ static if (is(sockaddr_un))
         }
     }
 
-    @safe unittest
+    version(none)@safe unittest
     {
         import core.stdc.stdio : remove;
         import std.file : deleteme;
diff --git a/std/stdio.d b/std/stdio.d
index 16d34f0e9..eab71ad10 100644
--- a/std/stdio.d
+++ b/std/stdio.d
@@ -81,6 +81,11 @@ version (CRuntime_Bionic)
     version = NO_GETDELIM;
 }
 
+version (CRuntime_Musl)
+{
+    version = GENERIC_IO;
+    version = HAS_GETDELIM;
+}
 // Character type used for operating system filesystem APIs
 version (Windows)
 {

One assert, three test blocks, and one linux test function were disabled. Also, the std.net.curl tests don't pass, looks like some of the socket declarations in druntime are wrong and cause runtime exceptions.

Other than that, looking good so far, just more polishing to do. I don't expect you to fix all these issues though, so whenever you're ready, we can get this pull in.

@yshui
Copy link
Contributor Author

yshui commented Feb 8, 2018

@joakim-noah pure and missing definitions added.

Copy link
Contributor

@joakim-noah joakim-noah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@joakim-noah
Copy link
Contributor

@wilzbach, this is ready to go. This port still needs some polishing for shared libraries and phobos to pass all their tests, but we can get that in later.

Copy link
Member

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please go step by step to success!

@@ -377,6 +377,7 @@ unittest
// For example, Bionic doesn't appear to do so, so this test is
// not run on Android.
version (CRuntime_Bionic) {} else
version (CRuntime_Musl) {} else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I thought Musl is promoted as drop-in replacement for glibc...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compatibility only extends to the C source level, plus musl doesn't implement all features.

@dlang-bot dlang-bot merged commit edf321a into dlang:master Feb 10, 2018
rracariu pushed a commit to rracariu/druntime that referenced this pull request Feb 12, 2018
commit cb32bd4
Merge: edf321a e714ea0
Author: The Dlang Bot <code+dlang-bot@dawg.eu>
Date:   Sun Feb 11 15:09:55 2018 +0100

    Merge pull request dlang#2085 from ibuclaw/linttrailing

    posix.mak: Add check for trailing whitespace from phobos
    merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>

commit e714ea0
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Sun Feb 11 11:01:26 2018 +0100

    posix.mak: Add lint check for trailing whitespace from Phobos

commit edf321a
Merge: 7653199 fdc4779
Author: The Dlang Bot <code+dlang-bot@dawg.eu>
Date:   Sat Feb 10 23:42:13 2018 +0100

    Merge pull request dlang#2071 from yshui/musl_fixes

    Some missing parts for musl
    merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>

commit fdc4779
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Thu Feb 8 08:18:49 2018 -0500

    Add pthread definitions for musl

commit 9a7be0e
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Thu Feb 8 08:15:30 2018 -0500

    Define posix_memalign for musl

commit 2c44836
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Thu Feb 8 08:14:19 2018 -0500

    Mark math functions as pure

commit 3441362
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Mon Feb 5 11:53:33 2018 -0500

    Add definition of utimes for musl

commit ea85b4d
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Mon Feb 5 11:52:23 2018 -0500

    Skip a test in core.sync.mutex for musl

    This test make sure that locking will fail after the mutex is destroyed,
    which is not the case for musl.

commit f08f10b
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Mon Feb 5 10:04:25 2018 -0500

    Relocate the strtab for musl as well

commit 6078897
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Mon Feb 5 04:31:52 2018 -0500

    Mark stat_t.st_*time functions as @safe

commit 4ddf2a1
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Wed Jan 31 00:24:25 2018 -0500

    Add definition of errno for musl

commit b37372f
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Wed Jan 31 00:18:35 2018 -0500

    Add musl definition to core/sys/posix/sys/utsname.d

commit 4b9af0c
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Wed Jan 31 00:16:12 2018 -0500

    Add missing sigevent

commit 525ddfb
Author: Yuxuan Shui <yshuiv7@gmail.com>
Date:   Wed Jan 31 00:14:55 2018 -0500

    Remove duplicate CLOCK_MONOTONIC
@joakim-noah
Copy link
Contributor

@yshui, any plans to finish off Phobos? I got that linux test function in std.file to work with Musl by adding declarations for utimensat and AT_FDCWD to druntime, as I had to for Bionic.

@yshui
Copy link
Contributor Author

yshui commented Feb 13, 2018

@joakim-noah Since you had it working, you could go ahead and open a PR, if you want to.

@joakim-noah
Copy link
Contributor

OK, just wanted to make sure we weren't duplicating effort. I will try to fix those remaining Phobos tests for Musl, not going to do anything with shared libraries though.

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

Successfully merging this pull request may close these issues.

6 participants