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:
parent
dd63b23246
commit
40521f8626
@ -13,12 +13,14 @@ import (
|
|||||||
var (
|
var (
|
||||||
mapFn = vmm.Map
|
mapFn = vmm.Map
|
||||||
earlyReserveRegionFn = vmm.EarlyReserveRegion
|
earlyReserveRegionFn = vmm.EarlyReserveRegion
|
||||||
|
memsetFn = mem.Memset
|
||||||
frameAllocFn = allocator.AllocFrame
|
frameAllocFn = allocator.AllocFrame
|
||||||
mallocInitFn = mallocInit
|
mallocInitFn = mallocInit
|
||||||
algInitFn = algInit
|
algInitFn = algInit
|
||||||
modulesInitFn = modulesInit
|
modulesInitFn = modulesInit
|
||||||
typeLinksInitFn = typeLinksInit
|
typeLinksInitFn = typeLinksInit
|
||||||
itabsInitFn = itabsInit
|
itabsInitFn = itabsInit
|
||||||
|
initGoPackagesFn = initGoPackages
|
||||||
|
|
||||||
// A seed for the pseudo-random number generator used by getRandomData
|
// A seed for the pseudo-random number generator used by getRandomData
|
||||||
prngSeed = 0xdeadc0de
|
prngSeed = 0xdeadc0de
|
||||||
@ -42,6 +44,13 @@ func mallocInit()
|
|||||||
//go:linkname mSysStatInc runtime.mSysStatInc
|
//go:linkname mSysStatInc runtime.mSysStatInc
|
||||||
func mSysStatInc(*uint64, uintptr)
|
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
|
// sysReserve reserves address space without allocating any memory or
|
||||||
// establishing any page mappings.
|
// establishing any page mappings.
|
||||||
//
|
//
|
||||||
@ -117,6 +126,8 @@ func sysAlloc(size uintptr, sysStat *uint64) unsafe.Pointer {
|
|||||||
if err = mapFn(page, frame, mapFlags); err != nil {
|
if err = mapFn(page, frame, mapFlags); err != nil {
|
||||||
return unsafe.Pointer(uintptr(0))
|
return unsafe.Pointer(uintptr(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memsetFn(page.Address(), 0, mem.PageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
mSysStatInc(sysStat, uintptr(regionSize))
|
mSysStatInc(sysStat, uintptr(regionSize))
|
||||||
@ -163,6 +174,8 @@ func Init() *kernel.Error {
|
|||||||
typeLinksInitFn() // uses maps, activeModules
|
typeLinksInitFn() // uses maps, activeModules
|
||||||
itabsInitFn() // uses activeModules
|
itabsInitFn() // uses activeModules
|
||||||
|
|
||||||
|
initGoPackagesFn()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,6 +136,7 @@ func TestSysAlloc(t *testing.T) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
earlyReserveRegionFn = vmm.EarlyReserveRegion
|
earlyReserveRegionFn = vmm.EarlyReserveRegion
|
||||||
mapFn = vmm.Map
|
mapFn = vmm.Map
|
||||||
|
memsetFn = mem.Memset
|
||||||
frameAllocFn = allocator.AllocFrame
|
frameAllocFn = allocator.AllocFrame
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -161,10 +162,15 @@ func TestSysAlloc(t *testing.T) {
|
|||||||
|
|
||||||
for specIndex, spec := range specs {
|
for specIndex, spec := range specs {
|
||||||
var (
|
var (
|
||||||
sysStat uint64
|
sysStat uint64
|
||||||
mapCallCount int
|
mapCallCount int
|
||||||
|
memsetCallCount int
|
||||||
)
|
)
|
||||||
|
|
||||||
|
memsetFn = func(_ uintptr, _ byte, _ mem.Size) {
|
||||||
|
memsetCallCount++
|
||||||
|
}
|
||||||
|
|
||||||
mapFn = func(_ vmm.Page, _ pmm.Frame, flags vmm.PageTableEntryFlag) *kernel.Error {
|
mapFn = func(_ vmm.Page, _ pmm.Frame, flags vmm.PageTableEntryFlag) *kernel.Error {
|
||||||
expFlags := vmm.FlagPresent | vmm.FlagNoExecute | vmm.FlagRW
|
expFlags := vmm.FlagPresent | vmm.FlagNoExecute | vmm.FlagRW
|
||||||
if flags != expFlags {
|
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)
|
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 {
|
if exp := uint64(spec.expMapCallCount << mem.PageShift); sysStat != exp {
|
||||||
t.Errorf("[spec %d] expected stat counter to be %d; got %d", specIndex, exp, sysStat)
|
t.Errorf("[spec %d] expected stat counter to be %d; got %d", specIndex, exp, sysStat)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user