ch6/ch6-particles does not compile on MacOS with `zsh: illegal hardware instruction cargo run`
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!
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.
Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to
eprintln!. Borrowed therun_guardedfunction from here and wrapped my calls toeprintln!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?
Could you guys please post a snip? That'd help the newbie here :-) Thanks!
I went ahead and just applied the workaround as suggested by @pjstein https://github.com/rust-in-action/code/pull/106
Thanks @ckoopmann @pjstein @Nsandomeno @undavide worked like a charm
Thanks for the solution, and this is what I found: Does the println macro allocate heap memory?.
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
}
Hey @Nsandomeno, I ran into this as well and found that I could work around it by guarding the calls to
eprintln!. Borrowed therun_guardedfunction from here and wrapped my calls toeprintln!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.