Give the MJIT header path name

Give the whole MJIT header path name by preloaded shared library
mjit_build_dir.so, than building the path from a given directory
name and the embedded base name.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66280 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-12-08 01:50:39 +00:00
parent 2301054a99
commit e15d9d86df
3 changed files with 33 additions and 42 deletions

View File

@ -596,7 +596,8 @@ mjit_config.h:
echo '#define RUBY_MJIT_CONFIG_H 1'; \
echo; \
sep=; \
quote MJIT_MIN_HEADER_NAME "/$(MJIT_HEADER_INSTALL_DIR)/$(MJIT_MIN_HEADER_NAME)"; \
quote MJIT_HEADER_INSTALL_DIR "/$(MJIT_HEADER_INSTALL_DIR)"; \
quote MJIT_MIN_HEADER_NAME "$(MJIT_MIN_HEADER_NAME)"; \
sep=,; \
quote "MJIT_CC_COMMON " $(MJIT_CC); \
quote "MJIT_CFLAGS MJIT_ARCHFLAG" $(MJIT_CFLAGS); \
@ -613,6 +614,8 @@ mjit_config.h:
} > $@
yes-test-almost yes-test-all: mjit_build_dir.$(SOEXT)
mjit_build_dir.$(SOEXT): $(MJIT_MIN_HEADER) $(srcdir)/ruby-runner.c ruby-runner.h
mjit_build_dir.$(SOEXT): $(MJIT_MIN_HEADER) $(srcdir)/ruby-runner.c ruby-runner.h mjit_config.h
$(ECHO) making $@
$(Q) $(DLDSHARED) $(MJIT_DLDFLAGS) $(ARCH_FLAG) $(CFLAGS) $(CPPFLAGS) -DMAKE_MJIT_BUILD_DIR=1 $(OUTFLAG)$@ $(srcdir)/ruby-runner.c
$(Q) $(DLDSHARED) $(MJIT_DLDFLAGS) $(ARCH_FLAG) $(CFLAGS) $(CPPFLAGS) \
-DMAKE_MJIT_BUILD_DIR=1 -DMJIT_MIN_HEADER='"$(MJIT_MIN_HEADER)"' \
$(OUTFLAG)$@ $(srcdir)/ruby-runner.c

64
mjit.c
View File

@ -365,10 +365,12 @@ static int
init_header_filename(void)
{
int fd;
#ifdef LOAD_RELATIVE
/* Root path of the running ruby process. Equal to RbConfig::TOPDIR. */
VALUE basedir_val;
const char *basedir;
size_t baselen;
#endif
const char *basedir = NULL;
size_t baselen = 0;
char *p;
#ifdef _WIN32
static const char libpathflag[] =
@ -380,33 +382,32 @@ init_header_filename(void)
;
const size_t libpathflag_len = sizeof(libpathflag) - 1;
#endif
#ifndef LOAD_RELATIVE
const char *build_dir = 0;
struct stat st;
#endif
#ifdef LOAD_RELATIVE
basedir_val = ruby_prefix_path;
basedir = StringValuePtr(basedir_val);
baselen = RSTRING_LEN(basedir_val);
#ifndef LOAD_RELATIVE
#else
if (getenv("MJIT_SEARCH_BUILD_DIR")) {
/* This path is not intended to be used on production, but using build directory's
header file here because people want to run `make test-all` without running
`make install`. Don't use $MJIT_SEARCH_BUILD_DIR except for test-all. */
build_dir = dlsym(RTLD_DEFAULT, "MJIT_BUILD_DIR");
if (!build_dir) {
verbose(1, "No mjit_build_directory");
struct stat st;
const char *hdr = dlsym(RTLD_DEFAULT, "MJIT_HEADER");
if (!hdr) {
verbose(1, "No MJIT_HEADER");
}
else if (build_dir[0] != '/') {
verbose(1, "Non-absolute path MJIT_BUILD_DIR: %s", build_dir);
else if (hdr[0] != '/') {
verbose(1, "Non-absolute header file path: %s", hdr);
}
else if (stat(build_dir, &st) || !S_ISDIR(st.st_mode)) {
verbose(1, "Non-directory path MJIT_BUILD_DIR: %s", build_dir);
else if (stat(hdr, &st) || !S_ISREG(st.st_mode)) {
verbose(1, "Non-file header file path: %s", hdr);
}
else if (!rb_path_check(build_dir)) {
verbose(1, "Unsafe MJIT_BUILD_DIR: %s", build_dir);
else if ((st.st_uid != getuid()) || (st.st_mode & 022) ||
!rb_path_check(hdr)) {
verbose(1, "Unsafe header file: uid=%ld mode=%#o %s",
(long)st.st_uid, (unsigned)st.st_mode, hdr);
return FALSE;
}
else {
@ -415,43 +416,30 @@ init_header_filename(void)
verbose(3, "PRELOADENV("PRELOADENV")=%s", getenv(PRELOADENV));
/* assume no other PRELOADENV in test-all */
unsetenv(PRELOADENV);
verbose(3, "MJIT_BUILD_DIR: %s", build_dir);
basedir = build_dir;
baselen = strlen(build_dir);
}
verbose(3, "MJIT_HEADER: %s", hdr);
header_file = ruby_strdup(hdr);
if (!header_file) return FALSE;
}
else
#endif
#ifndef _MSC_VER
{
/* A name of the header file included in any C file generated by MJIT for iseqs. */
static const char header_name[] = MJIT_MIN_HEADER_NAME;
static const char header_name[] = MJIT_HEADER_INSTALL_DIR "/" MJIT_MIN_HEADER_NAME;
const size_t header_name_len = sizeof(header_name) - 1;
header_file = xmalloc(baselen + header_name_len + 1);
p = append_str2(header_file, basedir, baselen);
p = append_str2(p, header_name, header_name_len + 1);
}
#ifndef _MSC_VER
{
if ((fd = rb_cloexec_open(header_file, O_RDONLY, 0)) < 0) {
verbose(1, "Cannot access header file: %s", header_file);
xfree(header_file);
header_file = NULL;
return FALSE;
}
#ifndef LOAD_RELATIVE
if (basedir == build_dir) {
memset(&st, 0, sizeof(st));
if (fstat(fd, &st) ||
(st.st_uid != getuid()) ||
(st.st_mode & 022)) {
(void)close(fd);
verbose(1, "Unsafe header file: uid=%ld mode=%#o %s",
(long)st.st_uid, (unsigned)st.st_mode, header_file);
xfree(header_file);
header_file = NULL;
return FALSE;
}
}
#endif
(void)close(fd);
}

View File

@ -10,7 +10,7 @@
#include "ruby/config.h"
#ifdef MAKE_MJIT_BUILD_DIR
const char MJIT_BUILD_DIR[] = BUILDDIR;
const char MJIT_HEADER[] = BUILDDIR "/" MJIT_MIN_HEADER;
#else
#define STRINGIZE(expr) STRINGIZE0(expr)