All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.wala.cast.js.vis.JsPaPanel Maven / Gradle / Ivy

There is a newer version: 1.6.7
Show newest version
/*
 * Copyright (c) 2013 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 */
package com.ibm.wala.cast.js.vis;

import com.ibm.wala.analysis.pointers.HeapGraph;
import com.ibm.wala.cast.ipa.callgraph.AstGlobalPointerKey;
import com.ibm.wala.cast.ipa.callgraph.ObjectPropertyCatalogKey;
import com.ibm.wala.core.viz.viewer.PaPanel;
import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
import com.ibm.wala.ipa.callgraph.propagation.PointerAnalysis;
import com.ibm.wala.ipa.callgraph.propagation.PointerKey;
import com.ibm.wala.util.intset.MutableMapping;
import com.ibm.wala.util.intset.OrdinalSetMapping;
import java.util.ArrayList;
import java.util.List;

/**
 * Augments the PaPanel with: 1) global pointer keys at the root level. 2) property catalog key for
 * instance keys.
 *
 * @author yinnonh
 */
public class JsPaPanel extends PaPanel {

  private static final long serialVersionUID = 1L;

  private final MutableMapping>
      instanceKeyIdToObjectPropertyCatalogKey =
          MutableMapping.>make();
  private final List globalsPointerKeys = new ArrayList<>();

  public JsPaPanel(CallGraph cg, PointerAnalysis pa) {
    super(cg, pa);
    initDataStructures(pa);
  }

  private void initDataStructures(PointerAnalysis pa) {
    HeapGraph heapGraph = pa.getHeapGraph();
    OrdinalSetMapping instanceKeyMapping = pa.getInstanceKeyMapping();
    for (Object n : heapGraph) {
      if (heapGraph.getPredNodeCount(n) == 0) {
        if (n instanceof PointerKey) {
          if (n instanceof ObjectPropertyCatalogKey) {
            ObjectPropertyCatalogKey opck = (ObjectPropertyCatalogKey) n;
            InstanceKey instanceKey = opck.getObject();
            int instanceKeyId = instanceKeyMapping.getMappedIndex(instanceKey);
            mapUsingMutableMapping(instanceKeyIdToObjectPropertyCatalogKey, instanceKeyId, opck);
          } else if (n instanceof AstGlobalPointerKey) {
            globalsPointerKeys.add((AstGlobalPointerKey) n);
          }
        } else {
          System.err.println("Non Pointer key root: " + n);
        }
      }
    }
  }

  @Override
  protected List getPointerKeysUnderInstanceKey(InstanceKey ik) {
    List ret = new ArrayList<>(super.getPointerKeysUnderInstanceKey(ik));
    int ikIndex = pa.getInstanceKeyMapping().getMappedIndex(ik);
    ret.addAll(nonNullList(instanceKeyIdToObjectPropertyCatalogKey.getMappedObject(ikIndex)));
    return ret;
  }

  private final String cgNodesRoot = "CGNodes";
  private final String globalsRoot = "Globals";

  @Override
  protected List getRootNodes() {
    List ret = new ArrayList<>(2);
    ret.add(cgNodesRoot);
    ret.add(globalsRoot);
    return ret;
  }

  @Override
  protected List getChildrenFor(Object node) {
    List ret = new ArrayList<>();
    if (node == cgNodesRoot) {
      for (int nodeId = 0; nodeId < cg.getNumberOfNodes(); nodeId++) {
        CGNode cgNode = cg.getNode(nodeId);
        ret.add(cgNode);
      }
    } else if (node == globalsRoot) {
      ret.addAll(globalsPointerKeys);
    } else {
      ret.addAll(super.getChildrenFor(node));
    }
    return ret;
  }
}