Program: 92451
Project: 92451
Buggy source:
public void runInThreadPool(){
  int tailLock;
  int state=m_state.get();
  for (; ; ) {
    if ((state & LENGTH_MASK) == 0) {
      if ((state & TAIL_LOCK) == 0)       tailLock=0;
 else       tailLock=-1;
      break;
    }
    assert((state & TAIL_LOCK) == 0);
    int newState=state;
    newState|=TAIL_LOCK;
    tailLock=1;
    if (m_state.compareAndSet(state,newState)) {
      state=newState;
      break;
    }
    state=m_state.get();
  }
  int pos0;
  long space;
  DataBlock prev;
  DataBlock dataBlock0;
  DataBlock dataBlock1;
  if (tailLock > 0) {
    dataBlock0=m_tail;
    pos0=dataBlock0.ww.position();
    space=(m_blockSize - pos0);
    if (space > 0) {
      prev=null;
      dataBlock1=m_dataBlockCache.get(1);
      space+=m_blockSize;
    }
 else {
      prev=dataBlock0;
      dataBlock0=m_dataBlockCache.get(2);
      dataBlock1=dataBlock0.next;
      dataBlock0.next=null;
      pos0=0;
      space=m_blockSize * 2;
    }
  }
 else {
    prev=null;
    dataBlock0=m_dataBlockCache.get(2);
    dataBlock1=dataBlock0.next;
    dataBlock0.next=null;
    pos0=0;
    space=m_blockSize * 2;
  }
  m_iov[0]=dataBlock0.ww;
  m_iov[1]=dataBlock1.ww;
  long bytesReceived;
  try {
    bytesReceived=m_socketChannel.read(m_iov,0,2);
    m_statReads++;
  }
 catch (  Exception ex) {
    if (s_logger.isLoggable(Level.WARNING)) {
      s_logger.warning(m_socketChannel.socket().getRemoteSocketAddress().toString() + "String_Node_Str" + ex.toString()+ "String_Node_Str");
    }
    bytesReceived=0;
  }
  m_iov[0]=null;
  m_iov[1]=null;
  if (bytesReceived > 0) {
    if (tailLock > 0) {
      if (prev != null) {
        assert(pos0 == 0);
        prev.next=dataBlock0;
      }
    }
 else {
      assert(prev == null);
      if (tailLock < 0) {
        for (; ; ) {
          state=m_state.get();
          if ((state & TAIL_LOCK) == 0)           break;
        }
      }
    }
    if (bytesReceived > (m_blockSize - pos0)) {
      dataBlock0.next=dataBlock1;
      m_tail=dataBlock1;
      dataBlock1=null;
    }
 else     m_tail=dataBlock0;
    for (; ; ) {
      int newState=state;
      newState&=LENGTH_MASK;
      newState+=bytesReceived;
      assert(newState < LENGTH_MASK);
      newState|=(state & ~LENGTH_MASK);
      if (tailLock > 0) {
        assert((newState & TAIL_LOCK) != 0);
        newState-=TAIL_LOCK;
      }
      if (m_state.compareAndSet(state,newState)) {
        state=newState;
        break;
      }
      state=m_state.get();
    }
    if ((state & LENGTH_MASK) > m_maxSize) {
    }
 else     if (bytesReceived == space) {
      m_speculativeRead=true;
      m_collider.executeInThreadPool(this);
    }
 else {
      m_speculativeRead=false;
      m_collider.executeInSelectorThread(m_starter);
    }
    if ((state & LENGTH_MASK) == bytesReceived) {
      m_statHandleData++;
      handleData(dataBlock0,state);
      if (prev != null) {
        prev.next=null;
        m_dataBlockCache.put(prev.reset());
      }
    }
    if (dataBlock1 != null) {
      assert(dataBlock1.ww.position() == 0);
      assert(dataBlock1.next == null);
      m_dataBlockCache.put(dataBlock1);
    }
  }
 else   if (m_speculativeRead) {
    assert(dataBlock0.next == null);
    assert(dataBlock1.next == null);
    assert(dataBlock1.ww.position() == 0);
    if (tailLock > 0) {
      for (; ; ) {
        assert((state & TAIL_LOCK) != 0);
        int newState=(state - TAIL_LOCK);
        if (m_state.compareAndSet(state,newState)) {
          state=newState;
          break;
        }
        state=m_state.get();
      }
      if ((state & LENGTH_MASK) == 0) {
        dataBlock1.next=dataBlock0;
        if (prev == null)         assert(m_tail == dataBlock0);
 else {
          assert(m_tail == prev);
          assert(m_tail.next == null);
          dataBlock0.next=prev.reset();
        }
        m_tail=null;
      }
 else {
        if (prev != null) {
          dataBlock1.next=dataBlock0;
        }
      }
    }
 else {
      assert(prev == null);
      dataBlock1.next=dataBlock0;
    }
    m_dataBlockCache.put(dataBlock1);
    m_speculativeRead=false;
    m_collider.executeInSelectorThread(m_starter);
  }
 else {
    m_session.handleReaderStopped();
    int newState;
    for (; ; ) {
      newState=state;
      newState|=CLOSED;
      if (tailLock > 0) {
        assert((state & TAIL_LOCK) != 0);
        newState-=TAIL_LOCK;
      }
      if (m_state.compareAndSet(state,newState))       break;
      state=m_state.get();
    }
    if (s_logger.isLoggable(Level.FINER)) {
      s_logger.finer(m_session.getRemoteAddress().toString() + "String_Node_Str" + stateToString(state)+ "String_Node_Str"+ stateToString(newState)+ "String_Node_Str");
    }
    assert(dataBlock0.next == null);
    assert(dataBlock1.next == null);
    if ((newState & LENGTH_MASK) == 0) {
      m_listener.onConnectionClosed();
      printStats();
      if (tailLock > 0) {
        dataBlock1.next=dataBlock0;
        if (prev == null)         assert(m_tail == dataBlock0);
 else {
          assert(m_tail == prev);
          assert(m_tail.next == null);
          dataBlock0.next=prev.reset();
        }
        m_tail=null;
      }
 else {
        assert(prev == null);
        dataBlock1.next=dataBlock0;
      }
    }
 else {
      if (tailLock > 0) {
        if (prev == null)         assert(m_tail == dataBlock0);
 else {
          assert(m_tail == prev);
          dataBlock1.next=dataBlock0;
        }
      }
 else {
        assert(prev == null);
        dataBlock1.next=dataBlock0;
      }
    }
    m_dataBlockCache.put(dataBlock1);
  }
}

