Skip to content

Commit

Permalink
h265: fix crash in DTS extractor; improve fuzz tests (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed May 18, 2024
1 parent 1e6e29b commit 0b44f66
Show file tree
Hide file tree
Showing 51 changed files with 170 additions and 83 deletions.
35 changes: 28 additions & 7 deletions pkg/codecs/h264/dts_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,42 @@ func TestDTSExtractor(t *testing.T) {
}
}

func FuzzDTSExtractor(f *testing.F) {
ex := NewDTSExtractor()

f.Fuzz(func(_ *testing.T, b []byte, p uint64) {
if len(b) < 1 {
func FuzzDTSExtractorFirstAU(f *testing.F) {
f.Fuzz(func(_ *testing.T, a []byte, b []byte) {
if len(a) < 1 || len(b) < 1 {
return
}

ex := NewDTSExtractor()

ex.Extract([][]byte{ //nolint:errcheck
a,
b,
}, 0)
})
}

func FuzzDTSExtractorSecondAU(f *testing.F) {
f.Fuzz(func(t *testing.T, a []byte) {
if len(a) < 1 {
return
}

ex := NewDTSExtractor()

_, err := ex.Extract([][]byte{
{ // SPS
0x27, 0x64, 0x00, 0x20, 0xac, 0x52, 0x18, 0x0f,
0x01, 0x17, 0xef, 0xff, 0x00, 0x01, 0x00, 0x01,
0x6a, 0x02, 0x02, 0x03, 0x6d, 0x85, 0x6b, 0xde,
0xf8, 0x08,
},
b,
}, time.Duration(p))
{ // IDR
0x05,
},
}, 0)
require.NoError(t, err)

ex.Extract([][]byte{a}, 1*time.Second) //nolint:errcheck
})
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
go test fuzz v1
[]byte("0")
uint64(200)
[]byte("0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("'")
[]byte("0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
[]byte("0")
[]byte("'0002B\x7fB20")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("A")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("A7")
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
go test fuzz v1
[]byte("0")
uint64(0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("A2")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("A0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("A10")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("AX00")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("AX\x000")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("A2A0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("A00000000000000000000000")
77 changes: 61 additions & 16 deletions pkg/codecs/h265/dts_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,26 +295,71 @@ func TestDTSExtractor(t *testing.T) {
}
}

func FuzzDTSExtractor(f *testing.F) {
sps := []byte{
0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03,
0x00, 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03,
0x00, 0x78, 0xa0, 0x03, 0xc0, 0x80, 0x10, 0xe5,
0x96, 0x66, 0x69, 0x24, 0xca, 0xe0, 0x10, 0x00,
0x00, 0x03, 0x00, 0x10, 0x00, 0x00, 0x03, 0x01,
0xe0, 0x80,
}
func FuzzDTSExtractorFirstAU(f *testing.F) {
f.Fuzz(func(_ *testing.T, a []byte, b []byte, c []byte) {
if len(a) < 1 || len(b) < 1 || len(c) < 1 {
return
}

pps := []byte{
0x44, 0x01, 0xc1, 0x72, 0xb4, 0x62, 0x40,
}
ex := NewDTSExtractor()

ex := NewDTSExtractor()
ex.Extract([][]byte{ //nolint:errcheck
a,
b,
c,
}, 0)
})
}

f.Fuzz(func(_ *testing.T, b []byte, p uint64) {
if len(b) < 1 {
func FuzzDTSExtractorSecondAU(f *testing.F) {
f.Fuzz(func(t *testing.T, sps int, a []byte) {
if len(a) < 1 {
return
}
ex.Extract([][]byte{sps, pps, b}, time.Duration(p)) //nolint:errcheck

ex := NewDTSExtractor()

switch sps % 2 {
case 0:
_, err := ex.Extract([][]byte{
{ // SPS
0x42, 0x01, 0x01, 0x01, 0x40, 0x00, 0x00, 0x03,
0x00, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03,
0x00, 0x99, 0xa0, 0x03, 0xc0, 0x80, 0x10, 0xe5,
0x8d, 0xa5, 0x92, 0x42, 0x36, 0x22, 0xec, 0xb8,
0x80, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00,
0x00, 0x05, 0x0f, 0xe2, 0xc4, 0xa0,
},
{ // PPS
0x44, 0x01, 0xc0, 0xe0, 0x98, 0x93, 0x03, 0x05,
0x14, 0x90,
},
{ // IDR
0x26,
},
}, 0)
require.NoError(t, err)

default:
_, err := ex.Extract([][]byte{
{ // SPS
0x42, 0x01, 0x01, 0x01, 0x60, 0x00, 0x00, 0x03,
0x00, 0x90, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03,
0x00, 0x78, 0xa0, 0x03, 0xc0, 0x80, 0x10, 0xe5,
0x96, 0x66, 0x69, 0x24, 0xca, 0xe0, 0x10, 0x00,
0x00, 0x03, 0x00, 0x10, 0x00, 0x00, 0x03, 0x01,
0xe0, 0x80,
},
{ // PPS
0x44, 0x01, 0xc1, 0x72, 0xb4, 0x62, 0x40,
},
{ // IDR
0x26,
},
}, 0)
require.NoError(t, err)
}

ex.Extract([][]byte{a}, 1*time.Second) //nolint:errcheck
})
}
4 changes: 4 additions & 0 deletions pkg/codecs/h265/sps.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ func (r *SPS_ShortTermRefPicSet) unmarshal(buf []byte, pos *int, stRpsIdx uint32
deltaRps := (1 - 2*s) * (int32(r.AbsDeltaRpsMinus1) + 1)

refRpsIdx := stRpsIdx - (r.DeltaIdxMinus1 + 1)
if refRpsIdx >= uint32(len(shortTermRefPicSets)) {
return fmt.Errorf("invalid refRpsIdx")
}

refRPS := shortTermRefPicSets[refRpsIdx]
numDeltaPocs := refRPS.NumNegativePics + refRPS.NumPositivePics
usedByCurrPicFlag := make([]bool, numDeltaPocs+1)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
[]byte("0")
[]byte("0")
[]byte("E")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
[]byte("0")
[]byte("B")
[]byte("0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
[]byte("0")
[]byte("0")
[]byte("B000000000000000000177\xe6018")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
[]byte("\xc400000")
[]byte("\xc4010")
[]byte("B000000000000000000177\xe601800000")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
[]byte("*0\xf0$00")
[]byte("\xc4010")
[]byte("B00000000000000B00177\xd0A0AA90000A0000000000000010A000000000Ay90700000000")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
[]byte("0")
[]byte("0")
[]byte("0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(0)
[]byte("*0\xd707\xd7")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(0)
[]byte("*0\xcc")
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
go test fuzz v1
int(0)
[]byte("*")
uint64(34)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(-47)
[]byte("*0\xa300")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(49)
[]byte("0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(0)
[]byte("*00")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(0)
[]byte("*0\xc0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(10)
[]byte("*0\xcc002")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(0)
[]byte("*0\xcc0")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go test fuzz v1
int(-11)
[]byte("*0\xa60")

0 comments on commit 0b44f66

Please sign in to comment.