Skip to content

Commit

Permalink
Compare version using VERSION_STRING
Browse files Browse the repository at this point in the history
  • Loading branch information
chadrako committed Mar 12, 2024
1 parent 7bbcc08 commit 80df890
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 59 deletions.
8 changes: 3 additions & 5 deletions make/modules/java.base/Launcher.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ ifeq ($(call isTargetOsType, unix), true)
NAME := jspawnhelper, \
SRC := $(TOPDIR)/src/$(MODULE)/unix/native/jspawnhelper, \
OPTIMIZATION := LOW, \
CFLAGS := $(CFLAGS_JDKEXE) -I$(TOPDIR)/src/$(MODULE)/unix/native/libjava \
-DVERSION_FEATURE=$(VERSION_FEATURE) \
-DVERSION_INTERIM=$(VERSION_INTERIM) \
-DVERSION_UPDATE=$(VERSION_UPDATE) \
-DVERSION_PATCH=$(VERSION_PATCH), \
CFLAGS := $(CFLAGS_JDKEXE) \
-I$(TOPDIR)/src/$(MODULE)/unix/native/libjava \
$(VERSION_CFLAGS), \
EXTRA_OBJECT_FILES := $(SUPPORT_OUTPUTDIR)/native/$(MODULE)/libjava/childproc$(OBJ_SUFFIX), \
LDFLAGS := $(LDFLAGS_JDKEXE), \
OUTPUT_DIR := $(SUPPORT_OUTPUTDIR)/modules_libs/$(MODULE), \
Expand Down
7 changes: 2 additions & 5 deletions make/modules/java.base/lib/CoreLibraries.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ TARGETS += $(BUILD_LIBVERIFY)

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

LIBJAVA_CFLAGS := -DARCHPROPNAME='"$(OPENJDK_TARGET_CPU_OSARCH)"' \
-DVERSION_FEATURE=$(VERSION_FEATURE) \
-DVERSION_INTERIM=$(VERSION_INTERIM) \
-DVERSION_UPDATE=$(VERSION_UPDATE) \
-DVERSION_PATCH=$(VERSION_PATCH)
LIBJAVA_CFLAGS := -DARCHPROPNAME='"$(OPENJDK_TARGET_CPU_OSARCH)"'

ifeq ($(call isTargetOs, macosx), true)
BUILD_LIBJAVA_java_props_md.c_CFLAGS := -x objective-c
Expand All @@ -63,6 +59,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \
CFLAGS := $(CFLAGS_JDKLIB) \
$(LIBJAVA_CFLAGS), \
jdk_util.c_CFLAGS := $(VERSION_CFLAGS), \
ProcessImpl_md.c_CFLAGS := $(VERSION_CFLAGS), \
WARNINGS_AS_ERRORS_xlc := false, \
DISABLED_WARNINGS_gcc_ProcessImpl_md.c := unused-result, \
LDFLAGS := $(LDFLAGS_JDKLIB) \
Expand Down
35 changes: 11 additions & 24 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,20 +51,8 @@ extern int errno;
#define ERR_PIPE 2
#define ERR_ARGS 3

#ifndef VERSION_FEATURE
#error VERSION_FEATURE must be defined
#endif

#ifndef VERSION_INTERIM
#error VERSION_INTERIM must be defined
#endif

#ifndef VERSION_UPDATE
#error VERSION_UPDATE must be defined
#endif

#ifndef VERSION_PATCH
#error VERSION_PATCH must be defined
#ifndef VERSION_STRING
#error VERSION_STRING must be defined
#endif

void error (int fd, int err) {
Expand Down Expand Up @@ -155,25 +144,23 @@ int main(int argc, char *argv[]) {
struct stat buf;
/* argv[1] contains the fd number to read all the child info */
int r, fdinr, fdinw, fdout;
int jdk_feature, jdk_interim, jdk_update, jdk_patch;
sigset_t unblock_signals;

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

#ifdef DEBUG
jtregSimulateCrash(0, 4);
#endif
r = sscanf (argv[1], "%d:%d:%d:%d:%d:%d:%d", &fdinr, &fdinw, &fdout, &jdk_feature, &jdk_interim, &jdk_update, &jdk_patch);
if (r == 7 && fcntl(fdinr, F_GETFD) != -1 && fcntl(fdinw, F_GETFD) != -1) {
// Check that JDK version and jspawnhelper version are the same
if (jdk_feature != VERSION_FEATURE || jdk_interim != VERSION_INTERIM || jdk_update != VERSION_UPDATE || jdk_patch != VERSION_PATCH) {
fprintf(stderr, "Expected jspawnhelper for Java %d.%d.%d.%d, ", jdk_feature, jdk_interim, jdk_update, jdk_patch);
fprintf(stderr, "but jspawnhelper for Java %d.%d.%d.%d was found.\n", VERSION_FEATURE, VERSION_INTERIM, VERSION_UPDATE, VERSION_PATCH);
shutItDown();
}

if (strcmp(argv[1], VERSION_STRING) != 0) {
fprintf(stderr, "Version check failed. jspawnhelper version: %s, JDK version: %s\n", VERSION_STRING, 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))
shutItDown();
Expand Down
32 changes: 10 additions & 22 deletions src/java.base/unix/native/libjava/ProcessImpl_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,20 +300,8 @@ Java_java_lang_ProcessImpl_init(JNIEnv *env, jclass clazz)
#define WTERMSIG(status) ((status)&0x7F)
#endif

#ifndef VERSION_FEATURE
#error VERSION_FEATURE must be defined
#endif

#ifndef VERSION_INTERIM
#error VERSION_INTERIM must be defined
#endif

#ifndef VERSION_UPDATE
#error VERSION_UPDATE must be defined
#endif

#ifndef VERSION_PATCH
#error VERSION_PATCH must be defined
#ifndef VERSION_STRING
#error VERSION_STRING must be defined
#endif

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

/* need to tell helper which fd is for receiving the childstuff
* and which fd to send response back on
*/
snprintf(buf1, sizeof(buf1), "%d:%d:%d:%d:%d:%d:%d", c->childenv[0], c->childenv[1], c->fail[1],
VERSION_FEATURE, VERSION_INTERIM, VERSION_UPDATE, VERSION_PATCH);

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
7 changes: 4 additions & 3 deletions test/jdk/java/lang/ProcessBuilder/JspawnhelperWarnings.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ private static void tryWithNArgs(int nArgs) throws Exception {
}

private static void testVersion() throws Exception {
String[] args = new String[2];
String[] args = new String[3];
args[0] = Paths.get(System.getProperty("java.home"), "lib", "jspawnhelper").toString();
args[1] = "1:1:1:1:1:1:1";
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("Expected jspawnhelper for Java 1.1.1.1");
oa.shouldContain("Version check failed");
}

public static void main(String[] args) throws Exception {
Expand Down

0 comments on commit 80df890

Please sign in to comment.