java-native-access / java-native-access/jna

Undefined behaviour when calling ioctl() multiple times

Open
#1,632 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
8.9k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the LibC interface and getWindowSize() reproduction in the issue, then run the loop on the stated macOS, Apple M1, and GraalVM setup. Inspect how the winsize argument is represented and passed through JNA during repeated ioctl() calls. Done means repeated calls no longer return errors or terminate with SIGSEGV, illegal instructions, or heap corruption.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.