Skip to content

Commit

Permalink
JACOBIN-583 Added circularity-resolving MinimalAbort() function point…
Browse files Browse the repository at this point in the history
…er to globals. Updated trace.go to use it.
platypusguy committed Jan 30, 2025
1 parent e2b760f commit b41b83b
Showing 7 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/config/buildno.go
Original file line number Diff line number Diff line change
@@ -8,4 +8,4 @@

package config

var BuildNo = 3434
var BuildNo = 3436
2 changes: 1 addition & 1 deletion src/exceptions/catchFrame.go
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ func locateExceptionFrame(f *frames.Frame, excName string, pc int) (*frames.Fram
methEntry, found := classloader.MTable[fullMethName]
if !found {
errMsg := fmt.Sprintf("locateExceptionFrame: Method %s not found in MTable", fullMethName)
minimalAbort(excNames.InternalException, errMsg)
MinimalAbort(excNames.InternalException, errMsg)
}

if methEntry.MType != 'J' {
2 changes: 1 addition & 1 deletion src/exceptions/exceptions_test.go
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ func TestMinimalThrow(t *testing.T) {
r, w, _ := os.Pipe()
os.Stderr = w

minimalAbort(excNames.UnknownError, "just a test")
MinimalAbort(excNames.UnknownError, "just a test")
// restore stderr to what it was before
_ = w.Close()
out, _ := io.ReadAll(r)
10 changes: 5 additions & 5 deletions src/exceptions/throw.go
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ func ThrowEx(which int, msg string, f *frames.Frame) bool {

// Frame pointer provided?
if f == nil {
minimalAbort(which, msg) // this calls exit()
MinimalAbort(which, msg) // this calls exit()
return NotCaught // only occurs in tests
}

@@ -84,7 +84,7 @@ func ThrowEx(which int, msg string, f *frames.Frame) bool {
th, ok := glob.Threads[f.Thread].(*thread.ExecThread)
if !ok {
errMsg := fmt.Sprintf("[ThrowEx] glob.Threads index not found or entry corrupted, thread index: %d", f.Thread)
minimalAbort(excNames.InternalException, errMsg)
MinimalAbort(excNames.InternalException, errMsg)
}
fs := th.Stack

@@ -279,9 +279,9 @@ func generateThrowBytecodes(f *frames.Frame, exceptionCPname string, msg string)
}
*/

// minimalAbort is the exception thrown when the frame info is not available,
// MinimalAbort is the exception thrown when the frame info is not available,
// such as during start-up, if the main class can't be found, etc.
func minimalAbort(whichException int, msg string) {
func MinimalAbort(whichException int, msg string) {
var stack string
bytes := debug.Stack()
if len(bytes) > 0 {
@@ -293,7 +293,7 @@ func minimalAbort(whichException int, msg string) {
glob.ErrorGoStack = stack
errMsg := fmt.Sprintf("%s: %s", excNames.JVMexceptionNames[whichException], msg)
_, _ = fmt.Fprintln(os.Stderr, errMsg)
// errMsg := fmt.Sprintf("[ThrowEx][minimalAbort] %s", msg)
// errMsg := fmt.Sprintf("[ThrowEx][MinimalAbort] %s", msg)
// ShowPanicCause(errMsg)
// ShowFrameStack(&thread.ExecThread{})
ShowGoStackTrace(nil)
8 changes: 8 additions & 0 deletions src/globals/globals.go
Original file line number Diff line number Diff line change
@@ -99,6 +99,7 @@ type Globals struct {
// Get around the golang circular dependency. To be set up in jvmStart.go
// Enables gfunctions to call these functions through a global variable.
FuncInstantiateClass func(string, *list.List) (any, error)
FuncMinimalAbort func(int, string)
FuncThrowException func(int, string) bool
FuncFillInStackTrace func([]any) any
}
@@ -155,6 +156,7 @@ func InitGlobals(progName string) Globals {
GoStackShown: false,
FuncInstantiateClass: fakeInstantiateClass,
FuncThrowException: fakeThrowEx,
FuncMinimalAbort: fakeMinimalAbort,
}

TraceInit = false
@@ -318,6 +320,12 @@ func fakeThrowEx(whichEx int, msg string) bool {
return false
}

// Fake MinimalAbort() in exceptions.go
func fakeMinimalAbort(whichEx int, msg string) {
errMsg := fmt.Sprintf("\n*Attempt to access uninitialized MinimalAbort pointer func")
fmt.Fprintf(os.Stderr, errMsg)
}

func InitStringPool() {

StringPoolLock.Lock()
1 change: 1 addition & 0 deletions src/jvm/jvmStart.go
Original file line number Diff line number Diff line change
@@ -61,6 +61,7 @@ func JVMrun() int {

// Enable functions call InstantiateClass through a global function variable. (This avoids circularity issues.)
globPtr.FuncInstantiateClass = InstantiateClass
globPtr.FuncMinimalAbort = exceptions.MinimalAbort
globPtr.FuncThrowException = exceptions.ThrowExNil
globPtr.FuncFillInStackTrace = gfunction.FillInStackTrace

32 changes: 17 additions & 15 deletions src/trace/trace.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ import (
"jacobin/excNames"
"jacobin/globals"
"os"
"runtime/debug"
"sync"
"time"
)
@@ -81,18 +80,21 @@ func Warning(argMsg string) {

// Duplicated from minimalAbort in the exceptions package exceptions.go due to a Go-diagnosed cycle.
func abruptEnd(whichException int, msg string) {
var stack string
bytes := debug.Stack()
if len(bytes) > 0 {
stack = string(bytes)
} else {
stack = ""
}
glob := globals.GetGlobalRef()
glob.ErrorGoStack = stack
errMsg := fmt.Sprintf("%s: %s", excNames.JVMexceptionNames[whichException], msg)
_, _ = fmt.Fprintln(os.Stderr, errMsg)
// exceptions.ShowGoStackTrace(nil) <------------ causes Go-diagnosed cycle: classloader > exceptions > classloader
// _ = shutdown.Exit(shutdown.APP_EXCEPTION) <--- causes Go-diagnosed cycle: shutdown > statics > trace > shutdown
os.Exit(UNKNOWN_ERROR)
globals.GetGlobalRef().FuncMinimalAbort(whichException, msg)
/*
var stack string
bytes := debug.Stack()
if len(bytes) > 0 {
stack = string(bytes)
} else {
stack = ""
}
glob := globals.GetGlobalRef()
glob.ErrorGoStack = stack
errMsg := fmt.Sprintf("%s: %s", excNames.JVMexceptionNames[whichException], msg)
_, _ = fmt.Fprintln(os.Stderr, errMsg)
// exceptions.ShowGoStackTrace(nil) // <------------ causes Go-diagnosed cycle: classloader > exceptions > classloader
// _ = shutdown.Exit(shutdown.APP_EXCEPTION) <--- causes Go-diagnosed cycle: shutdown > statics > trace > shutdown
os.Exit(UNKNOWN_ERROR)
*/
}

0 comments on commit b41b83b

Please sign in to comment.