summaryrefslogtreecommitdiff
path: root/chapter33/src
diff options
context:
space:
mode:
Diffstat (limited to 'chapter33/src')
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java17
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java3
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/node/CLInitNode.java4
-rw-r--r--chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java13
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java16
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java16
6 files changed, 57 insertions, 12 deletions
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java
index 51a6a197..4b0e7de4 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/CodeGen.java
@@ -357,11 +357,26 @@ public class CodeGen {
// Return the <clinit> for a CompUnit
public FunNode link( CompUnit cu ) {
for( FunNode fun : _linker )
- if( fun!=null && fun._compunit == cu && fun.isClz() )
+ if( fun!=null && !fun.isDead() && fun._compunit == cu && fun.isClz() )
return fun;
return null;
}
+ // An inline import can reveal a call to a private initializer whose
+ // unused summary was deleted after SCCP. Recreate the wrapper from its
+ // saved metadata; this is a direct call, not a function-pointer escape.
+ public FunNode restoreClinit(CompUnit cu) {
+ if( cu==null || cu._obj==null ) return null;
+ ElfReader elf = getElf(cu._obj);
+ if( elf._nodes!=null ) return null;
+ for( FunctionSummary summary : elf._functions )
+ if( summary.name().equals(TypeStruct.addClzPrefix(cu._cname)+".<clinit>") ) {
+ if( _stop._inputs.find(cu._stop)==-1 ) _stop.addDef(cu._stop);
+ return summary.install(this,cu);
+ }
+ return null;
+ }
+
// Insert linker mapping from constant function signature to the function
// being called.
public void link(FunNode fun) {
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java b/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java
index 6cd4035d..18bd98e3 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/codegen/FunctionSummary.java
@@ -5,7 +5,7 @@ import com.seaofnodes.simple.type.*;
// The small, eagerly readable part of a function's serialized implementation.
record FunctionSummary(TypeFunPtr sig, TypeTuple state, String name, int size, int[] classDependencyFIDXs) {
- void install(CodeGen code, CompUnit cu) {
+ FunNode install(CodeGen code, CompUnit cu) {
FunNode fun = new FunNode(null,sig,name,cu,null,cu._start);
fun._approxUIDs=size;
fun.init();
@@ -24,5 +24,6 @@ record FunctionSummary(TypeFunPtr sig, TypeTuple state, String name, int size, i
fun.setRet(ret);
cu._stop.addDef(ret);
code.link(fun);
+ return fun;
}
}
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/CLInitNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/CLInitNode.java
index fb762ff3..4a122090 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/node/CLInitNode.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/node/CLInitNode.java
@@ -129,6 +129,8 @@ public class CLInitNode extends CFGNode implements MultiNode {
public boolean link() {
if( in(0)._type.isHigh() || ptr()._type.isHigh() || ready()._type.isHigh() || bypass() ) return false;
FunNode target = CodeGen.CODE.link(cu());
+ boolean restored = target==null;
+ if( target==null && !CodeGen.CODE._midAssert ) target = CodeGen.CODE.restoreClinit(cu());
if( target==null ) return false; // Forward class reference during parsing.
assert !target.isDead() : "dead initializer "+target._name;
CProjNode call = cproj(3);
@@ -149,7 +151,7 @@ public class CLInitNode extends CFGNode implements MultiNode {
CodeGen.CODE.add(parm).addDef(arg);
}
if( CodeGen.CODE._phase.ordinal()>CodeGen.Phase.Opto.ordinal() ) {
- boolean repair = !target.compute().isa(target._type);
+ boolean repair = restored || !target.compute().isa(target._type);
for( Node use : target._outputs )
if( use instanceof ParmNode && !use.compute().isa(use._type) ) repair = true;
if( repair ) Iter.repair(CodeGen.CODE);
diff --git a/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java b/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java
index 4236cb6f..0f0e013c 100644
--- a/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java
+++ b/chapter33/src/main/java/com/seaofnodes/simple/node/GlobalEscapeNode.java
@@ -18,18 +18,13 @@ public class GlobalEscapeNode extends Node {
@Override public TypeMem compute() {
TypeMem xmem = TypeMem.START;
for( CallNode call : CodeGen.CODE._calls )
- if( !call.isDead() ) {
+ if( !call.isDead() )
xmem = xmem.escapes(call.exposed(this));
- }
for( FunNode fun : CodeGen.CODE._linker )
- if( fun!=null && fun.summary()!=null && !fun.summary().isDead() ) {
+ if( fun!=null && fun.summary()!=null && !fun.summary().isDead() )
xmem = xmem.escapes(fun.summary().exposed(this));
- // A reachable unloaded body can call these targets, including
- // private initializers. Its arguments are unavailable here.
- if( fun._type==Type.CONTROL )
- for( int fidx : fun.summary()._classDependencyFIDXs )
- if( fidx!=0 ) xmem = xmem.makeFrom(xmem._escFs.meet(TypeSet.of(fidx)),xmem._escAs);
- }
+ // Class-init dependencies describe internal calls, not publication of
+ // function pointers. They remain available for class-init cycle checks.
return xmem;
}
}
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java
index 8f1d1ee8..4ae7f771 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/ClassInitDependenciesTest.java
@@ -549,6 +549,22 @@ public class ClassInitDependenciesTest {
assertNull(Eval2.eval(code,0,10));
}
+ @Test public void testImportedPrivateInitializerCycle() throws Exception {
+ Path dir=tempDir("private-cycle-"), stubs=tempDir("private-stub-");
+ object(stubs,stubs,"Main","int value=arg;");
+ module(dir,stubs,"Library",Map.of("Library","val read_noInline={ -> _Hidden.value; };",
+ "Library/_Hidden","int value=Main.value;"));
+ CodeGen code=new CodeGen(null,dir.toString(),new Ary<>(new String[]{dir.toString()}),
+ "Main","int value=Library.read_noInline();",123,true,TypeInteger.BOT);
+ try {
+ code.driver(CodeGen.Phase.TypeCheck);
+ fail("Expected a cycle through the unloaded private initializer");
+ } catch( Parser.ParseException ex ) {
+ assertCycle(ex.getMessage(),"Main -> Library._Hidden -> Main");
+ assertNull(code.getElf(code._compunits.get("Library/_Hidden")._obj)._nodes);
+ }
+ }
+
@Test public void testImportedPrivateInitializer() throws Exception {
Path dir=tempDir("private-");
module(dir,dir,"Library",Map.of("Library","val read={ -> _Hidden.value; };",
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java b/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
index 84aa8a3f..15495696 100644
--- a/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
+++ b/chapter33/src/test/java/com/seaofnodes/simple/EscapeFlowTest.java
@@ -7,6 +7,22 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class EscapeFlowTest {
+ @Test public void testSummaryCallDependencyDoesNotEscape() {
+ CodeGen code = new CodeGen("return 0;").parse();
+ code._phase = CodeGen.Phase.Opto;
+ TypeFunPtr sig = TypeFunPtr.make1((byte)2,true,TypeFunPtr.TEMPTY,TypeInteger.BOT,100);
+ FunNode fun = new FunNode(null,sig,"summaryOwner",code.compunit(),code._start);
+ fun._type = Type.CONTROL;
+ TypeTuple state = TypeTuple.make(Type.CONTROL,
+ TypeMem.START.makeFrom(TypeSet.of(9),TypeSet.EMPTY),TypeInteger.BOT);
+ SummaryNode summary = new SummaryNode(state,new int[]{7},fun,code.con(TypeMem.START)).init();
+ code.link(fun);
+ assertSame(summary,fun.summary());
+ TypeMem exposed = new GlobalEscapeNode().compute();
+ assertTrue("Published function pointers escape",exposed._escFs.contains(9));
+ assertFalse("Internal call dependencies do not escape",exposed._escFs.contains(7));
+ }
+
@Test public void testArrayGeneratorDoesNotPublishUnrelatedFunctions() {
for( long seed : new long[]{1,123,987654321} )
for( String access : new String[]{"","~"} ) {