Module: Tebako::Packager::PatchLiterals

Included in:
Pass2NonMSysPatch, Pass2Patch
Defined in:
lib/tebako/packager/patch_literals.rb

Overview

Ruby patching literals (pass2)

Constant Summary collapse

TOOL_MKCONFIG_RB_SUBST =
<<~SUBST
  # -- Start of tebako patch --
      v_head_comp = "  CONFIG[\\"prefix\\"] \#{eq} "
      if v_head_comp == v[0...(v_head_comp.length)]
          v = "\#{v[0...(v_head_comp.length)]}'/__tebako_memfs__'
  "
      end
      v_head_comp = "  CONFIG[\\"RUBY_EXEC_PREFIX\\"] \#{eq} "
      if v_head_comp == v[0...(v_head_comp.length)]
        v = "\#{v[0...(v_head_comp.length)]}'/__tebako_memfs__'
  "
      end
  # -- End of tebako patch --
      if fast[name]
SUBST
TOOL_MKCONFIG_RB_SUBST_MSYS =
<<~SUBST
  # -- Start of tebako patch --
      v_head_comp = "  CONFIG[\\"prefix\\"] \#{eq} "
      if v_head_comp == v[0...(v_head_comp.length)]
        v = "\#{v[0...(v_head_comp.length)]}CONFIG[\\"RUBY_EXEC_PREFIX\\"] = 'A:/__tebako_memfs__'
  "
      end
      v_head_comp = "  CONFIG[\\"RUBY_EXEC_PREFIX\\"] \#{eq} "
      if v_head_comp == v[0...(v_head_comp.length)]
        v = "\#{v[0...(v_head_comp.length)]}'A:/__tebako_memfs__'
  "
      end
  # -- End of tebako patch --
      if fast[name]
SUBST
LINUX_MUSL_THREAD_PTHREAD_PATCH =

-- Patch no. 3 -- hack in "ENABLE_PATH_CHECK" disabling to suppress: "warning: Insecure world writable dir" Applied in CMakeLists.txt

{
  "#if MAINSTACKADDR_AVAILABLE && !defined(get_main_stack)" => <<~SUBST
    /* -- Start of tebako patch -- */
    #if defined(__linux__) && !defined(__GLIBC__) && defined(HAVE_GETRLIMIT)
    #ifndef PAGE_SIZE
    #include <unistd.h>
    #define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
    #endif
    static int
    get_main_stack(void **addr, size_t *size)
    {
        size_t start, end, limit, prevend = 0;
        struct rlimit r;
        FILE *f;
        char buf[PATH_MAX+80], s[8];
        int n;
        STACK_GROW_DIR_DETECTION;
        f = fopen("/proc/self/maps", "re");
        if (!f)
            return -1;
        n = 0;
        while (fgets(buf, sizeof buf, f)) {
            n = sscanf(buf, "%zx-%zx %*s %*s %*s %*s %7s", &start, &end, s);
            if (n >= 2) {
                if (n == 3 && strcmp(s, "[stack]") == 0)
                    break;
                prevend = end;
            }
            n = 0;
        }
        fclose(f);
        if (n == 0)
            return -1;
        limit = 100 << 20; /* 100MB stack limit */
        if (getrlimit(RLIMIT_STACK, &r)==0 && r.rlim_cur < limit)
            limit = r.rlim_cur & -PAGE_SIZE;
        if (limit > end) limit = end;
        if (prevend < end - limit) prevend = end - limit;
        if (start > prevend) start = prevend;
        *addr = IS_STACK_DIR_UPPER() ? (void *)start : (void *)end;
        *size = end - start;
        return 0;
    }
    #else
    /* -- End of tebako patch -- */
  SUBST
}.freeze
DIR_C_BASE_PATCH_ONE =

Compensate ruby incorrect processing of (f)getattrlist returning ENOTSUP Note. We are not patching need_normalization function In this function (f)getattrlist failure with ENOTSUP is processed correctly

<<~SUBST
  #if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
      /* tebako patch */ if (!within_tebako_memfs(path))
SUBST
DIR_C_BASE_PATCH_TWO =
<<~SUBST
  #if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
        /* tebako patch */ if (!within_tebako_memfs(path)) plain = 1; else magical = 1;
