Skip to content

Commit 7ca550f

Browse files
committed
Fix stats data errors and panic for VPP 20.05
- fixes panic occurring when updating error counters - fixes stat data length errors when updating stats Change-Id: If2d4bcb7df084bf1999ba469f128b7a01aa6be5e Signed-off-by: Ondrej Fabry <[email protected]>
1 parent d372b4e commit 7ca550f

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

adapter/statsclient/stat_segment.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ func (c *statSegment) copyEntryData(dirEntry *statSegDirectoryEntry) adapter.Sta
200200
return adapter.ScalarStat(dirEntry.unionData)
201201

202202
case statDirErrorIndex:
203+
if dirEntry.unionData == 0 {
204+
debugf("offset invalid for %s", dirEntry.name)
205+
break
206+
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
207+
debugf("offset out of range for %s", dirEntry.name)
208+
break
209+
}
210+
203211
_, errOffset, _ := c.getOffsets()
204212
offsetVector := unsafe.Pointer(&c.sharedHeader[errOffset])
205213

@@ -324,6 +332,14 @@ func (c *statSegment) updateEntryData(dirEntry *statSegDirectoryEntry, stat *ada
324332
*stat = adapter.ScalarStat(dirEntry.unionData)
325333

326334
case adapter.ErrorStat:
335+
if dirEntry.unionData == 0 {
336+
debugf("offset invalid for %s", dirEntry.name)
337+
break
338+
} else if dirEntry.unionData >= uint64(len(c.sharedHeader)) {
339+
debugf("offset out of range for %s", dirEntry.name)
340+
break
341+
}
342+
327343
_, errOffset, _ := c.getOffsets()
328344
offsetVector := unsafe.Pointer(&c.sharedHeader[errOffset])
329345

@@ -334,8 +350,9 @@ func (c *statSegment) updateEntryData(dirEntry *statSegDirectoryEntry, stat *ada
334350
val := *(*adapter.Counter)(statSegPointer(offsetVector, offset))
335351
errData = val
336352
} else {
337-
vecLen := vectorLen(offsetVector)
338-
for i := uint64(0); i < vecLen; i++ {
353+
vecLen := uint32(vectorLen(unsafe.Pointer(&c.sharedHeader[errOffset])))
354+
355+
for i := uint32(0); i < vecLen; i++ {
339356
cb := *(*uint64)(statSegPointer(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
340357
offset := uintptr(cb) + uintptr(dirEntry.unionData)*unsafe.Sizeof(adapter.Counter(0))
341358
val := *(*adapter.Counter)(statSegPointer(unsafe.Pointer(&c.sharedHeader[0]), offset))
@@ -353,22 +370,22 @@ func (c *statSegment) updateEntryData(dirEntry *statSegDirectoryEntry, stat *ada
353370
break
354371
}
355372

356-
vecLen := vectorLen(unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]))
373+
vecLen := uint32(vectorLen(unsafe.Pointer(&c.sharedHeader[dirEntry.unionData])))
357374
offsetVector := statSegPointer(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
358375

359376
data := (*stat).(adapter.SimpleCounterStat)
360-
if uint64(len(data)) != vecLen {
377+
if uint32(len(data)) != vecLen {
361378
return ErrStatDataLenIncorrect
362379
}
363-
for i := uint64(0); i < vecLen; i++ {
380+
for i := uint32(0); i < vecLen; i++ {
364381
cb := *(*uint64)(statSegPointer(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
365382
counterVec := unsafe.Pointer(&c.sharedHeader[uintptr(cb)])
366-
vecLen2 := vectorLen(counterVec)
383+
vecLen2 := uint32(vectorLen(counterVec))
367384
simpData := data[i]
368-
if uint64(len(simpData)) != vecLen2 {
385+
if uint32(len(simpData)) != vecLen2 {
369386
return ErrStatDataLenIncorrect
370387
}
371-
for j := uint64(0); j < vecLen2; j++ {
388+
for j := uint32(0); j < vecLen2; j++ {
372389
offset := uintptr(j) * unsafe.Sizeof(adapter.Counter(0))
373390
val := *(*adapter.Counter)(statSegPointer(counterVec, offset))
374391
simpData[j] = val
@@ -384,22 +401,22 @@ func (c *statSegment) updateEntryData(dirEntry *statSegDirectoryEntry, stat *ada
384401
break
385402
}
386403

387-
vecLen := vectorLen(unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]))
404+
vecLen := uint32(vectorLen(unsafe.Pointer(&c.sharedHeader[dirEntry.unionData])))
388405
offsetVector := statSegPointer(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
389406

390407
data := (*stat).(adapter.CombinedCounterStat)
391-
if uint64(len(data)) != vecLen {
408+
if uint32(len(data)) != vecLen {
392409
return ErrStatDataLenIncorrect
393410
}
394-
for i := uint64(0); i < vecLen; i++ {
411+
for i := uint32(0); i < vecLen; i++ {
395412
cb := *(*uint64)(statSegPointer(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
396413
counterVec := unsafe.Pointer(&c.sharedHeader[uintptr(cb)])
397-
vecLen2 := vectorLen(counterVec)
414+
vecLen2 := uint32(vectorLen(counterVec))
398415
combData := data[i]
399-
if uint64(len(combData)) != vecLen2 {
416+
if uint32(len(combData)) != vecLen2 {
400417
return ErrStatDataLenIncorrect
401418
}
402-
for j := uint64(0); j < vecLen2; j++ {
419+
for j := uint32(0); j < vecLen2; j++ {
403420
offset := uintptr(j) * unsafe.Sizeof(adapter.CombinedCounter{})
404421
val := *(*adapter.CombinedCounter)(statSegPointer(counterVec, offset))
405422
combData[j] = val
@@ -415,26 +432,26 @@ func (c *statSegment) updateEntryData(dirEntry *statSegDirectoryEntry, stat *ada
415432
break
416433
}
417434

418-
vecLen := vectorLen(unsafe.Pointer(&c.sharedHeader[dirEntry.unionData]))
435+
vecLen := uint32(vectorLen(unsafe.Pointer(&c.sharedHeader[dirEntry.unionData])))
419436
offsetVector := statSegPointer(unsafe.Pointer(&c.sharedHeader[0]), uintptr(dirEntry.offsetVector))
420437

421438
data := (*stat).(adapter.NameStat)
422-
if uint64(len(data)) != vecLen {
439+
if uint32(len(data)) != vecLen {
423440
return ErrStatDataLenIncorrect
424441
}
425-
for i := uint64(0); i < vecLen; i++ {
442+
for i := uint32(0); i < vecLen; i++ {
426443
cb := *(*uint64)(statSegPointer(offsetVector, uintptr(i)*unsafe.Sizeof(uint64(0))))
427444
if cb == 0 {
428445
continue
429446
}
430447
nameVec := unsafe.Pointer(&c.sharedHeader[cb])
431-
vecLen2 := vectorLen(nameVec)
448+
vecLen2 := uint32(vectorLen(nameVec))
432449

433450
nameData := data[i]
434-
if uint64(len(nameData))+1 != vecLen2 {
451+
if uint32(len(nameData))+1 != vecLen2 {
435452
return ErrStatDataLenIncorrect
436453
}
437-
for j := uint64(0); j < vecLen2; j++ {
454+
for j := uint32(0); j < vecLen2; j++ {
438455
offset := uintptr(j) * unsafe.Sizeof(byte(0))
439456
val := *(*byte)(statSegPointer(nameVec, offset))
440457
if val == 0 {

core/stats.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func newStatsConnection(stats adapter.StatsAPI) *StatsConnection {
8888
}
8989
}
9090

91-
// Connect connects to Stats API using specified adapter and returns a connection handle.
91+
// ConnectStats connects to Stats API using specified adapter and returns a connection handle.
9292
// This call blocks until it is either connected, or an error occurs.
9393
// Only one connection attempt will be performed.
9494
func ConnectStats(stats adapter.StatsAPI) (*StatsConnection, error) {
@@ -177,7 +177,7 @@ func (c *StatsConnection) updateStats(statDir **adapter.StatDir, patterns ...str
177177
return err
178178
}
179179

180-
// UpdateSystemStats retrieves VPP system stats.
180+
// GetSystemStats retrieves VPP system stats.
181181
func (c *StatsConnection) GetSystemStats(sysStats *api.SystemStats) (err error) {
182182
if err := c.updateStats(&c.sysStatsData, SystemStatsPrefix); err != nil {
183183
return err

0 commit comments

Comments
 (0)