|
| 1 | +import { CmdLetBase } from '../../commands/cmdlet.js'; |
| 2 | +import { Output } from '../../io/output.js'; |
| 3 | +import { Format } from '../../misc/format.js'; |
| 4 | +import { Token } from '../../io/token.js'; |
| 5 | +import { Var } from '../../vars/var.js'; |
| 6 | + |
| 7 | +interface Replacement { |
| 8 | + dest: Var; |
| 9 | + src: Var; |
| 10 | +} |
| 11 | + |
| 12 | +export class ReplaceCmdLet extends CmdLetBase { |
| 13 | + name = 'replace'; |
| 14 | + category = 'data'; |
| 15 | + help = |
| 16 | + 'replace a function with another implementation (returns the address of the trampoline)'; |
| 17 | + |
| 18 | + private byIndex: Map<number, Replacement> = new Map<number, Replacement>(); |
| 19 | + |
| 20 | + private getNextFreeIndex(): number { |
| 21 | + let idx = 1; |
| 22 | + while (true) { |
| 23 | + if (!this.byIndex.has(idx)) return idx; |
| 24 | + idx++; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private static readonly USAGE: string = `Usage: replace |
| 29 | +
|
| 30 | +replace dest src - replace function |
| 31 | + dest the address/symbol to replace |
| 32 | + src the address/symbol to replace with`; |
| 33 | + |
| 34 | + public runSync(tokens: Token[]): Var { |
| 35 | + const retDelete = this.runDelete(tokens); |
| 36 | + if (retDelete !== null) return retDelete; |
| 37 | + |
| 38 | + const retCreate = this.runCreate(tokens); |
| 39 | + if (retCreate !== null) return retCreate; |
| 40 | + |
| 41 | + const retShow = this.runShow(tokens); |
| 42 | + if (retShow !== null) return retShow; |
| 43 | + |
| 44 | + return this.usage(); |
| 45 | + } |
| 46 | + |
| 47 | + public runDelete(tokens: Token[]): Var | null { |
| 48 | + const vars = this.transform(tokens, [this.parseIndex, this.parseDelete]); |
| 49 | + if (vars === null) return null; |
| 50 | + const [index, _] = vars as [number, string]; |
| 51 | + const replacement = this.byIndex.get(index); |
| 52 | + if (replacement === undefined) { |
| 53 | + Output.writeln(`replacement #${index} not found`); |
| 54 | + return Var.ZERO; |
| 55 | + } |
| 56 | + |
| 57 | + Output.writeln(Output.blue('deleting replacement:')); |
| 58 | + this.printReplacement(index, replacement); |
| 59 | + this.byIndex.delete(index); |
| 60 | + Interceptor.revert(replacement.dest.toPointer()); |
| 61 | + return Var.ZERO; |
| 62 | + } |
| 63 | + |
| 64 | + public runCreate(tokens: Token[]): Var | null { |
| 65 | + const vars = this.transform(tokens, [this.parseVar, this.parseVar]); |
| 66 | + if (vars === null) return null; |
| 67 | + const [destVar, srcVar] = vars as [Var, Var]; |
| 68 | + |
| 69 | + const dest = destVar.toPointer(); |
| 70 | + const src = srcVar.toPointer(); |
| 71 | + |
| 72 | + try { |
| 73 | + const index = this.getNextFreeIndex(); |
| 74 | + const replacement = { dest: destVar, src: srcVar }; |
| 75 | + this.printReplacement(index, replacement); |
| 76 | + this.byIndex.set(index, replacement); |
| 77 | + const trampoline = Interceptor.replaceFast(dest, src); |
| 78 | + Output.writeln(Output.blue('created replacement:')); |
| 79 | + return new Var(uint64(trampoline.toString())); |
| 80 | + } catch (error) { |
| 81 | + throw new Error( |
| 82 | + `failed to replace ${Format.toHexString(dest)} with ${Format.toHexString(src)}, ${error}`, |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public runShow(tokens: Token[]): Var | null { |
| 88 | + if (tokens.length !== 0) return null; |
| 89 | + |
| 90 | + Output.writeln(Output.blue('replacements:')); |
| 91 | + this.byIndex.forEach((replacement, index) => { |
| 92 | + this.printReplacement(index, replacement); |
| 93 | + }); |
| 94 | + return Var.ZERO; |
| 95 | + } |
| 96 | + |
| 97 | + private printReplacement(index: number, replacement: Replacement) { |
| 98 | + const idxString = Output.green(`#${index.toString()}.`.padEnd(4, ' ')); |
| 99 | + const destString = `dest: ${Output.blue(replacement.dest.getLiteral())}`; |
| 100 | + const srcString = `src: ${Output.blue(replacement.src.getLiteral())}`; |
| 101 | + Output.writeln(`${idxString} ${destString} -> ${srcString}`); |
| 102 | + } |
| 103 | + |
| 104 | + public usage(): Var { |
| 105 | + Output.writeln(ReplaceCmdLet.USAGE); |
| 106 | + return Var.ZERO; |
| 107 | + } |
| 108 | +} |
0 commit comments