code icon indicating copy to clipboard operation
code copied to clipboard

ch6/ch6-particles does not compile on MacOS with `zsh: illegal hardware instruction cargo run`

Open Nsandomeno opened this issue 3 years ago • 8 comments

OS: MacOS

When in the root ch6-particles crate created with cargo, the following error is generated:

zsh: illegal hardware instruction  cargo run

Oddly, the programs behaves as expected (well, without the standard output) when commenting out the eprintln! line.

From my findings so far this appears to an OS specific issue and any suggestions would be much appreciated!

Nsandomeno avatar Dec 07 '22 03:12 Nsandomeno

Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to eprintln!. Borrowed the run_guarded function from here and wrapped my calls to eprintln! with that. Worked like a charm.

pjstein avatar Dec 22 '22 22:12 pjstein

Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to eprintln!. Borrowed the run_guarded function from here and wrapped my calls to eprintln! with that. Worked like a charm.

Thank you, @pjstein ! I did the same. Any Idea why we ran into this? Just a little catch when using MacOS perhaps?

Nsandomeno avatar Dec 24 '22 15:12 Nsandomeno

Could you guys please post a snip? That'd help the newbie here :-) Thanks!

undavide avatar Jan 05 '23 18:01 undavide

I went ahead and just applied the workaround as suggested by @pjstein https://github.com/rust-in-action/code/pull/106

ckoopmann avatar Jan 30 '23 04:01 ckoopmann

Thanks @ckoopmann @pjstein @Nsandomeno @undavide worked like a charm

leshec avatar Apr 05 '23 10:04 leshec

Thanks for the solution, and this is what I found: Does the println macro allocate heap memory?.

livexia avatar May 25 '23 15:05 livexia

Just pasting the snippet here from: https://github.com/andrewhickman/logging-allocator/blob/master/src/lib.rs#L42-L57

We need to add a new function

pub fn run_guarded<F>(f: F)
where
    F: FnOnce(),
{
    thread_local! {
        static GUARD: Cell<bool> = Cell::new(false);
    }

    GUARD.with(|guard| {
        if !guard.replace(true) {
            f();
            guard.set(false)
        }
    })
}

and then use it to wrap the eprintln! on the alloc function

    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let start = Instant::now();
        let ptr = System.alloc(layout);
        let end = Instant::now();
        let time_taken = end - start;
        let bytes_requested = layout.size();

        run_guarded(|| { 
            eprintln!("{}\t{}", bytes_requested, time_taken.as_nanos());
        });
        ptr
    }

edzzn avatar Nov 13 '23 00:11 edzzn

Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to eprintln!. Borrowed the run_guarded function from here and wrapped my calls to eprintln! with that. Worked like a charm.

Thank you, @pjstein ! I did the same. Any Idea why we ran into this? Just a little catch when using MacOS perhaps?

While I was unaware of this thread, I found this from the Rust Lang repo.

sm13 avatar Aug 31 '24 13:08 sm13