babashka-sci-dev

Crispin 2022-02-25T14:50:41.633119Z

Hey @rahul080327 @thiagokokada what is the reason to prefer musl static linking in native images over standard libc?

borkdude 2022-02-25T14:51:29.888919Z

@retrogradeorbit Oh I found some docs here: https://github.com/clj-easy/graal-docs#static-linking-with-musl

lispyclouds 2022-02-25T14:53:32.235939Z

Hey @retrogradeorbit essentially glibc cannot be fully static linked in a way musl can be, see this https://stackoverflow.com/a/57478728 pretty much the reason why musl was built. its a general problem for any native binaries, not specific to GraalVM native images

lispyclouds 2022-02-25T14:54:41.359299Z

Rust for instance only does static linked binaries when used with musl+linux targets.

lispyclouds 2022-02-25T14:55:42.592419Z

Or the other way to go is Go 😛 where none of the system libs are used in the same way other binaries link with

kokada 2022-02-25T14:55:45.615609Z

The SO link posted by lispyclouds pretty much nails it

kokada 2022-02-25T14:56:44.369129Z

glibc static is something that kinda works most of the time, however may results in segfaults if you for example use a non-glibc distro or uses a glibc distro with an older version of glibc

kokada 2022-02-25T14:57:01.393059Z

Or an older version of the kernel, or using on the wrong phase of the moon

kokada 2022-02-25T14:57:03.309569Z

Etc etc

lispyclouds 2022-02-25T14:57:36.937479Z

yeah one of the main reasons for the segfaults being a mismatch of the ABI expectations the binary has with the kernel

kokada 2022-02-25T14:57:47.258579Z

You can have trully static binaries with glibc, but AFAIK this needs a specially compiled version of glibc with some flags that are completely unsupported by upstream

Crispin 2022-02-25T14:58:22.594599Z

yeah... i dunno. I get that glibc static linking is a bit of a hack, and doesnt even work with all toolchains...

kokada 2022-02-25T14:58:45.278719Z

Also, musl has some other advantages

borkdude 2022-02-25T14:58:49.295649Z

I've had quite some problems with the glibc-ed static versions of babashka and clj-kondo and will never go back doing that: those segfaults are a major headache.

kokada 2022-02-25T14:58:49.379659Z

It results in smaller binaries

Crispin 2022-02-25T14:58:51.939819Z

but the graal static glibc image doesn't seem to match what that SO answer is talking about at all

kokada 2022-02-25T14:59:28.271879Z

Like, I may be wrong, but the static babashka with musl is smaller than the dynamically babashka with glibc

borkdude 2022-02-25T14:59:35.494849Z

that is true

Crispin 2022-02-25T14:59:36.678479Z

I don't see any lib deps, no dlopens, no nss, no iconv

Crispin 2022-02-25T14:59:48.862869Z

works on alpine

Crispin 2022-02-25T14:59:56.357849Z

I'll go have a compare of sizes

borkdude 2022-02-25T15:00:23.269939Z

I recommend either don't pretend to be static, or use musl.

Crispin 2022-02-25T15:00:27.129039Z

SO post may be talking in a more general sense of why static linking glibc is problematic?

kokada 2022-02-25T15:00:36.468299Z

Yeah, but for example

kokada 2022-02-25T15:00:47.899229Z

The old babashka static version (with glibc) didn't work in NixOS at all

🤔 1
Crispin 2022-02-25T15:00:49.423719Z

I dont think its pretending to be static

kokada 2022-02-25T15:00:52.585799Z

The new one does

lispyclouds 2022-02-25T15:00:57.338919Z

> I don't see any lib deps, no dlopens, no nss, no iconv well thats some of the possible calls the binary may make, the graalvm one doesnt i guess?

kokada 2022-02-25T15:01:23.392749Z

> well thats some of the possible calls the binary may make, the graalvm one doesnt i guess? Yeah, Java itself may end up calling nss for example

borkdude 2022-02-25T15:01:25.768829Z

For aarch64, musl is not supported. There I use the mostly static option: --static with dynamic glibc. that works fine.

kokada 2022-02-25T15:01:32.520019Z

So Java -> GraalVM can call it too

Crispin 2022-02-25T15:01:34.063649Z

yeah. and I experimented with musl versions and graal. And the new musl versions don't seem to work with graal at all

kokada 2022-02-25T15:02:16.825049Z

> For aarch64, musl is not supported. There I use the mostly static option: --static with dynamic glibc. that works fine. This is also a good point, static building glibc is not supported at all with GraalVM Native Image either

kokada 2022-02-25T15:02:23.343959Z

It works but it is not documented

