Skip to content

Crash when calling cuStreamSetAttribute #47

Description

@bleenhou

A bug in the writeStreamAttributeValueToNative JNI code causes an issue when deserializing the CUaccessPolicyWindow Object.

import jcuda.*;
import jcuda.driver.*;

import static jcuda.driver.JCudaDriver.*;

public class StreamAttributeExample {
    public static void main(String[] args) {
        JCudaDriver.setExceptionsEnabled(true);

        // Initialize CUDA
        cuInit(0);

        CUdevice device = new CUdevice();
        cuDeviceGet(device, 0);

        CUcontext context = new CUcontext();
        cuCtxCreate(context, 0, device);

        // Create stream
        CUstream stream = new CUstream();
        cuStreamCreate(stream, 0);

        // Allocate some device memory
        CUdeviceptr ptr = new CUdeviceptr();
        long size = 1024 * 1024; // 1 MB
        cuMemAlloc(ptr, size);

        // --- Access policy window setup ---
        CUaccessPolicyWindow window = new CUaccessPolicyWindow();
        window.base_ptr = ptr;
        window.num_bytes = size;
        window.hitRatio = 1.0f;
        window.hitProp = CUaccessProperty.CU_ACCESS_PROPERTY_PERSISTING;
        window.missProp = CUaccessProperty.CU_ACCESS_PROPERTY_STREAMING;

        // --- Attribute wrapper (IMPORTANT) ---
        CUstreamAttrValue attr = new CUstreamAttrValue();

        // Only set the field corresponding to the attribute!
        attr.accessPolicyWindow = window;

        // --- Apply attribute ---
        int result = cuStreamSetAttribute(
            stream,
            CUstreamAttrID.CU_STREAM_ATTRIBUTE_ACCESS_POLICY_WINDOW,
            attr
        );

        if (result != CUresult.CUDA_SUCCESS) {
            System.out.println("Error: " + CUresult.stringFor(result));
        }

        // Cleanup
        cuMemFree(ptr);
        cuStreamDestroy(stream);
        cuCtxDestroy(context);
    }
}
# A fatal error has been detected by the Java Runtime Environment: 
# SIGSEGV (0xb) at pc=0x00007c9a7e3af9db, pid=620345, tid=620352 
# JRE version: OpenJDK Runtime Environment (21.0.10+7) (build 21.0.10+7-Ubuntu-124.04) 
# Java VM: OpenJDK 64-Bit Server VM (21.0.10+7-Ubuntu-124.04, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64) 
# Problematic frame: # V [libjvm.so+0x9af9db] jni_GetLongField+0xab

Starting JVM with -Xcheck:jni gives

FATAL ERROR in native method: Wrong field ID passed to JNI

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions