jre crash when loading jni - java-native-interface

I have compiled a test dll for use with jni. It is actually completely empty except for #include<jni.h>. It compiles fine. I commented out everything else to try and get it working. I used gcc cygwin version and -shared and eclipse.
This is the class that loads the library:
static
{
final File f= new File(new File("res"), "mandc.dll");
System.out.println(f.exists());
System.load(f.getAbsolutePath());
System.out.println("Loaded!");
}
// public static native long mand(final double cx, final double cy,
// final double jx, final double jy, long iter);
public static native void mand();
true is the last thing that prints, proving that the error is between true and loaded!.
If I run from within eclipse, two error messages print, but the internet does not know what they are.
the messages:
2 [main] javaw <random number here> exception::handle: Exception: STATUS_STACK_OVERFLOW
667 [main] javaw <random number here> open_stackdumpfile: Dumping stack trace to javaw.exe.stackdump
the stack dump is uninformative.
Exception: STATUS_STACK_OVERFLOW at eip=61157C62
eax=0001C038 ebx=49E0834C ecx=49DE2ABC edx=49E27DB4 esi=49E07B2C edi=49E27B34
ebp=49E07ACC esp=49E07AB4 program=C:\Program Files\Java\jre7\bin\javaw.exe, pid 2688, thread main
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame Function Args
49E07ACC 61157C62 (49E2D000, 49E27B3A, 49E27DB4, 00000008)
49E07AFC 6106C0B5 (49E27B3A, 49E27D78, 00000008, 49E27B2C)
49E27B4C 6106C6D1 (49E27B70, 00000C90, 00000000, 49E27DB4)
49E27DEC 6100584E (49E27EBC, 611FBAF0, 611FBAEC, 49E27E2C)
49E27E3C 61005D28 (49E27FC3, 611FBAF0, 611FBAEC, 00000001)
49E2801C 61006F07 (00000000, 49E28058, 61006990, 49E2B268)
End of stack trace
The experiment also fails when I run the example from the wikipedia page. And it fails the same way.

Problem fixed. I switched toolchains from the cygwin gcc to the mingw gcc. To support this, I had to install extra mingw packages from the cygwin install program.
I guess that cygwin is incompatible with java. A better error message would have been appreciated.

Related

C++ DLL behaviour differ between py3.6 & py3.8 : SIGSEGV "__gnu_cxx::__exchange_and_add" with Python 3.6.6

