55import it .unipr .analysis .contract .SmartContract ;
66import it .unipr .cfg .EVMCFG ;
77import it .unipr .cfg .ProgramCounterLocation ;
8+ import it .unipr .utils .CustomPolicy ;
89import it .unipr .utils .VulnerabilitiesObject ;
910import it .unive .lisa .program .ClassUnit ;
1011import it .unive .lisa .program .cfg .CodeMemberDescriptor ;
1617import java .nio .file .Path ;
1718import java .util .*;
1819import org .apache .commons .io .FilenameUtils ;
19- import org .apache .commons .lang3 .tuple .Pair ;
2020import org .apache .logging .log4j .LogManager ;
2121import org .apache .logging .log4j .Logger ;
2222import org .json .JSONArray ;
@@ -30,7 +30,7 @@ public class Bridge implements Iterable<SmartContract> {
3030
3131 private EVMCFG xCFG ;
3232
33- private final List < Pair < String , String >> policy ;
33+ private CustomPolicy policy ;
3434
3535 /** Detected vulnerabilities in the bridge. */
3636 private VulnerabilitiesObject _vulnerabilities ;
@@ -85,7 +85,6 @@ public Bridge(Path bytecodeDirectoryPath, Path abiDirectoryPath) {
8585 public Bridge (Path bytecodeDirectoryPath , Path abiDirectoryPath , Path policyPath , String name ) {
8686 this .name = name ;
8787 this .contracts = new ArrayList <>();
88- this .policy = new ArrayList <>();
8988
9089 Map <String , Path > abiFiles = mapFilesByName (abiDirectoryPath , ".abi" );
9190 Map <String , Path > bytecodeFiles = mapFilesByName (bytecodeDirectoryPath , ".bytecode" );
@@ -100,10 +99,17 @@ public Bridge(Path bytecodeDirectoryPath, Path abiDirectoryPath, Path policyPath
10099 log .info ("Created bridge {} with {} contracts using bytecodes at {} and ABIs at {}." ,
101100 name , contracts .size (), bytecodeDirectoryPath .toString (), abiDirectoryPath .toString ());
102101
103- if (policyPath != null )
104- loadPolicy (policyPath );
105- else
102+ if (policyPath != null ) {
103+ try {
104+ this .policy = new CustomPolicy (policyPath );
105+ } catch (IOException e ) {
106+ log .warn ("Error reading policy file: {}. Creating bridge without custom policy. Using default policy" ,
107+ e .getMessage ());
108+ }
109+ } else {
110+ this .policy = new CustomPolicy ();
106111 log .info ("Created bridge without custom policy. Using default policy" );
112+ }
107113
108114 }
109115
@@ -163,36 +169,8 @@ public EVMCFG getXCFG() {
163169 *
164170 * @return a list of event-function pairs
165171 */
166- public List <Pair <String , String >> getPolicy () {
167- return policy ;
168- }
169-
170- /**
171- * Checks if an event has an associated function in the policy.
172- *
173- * @param event the name of the event to check
174- *
175- * @return true if the event has an associated function, false otherwise
176- */
177- public boolean hasEventFunctionMapping (String event ) {
178- for (Pair <String , String > pair : policy )
179- if (pair .getLeft ().equals (event ))
180- return true ;
181- return false ;
182- }
183-
184- /**
185- * Gets the function name associated with an event from the policy.
186- *
187- * @param event the name of the event
188- *
189- * @return the associated function name, or null if not found
190- */
191- public String getFunctionForEvent (String event ) {
192- for (Pair <String , String > pair : policy )
193- if (pair .getLeft ().equals (event ))
194- return pair .getRight ();
195- return null ;
172+ public CustomPolicy getPolicy () {
173+ return policy != null ? policy : new CustomPolicy ();
196174 }
197175
198176 /**
@@ -266,43 +244,6 @@ public EVMCFG buildPartialXCFG() {
266244 return xCFG ;
267245 }
268246
269- /**
270- * Loads the cross-chain policy from a JSON file and populates the policy
271- * list. The JSON file should contain an array named "policy" with
272- * event-function pairs.
273- *
274- * @param policyPath the path to the policy JSON file
275- */
276- private void loadPolicy (Path policyPath ) {
277- try {
278- log .info ("Loading policy from: {}" , policyPath );
279- String content = Files .readString (policyPath );
280- JSONObject policyJson = new JSONObject (content );
281-
282- if (policyJson .has ("policy" )) {
283- JSONArray eventFunctionPairs = policyJson .getJSONArray ("policy" );
284-
285- for (int i = 0 ; i < eventFunctionPairs .length (); i ++) {
286- JSONObject pair = eventFunctionPairs .getJSONObject (i );
287- String event = pair .getString ("event" );
288- String function = pair .getString ("function" );
289- policy .add (Pair .of (event , function ));
290- }
291-
292- log .info ("Loaded {} event-function pairs from policy." , policy .size ());
293- log .debug ("Policy: {}" , policy );
294- } else {
295- log .warn ("Policy file does not contain 'policy' array." );
296- }
297- } catch (IOException e ) {
298- log .warn ("Error reading policy file: {}. Creating bridge without custom policy. Using default policy" ,
299- e .getMessage ());
300- } catch (Exception e ) {
301- log .warn ("Error parsing policy JSON: {}. Creating bridge without custom policy. Using default policy" ,
302- e .getMessage ());
303- }
304- }
305-
306247 /**
307248 * Scans a directory for files with a given extension and maps them by their
308249 * base name. The base name is derived from the file name without its
@@ -342,14 +283,9 @@ public JSONObject toJson() {
342283 contractsArray .put (contract .toJson ());
343284 }
344285
345- JSONObject policyJson = new JSONObject ();
346- for (Pair <String , String > pair : policy ) {
347- policyJson .put (pair .getLeft (), pair .getRight ());
348- }
349-
350286 json .put ("name" , name );
351287 json .put ("smart_contracts" , contractsArray );
352- json .put ("policy" , policyJson );
288+ json .put ("policy" , policy != null ? policy . toJson () : new JSONArray () );
353289 json .put ("bridge_vulnerabilities" , _vulnerabilities != null ? _vulnerabilities .toJson () : new JSONArray ());
354290 return json ;
355291 }
0 commit comments