facebook / infer

A static analyzer for Java, C, C++, and Objective-C
http://fbinfer.com/
MIT License
14.93k stars 2.01k forks source link

java deadlock undetected #1148

Open sonu628 opened 5 years ago

sonu628 commented 5 years ago

deadlock from 2 threads get undetected in java. The code on which it was ran on -

import java.lang.Thread;
public class TestThread {
   public static Object Lock1 = new Object();
   public static Object Lock2 = new Object();

   public static void main(String args[]) {
      ThreadDemo1 T1 = new ThreadDemo1();
      ThreadDemo2 T2 = new ThreadDemo2();
      T1.start();
      T2.start();
   }

   private static class ThreadDemo1 extends Thread {
      public void run() {
         synchronized (Lock1) {
            System.out.println("Thread 1: Holding lock 1...");

            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 1: Waiting for lock 2...");

            synchronized (Lock2) {
               System.out.println("Thread 1: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class ThreadDemo2 extends Thread {
      public void run() {
         synchronized (Lock2) {
            System.out.println("Thread 2: Holding lock 2...");

            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 2: Waiting for lock 1...");

            synchronized (Lock1) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   } 
}

output of problem -

Analysis finished in 1.404ss

  No issues found  

However, when the problem os executed output from code shows it's stuck deadlock

Thread 1: Holding lock 1...
Thread 2: Holding lock 2...
Thread 1: Waiting for lock 2...
Thread 2: Waiting for lock 1...

infer version

$ infer --version
Infer version v0.17.0
Copyright 2009 - present Facebook. All Rights Reserved.

java version it was compiled on

$java -version 
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.222-b10, mixed mode)

Operating system is MacOS Mojave 10.14

ngorogiannis commented 5 years ago

We don't do whole-program analysis in general, so we don't actually pay attention to where/which threads are created, and so this false negative is a natural consequence. Thanks for reporting i any case!

sonu628 commented 5 years ago

so in future do you plan to include design where it would also match where threads are created?

SolomonSun2010 commented 2 years ago

add --starvation-whole-program option could help you.