Skip to content
Merged
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
1 change: 1 addition & 0 deletions .buildbot.config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[alloy]
bdwgc-assertions = true
bdwgc-link-shared = true

[rust]
codegen-units = 0 # Use many compilation units.
Expand Down
2 changes: 1 addition & 1 deletion .buildbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sh rustup.sh --default-host x86_64-unknown-linux-gnu \
-y
export PATH=$(pwd)/.cargo/bin/:$PATH

rustup toolchain link alloy build/x86_64-unknown-linux-gnu/stage1
rustup toolchain link alloy build/x86_64-unknown-linux-gnu/stage2

# Build and test yksom
git clone --recursive https://github.com/softdevteam/yksom
Expand Down
3 changes: 2 additions & 1 deletion .buildbot_dockerfile_debian
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ FROM debian:latest
ARG CI_UID
RUN useradd -m -u ${CI_UID} ci
RUN apt-get update && \
apt-get -y install build-essential curl cmake python3 git ninja-build time autoconf libtool libssl-dev pkg-config
apt-get -y install build-essential curl cmake python3 git ninja-build \
time autoconf libclang-dev libtool libssl-dev pkg-config
WORKDIR /ci
RUN chown ${CI_UID}:${CI_UID} .
COPY --chown=${CI_UID}:${CI_UID} . .
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@
[submodule "src/bdwgc"]
path = src/bdwgc
url = https://github.com/softdevteam/bdwgc
[submodule "src/tools/bindgen"]
path = src/tools/bindgen
url = https://github.com/rust-lang/rust-bindgen.git
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,12 @@ fn link_sanitizer_runtime(
}
}

fn add_bdwgc(sess: &Session, linker: &mut dyn Linker) {
let sysroot = sess.sysroot.clone();
linker.link_args(&["-rpath", &sysroot.join("lib").to_str().unwrap()]);
linker.link_dylib_by_name("gc", false, false);
}

