Skip to content

Commit

Permalink
Sync with Ghidra PR #4103 - 10.3 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
nneonneo committed May 23, 2023
1 parent da2aa20 commit 017bbb1
Show file tree
Hide file tree
Showing 35 changed files with 184 additions and 227 deletions.
58 changes: 0 additions & 58 deletions src/main/help/help/shared/Frontpage.css

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/help/help/topics/WebAssembly/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta charset="utf-8" />

<title>WebAssembly</title>
<link rel="stylesheet" type="text/css" href="../../shared/Frontpage.css">
<link rel="stylesheet" type="text/css" href="help/shared/DefaultStyle.css">
</head>

<body><h1>WebAssembly</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.bin.ByteProviderWrapper;
import ghidra.app.util.bin.MemBufferByteProvider;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.pcode.emulate.Emulate;
import ghidra.pcode.emulate.EmulateInstructionStateModifier;
import ghidra.pcode.emulate.EmulateMemoryStateBuffer;
Expand All @@ -33,6 +32,7 @@
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressFactory;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.data.LEB128;
import ghidra.program.model.lang.Register;
import ghidra.program.model.lang.RegisterValue;
import ghidra.program.model.pcode.PcodeOp;
Expand Down Expand Up @@ -69,12 +69,12 @@ public WasmFunctionAnalysisProvider(AddressFactory addressFactory, MemoryState m
while (true) {
moduleSize = reader.getPointerIndex();
int id = reader.readNextUnsignedByte();
LEB128 contentLength = LEB128.readUnsignedValue(reader);
reader.setPointerIndex(reader.getPointerIndex() + contentLength.asLong());
long contentLength = reader.readNext(LEB128::unsigned);
reader.setPointerIndex(reader.getPointerIndex() + contentLength);
// A custom section (id 0) must have a name, so a zero-length custom section
// is invalid. This happens if we have two consecutive null bytes, which
// suggests that we're at the end of the module.
if (id == 0 && contentLength.asLong() == 0) {
if (id == 0 && contentLength == 0) {
break;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/wasm/WasmLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private static void createCodeLengthData(Program program, MemoryBlock moduleBloc
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");
builder.addUnsignedLeb128(entry.getCodeSizeLeb128(), "code_size");
long offset = entry.getOffset() - entry.getCodeSizeLeb128().getLength();
createData(program, program.getListing(), moduleBlock.getStart().add(offset), builder.toStructure());
}
Expand Down Expand Up @@ -435,12 +435,12 @@ private void loadTables(Program program, FileBytes fileBytes, WasmModule module,

/**
* Copy element segment to table.
*
*
* This is public so that it can be called after loading, e.g. to load a passive
* element segment once the dynamic table index and offset are known.
*
* For example, this could be called from a script as follows:
*
*
* WasmLoader.loadElementsToTable(getCurrentProgram(),
* WasmAnalysis.getState(getCurrentProgram()).getModule(), elemidx, tableidx,
* offset, new ConsoleTaskMonitor())
Expand Down Expand Up @@ -520,12 +520,12 @@ private void loadMemories(Program program, FileBytes fileBytes, WasmModule modul

/**
* Copy data segment to memory.
*
*
* This is public so that it can be called after loading, e.g. to load a passive
* data segment once the dynamic memory index and offset are known.
*
* For example, this could be called from a script as follows:
*
*
* WasmLoader.loadDataToMemory(getCurrentProgram(),
* WasmAnalysis.getState(getCurrentProgram()).getModule(), dataidx, memidx,
* offset, new ConsoleTaskMonitor())
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/wasm/analysis/WasmDWARFAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/wasm/analysis/WasmFunctionAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.util.Map;

import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.LEB128;
import ghidra.program.model.lang.Register;
import ghidra.program.model.lang.RegisterValue;
import ghidra.program.model.listing.ContextChangeException;
Expand Down Expand Up @@ -352,11 +352,11 @@ public StackEffect getStackEffect(Address address) {

// #region BinaryReader utilities
private static long readLeb128(BinaryReader reader) throws IOException {
return LEB128.readUnsignedValue(reader).asLong();
return reader.readNext(LEB128::unsigned);
}

private static long readSignedLeb128(BinaryReader reader) throws IOException {
return LEB128.readSignedValue(reader).asLong();
return reader.readNext(LEB128::signed);
}
// #endregion

Expand Down Expand Up @@ -957,7 +957,7 @@ private void analyzeOpcode(WasmAnalysis analysis, Address instAddress, BinaryRea
break;
}
case 0xFC: {
int opcode2 = LEB128.readAsUInt32(reader);
int opcode2 = reader.readNextUnsignedVarIntExact(LEB128::unsigned);
switch (opcode2) {
case 0x00: /* i32.trunc_sat_f32_s */
case 0x01: /* i32.trunc_sat_f32_u */
Expand Down Expand Up @@ -1047,7 +1047,7 @@ private void analyzeOpcode(WasmAnalysis analysis, Address instAddress, BinaryRea
break;
}
case 0xFD: {
int opcode2 = LEB128.readAsUInt32(reader);
int opcode2 = reader.readNextUnsignedVarIntExact(LEB128::unsigned);
switch (opcode2) {
case 0x0: /* v128.load */
case 0x1: /* v128.load8x8_s */
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/wasm/analysis/WasmPreAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import ghidra.app.services.AnalyzerType;
import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.MemoryByteProvider;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.app.util.importer.MessageLog;
import ghidra.framework.options.Options;
import ghidra.program.disassemble.Disassembler;
import ghidra.program.disassemble.DisassemblerMessageListener;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.LEB128;
import ghidra.program.model.lang.Processor;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Program;
Expand Down Expand Up @@ -80,10 +80,10 @@ public void optionsChanged(Options options, Program program) {

private int guessCStackGlobalForFunction(Program program, Address funcAddress) throws IOException {
BinaryReader codeReader = new BinaryReader(new MemoryByteProvider(program.getMemory(), funcAddress), true);
int localsCount = LEB128.readAsInt32(codeReader);
int localsCount = codeReader.readNextVarInt(LEB128::signed);
for (int i = 0; i < localsCount; i++) {
LEB128.readAsInt32(codeReader); /* count */
LEB128.readAsInt32(codeReader); /* type */
codeReader.readNextVarInt(LEB128::signed); /* count */
codeReader.readNextVarInt(LEB128::signed); /* type */
}

/*
Expand All @@ -92,7 +92,7 @@ private int guessCStackGlobalForFunction(Program program, Address funcAddress) t
*/
if (codeReader.readNextUnsignedByte() != 0x23)
return -1;
return LEB128.readAsInt32(codeReader);
return codeReader.readNextVarInt(LEB128::signed);
}

private int guessCStackGlobal(Program program, List<WasmFuncSignature> functions, TaskMonitor monitor) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/wasm/analysis/WasmStackVariableAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wasm.analysis;

import java.util.ArrayList;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/wasm/format/StructureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import java.io.IOException;

import ghidra.app.util.bin.LEB128Info;
import ghidra.app.util.bin.StructConverter;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.CategoryPath;
import ghidra.program.model.data.DataType;
Expand Down Expand Up @@ -99,12 +99,12 @@ public void add(DataType dataType, String name) {
add(dataType, name, null);
}

public void add(LEB128 leb128, String name, String comment) {
addArray(StructConverter.BYTE, leb128.getLength(), name, comment);
public void addUnsignedLeb128(LEB128Info leb128, String name, String comment) {
add(StructConverter.ULEB128, leb128.getLength(), name, comment);
}

public void add(LEB128 leb128, String name) {
add(leb128, name, null);
public void addUnsignedLeb128(LEB128Info leb128, String name) {
addUnsignedLeb128(leb128, name, null);
}

public void add(StructConverter converter, String name, String comment) throws DuplicateNameException, IOException {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/wasm/format/sections/WasmCodeSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
import java.util.List;

import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.app.util.bin.LEB128Info;
import ghidra.util.exception.DuplicateNameException;
import wasm.format.StructureBuilder;
import wasm.format.sections.structures.WasmCodeEntry;

public class WasmCodeSection extends WasmSection {

private LEB128 count;
private LEB128Info count;
private List<WasmCodeEntry> functions = new ArrayList<WasmCodeEntry>();

public WasmCodeSection(BinaryReader reader) throws IOException {
super(reader);
count = LEB128.readUnsignedValue(reader);
count = reader.readNext(LEB128Info::unsigned);
for (int i = 0; i < count.asLong(); ++i) {
functions.add(new WasmCodeEntry(reader));
}
Expand All @@ -45,7 +45,7 @@ public List<WasmCodeEntry> getFunctions() {

@Override
protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException {
builder.add(count, "count");
builder.addUnsignedLeb128(count, "count");
// Do not add individual code entries to the structure: they need to be parsed
// as code instead of data.
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/wasm/format/sections/WasmCustomSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.io.IOException;

import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.app.util.bin.LEB128Info;
import ghidra.util.exception.DuplicateNameException;
import wasm.format.StructureBuilder;
import wasm.format.sections.structures.WasmName;
Expand All @@ -37,7 +37,7 @@ public static WasmCustomSection create(BinaryReader reader) throws IOException {
long initialOffset = reader.getPointerIndex();
/* skip section header: id + contentLength */
reader.readNextUnsignedByte();
LEB128.readUnsignedValue(reader);
reader.readNext(LEB128Info::unsigned);

String name = new WasmName(reader).getValue();
reader.setPointerIndex(initialOffset);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/wasm/format/sections/WasmDataSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
import java.util.List;

import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.app.util.bin.LEB128Info;
import ghidra.util.exception.DuplicateNameException;
import wasm.format.StructureBuilder;
import wasm.format.sections.structures.WasmDataSegment;

public class WasmDataSection extends WasmSection {

private LEB128 count;
private LEB128Info count;
private List<WasmDataSegment> dataSegments = new ArrayList<WasmDataSegment>();

public WasmDataSection(BinaryReader reader) throws IOException {
super(reader);
count = LEB128.readUnsignedValue(reader);
count = reader.readNext(LEB128Info::unsigned);
for (int i = 0; i < count.asLong(); ++i) {
dataSegments.add(new WasmDataSegment(reader));
}
Expand All @@ -45,7 +45,7 @@ public List<WasmDataSegment> getSegments() {

@Override
protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException {
builder.add(count, "count");
builder.addUnsignedLeb128(count, "count");
for (int i = 0; i < dataSegments.size(); i++) {
builder.add(dataSegments.get(i), "segment_" + i);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/wasm/format/sections/WasmElementSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
import java.util.List;

import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.format.dwarf4.LEB128;
import ghidra.app.util.bin.LEB128Info;
import ghidra.util.exception.DuplicateNameException;
import wasm.format.StructureBuilder;
import wasm.format.sections.structures.WasmElementSegment;

public class WasmElementSection extends WasmSection {

private LEB128 count;
private LEB128Info count;
private List<WasmElementSegment> elements = new ArrayList<WasmElementSegment>();

public WasmElementSection(BinaryReader reader) throws IOException {
super(reader);
count = LEB128.readUnsignedValue(reader);
count = reader.readNext(LEB128Info::unsigned);
for (int i = 0; i < count.asLong(); ++i) {
elements.add(new WasmElementSegment(reader));
}
Expand All @@ -45,7 +45,7 @@ public List<WasmElementSegment> getSegments() {

@Override
protected void addToStructure(StructureBuilder builder) throws DuplicateNameException, IOException {
builder.add(count, "count");
builder.addUnsignedLeb128(count, "count");
for (int i = 0; i < elements.size(); i++) {
builder.add(elements.get(i), "element_" + i);
}
Expand Down
Loading

0 comments on commit 017bbb1

Please sign in to comment.