Skip to content

Conversation

es-fabricemarie
Copy link

resolves #6
binfmt-dispatcher gets typically registered to binfmt-misc with flags POCF.

  • P will preserve the original argv[0] used
  • O will give the resolved binary (e.g. after symlink derefence) as an open file in fd 3
  • but we can't use it as-is as FEX (at least) doesn't support taking "pathname" (execve) as an open file descriptor.
  • C and F are irrelevant here.

To support properly binary file already open as fd 3, FEX needs to be patched to use fexecve(3, argv, env) kind of call when requested, it doesn't appear to support it yet.

Signed-off-by: Fabrice A. Marie [email protected]

resolves AsahiLinux#6
binfmt-dispatcher gets typically registered to binfmt-misc with flags `POCF`.
- `P` will preserve the original argv[0] used
- `O` will give the resolved binary (e.g. after symlink derefence) as an open file in fd 3
- but we can't use it as-is as FEX (at least) doesn't support taking "pathname" (execve) as an open file descriptor.
- `C` and `F` are irrelevant here.

To support properly binary file already open as fd 3, FEX needs to be patched to use
`fexecve(3, argv, env)` kind of call when requested, it doesn't appear to support it yet.

Signed-off-by: Fabrice A. Marie <[email protected]>
Copy link

@GnomedDev GnomedDev left a comment

Choose a reason for hiding this comment

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

I have an x64 Rust binary which prints std::env::args().collect::<Vec<String>>() which previously resulted in

gnome@daisy-macbookpro:~/Coding/Personal/temp$ ./target/x86_64-unknown-linux-musl/debug/temp foo bar
[binfmt_dispatcher] Using FEX with muvm
Failed to set the new pressure in the guest: could not connect to muvm server: No such file or directory (os error 2)
No IPv6 nameserver available for NDP/DHCPv6
Using default interface naming scheme 'v257'.
["/home/gnome/Coding/Personal/temp/target/x86_64-unknown-linux-musl/debug/temp", "./target/x86_64-unknown-linux-musl/debug/temp", "./target/x86_64-unknown-linux-musl/debug/temp", "foo", "bar"]

(mangled args, but executes the binary)

I have attempted testing this patch by building this branch and replacing /usr/bin/binfmt-dispatcher (and fixing the SELinux junk), and now I get the following error when executing the same binary.

gnome@daisy-macbookpro:~/Coding/Personal/temp$ ./target/x86_64-unknown-linux-musl/debug/temp foo bar
[binfmt_dispatcher] Using FEX with muvm
Failed to set the new pressure in the guest: could not connect to muvm server: No such file or directory (os error 2)
No IPv6 nameserver available for NDP/DHCPv6
Using default interface naming scheme 'v257'.
./target/x86_64-unknown-linux-musl/debug/temp: command not found
"/usr/bin/FEXInterpreter" process exited with status code: 248

(cannot execute the binary anymore)

@WhatAmISupposedToPutHere

I have an x64 Rust binary which prints std::env::args().collect::<Vec<String>>() which previously resulted in

gnome@daisy-macbookpro:~/Coding/Personal/temp$ ./target/x86_64-unknown-linux-musl/debug/temp foo bar
[binfmt_dispatcher] Using FEX with muvm
Failed to set the new pressure in the guest: could not connect to muvm server: No such file or directory (os error 2)
No IPv6 nameserver available for NDP/DHCPv6
Using default interface naming scheme 'v257'.
["/home/gnome/Coding/Personal/temp/target/x86_64-unknown-linux-musl/debug/temp", "./target/x86_64-unknown-linux-musl/debug/temp", "./target/x86_64-unknown-linux-musl/debug/temp", "foo", "bar"]

(mangled args, but executes the binary)

I have attempted testing this patch by building this branch and replacing /usr/bin/binfmt-dispatcher (and fixing the SELinux junk), and now I get the following error when executing the same binary.

gnome@daisy-macbookpro:~/Coding/Personal/temp$ ./target/x86_64-unknown-linux-musl/debug/temp foo bar
[binfmt_dispatcher] Using FEX with muvm
Failed to set the new pressure in the guest: could not connect to muvm server: No such file or directory (os error 2)
No IPv6 nameserver available for NDP/DHCPv6
Using default interface naming scheme 'v257'.
./target/x86_64-unknown-linux-musl/debug/temp: command not found
"/usr/bin/FEXInterpreter" process exited with status code: 248

