Skip to content

Commit

Permalink
8325621: Improve jspawnhelper version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrako committed Mar 5, 2024
1 parent b38a6c5 commit 02d1b6f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
8 changes: 8 additions & 0 deletions make/autoconf/jdk-version.m4
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,13 @@ AC_DEFUN_ONCE([JDKVER_SETUP_JDK_VERSION_NUMBERS],
# VERSION_NUMBER but always with exactly 4 positions, with 0 for empty positions.
VERSION_NUMBER_FOUR_POSITIONS=$VERSION_FEATURE.$VERSION_INTERIM.$VERSION_UPDATE.$VERSION_PATCH
# VERSION_NUMBER but always with exactly 4 positions, with 0 padding for all positions.
VERSION_FEATURE_PADDING=$(printf "%04d" $VERSION_FEATURE)
VERSION_INTERIM_PADDING=$(printf "%04d" $VERSION_INTERIM)
VERSION_UPDATE_PADDING=$(printf "%04d" $VERSION_UPDATE)
VERSION_PATCH_PADDING=$(printf "%04d" $VERSION_PATCH)
VERSION_NUMBER_FOUR_POSITIONS_PADDING=$VERSION_FEATURE_PADDING.$VERSION_INTERIM_PADDING.$VERSION_UPDATE_PADDING.$VERSION_PATCH_PADDING
# VERSION_NUMBER but always with all positions, with 0 for empty positions.
VERSION_NUMBER_ALL_POSITIONS=$VERSION_NUMBER_FOUR_POSITIONS.$VERSION_EXTRA1.$VERSION_EXTRA2.$VERSION_EXTRA3
Expand Down Expand Up @@ -571,6 +578,7 @@ AC_DEFUN_ONCE([JDKVER_SETUP_JDK_VERSION_NUMBERS],
AC_SUBST(VERSION_OPT)
AC_SUBST(VERSION_NUMBER)
AC_SUBST(VERSION_NUMBER_FOUR_POSITIONS)
AC_SUBST(VERSION_NUMBER_FOUR_POSITIONS_PADDING)
AC_SUBST(VERSION_STRING)
AC_SUBST(VERSION_SHORT)
AC_SUBST(VERSION_IS_GA)
Expand Down
2 changes: 2 additions & 0 deletions make/autoconf/spec.gmk.template
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ VERSION_OPT := @VERSION_OPT@
VERSION_NUMBER := @VERSION_NUMBER@
# VERSION_NUMBER but always with exactly 4 positions, with 0 for empty positions.
VERSION_NUMBER_FOUR_POSITIONS := @VERSION_NUMBER_FOUR_POSITIONS@
# VERSION_NUMBER but always with exactly 4 positions, with 0 padding for all positions.
VERSION_NUMBER_FOUR_POSITIONS_PADDING := @VERSION_NUMBER_FOUR_POSITIONS_PADDING@
# The complete version string, with additional build information
VERSION_STRING := @VERSION_STRING@
# The short version string, without trailing zeroes and just PRE, if present.
Expand Down
2 changes: 1 addition & 1 deletion make/modules/java.base/Launcher.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ 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) -I$(TOPDIR)/src/$(MODULE)/unix/native/libjava -DVERSION_NUMBER_FOUR_POSITIONS_PADDING='"$(VERSION_NUMBER_FOUR_POSITIONS_PADDING)"', \
EXTRA_OBJECT_FILES := $(SUPPORT_OUTPUTDIR)/native/$(MODULE)/libjava/childproc$(OBJ_SUFFIX), \
LDFLAGS := $(LDFLAGS_JDKEXE), \
OUTPUT_DIR := $(SUPPORT_OUTPUTDIR)/modules_libs/$(MODULE), \
Expand Down
2 changes: 1 addition & 1 deletion make/modules/java.base/lib/CoreLibraries.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TARGETS += $(BUILD_LIBVERIFY)

##########################################################################################

LIBJAVA_CFLAGS := -DARCHPROPNAME='"$(OPENJDK_TARGET_CPU_OSARCH)"'
LIBJAVA_CFLAGS := -DARCHPROPNAME='"$(OPENJDK_TARGET_CPU_OSARCH)"' -DVERSION_NUMBER_FOUR_POSITIONS_PADDING='"$(VERSION_NUMBER_FOUR_POSITIONS_PADDING)"'

ifeq ($(call isTargetOs, macosx), true)
BUILD_LIBJAVA_java_props_md.c_CFLAGS := -x objective-c
Expand Down
10 changes: 10 additions & 0 deletions src/java.base/unix/native/jspawnhelper/jspawnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand All @@ -50,6 +51,10 @@ extern int errno;
#define ERR_PIPE 2
#define ERR_ARGS 3

#ifndef VERSION_NUMBER_FOUR_POSITIONS_PADDING
#error VERSION_NUMBER_FOUR_POSITIONS_PADDING 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 Down Expand Up @@ -134,6 +139,11 @@ void initChildStuff (int fdin, int fdout, ChildStuff *c) {
}

