{"id":16271193,"url":"https://github.com/ehsanmok/smartalloc-rs","last_synced_at":"2025-09-17T18:31:03.463Z","repository":{"id":59145166,"uuid":"533120486","full_name":"ehsanmok/smartalloc-rs","owner":"ehsanmok","description":"Orphaned memory buffer detector","archived":false,"fork":false,"pushed_at":"2022-09-08T17:34:55.000Z","size":31,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-06T17:14:31.397Z","etag":null,"topics":["allocator","debug-tool","no-std","orphaned-buffer-detector","unsafe-code"],"latest_commit_sha":null,"homepage":"https://docs.rs/smartalloc","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ehsanmok.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-06T01:43:59.000Z","updated_at":"2023-08-14T15:28:19.000Z","dependencies_parsed_at":"2022-09-13T02:13:01.784Z","dependency_job_id":null,"html_url":"https://github.com/ehsanmok/smartalloc-rs","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fsmartalloc-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fsmartalloc-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fsmartalloc-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ehsanmok%2Fsmartalloc-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ehsanmok","download_url":"https://codeload.github.com/ehsanmok/smartalloc-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232789237,"owners_count":18576875,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["allocator","debug-tool","no-std","orphaned-buffer-detector","unsafe-code"],"created_at":"2024-10-10T18:12:49.611Z","updated_at":"2025-09-17T18:30:58.089Z","avatar_url":"https://github.com/ehsanmok.png","language":"Rust","readme":"# Smartalloc-rs\n\n[![Build](https://github.com/ehsanmok/smartalloc-rs/actions/workflows/build.yml/badge.svg)](https://github.com/ehsanmok/smartalloc-rs/actions/workflows/build.yml)\n[![no std](https://img.shields.io/badge/no-std-red)](https://img.shields.io/badge/no-std-red)\n[![crates.io](https://img.shields.io/crates/v/smartalloc.svg)](https://crates.io/crates/smartalloc)\n[![docs.rs](https://docs.rs/smartalloc/badge.svg)](https://docs.rs/smartalloc)\n\nThis crate provides a `no_std` idiomatic Rust binding to [smartalloc](https://www.fourmilab.ch/smartall/) used for\n**detecting orphaned buffer allocation** which is a type of heap memory leak that the program has lost all access to it.\nThe primary usecase is as a *debugging* tool when writing **unsafe** code where normal Rust static checks are not available.\nIt is best used along side [SANs](https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html) where SANs\nalone are either unable to detect or their outputs are cumbersome to work through.\nTo get the best experience, `RUSTFLAGS=-Zsanitizer=leak` is used and is included in `.cargo/config.toml`.\n\n## Usage\n\n```ini\n[dev-dependencies]\nsmartalloc = \"0.2\"\n```\n\nIn fact, with `#![cfg(debug_assertions)]` the crate does **not** compile in the `--release` mode so preventing from any accidental usage.\nThe crate **requires nightly** Rust toolchain (MSRV 1.65).\n\n## Example\n\nDuring debugging, configure the `SmartAlloc` as the global allocator. Then include `sm_dump(true)` at the end of an unsafe code block.\nHere is the [examples/orphan.rs](https://github.com/ehsanmok/smartalloc-rs/blob/main/examples/orphan.rs)\n\n```rust\nuse core::alloc::{GlobalAlloc, Layout};\n\nuse smartalloc::{sm_dump, SmartAlloc};\n\n#[global_allocator]\nstatic GLOBAL: SmartAlloc = SmartAlloc;\n\nfn main() {\n    unsafe {\n        let alloc = SmartAlloc;\n        let layout = Layout::from_size_align(8, 8).unwrap();\n        alloc.alloc(layout); // orphaned memory leak as it's pointer is lost\n                             // and there's no alloc.dealloc(ptr, layout)\n        sm_dump(true);\n    }\n}\n```\n\nwhich outputs\n\n```txt\nOrphaned buffer:       8 bytes allocated at line 12 of examples/orphan.rs\n```\n\n*Note* that the detector throws\n\n```txt\nOrphaned buffer:       5 bytes allocated at line 5 of examples/orphan.rs\nOrphaned buffer:      48 bytes allocated at line 5 of examples/orphan.rs\n```\n\nwhich refers to the `#[global_allocator]` itself and can be ignored.\n\n## Features\n\nThe detector can be turned off using `sm_static(true)` and turned back on `sm_static(false)` to wrap cases where allocation is done through std or safe cases such as [examples/native.rs](https://github.com/ehsanmok/smartalloc-rs/blob/main/examples/native.rs). For more details, checkout the original [docs](https://www.fourmilab.ch/smartall/).\n\n## Aren't SANs alone supposed to detect such errors?\n\nNeither of the `leak/address/memory` [sanitizers](https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html) are sufficient and can detect such errors *easily*.\nIn fact, running\n\n```txt\nRUSTFLAGS=\"-Zsanitizer=leak\" cargo +nightly run --example undetected\n// OR\nRUSTFLAGS=\"-Zsanitizer=address\" cargo +nightly run --example undetected\n```\n\nfor [examples/undetected.rs](https://github.com/ehsanmok/smartalloc-rs/blob/main/examples/undetected.rs) which is\n\n```rust\nunsafe {\n    let alloc = SmartAlloc;\n    let layout = Layout::from_size_align(8, 8).unwrap();\n    alloc.alloc(layout);\n}\n```\n\nwith no `sm_dump(true)` at the end, does not show anything, mainly because we specified\n\n```ini\n[profile.dev]\nopt-level = 0\n```\n\nfor the SmartAlloc to work with introspection as opposed to what has been advised to include (at least `opt-level=1`) [here](https://github.com/japaric/rust-san#unrealiable-leaksanitizer)\nto cirvumvent such a limitation but when is done the context gets destroyed. Also\n\n```txt\nRUSTFLAGS=\"-Zsanitizer=memory -Zsanitizer-memory-track-origins\" cargo +nightly run --example undetected\n```\n\ncannot compile and it throws unhelpful messages\n\n```txt\nerror: failed to run custom build command for `libc v0.2.132`\n\nCaused by:\n  process didn't exit successfully: `/home/workspace/smartalloc-rs/target/debug/build/libc-02d4e594eff5723f/build-script-build` (exit status: 1)\n  --- stdout\n  cargo:rerun-if-changed=build.rs\n\n  --- stderr\n  ==186416==WARNING: MemorySanitizer: use-of-uninitialized-value\n    #0 0x56367729226c  (/home/workspace/smartalloc-rs/target/debug/build/libc-02d4e594eff5723f/build-script-build+0x7a26c) (BuildId: ff090caba1904387acf3f0fecb58801c6fa5caed)\n    #1 0x56367728e95d  (/home/workspace/smartalloc-rs/target/debug/build/libc-02d4e594eff5723f/build-script-build+0x7695d) (BuildId: ff090caba1904387acf3f0fecb58801c6fa5caed)\n    ...\n    Uninitialized value was created by an allocation of '_2' in the stack frame of function '_ZN18build_script_build19rustc_minor_nightly17hfbf53e202478a57bE'\n      #0 0x563677291e70  (/home/workspace/smartalloc-rs/target/debug/build/libc-02d4e594eff5723f/build-script-build+0x79e70) (BuildId: ff090caba1904387acf3f0fecb58801c6fa5caed)\n\n    SUMMARY: MemorySanitizer: use-of-uninitialized-value (/home/workspace/smartalloc-rs/target/debug/build/libc-02d4e594eff5723f/build-script-build+0x7a26c) (BuildId: ff090caba1904387acf3f0fecb58801c6fa5caed)\n    Exiting\n```\n\nso it needs more work!\n\n## Known issue\n\n[smartalloc-sys/csrc/smartall.c](https://github.com/ehsanmok/smartalloc-rs/blob/main/smartalloc-sys/csrc/smartall.c)\nwrites into the passed filename pointer tracked by `#[track_caller]` (which is immutable)\nwhich is an UB that could result into displaying more garbage after the filename in its report using this binding.\n\n## License\n\nLicensed under either of\n\n* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your own will.\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally\nsubmitted for inclusion in the work by you, as defined in the Apache-2.0\nlicense, shall be dual licensed as above, without any additional terms or\nconditions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehsanmok%2Fsmartalloc-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fehsanmok%2Fsmartalloc-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fehsanmok%2Fsmartalloc-rs/lists"}