From 15b14aeca0c6d98ecaad3ffbd66eeecc1dbbc458 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Tue, 27 Jun 2017 11:03:17 -0700
Subject: [PATCH] LLVM: switch to ZipInputStream from ZipFile; fix PCRE library
 load bug

---
 .../r/ffi/impl/llvm/TruffleLLVM_DLL.java      | 55 +++++++------------
 .../r/ffi/impl/llvm/TruffleLLVM_PCRE.java     |  2 +-
 2 files changed, 21 insertions(+), 36 deletions(-)

diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DLL.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DLL.java
index ac0e87d34a..6a4a50bb5a 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DLL.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_DLL.java
@@ -23,14 +23,14 @@
 package com.oracle.truffle.r.ffi.impl.llvm;
 
 import java.io.BufferedInputStream;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.file.FileSystems;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
 
 import com.oracle.truffle.api.CallTarget;
 import com.oracle.truffle.api.source.Source;
@@ -104,12 +104,10 @@ public class TruffleLLVM_DLL implements DLLRFFI {
     static class LLVM_Handle {
         private final String libName;
         private final LLVM_IR[] irs;
-        private final boolean isZip;
 
-        LLVM_Handle(String libName, LLVM_IR[] irs, boolean isZip) {
+        LLVM_Handle(String libName, LLVM_IR[] irs) {
             this.libName = libName;
             this.irs = irs;
-            this.isZip = isZip;
         }
     }
 
@@ -118,38 +116,26 @@ public class TruffleLLVM_DLL implements DLLRFFI {
         boolean match(String name);
     }
 
-    public static boolean useZip(String path) {
-        String ziplibsProp = System.getenv("FASTR_ZIP_LIBS");
-        if (ziplibsProp == null) {
-            return false;
-        }
-        Path p = Paths.get(path);
-        String fileName = p.getFileName().toString();
-        fileName = fileName.substring(0, fileName.lastIndexOf('.'));
-        String[] ziplibs = ziplibsProp.split(",");
-        for (String ziplib : ziplibs) {
-            if (ziplib.equals(fileName)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
     public static LLVM_IR[] getZipLLVMIR(String path) {
-        try (ZipFile z = new ZipFile(path)) {
-            int numEntries = z.size();
-            LLVM_IR[] result = new LLVM_IR[numEntries];
-            Enumeration<? extends ZipEntry> entries = z.entries();
-            int index = 0;
-            while (entries.hasMoreElements()) {
-                ZipEntry entry = entries.nextElement();
-                try (BufferedInputStream bis = new BufferedInputStream(z.getInputStream(entry))) {
-                    byte[] bc = new byte[(int) entry.getSize()];
-                    bis.read(bc);
+        try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(path)))) {
+            ArrayList<LLVM_IR> irList = new ArrayList<>();
+            while (true) {
+                ZipEntry entry = zis.getNextEntry();
+                if (entry == null) {
+                    break;
+                }
+                int size = (int) entry.getSize();
+                byte[] bc = new byte[size];
+                int n;
+                int totalRead = 0;
+                while (totalRead < size && (n = zis.read(bc, totalRead, size - totalRead)) != -1) {
+                    totalRead += n;
                     LLVM_IR ir = new LLVM_IR.Binary(entry.getName(), bc);
-                    result[index++] = ir;
+                    irList.add(ir);
                 }
             }
+            LLVM_IR[] result = new LLVM_IR[irList.size()];
+            irList.toArray(result);
             return result;
         } catch (IOException ex) {
             // not a zip file
@@ -169,7 +155,6 @@ public class TruffleLLVM_DLL implements DLLRFFI {
         @Override
         public Object execute(String path, boolean local, boolean now) {
             try {
-                boolean isZip = false;
                 LLVM_IR[] irs = getZipLLVMIR(path);
                 if (irs == null) {
                     return tryOpenNative(path, local, now);
@@ -182,7 +167,7 @@ public class TruffleLLVM_DLL implements DLLRFFI {
                 for (LLVM_IR ir : irs) {
                     parseLLVM(libName, ir);
                 }
-                return new LLVM_Handle(libName, irs, isZip);
+                return new LLVM_Handle(libName, irs);
             } catch (
 
             Exception ex) {
diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_PCRE.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_PCRE.java
index 805688b759..cb36908153 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_PCRE.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/llvm/TruffleLLVM_PCRE.java
@@ -44,7 +44,7 @@ public class TruffleLLVM_PCRE implements PCRERFFI {
     TruffleLLVM_PCRE() {
         // Need to ensure that the native pcre library is loaded
         String pcrePath = LibPaths.getBuiltinLibPath("pcre");
-        TruffleLLVM_NativeDLL.NativeDLOpenRootNode.create().getCallTarget().call(pcrePath);
+        TruffleLLVM_NativeDLL.NativeDLOpenRootNode.create().getCallTarget().call(pcrePath, false, true);
     }
 
     private static class TruffleLLVM_MaketablesNode extends MaketablesNode {
-- 
GitLab