Skip to content

Commit

Permalink
WebAssembly: create code length data to avoid undefined bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
nneonneo committed Apr 20, 2022
1 parent 7436c51 commit 6e109d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/wasm/WasmLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import ghidra.util.Msg;
import ghidra.util.exception.InvalidInputException;
import ghidra.util.task.TaskMonitor;
import wasm.format.StructureBuilder;
import wasm.format.WasmConstants;
import wasm.format.WasmEnums.WasmExternalKind;
import wasm.format.WasmHeader;
Expand Down Expand Up @@ -339,6 +340,18 @@ private static void createGlobalBlock(Program program, DataType dataType, byte[]
Msg.error(WasmLoader.class, "Failed to create global block " + globalidx + " at " + dataStart, e);
}
}

private static void createCodeLengthData(Program program, MemoryBlock moduleBlock, WasmModule module) {
List<WasmImportEntry> imports = module.getImports(WasmExternalKind.EXT_FUNCTION);
List<WasmCodeEntry> codeEntries = module.getNonImportedFunctions();
for (int i = 0; i < codeEntries.size(); i++) {
WasmCodeEntry entry = codeEntries.get(i);
StructureBuilder builder = new StructureBuilder("code_" + (i + imports.size()));
builder.add(entry.getCodeSizeLeb128(), "code_size");
long offset = entry.getOffset() - entry.getCodeSizeLeb128().getLength();
createData(program, program.getListing(), moduleBlock.getStart().add(offset), builder.toStructure());
}
}
// #endregion

public void createImportExportSymbols(Program program, WasmModule module, int funcidx, Function function) throws Exception {
Expand Down Expand Up @@ -675,6 +688,8 @@ private void doLoad(ByteProvider provider, Program program, TaskMonitor monitor)
createData(program, program.getListing(), moduleBlock.getStart().add(section.getSectionOffset()), section.toDataType());
}

createCodeLengthData(program, moduleBlock, module);

createCustomSections(program, fileBytes, module, monitor);

loadFunctions(program, fileBytes, module, monitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public WasmCodeEntry(BinaryReader reader) throws IOException {
reader.setPointerIndex(codeOffset + codeSize.asLong());
}

public LEB128 getCodeSizeLeb128() {
return codeSize;
}

public long getCodeSize() {
return codeSize.asLong();
}
Expand Down

0 comments on commit 6e109d3

Please sign in to comment.