UDT: Fixing critical bugs in heap code. Fixing endless loops! #51 #49

This commit is contained in:
Kleissner
2021-11-03 20:52:32 +01:00
parent 4704b5194f
commit 62c262e2b6
6 changed files with 151 additions and 90 deletions

View File

@@ -62,18 +62,24 @@ func (h acceptSockHeap) compare(sockID uint32, initSeqNo packet.PacketID, idx in
// Find does a binary search of the heap for the specified packetID which is returned
func (h acceptSockHeap) Find(sockID uint32, initSeqNo packet.PacketID) (*udtSocket, int) {
len := len(h)
idx := 0
for idx < len {
cmp := h.compare(sockID, initSeqNo, idx)
if cmp == 0 {
return h[idx].sock, idx
} else if cmp > 0 {
idx = idx * 2
} else {
idx = idx*2 + 1
for n := 0; n < len(h); n++ {
if h[n].sock.sockID == sockID {
return h[n].sock, n
}
}
// len := len(h)
// idx := 0
// for idx < len {
// cmp := h.compare(sockID, initSeqNo, idx)
// if cmp == 0 {
// return h[idx].sock, idx
// } else if cmp > 0 {
// idx = idx * 2
// } else {
// idx = idx*2 + 1
// }
// }
return nil, -1
}

View File

@@ -41,17 +41,23 @@ func (h *ackHistoryHeap) Pop() interface{} {
// Find does a binary search of the heap for the specified ackID which is returned
func (h ackHistoryHeap) Find(ackID uint32) (*ackHistoryEntry, int) {
len := len(h)
idx := 0
for idx < len {
here := h[idx].ackID
if here == ackID {
return h[idx], idx
} else if here > ackID {
idx = idx * 2
} else {
idx = idx*2 + 1
for n := 0; n < len(h); n++ {
if h[n].ackID == ackID {
return h[n], n
}
}
// len := len(h)
// idx := 0
// for idx < len {
// here := h[idx].ackID
// if here == ackID {
// return h[idx], idx
// } else if here > ackID {
// idx = idx * 2
// } else {
// idx = idx*2 + 1
// }
// }
return nil, -1
}

View File

@@ -35,23 +35,34 @@ func (h *dataPacketHeap) Pop() interface{} {
// Find does a binary search of the heap for the specified packetID which is returned
func (h dataPacketHeap) Find(packetID packet.PacketID) (*packet.DataPacket, int) {
len := len(h)
idx := 0
for idx < len {
pid := h[idx].Seq
if pid == packetID {
return h[idx], idx
} else if pid.Seq > packetID.Seq {
idx = idx * 2
} else {
idx = idx*2 + 1
for n := 0; n < len(h); n++ {
if h[n].Seq == packetID {
return h[n], n
}
}
// len := len(h)
// idx := 0
// for idx < len {
// pid := h[idx].Seq
// if pid == packetID {
// return h[idx], idx
// } else if pid.Seq > packetID.Seq {
// idx = idx * 2
// } else {
// idx = idx*2 + 1
// }
// }
return nil, -1
}
// Min does a binary search of the heap for the entry with the lowest packetID greater than or equal to the specified value
func (h dataPacketHeap) Min(greaterEqual packet.PacketID, lessEqual packet.PacketID) (*packet.DataPacket, int) {
if len(h) == 0 { // none available!
return nil, -1
}
return h[0], 0
len := len(h)
idx := 0
wrapped := greaterEqual.Seq > lessEqual.Seq

View File

@@ -1,6 +1,8 @@
package udt
import "github.com/PeernetOfficial/core/udt/packet"
import (
"github.com/PeernetOfficial/core/udt/packet"
)
// packetIdHeap defines a list of sorted packet IDs
type packetIDHeap []packet.PacketID
@@ -31,37 +33,44 @@ func (h *packetIDHeap) Pop() interface{} {
// Min does a binary search of the heap for the entry with the lowest packetID greater than or equal to the specified value
func (h packetIDHeap) Min(greaterEqual packet.PacketID, lessEqual packet.PacketID) (packet.PacketID, int) {
len := len(h)
idx := 0
wrapped := greaterEqual.Seq > lessEqual.Seq
for idx < len {
pid := h[idx]
var next int
if pid.Seq == greaterEqual.Seq {
return h[idx], idx
} else if pid.Seq >= greaterEqual.Seq {
next = idx * 2
} else {
next = idx*2 + 1
}
if next >= len && h[idx].Seq > greaterEqual.Seq && (wrapped || h[idx].Seq <= lessEqual.Seq) {
return h[idx], idx
}
idx = next
if len(h) == 0 { // none available!
return packet.PacketID{Seq: 0}, -1
}
return h[0], 0
// can't find any packets with greater value, wrap around
if wrapped {
idx = 0
for {
next := idx * 2
if next >= len && h[idx].Seq <= lessEqual.Seq {
return h[idx], idx
}
idx = next
}
}
return packet.PacketID{Seq: 0}, -1
// Disable below buggy code. The second for loop is an infinite loop.
// This whole function probably makes 0 sense!
// len := len(h)
// wrapped := greaterEqual.Seq > lessEqual.Seq
// for i := 0; i < len; {
// pid := h[i]
// var next int
// if pid.Seq == greaterEqual.Seq {
// return h[i], i
// } else if pid.Seq >= greaterEqual.Seq {
// next = i * 2
// } else {
// next = i*2 + 1
// }
// if next >= len && h[i].Seq > greaterEqual.Seq && (wrapped || h[i].Seq <= lessEqual.Seq) {
// return h[i], i
// }
// i = next
// }
// // can't find any packets with greater value, wrap around
// if wrapped {
// for i := 0; ; {
// next := i * 2
// if next >= len && h[i].Seq <= lessEqual.Seq {
// return h[i], i
// }
// i = next
// }
// }
// return packet.PacketID{Seq: 0}, -1
}
func (h packetIDHeap) compare(pktID packet.PacketID, idx int) int {
@@ -76,17 +85,23 @@ func (h packetIDHeap) compare(pktID packet.PacketID, idx int) int {
// Find does a binary search of the heap for the specified packetID which is returned
func (h packetIDHeap) Find(pktID packet.PacketID) (*packet.PacketID, int) {
len := len(h)
idx := 0
for idx < len {
cmp := h.compare(pktID, idx)
if cmp == 0 {
return &h[idx], idx
} else if cmp > 0 {
idx = idx * 2
} else {
idx = idx*2 + 1
for n := 0; n < len(h); n++ {
if h[n].Seq == pktID.Seq {
return &h[n], n
}
}
// len := len(h)
// idx := 0
// for idx < len {
// cmp := h.compare(pktID, idx)
// if cmp == 0 {
// return &h[idx], idx
// } else if cmp > 0 {
// idx = idx * 2
// } else {
// idx = idx*2 + 1
// }
// }
return nil, -1
}

View File

@@ -42,6 +42,11 @@ func (h *receiveLossHeap) Pop() interface{} {
// Min does a binary search of the heap for the entry with the lowest packetID greater than or equal to the specified value
func (h receiveLossHeap) Min(greaterEqual packet.PacketID, lessEqual packet.PacketID) (packet.PacketID, int) {
if len(h) == 0 { // none available!
return packet.PacketID{Seq: 0}, -1
}
return h[0].packetID, 0
len := len(h)
idx := 0
wrapped := greaterEqual.Seq > lessEqual.Seq
@@ -77,18 +82,24 @@ func (h receiveLossHeap) Min(greaterEqual packet.PacketID, lessEqual packet.Pack
// Find does a binary search of the heap for the specified packetID which is returned
func (h receiveLossHeap) Find(packetID packet.PacketID) (*recvLossEntry, int) {
len := len(h)
idx := 0
for idx < len {
pid := h[idx].packetID
if pid == packetID {
return &h[idx], idx
} else if pid.Seq > packetID.Seq {
idx = idx * 2
} else {
idx = idx*2 + 1
for n := 0; n < len(h); n++ {
if h[n].packetID == packetID {
return &h[n], n
}
}
// len := len(h)
// idx := 0
// for idx < len {
// pid := h[idx].packetID
// if pid == packetID {
// return &h[idx], idx
// } else if pid.Seq > packetID.Seq {
// idx = idx * 2
// } else {
// idx = idx*2 + 1
// }
// }
return nil, -1
}

View File

@@ -42,23 +42,35 @@ func (h *sendPacketHeap) Pop() interface{} {
// Find does a binary search of the heap for the specified packetID which is returned
func (h sendPacketHeap) Find(packetID packet.PacketID) (*sendPacketEntry, int) {
len := len(h)
idx := 0
for idx < len {
pid := h[idx].pkt.Seq
if pid == packetID {
return &h[idx], idx
} else if pid.Seq > packetID.Seq {
idx = idx * 2
} else {
idx = idx*2 + 1
for n := 0; n < len(h); n++ {
if h[n].pkt.Seq == packetID {
return &h[n], n
}
}
// buggy crappy implementation
// len := len(h)
// idx := 0
// for idx < len {
// pid := h[idx].pkt.Seq
// if pid == packetID {
// return &h[idx], idx
// } else if pid.Seq > packetID.Seq {
// idx = idx * 2
// } else {
// idx = idx*2 + 1
// }
// }
return nil, -1
}
// Min does a binary search of the heap for the entry with the lowest packetID greater than or equal to the specified value
func (h sendPacketHeap) Min(greaterEqual packet.PacketID, lessEqual packet.PacketID) (*packet.DataPacket, int) {
if len(h) == 0 { // none available!
return nil, -1
}
return h[0].pkt, 0
len := len(h)
idx := 0
wrapped := greaterEqual.Seq > lessEqual.Seq