From 8cabcd2de8b46c42dffcaaf46ccc2dc4d092ebba Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Thu, 20 Jun 2013 20:01:38 +0200
Subject: DynamicLookupHelper: Add secure isFunctionAvailable(..);
 NativeLibrary: Add TODO comments, re protected lookup functions.

---
 .../com/jogamp/common/os/DynamicLibraryBundle.java |  6 +++
 .../com/jogamp/common/os/DynamicLookupHelper.java  |  7 ++-
 src/java/com/jogamp/common/os/NativeLibrary.java   | 63 ++++++++++++++++++----
 3 files changed, 65 insertions(+), 11 deletions(-)

(limited to 'src/java/com/jogamp/common/os')

diff --git a/src/java/com/jogamp/common/os/DynamicLibraryBundle.java b/src/java/com/jogamp/common/os/DynamicLibraryBundle.java
index b47c69b..fc36908 100644
--- a/src/java/com/jogamp/common/os/DynamicLibraryBundle.java
+++ b/src/java/com/jogamp/common/os/DynamicLibraryBundle.java
@@ -357,6 +357,7 @@ public class DynamicLibraryBundle implements DynamicLookupHelper {
         return 0;
     }
     
+    @Override
     public final long dynamicLookupFunction(String funcName) {
         if(!isToolLibLoaded() || null==funcName) {
             if(DEBUG_LOOKUP && !isToolLibLoaded()) {
@@ -383,6 +384,11 @@ public class DynamicLibraryBundle implements DynamicLookupHelper {
         }
         return addr;
     }
+    
+    @Override
+    public final boolean isFunctionAvailable(String funcName) {
+        return 0 != dynamicLookupFunction(funcName);
+    }
 
     /** Inherit access */
     static final class GlueJNILibLoader extends JNILibLoaderBase {
diff --git a/src/java/com/jogamp/common/os/DynamicLookupHelper.java b/src/java/com/jogamp/common/os/DynamicLookupHelper.java
index 20fd01e..94a7e92 100644
--- a/src/java/com/jogamp/common/os/DynamicLookupHelper.java
+++ b/src/java/com/jogamp/common/os/DynamicLookupHelper.java
@@ -51,7 +51,12 @@ public interface DynamicLookupHelper {
   public static final boolean DEBUG_LOOKUP = Debug.debug("NativeLibrary.Lookup");
   
   /**
-   * Try to fetch the function pointer for function 'funcName'.
+   * Returns the function handle for function 'funcName'.
    */
   public long dynamicLookupFunction(String funcName);
+  
+  /**
+   * Queries whether function 'funcName' is available.
+   */
+  public boolean isFunctionAvailable(String funcName);
 }
diff --git a/src/java/com/jogamp/common/os/NativeLibrary.java b/src/java/com/jogamp/common/os/NativeLibrary.java
index d34620e..9a86209 100644
--- a/src/java/com/jogamp/common/os/NativeLibrary.java
+++ b/src/java/com/jogamp/common/os/NativeLibrary.java
@@ -40,8 +40,15 @@
 
 package com.jogamp.common.os;
 
-import com.jogamp.common.util.IOUtil;
-import com.jogamp.common.util.cache.TempJarCache;
+import java.io.File;
+import java.lang.reflect.Method;
+import java.net.URISyntaxException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
 
 import jogamp.common.os.BionicDynamicLinkerImpl;
 import jogamp.common.os.MacOSXDynamicLinkerImpl;
@@ -49,11 +56,8 @@ import jogamp.common.os.PlatformPropsImpl;
 import jogamp.common.os.PosixDynamicLinkerImpl;
 import jogamp.common.os.WindowsDynamicLinkerImpl;
 
-import java.io.*;
-import java.lang.reflect.*;
-import java.net.URISyntaxException;
-import java.security.*;
-import java.util.*;
+import com.jogamp.common.util.IOUtil;
+import com.jogamp.common.util.cache.TempJarCache;
 
 /** Provides low-level, relatively platform-independent access to
     shared ("native") libraries. The core library routines
@@ -72,6 +76,10 @@ public final class NativeLibrary implements DynamicLookupHelper {
   private static final DynamicLinker dynLink;
   private static final String[] prefixes;
   private static final String[] suffixes;
+  /** TODO: Hide all lookup methods - Then make protected method accessible ..
+  private static final Method dynLinkLookupLocal;
+  private static final Method dynLinkLookupGlobal;
+  */
 
   static {
     // Instantiate dynamic linker implementation
@@ -106,6 +114,23 @@ public final class NativeLibrary implements DynamicLookupHelper {
         suffixes = new String[] { ".so" };
         break;
     }
+    
+    /** TODO: Hide all lookup methods - Then make protected method accessible ..
+        // public long lookupSymbol(long libraryHandle, String symbolName);
+        // public long lookupSymbolGlobal(String symbolName);    
+        final Method[] dlLookups = AccessController.doPrivileged(new PrivilegedAction<Method[]>() {
+            public Method[] run() {
+                final Method[] ms = new Method[2];
+                ms[0] = ReflectionUtil.getMethod(dynLink.getClass(), "lookupSymbol", Long.class, String.class);
+                ms[0].setAccessible(true);
+                ms[1] = ReflectionUtil.getMethod(dynLink.getClass(), "lookupSymbolGlobal", String.class);
+                ms[0].setAccessible(true);
+                return ms;
+            }
+        } );
+        dynLinkLookupLocal = dlLookups[0];
+        dynLinkLookupGlobal = dlLookups[1];
+      */
   }
 
   // Platform-specific representation for the handle to the open
@@ -184,7 +209,7 @@ public final class NativeLibrary implements DynamicLookupHelper {
     Platform.initSingleton(); // loads native gluegen-rt library
     // Iterate down these and see which one if any we can actually find.
     for (Iterator<String> iter = possiblePaths.iterator(); iter.hasNext(); ) {
-        String path = iter.next();
+        final String path = iter.next();
         if (DEBUG) {
             System.err.println("NativeLibrary.open(global "+global+"): Trying to load " + path);
         }
@@ -227,16 +252,34 @@ public final class NativeLibrary implements DynamicLookupHelper {
     return null;
   }
 
-  /** Looks up the given function name in this native library. */
+  @Override
   public final long dynamicLookupFunction(String funcName) {
-    if (libraryHandle == 0)
+    if ( 0 == libraryHandle ) {
       throw new RuntimeException("Library is not open");
+    }
     return dynLink.lookupSymbol(libraryHandle, funcName);
+    // TODO: return ( (Long) ReflectionUtil.callMethod(dynLink, dynLinkLookupLocal, Long.valueOf(libraryHandle), funcName) ).longValue();
+  }
+
+  @Override
+  public final boolean isFunctionAvailable(String funcName) {
+    if ( 0 == libraryHandle ) {
+      throw new RuntimeException("Library is not open");
+    }
+    return 0 != dynLink.lookupSymbol(libraryHandle, funcName);
+    // TODO return 0 != ( (Long) ReflectionUtil.callMethod(dynLink, dynLinkLookupLocal, Long.valueOf(libraryHandle), funcName) ).longValue();
   }
 
   /** Looks up the given function name in all loaded libraries. */
   public static final long dynamicLookupFunctionGlobal(String funcName) {
     return dynLink.lookupSymbolGlobal(funcName);
+    // TODO return ( (Long) ReflectionUtil.callMethod(dynLink, dynLinkLookupGlobal, funcName) ).longValue();
+  }
+
+  /** Looks up the given function name in all loaded libraries. */
+  public static final boolean isFunctionAvailableGlobal(String funcName) {
+    return 0 != dynLink.lookupSymbolGlobal(funcName);
+    // TODO return 0 != ( (Long) ReflectionUtil.callMethod(dynLink, dynLinkLookupGlobal, funcName) ).longValue();
   }
 
   /** Retrieves the low-level library handle from this NativeLibrary
-- 
cgit v1.2.3