add CustomSection: NameSection + small changes

This commit is contained in:
sirjonasxx 2020-12-21 04:49:10 +01:00
parent db981325c7
commit 272ec443e4
24 changed files with 459 additions and 39 deletions

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
@ -13,19 +14,22 @@ public abstract class Section extends WASMOpCode {
private int sectionId;
private long size;
protected Module module;
public Section(BufferedInputStream in, int sectionId) throws IOException, InvalidOpCodeException {
public Section(BufferedInputStream in, Module module, int sectionId) throws IOException, InvalidOpCodeException {
this.module = module;
this.sectionId = sectionId;
size = WUnsignedInt.read(in, 32);
}
public Section(int sectionId, long size) {
public Section(Module module, int sectionId, long size) {
this.module = module;
this.sectionId = sectionId;
this.size = size;
}
public Section(int sectionId) {
this(sectionId, -1);
public Section(Module module, int sectionId) {
this(module, sectionId, -1);
}
@Override

View File

@ -18,12 +18,12 @@ public class CodeSection extends Section {
private Vector<Code> codesEntries;
public CodeSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, CODE_SECTION_ID);
super(in, module, CODE_SECTION_ID);
codesEntries = new Vector<>(in, Code::new, module);
}
public CodeSection(List<Code> codesEntries) {
super(CODE_SECTION_ID);
public CodeSection(Module module, List<Code> codesEntries) {
super(module, CODE_SECTION_ID);
this.codesEntries = new Vector<>(codesEntries);
}

View File

@ -16,7 +16,7 @@ public abstract class CustomSection extends Section {
private String name;
public CustomSection(Module module, long size, String name) throws IOException, InvalidOpCodeException {
super(CUSTOM_SECTION_ID, size);
super(module, CUSTOM_SECTION_ID, size);
this.name = name;
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.custom;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.custom.namesection.NameSection;
import wasm.disassembly.values.WName;
import wasm.disassembly.values.WUnsignedInt;
@ -14,7 +15,9 @@ public class CustomSectionFactory {
long size = WUnsignedInt.read(in, 32);
String name = WName.read(in);
// select implementation
if (name.equals("name")) {
return new NameSection(in, module, size);
}
return new UnImplementedCustomSection(in, module, size, name);
}

View File

@ -2,6 +2,7 @@ package wasm.disassembly.modules.sections.custom;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.custom.CustomSection;
import wasm.disassembly.values.WName;
import java.io.BufferedInputStream;

View File

@ -0,0 +1,74 @@
package wasm.disassembly.modules.sections.custom.namesection;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.sections.custom.CustomSection;
import wasm.disassembly.modules.sections.custom.namesection.subsections.FunctionNamesSubSection;
import wasm.disassembly.modules.sections.custom.namesection.subsections.LocalNamesSubSection;
import wasm.disassembly.modules.sections.custom.namesection.subsections.ModuleNameSubSection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class NameSection extends CustomSection {
private ModuleNameSubSection moduleName;
private FunctionNamesSubSection functionNames;
private LocalNamesSubSection localNames;
public NameSection(BufferedInputStream in, Module module, long size) throws IOException, InvalidOpCodeException {
super(module, size, "name");
moduleName = isNextSection(in, 0) ? new ModuleNameSubSection(in, module) : null;
functionNames = isNextSection(in, 1) ? new FunctionNamesSubSection(in, module) : null;
localNames = isNextSection(in, 2) ? new LocalNamesSubSection(in, module) : null;
}
public NameSection(Module module, long size, String name, ModuleNameSubSection moduleName, FunctionNamesSubSection functionNames, LocalNamesSubSection localNames) throws IOException, InvalidOpCodeException {
super(module, size, name);
this.moduleName = moduleName;
this.functionNames = functionNames;
this.localNames = localNames;
}
public ModuleNameSubSection getModuleName() {
return moduleName;
}
public void setModuleName(ModuleNameSubSection moduleName) {
this.moduleName = moduleName;
}
public FunctionNamesSubSection getFunctionNames() {
return functionNames;
}
public void setFunctionNames(FunctionNamesSubSection functionNames) {
this.functionNames = functionNames;
}
public LocalNamesSubSection getLocalNames() {
return localNames;
}
public void setLocalNames(LocalNamesSubSection localNames) {
this.localNames = localNames;
}
@Override
protected void assemble3(OutputStream out) throws IOException, InvalidOpCodeException {
if (moduleName != null) moduleName.assemble(out);
if (functionNames != null) functionNames.assemble(out);
if (localNames != null) localNames.assemble(out);
}
private boolean isNextSection(BufferedInputStream in, int id) throws IOException {
in.mark(1);
if (in.read() == id) {
return true;
}
in.reset();
return false;
}
}

View File

@ -0,0 +1,40 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Creator;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.sections.custom.namesection.subsections.namemaps.NameMap;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FunctionNamesSubSection extends SubSection {
public static final int FUNCTION_NAMES_SUBSECTION_ID = 1;
private NameMap<FuncIdx> nameMap;
public FunctionNamesSubSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, FUNCTION_NAMES_SUBSECTION_ID);
nameMap = new NameMap<>(in, module, FuncIdx::new);
}
public FunctionNamesSubSection(NameMap<FuncIdx> nameMap) {
super(FUNCTION_NAMES_SUBSECTION_ID);
this.nameMap = nameMap;
}
public NameMap<FuncIdx> getNameMap() {
return nameMap;
}
public void setNameMap(NameMap<FuncIdx> nameMap) {
this.nameMap = nameMap;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
nameMap.assemble(out);
}
}

View File

@ -0,0 +1,42 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.conventions.Creator;
import wasm.disassembly.modules.Module;
import wasm.disassembly.modules.indices.FuncIdx;
import wasm.disassembly.modules.indices.LocalIdx;
import wasm.disassembly.modules.sections.custom.namesection.subsections.namemaps.IndirectNameMap;
import wasm.disassembly.modules.sections.custom.namesection.subsections.namemaps.NameMap;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class LocalNamesSubSection extends SubSection {
public static final int LOCAL_NAMES_SUBSECTION_ID = 2;
private IndirectNameMap<FuncIdx, LocalIdx> indirectNameMap;
public LocalNamesSubSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, LOCAL_NAMES_SUBSECTION_ID);
indirectNameMap = new IndirectNameMap<>(in, module, FuncIdx::new, LocalIdx::new);
}
public LocalNamesSubSection(IndirectNameMap<FuncIdx, LocalIdx> indirectNameMap) {
super(LOCAL_NAMES_SUBSECTION_ID);
this.indirectNameMap = indirectNameMap;
}
public IndirectNameMap<FuncIdx, LocalIdx> getIndirectNameMap() {
return indirectNameMap;
}
public void setIndirectNameMap(IndirectNameMap<FuncIdx, LocalIdx> indirectNameMap) {
this.indirectNameMap = indirectNameMap;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
indirectNameMap.assemble(out);
}
}

View File

@ -0,0 +1,39 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WName;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ModuleNameSubSection extends SubSection {
public static final int MODULE_NAME_SUBSECTION_ID = 0;
private String name;
public ModuleNameSubSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, MODULE_NAME_SUBSECTION_ID);
name = WName.read(in);
}
public ModuleNameSubSection(String name) {
super(MODULE_NAME_SUBSECTION_ID);
this.name = name;
}
@Override
protected void assemble2(OutputStream out) throws IOException, InvalidOpCodeException {
WName.write(name, out);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,44 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.values.WName;
import wasm.disassembly.values.WUnsignedInt;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public abstract class SubSection extends WASMOpCode {
private long size;
private int subSectionId;
public SubSection(BufferedInputStream in, int subSectionId) throws IOException, InvalidOpCodeException {
this.subSectionId = subSectionId;
size = WUnsignedInt.read(in, 32);
}
public SubSection(int subSectionId) {
this.subSectionId = subSectionId;
size = -1;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
out.write(subSectionId);
ByteArrayOutputStream fakeOutputStream = new ByteArrayOutputStream();
assemble2(fakeOutputStream);
byte[] asbytes = fakeOutputStream.toByteArray();
WUnsignedInt.write(asbytes.length, out, 32);
out.write(asbytes);
}
protected abstract void assemble2(OutputStream out) throws IOException, InvalidOpCodeException;
public int getSubSectionId() {
return subSectionId;
}
}

View File

@ -0,0 +1,48 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections.namemaps;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.conventions.Creator;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class IndirectNameAssoc<Idx extends WASMOpCode, InnerIdx extends WASMOpCode> extends WASMOpCode {
private Idx idx;
private NameMap<InnerIdx> nameMap;
public IndirectNameAssoc(BufferedInputStream in, Module module, Creator<Idx> creator, Creator<InnerIdx> innerCreator) throws IOException, InvalidOpCodeException {
idx = creator.create(in, module);
nameMap = new NameMap<>(in, module, innerCreator);
}
public IndirectNameAssoc(Idx idx, NameMap<InnerIdx> nameMap) {
this.idx = idx;
this.nameMap = nameMap;
}
public Idx getIdx() {
return idx;
}
public void setIdx(Idx idx) {
this.idx = idx;
}
public NameMap<InnerIdx> getNameMap() {
return nameMap;
}
public void setNameMap(NameMap<InnerIdx> nameMap) {
this.nameMap = nameMap;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
idx.assemble(out);
nameMap.assemble(out);
}
}

View File

@ -0,0 +1,38 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections.namemaps;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.conventions.Creator;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class IndirectNameMap<Idx extends WASMOpCode, InnerIdx extends WASMOpCode> extends WASMOpCode {
private Vector<IndirectNameAssoc<Idx, InnerIdx>> idxAndMaps;
public IndirectNameMap(BufferedInputStream in, Module module, Creator<Idx> creator, Creator<InnerIdx> innerCreator) throws IOException, InvalidOpCodeException {
idxAndMaps = new Vector<>(in, (in1, m) -> new IndirectNameAssoc<>(in1, m, creator, innerCreator), module);
}
public IndirectNameMap(Vector<IndirectNameAssoc<Idx, InnerIdx>> idxAndMaps) {
this.idxAndMaps = idxAndMaps;
}
public List<IndirectNameAssoc<Idx, InnerIdx>> getIdxAndMaps() {
return idxAndMaps.getElements();
}
public void setIdxAndMaps(List<IndirectNameAssoc<Idx, InnerIdx>> idxAndMaps) {
this.idxAndMaps = new Vector<>(idxAndMaps);
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
idxAndMaps.assemble(out);
}
}

View File

@ -0,0 +1,49 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections.namemaps;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.conventions.Creator;
import wasm.disassembly.modules.Module;
import wasm.disassembly.values.WName;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class NameAssoc<Idx extends WASMOpCode> extends WASMOpCode {
private Idx idx;
private String name;
public NameAssoc(BufferedInputStream in, Module module, Creator<Idx> creator) throws IOException, InvalidOpCodeException {
idx = creator.create(in, module);
name = WName.read(in);
}
public NameAssoc(Idx idx, String name) {
this.idx = idx;
this.name = name;
}
public Idx getIdx() {
return idx;
}
public void setIdx(Idx idx) {
this.idx = idx;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
idx.assemble(out);
WName.write(name, out);
}
}

View File

@ -0,0 +1,38 @@
package wasm.disassembly.modules.sections.custom.namesection.subsections.namemaps;
import wasm.disassembly.InvalidOpCodeException;
import wasm.disassembly.WASMOpCode;
import wasm.disassembly.conventions.Creator;
import wasm.disassembly.conventions.Vector;
import wasm.disassembly.modules.Module;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class NameMap<Idx extends WASMOpCode> extends WASMOpCode {
private Vector<NameAssoc<Idx>> idxAndNames;
public NameMap(BufferedInputStream in, Module module, Creator<Idx> creator) throws IOException, InvalidOpCodeException {
idxAndNames = new Vector<>(in, (in1, m) -> new NameAssoc<>(in1, m, creator), module);
}
public NameMap(Vector<NameAssoc<Idx>> idxAndNames) {
this.idxAndNames = idxAndNames;
}
public List<NameAssoc<Idx>> getIdxAndNames() {
return idxAndNames.getElements();
}
public void setIdxAndNames(List<NameAssoc<Idx>> idxAndNames) {
this.idxAndNames = new Vector<>(idxAndNames);
}
@Override
public void assemble(OutputStream out) throws IOException, InvalidOpCodeException {
idxAndNames.assemble(out);
}
}

View File

@ -18,12 +18,12 @@ public class DataSection extends Section {
private Vector<Data> dataSegments;
public DataSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, DATA_SECTION_ID);
super(in, module, DATA_SECTION_ID);
dataSegments = new Vector<>(in, Data::new, module);
}
public DataSection(List<Data> dataSegments) {
super(DATA_SECTION_ID);
public DataSection(Module module, List<Data> dataSegments) {
super(module, DATA_SECTION_ID);
this.dataSegments = new Vector<>(dataSegments);
}

View File

@ -17,12 +17,12 @@ public class ElementSection extends Section {
private Vector<Elem> elementSegments;
public ElementSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, ELEMENT_SECTION_ID);
super(in, module, ELEMENT_SECTION_ID);
elementSegments = new Vector<>(in, Elem::new, module);
}
public ElementSection(List<Elem> elementSegments) {
super(ELEMENT_SECTION_ID);
public ElementSection(Module module, List<Elem> elementSegments) {
super(module, ELEMENT_SECTION_ID);
this.elementSegments = new Vector<>(elementSegments);
}

View File

@ -17,12 +17,12 @@ public class ExportSection extends Section {
private Vector<Export> exports;
public ExportSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, EXPORT_SECTION_ID);
super(in, module, EXPORT_SECTION_ID);
exports = new Vector<>(in, Export::new, module);
}
public ExportSection(List<Export> exports) {
super(EXPORT_SECTION_ID);
public ExportSection(Module module, List<Export> exports) {
super(module, EXPORT_SECTION_ID);
this.exports = new Vector<>(exports);
}

View File

@ -19,12 +19,12 @@ public class FunctionSection extends Section {
public FunctionSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, FUNCTION_SECTION_ID);
super(in, module, FUNCTION_SECTION_ID);
typeIdxVector = new Vector<>(in, TypeIdx::new, module);
}
public FunctionSection(List<TypeIdx> typeIdxList) {
super(FUNCTION_SECTION_ID);
public FunctionSection(Module module, List<TypeIdx> typeIdxList) {
super(module, FUNCTION_SECTION_ID);
this.typeIdxVector = new Vector<>(typeIdxList);
}

View File

@ -18,12 +18,12 @@ public class GlobalSection extends Section {
public GlobalSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, GLOBAL_SECTION_ID);
super(in, module, GLOBAL_SECTION_ID);
globals = new Vector<>(in, Global::new, module);
}
public GlobalSection(List<Global> globals) {
super(GLOBAL_SECTION_ID);
public GlobalSection(Module module, List<Global> globals) {
super(module, GLOBAL_SECTION_ID);
this.globals = new Vector<>(globals);
}

View File

@ -24,7 +24,7 @@ public class ImportSection extends Section {
public ImportSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, IMPORT_SECTION_ID);
super(in, module, IMPORT_SECTION_ID);
this.module = module;
imports = new Vector<>(in, Import::new, module);
@ -36,8 +36,8 @@ public class ImportSection extends Section {
}
}
public ImportSection(List<Import> imports) {
super(IMPORT_SECTION_ID);
public ImportSection(Module module, List<Import> imports) {
super(module, IMPORT_SECTION_ID);
this.imports = new Vector<>(imports);
}

View File

@ -18,12 +18,12 @@ public class MemorySection extends Section {
public MemorySection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, MEMORY_SECTION_ID);
super(in, module, MEMORY_SECTION_ID);
memories = new Vector<>(in, Mem::new, module);
}
public MemorySection(List<Mem> memories) {
super(MEMORY_SECTION_ID);
public MemorySection(Module module, List<Mem> memories) {
super(module, MEMORY_SECTION_ID);
this.memories = new Vector<>(memories);
}

View File

@ -15,12 +15,12 @@ public class StartSection extends Section {
private Start start;
public StartSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, START_SECTION_ID);
super(in, module, START_SECTION_ID);
start = new Start(in, module);
}
public StartSection(Start start) {
super(START_SECTION_ID);
public StartSection(Module module, Start start) {
super(module, START_SECTION_ID);
this.start = start;
}

View File

@ -18,12 +18,12 @@ public class TableSection extends Section {
public TableSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, TABLE_SECTION_ID);
super(in, module, TABLE_SECTION_ID);
tables = new Vector<>(in, Table::new, module);
}
public TableSection(List<Table> tables) {
super(TABLE_SECTION_ID);
public TableSection(Module module, List<Table> tables) {
super(module, TABLE_SECTION_ID);
this.tables = new Vector<>(tables);
}

View File

@ -20,12 +20,12 @@ public class TypeSection extends Section {
public TypeSection(BufferedInputStream in, Module module) throws IOException, InvalidOpCodeException {
super(in, TYPE_SECTION_ID);
super(in, module, TYPE_SECTION_ID);
functionTypes = new Vector<>(in, FuncType::new, module);
}
public TypeSection(List<FuncType> functionTypes) {
super(TYPE_SECTION_ID);
public TypeSection(Module module, List<FuncType> functionTypes) {
super(module, TYPE_SECTION_ID);
this.functionTypes = new Vector<>(functionTypes);
}