Skip to content

Undefined behaviour when calling ioctl() multiple times #1632

@Gieted

Description

@Gieted

JNA version: 5.15.0
JVM: GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22)
OS: macOS 14.7
CPU: Apple M1 (64-bit)

public interface LibC extends Library {
    LibC libC = Native.load("c", LibC.class);
    int TIOCGWINSZ = 0x40087468; // this is definitely correct, I've checked it with C
    
    int ioctl(int fd, int request, winsize winsize);  
}

public static int getWindowSize() {
    val ttysize = new winsize();

    val returnCode = libC.ioctl(0, TIOCGWINSZ, ttysize);
    if (returnCode != 0) {
        throw new RuntimeException("There was a problem calling ioctl(): " + returnCode);
    }

    return ttysize.ws_col;
}

 for (int i = 0; i < 100; i++) {
      System.out.println(getWindowSize());
}

running this code results in either:

  1. ioctl() returning -1
  2. SIGSEGV
  3. Illegal instruction: 4
  4. Heap corruption detected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions