undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,262 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @key headful
* @bug 6980209
* @summary Make tracking SecondaryLoop.enter/exit methods easier
* @author Semyon Sadetsky
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Logger;
public class bug6980209 implements ActionListener {
private final static Logger log =
Logger.getLogger("java.awt.event.WaitDispatchSupport");
public static final int ATTEMPTS = 100;
public static final int EVENTS = 5;
private static boolean runInEDT;
private static JFrame frame;
private static int disorderCounter = 0;
private static Boolean enterReturn;
private static Boolean exitReturn;
private static int dispatchedEvents;
private static JButton button;
private static Point point;
public static void main(String[] args) throws Exception {
System.out.println(
"PLEASE DO NOT TOUCH KEYBOARD AND MOUSE DURING THE TEST RUN!");
// log.setLevel(java.util.logging.Level.FINE);
// log.setLevel(java.util.logging.Level.FINEST);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
frame = new JFrame();
frame.setUndecorated(true);
setup(frame);
}
});
final Robot robot = new Robot();
robot.delay(100);
robot.waitForIdle();
robot.setAutoDelay(10);
robot.setAutoWaitForIdle(true);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
point = button.getLocationOnScreen();
}
});
robot.mouseMove( point.x + 5, point.y + 5 );
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(100);
robot.waitForIdle();
testExitBeforeEnter();
System.out.println("Run random test in EDT");
runInEDT = true;
testRandomly();
System.out.println("Run random test in another thread");
runInEDT = false;
testRandomly();
System.out.println("ok");
} finally {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame.dispose();
}
});
}
}
private static void testExitBeforeEnter() throws Exception {
final SecondaryLoop loop =
Toolkit.getDefaultToolkit().getSystemEventQueue()
.createSecondaryLoop();
loop.exit();
Robot robot = new Robot();
robot.mouseWheel(1);
robot.waitForIdle();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
if(loop.enter()) {
throw new RuntimeException("Wrong enter() return value");
}
}
});
}
private static void testRandomly() throws AWTException {
disorderCounter = 0;
final Robot robot = new Robot();
robot.setAutoDelay(1);
for (int i = 0; i < ATTEMPTS; i++) {
enterReturn = null;
exitReturn = null;
dispatchedEvents = 0;
synchronized (bug6980209.class) {
try {
for (int j = 0; j < EVENTS; j++) {
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
}
// trigger the button action that starts secondary loop
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
for (int j = 0; j < EVENTS; j++) {
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
}
long time = System.nanoTime();
// wait for enter() returns
bug6980209.class.wait(1000);
if (enterReturn == null) {
System.out.println("wait time=" +
((System.nanoTime() - time) / 1E9) +
" seconds");
throw new RuntimeException(
"It seems the secondary loop will never end");
}
if (!enterReturn) disorderCounter++;
robot.waitForIdle();
if (dispatchedEvents <
2 * EVENTS) { //check that all events are dispatched
throw new RuntimeException(
"KeyEvent.VK_1 has been lost!");
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted!");
}
}
}
if (disorderCounter == 0) {
System.out.println(
"Zero disordered enter/exit caught. It is recommended to run scenario again");
} else {
System.out.println(
"Disordered calls is " + disorderCounter + " from " +
ATTEMPTS);
}
}
private static void setup(final JFrame frame) {
button = new JButton("Button");
frame.getContentPane().add(button);
button.addActionListener(new bug6980209());
frame.pack();
frame.setVisible(true);
button.setFocusable(true);
button.requestFocus();
button.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == '1') dispatchedEvents++;
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyChar() == '1') dispatchedEvents++;
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (runInEDT) {
runSecondaryLoop();
return;
}
new Thread("Secondary loop run thread") {
@Override
public void run() {
runSecondaryLoop();
}
}.start();
}
private static void runSecondaryLoop() {
log.fine("\n---TEST START---");
final SecondaryLoop loop =
Toolkit.getDefaultToolkit().getSystemEventQueue()
.createSecondaryLoop();
final Object LOCK = new Object(); //lock to start simultaneously
Thread exitThread = new Thread("Exit thread") {
@Override
public void run() {
synchronized (LOCK) {
LOCK.notify();
}
Thread.yield();
exitReturn = loop.exit();
log.fine("exit() returns " + exitReturn);
}
};
synchronized (LOCK) {
try {
exitThread.start();
LOCK.wait();
} catch (InterruptedException e1) {
throw new RuntimeException("What?");
}
}
enterReturn = loop.enter();
log.fine("enter() returns " + enterReturn);
try {
exitThread.join();
} catch (InterruptedException e) {
throw new RuntimeException("What?");
}
synchronized (bug6980209.class) {
bug6980209.class.notifyAll();
}
log.fine("\n---TEST END---");
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.*;
/*
* @test
* @summary Check that EventQueue constructor does not throw unexpected
* exceptions in headless mode
* @run main/othervm -Djava.awt.headless=true HeadlessEventQueue
*/
public class HeadlessEventQueue {
public static void main(String args[]) {
EventQueue eq = new EventQueue();
}
}

View file

@ -0,0 +1,203 @@
/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.*;
import java.awt.event.*;
/*
* @test
* @summary To Test the following assertions in InvovationEvent.
* 1.InvocationEvent when dispatched, should invoke the
* run() method of the Runnable Interface.
* 2.If catchExceptions is false, Exception should be
* propagated up to the EventDispatchThread's dispatch loop.
* 3.If catchExceptions is true, InvocationEvent.getExceptions()
* should return the exception thrown inside thr run() method.
* 4.When InvocationEvent object is posted on to the EventQueue,
* InvocationEvent.dispatch() method should be invoked by the
* EventQueue.
* 5.If the notifier object is not null, notifyAll() of the
* notifier object should be invoked when the run() method returns.
* 6.To test whether the threads are invoked in the right order
* When InvocationEvents are nested.
* 7.The getWhen method should return timestamp which is less than
* current System time and greater than the time before it has
* actually happened
* @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com)
* @run main InvocationEventTest
*/
public class InvocationEventTest {
EventQueue eventQ1 = new EventQueue();
Object lock = new Object();
static final int delay = 5000;
public volatile boolean notifierStatus = false;
public Object notifierLock = new Object();
public volatile boolean threadStatus = false;
public volatile boolean childInvoked = false;
public synchronized void doTest() throws Exception {
// Testing assertions 1, 2 and 7:
// 1.InvocationEvent when dispatched, should invoke the
// run() method of the Runnable Interface.
// 2.If catchExceptions is false, Exception should be
// propagated up to the EventDispatchThread's dispatch loop.
// 7.The getWhen method should return timestamp which is less than
// current System time and greater than the time before it has
// actually happened
long timeBeforeInvoking = System.currentTimeMillis();
Thread.sleep(10);
InvocationEvent invoc = new InvocationEvent(this, () -> { threadStatus = true; }, lock, false);
invoc.dispatch();
Thread.sleep(10);
if (!threadStatus) {
synchronized (lock) {
lock.wait(delay);
}
}
// testing getException() when no exception is thrown
if (invoc.getWhen() <= timeBeforeInvoking ||
invoc.getWhen() >= System.currentTimeMillis()) {
throw new RuntimeException("getWhen method is not getting the time at which event occured");
}
if (invoc.getException() != null) {
throw new RuntimeException("InvocationEvent.getException() does not return null " +
"when catchException is false");
}
// testing the normal behaviour of InvocationEvent
if (!threadStatus) {
throw new RuntimeException("InvocationEvent when dispatched, did not" +
" invoke the run() of the Runnable interface ");
}
threadStatus = false;
// Testing assertion 3:
// 3.If catchExceptions is true, InvocationEvent.getExceptions()
// should return the exception thrown inside the run() method.
RuntimeException sampleExn = new RuntimeException(" test exception");
invoc = new InvocationEvent(this, () -> { threadStatus = true; throw sampleExn; }, lock, true);
invoc.dispatch();
if (!threadStatus) {
synchronized (lock) {
lock.wait(delay);
}
}
// testing getException() when exception is thrown
// Should return the same exception thrown inside the run() method
if (!invoc.getException().equals(sampleExn)) {
throw new RuntimeException("getException() does not return " +
"the same Exception thrown inside the run() method ");
}
threadStatus = false;
// Testing assertions 4 and 5:
// 4.When InvocationEvent object is posted on to the EventQueue,
// InvocationEvent.dispatch() method should be invoked by the
// EventQueue.
// 5.If the notifier object is not null, notifyAll() of the
// notifier object should be invoked when the run() method returns.
Thread notify = new Thread(){
public void run() {
synchronized (this) {
try { wait(); } catch (InterruptedException e) { throw new RuntimeException(e); }
}
notifierStatus = true;
synchronized (notifierLock) {
notifierLock.notifyAll();
}
}
};
notify.start();
while (notify.getState() != Thread.State.WAITING)
Thread.sleep(delay/5);
InvocationEvent invocation = new InvocationEvent(this, () -> { }, (Object) notify, false);
eventQ1.postEvent(invocation);
while(!invocation.isDispatched())
synchronized (notifierLock) {
notifierLock.wait(delay);
}
while (notify.getState() != Thread.State.TERMINATED)
Thread.sleep(delay/5);
if (!notifierStatus) {
throw new RuntimeException("Notifier object did not get notified" +
" When the run method of the Runnable returns ");
}
// Testing assertion 6:
// 6.To test whether the threads are invoked in the right order
// When InvocationEvents are nested.
Thread thread = new Thread(){
public void run() {
InvocationEvent evt = new InvocationEvent(this, () -> { childInvoked = true; }, (Object) this, false);
new EventQueue().postEvent(evt);
synchronized (this) {
try {
wait(delay);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
threadStatus = true;
}
};
invocation = new InvocationEvent(this, thread, lock, false);
eventQ1.postEvent(invocation);
while (!invocation.isDispatched())
synchronized (lock) {
lock.wait(delay);
}
if (!threadStatus || !childInvoked) {
throw new RuntimeException("Nesting of InvocationEvents when dispatched," +
" did not invoke the run() of the Runnables properly ");
}
}
public static void main(String[] args) throws Exception {
new InvocationEventTest().doTest();
}
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8004584
* @summary Tests 8004584
* @author anthony.petrov@oracle.com, petr.pchelko@oracle.com
* @modules java.desktop/sun.awt
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sun.awt.*;
public class MainAppContext {
public static void main(String[] args) {
ThreadGroup secondGroup = new ThreadGroup("test");
new Thread(secondGroup, () -> {
SunToolkit.createNewAppContext();
test(true);
}).start();
// Sleep on the main thread so that the AWT Toolkit is initialized
// in a user AppContext first
try { Thread.sleep(2000); } catch (Exception e) {}
test(false);
}
private static void test(boolean expectAppContext) {
boolean appContextIsCreated = AppContext.getAppContext() != null;
if (expectAppContext != appContextIsCreated) {
throw new RuntimeException("AppContext is created: " + appContextIsCreated
+ " expected: " + expectAppContext);
}
}
}

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.AWTEvent;
import java.awt.ActiveEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* @test
* @bug 4137796
* @summary Checks that the posting of events whose sources are not Components
* does not corrupt the EventQueue.
*/
public class NonComponentSourcePost {
public static final int COUNT = 100;
public static CountDownLatch go = new CountDownLatch(COUNT);
public static void main(String[] args) throws Throwable {
EventQueue q = new EventQueue();
for (int i = 0; i < COUNT; i++) {
q.postEvent(new NewActionEvent());
}
if (!go.await(30, TimeUnit.SECONDS)) {
throw new RuntimeException("Timeout");
}
AWTEvent event = q.peekEvent();
if (event != null) {
throw new Exception("Non null event: " + event);
}
if (NewActionEvent.result != NewActionEvent.sum) {
throw new Exception("result: " + NewActionEvent.result +
" sum: " + NewActionEvent.sum);
}
// Simple way to shutdown the AWT machinery
Toolkit.getDefaultToolkit().getSystemEventQueue().push(q);
}
static class NewActionEvent extends ActionEvent implements ActiveEvent {
static int counter = 1;
static int sum = 0;
static int result = 0;
int myval;
public NewActionEvent() {
super("", ACTION_PERFORMED, "" + counter);
myval = counter++;
sum += myval;
}
public synchronized void dispatch() {
result += myval;
try {
wait(100);
} catch (InterruptedException e) {
}
go.countDown();
}
}
}

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test PostEventOrderingTest.java
* @bug 4171596 6699589
* @summary Checks that the posting of events between the PostEventQueue
* @summary and the EventQueue maintains proper ordering.
* @modules java.desktop/sun.awt
* @run main PostEventOrderingTest
* @author fredx
*/
import java.awt.*;
import java.awt.event.*;
import sun.awt.AppContext;
import sun.awt.SunToolkit;
public class PostEventOrderingTest {
static boolean testPassed = true;
public static void main(String[] args) throws Throwable {
EventQueue q = Toolkit.getDefaultToolkit().getSystemEventQueue();
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
q.postEvent(new PostActionEvent());
for (int k = 0; k < 10; k++) {
SunToolkit.postEvent(AppContext.getAppContext(), new PostActionEvent());
}
}
for (int k = 0; k < 100; k++) {
SunToolkit.postEvent(AppContext.getAppContext(), new PostActionEvent());
}
}
for (;;) {
Thread.currentThread().sleep(100);
if (q.peekEvent() == null) {
Thread.currentThread().sleep(100);
if (q.peekEvent() == null)
break;
}
}
if (!testPassed) {
throw new Exception("PostEventOrderingTest FAILED -- events dispatched out of order.");
} else {
System.out.println("PostEventOrderingTest passed!");
}
}
}
class PostActionEvent extends ActionEvent implements ActiveEvent {
static int counter = 0;
static int mostRecent = -1;
int myval;
public PostActionEvent() {
super("", ACTION_PERFORMED, "" + counter);
myval = counter++;
}
public void dispatch() {
//System.out.println("myval = "+myval+", mostRecent = "+mostRecent+", diff = "+(myval-mostRecent)+".");
if ((myval - mostRecent) != 1)
PostEventOrderingTest.testPassed = false;
mostRecent = myval;
}
}

View file

@ -0,0 +1,97 @@
/*
* Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4212687
* @summary Verifies that calling EventQueue.push() and EventQueue.pop()
* does not deadlock.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual PushPopDeadlock
*/
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Robot;
import java.awt.Toolkit;
public class PushPopDeadlock {
static int counter = 0;
static Robot robot;
static Frame f;
static Label l;
public static void main(String[] args) throws Exception {
robot = new Robot();
String INSTRUCTIONS = """
Click rapidly in the Frame labeled 'Click Here!'.
The number in the Frame should continue to increase. If the number
stops increasing (remains at a constant value), the test fails.
""";
PassFailJFrame pfJFrame = PassFailJFrame.builder()
.title("Test Instructions")
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 2)
.columns(35)
.testUI(PushPopDeadlock::createUI)
.build();
PushPopDeadlock.test();
pfJFrame.awaitAndCheck();
}
public static Frame createUI() {
f = new Frame("Click Here!");
l = new Label("Counter: " + counter);
f.add(l);
f.setSize(200, 200);
return f;
}
public static void test() {
EventQueue q = new EventQueue() {
public void push(EventQueue queue) {
super.push(queue);
pop();
}
};
EventQueue q2 = new EventQueue();
Toolkit.getDefaultToolkit().getSystemEventQueue().push(q);
new Thread(() -> {
while (true) {
robot.delay(500);
l.setText("Counter: " + ++counter);
q.push(q2);
try {
Thread.currentThread().sleep(500);
} catch (InterruptedException e) {
return;
}
}
}).start();
}
}

View file

@ -0,0 +1,108 @@
/*
* Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@key headful
@bug 4913324
@author Oleg Sukhodolsky: area=eventqueue
@modules java.desktop/sun.awt
@run main/timeout=30 PushPopTest
*/
import java.awt.*;
import java.awt.event.*;
import java.util.EmptyStackException;
import sun.awt.SunToolkit;
public class PushPopTest {
public static Frame frame;
public static void main(String[] args) {
frame = new Frame("");
frame.pack();
Runnable dummy = new Runnable() {
public void run() {
System.err.println("Dummy is here.");
System.err.flush();
}
};
EventQueue seq = Toolkit.getDefaultToolkit().getSystemEventQueue();
MyEventQueue1 eq1 = new MyEventQueue1();
MyEventQueue2 eq2 = new MyEventQueue2();
EventQueue.invokeLater(dummy);
seq.push(eq1);
EventQueue.invokeLater(dummy);
eq1.push(eq2);
EventQueue.invokeLater(dummy);
Runnable runnable = new Runnable() {
public void run() {
System.err.println("Dummy from SunToolkit");
System.err.flush();
}
};
InvocationEvent ie = new InvocationEvent(eq2, runnable, null, false);
// System.err.println(ie);
SunToolkit.postEvent(SunToolkit.targetToAppContext(frame), ie);
eq1.pop();
frame.dispose();
}
}
class MyEventQueue1 extends EventQueue {
public void pop() {
super.pop();
}
}
class MyEventQueue2 extends EventQueue {
protected void pop() {
System.err.println("pop2()");
Thread.dumpStack();
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
Runnable runnable = new Runnable() {
public void run() {
System.err.println("Dummy from pop");
System.err.flush();
}
};
InvocationEvent ie = new InvocationEvent(MyEventQueue2.this, runnable, null, false);
SunToolkit.postEvent(SunToolkit.targetToAppContext(PushPopTest.frame), ie);
postEvent(ie);
}
});
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (java.lang.reflect.InvocationTargetException ie) {
ie.printStackTrace();
}
super.pop();
}
}

