bugGNU Octave - Bugs: bug #65767, Inconsistent behavior of %!error...

 
 

bug #65767: Inconsistent behavior of %!error and mexErrMsgIdAndTxt on different architectures

Submitter:  Rafael Laboissière <rlaboiss>
Submitted:  Wed 22 May 2024 08:38:16 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Unexpected Error or Warning
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * 9.1.0 Operating System:  * GNU/Linux
Fixed Release:  9.2.0 Planned Release:  9.2.0
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Thu 20 Jun 2024 06:23:49 AM UTC, comment #14: 

Thank you for testing.

Closing as fixed.

Markus Mützel <mmuetzel>
Group administrator
Thu 20 Jun 2024 05:21:04 AM UTC, comment #13: 

Packages octave 9.2.0-1 and octave-stk 2.8.1-3 are now in Debian unstable. I tested the packages on an armhf box and everything went fine.

This bug report can be closed.

Rafael Laboissière <rlaboiss>
Sun 16 Jun 2024 09:18:17 PM UTC, comment #12: 

comment #11:

> @rlaboiss: Octave 9.2.0 (which should be including a fix for this issue) has been released approx. a week ago.
> Do you still need to manually append `-fexceptions` to the `CFLAGS` when building the stk package?
> Does exception propagation work correctly (without manually adding that flag) on armel, armhf, and mips64el with Octave 9.2.0?


As soon as the Debian package for Octave 9.2.0 will be released, I will check the points above.

Rafael Laboissière <rlaboiss>
Sun 16 Jun 2024 09:55:08 AM UTC, comment #11: 

@rlaboiss: Octave 9.2.0 (which should be including a fix for this issue) has been released approx. a week ago.
Do you still need to manually append `-fexceptions` to the `CFLAGS` when building the stk package?
Does exception propagation work correctly (without manually adding that flag) on armel, armhf, and mips64el with Octave 9.2.0?

Markus Mützel <mmuetzel>
Group administrator
Thu 23 May 2024 02:57:29 PM UTC, comment #10: 

I pushed the patch to the stable branch:
https://hg.savannah.gnu.org/hgweb/octave/rev/60522eca0756

Marking as ready for test.

Markus Mützel <mmuetzel>
Group administrator
Thu 23 May 2024 02:20:16 PM UTC, comment #9: 

Yes, please add the -feceptions flag on stable.

John W. Eaton <jwe>
Group administrator
Thu 23 May 2024 02:00:18 PM UTC, comment #8: 

Perfect. It looks like the patch would fix both issues.

@jwe: Ok to push to the stable branch?

Markus Mützel <mmuetzel>
Group administrator
Thu 23 May 2024 01:57:32 PM UTC, comment #7: 

I get this on armhf:


