/**
 * Simulates the native method side effects in class java.security.AccessController
 *
 * @author Feng Qian
 * @author <XXX>
 */

package soot.jimple.toolkits.pointer.nativemethods;

import soot.*;
import soot.jimple.toolkits.pointer.representations.*;
import soot.jimple.toolkits.pointer.util.*;

public class JavaSecurityAccessControllerNative extends NativeMethodClass {
    public JavaSecurityAccessControllerNative( Singletons.Global g ) {}
    public static JavaSecurityAccessControllerNative v() { return G.v().JavaSecurityAccessControllerNative(); }

  /**
   * Implements the abstract method simulateMethod.
   * It distributes the request to the corresponding methods 
   * by signatures.
   */
  public void simulateMethod(SootMethod method,
			     ReferenceVariable thisVar,
			     ReferenceVariable returnVar,
			     ReferenceVariable params[]){

    String subSignature = method.getSubSignature();

    if (subSignature.equals("java.lang.Object doPrivileged(java.security.PrivilegedAction)")){
      java_security_AccessController_doPrivileged(method, thisVar, 
						  returnVar, params);
      return;

    } else if (subSignature.equals("java.lang.Object doPrivileged(java.security.PrivilegedExceptionAction)")){
      java_security_AccessController_doPrivileged(method, thisVar, 
						  returnVar, params);
      return;

    } else if (subSignature.equals("java.lang.Object doPrivileged(java.security.PrivilegedAction,java.security.AccessControlContext)")){
      java_security_AccessController_doPrivileged(method, thisVar, 
						  returnVar, params);
      return;

    } else if (subSignature.equals("java.lang.Object doPrivileged(java.security.PrivilegedExceptionAction,java.security.AccessControlContext)")){
      java_security_AccessController_doPrivileged(method, thisVar, 
						  returnVar, params);
      return;

    } else if (subSignature.equals("java.security.AccessControlContext getStackAccessControlContext()")){
      java_security_AccessController_getStackAccessControlContext(method,
								  thisVar,
								  returnVar,
								  params);
      return;

    } else if (subSignature.equals("java.security.AccessControlContext getInheritedAccessControlContext()")){
      java_security_AccessController_getInheritedAccessControlContext(
					        method,
						thisVar,
						returnVar,
						params);
      return;

    } else {
      defaultMethod(method, thisVar, returnVar, params);
      return;

    }
  }

  /************************ java.security.AccessController ************/
  /* The return value of doPrivileged depends on the implementation.
   * 
   * public static native 
   *   java.lang.Object doPrivileged(java.security.PrivilegedAction);
   *
   * public static native 
   *   java.lang.Object doPrivileged(java.security.PrivilegedExceptionAction) 
   *                    throws java.security.PrivilegedActionException;
   *
   * public static native 
   *   java.lang.Object doPrivileged(java.security.PrivilegedAction, 
   *                                  java.security.AccessControlContext);
   *
   * public static native 
   *   java.lang.Object doPrivileged(java.security.PrivilegedExceptionAction, 
   *                                 java.security.AccessControlContext) 
   *                    throws java.security.PrivilegedActionException;
   */
  public static
    void java_security_AccessController_doPrivileged(
				 SootMethod method,
				 ReferenceVariable thisVar,
				 ReferenceVariable returnVar,
				 ReferenceVariable params[]){
    NativeHelper.assignObjectTo(returnVar, Environment.v().getLeastObject());
  }

  /**
   * Creates an access control context object.
   *
   * private static native 
   *   java.security.AccessControlContext getStackAccessControlContext();
   */
  public static 
    void java_security_AccessController_getStackAccessControlContext(
					    SootMethod method,
					    ReferenceVariable thisVar,
					    ReferenceVariable returnVar,
					    ReferenceVariable params[]){
    NativeHelper.assignObjectTo(returnVar, Environment.v().getLeastObject());
      //    throw new NativeMethodNotSupportedException(method);
  }


  /**
   * NOTE: not documented and not called by anyone
   *
   * static native 
   *   java.security.AccessControlContext getInheritedAccessControlContext();
   */
  public static
    void java_security_AccessController_getInheritedAccessControlContext(
					   SootMethod method,
					   ReferenceVariable thisVar,
					   ReferenceVariable returnVar,
					   ReferenceVariable params[]){
    NativeHelper.assignObjectTo(returnVar, Environment.v().getLeastObject());
    //    throw new NativeMethodNotSupportedException(method);
  }
}

@@ -3518,32 +3518,121 @@
  params%5B%5D)%7B%0A    
+// No longer necessary since Spark handles it itself in a more precise%0A    // way.%0A    //
 NativeHelper.ass
@@ -3689,16 +3689,16 @@
 ect());%0A
-
   %7D%0A%0A  /
@@ -4169,73 +4169,8 @@
 ));%0A
-      //    throw new NativeMethodNotSupportedException(method);%0A
   %7D%0A
@@ -4555,32 +4555,32 @@
 able params%5B%5D)%7B%0A
+
     NativeHelper
@@ -4645,71 +4645,8 @@
 ));%0A
-    //    throw new NativeMethodNotSupportedException(method);%0A
   %7D%0A

Remove redundant native assignment of LeastObject to return node from AccessController.doPrivileged because Spark now handles it itself in a more precise way.