View file

@ -0,0 +1,126 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
@test
@bug 6949936
@author Artem Ananiev: area=eventqueue
@run main/timeout=30 SecondaryLoopTest
*/
import java.awt.*;
/**
* Unit test for java.awt.SecondaryLoop implementation
*/
public class SecondaryLoopTest {
private static volatile boolean loopStarted;
private static volatile boolean doubleEntered;
private static volatile boolean loopActive;
private static volatile boolean eventDispatched;
public static void main(String[] args) throws Exception {
test(true, true);
test(true, false);
test(false, true);
test(false, false);
}
private static void test(final boolean enterEDT, final boolean exitEDT) throws Exception {
System.out.println("Running test(" + enterEDT + ", " + exitEDT + ")");
System.err.flush();
loopStarted = true;
Runnable enterRun = new Runnable() {
@Override
public void run() {
Toolkit tk = Toolkit.getDefaultToolkit();
EventQueue eq = tk.getSystemEventQueue();
final SecondaryLoop loop = eq.createSecondaryLoop();
doubleEntered = false;
eventDispatched = false;
Runnable eventRun = new Runnable() {
@Override
public void run() {
// Let the loop enter
sleep(1000);
if (loop.enter()) {
doubleEntered = true;
}
eventDispatched = true;
}
};
EventQueue.invokeLater(eventRun);
Runnable exitRun = new Runnable() {
@Override
public void run() {
// Let the loop enter and eventRun finish
sleep(2000);
if (doubleEntered) {
// Hopefully, we get here if the loop is entered twice
loop.exit();
}
loop.exit();
}
};
if (exitEDT) {
EventQueue.invokeLater(exitRun);
} else {
new Thread(exitRun).start();
}
if (!loop.enter()) {
loopStarted = false;
}
loopActive = eventDispatched;
}
};
if (enterEDT) {
EventQueue.invokeAndWait(enterRun);
} else {
enterRun.run();
}
// Print all the flags before we fail with exception
System.out.println(" loopStarted = " + loopStarted);
System.out.println(" doubleEntered = " + doubleEntered);
System.out.println(" loopActive = " + loopActive);
System.out.flush();
if (!loopStarted) {
throw new RuntimeException("Test FAILED: the secondary loop is not started");
}
if (doubleEntered) {
throw new RuntimeException("Test FAILED: the secondary loop is started twice");
}
if (!loopActive) {
throw new RuntimeException("Test FAILED: the secondary loop exited immediately");
}
}
private static void sleep(long t) {
try {
Thread.sleep(t);
} catch (InterruptedException e) {
e.printStackTrace(System.err);
}
}
}