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

Cannot use alloc_system allocator for binary #44510

Closed
mssun opened this issue Sep 11, 2017 · 7 comments
Closed

Cannot use alloc_system allocator for binary #44510

mssun opened this issue Sep 11, 2017 · 7 comments
Assignees
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. P-high High priority

Comments

@mssun
Copy link
Contributor

mssun commented Sep 11, 2017

According to the the book, we can override default allocator of a binary from jemelloc to system allocator by:

#![feature(alloc_system)]

extern crate alloc_system;

fn main() {
    let a = Box::new(4); // Allocates from the system allocator.
    println!("{}", a);
}

This feature works in previous nightly (back to Rust 1.9.0 and 1.11.0-nightly a967611 2016-05-30) according to this this blog, but it was broken in recent nightly versions.

I try to compile this code with cargo build --release, and here is the result:

$ ls -alh target/release/hello
-rwxrwxr-x 2 mssun mssun 3.9M Sep 11 22:44 target/release/hello

$ strings target/release/hello | grep je_malloc
je_malloc_mutex_prefork
je_malloc_printf
je_malloc_message malloc_message
je_malloc malloc
je_malloc_mutex_postfork_parent
je_malloc_conf malloc_conf
je_malloc_stats_print malloc_stats_print
je_malloc_mutex_unlock
je_mallocx mallocx
je_malloc_mutex_boot
je_malloc_mutex_postfork_child
je_malloc_mutex_init
je_malloc_mutex_lock
je_malloc_usable_size malloc_usable_size
je_malloc_tsd_boot0
je_malloc_tsd_boot1
je_malloc_strtoumax
je_malloc_write
je_malloc_mutex_assert_owner
je_malloc_mutex_assert_not_owner
je_malloc_snprintf
je_malloc_cprintf
je_malloc_tsd_cleanup_register
je_malloc_tsd_malloc
je_malloc_tsd_dalloc
je_malloc_tsd_no_cleanup
je_malloc_vsnprintf
je_malloc_vcprintf
je_malloc_mutex_init
je_malloc_tsd_boot0
je_malloc_printf
je_malloc_mutex_prefork
je_malloc_mutex_postfork_child
je_malloc_mutex_postfork_parent
je_malloc_mutex_boot
je_malloc_cprintf
je_malloc_tsd_boot1
je_malloc_snprintf
je_malloc_write
je_malloc_strtoumax
je_malloc_vsnprintf
je_malloc_tsd_no_cleanup
je_malloc_vcprintf

The binary should not this large, the strings results indicate that je_malloc is still used as the allocator.

Meta

rustc --version --verbose:
rustc 1.22.0-nightly (ca94c75 2017-09-10)
binary: rustc
commit-hash: ca94c75
commit-date: 2017-09-10
host: x86_64-unknown-linux-gnu
release: 1.22.0-nightly
LLVM version: 4.0

@sfackler
Copy link
Member

It looks like the documentation there is unfortunately out of date - this is the way the allocator is selected now: https://github.com/rust-lang/rust/blob/master/src/doc/unstable-book/src/language-features/global-allocator.md.

@mssun
Copy link
Contributor Author

mssun commented Sep 11, 2017

Thanks, @sfackler. Indeed, the documentation is outdated.

@sfackler sfackler added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Sep 11, 2017
@steveklabnik
Copy link
Member

is #![feature(alloc_system)] gone entirely?

@sfackler
Copy link
Member

It's still present for the alloc_system library - it's just that the method of selecting the global allocator changed.

https://github.com/rust-lang/rust/blob/master/src/liballoc_system/lib.rs#L13-L16

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Sep 17, 2017
@lilith
Copy link

lilith commented Oct 21, 2017

Can we please add an informative warning for #![feature(alloc_system)] use? This really bit me.

@steveklabnik steveklabnik added the P-high High priority label Oct 31, 2017
@steveklabnik steveklabnik self-assigned this Oct 31, 2017
@steveklabnik
Copy link
Member

tagging p-high and assigning myself; i think @SimonSapin or someone sent in a PR to address this?

@SimonSapin
Copy link
Contributor

The book’s example became incomplete when #[global_allocator] was introduced. (extern crate became no longer sufficient to select an allocator.) I fixed the example in #45217, I think this can be closed.

The way to select the system allocator is now:

#![feature(alloc_system, global_allocator, allocator_api)]

extern crate alloc_system;

#[global_allocator]
static A: alloc_system::System = alloc_system::System;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. P-high High priority
Projects
None yet
Development

No branches or pull requests

6 participants