Program: 92461
Project: 92461
Buggy source:
public long addData(ByteBuffer data){
  int dataSize=data.remaining();
  long state=m_state.get();
  for (; ; ) {
    if (state == -1) {
      state=m_state.get();
      continue;
    }
    final long offs=getOffs(state,m_blockSize);
    long space=(m_blockSize - offs);
    if (dataSize > space) {
      if ((state & WRITERS_MASK) != 0) {
        state=m_state.get();
        continue;
      }
      if (!m_state.compareAndSet(state,-1)) {
        state=m_state.get();
        continue;
      }
      if (space > 0) {
        ByteBuffer ww=m_ww[0];
        ww.position((int)offs);
        ww.limit(m_blockSize);
        data.limit(data.position() + (int)space);
        ww.put(data);
      }
      for (int idx=0; idx < WRITERS_WIDTH; idx++)       m_ww[idx]=null;
      int bytesRest=(dataSize - (int)space);
      for (; ; ) {
        DataBlock dataBlock=new DataBlock(m_useDirectBuffers,m_blockSize);
        ByteBuffer ww=dataBlock.buf.duplicate();
        m_tail.next=dataBlock;
        m_tail=dataBlock;
        if (bytesRest <= m_blockSize) {
          data.limit(data.position() + bytesRest);
          ww.put(data);
          m_ww[0]=ww;
          break;
        }
        data.limit(data.position() + m_blockSize);
        ww.put(data);
        bytesRest-=m_blockSize;
      }
      long newState=(state & OFFS_MASK);
      newState+=dataSize;
      if (newState > OFFS_MASK) {
        newState%=m_blockSize;
        if (newState == 0)         newState=m_blockSize;
      }
      boolean res=m_state.compareAndSet(-1,newState);
      assert(res);
      return dataSize;
    }
    final long writers=(state & WRITERS_MASK);
    if (writers == WRITERS_MASK) {
      state=m_state.get();
      continue;
    }
    long newState=(state & OFFS_MASK);
    newState+=dataSize;
    if (newState > OFFS_MASK) {
      newState%=m_blockSize;
      if (newState == 0)       newState=m_blockSize;
    }
    newState|=(state & ~OFFS_MASK);
    long writer=(1L << (START_WIDTH + OFFS_WIDTH));
    int writerIdx=0;
    for (; writerIdx < WRITERS_WIDTH; writerIdx++, writer<<=1) {
      if ((state & writer) == 0)       break;
    }
    newState|=writer;
    if (writers == 0) {
      assert((state & START_MASK) == 0);
      newState|=(offs << OFFS_WIDTH);
    }
    if (!m_state.compareAndSet(state,newState)) {
      state=m_state.get();
      continue;
    }
    ByteBuffer ww=m_ww[writerIdx];
    if (ww == null) {
      ww=m_tail.buf.duplicate();
      m_ww[writerIdx]=ww;
    }
    ww.position((int)offs);
    ww.put(data);
    state=newState;
    for (; ; ) {
      newState=(state - writer);
      long start=((state & START_MASK) >> OFFS_WIDTH);
      if ((newState & WRITERS_MASK) == 0) {
        newState&=~START_MASK;
        if (m_state.compareAndSet(state,newState)) {
          long end=getOffs(newState,m_blockSize);
          return (end - start);
        }
      }
 else       if (offs == start) {
        newState&=~START_MASK;
        newState|=((offs + dataSize) << OFFS_WIDTH);
        if (m_state.compareAndSet(state,newState))         return dataSize;
      }
 else {
        if (m_state.compareAndSet(state,newState))         return 0;
      }
      state=m_state.get();
    }
  }
}

Fixed version (ground truth):
public long addData(ByteBuffer data){
  int dataSize=data.remaining();
  long state=m_state.get();
  for (; ; ) {
    if (state == -1) {
      state=m_state.get();
      continue;
    }
    final long offs=getOffs(state,m_blockSize);
    long space=(m_blockSize - offs);
    if (dataSize > space) {
      if ((state & WRITERS_MASK) != 0) {
        state=m_state.get();
        continue;
      }
      if (!m_state.compareAndSet(state,-1)) {
        state=m_state.get();
        continue;
      }
      int bytesRest=dataSize;
      if (space > 0) {
        ByteBuffer ww=m_ww[0];
        ww.position((int)offs);
        data.limit(data.position() + (int)space);
        ww.put(data);
        bytesRest-=space;
      }
      return addDataLocked(state,data,dataSize,bytesRest);
    }
    final long writers=(state & WRITERS_MASK);
    if (writers == WRITERS_MASK) {
      state=m_state.get();
      continue;
    }
    long newState=(state & OFFS_MASK);
    newState+=dataSize;
    if (newState > OFFS_MASK) {
      newState%=m_blockSize;
      if (newState == 0)       newState=m_blockSize;
    }
    newState|=(state & ~OFFS_MASK);
    long writer=(1L << (START_WIDTH + OFFS_WIDTH));
    int writerIdx=0;
    for (; writerIdx < WRITERS_WIDTH; writerIdx++, writer<<=1) {
      if ((state & writer) == 0)       break;
    }
    newState|=writer;
    if (writers == 0) {
      assert((state & START_MASK) == 0);
      newState|=(offs << OFFS_WIDTH);
    }
    if (!m_state.compareAndSet(state,newState)) {
      state=m_state.get();
      continue;
    }
    ByteBuffer ww=m_ww[writerIdx];
    if (ww == null) {
      ww=m_tail.buf.duplicate();
      m_ww[writerIdx]=ww;
    }
    ww.position((int)offs);
    ww.put(data);
    state=newState;
    for (; ; ) {
      newState=(state - writer);
      long start=((state & START_MASK) >> OFFS_WIDTH);
      if ((newState & WRITERS_MASK) == 0) {
        newState&=~START_MASK;
        if (m_state.compareAndSet(state,newState)) {
          long end=getOffs(newState,m_blockSize);
          return (end - start);
        }
      }
 else       if (offs == start) {
        newState&=~START_MASK;
        newState|=((offs + dataSize) << OFFS_WIDTH);
        if (m_state.compareAndSet(state,newState))         return dataSize;
      }
 else {
        if (m_state.compareAndSet(state,newState))         return 0;
      }
      state=m_state.get();
    }
  }
}