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

Rust based glibc detector #1

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/glibc-detectors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
on:
push:
branches:
- main
pull_request:
paths:
- .github/workflows/glibc-detectors.yml
- linux-glibc-detectors/**

name: Glibc Detectors

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
RUST_LOG: info
RUST_BACKTRACE: 1
RUSTFLAGS: "-D warnings"
CARGO_TERM_COLOR: always

jobs:
build:
name: ${{ matrix.target }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- { name: x86_64, target: x86_64-unknown-linux-gnu }
- { name: i686, target: i686-unknown-linux-gnu }
- { name: aarch64, target: aarch64-unknown-linux-gnu }
- { name: armv7l, target: arm-unknown-linux-gnueabi }
- { name: ppc64, target: powerpc64-unknown-linux-gnu }
- { name: ppc64le, target: powerpc64le-unknown-linux-gnu }
# - { name: s390x, target: s390x-unknown-linux-gnu }
steps:
- name: Checkout source code
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly-2024-07-14
targets: ${{ matrix.target }}
components: rust-src

- uses: taiki-e/install-action@v2
name: Install cargo-zigbuild
with:
tool: cargo-zigbuild@0.19.1

- name: Install Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.12.0

- name: Run build
run: cargo +nightly-2024-07-14 zigbuild --manifest-path linux-glibc-detectors/Cargo.toml -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target ${{ matrix.target }} --release

- name: Move binary
run: mv linux-glibc-detectors/target/${{ matrix.target }}/release/linux-glibc-detector linux-glibc-detector-${{ matrix.name }}

- uses: actions/upload-artifact@v4
with:
name: linux-glibc-detector-${{ matrix.name }}
path: linux-glibc-detector-${{ matrix.name }}
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ description = "Detects the runtime version of libc on Linux systems"
[dependencies]
tracing = "0.1.40"
tempfile = "3.9.0"

[dev-dependencies]
tracing-test = "0.2.5"
3 changes: 3 additions & 0 deletions linux-glibc-detectors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/Cargo.lock
.idea/
16 changes: 16 additions & 0 deletions linux-glibc-detectors/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions linux-glibc-detectors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "linux-glibc-detector"
version = "0.1.0"
edition = "2021"

[dependencies]
libc = "0.2.155"

[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
20 changes: 20 additions & 0 deletions linux-glibc-detectors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# GLibc detector

This directory contains the source code for a very simple program to detect the glibc version using the `gnu_get_libc_version` function.

## Building

To build the program, we need to make sure that we link to an ancient version of glibc to ensure maximum compatiblity.
We can use [cargo zigbuild](https://github.com/rust-cross/cargo-zigbuild) to use `zig` as the linker to allow linking against a specific glibc version without having to muck about with docker images.

To build to program, run the following commands:

```sh
ARCH=x86_64-unknown-linux-gnu

# Make sure you have the appropriate target to build for.
rustup target add $ARCH

# Build the program using zigbuild
cargo +nightly zigbuild -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target $ARCH --release
```
1 change: 1 addition & 0 deletions linux-glibc-detectors/bin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
These files are generated on CI and copied here. Do not edit manually.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
21 changes: 21 additions & 0 deletions linux-glibc-detectors/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![no_std]
#![no_main]

// Link with libc
#[link(name = "c")]
extern "C" {}

extern crate libc;

#[no_mangle]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
unsafe {
libc::puts(libc::gnu_get_libc_version());
}
0
}

#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.75.0
1.77.0
32 changes: 21 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,45 @@
///
/// A glibc detector is a binary that can be executed to determine the version of glibc.
fn glibc_detectors() -> Vec<(&'static str, &'static [u8])> {
let mut detectors = Vec::new();

Check failure on line 18 in src/lib.rs

View workflow job for this annotation

GitHub Actions / s390x-unknown-linux-gnu

variable does not need to be mutable

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
{
detectors.push((
"x86_64",
include_bytes!("./linux-glibc-detectors/glibc-detector-x86_64").as_slice(),
include_bytes!("../linux-glibc-detectors/bin/linux-glibc-detector-x86_64").as_slice(),
));
detectors.push((
"i686",
include_bytes!("./linux-glibc-detectors/glibc-detector-i686").as_slice(),
include_bytes!("../linux-glibc-detectors/bin/linux-glibc-detector-i686").as_slice(),
));
}

#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
{
detectors.push((
"aarch64",
include_bytes!("./linux-glibc-detectors/glibc-detector-aarch64").as_slice(),
include_bytes!("../linux-glibc-detectors/bin/linux-glibc-detector-aarch64").as_slice(),
));
detectors.push((
"armv7l",
include_bytes!("./linux-glibc-detectors/glibc-detector-armv7l").as_slice(),
include_bytes!("../linux-glibc-detectors/bin/linux-glibc-detector-armv7l").as_slice(),
));
}

#[cfg(target_arch = "powerpc64")]
#[cfg(all(target_arch = "powerpc64", target_endian = "big"))]
{
detectors.push((
"ppc64le",
include_bytes!("./linux-glibc-detectors/glibc-detector-ppc64le").as_slice(),
"ppc64",
include_bytes!("../linux-glibc-detectors/bin/linux-glibc-detector-ppc64").as_slice(),
));
}

#[cfg(target_arch = "s390x")]
#[cfg(all(target_arch = "powerpc64", target_endian = "little"))]
{
detectors.push((
"s390x",
include_bytes!("./linux-glibc-detectors/glibc-detector-s390x").as_slice(),
"ppc64le",
include_bytes!("../linux-glibc-detectors/bin/linux-glibc-detector-ppc64le").as_slice(),
));
}

Expand Down Expand Up @@ -129,7 +129,15 @@

/// Detect the current version of `musl` `libc` by inspecting the `/lib/ld-musl-*.so.1` loaders.
pub fn musl_libc_version() -> Option<(u32, u32)> {
for arch in ["x86_64", "aarch64", "i386", "armhf", "powerpc64le", "s390x"] {
for arch in [
"x86_64",
"aarch64",
"i386",
"armhf",
"arm",
"powerpc64le",
"s390x",
] {
let loader = PathBuf::from(format!("/lib/ld-musl-{arch}.so.1"));
if !loader.exists() {
continue;
Expand Down Expand Up @@ -217,6 +225,7 @@
#[cfg(test)]
mod test {
use super::*;
use tracing_test::traced_test;

#[test]
fn test_glibc_version() {
Expand All @@ -229,6 +238,7 @@
}

#[test]
#[traced_test]
fn test_libc_version() {
let version = libc_version();
match version {
Expand Down
24 changes: 0 additions & 24 deletions src/linux-glibc-detectors/README.md

This file was deleted.

Binary file removed src/linux-glibc-detectors/glibc-detector-aarch64
Binary file not shown.
Binary file removed src/linux-glibc-detectors/glibc-detector-armv7l
Binary file not shown.
Binary file removed src/linux-glibc-detectors/glibc-detector-i686
Binary file not shown.
Binary file removed src/linux-glibc-detectors/glibc-detector-ppc64le
Binary file not shown.
Binary file removed src/linux-glibc-detectors/glibc-detector-s390x
Binary file not shown.
Binary file removed src/linux-glibc-detectors/glibc-detector-x86_64
Binary file not shown.
16 changes: 0 additions & 16 deletions src/linux-glibc-detectors/glibc-detector.c

This file was deleted.

62 changes: 0 additions & 62 deletions src/linux-glibc-detectors/rebuild.py

This file was deleted.

3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("{:?}", libc_detector::libc_version());
}
Loading