Fixed version (ground truth):
public void runInThreadPool(){
  int tailLock;
  int state=m_state.get();
  for (; ; ) {
    if ((state & LENGTH_MASK) == 0) {
      if ((state & TAIL_LOCK) == 0)       tailLock=0;
 else       tailLock=-1;
      break;
    }
    assert((state & TAIL_LOCK) == 0);
    int newState=state;
    newState|=TAIL_LOCK;
    tailLock=1;
    if (m_state.compareAndSet(state,newState)) {
      state=newState;
      break;
    }
    state=m_state.get();
  }
  int pos0;
  long space;
  DataBlock prev;
  DataBlock dataBlock0;
  DataBlock dataBlock1;
  if (tailLock > 0) {
    dataBlock0=m_tail;
    pos0=dataBlock0.ww.position();
    space=(m_blockSize - pos0);
    if (space > 0) {
      prev=null;
      dataBlock1=m_dataBlockCache.get(1);
      space+=m_blockSize;
    }
 else {
      prev=dataBlock0;
      dataBlock0=m_dataBlockCache.get(2);
      dataBlock1=dataBlock0.next;
      dataBlock0.next=null;
      pos0=0;
      space=m_blockSize * 2;
    }
  }
 else {
    prev=null;
    dataBlock0=m_dataBlockCache.get(2);
    dataBlock1=dataBlock0.next;
    dataBlock0.next=null;
    pos0=0;
    space=m_blockSize * 2;
  }
  m_iov[0]=dataBlock0.ww;
  m_iov[1]=dataBlock1.ww;
  long bytesReceived;
  try {
    bytesReceived=m_socketChannel.read(m_iov,0,2);
    m_statReads++;
  }
 catch (  Exception ex) {
    if (s_logger.isLoggable(Level.WARNING)) {
      s_logger.warning(m_socketChannel.socket().getRemoteSocketAddress().toString() + "String_Node_Str" + ex.toString()+ "String_Node_Str");
    }
    bytesReceived=0;
  }
  m_iov[0]=null;
  m_iov[1]=null;
  if (bytesReceived > 0) {
    if (tailLock > 0) {
      if (prev != null) {
        assert(pos0 == 0);
        prev.next=dataBlock0;
      }
    }
 else {
      assert(prev == null);
      if (tailLock < 0) {
        for (; ; ) {
          state=m_state.get();
          if ((state & TAIL_LOCK) == 0)           break;
        }
      }
    }
    if (bytesReceived > (m_blockSize - pos0)) {
      dataBlock0.next=dataBlock1;
      m_tail=dataBlock1;
      dataBlock1=null;
    }
 else     m_tail=dataBlock0;
    for (; ; ) {
      int newState=state;
      newState&=LENGTH_MASK;
      newState+=bytesReceived;
      assert(newState < LENGTH_MASK);
      newState|=(state & ~LENGTH_MASK);
      if (tailLock > 0) {
        assert((newState & TAIL_LOCK) != 0);
        newState-=TAIL_LOCK;
      }
      if (m_state.compareAndSet(state,newState)) {
        state=newState;
        break;
      }
      state=m_state.get();
    }
    if ((state & LENGTH_MASK) > m_maxSize) {
    }
 else     if (bytesReceived == space) {
      m_speculativeRead=true;
      m_collider.executeInThreadPool(this);
    }
 else {
      m_speculativeRead=false;
      m_collider.executeInSelectorThread(m_starter);
    }
    if ((state & LENGTH_MASK) == bytesReceived) {
      m_statHandleData++;
      handleData(dataBlock0,state);
      if (prev != null) {
        prev.next=null;
        m_dataBlockCache.put(prev.reset());
      }
    }
    if (dataBlock1 != null) {
      assert(dataBlock1.ww.position() == 0);
      assert(dataBlock1.next == null);
      m_dataBlockCache.put(dataBlock1);
    }
  }
 else   if (m_speculativeRead) {
    assert(dataBlock0.next == null);
    assert(dataBlock1.next == null);
    assert(dataBlock1.ww.position() == 0);
    if (tailLock > 0) {
      for (; ; ) {
        assert((state & TAIL_LOCK) != 0);
        int newState=(state - TAIL_LOCK);
        if (m_state.compareAndSet(state,newState)) {
          state=newState;
          break;
        }
        state=m_state.get();
      }
      if ((state & LENGTH_MASK) == 0) {
        if (prev == null) {
          assert(m_tail == dataBlock0);
          dataBlock1.next=dataBlock0.reset();
        }
 else {
          assert(m_tail == prev);
          dataBlock1.next=dataBlock0;
          dataBlock0.next=prev.reset();
        }
        m_tail=null;
      }
 else {
        assert((m_tail == null) || ((prev == null) && (m_tail == dataBlock0)) || (m_tail == prev));
        if (prev != null)         dataBlock1.next=dataBlock0;
      }
    }
 else {
      assert(prev == null);
      dataBlock1.next=dataBlock0;
    }
    m_dataBlockCache.put(dataBlock1);
    m_speculativeRead=false;
    m_collider.executeInSelectorThread(m_starter);
  }
 else {
    m_session.handleReaderStopped();
    int newState;
    for (; ; ) {
      newState=state;
      newState|=CLOSED;
      if (tailLock > 0) {
        assert((state & TAIL_LOCK) != 0);
        newState-=TAIL_LOCK;
      }
      if (m_state.compareAndSet(state,newState))       break;
      state=m_state.get();
    }
    if (s_logger.isLoggable(Level.FINER)) {
      s_logger.finer(m_session.getRemoteAddress().toString() + "String_Node_Str" + stateToString(state)+ "String_Node_Str"+ stateToString(newState)+ "String_Node_Str");
    }
    assert(dataBlock0.next == null);
    assert(dataBlock1.next == null);
    if ((newState & LENGTH_MASK) == 0) {
      m_listener.onConnectionClosed();
      printStats();
      if (tailLock > 0) {
        if (prev == null) {
          assert(m_tail == dataBlock0);
          dataBlock1.next=dataBlock0.reset();
        }
 else {
          assert(m_tail == prev);
          dataBlock1.next=dataBlock0;
          dataBlock0.next=prev.reset();
        }
        m_tail=null;
      }
 else {
        assert(prev == null);
        dataBlock1.next=dataBlock0;
      }
    }
 else {
      if (tailLock > 0) {
        assert((m_tail == null) || ((prev == null) && (m_tail == dataBlock0)) || (m_tail == prev));
        if (prev != null)         dataBlock1.next=dataBlock0;
      }
 else {
        assert(prev == null);
        dataBlock1.next=dataBlock0;
      }
    }
    m_dataBlockCache.put(dataBlock1);
  }
}