-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
193 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,45 @@ | ||
import "./DisassemblyView.css"; | ||
import loadR2 from "./r2.js"; | ||
import { useRef } from "react"; | ||
import { NativeModule } from "./model.js"; | ||
import { useR2 } from "./use-r2.js"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import { useStayAtBottom } from "react-stay-at-bottom"; | ||
|
||
let r2Output = ""; | ||
export interface DisassemblyViewProps { | ||
mainModule?: NativeModule | null; | ||
} | ||
|
||
export default function DisassemblyView() { | ||
export default function DisassemblyView({ mainModule = null }: DisassemblyViewProps = {}) { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const [r2Output, setR2Output] = useState(""); | ||
const { executeR2Command } = useR2(); | ||
|
||
useStayAtBottom(containerRef, { | ||
initialStay: true, | ||
autoStay: true | ||
}); | ||
|
||
return ( | ||
<div ref={containerRef} className="disassembly-view" dangerouslySetInnerHTML={{__html: r2Output}} /> | ||
); | ||
} | ||
useEffect(() => { | ||
if (mainModule === null) { | ||
return; | ||
} | ||
|
||
let ignore = false; | ||
|
||
async function start() { | ||
const r2 = await loadR2({ | ||
noInitialRun: true, | ||
offset: 0, | ||
async onRead(offset: number, size: number) { | ||
const result = new Uint8Array(size); | ||
for (let i = 0; i !== size; i++) { | ||
result[i] = (i % 2 === 0) ? 0x13 : 0x37; | ||
async function start() { | ||
const result = await executeR2Command(`s ${mainModule!.base}; x`); | ||
if (!ignore) { | ||
setR2Output(result); | ||
} | ||
return result; | ||
}, | ||
}); | ||
} | ||
|
||
await r2.ccall("main", "int", [], { async: true }); | ||
start(); | ||
|
||
const _run = r2.cwrap("evaluate", "number", ["string"], { async: true }); | ||
return () => { | ||
ignore = true; | ||
}; | ||
}, [mainModule]); | ||
|
||
const rawResult = await _run("x"); | ||
try { | ||
const result = r2.UTF8ToString(rawResult); | ||
r2Output = result; | ||
} finally { | ||
r2._free(rawResult) | ||
} | ||
return ( | ||
<div ref={containerRef} className="disassembly-view" dangerouslySetInnerHTML={{ __html: r2Output }} /> | ||
); | ||
} | ||
|
||
start() | ||
.catch(e => { | ||
console.error("Unable to load r2:", e); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import loadR2Module, { type MainModule } from "./r2.js"; | ||
import { useCallback, useEffect, useRef } from "react"; | ||
|
||
export interface R2Props { | ||
onReadRequest?: ReadRequestHandler; | ||
} | ||
|
||
export type ReadRequestHandler = (address: string, size: number) => void; | ||
|
||
let state: "unloaded" | "loading" | "loaded" | "executing-command" = "unloaded"; | ||
let r2Module: MainModule | null = null; | ||
const pendingCommands: CommandRequest[] = []; | ||
const pendingReads: ((result: Uint8Array | null) => void)[] = []; | ||
|
||
interface CommandRequest { | ||
command: string; | ||
onComplete(result: string): void; | ||
} | ||
|
||
export function useR2({ onReadRequest }: R2Props = {}) { | ||
const onReadRequestRef = useRef<ReadRequestHandler>(); | ||
|
||
useEffect(() => { | ||
if (onReadRequest === undefined) { | ||
return; | ||
} | ||
|
||
onReadRequestRef.current = onReadRequest; | ||
|
||
if (state === "unloaded") { | ||
state = "loading"; | ||
loadR2(onReadRequestRef as React.MutableRefObject<ReadRequestHandler>); | ||
} | ||
}); | ||
|
||
const executeR2Command = useCallback((command: string) => { | ||
return new Promise<string>(resolve => { | ||
pendingCommands.push({ | ||
command, | ||
onComplete: resolve, | ||
}); | ||
maybeProcessPendingCommands(); | ||
}); | ||
}, []); | ||
|
||
const deliverR2ReadResponse = useCallback((result: Uint8Array | null) => { | ||
pendingReads.shift()!(result); | ||
}, []); | ||
|
||
return { | ||
executeR2Command, | ||
deliverR2ReadResponse, | ||
}; | ||
} | ||
|
||
async function loadR2(onReadRequestRef: React.MutableRefObject<ReadRequestHandler>) { | ||
const r2 = await loadR2Module({ | ||
noInitialRun: true, | ||
offset: 0, | ||
onRead(offset: number, size: number): Promise<Uint8Array> { | ||
return new Promise((resolve, reject) => { | ||
pendingReads.push(result => { | ||
if (result !== null) { | ||
resolve(result); | ||
} else { | ||
reject(new Error("read failed")); | ||
} | ||
}); | ||
onReadRequestRef.current(offset.toString(), size); | ||
}); | ||
}, | ||
}); | ||
|
||
await r2.ccall("main", "int", [], { async: true }); | ||
|
||
state = "loaded"; | ||
r2Module = r2; | ||
maybeProcessPendingCommands(); | ||
} | ||
|
||
async function maybeProcessPendingCommands() { | ||
if (state !== "loaded") { | ||
return; | ||
} | ||
|
||
state = "executing-command"; | ||
|
||
const r = r2Module!; | ||
const evaluate = r.cwrap("evaluate", "number", ["string"], { async: true }); | ||
|
||
let req: CommandRequest | undefined; | ||
while ((req = pendingCommands.shift()) !== undefined) { | ||
const rawResult = await evaluate(req.command); | ||
try { | ||
const result = r.UTF8ToString(rawResult); | ||
req.onComplete(result); | ||
} finally { | ||
r._free(rawResult) | ||
} | ||
} | ||
|
||
state = "loaded"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters