Skip to content

SIGSEGV in unique.canonMap[string].cleanup via log_write #268

Backlog

Description

@ignoramous

A12 on SM-S918U (v058 e211):

Crashed: Thread: SIGSEGV  0x0000000000000000
#00 pc 0xd587ee libgojni.so (unique.(*canonMap[go.shape.string]).cleanup [canonmap.go:187]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)
#01 pc 0xd586a7 libgojni.so (unique.(*canonMap[go.shape.string]).LoadOrStore.func1 [canonmap.go:138]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)
#02 pc 0xa12354 libgojni.so (runtime.callCleanup[go.shape.struct {}] [mcleanup.go:205]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)
#03 pc 0xd58725 libgojni.so (runtime.AddCleanup[go.shape.string,go.shape.struct {}].func1 [mcleanup.go:183]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)
#04 pc 0x98aa94 libgojni.so (runtime.runCleanups [mcleanup.go:726]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)
#05 pc 0x14355a0 libgojni.so (_cgo_1ebd2d8f1b1c_Cfunc___android_log_write [cgo-gcc-prolog:63]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)
#06 pc 0x9f2e41 libgojni.so (runtime.goexit [asm_amd64.s:1265]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)
#07 pc 0x131f898 libgojni.so (_cgo_1ebd2d8f1b1c_Cfunc___android_log_write [cgo-gcc-prolog:63]) (BuildId: b8350bb8099fd5f4001083c34ebf832f63676164)

Gemini 4:

Potential Causes

  1. Memory Management Incompatibility (Go Runtime vs. Android 12 Memory Allocator): Android 12 introduced Scudo as the default hardened memory allocator. On certain Samsung devices running Android 12, customized kernel memory optimizations or aggressive process/thread termination strategies can cause the memory spaces managed by the Go runtime (which bypasses the standard JVM/ART allocator) to be prematurely reclaimed, unmapped, or zeroed out.
  2. Unsafe JNI / Garbage Collection Race Condition: The unique package (introduced in Go 1.23 for interning values) relies on weak pointers and active GC sweeps. If a Java/Kotlin thread interacts with a Go-bound string through JNI, and the JVM garbage collects the wrapper object while the Go runtime is running cleanup on the underlying canonMap, a null pointer is encountered.
  3. Samsung-Specific Background Thread Restrictions: Samsung’s custom Android OS layer (One UI) aggressively manages background threads and resource limits on Android 12. If the OS forces a background thread running the Go scheduler into a low-resource state or suspends it mid-execution, memory structures within canonmap.go can become corrupted or partially deallocated.

Recommendations

  1. Avoid Go 1.23+ unique Package Features in JNI Boundary: The unique package's underlying concurrent map optimization (canonMap) may not behave predictably when exposed to the unstable threading lifecycle of Android 12 on Samsung devices. If you are using Go 1.23+ features that implicitly use unique (such as string interning libraries), revert to standard Go maps protected by read-write mutexes (sync.RWMutex) for string caching.

  2. Configure Android 12 Memory Allocation Behavior: To prevent Android 12's Scudo allocator or Samsung's aggressive memory management from corrupting the Go runtime heap, ensure the native library loaded via JNI is pinned or loaded in a way that prevents premature unloading. Keep a strong reference to the JNI class wrapper in JVM memory so that the garbage collector does not trigger a teardown on the Go side while cleanup is running:

Prevent GC from reclaiming the JNI bridge prematurely on Android 12
public class GoBridge {
    private static GoBridge instance;

    public static synchronized void initialize() {
        if (instance == null) {
            System.loadLibrary("go_native_lib");
            instance = new GoBridge();
        }
    }
    
    // Keep reference alive explicitly during long-running operations
    public void executeNativeCall(String input) {
        try {
            nativeProcessString(input);
        } finally {
            // ReachabilityFence ensures 'this' is not GC'ed during the native call
            java.lang.ref.Reference.reachabilityFence(this);
        }
    }

    private native void nativeProcessString(String input);
}
  1. Native Signal Chaining for SIGSEGV handling: On Android 12, custom ROMs like Samsung's One UI can intercept signals or alter the signal mask. Ensure that Go's signal handling does not conflict with the Android Runtime (ART) signal handlers. When compiling your Go shared library, compile with target options that respect Android signal chaining. If compiling using gomobile, ensure you build with the latest stable Go version that contains patches targeting Android 12 memory compatibility:
Force reconstruction of native archives using Go compiler flags that minimize aggressive inlining of map cleanups
gomobile bind -target=android -ldflags="-shared" ./yourgopackage

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions