Skip to content

feat: add rust bindings #772

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:
path: "~/.cache/bazel"
key: bazel
- run: bazel build -c opt //:snmalloc
- run: bazel build -c opt //:snmalloc-rs
- run: bazel build -c opt //:snmalloc-rust-support

# If this looks remarkably familiar, that's because it is. Sigh.
macos:
Expand Down Expand Up @@ -166,7 +166,7 @@ jobs:
path: "~/.cache/bazel"
key: bazel
- run: bazel build -c opt //:snmalloc
- run: bazel build -c opt //:snmalloc-rs
- run: bazel build -c opt //:snmalloc-rust-support


# GitHub doesn't natively support *BSD, but we can run them in VMs on Mac /
Expand Down
41 changes: 39 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
load("@rules_cc//cc:defs.bzl", "cc_import")
load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_toolchain")

filegroup(
name = "srcs",
Expand Down Expand Up @@ -38,7 +41,6 @@ CMAKE_FLAGS = {
"SNMALLOC_OPTIMISE_FOR_CURRENT_MACHINE": "ON",
"SNMALLOC_USE_SELF_VENDORED_STL": "OFF",
"SNMALLOC_IPO": "ON",
"USE_SNMALLOC_STATS": "ON",
} | select({
":release_with_debug": {"CMAKE_BUILD_TYPE": "RelWithDebInfo"},
":release": {"CMAKE_BUILD_TYPE": "Release"},
Expand All @@ -65,10 +67,11 @@ cmake(
],
postfix_script = "ninja",
visibility = ["//visibility:public"],
alwayslink = True,
)

cmake(
name = "snmalloc-rs",
name = "snmalloc-rust-support",
cache_entries = CMAKE_FLAGS | {
"SNMALLOC_RUST_SUPPORT": "ON",
},
Expand All @@ -87,5 +90,39 @@ cmake(
"libsnmalloc-new-override.a",
],
postfix_script = "ninja",
visibility = ["//src/snmalloc_rs:__pkg__"],
alwayslink = True,
)

alias(
name = "snmalloc_rs",
actual = "//src/snmalloc_rs",
visibility = ["//visibility:public"],
)

native_binary(
name = "clang",
src = "@llvm_toolchain_llvm//:bin/clang",
visibility = ["//snmalloc_rs:__subpackages__"],
)

cc_import(
name = "libclang",
shared_library = "@llvm_toolchain_llvm//:libclang",
visibility = ["//snmalloc_rs:__subpackages__"],
)

rust_bindgen_toolchain(
name = "rust_bindgen_toolchain",
bindgen = "@rules_rust_bindgen//3rdparty:bindgen",
clang = ":clang",
libclang = ":libclang",
visibility = ["//snmalloc_rs:__subpackages__"],
)

toolchain(
name = "default_bindgen_toolchain",
toolchain = ":rust_bindgen_toolchain",
toolchain_type = "@rules_rust_bindgen//:toolchain_type",
visibility = ["//snmalloc_rs:__subpackages__"],
)
37 changes: 37 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
module(name = "snmalloc")

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "rules_cc", version = "0.1.1")
bazel_dep(name = "rules_foreign_cc", version = "0.14.0")
bazel_dep(name = "fuzztest", version = "20250214.0")
bazel_dep(name = "googletest", version = "1.16.0")
bazel_dep(name = "toolchains_llvm", version = "1.4.0")
bazel_dep(name = "rules_rust", version = "0.61.0")
bazel_dep(name = "rules_rust_bindgen", version = "0.61.0")

# Configure and register the toolchain.
# https://github.com/bazel-contrib/toolchains_llvm/blob/master/toolchain/internal/llvm_distributions.bzl
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency = True)

# LLVM toolchain.
llvm.toolchain(
name = "llvm_toolchain",
cxx_standard = {"": "c++20"},
llvm_version = "17.0.6",
)
use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")

#register_toolchains("@llvm_toolchain//:all")

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust", dev_dependency = True)
rust.toolchain(
edition = "2021",
versions = [
"1.86.0",
],
)
use_repo(rust, "rust_toolchains")

register_toolchains(
"@rust_toolchains//:all",
dev_dependency = True,
)

register_toolchains(
"//:default_bindgen_toolchain",
dev_dependency = True,
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
snmalloc is a high-performance allocator.
snmalloc can be used directly in a project as a header-only C++ library,
it can be `LD_PRELOAD`ed on Elf platforms (e.g. Linux, BSD),
and there is a [crate](https://crates.io/crates/snmalloc-rs) to use it from Rust.
and there is a [crate](https://crates.io/crates/snmalloc_rs) to use it from Rust.

Its key design features are:

Expand Down
2 changes: 1 addition & 1 deletion fuzzing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cc_test(
copts = [
"-fsanitize=address",
] + select({
"@bazel_tools//tools/cpp:clang-cl": ["-fexperimental-library"], # needed for std::execution::unseq,
"@bazel_tools//src/conditions:darwin": ["-fexperimental-library"],
"//conditions:default": ["-mcx16"],
}),
defines = [
Expand Down
65 changes: 65 additions & 0 deletions src/snmalloc_rs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen_library")
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "wrapper",
hdrs = ["wrapper.h"],
defines = ["SNMALLOC_USE_WAIT_ON_ADDRESS"],
linkstatic = True,
visibility = ["//visibility:private"],
deps = ["//:snmalloc-rust-support"],
)

rust_bindgen_library(
name = "snmalloc_sys",
bindgen_flags = [
"--allowlist-function=aligned_alloc",
"--allowlist-function=calloc",
"--allowlist-function=get_malloc_info_v1", # getting mangles
"--allowlist-function=malloc",
"--allowlist-function=malloc_usable_size", # getting mangles
"--allowlist-function=realloc",
"--allowlist-function=free",
# comment out the above and uncomment the two lines below to generate bindings for everything
# "--opaque-type=std::.*",
# "--blocklist-item=std::value",
"--use-core", # don't compile with std, allows baremetal envs
],
cc_lib = ":wrapper",
clang_flags = [
# "-v", # enable for debugging
"-x",
"c++",
"-std=c++20",
"-I",
],
header = ":wrapper.h",
)

rust_library(
name = "snmalloc_rs",
srcs = ["lib.rs"],
visibility = ["//visibility:public"],
deps = [":snmalloc_sys"],
)

# bazel test //src/snmalloc_rs/... --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux
rust_test(
name = "test",
size = "small",
crate = ":snmalloc_rs",
)

# bazel run //snmalloc_rs:main --extra_toolchains=@llvm_toolchain//:cc-toolchain-x86_64-linux
rust_binary(
name = "main",
srcs = ["main.rs"],
rustc_flags = [
"-Ccodegen-units=1",
"-Copt-level=3",
"-Cstrip=symbols",
],
deps = [":snmalloc_rs"],
)
16 changes: 16 additions & 0 deletions src/snmalloc_rs/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 src/snmalloc_rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "snmalloc_rs"
version = "0.0.0"
edition = "2024"
publish = false
homepage = "https://github.com/microsoft/snmalloc"
repository = "https://github.com/microsoft/snmalloc"


[lib]
path = "lib.rs"

[dependencies]
libc = "0.2.172"
Loading
Loading