From 61b5eee4efa7e8f8ee33b65ffcb5a5a0581b4e82 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Wed, 22 Feb 2017 10:35:57 -0800 Subject: [PATCH] Adding object ID tracking for simultaneous generation of stubs/skeletons (e.g. for one benchmark); Fixing a few small bugs in compiler --- .../CameraCallback_Skeleton.java | 13 ++-- .../CameraSmart_Stub.java | 2 +- .../LightBulbSmart_Stub.java | 2 +- .../SmartLightsController/RoomSmart_Stub.java | 4 +- .../CameraSmartCallback_Stub.java | 2 +- .../Java/AmcrestCamera/Camera_Skeleton.java | 13 ++-- .../drivers/Java/LabRoom/Room_Skeleton.java | 13 ++-- .../LifxLightBulb/LightBulb_Skeleton.java | 11 ++- iotjava/Makefile | 6 ++ iotjava/iotpolicy/IoTCompiler.java | 72 +++---------------- 10 files changed, 44 insertions(+), 94 deletions(-) diff --git a/benchmarks/Java/SmartLightsController/CameraCallback_Skeleton.java b/benchmarks/Java/SmartLightsController/CameraCallback_Skeleton.java index 7410368..0ee1c4f 100644 --- a/benchmarks/Java/SmartLightsController/CameraCallback_Skeleton.java +++ b/benchmarks/Java/SmartLightsController/CameraCallback_Skeleton.java @@ -17,22 +17,21 @@ import iotcode.interfaces.*; public class CameraCallback_Skeleton implements CameraCallback { private CameraCallback mainObj; - private int objectId = 0; + private int objectId = 3; // Communications and synchronizations private IoTRMIComm rmiComm; private AtomicBoolean didAlreadyInitWaitInvoke; private AtomicBoolean methodReceived; private byte[] methodBytes = null; // Permissions - private final static int object0Id = 0; //CameraSmartCallback - private static Integer[] object0Permission = { 0 }; - private static List set0Allowed; + private static Integer[] object3Permission = { 0 }; + private static List set3Allowed; public CameraCallback_Skeleton(CameraCallback _mainObj, int _portSend, int _portRecv) throws Exception { mainObj = _mainObj; rmiComm = new IoTRMICommServer(_portSend, _portRecv); - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set3Allowed = new ArrayList(Arrays.asList(object3Permission)); IoTRMIUtil.mapSkel.put(_mainObj, this); IoTRMIUtil.mapSkelId.put(_mainObj, objectId); didAlreadyInitWaitInvoke = new AtomicBoolean(false); @@ -56,7 +55,7 @@ public class CameraCallback_Skeleton implements CameraCallback { mainObj = _mainObj; rmiComm = _rmiComm; objectId = _objectId; - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set3Allowed = new ArrayList(Arrays.asList(object3Permission)); didAlreadyInitWaitInvoke = new AtomicBoolean(false); methodReceived = new AtomicBoolean(false); rmiComm.registerSkeleton(objectId, methodReceived); @@ -88,7 +87,7 @@ public class CameraCallback_Skeleton implements CameraCallback { int _objectId = IoTRMIComm.getObjectId(methodBytes); int methodId = IoTRMIComm.getMethodId(methodBytes); if (_objectId == objectId) { - if (!set0Allowed.contains(methodId)) { + if (!set3Allowed.contains(methodId)) { throw new Error("Object with object Id: " + _objectId + " is not allowed to access method: " + methodId); } } diff --git a/benchmarks/Java/SmartLightsController/CameraSmart_Stub.java b/benchmarks/Java/SmartLightsController/CameraSmart_Stub.java index 48fe2f9..788b4e1 100644 --- a/benchmarks/Java/SmartLightsController/CameraSmart_Stub.java +++ b/benchmarks/Java/SmartLightsController/CameraSmart_Stub.java @@ -16,7 +16,7 @@ import iotcode.interfaces.*; public class CameraSmart_Stub implements CameraSmart { - private int objectId = 0; + private int objectId = 2; private IoTRMIComm rmiComm; // Synchronization variables private AtomicBoolean retValueReceived8 = new AtomicBoolean(false); diff --git a/benchmarks/Java/SmartLightsController/LightBulbSmart_Stub.java b/benchmarks/Java/SmartLightsController/LightBulbSmart_Stub.java index ef0c4bc..978710f 100644 --- a/benchmarks/Java/SmartLightsController/LightBulbSmart_Stub.java +++ b/benchmarks/Java/SmartLightsController/LightBulbSmart_Stub.java @@ -12,7 +12,7 @@ import iotrmi.Java.IoTRMICommClient; import iotrmi.Java.IoTRMICommServer; import iotrmi.Java.IoTRMIUtil; -import iotcode.interfaces.LightBulbSmart; +import iotcode.interfaces.*; public class LightBulbSmart_Stub implements LightBulbSmart { diff --git a/benchmarks/Java/SmartLightsController/RoomSmart_Stub.java b/benchmarks/Java/SmartLightsController/RoomSmart_Stub.java index b1e7b41..4a6f829 100644 --- a/benchmarks/Java/SmartLightsController/RoomSmart_Stub.java +++ b/benchmarks/Java/SmartLightsController/RoomSmart_Stub.java @@ -12,11 +12,11 @@ import iotrmi.Java.IoTRMICommClient; import iotrmi.Java.IoTRMICommServer; import iotrmi.Java.IoTRMIUtil; -import iotcode.interfaces.RoomSmart; +import iotcode.interfaces.*; public class RoomSmart_Stub implements RoomSmart { - private int objectId = 2; + private int objectId = 4; private IoTRMIComm rmiComm; // Synchronization variables private AtomicBoolean retValueReceived0 = new AtomicBoolean(false); diff --git a/benchmarks/drivers/Java/AmcrestCamera/CameraSmartCallback_Stub.java b/benchmarks/drivers/Java/AmcrestCamera/CameraSmartCallback_Stub.java index 0610f84..923737c 100644 --- a/benchmarks/drivers/Java/AmcrestCamera/CameraSmartCallback_Stub.java +++ b/benchmarks/drivers/Java/AmcrestCamera/CameraSmartCallback_Stub.java @@ -16,7 +16,7 @@ import iotcode.interfaces.*; public class CameraSmartCallback_Stub implements CameraSmartCallback { - private int objectId = 0; + private int objectId = 3; private IoTRMIComm rmiComm; // Synchronization variables diff --git a/benchmarks/drivers/Java/AmcrestCamera/Camera_Skeleton.java b/benchmarks/drivers/Java/AmcrestCamera/Camera_Skeleton.java index cbb98bf..ccaf7a3 100644 --- a/benchmarks/drivers/Java/AmcrestCamera/Camera_Skeleton.java +++ b/benchmarks/drivers/Java/AmcrestCamera/Camera_Skeleton.java @@ -17,22 +17,21 @@ import iotcode.interfaces.*; public class Camera_Skeleton implements Camera { private Camera mainObj; - private int objectId = 0; + private int objectId = 2; // Communications and synchronizations private IoTRMIComm rmiComm; private AtomicBoolean didAlreadyInitWaitInvoke; private AtomicBoolean methodReceived; private byte[] methodBytes = null; // Permissions - private final static int object0Id = 0; //CameraSmart - private static Integer[] object0Permission = { 8, 7, 9, 6, 2, 1, 4, 3, 0, 10, 5 }; - private static List set0Allowed; + private static Integer[] object2Permission = { 8, 7, 9, 6, 2, 1, 4, 3, 0, 10, 5 }; + private static List set2Allowed; public Camera_Skeleton(Camera _mainObj, int _portSend, int _portRecv) throws Exception { mainObj = _mainObj; rmiComm = new IoTRMICommServer(_portSend, _portRecv); - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set2Allowed = new ArrayList(Arrays.asList(object2Permission)); IoTRMIUtil.mapSkel.put(_mainObj, this); IoTRMIUtil.mapSkelId.put(_mainObj, objectId); didAlreadyInitWaitInvoke = new AtomicBoolean(false); @@ -56,7 +55,7 @@ public class Camera_Skeleton implements Camera { mainObj = _mainObj; rmiComm = _rmiComm; objectId = _objectId; - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set2Allowed = new ArrayList(Arrays.asList(object2Permission)); didAlreadyInitWaitInvoke = new AtomicBoolean(false); methodReceived = new AtomicBoolean(false); rmiComm.registerSkeleton(objectId, methodReceived); @@ -232,7 +231,7 @@ public class Camera_Skeleton implements Camera { int _objectId = IoTRMIComm.getObjectId(methodBytes); int methodId = IoTRMIComm.getMethodId(methodBytes); if (_objectId == objectId) { - if (!set0Allowed.contains(methodId)) { + if (!set2Allowed.contains(methodId)) { throw new Error("Object with object Id: " + _objectId + " is not allowed to access method: " + methodId); } } diff --git a/benchmarks/drivers/Java/LabRoom/Room_Skeleton.java b/benchmarks/drivers/Java/LabRoom/Room_Skeleton.java index 015af4a..222c0df 100644 --- a/benchmarks/drivers/Java/LabRoom/Room_Skeleton.java +++ b/benchmarks/drivers/Java/LabRoom/Room_Skeleton.java @@ -17,22 +17,21 @@ import iotcode.interfaces.Room; public class Room_Skeleton implements Room { private Room mainObj; - private int objectId = 2; + private int objectId = 4; // Communications and synchronizations private IoTRMIComm rmiComm; private AtomicBoolean didAlreadyInitWaitInvoke; private AtomicBoolean methodReceived; private byte[] methodBytes = null; // Permissions - private final static int object0Id = 0; //RoomSmart - private static Integer[] object0Permission = { 0 }; - private static List set0Allowed; + private static Integer[] object4Permission = { 0 }; + private static List set4Allowed; public Room_Skeleton(Room _mainObj, int _portSend, int _portRecv) throws Exception { mainObj = _mainObj; rmiComm = new IoTRMICommServer(_portSend, _portRecv); - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set4Allowed = new ArrayList(Arrays.asList(object4Permission)); IoTRMIUtil.mapSkel.put(_mainObj, this); IoTRMIUtil.mapSkelId.put(_mainObj, objectId); didAlreadyInitWaitInvoke = new AtomicBoolean(false); @@ -56,7 +55,7 @@ public class Room_Skeleton implements Room { mainObj = _mainObj; rmiComm = _rmiComm; objectId = _objectId; - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set4Allowed = new ArrayList(Arrays.asList(object4Permission)); didAlreadyInitWaitInvoke = new AtomicBoolean(false); methodReceived = new AtomicBoolean(false); rmiComm.registerSkeleton(objectId, methodReceived); @@ -89,7 +88,7 @@ public class Room_Skeleton implements Room { int _objectId = IoTRMIComm.getObjectId(methodBytes); int methodId = IoTRMIComm.getMethodId(methodBytes); if (_objectId == objectId) { - if (!set0Allowed.contains(methodId)) { + if (!set4Allowed.contains(methodId)) { throw new Error("Object with object Id: " + _objectId + " is not allowed to access method: " + methodId); } } diff --git a/benchmarks/drivers/Java/LifxLightBulb/LightBulb_Skeleton.java b/benchmarks/drivers/Java/LifxLightBulb/LightBulb_Skeleton.java index af9d817..3b808fa 100644 --- a/benchmarks/drivers/Java/LifxLightBulb/LightBulb_Skeleton.java +++ b/benchmarks/drivers/Java/LifxLightBulb/LightBulb_Skeleton.java @@ -24,15 +24,14 @@ public class LightBulb_Skeleton implements LightBulb { private AtomicBoolean methodReceived; private byte[] methodBytes = null; // Permissions - private final static int object0Id = 0; //LightBulbSmart - private static Integer[] object0Permission = { 2, 10, 1, 3, 11, 8, 12, 7, 13, 9, 6, 16, 17, 4, 0, 14, 15, 5 }; - private static List set0Allowed; + private static Integer[] object1Permission = { 2, 10, 1, 3, 11, 8, 12, 7, 13, 9, 6, 16, 17, 4, 0, 14, 15, 5 }; + private static List set1Allowed; public LightBulb_Skeleton(LightBulb _mainObj, int _portSend, int _portRecv) throws Exception { mainObj = _mainObj; rmiComm = new IoTRMICommServer(_portSend, _portRecv); - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set1Allowed = new ArrayList(Arrays.asList(object1Permission)); IoTRMIUtil.mapSkel.put(_mainObj, this); IoTRMIUtil.mapSkelId.put(_mainObj, objectId); didAlreadyInitWaitInvoke = new AtomicBoolean(false); @@ -56,7 +55,7 @@ public class LightBulb_Skeleton implements LightBulb { mainObj = _mainObj; rmiComm = _rmiComm; objectId = _objectId; - set0Allowed = new ArrayList(Arrays.asList(object0Permission)); + set1Allowed = new ArrayList(Arrays.asList(object1Permission)); didAlreadyInitWaitInvoke = new AtomicBoolean(false); methodReceived = new AtomicBoolean(false); rmiComm.registerSkeleton(objectId, methodReceived); @@ -288,7 +287,7 @@ public class LightBulb_Skeleton implements LightBulb { int _objectId = IoTRMIComm.getObjectId(methodBytes); int methodId = IoTRMIComm.getMethodId(methodBytes); if (_objectId == objectId) { - if (!set0Allowed.contains(methodId)) { + if (!set1Allowed.contains(methodId)) { throw new Error("Object with object Id: " + _objectId + " is not allowed to access method: " + methodId); } } diff --git a/iotjava/Makefile b/iotjava/Makefile index 40c2436..93ba52c 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -28,6 +28,12 @@ run-compiler-dev: cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy_callbacks.pol testclassrequires_callbacks.pol callbackpolicy.pol callbackrequires.pol -cplus Cplus -java Java #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy_callbacks_three.pol testclassrequires_callbacks_three.pol callbackpolicy.pol callbackrequires.pol callbackpolicy_two.pol callbackrequires_two.pol -cplus Cplus -java Java +PHONY += run-compiler-smartlight +run-compiler-smartlight: + cp ../localconfig/iotpolicy/LifxLightBulb/*.pol $(BIN_DIR)/iotpolicy/ + cp ../localconfig/iotpolicy/LifxLightBulb/*.req $(BIN_DIR)/iotpolicy/ + cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler lifxlightbulb.pol smartlightsbulb.req amcrestcamera.pol smartlightscam.req motiondetection.pol motiondetection.req room.pol roomsmart.req -java Java + PHONY += run-compiler-lbtest run-compiler-lbtest: cp ../localconfig/iotpolicy/LifxLightBulb/*.pol $(BIN_DIR)/iotpolicy/ diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 425eebc..a114491 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -61,6 +61,7 @@ public class IoTCompiler { private String subdir; private Map mapPortCount; // Counter for ports private static int portCount = 0; + private static int countObjId = 1; // Always increment object Id for a new stub/skeleton private String mainClass; @@ -143,7 +144,7 @@ public class IoTCompiler { mapIntfacePTH.put(origInt, ptHandler); mapIntDeclHand.put(origInt, decHandler); // Set object Id counter to 0 for each interface - mapIntfaceObjId.put(origInt, new Integer(0)); + mapIntfaceObjId.put(origInt, countObjId++); } @@ -363,9 +364,9 @@ public class IoTCompiler { */ private void updateIntfaceObjIdMap(String intface, String newIntface) { + // We are assuming that we only generate one stub per one skeleton at this point @Feb 2017 Integer objId = mapIntfaceObjId.get(intface); mapNewIntfaceObjId.put(newIntface, objId); - mapIntfaceObjId.put(intface, objId++); } @@ -416,8 +417,8 @@ public class IoTCompiler { for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); int newObjectId = getNewIntfaceObjectId(newIntface); - println("private final static int object" + newObjectId + "Id = " + - newObjectId + ";\t//" + newIntface); + //println("private final static int object" + newObjectId + "Id = " + + // newObjectId + ";\t//" + newIntface); Set methodIds = intMeth.getValue(); print("private static Integer[] object" + newObjectId + "Permission = { "); int i = 0; @@ -520,42 +521,6 @@ public class IoTCompiler { } - /** - * HELPER: writeJavaMethodCallbackPermission() writes permission checks in stub for callbacks - */ - private void writeJavaMethodCallbackPermission(String intface) { - - println("int methodId = IoTRMIObject.getMethodId(method);"); - // Get all the different stubs - Map> mapNewIntMethods = mapInt2NewInts.get(intface); - for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { - String newIntface = intMeth.getKey(); - int newObjectId = getNewIntfaceObjectId(newIntface); - println("if (!set" + newObjectId + "Allowed.contains(methodId)) {"); - println("throw new Error(\"Callback object for " + intface + " is not allowed to access method: \" + methodId);"); - println("}"); - } - } - - - /** - * HELPER: writeJavaInitCallbackPermission() writes the permission for callback - */ - private void writeJavaInitCallbackPermission(String intface, InterfaceDecl intDecl, boolean callbackExist) { - - if (callbackExist) { - String method = "___initCallBack()"; - int methodNumId = intDecl.getHelperMethodNumId(method); - Map> mapNewIntMethods = mapInt2NewInts.get(intface); - for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { - String newIntface = intMeth.getKey(); - int newObjectId = getNewIntfaceObjectId(newIntface); - println("set" + newObjectId + "Allowed.add(" + methodNumId + ");"); - } - } - } - - /** * HELPER: getPortCount() gets port count for different stubs and skeletons */ @@ -1191,7 +1156,9 @@ public class IoTCompiler { private void writePropertiesJavaSkeleton(String intface, InterfaceDecl intDecl) { println("private " + intface + " mainObj;"); - println("private int objectId = 0;"); + //println("private int objectId = 0;"); + Integer objId = mapIntfaceObjId.get(intface); + println("private int objectId = " + objId + ";"); println("// Communications and synchronizations"); println("private IoTRMIComm rmiComm;"); println("private AtomicBoolean didAlreadyInitWaitInvoke;"); @@ -1245,7 +1212,6 @@ public class IoTCompiler { println("rmiComm = new IoTRMICommServer(_portSend, _portRecv);"); // Generate permission control initialization writeConstructorJavaPermission(intface); - //writeJavaInitCallbackPermission(intface, intDecl, callbackExist); writeStructPermissionJavaSkeleton(methods, intDecl, intface); println("IoTRMIUtil.mapSkel.put(_mainObj, this);"); println("IoTRMIUtil.mapSkelId.put(_mainObj, objectId);"); @@ -3065,7 +3031,6 @@ public class IoTCompiler { for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { String newIntface = intMeth.getKey(); int newObjectId = getNewIntfaceObjectId(newIntface); - println("int objectId = " + newObjectId + ";"); println("static set set" + newObjectId + "Allowed;"); } } @@ -3153,25 +3118,6 @@ public class IoTCompiler { } - /** - * HELPER: writeCplusMethodCallbackPermission() writes permission checks in stub for callbacks - */ - private void writeCplusMethodCallbackPermission(String intface) { - - println("int methodId = IoTRMIObject::getMethodId(method);"); - // Get all the different stubs - Map> mapNewIntMethods = mapInt2NewInts.get(intface); - for (Map.Entry> intMeth : mapNewIntMethods.entrySet()) { - String newIntface = intMeth.getKey(); - int newObjectId = getNewIntfaceObjectId(newIntface); - println("if (set" + newObjectId + "Allowed.find(methodId) == set" + newObjectId + "Allowed.end()) {"); - println("cerr << \"Callback object for " + intface + " is not allowed to access method: \" << methodId;"); - println("return;"); - println("}"); - } - } - - /** * generateCPlusStubClassesHpp() generate stubs based on the methods list in C++ (.hpp file) */ @@ -3310,6 +3256,8 @@ public class IoTCompiler { println("IoTRMIComm *rmiComm;"); println("char* methodBytes;"); println("int methodLen;"); + Integer objId = mapIntfaceObjId.get(intface); + println("int objectId = " + objId + ";"); // Keep track of object Ids of all stubs registered to this interface writePropertiesCplusPermission(intface); println("// Synchronization variables"); -- 2.34.1