HTran | connection bouncer , a kind of proxy server | TCP library
kandi X-RAY | HTran Summary
kandi X-RAY | HTran Summary
HTran is a connection bouncer, a kind of proxy server. A “listener” program is hacked stealthily onto an unsuspecting host anywhere on the Internet. When it receives signals from the actual target system, it redirects it to the hacker’s server. HTran have two platform version: Windows and Unix-like(Linux、Unix、etc...) HTran 's Windows version code by lion and bkbll. Ported to Linux/Unix by Hwin.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of HTran
HTran Key Features
HTran Examples and Code Snippets
Community Discussions
Trending Discussions on HTran
QUESTION
In my application, I am using JNA to use the native code written in C. Where I get the notification from native application in callback.
In callback, I get a pointer and some other data to process. In JNA callback code, I have to use this pointer again to call some other native library code and have to pass that pointer. After that I have to return from this callback.
If I don't call that intermediate native library method from callback, which passes the pointer, it works fine, but if I add this call my application crashes intermittently (mostly after processing few hundred of callback requests, sometimes it run for thousands of callbacks also sucessfully).
This NotificationHook
class objects for which hook is set up in native code is a static variable, as there will be only one hook for the application. And native library call this one by one.
ANSWER
Answered 2022-Jan-10 at 21:50Based on the code you've provided, the problem is the same as other Callback-related questions here: you're losing the native allocation of TRANX
due to Java's garbage collection.
A JNA Structure
consists of two parts: a pointer (to data), and the data itself. You have not provided the native typedef for TRANX
to confirm your JNA mapping, but an instantiated object will have an internal pointer reference, pointing to a 4-byte allocation of memory (the int unused
).
You only show the callback code where TRANX
is already an argument, meaning you've already instantiated it to pass to the callback.
If you allocated it yourself using new TRANX()
or new TRANX(int unused)
, then JNA has
- allocated 4 bytes of native memory
- stored the pointer to it internally
In JNA, the native memory attached to a Structure
is automatically freed as a part of the garbage collection process. This is a common problem with callbacks, as you generally don't control the timing of the callback return, so the following sequence occurs:
- You create the object in Java (allocating the native 4 bytes which the
TRANX
structure tracks the pointer to internally) - You pass the
TRANX
object to the callback - Immediately after passing the object, Java no longer has need for its own object; it is unreachable and thus eligible for garbage collection
- When GC occurs the native 4 bytes are freed as part of the process
- The
TRANX
object in the callback still has the pointer internally, but it now points to memory that is no longer allocated, resulting in theSIGSEGV
(or Invalid Memory Access error, or strange symptoms if the memory is allocated by another thread, or other undefined behavior).
The solution to the problem is to track the memory associated with TRANX
.
- If you are allocating it yourself, keep a reference to the
TRANX
object to prevent it from being unreachable.- This generally requires accessing the
TRANX
structure at some later point after you are sure the callback will have been processed - In JDK9+ a
ReachabilityFence
can be used for this. - In JDK8 you should manipulate the class in some way (e.g., read a value from it, or call toString on it, etc.).
- This generally requires accessing the
- If you are using a native allocation and creating the pointer from the
peer
value returned from the native API, then read the API to determine when that memory is freed.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install HTran
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page