From e41db0cbdd1713a35f26b6f88ab5c2f27aa5cc61 Mon Sep 17 00:00:00 2001 From: Lucas Teske Date: Tue, 8 Sep 2020 18:06:42 -0300 Subject: [PATCH 1/3] nintendoswitch: Fix invalid memory read / write in print calls --- src/runtime/runtime_nintendoswitch.go | 17 +++++++---------- src/runtime/runtime_nintendoswitch_heap.go | 14 +++++++------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/runtime/runtime_nintendoswitch.go b/src/runtime/runtime_nintendoswitch.go index 690f559e1c..842a1ae0d4 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, 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 >= 119 { + 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 5518825e3c..5555c739a6 100644 --- a/src/runtime/runtime_nintendoswitch_heap.go +++ b/src/runtime/runtime_nintendoswitch_heap.go @@ -4,17 +4,12 @@ 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)) + failedToAllocateHeapString = []byte("failed to allocate heap") ) //export setHeapSize @@ -24,7 +19,12 @@ func preinit() { setHeapSize(&heapStart, heapSize) if heapStart == 0 { - panic("failed to allocate heap") + // Panic should work, but if we call panic here, it triggers a compiler bug that breaks + // all print calls in the whole application. So far we couldn't figure out why, that's + // why we're using a byte array and nxOutputString directly + nxOutputString(&failedToAllocateHeapString[0], uint64(len(failedToAllocateHeapString))) + exit(1) + //panic("failed to allocate heap") } heapEnd = heapStart + heapSize From ad8e5482622f593651c66f01ef55337f52c8df8e Mon Sep 17 00:00:00 2001 From: Lucas Teske Date: Tue, 8 Sep 2020 18:29:07 -0300 Subject: [PATCH 2/3] nintendoswitch: fix formatting in runtime_nintendoswitch_heap.go --- src/runtime/runtime_nintendoswitch_heap.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/runtime_nintendoswitch_heap.go b/src/runtime/runtime_nintendoswitch_heap.go index 5555c739a6..56ef5ebdaa 100644 --- a/src/runtime/runtime_nintendoswitch_heap.go +++ b/src/runtime/runtime_nintendoswitch_heap.go @@ -7,8 +7,8 @@ package runtime const heapSize = 0x2000000 * 16 // Default by libnx var ( - heapStart = uintptr(0) - heapEnd = uintptr(0) + heapStart = uintptr(0) + heapEnd = uintptr(0) failedToAllocateHeapString = []byte("failed to allocate heap") ) From 0bc06e30ce0ad92f2d609ee69036a0941b0ce2b7 Mon Sep 17 00:00:00 2001 From: Lucas Teske Date: Wed, 9 Sep 2020 13:13:13 -0300 Subject: [PATCH 3/3] nintendoswitch: some fixes / chores for runtime --- src/runtime/runtime_nintendoswitch.go | 4 ++-- src/runtime/runtime_nintendoswitch_heap.go | 12 +++--------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/runtime/runtime_nintendoswitch.go b/src/runtime/runtime_nintendoswitch.go index 842a1ae0d4..26712ddab0 100644 --- a/src/runtime/runtime_nintendoswitch.go +++ b/src/runtime/runtime_nintendoswitch.go @@ -40,11 +40,11 @@ func ticks() timeUnit { return timeUnit(ticksToNanoseconds(timeUnit(getArmSystemTick()))) } -var stdoutBuffer = make([]byte, 120, 120) +var stdoutBuffer = make([]byte, 120) var position = 0 func putchar(c byte) { - if c == '\n' || position >= 119 { + if c == '\n' || position > len(stdoutBuffer) { nxOutputString(&stdoutBuffer[0], uint64(position)) position = 0 return diff --git a/src/runtime/runtime_nintendoswitch_heap.go b/src/runtime/runtime_nintendoswitch_heap.go index 56ef5ebdaa..e8df7e4bc1 100644 --- a/src/runtime/runtime_nintendoswitch_heap.go +++ b/src/runtime/runtime_nintendoswitch_heap.go @@ -7,9 +7,8 @@ package runtime const heapSize = 0x2000000 * 16 // Default by libnx var ( - heapStart = uintptr(0) - heapEnd = uintptr(0) - failedToAllocateHeapString = []byte("failed to allocate heap") + heapStart = uintptr(0) + heapEnd = uintptr(0) ) //export setHeapSize @@ -19,12 +18,7 @@ func preinit() { setHeapSize(&heapStart, heapSize) if heapStart == 0 { - // Panic should work, but if we call panic here, it triggers a compiler bug that breaks - // all print calls in the whole application. So far we couldn't figure out why, that's - // why we're using a byte array and nxOutputString directly - nxOutputString(&failedToAllocateHeapString[0], uint64(len(failedToAllocateHeapString))) - exit(1) - //panic("failed to allocate heap") + runtimePanic("failed to allocate heap") } heapEnd = heapStart + heapSize