Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Leb128 parsing of WasmLoader. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/wasm/analysis/MetaInstruction.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package wasm.analysis;

import java.io.IOException;
import java.util.ArrayList;

import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.program.model.address.Address;
import ghidra.program.model.lang.InjectContext;
import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.program.model.pcode.Varnode;
import wasm.format.Leb128;
import wasm.format.WasmFuncSignature;
import wasm.format.sections.structures.WasmFuncType;
import wasm.pcodeInject.PcodeHelper;
Expand Down Expand Up @@ -113,7 +114,11 @@ public static MetaInstruction create(Type ty, InjectContext con, Program p) {
public static long getLeb128Operand(Program p, Address brAddress) throws MemoryAccessException {
byte[] buf = new byte[16];
p.getMemory().getBytes(brAddress.add(1), buf); //add 1 to go past the opcode
return Leb128.readUnsignedLeb128(buf);
try {
return LEB128.decode(buf,false);
} catch (IOException e) {
throw new MemoryAccessException("Error while decoding leb128 :"+e.getMessage());
}
}

public abstract Type getType();
Expand Down
231 changes: 0 additions & 231 deletions src/main/java/wasm/format/Leb128.java

This file was deleted.

15 changes: 7 additions & 8 deletions src/main/java/wasm/format/sections/WasmCodeSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
import java.util.List;

import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.StructConverter;
import ghidra.program.model.data.DataType;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.Structure;
import ghidra.program.model.data.StructureDataType;
import ghidra.util.exception.DuplicateNameException;
import wasm.format.Leb128;
import wasm.format.sections.structures.WasmFunctionBody;
import static ghidra.app.util.bin.StructConverter.BYTE;

public class WasmCodeSection implements WasmPayload {

private Leb128 count;
private LEB128 count;
List<WasmFunctionBody> functions = new ArrayList <WasmFunctionBody>();

public WasmCodeSection (BinaryReader reader) throws IOException {
count = new Leb128(reader);
for (int i =0; i < count.getValue(); ++i) {
count = LEB128.readUnsignedValue(reader);
for (int i =0; i < count.asInt32(); ++i) {
functions.add(new WasmFunctionBody(reader));
}
}
Expand All @@ -31,7 +30,7 @@ public List<WasmFunctionBody> getFunctions() {

@Override
public void addToStructure(Structure structure) throws IllegalArgumentException, DuplicateNameException, IOException {
structure.add(count.toDataType(), count.toDataType().getLength(), "count", null);
structure.add( new ArrayDataType( BYTE, count.getLength(), BYTE.getLength( ) ), "count", null );
int function_id = 0;
for (WasmFunctionBody function: functions) {
structure.add(function.toDataType(), function.toDataType().getLength(), "function_" + function_id, null);
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/wasm/format/sections/WasmCustomSection.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package wasm.format.sections;

import static ghidra.app.util.bin.StructConverter.BYTE;

import java.io.IOException;

import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.StructConverter;
import ghidra.program.model.data.DataType;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.Structure;
import ghidra.program.model.data.StructureDataType;
import ghidra.util.exception.DuplicateNameException;
import wasm.format.Leb128;

public class WasmCustomSection implements WasmPayload {
Leb128 namelen;
LEB128 namelen;
String name;
byte[] contents;

protected WasmCustomSection (Leb128 namelen, String name, BinaryReader reader, int contentlen) throws IOException {
protected WasmCustomSection (LEB128 namelen, String name, BinaryReader reader, int contentlen) throws IOException {
this.namelen = namelen;
this.name = name;
contents = reader.readNextByteArray(contentlen);
Expand All @@ -24,8 +25,8 @@ protected WasmCustomSection (Leb128 namelen, String name, BinaryReader reader, i
public static WasmCustomSection create(BinaryReader reader, long len) throws IOException {
long readUntil = reader.getPointerIndex() + len;

Leb128 namelen = new Leb128(reader);
String name = new String(reader.readNextByteArray((int)namelen.getValue()));
LEB128 namelen = LEB128.readUnsignedValue(reader);
String name = new String(reader.readNextByteArray((int)namelen.asInt32()));

int contentlen = (int)(readUntil - reader.getPointerIndex());

Expand All @@ -38,7 +39,7 @@ public static WasmCustomSection create(BinaryReader reader, long len) throws IOE

@Override
public void addToStructure(Structure structure) throws IllegalArgumentException, DuplicateNameException, IOException {
structure.add(namelen.toDataType(), "name_len", null);
structure.add( new ArrayDataType( BYTE, namelen.getLength(), BYTE.getLength( ) ), "name_len", null );
structure.add(StructConverter.STRING, name.length(), "name", null);
structure.add(StructConverter.STRING, contents.length, "contents", null);
}
Expand Down
Loading