Octave malloc error - malloc

I'm getting an error in a script that calls Octave that seems to be a memory leak or something. I have no idea how to debug it, as Octave is called from within the script, not working in Octave itself. The error message is as follows:
octave(9568) malloc: * error for
object 0x1033dd6e0: pointer being
freed was not allocated
* set a breakpoint in malloc_error_break to debug panic:
Abort trap -- stopping myself...
attempting to save variables to
octave-core'... save tooctave-core'
complete
Can anyone suggest how to debug this?

Related

Why does a panic while panicking result in an illegal instruction?

Consider the following code that purposely causes a double panic:
use scopeguard::defer; // 1.1.0
fn main() {
defer!{ panic!() };
defer!{ panic!() };
}
I know this typically happens when a Drop implementation panics while unwinding from a previous panic, but why does it cause the program to issue an illegal instruction? That sounds like the code is corrupted or jumped somewhere unintended. I figure this might be system or code generation dependent but I tested on various platforms and they all issue similar errors with the same reason:
Linux:
thread panicked while panicking. aborting.
Illegal instruction (core dumped)
Windows (with cargo run):
thread panicked while panicking. aborting.
error: process didn't exit successfully: `target\debug\tests.exe` (exit code: 0xc000001d, STATUS_ILLEGAL_INSTRUCTION)
The Rust Playground:
thread panicked while panicking. aborting.
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11: 8 Illegal instruction timeout --signal=KILL ${timeout} "$#"
What's going on? What causes this?
This behavior is intended.
From a comment by Jonas Schievink in Why does panicking in a Drop impl cause SIGILL?:
It calls intrinsics::abort(), which LLVM turns into a ub2 instruction, which is illegal, thus SIGILL
I couldn't find any documentation for how double panics are handled, but a paragraph for std::intrinsics::abort() lines up with this behavior:
The current implementation of intrinsics::abort is to invoke an invalid instruction, on most platforms. On Unix, the process will probably terminate with a signal like SIGABRT, SIGILL, SIGTRAP, SIGSEGV or SIGBUS. The precise behaviour is not guaranteed and not stable.
Curiously, this behavior is different from calling std::process::abort(), which always terminates with SIGABRT.
The illegal instruction of choice on x86 is UD2 (I think a typo in the comment above) a.k.a. an undefined instruction which is paradoxically reserved and documented to not be an instruction. So there is no corruption or invalid jump, just a quick and loud way to tell the OS that something has gone very wrong.

What is the recommended way to propagate panics in tokio tasks?

Right now my panics are being swallowed. In my use case, I would like it to crash entire program and also print the stack trace. How should I configure it?
Panics are generally not swallowed, instead they are returned as an error when awaiting the tokio::task::JoinHandle returned from tokio::task::spawn() or tokio::task::spawn_blocking() and can be handled accordingly.
If a panic occurs within the Tokio runtime an error message is printed to stderr like this: "thread 'tokio-runtime-worker' panicked at 'Panicking...', src\main.rs:26:17". If you run the binary with the environment variable RUST_BACKTRACE set to 1 a stacktrace is printed as well.
As with all Rust programs you can set your own panic handler with std::panic::set_hook() to make it exit if any thread panics after printing the panic info like this:
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
std::process::exit(1);
}));

Rust Embedded panic destroys stack

