Skip to content

Commit

Permalink
8325621: Improve jspawnhelper version checks
Browse files Browse the repository at this point in the history
Reviewed-by: shade
Backport-of: a232e8fb4e6e9e2e9a5285bf01c93b8d1d995f04
  • Loading branch information
chadrako authored and shipilev committed Jun 17, 2024
1 parent 0fc9b02 commit d056b73
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
3 changes: 2 additions & 1 deletion make/modules/java.base/Launcher.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ ifeq ($(call isTargetOs, macosx aix linux), true)
NAME := jspawnhelper, \
SRC := $(TOPDIR)/src/$(MODULE)/unix/native/jspawnhelper, \
OPTIMIZATION := LOW, \
CFLAGS := $(CFLAGS_JDKEXE) -I$(TOPDIR)/src/$(MODULE)/unix/native/libjava, \
CFLAGS := $(CFLAGS_JDKEXE) $(VERSION_CFLAGS) \
-I$(TOPDIR)/src/$(MODULE)/unix/native/libjava, \
EXTRA_OBJECT_FILES := $(SUPPORT_OUTPUTDIR)/native/$(MODULE)/libjava/childproc.o, \
LDFLAGS := $(LDFLAGS_JDKEXE), \
OUTPUT_DIR := $(SUPPORT_OUTPUTDIR)/modules_libs/$(MODULE), \
Expand Down
1 change: 1 addition & 0 deletions make/modules/java.base/lib/CoreLibraries.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \
CFLAGS := $(CFLAGS_JDKLIB) \
$(LIBJAVA_CFLAGS), \
jdk_util.c_CFLAGS := $(VERSION_CFLAGS), \
ProcessImpl_md.c_CFLAGS := $(VERSION_CFLAGS), \
EXTRA_HEADER_DIRS := libfdlibm, \
WARNINGS_AS_ERRORS_xlc := false, \
DISABLED_WARNINGS_gcc := unused-result unused-function, \
Expand Down
28 changes: 22 additions & 6 deletions src/java.base/unix/native/jspawnhelper/jspawnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand All @@ -49,6 +50,10 @@ extern int errno;
#define ERR_PIPE 2
#define ERR_ARGS 3

#ifndef VERSION_STRING
#error VERSION_STRING must be defined
#endif

void error (int fd, int err) {
if (write (fd, &err, sizeof(err)) != sizeof(err)) {
/* Not sure what to do here. I have no one to speak to. */
Expand All @@ -58,6 +63,7 @@ void error (int fd, int err) {
}

void shutItDown() {
fprintf(stdout, "jspawnhelper version %s\n", VERSION_STRING);
fprintf(stdout, "This command is not for general use and should ");
fprintf(stdout, "only be run as the result of a call to\n");
fprintf(stdout, "ProcessBuilder.start() or Runtime.exec() in a java ");
Expand Down Expand Up @@ -143,19 +149,29 @@ int main(int argc, char *argv[]) {
/* argv[1] contains the fd number to read all the child info */
int r, fdinr, fdinw, fdout;

if (argc != 2) {
shutItDown();
}

#ifdef DEBUG
jtregSimulateCrash(0, 4);
#endif
r = sscanf (argv[1], "%d:%d:%d", &fdinr, &fdinw, &fdout);

if (argc != 3) {
fprintf(stdout, "Incorrect number of arguments: %d\n", argc);
shutItDown();
}

if (strcmp(argv[1], VERSION_STRING) != 0) {
fprintf(stdout, "Incorrect Java version: %s\n", argv[1]);
shutItDown();
}

r = sscanf (argv[2], "%d:%d:%d", &fdinr, &fdinw, &fdout);
if (r == 3 && fcntl(fdinr, F_GETFD) != -1 && fcntl(fdinw, F_GETFD) != -1) {
fstat(fdinr, &buf);
if (!S_ISFIFO(buf.st_mode))
if (!S_ISFIFO(buf.st_mode)) {
fprintf(stdout, "Incorrect input pipe\n");
shutItDown();
}
} else {
fprintf(stdout, "Incorrect FD array data: %s\n", argv[2]);
shutItDown();
}
// Close the writing end of the pipe we use for reading from the parent.
Expand Down
14 changes: 10 additions & 4 deletions src/java.base/unix/native/libjava/ProcessImpl_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ Java_java_lang_ProcessImpl_init(JNIEnv *env, jclass clazz)
#define WTERMSIG(status) ((status)&0x7F)
#endif

#ifndef VERSION_STRING
#error VERSION_STRING must be defined
#endif

static const char *
getBytes(JNIEnv *env, jbyteArray arr)
{
Expand Down Expand Up @@ -491,7 +495,7 @@ spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath)
jboolean isCopy;
int i, offset, rval, bufsize, magic;
char *buf, buf1[(3 * 11) + 3]; // "%d:%d:%d\0"
char *hlpargs[3];
char *hlpargs[4];
SpawnInfo sp;

/* need to tell helper which fd is for receiving the childstuff
Expand All @@ -500,11 +504,13 @@ spawnChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath)
snprintf(buf1, sizeof(buf1), "%d:%d:%d", c->childenv[0], c->childenv[1], c->fail[1]);
/* NULL-terminated argv array.
* argv[0] contains path to jspawnhelper, to follow conventions.
* argv[1] contains the fd string as argument to jspawnhelper
* argv[1] contains the version string as argument to jspawnhelper
* argv[2] contains the fd string as argument to jspawnhelper
*/
hlpargs[0] = (char*)helperpath;
hlpargs[1] = buf1;
hlpargs[2] = NULL;
hlpargs[1] = VERSION_STRING;
hlpargs[2] = buf1;
hlpargs[3] = NULL;

/* Following items are sent down the pipe to the helper
* after it is spawned.
Expand Down
21 changes: 20 additions & 1 deletion test/jdk/java/lang/ProcessBuilder/JspawnhelperWarnings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/*
* @test
* @bug 8325567
* @bug 8325567 8325621
* @requires (os.family == "linux") | (os.family == "aix") | (os.family == "mac")
* @library /test/lib
* @run driver JspawnhelperWarnings
Expand All @@ -47,11 +47,30 @@ private static void tryWithNArgs(int nArgs) throws Exception {
OutputAnalyzer oa = new OutputAnalyzer(p);
oa.shouldHaveExitValue(1);
oa.shouldContain("This command is not for general use");
if (nArgs != 2) {
oa.shouldContain("Incorrect number of arguments");
} else {
oa.shouldContain("Incorrect Java version");
}
}

private static void testVersion() throws Exception {
String[] args = new String[3];
args[0] = Paths.get(System.getProperty("java.home"), "lib", "jspawnhelper").toString();
args[1] = "wrongVersion";
args[2] = "1:1:1";
Process p = ProcessTools.startProcess("jspawnhelper", new ProcessBuilder(args));
OutputAnalyzer oa = new OutputAnalyzer(p);
oa.shouldHaveExitValue(1);
oa.shouldContain("This command is not for general use");
oa.shouldContain("Incorrect Java version: wrongVersion");
}

public static void main(String[] args) throws Exception {
for (int nArgs = 0; nArgs < 10; nArgs++) {
tryWithNArgs(nArgs);
}

testVersion();
}
}

1 comment on commit d056b73

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.