process-api: ProcessBuilder and Process API module (pipe-buffer deadlock and its fixes, timeouts and process-tree kill, grandchild holding the pipe, environment/redirect/stdin, ProcessHandle, startPipeline, shell and option injection), ProcessRunner, 14 contract tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3W3uHbHAmM3orhn7b
This commit is contained in:
Ankur
2026-11-26 00:48:44 +05:30
co-authored by Claude Sonnet 5.5
parent 47e4ccbefa
commit a714780bca
21 changed files with 952 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
$ java -version
openjdk version "25.0.5.1" 2026-11-17 LTS
OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS)
OpenJDK 64-Bit Server VM Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS, mixed mode, sharing)
$ uname -sr
Linux 6.18.44-fc-v880
+18
View File
@@ -0,0 +1,18 @@
== 1. the smallest thing that works
stdout = hello from a child process
exit code = 0
== 2. a failing command is NOT an exception: it is a non-zero exit code
stderr = ls: cannot access '/definitely/not/here': No such file or directory
exit code = 2
== 3. a command that does not exist IS an exception, thrown by start()
IOException: Cannot run program "no-such-binary-xyz": Exec failed, error: 2 (No such file or directory)
== 4. exitValue() before the process ends throws
IllegalThreadStateException: process hasn't exited
waitFor(200ms) returned false (false = still running, true = exited)
after destroyForcibly(): exit code = 137 (128 + SIGKILL 9 = 137 on Linux)
== 5. the shell is not involved: no pipes, no globs, no $VARS
stdout = *.txt $HOME a|b
+19
View File
@@ -0,0 +1,19 @@
== 1. WRONG: waitFor() first, read afterwards
child exited within 3s? false
child still alive? true
bytes sitting in the pipe: 65536
== 2. RIGHT (one stream): read to EOF, then waitFor()
read 200000 bytes, exit code 0
== 3. STILL WRONG: read stdout to EOF, but the child also fills stderr
stdout reader finished within 3s? false
== 4. FIX A: merge stderr into stdout, one stream to drain
read 400000 bytes, exit code 0
== 5. FIX B: drain both streams concurrently (what ProcessRunner does)
stdout=200000 stderr=200000 exit=0 timedOut=false
== 6. FIX C: do not pipe at all. Send output to a file or discard it
waitFor() returned immediately with exit code 0
+20
View File
@@ -0,0 +1,20 @@
== 1. waitFor(timeout) returns false; it does NOT stop the process
waitFor(500ms) = false
still alive = true
destroy() then waitFor -> exit code 143 (128 + SIGTERM 15 = 143)
== 2. a shell command spawns a child; destroying the shell orphans it
descendants of the shell: 1 -> [/usr/bin/sleep]
shell dead, orphan alive = true <- leaked process
== 3. kill the tree: snapshot descendants first, then kill all
shell alive = false, former child alive = false
== 4. ProcessRunner with a deadline
timedOut=true exit=-1 stdout=started elapsed<2s=true
== 5. a background grandchild keeps the pipe open: readAllBytes() waits for IT
sh exited at once, but readAllBytes() took ~4s; got done|late|
== 6. ProcessRunner is not held hostage by the same command
exit=0 stdout=done| returned in under 2s: true (bounded by the 1s reader grace, not by the 4s grandchild)
+21
View File
@@ -0,0 +1,21 @@
== 1. the child inherits a COPY of our environment
child sees : GREETING=hello MODE=
our own env: GREETING=null (unchanged)
== 2. clear() and rebuild: pass only what the child needs
child sees : HOME=[] PATH=[/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin] MODE=[minimal]
== 3. which PATH finds the program? start() uses OURS; the child's PATH is for the child
start("date") with child PATH=/nonexistent : true (resolved with the parent's PATH)
sh -c 'date +%Y' with child PATH=/nonexistent : sh: 1: date: not found
== 4. working directory
ls in temp dir: marker.txt
bad directory -> IOException: Cannot run program "ls" (in directory "/no/such/dir"): Exec failed, error: 2 (No such file or directory)
== 5. redirect to files: no pipe, so nothing to drain
exit 0, file contains: to-stdout | to-stderr |
getInputStream() on a redirected process is a null stream: read() = -1
== 6. stdin: close it, or a reader like cat never sees EOF
cat echoed: line one | line two | exit 0
+21
View File
@@ -0,0 +1,21 @@
== 1. our own process
pid > 0: true, command ends with java: true
has a parent: true
== 2. onExit() instead of a blocked thread
main thread is free; alive = true
callback: exited with 3
== 3. what ProcessHandle.Info reveals about a child
command = /usr/bin/sleep
arguments = 5
user set = true, startInstant set = true
== 4. a handle to a finished process
alive after exit: false
== 5. a real pipeline without a shell: printf | sort | uniq -c
1 a
3 b
1 c
stages: 3
+25
View File
@@ -0,0 +1,25 @@
== 1. VULNERABLE: build a shell string from user input
$ sh -c wc -c report.txt; cat secret.txt
18 report.txt
TOP-SECRET [exit 0]
== 2. SAFE: argument list, no shell. The whole input is one filename
$ wc -c report.txt; cat secret.txt
wc: 'report.txt; cat secret.txt': No such file or directory [exit 1]
== 3. SAFE but still a trap: option injection. Input '-l' is read as a flag
$ ls -l
total 8
-rw-r--r-- 1 root root 18 Nov 26 00:48 report.txt
-rw-r--r-- 1 root root 11 Nov 26 00:48 secret.txt [exit 0]
(the caller wanted to list a file called '-l'; ls took it as an option)
== 4. FIX: '--' ends option parsing, then validate
$ ls -- -l
ls: cannot access '-l': No such file or directory [exit 2]
== 5. FIX: allow-list the input before it gets anywhere near a command
'report.txt' -> true
'report.txt; cat secret.txt' -> false
'-l' -> false
'../etc/passwd' -> false
+4
View File
@@ -0,0 +1,4 @@
-------------------------------------------------------------------------------
Test set: com.ankurm.process.ProcessClaimsTest
-------------------------------------------------------------------------------
Tests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 16.26 s -- in com.ankurm.process.ProcessClaimsTest