I have a project running on Python 3.8 x64 windows, with an anaconda environment.
It uses an inhouse C++ DLL x64, with a python wrapping using ctypes (the C++ code cannot be shared due to various reasons; the most important reason is that I do not have access to 99% of the code myself ...).
The project works perfectly fine with Python 3.8 x64.
The main C++ DLL entry point takes a string path to a JSON file for initialization.
The ctypes binding code looks like this:
class RedactedName:
def __init__(some_args): # redacted
# some code
self.dll = ctypes.CDLL(self.dll_path)
self.dll.load_config_file.argtypes = [ctypes.c_char_p]
self.dll.load_config_file.restype = ctypes.c_bool
# some code
def load_config_file(self, file_path: str) -> bool:
self.conf_file_path = file_path # path of a JSON file, the C++ will load it
s = ctypes.c_char_p(file_path.encode('utf-8'))
b = self.dll.load_config_file(s) # <-- SEG FAULT HERE with py3.6.6 only. ok with py3.8
return bool(b)
The corresponding C++ code : (namely, the 1% part I am 100% sure of)
bool load_config_file(/* in */ const char * input_json) {
json conf;
const std::string & json_file_path_str(input_json);
REDACTED_FUNCTION_CALL_A(/* in */ json_file_path_str, /* out */ conf); // <-- seg fault in here, depending on python version, see GDB call stack
// other stuff
return true; // no joke
}
For various reasons, I need to downgrade the Python version to Python 3.6.6 x64 (with pip only; if that matters).
With Python 3.6.6 x64, the C++ call self.dll.load_config_file goes into SIGSEGV.
The C++ DLL call stack looks like this (luckily the DLL contains some symbols).
# (with python breakpoint right before DLL load)
$ attach <ptyhon process PID>
$ catch load mylibname
$ catch throw
$ c
# now, resume python execution
# when gdb breaks on dll load, add breakpoint
$ b load_config_file
exception catched
(gdb) n
Single stepping until exit from function
_ZN3REDACTED5REDACTED8REDACTED4REDACTEDKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERN8nlohmann10basic_jsonISt3mapSt6vectorS7_bxydSaNSA_14adl_serializerEEE,
which has no line number information.
Thread 1 received signal SIGSEGV, Segmentation fault.
0x000000006fcd2194 in __gnu_cxx::__exchange_and_add (__val=-1, __mem=0x0) at /it/sbx/20.1/x86_64-windows/gcc/build/x86_64-pc-mingw32/libstdc++-v3/include/ext/atomicity.h:82
82 /it/sbx/20.1/x86_64-windows/gcc/build/x86_64-pc-mingw32/libstdc++-v3/include/ext/atomicity.h: No such file or directory.
backtrace :
#0 0x000000006fcd2194 in __gnu_cxx::__exchange_and_add (__val=-1, __mem=0x0) at /it/sbx/20.1/x86_64-windows/gcc/build/x86_64-pc-mingw32/libstdc++-v3/include/ext/atomicity.h:82
#1 __gnu_cxx::__exchange_and_add_dispatch (__val=-1, __mem=0x0) at /it/sbx/20.1/x86_64-windows/gcc/build/x86_64-pc-mingw32/libstdc++-v3/include/ext/atomicity.h:82
#2 __gnu_cxx::__exchange_and_add_dispatch (__val=-1, __mem=0x0) at /it/sbx/20.1/x86_64-windows/gcc/build/x86_64-pc-mingw32/libstdc++-v3/include/ext/atomicity.h:78
#3 std::locale::_Impl::_M_remove_reference (this=0x0) at /it/sbx/20.1/x86_64-windows/gcc/build/x86_64-pc-mingw32/libstdc++-v3/include/bits/locale_classes.h:564
#4 std::locale::operator= (this=this#entry=0xbc8f3ecf50, __other=
#0xbc8f3ec9f8: {static none = 0, static ctype = 1, static numeric = 2, static collate = 4, static time = 8, static monetary = 16, static messages = 32, static all = 63, _M_impl = 0x6fcfc6a0 <(anonymous namespace)::c_locale_impl>, static _S_classic = 0x6fcfc6a0 <(anonymous namespace)::c_locale_impl>, static _S_global = 0x6fcfc6a0 <(anonymous namespace)::c_locale_impl>, static _S_categories = 0x6fd06aa0 <__gnu_cxx::category_names>, static _S_once = {done = 1, started = 0}, static _S_twinned_facets = 0x6fd09140 <std::locale::_S_twinned_facets>}) at ../../../../../src/libstdc++-v3/src/c++98/locale.cc:116
#5 0x000000006fce906c in std::ios_base::_M_init (this=this#entry=0xbc8f3ece80) at ../../../../../src/libstdc++-v3/src/c++98/ios_locale.cc:44
#6 0x000000006fceaf91 in std::basic_ios<char, std::char_traits<char> >::init (this=0xbc8f3ece80, __sb=0x0) at /it/sbx/20.1/x86_64-windows/gcc/build/x86_64-pc-mingw32/libstdc++-v3/include/bits/basic_ios.tcc:126
#7 0x000000006ce0b7fb in REDACTED_FUNCTION_CALL_A(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long long, unsigned long long, double, std::allocator, nlohmann::adl_serializer>&) ()
from C:\Users\<redacted>/<redacted>_lib.dll
#8 0x000000006cdc15c6 in load_config_file (input_json=0x27eee2877a0 "C:\\Users\\<a_valid_path_to_avalid_json_file>") at <redacted>
#9 0x0000000062880943 in _ctypes!DllCanUnloadNow () from C:\Program Files\Python36\DLLs\_ctypes.pyd
Additional informations:
The python calls are purely sequential, there is no threading, so the s variable cannot be garbage collected before the end of the self.dll.load_config_file call.
The C++ DLL has not been recompiled. It is the exact same as in the Py3.8 environment.
I can switch back and forth from py3.6 to 3.8 envs. 3.6 fails, 3.8 works.
the C++ DLL depends on libwinpthread-1.dll, libgcc_s_seh-1.dll, libstdc++-6.dll
DLL was built with GNAT PRO g++ (GCC) 8.3.1 20190923 (for GNAT Pro 20.1)
DLL compilation directive contains: "-std=c++11", "-O0"
DLL link directive contains "-static-libgcc"
Any clue of why py3.6.6 gives this weird cpp call stack & SIGSEGV and py3.8 not ?
If the py3.8 version wasn't working, I would suppose it is a duplicate of undefined reference to `__gnu_cxx::__exchange_and_add(int*, int)'.
But it is not the case ...
Hypothesis:
std::basic_ios<char, std::char_traits<char> >::init seems related to stream buffer initialization. Could it be some conflicts of std output stream ? very unlikely
python3.6 messing with env vars ? very unlikely
python3.6 messing with DLLs, creating dependancy conflicts ? unlikely
As far as I can tell, these questions are NOT related because function return type is bool and accurate with source code. And py3.8 works.
Porting a ctypes Library Call from Python 2.7 to 3.8
Python ctypes: Access violation
Python changelogs
3.6
3.7
3.8
Update 2021-11-04 1
~~ I have found a potential root cause.~~
I used catch load my_lib and catch load libstdc++ to be sure which libs were loaded.
When using the Py3.8 anaconda environment, my C++ DLL is loaded (first catch), and then it loads libstdc++-6.dll that is stored right next to it (second catch) (can't tell the version, thats the magic of black box binary deliveries ...).
When using the pip venv Py3.6, my C++ DLL is loaded (first catch), and then loads libstdc++-6.dll coming from my GNAT install (second catch).
So far, this may be the major difference I can spot.
I am pretty sure the 2 libstdc++ have different version.
I do not understand why library loading precedence differs between the two versions.
Content of python os.environ output is slightly different between the two envs, mainly due to the env var LIBRARY_ROOTS pointing to the respective python install lib folders (In both cases PATH contains the path to the gnat pro install bin dir).
Update 2021-11-04 2
When trying different libstdc++-6.dll substitutions to ensure both python env version use the exact same one from the same path, it appears the libstdc++-6.dll version has no effect on the observed behaviour.
It looks like that the Python version changes the DLL execution.
(C++ snipped updated to reflex the char* usage)

Printing a double to std::cout results in Segmentation fault (C++)

I am new to C++ and want to print out a double value. It is not that I actually need to print that value, I just want to know what is going wrong here.
This is my code (HelloWorld.cpp):
#include <iostream>
int main() {
double i = 5.5;
std::cout << i << std::endl;
return 0;
}
Executing this with the debugger attached results in the following error:
Thread 1 hit Breakpoint 1, main () at src/HelloWorld.cpp:4
4 double i = 5.5;
Thread 1 received signal SIGSEGV, Segmentation fault.
0x00000000929ba260 in ?? ()
When I put a breakpoint in there, creating and assigning the variable is no problem. The error only occurs once the program is supposed to print that value. Executing the exe without the debugger results in no output at all. The same happens when I replace the double with a long double or float. Printing anything else works fine (Strings, char, int, short, etc.).
I am using Visual Studio Code and MinGW (x86_64-8.1.0-posix-seh-rt_v6-rev0). VS Code is using following files for compilation / debugging:
c_cpp_properties.json
launch.json
tasks.json
And here you can see the complete output, in case that helps.
Any Idea what I am doing wrong here? Thank you.
EDIT:
When compiling manually using g++ -g .\src\HelloWorld.cpp -std=c++11 -o HelloWorld.exe (or just g++ .\src\HelloWorld.cpp -o HelloWorld.exe) and running that from the console, the same happens (no output).
I installed MinGW from here using the following settings:
Version: 8.1.0
Architecture: x86_64
Threads: posix
Exception: seh
Build revision: 0
EDIT 2:
Found the problem. There was an old version of gcc lurking in my PATH (maybe from Visual Studio or MSSQL Server?). I just move the current gcc to the top of PATH and now it's working fine. Thank you all for your help!
As many pointed out, this should usually work. The problem was with my setup: I had an old version of gcc somewhere in my PATH variable (version 4.1). Moving the path of the newer version to the beginning of PATH resolves the issue. Thank you all for helping.
To check weather the same happens to you you can do the following: execute g++ --version in your project directory. Compare this with the output of g++.exe --version when you are in the directory where gcc is installed (for me, this was C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin).

Gtk::AboutDialog crash when clicking on URL

I am working with GTKmm 3, MSYS2, MinGW (GCC 5.3.1) under Windows 7.
I made a Glade interface with a normal window and a Gtk::AboutDialog.
All is working OK, but when opening the About dialog and clicking on its URL link, then the next error occours and the program exits:
(GladeTest1.exe:3440): GLib-CRITICAL **: unquote_string_inplace: assertion 'err == NULL || *err == NULL' failed
This is the code:
Gtk::Window *window1;
Gtk::AboutDialog *aboutDialog1;
builder->get_widget("window1", window1);
builder->get_widget("aboutdialog1", aboutDialog1);
aboutDialog1->set_transient_for(*window1);
aboutDialog1->show();
Using Dr. Mingw debugger I got this traces:
GladeTest1.exe caused an Access Violation at location 000000000070255A in module libgtk-3-0.dll Reading from location 0000000000000008.
Loading symbols... done.
Registers:
eax=00000000 ebx=03d5c1a8 ecx=00000000 edx=00000001 esi=024d8020 edi=00000016
eip=0070255a esp=0028ea9c ebp=0028eb54 iopl=0 nv up ei pl nz ac po nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00210216
AddrPC Params
0070255A 02514D30 03B36FA0 0250FB00 libgtk-3-0.dll!gtk_search_entry_handle_event
0082845C 0257C900 0028EC84 00000002 libgtk-3-0.dll!gtk_main_do_event
63C45FD2 0028ED9C 0028ED00 0028EDBC libgobject-2.0-0.dll!g_closure_invoke
63C5F31A 025025A8 025297B0 00000000 libgobject-2.0-0.dll!g_signal_emit_valist
6883689B 025297B0 00000000 02514D30 libglib-2.0-0.dll!g_mutex_unlock
025025A8 00000000 02514D30 00000000
025297B0 02514D30 00000000 00000000
It seems like there is some problem with libgtk-3-0.dll or libglib DLL.
I am looking for an answer for 2 days without success.
Something important is missing?
Anybody has a clue?
Thanks
We had the same errors/crashes under WinXP64 and Win7/G4 with MSys2/MingW 6.2.0 using Gtk+ ( i.e. Gtk2).
However, when we compiled/build our own Gtk/Gdk libs via MingW, exactly the same Gtk_About dialog's Http link worked.
... this seems to point the finger at something in the MSys2 build. Indeed, when trying to build Gtk following the MSys2 "pkg build directions" also fails (at least for us).
... we haven't had too much "luck" communicating with the MSys2 people.
... Instead, we use the so-called "Ingar Build" approach found here (http://ingar.satgnu.net/devenv/mingw32/gtk.html). His instructions are for the entire build from the ground up, which is a few day's work, but it is very much worth it for us ... and provides massively more control and flexibility compared to MSys2.
P.S. You will need to make a few adjustments to Ingar's instructions for Gtk3. Also, if you build the packages with versions much later/newer compared to those on Ingar's site, additional packages will be required (due to the additional dependencies in newer versions).

Unable to run Woden Physics Example in Pharo

I am trying to run the Woden Physics Example inside Pharo which involves getting Bullet properly compiled and the smalltalk bindings properly installed in Pharo.
I am using Linux Mint 17 x64.
But NativeBoost seems unable to load the compiled libraries. I have been using the sources provided here:
https://github.com/ronsaldo/bullet-pharo
https://github.com/ronsaldo/swig
I built the modified version of swig as well as the bullet libraries and bindings with the provided build scripts.
I also have doublechecked that the bullet libraries are 32 bit.
Opening up the Woden physics example returns this error:
failed to get a symbol address:
PharoNB_new_BTDefaultCollisionConfiguration__SWIG_1
When examining the call stack in the debugger, it turns out that the module handle is 0.
I verified this by executing the same message as
BulletCInterface nbLibraryNameOrHandle
executes:
NativeBoost forCurrentPlatform loadModule: 'BulletPharo'
This message returns 0. I tried to specify the full path to libPharoBullet.so in the workspace, like:
NativeBoost forCurrentPlatform loadModule:
'/home/martin/.local/share/Pharo/bullet-pharo/libBulletPharo.so'
with the same result. I also verified it with a 32 bit system library of mine (liblzma) and there NativeBoost was able to load it, as it returned a non-zero handle.
So i suspect something during compilation went wrong...
I also did
readelf -h libPharoBullet.so
and its ABI was "UNIX - GNU" while the ABI of pharo-vm is "UNIX - System V"
Could this be the problem here ?
How can i force the ABI to be System V when compiling ? I use gcc 4.8.2
Or what steps could i otherwise perform ?

Win32 API stack walk with MinGW/MSYS?

i've to develop a stack trace walk to our win32 API plattform support to catch any exception with a behaviour of a JAVA stacktrace. Actually we got a full functional implementation for Linux plattforms.
First i've tried to implement a stackwalk using the win32 CaptureStackBackTrace API
mechanism. But this method is not integrated in the actually winbase header of mingw (using MSYS/MinGW 5.1.x) ...
So i decided tried use the dbgheader mechanism follwoing the instructions of this link :
http://sites.google.com/site/kenscode/prime-programs/boinc-with-mingw-on-win32
But i fail again and run into linker failures i could not solve. I think MinGW could not resolve the dbgheader library ....
DrMinGW is not an option for me, while it is a JIT Debugger, i've to implement a stack trace mechansim, for any exception occuring on runtime with a customizable log file tracebility like we know from JAVA ...
Has anyone get MSYS/MinGW runable with the win32 API in handshake? I will not change the compiler to the ugly MVC compiler instead of using MSYS/MinGW...
Thanks for any hint.
Best regards,
Christian
Check Mr. Edd's stack trace library at the following link. It will produce a nice stack frame listing and has specific code to support MinGW.
http://www.mr-edd.co.uk/code/stack_trace
His library uses dbghelp.dll, however, so you may get into some problems trying to compile it. As far as I know, MinGW doesn't include an import library for this DLL (see a old feature request here). I had success, however, creating one import library myself. You can do the same just using a .def file from the Wine project (check the previous link's attached files for one) and running the MingW utility dlltool:
dlltool -k -d dbghelp.def -l dbghelp.a
You can then include the resulting dbghelp.a file in your project. You won't probably like to have dependencies towards dbghelp.dll in your release builds, as the DLL itself is surely not redistributable.
Here is a method to walk the call stack using the Win32 API which you can call from MinGW.
http://www.codeproject.com/KB/threads/StackWalker.aspx
I got stack traces working in MingGW with Edd's dbg library, which is a successor to his minimal stack_trace library:
With msys2, this should get you a stack trace:
$ pacman -S mingw-w64-x86_64-edd-dbg
// main.cpp
#include <dbg/frames.hpp>
#include <dbg/symbols.hpp>
#include <iostream>
int main()
{
dbg::symdb db;
dbg::call_stack<64> traceback;
traceback.collect(0);
traceback.log(db, std::cout);
return 0;
}
$ g++ -ggdb main.cpp -ldbg
$ ./a.exe
0x0000000000402ee9: dbg::call_stack<64u>::collect(unsigned int) in C:\msys64\home\phil\stacktrace-example\a.exe
0x00000000004015f2: main in C:\msys64\home\phil\stacktrace-example\a.exe
0x00000000004013f8: __tmainCRTStartup in C:\msys64\home\phil\stacktrace-example\a.exe
0x000000000040151b: mainCRTStartup in C:\msys64\home\phil\stacktrace-example\a.exe
0x00007ffbb0838102: BaseThreadInitThunk in C:\WINDOWS\system32\KERNEL32.DLL
0x00007ffbb27cc5b4: RtlUserThreadStart in C:\WINDOWS\SYSTEM32\ntdll.dll
More about dbg can be found in the Wiki. The code is available here: https://bitbucket.org/edd/dbg/src