a whole lot of updates tbh

This commit is contained in:
sirjonasxx 2020-11-14 20:17:06 +01:00
parent fbabbed177
commit e03df8be08
71 changed files with 552 additions and 206 deletions

58
pom.xml
View File

@ -8,5 +8,63 @@
<artifactId>G-Wasm</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/bin</outputDirectory>
<archive>
<manifest>
<mainClass>wasm.GWasm</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>/src/main/resources</directory>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>G-Earth</groupId>
<artifactId>G-Earth</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,82 @@
package wasm;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.indices.TypeIdx;
import wasm.disassembly.modules.sections.export.Export;
import wasm.disassembly.modules.sections.export.ExportDesc;
import wasm.disassembly.modules.sections.imprt.Import;
import wasm.disassembly.modules.sections.imprt.ImportDesc;
import wasm.disassembly.types.FuncType;
import wasm.disassembly.types.ResultType;
import wasm.disassembly.types.ValType;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class GWasm {
public static void extractMethods(Module module) {
List<TypeIdx> typesFromImports = new ArrayList<>();
for (Import imp : module.getImportSection().getImports()) {
if (imp.getImportDescription().getImportType() == 0) {
typesFromImports.add((TypeIdx)imp.getImportDescription().getImportValue());
}
}
List<Export> exportList = module.getExportSection().getExports();
for(Export export : exportList) {
ExportDesc desc = export.getExportDesc();
if (desc.getExportType() == 0) {
FuncIdx funcIdx = (FuncIdx) desc.getExportValue();
TypeIdx typeIdx;
if (funcIdx.getX() < typesFromImports.size()) {
typeIdx = typesFromImports.get((int)(funcIdx.getX()));
}
else {
typeIdx = module.getFunctionSection().getTypeIdxVector().get((int)(funcIdx.getX()) - typesFromImports.size());
}
FuncType funcType = module.getTypeSection().getFunctionTypes().get((int)(typeIdx.getX()));
System.out.println(String.format("%s ::= %s -> %s",
export.getName(),
"(" + funcType.getParameterType().typeList().stream().map(Enum::name).collect(Collectors.joining(" ")) + ")",
"(" + funcType.getResultType().typeList().stream().map(Enum::name).collect(Collectors.joining(" ")) + ")"
));
}
}
}
public static void main(String[] args) throws IOException, InvalidOpCodeException {
long start = System.currentTimeMillis();
Module module = new Module("C:\\Users\\jonas\\Desktop\\Projects\\Jznnp\\S\\habbo2020\\rawfiles\\0.6.0_(7)\\habbo2020-global-prod.wasm.code.unityweb");
long end = System.currentTimeMillis();
System.out.println("Assembly time: " + (end - start));
// extractMethods(module);
//
// module.getImportSection().importFunctions(Collections.singletonList(
// new Import(
// "env",
// "consoleLog",
// new ImportDesc(module.getTypeSection().getTypeIdxForFuncType(new FuncType(
// new ResultType(Collections.singletonList(ValType.I32)),
// new ResultType(Collections.emptyList())
// )))
// )
// ));
start = System.currentTimeMillis();
module.assembleToFile("C:\\Users\\jonas\\Desktop\\Projects\\Jznnp\\S\\habbo2020\\rawfiles\\0.6.0_(7)\\out\\habbo2020-global-prod.wasm.code.unityweb");
end = System.currentTimeMillis();
System.out.println("Disassembly time: " + (end - start));
}
}

View File

