From 0b9b2936518603b8bfa41b31f6ba95f3d511a018 Mon Sep 17 00:00:00 2001 From: Lucas Teske Date: Wed, 9 Sep 2020 13:51:00 -0300 Subject: [PATCH] nintendoswitch: Fix invalid memory read / write in print calls --- src/runtime/runtime_nintendoswitch.go | 17 +++++++---------- src/runtime/runtime_nintendoswitch_heap.go | 8 +------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/runtime/runtime_nintendoswitch.go b/src/runtime/runtime_nintendoswitch.go index 690f559e..26712dda 100644 --- a/src/runtime/runtime_nintendoswitch.go +++ b/src/runtime/runtime_nintendoswitch.go @@ -40,21 +40,18 @@ func ticks() timeUnit { return timeUnit(ticksToNanoseconds(timeUnit(getArmSystemTick()))) } -var stdoutBuffer = make([]byte, 0, 120) +var stdoutBuffer = make([]byte, 120) +var position = 0 func putchar(c byte) { - if c == '\n' || len(stdoutBuffer)+1 >= 120 { - NxOutputString(string(stdoutBuffer)) - stdoutBuffer = stdoutBuffer[:0] + if c == '\n' || position > len(stdoutBuffer) { + nxOutputString(&stdoutBuffer[0], uint64(position)) + position = 0 return } - stdoutBuffer = append(stdoutBuffer, c) -} - -func usleep(usec uint) int { - sleepThread(uint64(usec) * 1000) - return 0 + stdoutBuffer[position] = c + position++ } func abort() { diff --git a/src/runtime/runtime_nintendoswitch_heap.go b/src/runtime/runtime_nintendoswitch_heap.go index 5518825e..e8df7e4b 100644 --- a/src/runtime/runtime_nintendoswitch_heap.go +++ b/src/runtime/runtime_nintendoswitch_heap.go @@ -4,17 +4,11 @@ package runtime -import "unsafe" - const heapSize = 0x2000000 * 16 // Default by libnx -//go:extern _stack_top -var stackTopSymbol [0]byte - var ( heapStart = uintptr(0) heapEnd = uintptr(0) - stackTop = uintptr(unsafe.Pointer(&stackTopSymbol)) ) //export setHeapSize @@ -24,7 +18,7 @@ func preinit() { setHeapSize(&heapStart, heapSize) if heapStart == 0 { - panic("failed to allocate heap") + runtimePanic("failed to allocate heap") } heapEnd = heapStart + heapSize