Skip to content

Commit

Permalink
Fix sbrk when amount % PAGE_SIZE === 0
Browse files Browse the repository at this point in the history
  • Loading branch information
tomusdrw committed Jan 8, 2025
1 parent 0ea5890 commit 557ac0c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
4 changes: 2 additions & 2 deletions assembly/memory-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export class RawPage {

export class Arena {
private free: RawPage[];
private arenaBytes: number;
private readonly arenaBytes: u32;
private extraPageIndex: ArenaId;

constructor(pageCount: u32) {
this.arenaBytes = PAGE_SIZE * pageCount;
const data = new ArrayBuffer(this.arenaBytes);
this.free = [];
this.extraPageIndex = pageCount;
const data = new ArrayBuffer(this.arenaBytes);
for (let i = 0; i < <i32>pageCount; i++) {
this.free.unshift(new RawPage(i, Uint8Array.wrap(data, i * PAGE_SIZE, PAGE_SIZE)));
}
Expand Down
2 changes: 1 addition & 1 deletion assembly/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Memory {
}
this.sbrkAddress = u32(newSbrk);

const pageIdx = i32(newSbrk >> PAGE_SIZE_SHIFT);
const pageIdx = i32((newSbrk - 1) >> PAGE_SIZE_SHIFT);
if (pageIdx === this.lastAllocatedPage) {
return freeMemoryStart;
}
Expand Down
43 changes: 25 additions & 18 deletions bin/fuzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import fs from 'node:fs';
import { Pvm } from "@typeberry/pvm-debugger-adapter";
import { wrapAsProgram, runVm, disassemble, InputKind } from "../build/release.js";

let runNumber = 0;

export function fuzz(data) {
const gas = 200n;
const pc = 0;
const pvm = new Pvm();
const program = wrapAsProgram(new Uint8Array(data));
if (program.length > 100) {
return;
}

try {
pvm.reset(
Expand Down Expand Up @@ -37,24 +42,26 @@ export function fuzz(data) {
assert(pvm.getProgramCounter(), output.pc, 'pc');
});

// try {
// writeTestCase(
// program,
// {
// pc,
// gas,
// registers,
// },
// {
// status: pvm.getStatus(),
// gasLeft: pvm.getGasLeft(),
// pc: pvm.getProgramCounter(),
// registers: pvm.getRegisters()
// },
// );
// } catch (e) {
// console.warn('Unable to write file', e);
// }
try {
if (runNumber % 5000 === 0) {
writeTestCase(
program,
{
pc,
gas,
registers,
},
{
status: pvm.getStatus(),
gasLeft: pvm.getGasLeft(),
pc: pvm.getProgramCounter(),
registers: pvm.getRegisters()
},
);
}
} catch (e) {
console.warn('Unable to write file', e);
}
} catch (e) {
const hex = programHex(program);
console.log(program);
Expand Down

0 comments on commit 557ac0c

Please sign in to comment.