I'm using Rust on a Cortex-M4 and using gdb with openocd to debug it.
From C(++) I'm used to looking at the call stack when an exception (like a hardfault) happens. It's really helpful to see which line caused the exception.
However, in Rust, when a panic happens, the call stack is almost empty. Why does this happen?
Is there a way to make Rust preserve the stack (only for the debugger, I don't need to print it)? Or can I insert a breakpoint somewhere where the call stack hasn't been destroyed yet?
Right now I have an unwrap somewhere that panics, but I can't find where unless I step through a whole lot of code.
EDIT: This is the stack trace I do get in the panic handler:
i stack
#0 rust_begin_unwind (info=0x2001f810) at src\main.rs:122
#1 0x080219dc in cortex_m::itm::write_fmt (port=0x2001f820, args=...) at C:\Users\d.dokter\.cargo\registry\src\github.com-1ecc6299db9ec823\cortex-m-0.6.1\src/itm.rs:128
#2 0x2001f894 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
It's also weird that the write_fmt function is on the stack as that is being called inside the handler to log the panic. I find that 0x2001f894 address very suspicious as well, because that's a RAM address.
The easiest solution is to set the panic handler to call abort() instead of unwinding the stack. This can be done by adding this to your Cargo.toml
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"
With this setting, the panic handler will immediately call abort(), so gdb can still see the whole backtrace.
If you just want to print the stack trace, you can also set the environment variable RUST_BACKTRACE=1.

What is difference between method process.exit(1) and process.exit(0) in node.js?

In node.js applications i saw usage of both these methods process.exit(1) and process.exit(0). Can anybody give me the exact answer ?
Node normally exits with a 0 status code when no more async operations
are pending. There are other exit codes which are described below:
1 - Uncaught Fatal Exception: There was an uncaught exception, and it was not handled by a domain or an uncaughtException event handler.
2 - Unused: Reserved by Bash for built in misuse.
3 - Internal JavaScript Parse Error: The JavaScript source code internal in Node's bootstrapping process caused a parse error. This is
extremely rare, and generally can only happen during the development
of Node itself.
4 - Internal JavaScript Evaluation Failure: The JavaScript source code internal in Node's bootstrapping process failed to return a
function value when evaluated. This is extremely rare, and generally
can only happen during the development of Node itself.
5 - Fatal Error: There was a fatal unrecoverable error in V8. Typically, a message will be printed to stderr with the prefix FATAL
ERROR.
6 - Non-function Internal Exception Handler: There was an uncaught exception, but the internal fatal exception handler function was
somehow set to a non-function, and could not be called.
7 - Internal Exception Handler Run-Time Failure: There was an uncaught exception, and the internal fatal exception handler function
itself threw an error while attempting to handle it.
8 - Unused
9 - Invalid Argument: Either an unknown option was specified, or an option requiring a value was provided without a value.
10 - Internal JavaScript Run-Time Failure: The JavaScript source code internal in Node's bootstrapping process threw an error when the
bootstrapping function was called. This is extremely rare, and
generally can only happen during the development of Node itself.
11 - Invalid Debug Argument: The --debug and/or --debug-brk options were set, but an invalid port number was chosen
>128 - Signal Exits: If Node receives a fatal signal such as SIGKILL or SIGHUP, then its exit code will be 128 plus the value
of the signal code. This is a standard Unix practice, since exit codes
are defined to be 7-bit integers, and signal exits set the high-order
bit, and then contain the value of the signal code.
Source: https://www.tutorialspoint.com/nodejs/nodejs_process.htm
You can find the answer to your question in the documentation: https://nodejs.org/api/process.html#process_process_exit_code
Basically if you want to exit with success use 0 if you want to exit with failure use 1.
0 is a success code and 1 (or another number) can be a failure code. 0 will be used if nothing is specified. Useful for passing information on the way out. Answered on SO here:
https://stackoverflow.com/a/5266239/5463636
More info direct from the Node.js docs here:
https://nodejs.org/api/process.html#process_process_exit_code

Cocoa thread cannot access Cocoa API

I am starting a new thread from the
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
method, using
MyThread *thread1 = [[MyThread alloc] init] ;
[thread1 start] ;
where MyThread is a subclass of NSThread.
If I run an empty for loop in the thread's main method and quit, it works fine. But as soon as I try to use a Cocoa API such as NSString or even NSAutoReleasePool, my program just hangs by entering the debugger.
What could be the source of the problem ?
[Hint]: I tried stepping thru the debugger and it once gave me a SIGBUS error. What memory access issues could there be ?
The problem was tricky. I was declaring a large char array of 1 MB in the middle of my main() method. The compiler was pushing this declaration to the top of the compiled method. Somehow this space was too large to ask for and hence the program would crash immediately upon entering the main method. Unfortunately, even when I inserted a return statement much before this declaration, the program would crash because the compiler would push this declaration to the very beginning. I changed that array declaration to a malloc() and the program ran.

Resources