Skip to content

Commit

Permalink
Add missing emissions mostly on the primitives path
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmonettas committed Apr 1, 2024
1 parent 19e540e commit a4f0ebb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 29 deletions.
10 changes: 10 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ You can also disable/enable specific types of instrumentation by using :
* [FlowStorm debugger](http://www.flow-storm.org)
* [Clofidence](https://github.com/flow-storm/clofidence)

## Building

- Modify the version in pom.xml
- make install

## Running the tests

You can run all the tests with `mvn test`. To specifically run storm tests see the comment
on build.xml `test` target.

## Clojure

* Clojure
Expand Down
8 changes: 7 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
unless="maven.test.skip">
<java classname="clojure.main" failonerror="true" fork="true">
<sysproperty key="clojure.compiler.direct-linking" value="${directlinking}"/>

<sysproperty key="clojure.storm.instrumentEnable" value="true"/>
<sysproperty key="clojure.storm.instrumentOnlyPrefixes" value="clojure.test-clojure.storm-test-code"/>

<classpath>
<pathelement path="${maven.test.classpath}"/>
<path location="${test-classes}"/>
Expand Down Expand Up @@ -173,7 +177,9 @@

<target name="test"
description="Run all the tests"
depends="test-example,test-generative"/> <!-- test-example,test-generative,test-storm-example -->
depends="test-example,test-generative"/>
<!-- change the depends to only test-storm-example to only run those, and then mvn test -->
<!-- test-example,test-generative,test-storm-example -->

<target name="build"
description="Build Clojure (compilation only, no tests)."
Expand Down
56 changes: 33 additions & 23 deletions src/jvm/clojure/lang/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public enum C{
private class Recur {};
static final public Class RECUR_CLASS = Recur.class;

interface Expr{
public interface Expr{
Object eval() ;

void emit(C context, ObjExpr objx, GeneratorAdapter gen);
Expand Down Expand Up @@ -1848,6 +1848,7 @@ public void emitUnboxed(C context, ObjExpr objx, GeneratorAdapter gen){
method.emitClearLocals(gen);
}
Object ops = RT.get(Intrinsics.ops, method.toString());
Type retType = Type.getReturnType(method);
if(ops != null)
{
if(ops instanceof Object[])
Expand All @@ -1861,11 +1862,10 @@ public void emitUnboxed(C context, ObjExpr objx, GeneratorAdapter gen){
else
{
Type type = Type.getType(c);
Type retType = Type.getReturnType(method);
Method m = new Method(methodName, retType, Type.getArgumentTypes(method));
gen.visitMethodInsn(INVOKESTATIC, type.getInternalName(), methodName, m.getDescriptor(), c.isInterface());
Emitter.emitExprTrace(gen, objx, coord, retType);
}
Emitter.emitExprTrace(gen, objx, coord, retType);
}
else
throw new UnsupportedOperationException("Unboxed emit of unknown member");
Expand Down Expand Up @@ -2840,26 +2840,28 @@ else if(maybePrimitiveType(testExpr) == boolean.class)
gen.getStatic(BOOLEAN_OBJECT_TYPE, "FALSE", BOOLEAN_OBJECT_TYPE);
gen.visitJumpInsn(IF_ACMPEQ, falseLabel);
}
if(emitUnboxed)
((MaybePrimitiveExpr)thenExpr).emitUnboxed(context, objx, gen);
else {
thenExpr.emit(context, objx, gen);
if(context != C.STATEMENT)
Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
if(emitUnboxed) {
((MaybePrimitiveExpr) thenExpr).emitUnboxed(context, objx, gen);
if (context != C.STATEMENT) Emitter.emitExprTrace(gen, objx, coord, thenExpr);
} else {
thenExpr.emit(context, objx, gen);
if(context != C.STATEMENT) Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
}


gen.goTo(endLabel);
gen.mark(nullLabel);
gen.pop();

gen.mark(falseLabel);
if(emitUnboxed)
((MaybePrimitiveExpr)elseExpr).emitUnboxed(context, objx, gen);
else {
if(emitUnboxed) {
((MaybePrimitiveExpr) elseExpr).emitUnboxed(context, objx, gen);
if(context != C.STATEMENT) Emitter.emitExprTrace(gen, objx, coord, elseExpr);
} else {
elseExpr.emit(context, objx, gen);
if(context != C.STATEMENT)
Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
if(context != C.STATEMENT) Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
}


gen.mark(endLabel);

Expand Down Expand Up @@ -3515,10 +3517,12 @@ public Class getJavaClass() {
public static class InstanceOfExpr implements Expr, MaybePrimitiveExpr{
Expr expr;
Class c;
IPersistentVector coord;

public InstanceOfExpr(Class c, Expr expr){
public InstanceOfExpr(Class c, Expr expr, IPersistentVector coord){
this.expr = expr;
this.c = c;
this.coord = coord;
}

public Object eval() {
Expand All @@ -3533,12 +3537,13 @@ public boolean canEmitPrimitive(){

public void emitUnboxed(C context, ObjExpr objx, GeneratorAdapter gen){
expr.emit(C.EXPRESSION, objx, gen);
gen.instanceOf(getType(c));
gen.instanceOf(getType(c));
}

public void emit(C context, ObjExpr objx, GeneratorAdapter gen){
emitUnboxed(context,objx,gen);
HostExpr.emitBoxReturn(objx,gen,Boolean.TYPE);
HostExpr.emitBoxReturn(objx,gen,Boolean.TYPE);
Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
if(context == C.STATEMENT)
gen.pop();
}
Expand Down Expand Up @@ -3940,7 +3945,7 @@ static public Expr parse(C context, ISeq form) {
Object val = ((ConstantExpr) sexpr).val();
if(val instanceof Class)
{
return new InstanceOfExpr((Class) val, analyze(context, RT.third(form)));
return new InstanceOfExpr((Class) val, analyze(context, RT.third(form)), coord);
}
}
}
Expand Down Expand Up @@ -6722,12 +6727,14 @@ public void doEmit(C context, ObjExpr objx, GeneratorAdapter gen, boolean emitUn
{
Var.pushThreadBindings(RT.map(LOOP_LABEL, loopLabel));
if(emitUnboxed)
((MaybePrimitiveExpr)body).emitUnboxed(context, objx, gen);
{
((MaybePrimitiveExpr)body).emitUnboxed(context, objx, gen);
if (context == C.EXPRESSION || context == C.RETURN) Emitter.emitExprTrace(gen, objx, coord, body);
}
else
{
body.emit(context, objx, gen);
if (context == C.EXPRESSION || context == C.RETURN)
Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
if (context == C.EXPRESSION || context == C.RETURN) Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
}
}
finally
Expand All @@ -6738,12 +6745,15 @@ public void doEmit(C context, ObjExpr objx, GeneratorAdapter gen, boolean emitUn
else
{
if(emitUnboxed)
{
((MaybePrimitiveExpr)body).emitUnboxed(context, objx, gen);
if (context == C.EXPRESSION || context == C.RETURN) Emitter.emitExprTrace(gen, objx, coord, body);
}

else
{
body.emit(context, objx, gen);
if (context == C.EXPRESSION || context == C.RETURN)
Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
if (context == C.EXPRESSION || context == C.RETURN) Emitter.emitExprTrace(gen, objx, coord, OBJECT_TYPE);
}

}
Expand Down
13 changes: 10 additions & 3 deletions src/jvm/clojure/storm/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,24 @@ private static void emitCoord(GeneratorAdapter gen, IPersistentVector coord) {
emittedCoords.add(strCoord);
}

public static void emitExprTrace(GeneratorAdapter gen, ObjExpr objx, IPersistentVector coord, Type retType) {
public static void emitExprTrace(GeneratorAdapter gen, ObjExpr objx, IPersistentVector coord, Compiler.Expr expr) {
Type exprType = Type.getType(Object.class);
if (expr.hasJavaClass() && expr.getJavaClass() != null)
exprType = Type.getType(expr.getJavaClass());
emitExprTrace(gen, objx, coord, exprType);
}

public static void emitExprTrace(GeneratorAdapter gen, ObjExpr objx, IPersistentVector coord, Type exprType) {
if (exprInstrumentationEnable && coord != null) {

Integer formId = (Integer) Compiler.FORM_ID.deref();
if (formId == null) formId = 0;

if ((objx instanceof FnExpr || objx instanceof NewInstanceExpr) && !skipInstrumentation(objx.name())) {

// assumes the stack contains the value to be traced
// duplicate the value for tracing, so we don't consume it
dupAndBox(gen, retType);
dupAndBox(gen, exprType);

emitCoord(gen, coord);

Expand Down
29 changes: 27 additions & 2 deletions test/clojure/test_clojure/storm_bodies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
[:expr-exec 3 "3"]
[:fn-return 3 ""]]
(u/capture)) "captured traces should match.")))

(deftest let-test
(let [r (b/letter)]
(is (= 15 r) "function return should be right.")
Expand All @@ -97,7 +97,8 @@
[:expr-exec 10 "3,1,5,1,1,2"]
[:expr-exec 15 "3,1,5,1,1"]
[:bind "z" 15 "3,1,5"]
[:expr-exec 15 "3,1,5,2"]
[:expr-exec 15 "3,1,5,2"]
[:expr-exec 15 "3,1,5"]
[:bind "c" 15 "3"]
[:expr-exec 15 "3,2"]
[:expr-exec 15 "3"]
Expand Down Expand Up @@ -132,6 +133,30 @@
[:fn-return 4 ""]]
(u/capture)) "captured traces should match.")))

(deftest hinted-and-static-test
(let [r (b/hinted-and-static 42)]
(is (= r 174) "function return should be right.")
(is (= [[:fn-call "clojure.test-clojure.storm-test-code.bodies" "hinted-and-static" [42] 1633944069]
[:bind "n" 42 ""]
[:expr-exec "#object[...]" "3,1,1"]
[:bind "arr" "#object[...]" "3"]
[:expr-exec "#object[...]" "3,1,3,1"]
[:expr-exec 3 "3,1,3"]
[:bind "e" 3 "3"]
[:expr-exec 3 "3,1,5,1"]
[:expr-exec 3 "3,1,5"]
[:bind "l" 3 "3"]
[:expr-exec 42 "3,1,7,1"]
[:expr-exec 168 "3,1,7"]
[:bind "b" 168 "3"]
[:expr-exec 3 "3,2,1"]
[:expr-exec 3 "3,2,2"]
[:expr-exec 168 "3,2,3"]
[:expr-exec 174 "3,2"]
[:expr-exec 174 "3"]
[:fn-return 174 ""]]
(u/capture)) "captured traces should match.")))

;; (deftest interop-test
;; (let [r (b/interopter #js {:num 2 :f (fn f [x] x)})]
;; (is (= 42 r) "function return should be right.")
Expand Down
7 changes: 7 additions & 0 deletions test/clojure/test_clojure/storm_test_code/bodies.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
(defn constructor []
(count (String. "ctor")))

(defn hinted-and-static ^long [^long n]
(let [arr (byte-array [1 2 3 4])
e (aget arr 2)
l (long e)
b (bit-shift-left n 2)]
(+ e l b)))

(defn interopter [o]
;; TODO
)

0 comments on commit a4f0ebb

Please sign in to comment.