(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ mkoctfile --mex --output __buggy.mex buggy.c
(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ echo buggy | octave-cli
terminate called after throwing an instance of 'octave::execution_exception'
  what():  __buggy: Failed
fatal: caught signal Aborted -- stopping myself...
Aborted
(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ CFLAGS="$(mkoctfile -p CFLAGS) -fexceptions" mkoctfile --mex --output __buggy.mex buggy.c
(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ echo buggy | octave-cli
error: __buggy: Failed
error: called from
    buggy at line 2 column 3
(sid_armhf-dchroot)rafael@abel:~/bug-1069477$


Rafael Laboissière <rlaboiss>
Thu 23 May 2024 12:07:08 PM UTC, comment #6: 

Thank you for testing.

Does Octave still terminate like before on `echo buggy | octave-cli`? Is that issue also resolved by adding -fexceptions to the CFLAGS?

Anyway, attached is a patch that adds -fexceptions to the XTRA_CFLAGS and FFLAGS so that this flag is added when mkoctfile builds C or Fortran sources.
The flag is also used when building any C or Fortran sources of Octave itself.

I tested with the default branch of Octave (where I couldn't see a negative impact). But it would probably make sense to apply that change on the stable branch.
Would that be ok?


(file #56095)

Markus Mützel <mmuetzel>
Group administrator
Thu 23 May 2024 07:58:11 AM UTC, comment #5: 

Using of -fexceptions makes the problem go away, indeed. On the armhf system:


(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ mkoctfile --mex --output __buggy.mex buggy.c
(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ echo test buggy | octave-cli
terminate called after throwing an instance of 'octave::execution_exception'
  what():  __buggy: Failed
fatal: caught signal Aborted -- stopping myself...
Aborted
(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ CFLAGS="$(mkoctfile -p CFLAGS) -fexceptions" mkoctfile --mex --output __buggy.mex buggy.c
(sid_armhf-dchroot)rafael@abel:~/bug-1069477$ echo test buggy | octave-cli
PASSES 1 out of 1 test


Rafael Laboissière <rlaboiss>
Wed 22 May 2024 06:37:20 PM UTC, comment #4: 

Thanks for the reminder. I forgot about that.

Afaict, we never acted on that discussion. At least, I can't find `-fexceptions` anywhere in the source tree.

Maybe, that would actually help for the problem here.

@rlaboiss: Does adding `-fexceptions` to the CFLAGS when building that .mex file make a difference for you?
E.g., like this:

CFLAGS="$(mkoctfile -p CFLAGS) -fexceptions" mkoctfile --mex --output __buggy.mex buggy.c


Does that still lead to the uncaught exception when you run the following?

echo buggy | octave-cli


Markus Mützel <mmuetzel>
Group administrator
Wed 22 May 2024 01:53:43 PM UTC, comment #3: 

Could this issue be caused by what we discussed here: https://octave.discourse.group/t/exception-handling-with-c-calling-c-code/4909 ?

Did we end up adding -fexceptions to the compiler flags that mkoctfile uses when compiling C files?  If not, would using it avoid the problem?

John W. Eaton <jwe>
Group administrator
Wed 22 May 2024 01:10:25 PM UTC, comment #2: 

Forgot to attach the patch. Here it is.

(file #56075)

Markus Mützel <mmuetzel>
Group administrator
Wed 22 May 2024 01:09:53 PM UTC, comment #1: 

Thank you for the detailed report and the reproducer.

Something doesn't look right indeed. Afaict, calling `buggy` shouldn't terminate the Octave process. Instead, the error that is raised in the .mex file should be handled by the interpreter.

I tried to reproduce the issue. But it seems to work fine for me on Windows 11 (MSYS2) and on Ubuntu 23.10 with a current head of the default branch:

$ echo buggy | octave-cli
error: __buggy: Failed
error: called from
    buggy at line 2 column 3
$ echo test buggy | octave-cli
PASSES 1 out of 1 test


Matlab documents the following:
https://www.mathworks.com/help/matlab/apiref/mexerrmsgidandtxt.html#bqvqxx6-4

> After the error message prints, MATLAB terminates the MEX file and returns control to the MATLAB prompt.


And:
https://www.mathworks.com/help/matlab/matlab_external/compiling-c-mex-files-with-mingw.html#buzqor_-1

> Error handling in C++ MEX files compiled with the MinGW-w64 compiler is not consistent with MATLAB error handling. If a C++ MEX file contains a class, using the mexErrMsgIdAndTxt function to throw a MEX exception can cause a memory leak for objects created for the class.


From that, I gather they are raising a signal in C code (without C++ exception handling).

Does the attached patch make a difference for you?

I don't have access to a armhf system.
On Windows 11 and Ubuntu 23.10 (both are amd64), applying that patch doesn't seem to make a difference.

Markus Mützel <mmuetzel>
Group administrator
Wed 22 May 2024 08:38:16 AM UTC, original submission:  

For the context of this issue, see the Debian bug #1069477. This bug was filed against the octave-stk package of Debian and is related to a failure in a unit test, during package build.

The build logs indicate failures for the architectures armel, armhf, and mips64el:


>>>>> /<<PKGBUILDDIR>>/inst/sampling/stk_sampling_vdc_rr2.m
***** error __stk_sampling_vdc_rr2__()           % two inputs required
terminate called after throwing an instance of 'octave::execution_exception'
  what():  __stk_sampling_vdc_rr2__: Two inputs required.
fatal: caught signal Aborted -- stopping myself...
Aborted


I investigated this issue and concluded that it is not in the stk package per se, but actually something related to the building of the MEX files.

In order to illustrate the problem, I prepared a tarball, attached to this bug report, containing three files: buggy.m, buggy.c, and Makefile.

The buggy.c file contains the following:

    #include “mex.h

    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {
       mexErrMsgIdAndTxt(“dummy”, “Failed”);
       mexErrMsgIdAndTxt(“dummy”, “End of mex function\n”);
    }

It must be compiled to obtain a __buggy.mex file, like this:

    mkoctfile --mex --output __buggy.mex buggy.c

The buggy.m file contains the following:

    function buggy
        __buggy();
    endfunction

    %!error buggy()

When the buggy function is called, it should produce an error, on all architecture, like this:

    $ echo buggy | octave-cli
    terminate called after throwing an instance of 'octave::execution_exception'
      what():  __buggy: Failed
    fatal: caught signal Aborted -- stopping myself...
    Aborted

When the buggy.m unit test is run, it should yield:

    $ echo test buggy | octave-cli
    PASSES 1 out of 1 test

At least, that's what I'm seeing on my amd64 system. However, when I run the last command on an armhf system, I get this:

    echo test buggy | octave-cli
    terminate called after throwing an instance of 'octave::execution_exception'
      what():  __buggy: Failed
    fatal: caught signal Aborted -- stopping myself...
    Aborted

As you can see, the behavior is not the same depending on the system architecture.

P.S.: Note that when the buggy.c file contains a single call to mexErrMsgIdAndTxt, everything goes fine on all architectures.

Rafael Laboissière <rlaboiss>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #56095:  bug65767-add-fexceptions-flags.patch added by mmuetzel (1KiB - application/octet-stream)
file #56075:  bug65767-mex-raise-signal-on-error.patch added by mmuetzel (3KiB - application/octet-stream)
file #56074:  bug-1069477.tgz added by rlaboiss (533B - application/x-compressed-tar)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by mmuetzel (Posted a comment)
  • -email is unavailable- added by rlaboiss (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 11 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-06-20 mmuetzel Open/ClosedOpen Closed
    2024-06-20 mmuetzel StatusReady For Test Fixed
        Fixed ReleaseNone 9.2.0
    2024-05-23 mmuetzel StatusPatch Submitted Ready For Test
    2024-05-23 mmuetzel Attached File- Added bug65767-add-fexceptions-flags.patch, #56095
        StatusNeed Info Patch Submitted
        Planned ReleaseNone 9.2.0
    2024-05-22 mmuetzel Attached File- Added bug65767-mex-raise-signal-on-error.patch, #56075
    2024-05-22 mmuetzel CategoryOther Interpreter
        StatusNone Need Info
    2024-05-22 rlaboiss Attached File- Added bug-1069477.tgz, #56074

    Back to the top

    Powered by Savane 3.13-397b.
    Corresponding source code