/// Returns a boolean indicating whether the specified crate should be ignored
/// during LTO.
///
Expand Down Expand Up @@ -2254,6 +2260,8 @@ fn linker_with_args(
// Sanitizer libraries.
add_sanitizer_libraries(sess, flavor, crate_type, cmd);

add_bdwgc(sess, cmd);

// Object code from the current crate.
// Take careful note of the ordering of the arguments we pass to the linker
// here. Linkers will assume that things on the left depend on things to the
Expand Down
1 change: 1 addition & 0 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ dependencies = [
name = "sysroot"
version = "0.0.0"
dependencies = [
"bdwgc",
"proc_macro",
"profiler_builtins",
"std",
Expand Down
82 changes: 34 additions & 48 deletions library/bdwgc/build.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,39 @@
use std::env;
use std::path::Path;
use std::process::Command;
#[cfg(not(all(target_pointer_width = "64", target_arch = "x86_64")))]
compile_error!("Requires x86_64 with 64 bit pointer width.");

#[cfg(not(feature = "link-shared"))]
fn build_bdwgc() {
use std::env;
use std::path::PathBuf;
use std::process::Command;

const BDWGC_REPO: &str = "../../src/bdwgc";
const BDWGC_BUILD_DIR: &str = "lib";

let out_dir = env::var("OUT_DIR").unwrap();
let bdwgc_src = PathBuf::from(BDWGC_REPO);

if bdwgc_src.read_dir().unwrap().count() == 0 {
Command::new("git")
.args(["submodule", "update", "--init", BDWGC_REPO])
.output()
.expect("Failed to clone BDWGC repo");
}

let mut build_dir = PathBuf::from(&out_dir);
build_dir.push(BDWGC_BUILD_DIR);

let mut build = cmake::Config::new(&bdwgc_src);
build
.pic(true)
.define("BUILD_SHARED_LIBS", "OFF")
.define("enable_parallel_mark", "Off")
.cflag("-DGC_ALWAYS_MULTITHREADED");

#[cfg(feature = "gc-assertions")]
build.define("enable_gc_assertions", "ON");

#[cfg(not(feature = "gc-debug"))]
build.profile("Release");

#[cfg(feature = "gc-debug")]
build.profile("Debug");

build.build();

println!("cargo:rustc-link-search=native={}", &build_dir.display());
println!("cargo:rustc-link-lib=static=gc");
}

fn main() {
#[cfg(not(feature = "link-shared"))]
build_bdwgc();
#[cfg(feature = "link-shared")]
println!("cargo:rustc-link-lib=dylib=gc");
let cwd = env::var("CARGO_MANIFEST_DIR").unwrap();
let header = Path::new(&cwd)
.parent()
.unwrap()
.parent()
.unwrap()
.join("src")
.join("bdwgc")
.join("include")
.join("gc.h");

let bindgen = std::env::var("RUSTC_BINDGEN").unwrap();
let out = Path::new(&env::var("OUT_DIR").unwrap()).join("bindings.rs");
let status = Command::new(bindgen)
.args(&[
"--ctypes-prefix",
"libc",
"--use-core",
"-o",
out.to_str().unwrap(),
header.to_str().unwrap(),
"--",
"-DGC_THREADS",
])
.status()
.unwrap();

if !status.success() {
panic!("bindgen failed with status: {:?}", status);
}
println!("cargo::rustc-link-lib=dylib=gc");
}
70 changes: 4 additions & 66 deletions library/bdwgc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![no_std]
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

#[repr(C)]
#[derive(Default)]
Expand Down Expand Up @@ -29,69 +33,3 @@ pub struct ProfileStats {
/// Number of bytes freed explicitly since the recent GC.
pub expl_freed_bytes_since_gc: usize,
}

#[link(name = "gc")]
extern "C" {
pub fn GC_malloc(nbytes: usize) -> *mut u8;

pub fn GC_posix_memalign(mem_ptr: *mut *mut u8, align: usize, nbytes: usize) -> i32;

pub fn GC_realloc(old: *mut u8, new_size: usize) -> *mut u8;

pub fn GC_free(dead: *mut u8);

pub fn GC_base(mem_ptr: *mut u8) -> *mut u8;

pub fn GC_register_finalizer(
ptr: *mut u8,
finalizer: Option<unsafe extern "C" fn(*mut u8, *mut u8)>,
client_data: *mut u8,
old_finalizer: *mut extern "C" fn(*mut u8, *mut u8),
old_client_data: *mut *mut u8,
);

pub fn GC_register_finalizer_no_order(
ptr: *mut u8,
finalizer: Option<unsafe extern "C" fn(*mut u8, *mut u8)>,
client_data: *mut u8,
old_finalizer: *mut extern "C" fn(*mut u8, *mut u8),
old_client_data: *mut *mut u8,
);

pub fn GC_gcollect();

pub fn GC_thread_is_registered() -> u32;

pub fn GC_pthread_create(
native: *mut libc::pthread_t,
attr: *const libc::pthread_attr_t,
f: extern "C" fn(_: *mut libc::c_void) -> *mut libc::c_void,
value: *mut libc::c_void,
) -> libc::c_int;

pub fn GC_pthread_join(native: libc::pthread_t, value: *mut *mut libc::c_void) -> libc::c_int;

pub fn GC_pthread_exit(value: *mut libc::c_void) -> !;

pub fn GC_pthread_detach(thread: libc::pthread_t) -> libc::c_int;

pub fn GC_init();

pub fn GC_keep_alive(ptr: *mut u8);

pub fn GC_set_finalize_on_demand(state: i32);

pub fn GC_set_finalizer_notifier(f: extern "C" fn());

pub fn GC_should_invoke_finalizers() -> u32;

pub fn GC_invoke_finalizers() -> u64;

pub fn GC_get_gc_no() -> u64;

pub fn GC_enable();

pub fn GC_is_disabled() -> i32;

pub fn GC_disable();
}
12 changes: 6 additions & 6 deletions library/std/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ unsafe fn gc_malloc(layout: Layout) -> *mut u8 {
#[inline]
unsafe fn gc_realloc(ptr: *mut u8, old_layout: Layout, new_size: usize) -> *mut u8 {
if old_layout.align() <= MIN_ALIGN && old_layout.align() <= new_size {
unsafe { bdwgc::GC_realloc(ptr, new_size) as *mut u8 }
unsafe { bdwgc::GC_realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
} else {
unsafe {
let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
Expand All @@ -144,7 +144,7 @@ unsafe fn gc_realloc(ptr: *mut u8, old_layout: Layout, new_size: usize) -> *mut
#[inline]
unsafe fn gc_free(ptr: *mut u8, _: Layout) {
unsafe {
bdwgc::GC_free(ptr);
bdwgc::GC_free(ptr as *mut libc::c_void);
}
}

Expand Down Expand Up @@ -209,7 +209,7 @@ pub fn init() {
unsafe {
bdwgc::GC_init();
bdwgc::GC_set_finalize_on_demand(1);
bdwgc::GC_set_finalizer_notifier(notify_finalizer_thread);
bdwgc::GC_set_finalizer_notifier(Some(notify_finalizer_thread));
#[cfg(feature = "bdwgc-disable")]
bdwgc::GC_disable()
}
Expand All @@ -220,7 +220,7 @@ pub fn thread_registered() -> bool {
}

pub fn keep_alive<T>(ptr: *mut T) {
unsafe { bdwgc::GC_keep_alive(ptr as *mut u8) }
unsafe { bdwgc::GC_keep_alive(ptr as *mut _ as u64) }
}

pub fn is_enabled() -> bool {
Expand Down Expand Up @@ -606,7 +606,7 @@ impl<T> Gc<T> {
}
}

unsafe extern "C" fn finalizer_shim<T>(obj: *mut u8, _: *mut u8) {
unsafe extern "C" fn finalizer_shim<T>(obj: *mut libc::c_void, _: *mut libc::c_void) {
let drop_fn = drop_in_place::<GcBox<T>>;
unsafe { drop_fn(obj as *mut GcBox<T>) };
}
Expand All @@ -625,7 +625,7 @@ impl<T> Gc<T> {
let ptr = Box::leak(Box::new_in(GcBox { value }, GcAllocator));
unsafe {
bdwgc::GC_register_finalizer_no_order(
ptr as *mut _ as *mut u8,
ptr as *mut _ as *mut libc::c_void,
Some(finalizer_shim::<T>),
ptr::null_mut(),
ptr::null_mut(),
Expand Down
8 changes: 6 additions & 2 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ impl Thread {
};
}

let ret =
crate::bdwgc::GC_pthread_create(&mut native, attr.as_ptr(), thread_start, p as *mut _);
let ret = crate::bdwgc::GC_pthread_create(
&mut native,
attr.as_ptr() as *const bdwgc::pthread_attr_t,
Some(thread_start),
p as *mut _,
);
// Note: if the thread creation fails and this assert fails, then p will
// be leaked. However, an alternative design could cause double-free
// which is clearly worse.
Expand Down
1 change: 1 addition & 0 deletions library/sysroot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ proc_macro = { path = "../proc_macro", public = true }
profiler_builtins = { path = "../profiler_builtins", optional = true }
std = { path = "../std", public = true }
test = { path = "../test", public = true }
bdwgc = { path = "../bdwgc", public = true }

# Forward features to the `std` crate as necessary
[features]
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ignore = [
"src/doc/rust-by-example",
"src/doc/rustc-dev-guide",
"src/llvm-project",
"src/tools/bindgen",
"src/tools/cargo",
"src/tools/clippy",
"src/tools/enzyme",
Expand Down
Loading