Skip to content

Commit

Permalink
Add a flag for verifying generated bytecode
Browse files Browse the repository at this point in the history
This flag is enabled by default in tests through inspecting the
assertion status on the GeneratedQueryStructure class. If assertions are
enabled, the byte code verification will be enabled, otherwise it is not
enabled. When running tests, assertions are enabled.
  • Loading branch information
thobe committed Mar 15, 2017
1 parent 1473fd6 commit 078c793
Show file tree
Hide file tree
Showing 14 changed files with 777 additions and 34 deletions.
17 changes: 16 additions & 1 deletion community/codegen/pom.xml
Expand Up @@ -12,6 +12,7 @@
<bundle.namespace>org.neo4j.codegen</bundle.namespace> <bundle.namespace>org.neo4j.codegen</bundle.namespace>
<license-text.header>GPL-3-header.txt</license-text.header> <license-text.header>GPL-3-header.txt</license-text.header>
<licensing.prepend.text>notice-gpl-prefix.txt</licensing.prepend.text> <licensing.prepend.text>notice-gpl-prefix.txt</licensing.prepend.text>
<asm.version>5.2</asm.version>
</properties> </properties>


<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -53,7 +54,21 @@ the relevant Commercial Agreement.
<dependency> <dependency>
<groupId>org.ow2.asm</groupId> <groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId> <artifactId>asm</artifactId>
<version>5.2</version> <version>${asm.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-util</artifactId>
<version>${asm.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-analysis</artifactId>
<version>${asm.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
Expand Down
Expand Up @@ -46,6 +46,7 @@ protected String name()
return "BYTECODE"; return "BYTECODE";
} }
}; };
public static final CodeGeneratorOption VERIFY_GENERATED_BYTECODE = load( "Verifier" );


@Override @Override
public void applyTo( Object target ) public void applyTo( Object target )
Expand All @@ -55,4 +56,17 @@ public void applyTo( Object target )
((Configuration) target).withFlag( this ); ((Configuration) target).withFlag( this );
} }
} }

private static CodeGeneratorOption load( String option )
{
try
{
return (CodeGeneratorOption) Class.forName( ByteCode.class.getName() + option )
.getDeclaredMethod( "load" + option ).invoke( null );
}
catch ( Throwable e )
{
return BLANK_OPTION;
}
}
} }
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.codegen.bytecode;

import java.util.Collection;

import org.neo4j.codegen.ByteCodes;
import org.neo4j.codegen.CompilationFailureException;

interface ByteCodeChecker
{
void check( ClassLoader classpathLoader, Collection<ByteCodes> byteCodes ) throws CompilationFailureException;
}
Expand Up @@ -19,7 +19,9 @@
*/ */
package org.neo4j.codegen.bytecode; package org.neo4j.codegen.bytecode;


import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;


import org.neo4j.codegen.ByteCodes; import org.neo4j.codegen.ByteCodes;
Expand All @@ -28,12 +30,12 @@
import org.neo4j.codegen.CompilationFailureException; import org.neo4j.codegen.CompilationFailureException;
import org.neo4j.codegen.TypeReference; import org.neo4j.codegen.TypeReference;


public class ByteCodeGenerator extends CodeGenerator class ByteCodeGenerator extends CodeGenerator
{ {
private final Configuration configuration; private final Configuration configuration;
private final Map<TypeReference,ClassByteCodeWriter> classes = new HashMap<>(); private final Map<TypeReference,ClassByteCodeWriter> classes = new HashMap<>();


public ByteCodeGenerator( ClassLoader parentClassLoader, Configuration configuration) ByteCodeGenerator( ClassLoader parentClassLoader, Configuration configuration )
{ {
super( parentClassLoader ); super( parentClassLoader );
this.configuration = configuration; this.configuration = configuration;
Expand All @@ -58,6 +60,16 @@ protected ClassEmitter generate( TypeReference type, TypeReference base, TypeRef


protected Iterable<? extends ByteCodes> compile( ClassLoader classpathLoader ) throws CompilationFailureException protected Iterable<? extends ByteCodes> compile( ClassLoader classpathLoader ) throws CompilationFailureException
{ {
return classes.values().stream().map( ClassByteCodeWriter::toByteCodes )::iterator; List<ByteCodes> byteCodes = new ArrayList<>( classes.size() );
for ( ClassByteCodeWriter writer : classes.values() )
{
byteCodes.add( writer.toByteCodes() );
}
ByteCodeChecker checker = configuration.bytecodeChecker();
if ( checker != null )
{
checker.check( classpathLoader, byteCodes );
}
return byteCodes;
} }
} }

0 comments on commit 078c793

Please sign in to comment.