(cannot execute the binary anymore)

muvm does not preserve cwd. AsahiLinux/muvm@1edd91d

@xiangzhai
Copy link

Hi @es-fabricemarie

Before:

[binfmt_dispatcher] Using box64 with muvm
No IPv6 nameserver available for NDP/DHCPv6
[BOX64] Box64 arm64 v0.3.7 df2e4f94 with Dynarec built on Aug  9 2025 17:37:29
[BOX64] Dynarec for ARM64, with extension: ASIMD AES CRC32 PMULL ATOMICS SHA1 SHA2 USCAT FLAGM FLAGM2 FRINT
[BOX64] Running on Avalanche-M2 with 4 cores, pagesize: 4096
[BOX64] Will use hardware counter measured at 24.0 MHz emulating 3.0 GHz
[BOX64] Detected 48bits at least of address space
Using default interface naming scheme 'v257'.
[BOX64] Counted 25 Env var
[BOX64] Library search path: 
[BOX64] Binary search path: ./:bin/:/home/zhaixiang/.local/bin/:/home/zhaixiang/bin/:/usr/local/bin/:/usr/bin/:/bin/:/usr/local/sbin/:/usr/sbin/:/sbin/
[BOX64] Looking for /mnt/zulu23.30.13-ca-fx-jdk23.0.1-linux_x64/bin/java
=> [BOX64] argv[1]="/mnt/zulu23.30.13-ca-fx-jdk23.0.1-linux_x64/bin/java"
=> [BOX64] argv[2]="/mnt/zulu23.30.13-ca-fx-jdk23.0.1-linux_x64/bin/java"
[BOX64] argv[3]="-version"
[BOX64] Rename process to "java"
...
Error: Could not find or load main class .mnt.zulu23.30.13-ca-fx-jdk23.0.1-linux_x64.bin.java
Caused by: java.lang.ClassNotFoundException: /mnt/zulu23/30/13-ca-fx-jdk23/0/1-linux_x64/bin/java
"/usr/local/bin/box64" process exited with status code: 1

After:

[binfmt_dispatcher] DEBUG: Using box64 with muvm
No IPv6 nameserver available for NDP/DHCPv6
[BOX64] Box64 arm64 v0.3.7 df2e4f94 with Dynarec built on Aug  9 2025 17:37:29
[BOX64] Dynarec for ARM64, with extension: ASIMD AES CRC32 PMULL ATOMICS SHA1 SHA2 USCAT FLAGM FLAGM2 FRINT
[BOX64] Running on Avalanche-M2 with 4 cores, pagesize: 4096
[BOX64] Will use hardware counter measured at 24.0 MHz emulating 3.0 GHz
Using default interface naming scheme 'v257'.
[BOX64] Detected 48bits at least of address space
[BOX64] Counted 25 Env var
[BOX64] Library search path: 
[BOX64] Binary search path: ./:bin/:/home/zhaixiang/.local/bin/:/home/zhaixiang/bin/:/usr/local/bin/:/usr/bin/:/bin/:/usr/local/sbin/:/usr/sbin/:/sbin/
[BOX64] Looking for /mnt/zulu23.30.13-ca-fx-jdk23.0.1-linux_x64/bin/java
=> [BOX64] argv[1]="-version"
[BOX64] Rename process to "java"
...
openjdk version "23.0.1" 2024-10-15
OpenJDK Runtime Environment Zulu23.30+13-CA (build 23.0.1+11)
OpenJDK 64-Bit Server VM Zulu23.30+13-CA (build 23.0.1+11, mixed mode, sharing)

Thanks for your patch!

Leslie Zhai

}

// File descriptor 3 is where binfmt_misc typically passes the executable
let binary: PathBuf = read_link("/proc/self/fd/3").unwrap_or_else(|e| {

Choose a reason for hiding this comment

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

warning: unused import: `read_link`
  --> src/main.rs:12:29
   |
12 | use std::fs::{canonicalize, read_link};
   |                             ^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: unused import: `PathBuf`
  --> src/main.rs:15:23
   |
15 | use std::path::{Path, PathBuf};
   |                       ^^^^^^^

warning: `binfmt-dispatcher` (bin "binfmt-dispatcher") generated 2 warnings (run `cargo fix --bin "binfmt-dispatcher"` to apply 2 suggestions)

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

Successfully merging this pull request may close these issues.

strange argv handling / mangled argv ?
5 participants