@ -1,12 +1,13 @@
package wasm.disassembly.conventions;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
public interface Creator<B> {
B create(BufferedInputStream in) throws IOException, InvalidOpCodeException;
B create(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException;
}

View File

@ -1,6 +1,8 @@
package wasm.disassembly.conventions;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -9,16 +11,16 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class CustomVector<B> {
public class CustomVector<B> extends WASMOpCode {
private List<B> elements;
private Assembler<B> assembler;
public CustomVector(BufferedInputStream in, Creator<B> creator, Assembler<B> assembler) throws IOException, InvalidOpCodeException {
public CustomVector(BufferedInputStream in, Creator<B> creator, Assembler<B> assembler, Module module) throws IOException, InvalidOpCodeException {
long length = WUnsignedInt.read(in, 32);
elements = new ArrayList<>();
for (int i = 0; i < length; i++) {
elements.add(creator.create(in));
elements.add(creator.create(in, module));
}
this.assembler = assembler;
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.conventions;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -14,11 +15,11 @@ public class Vector<B extends WASMOpCode> extends WASMOpCode {
private List<B> elements;
public Vector(BufferedInputStream in, Creator<B> creator) throws IOException, InvalidOpCodeException {
public Vector(BufferedInputStream in, Creator<B> creator, Module module) throws IOException, InvalidOpCodeException {
long length = WUnsignedInt.read(in, 32);
elements = new ArrayList<>();
for (int i = 0; i < length; i++) {
elements.add(creator.create(in));
elements.add(creator.create(in, module));
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.instructions;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -13,11 +14,11 @@ public class Expression extends WASMOpCode {
private List<Instr> instructions;
public Expression(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public Expression(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
instructions = new ArrayList<>();
InstrType type;
while ((type = InstrFactory.disassembleType(in)) != InstrType.END) {
instructions.add(InstrFactory.disassemble(in, type));
instructions.add(InstrFactory.disassemble(in, type, module));
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.instructions;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import java.io.IOException;
import java.io.OutputStream;

View File

@ -8,6 +8,7 @@ import wasm.disassembly.instructions.misc.SingleByteInstr;
import wasm.disassembly.instructions.numeric.*;
import wasm.disassembly.instructions.variable.GlobalVariableInstr;
import wasm.disassembly.instructions.variable.LocalVariableInstr;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -70,7 +71,7 @@ public class InstrFactory {
map.put(InstrType.IXX_TRUNC_SAT_FXX_SU, TruncSatInstr::new);
}
public static Instr disassemble(BufferedInputStream in, InstrType instrType) throws InvalidOpCodeException, IOException {
public static Instr disassemble(BufferedInputStream in, InstrType instrType, Module module) throws InvalidOpCodeException, IOException {
if (instrType == InstrType.END || instrType == InstrType.ELSE) {
throw new InvalidOpCodeException("Instruction invalid as a standalone instruction");
}
@ -79,7 +80,7 @@ public class InstrFactory {
throw new InvalidOpCodeException("Invalid instruction prefix");
}
return map.get(instrType).get(in, instrType);
return map.get(instrType).get(in, instrType, module);
}
}

View File

@ -1,12 +1,13 @@
package wasm.disassembly.instructions;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
public interface InstrSupplier {
Instr get(BufferedInputStream in, InstrType type) throws IOException, InvalidOpCodeException;
Instr get(BufferedInputStream in, InstrType type, Module module) throws IOException, InvalidOpCodeException;
}

View File

@ -4,6 +4,7 @@ import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrFactory;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -16,15 +17,15 @@ public class BlockInstr extends Instr {
private List<Instr> blockInstructions;
private BlockType blockType;
public BlockInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public BlockInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
blockType = new BlockType(in);
blockType = new BlockType(in, module);
blockInstructions = new ArrayList<>();
InstrType type;
while ((type = InstrFactory.disassembleType(in)) != InstrType.END) {
blockInstructions.add(InstrFactory.disassemble(in, type));
blockInstructions.add(InstrFactory.disassemble(in, type, module));
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.instructions.control;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.types.ValType;
import wasm.disassembly.values.WSignedLong;
@ -13,7 +14,7 @@ public class BlockType extends WASMOpCode {
private Object value;
public BlockType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public BlockType(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
in.mark(1);
int first = in.read();

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.control;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.LabelIdx;
import java.io.BufferedInputStream;
@ -13,9 +14,9 @@ public class BranchInstr extends Instr {
private LabelIdx labelIdx;
public BranchInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public BranchInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
labelIdx = new LabelIdx(in);
labelIdx = new LabelIdx(in, module);
}
public BranchInstr(InstrType instrType, LabelIdx labelIdx) throws IOException {

View File

@ -1,9 +1,11 @@
package wasm.disassembly.instructions.control;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Creator;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.LabelIdx;
import java.io.BufferedInputStream;
@ -15,10 +17,10 @@ public class BranchTableInstr extends Instr {
private Vector<LabelIdx> table;
private LabelIdx labelIdx;
public BranchTableInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public BranchTableInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
table = new Vector<>(in, LabelIdx::new);
labelIdx = new LabelIdx(in);
table = new Vector<>(in, LabelIdx::new, module);
labelIdx = new LabelIdx(in, module);
}
public BranchTableInstr(InstrType instrType, Vector<LabelIdx> table, LabelIdx labelIdx) throws IOException {

View File

@ -3,7 +3,9 @@ package wasm.disassembly.instructions.control;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.indices.TypeIdx;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -11,32 +13,32 @@ import java.io.OutputStream;
public class CallIndirectInstr extends Instr {
private FuncIdx funcIdx;
private TypeIdx typeIdx;
public CallIndirectInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public CallIndirectInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
funcIdx = new FuncIdx(in);
typeIdx = new TypeIdx(in, module);
if (in.read() != 0x00) {
throw new InvalidOpCodeException("Unexpected non-zero byte");
}
}
public CallIndirectInstr(InstrType instrType, FuncIdx funcIdx) throws IOException {
public CallIndirectInstr(InstrType instrType, TypeIdx typeIdx) throws IOException {
super(instrType);
this.funcIdx = funcIdx;
this.typeIdx = typeIdx;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
funcIdx.assemble(out);
typeIdx.assemble(out);
out.write(0x00);
}
public FuncIdx getFuncIdx() {
return funcIdx;
public TypeIdx getTypeIdx() {
return typeIdx;
}
public void setFuncIdx(FuncIdx funcIdx) {
this.funcIdx = funcIdx;
public void setTypeIdx(TypeIdx typeIdx) {
this.typeIdx = typeIdx;
}
}

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.control;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import java.io.BufferedInputStream;
@ -13,13 +14,13 @@ public class CallInstr extends Instr {
private FuncIdx funcIdx;
public CallInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public CallInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
funcIdx = new FuncIdx(in);
funcIdx = new FuncIdx(in, module);
}
public CallInstr(InstrType instrType, FuncIdx funcIdx) throws IOException {
super(instrType);
public CallInstr(FuncIdx funcIdx) throws IOException {
super(InstrType.CALL); // can only be call instr
this.funcIdx = funcIdx;
}

View File

@ -4,6 +4,7 @@ import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrFactory;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -19,10 +20,10 @@ public class IfElseInstr extends Instr {
private BlockType blockType;
public IfElseInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public IfElseInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
blockType = new BlockType(in);
blockType = new BlockType(in, module);
ifInstructions = new ArrayList<>();
elseInstructions = null;
List<Instr> currentBlock = ifInstructions;
@ -34,7 +35,7 @@ public class IfElseInstr extends Instr {
currentBlock = elseInstructions;
}
else {
currentBlock.add(InstrFactory.disassemble(in, type));
currentBlock.add(InstrFactory.disassemble(in, type, module));
}
}
}

View File

@ -3,13 +3,14 @@ package wasm.disassembly.instructions.memory;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Mem0Instr extends Instr {
public Mem0Instr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public Mem0Instr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
if (in.read() != 0x00) {
throw new InvalidOpCodeException("Unexpected non-zero byte");

View File

@ -2,6 +2,7 @@ package wasm.disassembly.instructions.memory;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -13,7 +14,7 @@ public class MemArg extends WASMOpCode {
private long align;
private long offset;
public MemArg(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public MemArg(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
align = WUnsignedInt.read(in, 32);
offset = WUnsignedInt.read(in, 32);
}

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.memory;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -12,9 +13,9 @@ public class MemInstr extends Instr {
private MemArg memArg;
public MemInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public MemInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
memArg = new MemArg(in);
memArg = new MemArg(in, module);
}
public MemInstr(InstrType instrType, MemArg memArg) throws IOException {

View File

@ -2,13 +2,14 @@ package wasm.disassembly.instructions.misc;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class SingleByteInstr extends Instr {
public SingleByteInstr(BufferedInputStream in, InstrType instrType) throws IOException {
public SingleByteInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException {
super(instrType);
}

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.numeric;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WFloat;
import java.io.BufferedInputStream;
@ -13,13 +14,13 @@ public class NumericF32ConstInstr extends Instr {
private float constValue;
public NumericF32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public NumericF32ConstInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
constValue = WFloat.read(in);
}
public NumericF32ConstInstr(InstrType instrType, float constValue) throws IOException {
super(instrType);
public NumericF32ConstInstr(float constValue) throws IOException {
super(InstrType.F32_CONST);
this.constValue = constValue;
}

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.numeric;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WDouble;
import java.io.BufferedInputStream;
@ -13,13 +14,13 @@ public class NumericF64ConstInstr extends Instr {
private double constValue;
public NumericF64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public NumericF64ConstInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
constValue = WDouble.read(in);
}
public NumericF64ConstInstr(InstrType instrType, float constValue) throws IOException {
super(instrType);
public NumericF64ConstInstr(float constValue) throws IOException {
super(InstrType.F64_CONST);
this.constValue = constValue;
}

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.numeric;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WSignedInt;
import java.io.BufferedInputStream;
@ -13,13 +14,13 @@ public class NumericI32ConstInstr extends Instr {
private int constValue;
public NumericI32ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public NumericI32ConstInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
constValue = WSignedInt.read(in, 32);
}
public NumericI32ConstInstr(InstrType instrType, int constValue) throws IOException {
super(instrType);
public NumericI32ConstInstr(int constValue) throws IOException {
super(InstrType.I32_CONST);
this.constValue = constValue;
}

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.numeric;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WSignedLong;
import java.io.BufferedInputStream;
@ -13,13 +14,13 @@ public class NumericI64ConstInstr extends Instr {
private long constValue;
public NumericI64ConstInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public NumericI64ConstInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
constValue = WSignedLong.read(in, 64);
}
public NumericI64ConstInstr(InstrType instrType, long constValue) throws IOException {
super(instrType);
public NumericI64ConstInstr(long constValue) throws IOException {
super(InstrType.I64_CONST);
this.constValue = constValue;
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.instructions.numeric;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -9,7 +10,7 @@ import java.io.OutputStream;
public class NumericInstr extends Instr {
public NumericInstr(BufferedInputStream in, InstrType instrType) throws IOException {
public NumericInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException {
super(instrType);
}

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.numeric;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -13,7 +14,7 @@ public class TruncSatInstr extends Instr {
private long type;
public TruncSatInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public TruncSatInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
type = WUnsignedInt.read(in, 32);
if (type < 0 || type > 7) {

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.variable;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.GlobalIdx;
import java.io.BufferedInputStream;
@ -13,9 +14,9 @@ public class GlobalVariableInstr extends Instr {
private GlobalIdx globalIdx;
public GlobalVariableInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public GlobalVariableInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
globalIdx = new GlobalIdx(in);
globalIdx = new GlobalIdx(in, module);
}
public GlobalVariableInstr(InstrType instrType, GlobalIdx globalIdx) throws IOException {

View File

@ -3,6 +3,7 @@ package wasm.disassembly.instructions.variable;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.instructions.Instr;
import wasm.disassembly.instructions.InstrType;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.LocalIdx;
import java.io.BufferedInputStream;
@ -13,9 +14,9 @@ public class LocalVariableInstr extends Instr {
private LocalIdx localIdx;
public LocalVariableInstr(BufferedInputStream in, InstrType instrType) throws IOException, InvalidOpCodeException {
public LocalVariableInstr(BufferedInputStream in, InstrType instrType, Module module) throws IOException, InvalidOpCodeException {
super(instrType);
localIdx = new LocalIdx(in);
localIdx = new LocalIdx(in, module);
}
public LocalVariableInstr(InstrType instrType, LocalIdx localIdx) throws IOException {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.sections.Section;
import wasm.disassembly.modules.sections.code.CodeSection;
import wasm.disassembly.modules.sections.custom.CustomSection;
@ -17,9 +18,7 @@ import wasm.disassembly.modules.sections.start.StartSection;
import wasm.disassembly.modules.sections.table.TableSection;
import wasm.disassembly.modules.sections.type.TypeSection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
@ -41,6 +40,7 @@ public class Module extends WASMOpCode {
private DataSection dataSection;
private List<List<CustomSection>> customSectionsList;
private List<FuncIdx> allFuncIdxs = new ArrayList<>();
public Module(BufferedInputStream in) throws IOException, InvalidOpCodeException {
customSectionsList = new ArrayList<>();
@ -49,29 +49,34 @@ public class Module extends WASMOpCode {
version = new Version(in);
disassembleCustomSections(in);
typeSection = isNextSection(in, 1) ? new TypeSection(in) : null;
typeSection = isNextSection(in, 1) ? new TypeSection(in, this) : null;
disassembleCustomSections(in);
importSection = isNextSection(in, 2) ? new ImportSection(in) : null;
importSection = isNextSection(in, 2) ? new ImportSection(in, this) : null;
disassembleCustomSections(in);
functionSection = isNextSection(in, 3) ? new FunctionSection(in) : null;
functionSection = isNextSection(in, 3) ? new FunctionSection(in, this) : null;
disassembleCustomSections(in);
tableSection = isNextSection(in, 4) ? new TableSection(in) : null;
tableSection = isNextSection(in, 4) ? new TableSection(in, this) : null;
disassembleCustomSections(in);
memorySection = isNextSection(in, 5) ? new MemorySection(in) : null;
memorySection = isNextSection(in, 5) ? new MemorySection(in, this) : null;
disassembleCustomSections(in);
globalSection = isNextSection(in , 6) ? new GlobalSection(in) : null;
globalSection = isNextSection(in , 6) ? new GlobalSection(in, this) : null;
disassembleCustomSections(in);
exportSection = isNextSection(in, 7) ? new ExportSection(in) : null;
exportSection = isNextSection(in, 7) ? new ExportSection(in, this) : null;
disassembleCustomSections(in);
startSection = isNextSection(in, 8) ? new StartSection(in) : null;
startSection = isNextSection(in, 8) ? new StartSection(in, this) : null;
disassembleCustomSections(in);
elementSection = isNextSection(in, 9) ? new ElementSection(in) : null;
elementSection = isNextSection(in, 9) ? new ElementSection(in, this) : null;
disassembleCustomSections(in);
codeSection = isNextSection(in, 10) ? new CodeSection(in) : null;
codeSection = isNextSection(in, 10) ? new CodeSection(in, this) : null;
disassembleCustomSections(in);
dataSection = isNextSection(in, 11) ? new DataSection(in) : null;
dataSection = isNextSection(in, 11) ? new DataSection(in, this) : null;
disassembleCustomSections(in);
in.close();
}
public Module(String fileName) throws IOException, InvalidOpCodeException {
this(new BufferedInputStream(new FileInputStream(new File(fileName))));
}
public Module(Magic magic, Version version, TypeSection typeSection, ImportSection importSection, FunctionSection functionSection, TableSection tableSection, MemorySection memorySection, GlobalSection globalSection, ExportSection exportSection, StartSection startSection, ElementSection elementSection, CodeSection codeSection, DataSection dataSection) {
@ -94,7 +99,6 @@ public class Module extends WASMOpCode {
customSectionsList.add(new ArrayList<>());
}
}
public Module(TypeSection typeSection, ImportSection importSection, FunctionSection functionSection, TableSection tableSection, MemorySection memorySection, GlobalSection globalSection, ExportSection exportSection, StartSection startSection, ElementSection elementSection, CodeSection codeSection, DataSection dataSection, List<List<CustomSection>> customSectionsList) {
this(new Magic(), new Version(), typeSection, importSection, functionSection, tableSection, memorySection,
globalSection, exportSection, startSection, elementSection, codeSection, dataSection);
@ -104,7 +108,7 @@ public class Module extends WASMOpCode {
List<CustomSection> customSections = new ArrayList<>();
while (isNextSection(in, 0)) {
customSections.add(CustomSectionFactory.get(in));
customSections.add(CustomSectionFactory.get(in, this));
}
in.reset();
@ -144,6 +148,11 @@ public class Module extends WASMOpCode {
}
}
public void assembleToFile(String fileName) throws IOException, InvalidOpCodeException {
FileOutputStream habAssembled = new FileOutputStream(fileName);
assemble(habAssembled);
habAssembled.close();
}
public Magic getMagic() {
return magic;
@ -256,4 +265,8 @@ public class Module extends WASMOpCode {
public void setCustomSectionsList(List<List<CustomSection>> customSectionsList) {
this.customSectionsList = customSectionsList;
}
public List<FuncIdx> getAllFuncIdxs() {
return allFuncIdxs;
}
}

View File

@ -1,38 +1,41 @@
package wasm.disassembly.modules.indices;
import com.sun.org.apache.xpath.internal.functions.FuncId;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class FuncIdx extends WASMOpCode {
public static int OFFSET = 0;
private long x;
public FuncIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public FuncIdx(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
x = WUnsignedInt.read(in, 32);
module.getAllFuncIdxs().add(this);
}
public FuncIdx(long x) {
this.x = x - OFFSET;
public FuncIdx(long x, Module module) {
this.x = x;
module.getAllFuncIdxs().add(this);
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
WUnsignedInt.write(x + OFFSET, out, 32);
WUnsignedInt.write(x, out, 32);
}
public long getX() {
return x + OFFSET;
return x;
}
public void setX(long x) {
this.x = x - OFFSET;
this.x = x;
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.indices;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -12,7 +13,7 @@ public class GlobalIdx extends WASMOpCode {
private long x;
public GlobalIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public GlobalIdx(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
x = WUnsignedInt.read(in, 32);
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.indices;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -12,7 +13,7 @@ public class LabelIdx extends WASMOpCode {
private long l;
public LabelIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public LabelIdx(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
l = WUnsignedInt.read(in, 32);
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.indices;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -12,7 +13,7 @@ public class LocalIdx extends WASMOpCode {
private long x;
public LocalIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public LocalIdx(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
x = WUnsignedInt.read(in, 32);
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.indices;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -12,7 +13,7 @@ public class MemIdx extends WASMOpCode {
private long x;
public MemIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public MemIdx(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
x = WUnsignedInt.read(in, 32);
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.indices;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -12,7 +13,7 @@ public class TableIdx extends WASMOpCode {
private long x;
public TableIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public TableIdx(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
x = WUnsignedInt.read(in, 32);
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.indices;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -12,7 +13,7 @@ public class TypeIdx extends WASMOpCode {
private long x;
public TypeIdx(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public TypeIdx(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
x = WUnsignedInt.read(in, 32);
}

View File

@ -0,0 +1,63 @@
package wasm.disassembly.modules.misc;
import wasm.disassembly.instructions.Expression;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.indices.TypeIdx;
import wasm.disassembly.modules.sections.code.Code;
import wasm.disassembly.modules.sections.code.Func;
import wasm.disassembly.types.FuncType;
import wasm.disassembly.types.ValType;
import java.util.List;
public class Function {
private FuncType funcType;
private List<ValType> locals;
private Expression code;
public Function(FuncType funcType, List<ValType> locals, Expression code) {
this.funcType = funcType;
this.locals = locals;
this.code = code;
}
public FuncIdx addToModule(Module module) {
TypeIdx typeIdx = module.getTypeSection().getTypeIdxForFuncType(funcType);
ValType[] valTypeArr = locals.toArray(new ValType[0]);
Func func = new Func(valTypeArr, code);
module.getFunctionSection().getTypeIdxVector().add(typeIdx);
module.getCodeSection().getCodesEntries().add(new Code(func));
return new FuncIdx(
module.getImportSection().getTotalFuncImports() + module.getCodeSection().getCodesEntries().size() - 1,
module
);
}
public FuncType getFuncType() {
return funcType;
}
public void setFuncType(FuncType funcType) {
this.funcType = funcType;
}
public List<ValType> getLocals() {
return locals;
}
public void setLocals(List<ValType> locals) {
this.locals = locals;
}
public Expression getCode() {
return code;
}
public void setCode(Expression code) {
this.code = code;
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.code;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -13,9 +14,9 @@ public class Code extends WASMOpCode {
private Func code;
public Code(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public Code(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
long sizeInBytes = WUnsignedInt.read(in, 32); // don't use
code = new Func(in);
code = new Func(in, module);
}
public Code(Func code) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.code;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -16,9 +17,9 @@ public class CodeSection extends Section {
private Vector<Code> codesEntries;
public CodeSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public CodeSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, CODE_SECTION_ID);
codesEntries = new Vector<>(in, Code::new);
codesEntries = new Vector<>(in, Code::new, module);
}
public CodeSection(List<Code> codesEntries) {

View File

@ -4,10 +4,13 @@ import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.instructions.Expression;
import wasm.disassembly.modules.Module;
import wasm.disassembly.types.ValType;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Func extends WASMOpCode {
@ -16,9 +19,9 @@ public class Func extends WASMOpCode {
private Expression expression;
public Func(BufferedInputStream in) throws IOException, InvalidOpCodeException {
localss = new Vector<>(in, Locals::new);
expression = new Expression(in);
public Func(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
localss = new Vector<>(in, Locals::new, module);
expression = new Expression(in, module);
}
public Func(List<Locals> localss, Expression expression) {
@ -26,6 +29,11 @@ public class Func extends WASMOpCode {
this.expression = expression;
}
public Func(ValType[] localVariables, Expression expression) {
setLocalVariables(localVariables);
this.expression = expression;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
localss.assemble(out);
@ -47,4 +55,21 @@ public class Func extends WASMOpCode {
public void setExpression(Expression expression) {
this.expression = expression;
}
public void setLocalVariables(ValType[] locals) {
List<Locals> localss = new ArrayList<>();
Locals current = null;
for (ValType valType : locals) {
if (current == null || current.getValType() != valType) {
if (current != null) localss.add(current);
current = new Locals(1, valType);
}
else {
current.setAmount(current.getAmount() + 1);
}
}
if (current != null) localss.add(current);
this.localss = new Vector<>(localss);
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.code;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.types.ValType;
import wasm.disassembly.values.WUnsignedInt;
@ -14,12 +15,12 @@ public class Locals extends WASMOpCode {
private long amount;
private ValType valType;
public Locals(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public Locals(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
amount = WUnsignedInt.read(in, 32);
valType = ValType.from_val(in.read());
}
public Locals(long amount, ValType valType) throws InvalidOpCodeException {
public Locals(long amount, ValType valType) {
this.amount = amount;
this.valType = valType;
}

View File

@ -1,6 +1,7 @@
package wasm.disassembly.modules.sections.custom;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import wasm.disassembly.values.WName;
@ -14,7 +15,7 @@ public abstract class CustomSection extends Section {
private String name;
public CustomSection(long size, String name) throws IOException, InvalidOpCodeException {
public CustomSection(Module module, long size, String name) throws IOException, InvalidOpCodeException {
super(CUSTOM_SECTION_ID, size);
this.name = name;
}

View File

@ -1,6 +1,7 @@
package wasm.disassembly.modules.sections.custom;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WName;
import wasm.disassembly.values.WUnsignedInt;
@ -9,12 +10,12 @@ import java.io.IOException;
public class CustomSectionFactory {
public static CustomSection get(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public static CustomSection get(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
long size = WUnsignedInt.read(in, 32);
String name = WName.read(in);
// select implementation
return new UnImplementedCustomSection(in, size, name);
return new UnImplementedCustomSection(in, module, size, name);
}
}

View File

@ -1,6 +1,7 @@
package wasm.disassembly.modules.sections.custom;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WName;
import java.io.BufferedInputStream;
@ -13,8 +14,8 @@ public class UnImplementedCustomSection extends CustomSection {
private byte[] bytes;
public UnImplementedCustomSection(BufferedInputStream in, long size, String name) throws IOException, InvalidOpCodeException {
super(size, name);
public UnImplementedCustomSection(BufferedInputStream in, Module module, long size, String name) throws IOException, InvalidOpCodeException {
super(module, size, name);
ByteArrayOutputStream nameOut = new ByteArrayOutputStream();
WName.write(name, nameOut);

View File

@ -3,6 +3,7 @@ package wasm.disassembly.modules.sections.data;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.instructions.Expression;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.MemIdx;
import wasm.disassembly.values.WUnsignedInt;
@ -17,9 +18,9 @@ public class Data extends WASMOpCode {
private byte[] data;
public Data(BufferedInputStream in) throws IOException, InvalidOpCodeException {
dataMemId = new MemIdx(in);
offset = new Expression(in);
public Data(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
dataMemId = new MemIdx(in, module);
offset = new Expression(in, module);
long length = WUnsignedInt.read(in, 32);
data = new byte[(int)length];
in.read(data);

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.data;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -16,9 +17,9 @@ public class DataSection extends Section {
private Vector<Data> dataSegments;
public DataSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public DataSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, DATA_SECTION_ID);
dataSegments = new Vector<>(in, Data::new);
dataSegments = new Vector<>(in, Data::new, module);
}
public DataSection(List<Data> dataSegments) {

View File

@ -4,6 +4,7 @@ import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.instructions.Expression;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.indices.TableIdx;
@ -17,10 +18,10 @@ public class Elem extends WASMOpCode {
private Expression offset;
private Vector<FuncIdx> init;
public Elem(BufferedInputStream in) throws IOException, InvalidOpCodeException {
tableIdx = new TableIdx(in);
offset = new Expression(in);
init = new Vector<>(in, FuncIdx::new);
public Elem(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
tableIdx = new TableIdx(in, module);
offset = new Expression(in, module);
init = new Vector<>(in, FuncIdx::new, module);
}
public Elem(TableIdx tableIdx, Expression offset, Vector<FuncIdx> init) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.element;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -15,9 +16,9 @@ public class ElementSection extends Section {
private Vector<Elem> elementSegments;
public ElementSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public ElementSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, ELEMENT_SECTION_ID);
elementSegments = new Vector<>(in, Elem::new);
elementSegments = new Vector<>(in, Elem::new, module);
}
public ElementSection(List<Elem> elementSegments) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.export;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WName;
import java.io.BufferedInputStream;
@ -13,9 +14,9 @@ public class Export extends WASMOpCode {
private String name;
private ExportDesc exportDesc;
public Export(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public Export(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
name = WName.read(in);
exportDesc = new ExportDesc(in);
exportDesc = new ExportDesc(in, module);
}
public Export(String name, ExportDesc exportDesc) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.export;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.indices.GlobalIdx;
import wasm.disassembly.modules.indices.MemIdx;
@ -17,16 +18,16 @@ public class ExportDesc extends WASMOpCode {
private int exportType;
public ExportDesc(BufferedInputStream in) throws InvalidOpCodeException, IOException {
public ExportDesc(BufferedInputStream in, Module module) throws InvalidOpCodeException, IOException {
exportType = in.read();
if (exportType < 0x00 || exportType > 0x03) {
throw new InvalidOpCodeException("invalid importdesc type");
}
exportValue = exportType == 0x00 ? new FuncIdx(in) :
(exportType == 0x01 ? new TableIdx(in) :
(exportType == 0x02 ? new MemIdx(in) :
new GlobalIdx(in)));
exportValue = exportType == 0x00 ? new FuncIdx(in, module) :
(exportType == 0x01 ? new TableIdx(in, module) :
(exportType == 0x02 ? new MemIdx(in, module) :
new GlobalIdx(in, module)));
}
public ExportDesc(WASMOpCode exportValue, int exportType) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.export;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -15,9 +16,9 @@ public class ExportSection extends Section {
private Vector<Export> exports;
public ExportSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public ExportSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, EXPORT_SECTION_ID);
exports = new Vector<>(in, Export::new);
exports = new Vector<>(in, Export::new, module);
}
public ExportSection(List<Export> exports) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.function;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.TypeIdx;
import wasm.disassembly.modules.sections.Section;
@ -17,9 +18,9 @@ public class FunctionSection extends Section {
private Vector<TypeIdx> typeIdxVector;
public FunctionSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public FunctionSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, FUNCTION_SECTION_ID);
typeIdxVector = new Vector<>(in, TypeIdx::new);
typeIdxVector = new Vector<>(in, TypeIdx::new, module);
}
public FunctionSection(List<TypeIdx> typeIdxList) {

View File

@ -3,6 +3,7 @@ package wasm.disassembly.modules.sections.global;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.instructions.Expression;
import wasm.disassembly.modules.Module;
import wasm.disassembly.types.GlobalType;
import java.io.BufferedInputStream;
@ -14,9 +15,9 @@ public class Global extends WASMOpCode {
private GlobalType globalType;
private Expression expression;
public Global(BufferedInputStream in) throws IOException, InvalidOpCodeException {
globalType = new GlobalType(in);
expression = new Expression(in);
public Global(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
globalType = new GlobalType(in, module);
expression = new Expression(in, module);
}
public Global(GlobalType globalType, Expression expression) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.global;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -16,9 +17,9 @@ public class GlobalSection extends Section {
private Vector<Global> globals;
public GlobalSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public GlobalSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, GLOBAL_SECTION_ID);
globals = new Vector<>(in, Global::new);
globals = new Vector<>(in, Global::new, module);
}
public GlobalSection(List<Global> globals) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.imprt;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WName;
import java.io.BufferedInputStream;
@ -14,10 +15,10 @@ public class Import extends WASMOpCode {
private String name;
private ImportDesc importDescription;
public Import(BufferedInputStream in) throws IOException, InvalidOpCodeException {
module = WName.read(in);
public Import(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
this.module = WName.read(in);
name = WName.read(in);
importDescription = new ImportDesc(in);
importDescription = new ImportDesc(in, module);
}
public Import(String module, String name, ImportDesc importDescription) {

View File

@ -2,6 +2,8 @@ package wasm.disassembly.modules.sections.imprt;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.indices.TypeIdx;
import wasm.disassembly.types.GlobalType;
import wasm.disassembly.types.MemType;
@ -16,16 +18,16 @@ public class ImportDesc extends WASMOpCode {
private WASMOpCode importValue;
private int importType;
public ImportDesc(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public ImportDesc(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
importType = in.read();
if (importType < 0x00 || importType > 0x03) {
throw new InvalidOpCodeException("invalid importdesc type");
}
importValue = importType == 0x00 ? new TypeIdx(in) :
(importType == 0x01 ? new TableType(in) :
(importType == 0x02 ? new MemType(in) :
new GlobalType(in)));
importValue = importType == 0x00 ? new TypeIdx(in, module) :
(importType == 0x01 ? new TableType(in, module) :
(importType == 0x02 ? new MemType(in, module) :
new GlobalType(in, module)));
}
public ImportDesc(WASMOpCode importValue, int importType) {

View File

@ -1,7 +1,9 @@
package wasm.disassembly.modules.sections.imprt;
import com.sun.org.apache.xpath.internal.functions.FuncId;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.sections.Section;
@ -14,13 +16,24 @@ import java.util.List;
public class ImportSection extends Section {
public static final int IMPORT_SECTION_ID = 2;
private Module module;
private int totalFuncImports;
private Vector<Import> imports;
public ImportSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public ImportSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, IMPORT_SECTION_ID);
imports = new Vector<>(in, Import::new);
this.module = module;
imports = new Vector<>(in, Import::new, module);
totalFuncImports = 0;
for (Import imprt : imports.getElements()) {
if (imprt.getImportDescription().getImportType() == 0) {
totalFuncImports++;
}
}
}
public ImportSection(List<Import> imports) {
@ -41,11 +54,28 @@ public class ImportSection extends Section {
this.imports = new Vector<>(imports);
}
public void importFunctions(List<Import> functions) {
List<Import> newImports = new ArrayList<>(functions);
newImports.addAll(imports.getElements());
setImports(newImports);
FuncIdx.OFFSET += functions.size();
public List<FuncIdx> importFunctions(List<Import> functions) throws InvalidOpCodeException {
for (Import imprt : functions) {
if (imprt.getImportDescription().getImportType() != 0) {
throw new InvalidOpCodeException("Tried to import non-function as function");
}
}
for (FuncIdx funcIdx : module.getAllFuncIdxs()) {
if (funcIdx.getX() >= totalFuncImports) {
funcIdx.setX(funcIdx.getX() + functions.size());
}
}
List<FuncIdx> newFuncIdxs = new ArrayList<>();
for (long i = totalFuncImports; i < totalFuncImports + functions.size(); i++) {
newFuncIdxs.add(new FuncIdx(i, module));
}
imports.getElements().addAll(functions);
totalFuncImports += functions.size();
return newFuncIdxs;
}
public int getTotalFuncImports() {
return totalFuncImports;
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.memory;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.types.MemType;
import java.io.BufferedInputStream;
@ -12,8 +13,8 @@ public class Mem extends WASMOpCode {
private MemType memType;
public Mem(BufferedInputStream in) throws IOException, InvalidOpCodeException {
memType = new MemType(in);
public Mem(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
memType = new MemType(in, module);
}
public Mem(MemType memType) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.memory;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -16,9 +17,9 @@ public class MemorySection extends Section {
private Vector<Mem> memories;
public MemorySection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public MemorySection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, MEMORY_SECTION_ID);
memories = new Vector<>(in, Mem::new);
memories = new Vector<>(in, Mem::new, module);
}
public MemorySection(List<Mem> memories) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.start;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import java.io.BufferedInputStream;
@ -12,8 +13,8 @@ public class Start extends WASMOpCode {
private FuncIdx funcIdx;
public Start(BufferedInputStream in) throws IOException, InvalidOpCodeException {
funcIdx = new FuncIdx(in);
public Start(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
funcIdx = new FuncIdx(in, module);
}
public Start(FuncIdx funcIdx) {

View File

@ -1,6 +1,7 @@
package wasm.disassembly.modules.sections.start;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -13,9 +14,9 @@ public class StartSection extends Section {
private Start start;
public StartSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public StartSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, START_SECTION_ID);
start = new Start(in);
start = new Start(in, module);
}
public StartSection(Start start) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.table;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.types.TableType;
import java.io.BufferedInputStream;
@ -12,8 +13,8 @@ public class Table extends WASMOpCode {
private TableType tableType;
public Table(BufferedInputStream in) throws IOException, InvalidOpCodeException {
tableType = new TableType(in);
public Table(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
tableType = new TableType(in, module);
}
public Table(TableType tableType) {

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.table;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.Section;
import java.io.BufferedInputStream;
@ -16,9 +17,9 @@ public class TableSection extends Section {
private Vector<Table> tables;
public TableSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public TableSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, TABLE_SECTION_ID);
tables = new Vector<>(in, Table::new);
tables = new Vector<>(in, Table::new, module);
}
public TableSection(List<Table> tables) {

View File

@ -2,6 +2,8 @@ package wasm.disassembly.modules.sections.type;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.TypeIdx;
import wasm.disassembly.modules.sections.Section;
import wasm.disassembly.types.FuncType;
@ -17,9 +19,9 @@ public class TypeSection extends Section {
private Vector<FuncType> functionTypes;
public TypeSection(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public TypeSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, TYPE_SECTION_ID);
functionTypes = new Vector<>(in, FuncType::new);
functionTypes = new Vector<>(in, FuncType::new, module);
}
public TypeSection(List<FuncType> functionTypes) {
@ -39,4 +41,16 @@ public class TypeSection extends Section {
public void setFunctionTypes(List<FuncType> functionTypes) {
this.functionTypes = new Vector<>(functionTypes);
}
public TypeIdx getTypeIdxForFuncType(FuncType newFuncType) {
for (int i = 0; i < getFunctionTypes().size(); i++) {
FuncType funcType = getFunctionTypes().get(i);
if (funcType.equals(newFuncType)) {
return new TypeIdx(i);
}
}
getFunctionTypes().add(newFuncType);
return new TypeIdx(getFunctionTypes().size() - 1);
}
}

View File

@ -2,6 +2,8 @@ package wasm.disassembly.types;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.code.Func;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -12,11 +14,11 @@ public class FuncType extends WASMOpCode {
private ResultType parameterType;
private ResultType resultType;
public FuncType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public FuncType(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
if (in.read() != 0x60) throw new InvalidOpCodeException("Function types must be encoded with 0x60");
parameterType = new ResultType(in);
resultType = new ResultType(in);
parameterType = new ResultType(in, module);
resultType = new ResultType(in, module);
}
public FuncType(ResultType parameterType, ResultType resultType) {
@ -46,4 +48,16 @@ public class FuncType extends WASMOpCode {
public void setResultType(ResultType resultType) {
this.resultType = resultType;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof FuncType)) {
return false;
}
FuncType other = (FuncType) obj;
return parameterType.equals(other.parameterType) &&
resultType.equals(other.resultType);
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.types;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -12,7 +13,7 @@ public class GlobalType extends WASMOpCode {
private ValType valType;
private Mutability mutability;
public GlobalType(BufferedInputStream in) throws IOException {
public GlobalType(BufferedInputStream in, Module module) throws IOException {
valType = ValType.from_val(in.read());
mutability = Mutability.from_val(in.read());
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.types;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -13,7 +14,7 @@ public class Limits extends WASMOpCode {
private long min;
private long max;
public Limits(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public Limits(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
int flag = in.read();
if (flag == 0x00) {
min = WUnsignedInt.read(in, 32);

View File

@ -2,6 +2,7 @@ package wasm.disassembly.types;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -11,8 +12,8 @@ public class MemType extends WASMOpCode {
private Limits limits;
public MemType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
limits = new Limits(in);
public MemType(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
limits = new Limits(in, module);
}
public MemType(Limits limits) {

View File

@ -3,6 +3,7 @@ package wasm.disassembly.types;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.conventions.CustomVector;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -13,11 +14,12 @@ public class ResultType extends WASMOpCode {
private CustomVector<ValType> vector;
public ResultType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public ResultType(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
vector = new CustomVector<>(
in,
in1 -> ValType.from_val(in1.read()),
(valType, out) -> out.write(valType.val)
(in1, m) -> ValType.from_val(in1.read()),
(valType, out) -> out.write(valType.val),
module
);
}
@ -48,4 +50,14 @@ public class ResultType extends WASMOpCode {
public void setVector(CustomVector<ValType> vector) {
this.vector = vector;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ResultType)) {
return false;
}
ResultType other = (ResultType) obj;
return vector.getElements().equals(other.vector.getElements());
}
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.types;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
@ -12,12 +13,12 @@ public class TableType extends WASMOpCode {
private ElemType elemType;
private Limits limits;
public TableType(BufferedInputStream in) throws IOException, InvalidOpCodeException {
public TableType(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
elemType = ElemType.from_val(in.read());
if (elemType == null) {
throw new InvalidOpCodeException("No such element type");
}
limits = new Limits(in);
limits = new Limits(in, module);
}
public TableType(ElemType elemType, Limits limits) {

View File

@ -1,32 +0,0 @@
package wasm.disassembly.values;
import wasm.disassembly.WASMOpCode;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class WByte extends WASMOpCode {
private int b;
public WByte(BufferedInputStream inputStream) throws IOException {
b = inputStream.read();
}
public WByte(int b) {
this.b = b;
}
public void assemble(OutputStream out) throws IOException {
out.write(b);
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
}