Crispin 2022-02-25T15:02:48.217709Z

hmm but I just built a static glibc with graal

Crispin 2022-02-25T15:02:57.155469Z

not mostly static. fully static

kokada 2022-02-25T15:02:57.800369Z

The documentation explicitly says: • Dynamically compilation • Static compilation with musl • Static compilation with glibc dynamically linked

kokada 2022-02-25T15:03:20.225519Z

> not mostly static. fully static Like I said, it works. It is NOT supported though

Crispin 2022-02-25T15:03:47.726729Z

is it my graal version?

kokada 2022-02-25T15:03:51.919329Z

No

Crispin 2022-02-25T15:03:52.341399Z

Im using 21.3.0

kokada 2022-02-25T15:03:55.080429Z

It was never supported

kokada 2022-02-25T15:03:58.632069Z

It works

kokada 2022-02-25T15:04:04.628599Z

But was never documented

kokada 2022-02-25T15:04:11.766969Z

You can see in the documentation I posted above

kokada 2022-02-25T15:04:30.622209Z

It either recommends you to build with musl OR dynamically loaded glibc

☝️ 1
Crispin 2022-02-25T15:06:17.831919Z

yeah it does

borkdude 2022-02-25T15:06:26.779889Z

This musl or dynamic documentation showed up around a year or so ago, before then the only option was --static but this was in hindsight a mistake on their end.

lispyclouds 2022-02-25T15:07:07.073259Z

yeah thats how we initially built the bb and kondo images with quite the suffering later on

lispyclouds 2022-02-25T15:07:46.914319Z

its pretty much undefined behaviour when the image is "mostly static"

Crispin 2022-02-25T15:09:13.920509Z

ok. i think I get it

Crispin 2022-02-25T15:09:24.183659Z

its always been a hack until musl

Crispin 2022-02-25T15:10:24.697559Z

just curiously do you remember how the problem of static glibc manifested on other platforms?

Crispin 2022-02-25T15:10:29.046539Z

segfault?

borkdude 2022-02-25T15:10:46.483579Z

@rahul080327 I refer to "mostly static" as a supported option explictly by the GraalVM docs. This is supported and gives defined behavior. https://www.graalvm.org/22.0/reference-manual/native-image/StaticImages/#build-mostly-static-native-image

👍🏼 1
borkdude 2022-02-25T15:10:58.565799Z

@retrogradeorbit yes

Crispin 2022-02-25T15:11:26.973679Z

do you remember what platform?

borkdude 2022-02-25T15:13:06.620659Z

don't remember, but I think it happened on systems that had a different (old?) glibc than the ubuntu I compiled it on

borkdude 2022-02-25T15:13:15.028339Z

maybe search in babashka's issue

Crispin 2022-02-25T15:15:24.671319Z

awesome. this is great info. thanks

lispyclouds 2022-02-25T15:16:15.546959Z

if i rack my brains it was debian 9 i think ?

Crispin 2022-02-25T15:18:30.507369Z

reading that ticket... wow that is horrific

lispyclouds 2022-02-25T15:19:26.911589Z

yeah when it hit us it was very nasty head scratcher for us 😕

kokada 2022-02-25T15:23:58.636739Z

Another fun one: https://github.com/oracle/graal/issues/3737

borkdude 2022-02-25T15:25:04.634809Z

I now use the -H:+StaticExecutableWithDynamicLibC option for aarch64

kokada 2022-02-25T15:25:57.505869Z

Yeah, I took this issue exactly from Babashka source code (describing why we are using -H:+StaticExecutableWithDynamicLibC) 😅

kokada 2022-02-25T15:26:09.962999Z

BTW, does those binaries work on Android?

kokada 2022-02-25T15:26:16.929169Z

I assume no?

borkdude 2022-02-25T15:26:22.714939Z

yes, they do on android tablets

kokada 2022-02-25T15:26:48.022859Z

Weird, Android shouldn't have glibc

kokada 2022-02-25T15:27:54.691529Z

Does it work in Alpine?

borkdude 2022-02-25T15:28:16.916009Z

Several mentions of android here: https://github.com/babashka/babashka/issues/241

borkdude 2022-02-25T15:28:43.080149Z

Dunno, try it ;)

borkdude 2022-02-25T15:28:51.584389Z

I expect no, since it requires glibc

kokada 2022-02-25T15:29:16.422449Z

With termux I can understand

kokada 2022-02-25T15:29:21.540949Z

Termux may bring glibc

kokada 2022-02-25T15:30:16.537979Z

https://github.com/termux/termux-packages/issues/2798#issuecomment-632134264 Ok, my mind is blown