Skip to content

Commit

Permalink
fully clean unsafe.Pointer usage against checkptr
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic committed Dec 20, 2019
1 parent 7fa72aa commit 6178a02
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
22 changes: 16 additions & 6 deletions pkg/powershell/chelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ func makeUintptrFromUint64(v uint64) uintptr {
func allocWrapper(size uint64) (uintptr, error) {
return nativePowerShell_DefaultAlloc(size)
}

func localAllocWrapper(size uint64) (uintptr, error) {
return localAlloc(size)
}

func freeWrapper(v uintptr) {
nativePowerShell_DefaultFree(v)
}

func mallocCopyLogStringHolder(input nativePowerShell_LogString_Holder) uintptr {
func localMallocCopyLogStringHolder(input nativePowerShell_LogString_Holder) uintptr {

size := uint64(unsafe.Sizeof(input))

data, err := allocWrapper(size)
data, err := localAllocWrapper(size)
if err != nil {
panic("Couldn't allocate memory")
}
Expand All @@ -32,16 +37,21 @@ func mallocCopyLogStringHolder(input nativePowerShell_LogString_Holder) uintptr
return data
}

func mallocCopyGenericPowerShellObject(input *nativePowerShell_GenericPowerShellObject, inputCount uint64) uintptr {
func mallocCopyArrayGenericPowerShellObject(input []nativePowerShell_GenericPowerShellObject) uintptr {

inputCount := uint64(len(input))

size := inputCount * uint64(unsafe.Sizeof(*input))
var size uint64
if inputCount != 0 {
size = inputCount * uint64(unsafe.Sizeof(input[0]))
}

data, err := allocWrapper(size)
if err != nil {
panic("Couldn't allocate memory")
}

_ = memcpyGenericPowerShellObject(data, input, size)
_ = memcpyGenericPowerShellObject(data, &input[0], size)

return data
}
Expand All @@ -63,7 +73,7 @@ func wsclen(str uintptr) uint64 {
var charCode uint16 = 1
var i uint64 = 0
for ; charCode != 0; i++ {
charCode = *((*uint16)(unsafe.Pointer(str + (makeUintptrFromUint64(i) * unsafe.Sizeof(charCode)))))
charCode = castToUint16(str + (makeUintptrFromUint64(i) * unsafe.Sizeof(charCode)))
}
return i
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/powershell/hostcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (writer *callbackResultsWriter) filloutResults(res uintptr) {

if writer.objects != nil && len(writer.objects) > 0 {
results.count = uint32(len(writer.objects))
results.objects = mallocCopyGenericPowerShellObject(&writer.objects[0], uint64(len(writer.objects)))
results.objects = mallocCopyArrayGenericPowerShellObject(writer.objects)
}
_ = memcpyJsonReturnValues(res, results)
}
3 changes: 1 addition & 2 deletions pkg/powershell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ func (command psCommand) Invoke() *InvokeResults {
}

func makePowerShellObjectIndexed(objects uintptr, index uint32) Object {
// I don't get why I have to use unsafe.Pointer on C memory
var handle nativePowerShell_PowerShellObject

offset := (uintptr(index) * unsafe.Sizeof(handle))
handle = *(*nativePowerShell_PowerShellObject)(unsafe.Pointer(objects + offset))
handle = castToPowershellObject(objects + offset)
return makePowerShellObject(handle)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/powershell/runspacehelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var (
// intentionally leak loggerCallbackPointer (once per process)
// doing this so the callback object will be around for lifetime of process
// If it was golang object just marshalled for duration of call, golang garbage collection could move things and we would crash
loggerCallbackPointer uintptr = mallocCopyLogStringHolder(loggerCallbackHolder)
loggerCallbackPointer uintptr = localMallocCopyLogStringHolder(loggerCallbackHolder)
commandCallbackPointer uintptr = syscall.NewCallbackCDecl(commandCallback)
)

Expand Down
21 changes: 21 additions & 0 deletions pkg/powershell/zpsh_host.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6178a02

Please sign in to comment.