Skip to content

Initial nearly-minimal changes to assembler for Pickled Canary #5801

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

Closed
Closed
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
1 change: 1 addition & 0 deletions Ghidra/Framework/SoftwareModeling/certification.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ src/main/antlr/ghidra/sleigh/grammar/SleighCompiler.g||GHIDRA||||END|
src/main/antlr/ghidra/sleigh/grammar/SleighEcho.g||GHIDRA||||END|
src/main/antlr/ghidra/sleigh/grammar/SleighLexer.g||GHIDRA||||END|
src/main/antlr/ghidra/sleigh/grammar/SleighParser.g||GHIDRA||||END|
src/main/java/ghidra/app/plugin/assembler/README.md||GHIDRA||||END|
src/main/java/ghidra/program/database/databaseTables.txt||GHIDRA||||END|
src/main/java/ghidra/program/database/package.html||GHIDRA||||END|
src/main/java/ghidra/program/model/address/package.html||GHIDRA||||END|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

import ghidra.app.plugin.assembler.sleigh.parse.AssemblyParseResult;
import ghidra.app.plugin.assembler.sleigh.sem.*;
import ghidra.app.plugin.assembler.sleigh.WildcardedInstruction;

import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressOverflowException;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.listing.InstructionIterator;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.util.task.TaskMonitor;

/**
* The primary interface for performing assembly in Ghidra.
Expand Down Expand Up @@ -110,6 +113,8 @@ public byte[] assembleLine(Address at, String line, AssemblyPatternBlock ctx)
* @return the results of parsing
*/
public Collection<AssemblyParseResult> parseLine(String line);

public Collection<AssemblyParseResult> parseLine(WildcardedInstruction node, TaskMonitor monitor);

/**
* Resolve a given parse tree at the given address, assuming the given context
Expand Down Expand Up @@ -163,6 +168,9 @@ public AssemblyResolutionResults resolveTree(AssemblyParseResult parse, Address
*/
public AssemblyResolutionResults resolveLine(Address at, String line)
throws AssemblySyntaxException;

public AssemblyResolutionResults resolveLine(Address at, WildcardedInstruction line,
AssemblyPatternBlock ctx, TaskMonitor monitor) throws AssemblySyntaxException;

/**
* Assemble a line instruction at the given address, assuming the given context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public byte[] assembleLine(Address at, String line)
public Collection<AssemblyParseResult> parseLine(String line) {
return parser.parse(line, getNumericSymbols());
}

@Override
public Collection<AssemblyParseResult> parseLine(WildcardedInstruction line, TaskMonitor monitor) {
return parser.parse(line, getNumericSymbols(), monitor);
}


@Override
public AssemblyResolutionResults resolveTree(AssemblyParseResult parse, Address at) {
Expand Down Expand Up @@ -236,6 +242,38 @@ public AssemblyResolutionResults resolveLine(Address at, String line, AssemblyPa
}
return results;
}

@Override
public AssemblyResolutionResults resolveLine(Address at, WildcardedInstruction line,
AssemblyPatternBlock ctx, TaskMonitor monitor) throws AssemblySyntaxException {

if (!ctx.isFullMask()) {
throw new AssemblyError(
"Context must be fully-specified (full length, no shift, no unknowns)");
}
if (lang.getContextBaseRegister() != null &&
ctx.length() < lang.getContextBaseRegister().getMinimumByteSize()) {
throw new AssemblyError(
"Context must be fully-specified (full length, no shift, no unknowns)");
}
Collection<AssemblyParseResult> parse = parseLine(line, monitor);
if (monitor.isCancelled()) {
return new AssemblyResolutionResults();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally, we'd throw a CancelledException here. Is your purpose to keep the partial results up to the point of cancellation, or was it because the method is not declared throws CancelledException?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i.e., we usually call monitor.checkCancelled())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember our exact reasoning here. At one point it was handy to keep the partial results because of how slow it was, but now I don't think that's as big a deal (it's faster now due to other changes). It seems we'd have to add a throws CancelledException if we want to go with that (cleaner) style check. I don't have a big preference either way.

}
parse = selector.filterParse(parse);
if (!parse.iterator().hasNext()) { // Iterator.isEmpty()???
throw new AssemblySelectionError(
"Must select at least one parse result. Report errors via AssemblySyntaxError");
}
AssemblyResolutionResults results = new AssemblyResolutionResults();
for (AssemblyParseResult p : parse) {
if (monitor.isCancelled()) {
return new AssemblyResolutionResults();
}
results.absorb(resolveTree(p, at, ctx));
}
return results;
}

@Override
public byte[] assembleLine(Address at, String line, AssemblyPatternBlock ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* ###
* IP: GHIDRA
**/
// Copyright (C) 2023 The MITRE Corporation All Rights Reserved

package ghidra.app.plugin.assembler.sleigh;

import java.util.Optional;

import ghidra.program.model.address.Address;

public interface WildcardMetadata {
public Address getAnticipatedAddress();
public int getInstanceId();
public String getName();
public Optional<String> getInitiator();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* ###
* IP: GHIDRA
**/
// Copyright (C) 2023 The MITRE Corporation All Rights Reserved

package ghidra.app.plugin.assembler.sleigh;

import java.util.Map;


public interface WildcardedInstruction {
public Map<Integer, WildcardMetadata> getWildcardMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ghidra.app.plugin.assembler.sleigh.sem.AssemblyResolution;
import ghidra.app.plugin.assembler.sleigh.sem.AssemblyResolvedPatterns;

import ghidra.app.plugin.processors.sleigh.expression.MultExpression;
import ghidra.app.plugin.processors.sleigh.expression.PatternExpression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
*/
public class OrExpressionSolver extends AbstractBinaryExpressionSolver<OrExpression> {
protected static class Matchers implements ExpressionMatcher.Context {
protected ExpressionMatcher<ConstantValue> val = var(ConstantValue.class);
protected ExpressionMatcher<ConstantValue> size = var(ConstantValue.class);
protected ExpressionMatcher<PatternValue> fld = fldSz(size);
protected final ExpressionMatcher<ConstantValue> val = var(ConstantValue.class);
protected final ExpressionMatcher<ConstantValue> size = var(ConstantValue.class);
protected final ExpressionMatcher<PatternValue> fld = fldSz(size);

protected ExpressionMatcher<?> neqConst = or(
protected final ExpressionMatcher<?> neqConst = or(
and(shr(sub(opnd(fld), val), size), cv(1)),
and(shr(sub(val, opnd(fld)), size), cv(1)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.*;

import generic.util.DequePush;

import ghidra.app.plugin.assembler.sleigh.grammars.AssemblyProduction;
import ghidra.app.plugin.assembler.sleigh.grammars.AssemblySentential;
import ghidra.app.plugin.assembler.sleigh.grammars.AssemblySentential.TruncatedWhiteSpaceParseToken;
Expand Down
Loading