1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

Ensure that the memory returned by sysAlloc is always zeroed

This commit is contained in:
Achilleas Anagnostopoulos 2017-07-05 16:50:59 +01:00
parent dd63b23246
commit 40521f8626
2 changed files with 26 additions and 2 deletions

View File

@ -13,12 +13,14 @@ import (
var (
mapFn = vmm.Map
earlyReserveRegionFn = vmm.EarlyReserveRegion
memsetFn = mem.Memset
frameAllocFn = allocator.AllocFrame
mallocInitFn = mallocInit
algInitFn = algInit
modulesInitFn = modulesInit
typeLinksInitFn = typeLinksInit
itabsInitFn = itabsInit
initGoPackagesFn = initGoPackages
// A seed for the pseudo-random number generator used by getRandomData
prngSeed = 0xdeadc0de
@ -42,6 +44,13 @@ func mallocInit()
//go:linkname mSysStatInc runtime.mSysStatInc
func mSysStatInc(*uint64, uintptr)
// initGoPackages is an alias to main.init which recursively calls the init()
// methods in all imported packages. Unless this function is called, things like
// package errors will not be properly initialized causing various problems when
// we try to use the stdlib.
//go:linkname initGoPackages main.init
func initGoPackages()
// sysReserve reserves address space without allocating any memory or
// establishing any page mappings.
//
@ -117,6 +126,8 @@ func sysAlloc(size uintptr, sysStat *uint64) unsafe.Pointer {
if err = mapFn(page, frame, mapFlags); err != nil {
return unsafe.Pointer(uintptr(0))
}
memsetFn(page.Address(), 0, mem.PageSize)
}
mSysStatInc(sysStat, uintptr(regionSize))
@ -163,6 +174,8 @@ func Init() *kernel.Error {
typeLinksInitFn() // uses maps, activeModules
itabsInitFn() // uses activeModules
initGoPackagesFn()
return nil
}

View File

@ -136,6 +136,7 @@ func TestSysAlloc(t *testing.T) {
defer func() {
earlyReserveRegionFn = vmm.EarlyReserveRegion
mapFn = vmm.Map
memsetFn = mem.Memset
frameAllocFn = allocator.AllocFrame
}()
@ -161,10 +162,15 @@ func TestSysAlloc(t *testing.T) {
for specIndex, spec := range specs {
var (
sysStat uint64
mapCallCount int
sysStat uint64
mapCallCount int
memsetCallCount int
)
memsetFn = func(_ uintptr, _ byte, _ mem.Size) {
memsetCallCount++
}
mapFn = func(_ vmm.Page, _ pmm.Frame, flags vmm.PageTableEntryFlag) *kernel.Error {
expFlags := vmm.FlagPresent | vmm.FlagNoExecute | vmm.FlagRW
if flags != expFlags {
@ -182,6 +188,11 @@ func TestSysAlloc(t *testing.T) {
t.Errorf("[spec %d] expected vmm.Map call count to be %d; got %d", specIndex, spec.expMapCallCount, mapCallCount)
}
// sysAlloc should perform the same number of memset calls as map calls
if memsetCallCount != spec.expMapCallCount {
t.Errorf("[spec %d] expected mem.Memset call count to be %d; got %d", specIndex, spec.expMapCallCount, memsetCallCount)
}
if exp := uint64(spec.expMapCallCount << mem.PageShift); sysStat != exp {
t.Errorf("[spec %d] expected stat counter to be %d; got %d", specIndex, exp, sysStat)
}