Skip to content

[GR-64789] [Native Image] FFI Bug #11109

Open
@akashKarmakar02

Description

@akashKarmakar02

Describe the Issue

When i pass a struct to c function @cstruct annotated interface it's values are garbage

Using the latest version of GraalVM can resolve many issues.

GraalVM Version

java version "24" 2025-03-18
Java(TM) SE Runtime Environment Oracle GraalVM 24+36.1 (build 24+36-jvmci-b01)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 24+36.1 (build 24+36-jvmci-b01, mixed mode, sharing)

Operating System and Version

Linux fedora 6.14.3-300.fc42.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Apr 20 16:08:39 UTC 2025 x86_64 GNU/Linux

Troubleshooting Confirmation

Run Command

native-image -cp . -H:CLibraryPath=$(pwd) -H:IncludeResources=add.h --native-compiler-options="-I$(pwd)" -Ob -jar Main.jar

Expected Behavior

5
Age: 5

Actual Behavior

5
Age: -678812048

Steps to Reproduce

1.create a c function which takes a struct as param
2. pass the struct obj from java
3. print the properties in c and java

Additional Context

java code:

import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.struct.CField;
import org.graalvm.nativeimage.c.struct.CStruct;
import org.graalvm.word.PointerBase;

@CStruct("User")
@CContext(Main.Directives.class)
public interface User extends PointerBase {

    @CField("age")
    int age();

    @CField("age")
    void age(int age);

}
 User u = StackValue.get(User.class);

u.age(5);
System.out.println(u.age());

print_user(u);

C code:

void print_user(User u) {
    printf("Age: %d", u.age);
}

Run-Time Log Output and Error Messages

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions