Skip to content

Commit 9f0a523

Browse files
committed
chore(ps): Append/remove module by base address
Instead of appending/removing the process modules by its image name, we're now using the module base address to determine if the module was already added. The same logic is used to remove the existing module.
1 parent 94d5bcf commit 9f0a523

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

internal/etw/processors/image_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func (m *imageProcessor) ProcessEvent(e *kevent.Kevent) (*kevent.Kevent, bool, e
4444
}
4545
if e.IsUnloadImage() {
4646
pid := e.Kparams.MustGetPid()
47-
mod := e.GetParamAsString(kparams.ImageFilename)
47+
addr := e.Kparams.TryGetAddress(kparams.ImageBase)
4848
if pid == 0 {
4949
pid = e.PID
5050
}
51-
return e, false, m.psnap.RemoveModule(pid, mod)
51+
return e, false, m.psnap.RemoveModule(pid, addr)
5252
}
5353
if e.IsLoadImage() || e.IsImageRundown() {
5454
return e, false, m.psnap.AddModule(e)

internal/etw/processors/image_windows_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/rabbitstack/fibratus/pkg/kevent/ktypes"
2525
"github.com/rabbitstack/fibratus/pkg/ps"
2626
"github.com/rabbitstack/fibratus/pkg/util/signature"
27+
"github.com/rabbitstack/fibratus/pkg/util/va"
2728
"github.com/stretchr/testify/assert"
2829
"github.com/stretchr/testify/mock"
2930
"github.com/stretchr/testify/require"
@@ -79,7 +80,7 @@ func TestImageProcessor(t *testing.T) {
7980
},
8081
func() *ps.SnapshotterMock {
8182
psnap := new(ps.SnapshotterMock)
82-
psnap.On("RemoveModule", uint32(676), "C:\\Windows\\system32\\kernel32.dll").Return(nil)
83+
psnap.On("RemoveModule", uint32(676), va.Address(0xfffb313833a3)).Return(nil)
8384
psnap.On("FindModule", mock.Anything).Return(false, nil)
8485
return psnap
8586
},

internal/etw/source_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ func (s *NoopPsSnapshotter) AddThread(kevt *kevent.Kevent) error
826826
func (s *NoopPsSnapshotter) AddModule(kevt *kevent.Kevent) error { return nil }
827827
func (s *NoopPsSnapshotter) FindModule(addr va.Address) (bool, *pstypes.Module) { return false, nil }
828828
func (s *NoopPsSnapshotter) RemoveThread(pid uint32, tid uint32) error { return nil }
829-
func (s *NoopPsSnapshotter) RemoveModule(pid uint32, mod string) error { return nil }
829+
func (s *NoopPsSnapshotter) RemoveModule(pid uint32, addr va.Address) error { return nil }
830830
func (s *NoopPsSnapshotter) WriteFromKcap(kevt *kevent.Kevent) error { return nil }
831831
func (s *NoopPsSnapshotter) AddFileMapping(kevt *kevent.Kevent) error { return nil }
832832
func (s *NoopPsSnapshotter) RemoveFileMapping(pid uint32, address va.Address) error { return nil }
@@ -840,7 +840,7 @@ func TestCallstackEnrichment(t *testing.T) {
840840

841841
// exercise callstack enrichment with a noop
842842
// process snapshotter. This will make the
843-
// symbolizer to always fallback to Debug Help
843+
// symbolizer to always fall back to Debug Help
844844
// API when resolving symbolic information
845845
nopsnap := new(NoopPsSnapshotter)
846846
log.Info("test callstack enrichment with noop ps snapshotter")

pkg/ps/snapshotter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ type Snapshotter interface {
3939
// RemoveThread removes the thread from the given process.
4040
RemoveThread(pid uint32, tid uint32) error
4141
// RemoveModule removes the module the given process.
42-
RemoveModule(pid uint32, mod string) error
42+
RemoveModule(pid uint32, addr va.Address) error
4343
// AddFileMapping adds a new data memory-mapped file to this process state.
4444
AddFileMapping(*kevent.Kevent) error
4545
// RemoveFileMapping removes memory-mapped file at the given base address.
46-
RemoveFileMapping(pid uint32, address va.Address) error
46+
RemoveFileMapping(pid uint32, addr va.Address) error
4747
// WriteFromKcap appends a new process state to the snapshotter from the captured kernel event.
4848
WriteFromKcap(kevt *kevent.Kevent) error
4949
// Remove deletes process's state from the snapshotter.

pkg/ps/snapshotter_mock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func (s *SnapshotterMock) RemoveThread(pid uint32, tid uint32) error {
9797
}
9898

9999
// RemoveModule method
100-
func (s *SnapshotterMock) RemoveModule(pid uint32, mod string) error {
101-
args := s.Called(pid, mod)
100+
func (s *SnapshotterMock) RemoveModule(pid uint32, addr va.Address) error {
101+
args := s.Called(pid, addr)
102102
return args.Error(0)
103103
}
104104

pkg/ps/snapshotter_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,14 @@ func (s *snapshotter) RemoveThread(pid uint32, tid uint32) error {
241241
return nil
242242
}
243243

244-
func (s *snapshotter) RemoveModule(pid uint32, module string) error {
244+
func (s *snapshotter) RemoveModule(pid uint32, addr va.Address) error {
245245
s.mu.Lock()
246246
defer s.mu.Unlock()
247247
proc, ok := s.procs[pid]
248248
if !ok {
249249
return nil
250250
}
251-
proc.RemoveModule(module)
251+
proc.RemoveModule(addr)
252252
moduleCount.Add(-1)
253253
return nil
254254
}

pkg/ps/types/types_windows.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,17 +407,17 @@ func (ps *PS) RemoveHandle(handle windows.Handle) {
407407

408408
// AddModule adds a new module to this process state.
409409
func (ps *PS) AddModule(mod Module) {
410-
m := ps.FindModule(mod.Name)
410+
m := ps.FindModuleByAddr(mod.BaseAddress)
411411
if m != nil {
412412
return
413413
}
414414
ps.Modules = append(ps.Modules, mod)
415415
}
416416

417417
// RemoveModule removes a specified module from this process state.
418-
func (ps *PS) RemoveModule(path string) {
418+
func (ps *PS) RemoveModule(addr va.Address) {
419419
for i, mod := range ps.Modules {
420-
if filepath.Base(mod.Name) == filepath.Base(path) {
420+
if mod.BaseAddress == addr {
421421
ps.Modules = append(ps.Modules[:i], ps.Modules[i+1:]...)
422422
break
423423
}
@@ -434,6 +434,16 @@ func (ps *PS) FindModule(path string) *Module {
434434
return nil
435435
}
436436

437+
// FindModuleByAddr finds the module by its base address.
438+
func (ps *PS) FindModuleByAddr(addr va.Address) *Module {
439+
for _, mod := range ps.Modules {
440+
if mod.BaseAddress == addr {
441+
return &mod
442+
}
443+
}
444+
return nil
445+
}
446+
437447
// FindModuleByVa finds the module name by
438448
// probing the range of the given virtual address.
439449
func (ps *PS) FindModuleByVa(addr va.Address) *Module {

0 commit comments

Comments
 (0)