<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://jogamp.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.2"
          urlbase="https://jogamp.org/bugzilla/"
          
          maintainer="sgothel@jausoft.com"
>

    <bug>
          <bug_id>1468</bug_id>
          
          <creation_ts>2023-10-01 19:53:20 +0200</creation_ts>
          <short_desc>SIGSEGV on use after free when destroying NEWT Window/Display via a native dispatch&apos;ed event like key/mouse/touch input</short_desc>
          <delta_ts>2023-10-02 19:43:40 +0200</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>JogAmp</classification>
          <product>Newt</product>
          <component>core</component>
          <version>2.6.0</version>
          <rep_platform>All</rep_platform>
          <op_sys>all</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>major</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Sven Gothel">sgothel</reporter>
          <assigned_to name="Sven Gothel">sgothel</assigned_to>
          
          
          <cf_type>DEFECT</cf_type>
          <cf_scm_refs>f842843df2c77f5badaace6858d3336151ce0827</cf_scm_refs>
          <cf_workaround>---</cf_workaround>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>6905</commentid>
    <comment_count>0</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2023-10-01 19:53:20 +0200</bug_when>
    <thetext>SIGSEGV on use after free of native X11 Display* at XEventsQueued in DisplayDriver.DispatchMessages0.

This potentially happens when an application destroys 
the NEWT Window/Display from an action being called directly
from DisplayDriver.DispatchMessages0 (itself), i.e. keyboard or mouse input.

DisplayDriver.DispatchMessages0 stays in the event loop and the next
XEventsQueued call causes a SIGSEGV due to already deleted 
display driver connection and hence invalid native X11 Display*.

Perhaps mitigate by asking the JavaObject DisplayDriver whether 
it is still alive - since we might have not other means to validate 
the X11 DisplayDriver* after free.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>6906</commentid>
    <comment_count>1</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2023-10-02 13:09:02 +0200</bug_when>
    <thetext>This issue may also exist for other Windowing System drivers,
where the native (dispatch) method sticks to a loop
and still (re)uses the window or display handle.

One is WindowsWindow, where touch events are looped,
but such handler could have closed the window.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>6907</commentid>
    <comment_count>2</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2023-10-02 13:55:50 +0200</bug_when>
    <thetext>Querying the status of a window / display instance before dispatching
might not be good enough
- resource could already be GC&apos;ed, so we also would need to query jobject status
- would imply an addition Java callback

+++

Instead, let&apos;s do the sweaty work of having the Java callbacks return
a boolean with the value Window.isNativeValid().

This way the dispatch logic
- can bail out right away w/o using the resource anymore

- must be reviewed by myself due to changed Call{Void-&gt;Boolean}*(..) 
  invocation change.
  This review shall resolve potential similar issues.

...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>6908</commentid>
    <comment_count>3</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2023-10-02 19:43:40 +0200</bug_when>
    <thetext>commit f842843df2c77f5badaace6858d3336151ce0827

...

This fix: Having the Java callbacks return
a boolean with the value Window.isNativeValid().
    
This way the dispatch logic
- can bail out right away w/o using the resource anymore
    
- must be reviewed by myself due to changed Call{Void-&gt;Boolean}*(..)
  invocation change.
  This review shall resolve potential similar issues.
    
+++ 
    
Tested on X11/Linux/GNU, Windows and MacOS
with new TestDestroyGLAutoDrawableNewtAWT,
which tests all destruction invocation variants.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>