SUBST
DIR_C_BASE_PATCH =
{
  "#if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT" => DIR_C_BASE_PATCH_ONE,
  "#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME\n	    plain = 1;" => DIR_C_BASE_PATCH_TWO,
  "if (is_case_sensitive(dirp, path) == 0)" =>
    "if (is_case_sensitive(dirp, path) == 0 /* tebako patch */ && !within_tebako_memfs(path))",
  "if ((*cur)->type == ALPHA) {" =>
    "if ((*cur)->type == ALPHA /* tebako patch */ && !within_tebako_memfs(buf)) {",
  "else if (e == EIO) {" =>
    "else if (e == EIO /* tebako patch */ && !within_tebako_memfs(path)) {"
}.freeze
DLN_C_PRE34_PATCH_PATTERN =
"static const char funcname_prefix[sizeof(FUNCNAME_PREFIX) - 1] = FUNCNAME_PREFIX;"
DLN_C_PATCH_PATTERN =
"#define init_funcname(buf, file) build_funcname(FUNCNAME_PREFIX, buf, file)"
DLN_C_MSYS_PATCH_PRE32 =
{
  "    winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);" => <<~SUBST
    /* -- Start of tebako patch -- */
      char *f = NULL;
      winfile = NULL;
      if (file && within_tebako_memfs(file)) {
        f = tebako_dlmap2file(file);
        if (f) {
          winfile = rb_w32_mbstr_to_wstr(CP_UTF8, f, -1, NULL);
          free(f);
        }
        else {
          goto failed;
        }
      }
      else {
        winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);
      }
    /* -- End of tebako patch -- */
  SUBST
}.freeze
DLN_C_MSYS_PATCH =
{
  "    WCHAR *winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);" => <<~SUBST
    /* -- Start of tebako patch -- */
      char *f = NULL;
      WCHAR *winfile = NULL;
      if (file && within_tebako_memfs(file)) {
        f = tebako_dlmap2file(file);
        if (f) {
          winfile = rb_w32_mbstr_to_wstr(CP_UTF8, f, -1, NULL);
          free(f);
        }
        else {
          goto failed;
        }
      }
      else {
        winfile = rb_w32_mbstr_to_wstr(CP_UTF8, file, -1, NULL);
      }
    /* -- End of tebako patch -- */
  SUBST
}.freeze
C_FILE_SUBST =
<<~SUBST
  /* -- Start of tebako patch -- */
  #include <tebako/tebako-config.h>
  #include <tebako/tebako-defines.h>
  #include <tebako/tebako-io.h>
  /* -- End of tebako patch -- */
SUBST
C_FILE_SUBST_LESS =
<<~SUBST
  /* -- Start of tebako patch -- */
  #include <tebako/tebako-config.h>
  #include <tebako/tebako-io.h>
  /* -- End of tebako patch -- */
SUBST
IO_C_SUBST =
<<~SUBST
  /* -- Start of tebako patch -- */
      if (is_tebako_file_descriptor(fd)) return;
      /* -- End of tebako patch -- */
      flags = fcntl(fd, F_GETFD); /* should not fail except EBADF. */
SUBST
FILE_C_MSYS_SUBST =
<<~SUBST
  /* -- Start of tebako patch -- */
         if (is_tebako_file_descriptor((fptr)->fd)) return ENOTSUP;
         /* -- End of tebako patch -- */
         while ((int)rb_thread_io_blocking_region(rb_thread_flock, op, fptr->fd) < 0) {
SUBST
FILE_C_MSYS_PATCH =
{
  "while ((int)rb_thread_io_blocking_region(rb_thread_flock, op, fptr->fd) < 0) {" => FILE_C_MSYS_SUBST
}.freeze
LINUX_PATCHES =
{
  "ext/extmk.rb" => {
    "mf.macro \"EXTLIBS\", $extlibs" => "#  mf.macro \"EXTLIBS\", $extlibs   tebako patched"
  }
}.freeze
PRISM_PATTERN_1 =
"pm_string_init_result_t init_result = pm_read_file(&result->input, RSTRING_PTR(filepath));"
PRISM_SUBST_1 =
<<~SUBST
  /* -- Start of tebako patch -- */
      pm_string_init_result_t init_result;
      if (within_tebako_memfs(RSTRING_PTR(filepath)))
      {
        init_result = tebako_string_file_init(&result->input, RSTRING_PTR(filepath));
      }
      else
      {
        init_result = pm_read_file(&result->input, RSTRING_PTR(filepath));
      }
      /* -- End of tebako patch -- */
SUBST
PRISM_PATTERN_2 =
"#include \"prism.h\""
PRISM_SUBST_2 =
<<~SUBST
  #{PRISM_PATTERN_2}

  /* -- Start of tebako patch -- */
  #include <tebako/tebako-config.h>
  #include <tebako/tebako-defines.h>
  #include <tebako/tebako-io.h>
  #include <tebako/tebako-prism.h>
  /* -- End of tebako patch -- */

SUBST
PRISM_PATCHES =
{
  PRISM_PATTERN_1 => PRISM_SUBST_1,
  PRISM_PATTERN_2 => PRISM_SUBST_2
}.freeze