int main(int argc, char *argv[]) {
if (argc == 2 && strcmp(argv[1], "-version") == 0) {
fprintf(stdout, "%s\n", VERSION_NUMBER_FOUR_POSITIONS_PADDING);
exit (0);
}

ChildStuff c;
struct stat buf;
/* argv[1] contains the fd number to read all the child info */
Expand Down
29 changes: 29 additions & 0 deletions src/java.base/unix/native/libjava/ProcessImpl_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <sys/wait.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>

#include <spawn.h>

Expand Down Expand Up @@ -609,6 +610,10 @@ startChild(JNIEnv *env, jobject process, ChildStuff *c, const char *helperpath)
}
}

#ifndef VERSION_NUMBER_FOUR_POSITIONS_PADDING
#error VERSION_NUMBER_FOUR_POSITIONS_PADDING must be defined
#endif

JNIEXPORT jint JNICALL
Java_java_lang_ProcessImpl_forkAndExec(JNIEnv *env,
jobject process,
Expand All @@ -631,6 +636,12 @@ Java_java_lang_ProcessImpl_forkAndExec(JNIEnv *env,
const char *penvBlock = NULL;
ChildStuff *c;

char *phelperpathversion = NULL;
FILE *stream = NULL;

const int version_length = 20;
char version_data[version_length];

in[0] = in[1] = out[0] = out[1] = err[0] = err[1] = fail[0] = fail[1] = -1;
childenv[0] = childenv[1] = -1;
// Reset errno to protect against bogus error messages
Expand All @@ -655,6 +666,19 @@ Java_java_lang_ProcessImpl_forkAndExec(JNIEnv *env,
c->argc = argc + 2;
initVectorFromBlock(c->argv+1, pargBlock, argc);

// Check that jspawnhelper is correct version
const char *dash_version = " -version";
if ((phelperpathversion = NEW(char, strlen(phelperpath) + strlen(dash_version) + 1)) == NULL) goto Catch;
strcpy(phelperpathversion, phelperpath);
strcat(phelperpathversion, dash_version);

stream = popen(phelperpathversion, "r");
fgets(version_data, version_length, stream);

if (strcmp(VERSION_NUMBER_FOUR_POSITIONS_PADDING, version_data) != 0) {
throwIOException(env, errno, "Incorrect jspawnhelper version");
}

if (envBlock != NULL) {
/* Convert envBlock into a char ** envv */
if ((penvBlock = getBytes(env, envBlock)) == NULL) goto Catch;
Expand Down Expand Up @@ -787,6 +811,11 @@ Java_java_lang_ProcessImpl_forkAndExec(JNIEnv *env,
free(c->argv);
free(c->envv);
free(c);
free(phelperpathversion);

if (stream) {
pclose(stream);
}

if (fds != NULL)
(*env)->ReleaseIntArrayElements(env, std_fds, fds, 0);
Expand Down

0 comments on commit 02d1b6f

Please sign in to comment.