Skip to content

Commit

Permalink
Merge pull request TheThirdOne#74 from privat/test_selfmod
Browse files Browse the repository at this point in the history
Test selfmod
  • Loading branch information
privat authored Aug 15, 2024
2 parents 01e7cbd + 4a131ff commit 501dfb3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/rars/api/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public Program(Options set){
simulation = new Memory();
}

public Options getOptions() {
return set;
}

/**
* Assembles from a list of files
*
Expand Down
13 changes: 9 additions & 4 deletions test/RarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,18 @@ Program setupProgram(boolean rv64) {
Options opt = new Options();
opt.startAtMain = true;
opt.maxSteps = 1000000;
opt.selfModifyingCode = true;
return new Program(opt);
}

public void checkPrograms() {

// 32-bit tests
Program p = setupProgram(false);
runDirectory("./test", p);
runDirectory("./test/riscv-tests", p);

Globals.getSettings().setBooleanSettingNonPersistent(Settings.Bool.RV64_ENABLED,true);
InstructionSet.rv64 = true;
Globals.instructionSet.populate();
// 64-bit tests
p = setupProgram(true);
runDirectory("./test", p);
runDirectory("./test/riscv-tests-64", p);

Expand Down Expand Up @@ -100,6 +99,7 @@ public static String run(String path, Program p){
int exitCode = 0;
// TODO: better config system
// This is just a temporary solution that should work for the tests I want to write
p.getOptions().selfModifyingCode = false;
try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine();
Expand Down Expand Up @@ -131,6 +131,11 @@ public static String run(String path, Program p){
programArgumentList = new ProgramArgumentList(args).getProgramArgumentList();
} else if (line.startsWith("#error:")) {
errorMessage = line.replaceFirst("#error:", "");
} else if (line.startsWith("#selfmod:")) {
String selfmod = line.replaceFirst("#selfmod:", "");
if (selfmod.equals("true")) {
p.getOptions().selfModifyingCode = true;
}
}
line = br.readLine();
}
Expand Down
1 change: 1 addition & 0 deletions test/selfmod.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#exit:42
#selfmod:true
.text
main:
la s0, toload
Expand Down
59 changes: 59 additions & 0 deletions test/selfmod_fail.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#error:Instruction load access error
.text
main:
la s0, toload
la s1, torun

li a0, 2
call toload

# First copy by word to data
lw t0, (s0)
sw t0, (s1)
lw t0, 4(s0)
sw t0, 4(s1)

# try running it
call torun

# Copy by byte to data
mv s2, a0
mv a0, s0
mv a1, s1
call copy

mv a0, s2
call torun

# copy to .text
mv s2, a0
la a0, torun
la a1, main
call copy

mv a0, s2
call main


li a7, 93
ecall

toload:
addi a0, a0, 10
ret

copy:
li t0, 0
li t3, 8
loop:
add t1, a0, t0
lb t1, (t1)
add t2, a1, t0
sb t1, (t2)
addi t0, t0, 1
blt t0, t3, loop
ret

.data
torun:
.space 8
8 changes: 8 additions & 0 deletions test/selfmod_read.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Test read-only in the code segment (should not require selfmod to be set)
#stdout:1050771
.text
foo: li a7, 1 # PrintInt
lw a0, foo
ecall
li a7, 10
ecall

0 comments on commit 501dfb3

Please sign in to comment.