Skip to content

Commit

Permalink
Initial support for codeFlows
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jul 18, 2024
1 parent 8ec79b1 commit 7850cee
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
50 changes: 46 additions & 4 deletions src/plugin.r2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SarifDocument, Driver, ResultKind, BinaryLocation, ResultLevel, Rule, Result, isValidLevel, isValidKind } from "./sarif/types.js";
import { StringMap, SarifDocument, Driver, ResultKind, BinaryLocation, ResultLevel, Rule, Result, isValidLevel, isValidKind } from "./sarif/types.js";
import { SarifGenerator, SarifRun } from "./sarif/generator.js";
import { tabulateText } from "./sarif/utilsgen.js";
import { SarifParser } from "./sarif/parser.js"
Expand Down Expand Up @@ -191,6 +191,7 @@ class R2Sarif {
}
return true;
}

selectDriver(index: number): boolean {
if (index === -1) {
this.currentDriverIndex = -1;
Expand Down Expand Up @@ -249,13 +250,54 @@ class R2Sarif {
r2.log(JSON.stringify(doc));
}
}

listRulesAsMap(): Map<string, string> {
const myMap: Map<string, string> = new Map<string, string>();
const rules = this.listRules(true);
for (const rule of rules) {
// const desc = rule.shortDescription?.text ?? rule.fullDescription?.text ?? "";
const desc = rule.name;
myMap.set(rule.id, desc);
}
return myMap;
}

listResults(): Result[] {
var res: Result[] = [];
const ruleMap = this.listRulesAsMap();
for (const doc of this.docs) {
for (const run of doc.runs) {
if (run.results) {
for (const res of run.results) {
r2.log(res.ruleId)
let resultText = res.ruleId;
const desc = ruleMap.get(res.ruleId);
if (desc !== undefined) {
resultText += " :: " + desc;
}
r2.log(resultText);
if (res.message && res.message.arguments) {
const args = res.message.arguments;
const arg0 = args[0];
args.shift();
r2.log(" :: " + arg0 + " (" + args.join (", ") + ")");
}
if (res.codeFlows !== undefined) {
for (const cf of res.codeFlows) {
for (const tf of cf.threadFlows) {
for (const tfloc of tf.locations) {
let addr = "0x" + tfloc.location.physicalLocation.address?.absoluteAddress.toString(16);
let relAddr = tfloc.location.physicalLocation.region.byteOffset;
let text = " - " + addr + " " + tfloc.module;
if (tfloc.location) {
const phys = tfloc.location.physicalLocation;
text += " " + phys.artifactLocation.uri;
text += " +" + relAddr;
}
r2.log(text);
}
}
}
}
}
}
}
Expand All @@ -282,9 +324,9 @@ class R2Sarif {
}
return driver.rules;
}
listRules(): Rule[] {
listRules(quiet? : boolean): Rule[] {
if (this.currentDriver !== null) {
return this.listRulesForDriver(this.currentDriver);
return this.listRulesForDriver(this.currentDriver, quiet);
}
var res: Rule[] = [];
for (const doc of this.docs) {
Expand Down
12 changes: 12 additions & 0 deletions src/sarif/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// XXX UNUSED CODE
// XXX we can remove this file entirely

import { sarifTemplate } from "./template.js";
import { Rule, SarifDocument, SarifError } from "./types.js";

Expand All @@ -12,6 +15,7 @@ class Sarif {

/* load rules from a sarif document */
loadRules(sarifDocument: any) {
// unused
for (const run of sarifDocument.runs) {
// maybe this should be a warning.. and loadingRules should return a LoadingReport instead
if (!run.tool || !run.tool.driver || !run.tool.driver.rules) {
Expand All @@ -25,6 +29,7 @@ class Sarif {
}

loadResults(sarifDocument: any) : Error | undefined {
// unused
const r2baddr = 0;
for (const run of sarifDocument.runs) {
let baddr = 0;
Expand All @@ -46,6 +51,13 @@ class Sarif {
} else {
message = res.message.text;
}
try {
const flows = res.codeFlows;
console.log(flows);
} catch (e) {
console.error(e);
// ignore
}
const loc0 = res.locations[0];
try {
// console.log(JSON.stringify(loc0, null, 2));
Expand Down
2 changes: 1 addition & 1 deletion src/sarif/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ export class SarifParser {
}
});
}
}
}
33 changes: 33 additions & 0 deletions src/sarif/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

export interface ResultMessage {
text: string;
arguments?: string[];
}

export interface StringMap {
[key: string]: string;
}
export interface Result {
ruleId: string;
level: ResultLevel;
kind?: string;
message: ResultMessage;
locations: Location[]; // (optional) file, line, column information
codeFlows?: CodeFlow[]; // (optional)
relatedLocations?: Location[]; // (optional) additional locations
properties?: any;
}
Expand Down Expand Up @@ -40,12 +46,34 @@ export class SarifError extends Error {
}

export interface BinaryLocation {
message?: string;
physicalLocation: BinaryPhysicalLocation;
properties: BinaryLocationProperties;
}

export type Location = SourceLocation | BinaryLocation | SourceLineLocation;

export interface ThreadFlowMessage {
text: string;
}
export interface ThreadFlowLocation {
module: string;
message: ThreadFlowMessage;
location: BinaryLocation;
// physicalLocation: BinaryPhysicalLocation;
// properties: any; // memoryAddress: string
}

export interface ThreadFlow {
id: string;
message: ThreadFlowMessage;
locations: ThreadFlowLocation[];
}

export interface CodeFlow {
threadFlows: ThreadFlow[]
}

export interface BinaryLocationProperties {
memoryAddress: string;
}
Expand All @@ -60,8 +88,13 @@ export interface BinaryArtifactLocation {
uriBaseId?: string; // "%SRCROOT%"
}

export interface BinaryPhysicalLocationAddress {
absoluteAddress: number;
}

export interface BinaryPhysicalLocation {
artifactLocation: BinaryArtifactLocation;
address?: BinaryPhysicalLocationAddress;
region: BinaryRegionLocation;
}

Expand Down

0 comments on commit 7850cee

Please sign in to comment.