Skip to content

Instantly share code, notes, and snippets.

@ErnWong
Last active February 16, 2018 02:22
Show Gist options
  • Save ErnWong/771f0f63926d248cba8d55a97092bb2d to your computer and use it in GitHub Desktop.
Save ErnWong/771f0f63926d248cba8d55a97092bb2d to your computer and use it in GitHub Desktop.
Additional tools for debugging v86 programs
// Searches memory for specific sequence represented by string
// want = desired string sequence
// buf = memory buffer
// skip = spacing between each consequtive element of the sequence in the buf to find
function find_str_in_mem(want, buf, skip) {
skip = skip || 1;
var wantbuf = new Uint8Array(want.length);
for (let i = 0; i < want.length; i++) {
wantbuf[i] = want.charCodeAt(i);
}
var found = [];
for (let k = 0; k < buf.length; k += 0x1000000) {
console.log(' Entering ' + h(k));
for (let i = k; i < buf.length && i < k + 0x1000000; i++) {
if (wantbuf[0] !== buf[i]) continue;
for (let j = 0; j < want.length; j++) {
if (wantbuf[j] !== buf[i + j * skip]) break;
if (j + 1 === want.length) {
found.push(i);
console.log('Found at ' + h(i));
}
}
}
}
return found;
}
// Looks at the ansi string representation of a block of memory
function inspect_mem(location, size) {
size = size || 2048;
return cpu.mem8.subarray(location, location + size).reduce((a,v) => a + String.fromCharCode(v), '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment