How to use Rand method of prog Package

Best Syzkaller code snippet using prog.Rand

cpu_test.go

Source:cpu_test.go Github

copy

Full Screen

1package main2import (3 "fmt"4 "io/ioutil"5 "math/rand"6 "os"7 "os/exec"8 "path/filepath"9 "strings"10 "testing"11 "text/template"12)13const _ToolPrefix = "./toolchain/bin/riscv32-unknown-elf"14const _CC = _ToolPrefix + "-gcc"15const _AS = _ToolPrefix + "-as"16const _LD = _ToolPrefix + "-ld"17const _OBJCOPY = _ToolPrefix + "-objcopy"18type DebugBoard struct {19 board *Board20 output *strings.Builder21}22func NewDebugBoard(prog []uint8) *DebugBoard {23 output := strings.Builder{}24 board := NewBoard(prog, nil, &output)25 return &DebugBoard{26 board: board,27 output: &output,28 }29}30func (db *DebugBoard) Cpu() *Cpu {31 return db.board.Cpu()32}33type ProgArgs map[string]interface{}34type ProgTemplate struct {35 t *template.Template36 sb *strings.Builder37}38func (pt *ProgTemplate) Execute(data interface{}) string {39 pt.sb.Reset()40 pt.t.Execute(pt.sb, data)41 return pt.sb.String()42}43func NewProgTemplate(tmpl string) *ProgTemplate {44 return &ProgTemplate{45 // we add the newline bause the assembler likes having a newline46 // at the end of the last command47 template.Must(template.New("prog").Parse(48 ".global _start\n" +49 "_start:\n" +50 tmpl +51 "\n")),52 &strings.Builder{},53 }54}55func assertRegEq(t *testing.T, cpu *Cpu, reg uint8, v uint32) bool {56 regv := cpu.GetReg(reg)57 res := regv == v58 if !res {59 t.Errorf(("expected reg %d to have value 0x%08x" +60 " but it had value 0x%08x"), reg, v, regv)61 }62 return res63}64func assertCsrEq(t *testing.T, cpu *Cpu, csr uint32, v uint32) bool {65 regv := cpu.GetCsr(csr)66 res := regv == v67 if !res {68 t.Errorf(("expected csr 0x%03x to have value 0x%08x" +69 " but it had value 0x%08x"), csr, v, regv)70 }71 return res72}73func assertPcEq(t *testing.T, cpu *Cpu, v uint32) bool {74 res := cpu.pc == v75 if !res {76 t.Errorf(("expected pc to have value 0x%08x" +77 " but it had value 0x%08x"), v, cpu.pc)78 }79 return res80}81/*82func disassemble(prog []byte) string {83 cmd = exec.Command("riscv64-linux-gnu-objdump",84 "-m", "riscv:rv32",85 "-b", "binary",86 "-D",87 binPath)88 out, err = cmd.CombinedOutput()89 if err != nil {90 panic(fmt.Sprint("disassemble failed (", err, ") ", string(out)))91 }92 fmt.Println(string(out))93}94*/95func compile(prog string) []byte {96 dir, err := ioutil.TempDir("", "riscv_cpu_test")97 if err != nil {98 panic(err)99 }100 defer os.RemoveAll(dir)101 srcPath := filepath.Join(dir, "prog.c")102 objPath := filepath.Join(dir, "prog.o")103 elfPath := filepath.Join(dir, "prog.elf")104 binPath := filepath.Join(dir, "prog.bin")105 // write source106 err = ioutil.WriteFile(srcPath, []byte(prog), 0444)107 if err != nil {108 panic(err)109 }110 // compile111 cmd := exec.Command(_CC,112 "-c",113 "-Iruntime/",114 "-ffreestanding",115 "-nostdlib",116 "-std=c99",117 "-march=rv32i",118 "-mabi=ilp32",119 "-o", objPath,120 srcPath)121 out, err := cmd.CombinedOutput()122 if err != nil {123 panic(fmt.Sprint("compilation failed (", err, ") ", string(out)))124 }125 // link126 cmd = exec.Command(_LD,127 "-T", "runtime/prog.ld",128 "-static",129 "-m", "elf32lriscv",130 "-o", elfPath,131 objPath)132 out, err = cmd.CombinedOutput()133 if err != nil {134 panic(fmt.Sprint("linkage failed (", err, ") ", string(out)))135 }136 //@fixe: there is a bug in gnu-ld for riscv so we have to use objcopy137 // convert to binary138 cmd = exec.Command(_OBJCOPY,139 "-O", "binary",140 elfPath, binPath)141 out, err = cmd.CombinedOutput()142 if err != nil {143 panic(fmt.Sprint("copy failed (", err, ") ", string(out)))144 }145 // read binary146 res, err := ioutil.ReadFile(binPath)147 if err != nil {148 panic(err)149 }150 return res151}152func assemble(prog string) []byte {153 dir, err := ioutil.TempDir("", "riscv_cpu_test")154 if err != nil {155 panic(err)156 }157 defer os.RemoveAll(dir)158 srcPath := filepath.Join(dir, "prog.s")159 objPath := filepath.Join(dir, "prog.o")160 elfPath := filepath.Join(dir, "prog.elf")161 binPath := filepath.Join(dir, "prog.bin")162 // write source163 err = ioutil.WriteFile(srcPath, []byte(prog), 0444)164 if err != nil {165 panic(err)166 }167 // compile168 cmd := exec.Command(_AS,169 "-o", objPath,170 "-march=rv32i",171 "-mabi=ilp32",172 srcPath)173 out, err := cmd.CombinedOutput()174 if err != nil {175 panic(fmt.Sprint("compilation failed (", err, ") ", string(out)))176 }177 // link178 cmd = exec.Command(_LD,179 "-nostdlib",180 "-Ttext", "0x100",181 "-m", "elf32lriscv",182 "-o", elfPath, objPath)183 out, err = cmd.CombinedOutput()184 if err != nil {185 panic(fmt.Sprint("linkage failed (", err, ") ", string(out)))186 }187 // dump188 cmd = exec.Command(_OBJCOPY,189 "-O", "binary",190 elfPath, binPath)191 out, err = cmd.CombinedOutput()192 if err != nil {193 panic(fmt.Sprint("dump failed (", err, ") ", string(out)))194 }195 // read binary196 res, err := ioutil.ReadFile(binPath)197 if err != nil {198 panic(err)199 }200 return res201}202// since we can't check every permutation we check random permutations203// this defines how many random permutations to try204const FUZZ_ITER = 10205func randReg() uint8 {206 return uint8((rand.Uint32() % 31) + 1)207}208func sextend(imm int32) uint32 {209 vv := uint32(imm)210 if imm < 0 {211 vv |= 0xfffff800212 }213 return vv214}215func TestMOVI(t *testing.T) {216 for i := 0; i < FUZZ_ITER; i++ {217 reg := randReg()218 imm := rand.Int31()%0xfff - 0x800219 prog := fmt.Sprintf("addi x%d, x0, %d", reg, imm)220 t.Log("prog: ", prog)221 cpu := NewDebugBoard(assemble(prog)).Cpu()222 cpu.Step()223 expected := sextend(imm)224 if cpu.GetReg(reg) != expected {225 t.Error("expected", expected, "got", cpu.GetReg(reg))226 }227 }228}229func TestADDI(t *testing.T) {230 for i := 0; i < FUZZ_ITER; i++ {231 rd := randReg()232 rs1 := randReg()233 imm := rand.Int31()%0xfff - 0x800234 v := rand.Uint32()235 prog := fmt.Sprintf("addi x%d, x%d, %d", rd, rs1, imm)236 vv := sextend(imm)237 vv += v238 t.Log("prog: ", prog)239 cpu := NewDebugBoard(assemble(prog)).Cpu()240 cpu.SetReg(rs1, v)241 cpu.Step()242 if cpu.GetReg(rd) != vv {243 t.Error("expected", vv, "got", cpu.GetReg(rd))244 }245 }246}247func TestSTLI(t *testing.T) {248 for i := 0; i < FUZZ_ITER; i++ {249 rd := randReg()250 rs1 := randReg()251 v := rand.Int31()%0xfff - 0x800252 rs1v := rand.Uint32()253 prog := fmt.Sprintf("slti x%d, x%d, %d", rd, rs1, v)254 var vv uint32255 if int32(rs1v) < v {256 vv = 1257 } else {258 vv = 0259 }260 t.Log("prog: ", prog)261 cpu := NewDebugBoard(assemble(prog)).Cpu()262 cpu.SetReg(rs1, rs1v)263 cpu.Step()264 if cpu.GetReg(rd) != vv {265 t.Error("expected", vv, "got", cpu.GetReg(rd))266 }267 }268}269func TestSTLU(t *testing.T) {270 for i := 0; i < FUZZ_ITER; i++ {271 rd := randReg()272 rs1 := randReg()273 imm := rand.Int31()%0xfff - 0x800274 rs1v := rand.Uint32()275 prog := fmt.Sprintf("sltu x%d, x%d, %d", rd, rs1, imm)276 var expected uint32277 if rs1v < sextend(imm) {278 expected = 1279 } else {280 expected = 0281 }282 t.Log("prog: ", prog)283 cpu := NewDebugBoard(assemble(prog)).Cpu()284 cpu.SetReg(rs1, rs1v)285 cpu.Step()286 if cpu.GetReg(rd) != expected {287 t.Error("expected", expected, "got", cpu.GetReg(rd))288 }289 }290}291func TestANDI(t *testing.T) {292 for i := 0; i < FUZZ_ITER; i++ {293 rd := randReg()294 rs1 := randReg()295 imm := rand.Int31()%0xfff - 0x800296 rs1v := rand.Uint32()297 prog := fmt.Sprintf("andi x%d, x%d, %d", rd, rs1, imm)298 expected := rs1v & sextend(imm)299 t.Log("prog: ", prog)300 cpu := NewDebugBoard(assemble(prog)).Cpu()301 cpu.SetReg(rs1, rs1v)302 cpu.Step()303 if cpu.GetReg(rd) != expected {304 t.Error("expected", expected, "got", cpu.GetReg(rd))305 }306 }307}308func TestORI(t *testing.T) {309 for i := 0; i < FUZZ_ITER; i++ {310 rd := randReg()311 rs1 := randReg()312 imm := rand.Int31()%0xfff - 0x800313 rs1v := rand.Uint32()314 prog := fmt.Sprintf("ori x%d, x%d, %d", rd, rs1, imm)315 expected := rs1v | sextend(imm)316 t.Log("prog: ", prog)317 cpu := NewDebugBoard(assemble(prog)).Cpu()318 cpu.SetReg(rs1, rs1v)319 cpu.Step()320 if cpu.GetReg(rd) != expected {321 t.Error("expected", expected, "got", cpu.GetReg(rd))322 }323 }324}325func TestXORI(t *testing.T) {326 for i := 0; i < FUZZ_ITER; i++ {327 rd := randReg()328 rs1 := randReg()329 imm := rand.Int31()%0xfff - 0x800330 rs1v := rand.Uint32()331 prog := fmt.Sprintf("xori x%d, x%d, %d", rd, rs1, imm)332 expected := rs1v ^ sextend(imm)333 t.Log("prog: ", prog)334 cpu := NewDebugBoard(assemble(prog)).Cpu()335 cpu.SetReg(rs1, rs1v)336 cpu.Step()337 if cpu.GetReg(rd) != expected {338 t.Error("expected", expected, "got", cpu.GetReg(rd))339 }340 }341}342func TestSLLI(t *testing.T) {343 for i := 0; i < FUZZ_ITER; i++ {344 rd := randReg()345 rs1 := randReg()346 shamt := rand.Uint32() % 32347 rs1v := rand.Uint32()348 prog := fmt.Sprintf("slli x%d, x%d, %d", rd, rs1, shamt)349 expected := rs1v << shamt350 t.Log("prog: ", prog)351 cpu := NewDebugBoard(assemble(prog)).Cpu()352 cpu.SetReg(rs1, rs1v)353 cpu.Step()354 if cpu.GetReg(rd) != expected {355 t.Error("expected", expected, "got", cpu.GetReg(rd))356 }357 }358}359func TestSRLI(t *testing.T) {360 for i := 0; i < FUZZ_ITER; i++ {361 rd := randReg()362 rs1 := randReg()363 shamt := rand.Uint32() % 32364 rs1v := rand.Uint32()365 prog := fmt.Sprintf("srli x%d, x%d, %d", rd, rs1, shamt)366 expected := rs1v >> shamt367 t.Log("prog: ", prog)368 cpu := NewDebugBoard(assemble(prog)).Cpu()369 cpu.SetReg(rs1, rs1v)370 cpu.Step()371 if cpu.GetReg(rd) != expected {372 t.Error("expected", expected, "got", cpu.GetReg(rd))373 }374 }375}376func TestSRAI(t *testing.T) {377 for i := 0; i < FUZZ_ITER; i++ {378 rd := randReg()379 rs1 := randReg()380 shamt := rand.Uint32() % 32381 rs1v := rand.Uint32()382 prog := fmt.Sprintf("srai x%d, x%d, %d", rd, rs1, shamt)383 expected := uint32(int32(rs1v) >> shamt)384 t.Log("prog: ", prog)385 cpu := NewDebugBoard(assemble(prog)).Cpu()386 cpu.SetReg(rs1, rs1v)387 cpu.Step()388 if cpu.GetReg(rd) != expected {389 t.Error("expected", expected, "got", cpu.GetReg(rd))390 }391 }392}393func TestLUI(t *testing.T) {394 for i := 0; i < FUZZ_ITER; i++ {395 rd := randReg()396 imm := rand.Uint32() % 0xfffff397 prog := fmt.Sprintf("lui x%d, %d", rd, imm)398 expected := imm << 12399 t.Log("prog: ", prog)400 cpu := NewDebugBoard(assemble(prog)).Cpu()401 cpu.Step()402 if cpu.GetReg(rd) != expected {403 t.Error("expected", expected, "got", cpu.GetReg(rd))404 }405 }406}407func TestAUIPC(t *testing.T) {408 //@todo: this test assume pc is at 0409 for i := 0; i < FUZZ_ITER; i++ {410 rd := randReg()411 imm := rand.Uint32() % 0xfffff412 prog := fmt.Sprintf("auipc x%d, %d", rd, imm)413 t.Log("prog: ", prog)414 cpu := NewDebugBoard(assemble(prog)).Cpu()415 expected := (imm << 12) + cpu.pc416 cpu.Step()417 if cpu.GetReg(rd) != expected {418 t.Error("expected", expected, "got", cpu.GetReg(rd))419 }420 }421}422func TestADD(t *testing.T) {423 for i := 0; i < FUZZ_ITER; i++ {424 rd := randReg()425 rs1 := randReg()426 rs2 := randReg()427 rs1v := rand.Uint32()428 rs2v := rand.Uint32()429 if rs1 == rs2 {430 rs2v = rs1v431 }432 prog := fmt.Sprintf("add x%d, x%d, x%d", rd, rs1, rs2)433 t.Log("prog: ", prog)434 cpu := NewDebugBoard(assemble(prog)).Cpu()435 cpu.SetReg(rs1, rs1v)436 cpu.SetReg(rs2, rs2v)437 cpu.Step()438 expected := rs1v + rs2v439 if cpu.GetReg(rd) != expected {440 t.Error("expected", expected, "got", cpu.GetReg(rd))441 }442 }443}444func TestSUB(t *testing.T) {445 for i := 0; i < FUZZ_ITER; i++ {446 rd := randReg()447 rs1 := randReg()448 rs2 := randReg()449 rs1v := rand.Uint32()450 rs2v := rand.Uint32()451 if rs1 == rs2 {452 rs2v = rs1v453 }454 prog := fmt.Sprintf("sub x%d, x%d, x%d", rd, rs1, rs2)455 t.Log("prog: ", prog)456 cpu := NewDebugBoard(assemble(prog)).Cpu()457 cpu.SetReg(rs1, rs1v)458 cpu.SetReg(rs2, rs2v)459 cpu.Step()460 expected := rs1v - rs2v461 if cpu.GetReg(rd) != expected {462 t.Error("expected", expected, "got", cpu.GetReg(rd))463 }464 }465}466func TestSLT(t *testing.T) {467 for i := 0; i < FUZZ_ITER; i++ {468 rd := randReg()469 rs1 := randReg()470 rs2 := randReg()471 rs1v := rand.Uint32()472 rs2v := rand.Uint32()473 if rs1 == rs2 {474 rs2v = rs1v475 }476 prog := fmt.Sprintf("slt x%d, x%d, x%d", rd, rs1, rs2)477 t.Log("prog: ", prog)478 cpu := NewDebugBoard(assemble(prog)).Cpu()479 cpu.SetReg(rs1, rs1v)480 cpu.SetReg(rs2, rs2v)481 cpu.Step()482 var expected uint32483 if int32(rs1v) < int32(rs2v) {484 expected = 1485 } else {486 expected = 0487 }488 if cpu.GetReg(rd) != expected {489 t.Error("expected", expected, "got", cpu.GetReg(rd))490 }491 }492}493func TestSLTU(t *testing.T) {494 for i := 0; i < FUZZ_ITER; i++ {495 rd := randReg()496 rs1 := randReg()497 rs2 := randReg()498 rs1v := rand.Uint32()499 rs2v := rand.Uint32()500 if rs1 == rs2 {501 rs2v = rs1v502 }503 prog := fmt.Sprintf("sltu x%d, x%d, x%d", rd, rs1, rs2)504 t.Log("prog: ", prog)505 cpu := NewDebugBoard(assemble(prog)).Cpu()506 cpu.SetReg(rs1, rs1v)507 cpu.SetReg(rs2, rs2v)508 cpu.Step()509 var expected uint32510 if rs1v < rs2v {511 expected = 1512 } else {513 expected = 0514 }515 if cpu.GetReg(rd) != expected {516 t.Error("expected", expected, "got", cpu.GetReg(rd))517 }518 }519}520func TestAND(t *testing.T) {521 for i := 0; i < FUZZ_ITER; i++ {522 rd := randReg()523 rs1 := randReg()524 rs2 := randReg()525 rs1v := rand.Uint32()526 rs2v := rand.Uint32()527 if rs1 == rs2 {528 rs2v = rs1v529 }530 prog := fmt.Sprintf("and x%d, x%d, x%d", rd, rs1, rs2)531 t.Log("prog: ", prog)532 cpu := NewDebugBoard(assemble(prog)).Cpu()533 cpu.SetReg(rs1, rs1v)534 cpu.SetReg(rs2, rs2v)535 cpu.Step()536 expected := rs1v & rs2v537 if cpu.GetReg(rd) != expected {538 t.Error("expected", expected, "got", cpu.GetReg(rd))539 }540 }541}542func TestOR(t *testing.T) {543 for i := 0; i < FUZZ_ITER; i++ {544 rd := randReg()545 rs1 := randReg()546 rs2 := randReg()547 rs1v := rand.Uint32()548 rs2v := rand.Uint32()549 if rs1 == rs2 {550 rs2v = rs1v551 }552 prog := fmt.Sprintf("or x%d, x%d, x%d", rd, rs1, rs2)553 t.Log("prog: ", prog)554 cpu := NewDebugBoard(assemble(prog)).Cpu()555 cpu.SetReg(rs1, rs1v)556 cpu.SetReg(rs2, rs2v)557 cpu.Step()558 expected := rs1v | rs2v559 if cpu.GetReg(rd) != expected {560 t.Error("expected", expected, "got", cpu.GetReg(rd))561 }562 }563}564func TestXOR(t *testing.T) {565 for i := 0; i < FUZZ_ITER; i++ {566 rd := randReg()567 rs1 := randReg()568 rs2 := randReg()569 rs1v := rand.Uint32()570 rs2v := rand.Uint32()571 if rs1 == rs2 {572 rs2v = rs1v573 }574 prog := fmt.Sprintf("xor x%d, x%d, x%d", rd, rs1, rs2)575 t.Log("prog: ", prog)576 cpu := NewDebugBoard(assemble(prog)).Cpu()577 cpu.SetReg(rs1, rs1v)578 cpu.SetReg(rs2, rs2v)579 cpu.Step()580 expected := rs1v ^ rs2v581 if cpu.GetReg(rd) != expected {582 t.Error("expected", expected, "got", cpu.GetReg(rd))583 }584 }585}586func TestSLL(t *testing.T) {587 for i := 0; i < FUZZ_ITER; i++ {588 rd := randReg()589 rs1 := randReg()590 rs2 := randReg()591 rs1v := rand.Uint32()592 rs2v := rand.Uint32()593 if rs1 == rs2 {594 rs2v = rs1v595 }596 prog := fmt.Sprintf("sll x%d, x%d, x%d", rd, rs1, rs2)597 t.Log("prog: ", prog)598 cpu := NewDebugBoard(assemble(prog)).Cpu()599 cpu.SetReg(rs1, rs1v)600 cpu.SetReg(rs2, rs2v)601 cpu.Step()602 expected := rs1v << (rs2v & 0x1f)603 if cpu.GetReg(rd) != expected {604 t.Error("expected", expected, "got", cpu.GetReg(rd))605 }606 }607}608func TestSRL(t *testing.T) {609 for i := 0; i < FUZZ_ITER; i++ {610 rd := randReg()611 rs1 := randReg()612 rs2 := randReg()613 rs1v := rand.Uint32()614 rs2v := rand.Uint32()615 if rs1 == rs2 {616 rs2v = rs1v617 }618 prog := fmt.Sprintf("srl x%d, x%d, x%d", rd, rs1, rs2)619 t.Log("prog: ", prog)620 cpu := NewDebugBoard(assemble(prog)).Cpu()621 cpu.SetReg(rs1, rs1v)622 cpu.SetReg(rs2, rs2v)623 cpu.Step()624 expected := rs1v >> (rs2v & 0x1f)625 if cpu.GetReg(rd) != expected {626 t.Error("expected", expected, "got", cpu.GetReg(rd))627 }628 }629}630func TestSRA(t *testing.T) {631 for i := 0; i < FUZZ_ITER; i++ {632 rd := randReg()633 rs1 := randReg()634 rs2 := randReg()635 rs1v := rand.Uint32()636 rs2v := rand.Uint32()637 if rs1 == rs2 {638 rs2v = rs1v639 }640 prog := fmt.Sprintf("sra x%d, x%d, x%d", rd, rs1, rs2)641 t.Log("prog: ", prog)642 cpu := NewDebugBoard(assemble(prog)).Cpu()643 cpu.SetReg(rs1, rs1v)644 cpu.SetReg(rs2, rs2v)645 cpu.Step()646 expected := uint32(int32(rs1v) >> (rs2v & 0x1f))647 if cpu.GetReg(rd) != expected {648 t.Error("expected", expected, "got", cpu.GetReg(rd))649 }650 }651}652func TestJAL(t *testing.T) {653 progTmpl := NewProgTemplate(`jal x{{.rd}}, {{.offt}}`)654 for i := 0; i < FUZZ_ITER; i++ {655 rd := randReg()656 offt := (rand.Int31() % 0x3ffff) << 1657 prog := progTmpl.Execute(ProgArgs{"rd": rd, "offt": offt})658 t.Log("prog: ", prog)659 cpu := NewDebugBoard(assemble(prog)).Cpu()660 cpu.Step()661 imm := uint32(offt)662 if offt < 0 {663 imm &= 0xfff00000664 }665 assertRegEq(t, cpu, rd, cpu.initialAddr+4)666 assertPcEq(t, cpu, imm)667 }668}669func TestJALR(t *testing.T) {670 //TODO: also test immediate offset671 progTmpl := NewProgTemplate(`jalr x{{.rd}}, x{{.rs1}}`)672 for i := 0; i < FUZZ_ITER; i++ {673 rd := randReg()674 rs1 := randReg()675 rs1v := rand.Uint32()676 prog := progTmpl.Execute(ProgArgs{"rd": rd, "rs1": rs1})677 t.Log("prog: ", prog)678 cpu := NewDebugBoard(assemble(prog)).Cpu()679 cpu.SetReg(rs1, rs1v)680 cpu.Step()681 assertRegEq(t, cpu, rd, cpu.initialAddr+4)682 assertPcEq(t, cpu, rs1v&0xfffffffe)683 }684}685func testBranch(t *testing.T, opcode string, cond func(rs1v, rs2v uint32) bool) {686 progTmpl := NewProgTemplate(`687 {{.opcode}} x{{.rs1}}, x{{.rs2}}, true688 nop689 true:690 nop691 `)692 for i := 0; i < FUZZ_ITER; i++ {693 rs1 := randReg()694 rs2 := randReg()695 rs1v := rand.Uint32()696 rs2v := rand.Uint32()697 if rs1 == rs2 {698 rs2v = rs1v699 }700 prog := progTmpl.Execute(ProgArgs{701 "opcode": opcode,702 "rs1": rs1,703 "rs2": rs2,704 })705 t.Log("prog: ", prog)706 cpu := NewDebugBoard(assemble(prog)).Cpu()707 cpu.SetReg(rs1, rs1v)708 cpu.SetReg(rs2, rs2v)709 cpu.Step()710 var expected uint32711 if cond(rs1v, rs2v) {712 expected = cpu.initialAddr + 8713 } else {714 expected = cpu.initialAddr + 4715 }716 assertPcEq(t, cpu, expected)717 }718}719func TestBEQ(t *testing.T) {720 testBranch(t, "beq", func(rs1v, rs2v uint32) bool { return rs1v == rs2v })721}722func TestBNE(t *testing.T) {723 testBranch(t, "bne", func(rs1v, rs2v uint32) bool { return rs1v != rs2v })724}725func TestBLT(t *testing.T) {726 testBranch(t, "blt", func(rs1v, rs2v uint32) bool { return int32(rs1v) < int32(rs2v) })727}728func TestBLTU(t *testing.T) {729 testBranch(t, "bltu", func(rs1v, rs2v uint32) bool { return rs1v < rs2v })730}731func TestBGE(t *testing.T) {732 testBranch(t, "bge", func(rs1v, rs2v uint32) bool { return int32(rs1v) > int32(rs2v) })733}734func TestBGEU(t *testing.T) {735 testBranch(t, "bgeu", func(rs1v, rs2v uint32) bool { return rs1v > rs2v })736}737func testLd(t *testing.T, opcode string, bits uint, isSigned bool) {738 progTmpl := NewProgTemplate(`739 {{.opcode}} x{{.rd}}, data740 data:741 .word {{.v}}742 `)743 for i := 0; i < FUZZ_ITER; i++ {744 dest := randReg()745 v := rand.Uint32()746 prog := progTmpl.Execute(ProgArgs{747 "opcode": opcode,748 "rd": dest,749 "v": v,750 })751 t.Log("prog: ", prog)752 cpu := NewDebugBoard(assemble(prog)).Cpu()753 //@investigate: load instruction get broken down to 2754 // instructions by the assmebler I need to figure out755 // how to make sure only one is made is that this is756 // a unit test for single instruction like all the others757 cpu.Step()758 cpu.Step()759 expected := v & ((1 << bits) - 1)760 if isSigned {761 expected = signExtend(expected, bits)762 }763 assertRegEq(t, cpu, dest, expected)764 }765}766func TestLW(t *testing.T) {767 testLd(t, "lw", 32, false)768}769func TestLH(t *testing.T) {770 testLd(t, "lh", 16, true)771}772func TestLB(t *testing.T) {773 testLd(t, "lb", 8, true)774}775func TestLBU(t *testing.T) {776 testLd(t, "lbu", 8, false)777}778func TestLHU(t *testing.T) {779 testLd(t, "lhu", 16, false)780}781func testStr(t *testing.T, opcode string, bits uint) {782 progTmpl := NewProgTemplate(`783 {{.opcode}} x{{.rs1}}, 0x104(x0)784 data:785 .word 0786 `)787 for i := 0; i < FUZZ_ITER; i++ {788 rs1 := randReg()789 v := rand.Uint32()790 prog := progTmpl.Execute(ProgArgs{791 "opcode": opcode,792 "rs1": rs1,793 "v": v,794 })795 t.Log("prog: ", prog)796 cpu := NewDebugBoard(assemble(prog)).Cpu()797 cpu.SetReg(rs1, v)798 cpu.Step()799 cpu.Step()800 expected := v & ((1 << bits) - 1)801 if cpu.memory.LoadWord(cpu.initialAddr+4) != expected {802 t.Errorf("expected 0x%x got 0x%x", expected, cpu.LoadWord(4))803 }804 }805}806func TestSW(t *testing.T) {807 testStr(t, "sw", 32)808}809func TestSH(t *testing.T) {810 testStr(t, "sh", 16)811}812func TestSB(t *testing.T) {813 testStr(t, "sb", 8)814}815func TestRdcycle(t *testing.T) {816 progTmpl := NewProgTemplate(`817 rdcycle x{{.rd}}818 `)819 for i := 0; i < FUZZ_ITER; i++ {820 rd := randReg()821 cycles := rand.Uint64()822 prog := progTmpl.Execute(ProgArgs{"rd": rd})823 t.Log("prog: ", prog)824 cpu := NewDebugBoard(assemble(prog)).Cpu()825 cpu.cycles = cycles826 cpu.Step()827 assertRegEq(t, cpu, rd, uint32(cycles))828 }829}830func TestRdcycleh(t *testing.T) {831 progTmpl := NewProgTemplate(`832 rdcycleh x{{.rd}}833 `)834 for i := 0; i < FUZZ_ITER; i++ {835 rd := randReg()836 cycles := rand.Uint64()837 prog := progTmpl.Execute(ProgArgs{"rd": rd})838 t.Log("prog: ", prog)839 cpu := NewDebugBoard(assemble(prog)).Cpu()840 cpu.cycles = cycles841 cpu.Step()842 assertRegEq(t, cpu, rd, uint32(cycles>>32))843 }844}845func TestRdtime(t *testing.T) {846 progTmpl := NewProgTemplate(`847 rdtime x{{.rd}}848 `)849 for i := 0; i < FUZZ_ITER; i++ {850 rd := randReg()851 ticks := rand.Uint64()852 prog := progTmpl.Execute(ProgArgs{"rd": rd})853 t.Log("prog: ", prog)854 cpu := NewDebugBoard(assemble(prog)).Cpu()855 cpu.ticks = ticks856 cpu.Step()857 assertRegEq(t, cpu, rd, uint32(ticks))858 }859}860func TestRdtimeh(t *testing.T) {861 progTmpl := NewProgTemplate(`862 rdtimeh x{{.rd}}863 `)864 for i := 0; i < FUZZ_ITER; i++ {865 rd := randReg()866 ticks := rand.Uint64()867 prog := progTmpl.Execute(ProgArgs{"rd": rd})868 t.Log("prog: ", prog)869 cpu := NewDebugBoard(assemble(prog)).Cpu()870 cpu.ticks = ticks871 cpu.Step()872 assertRegEq(t, cpu, rd, uint32(ticks>>32))873 }874}875func TestRdinstret(t *testing.T) {876 progTmpl := NewProgTemplate(`rdinstret x{{.rd}}`)877 for i := 0; i < FUZZ_ITER; i++ {878 rd := randReg()879 instret := rand.Uint64()880 prog := progTmpl.Execute(ProgArgs{"rd": rd})881 t.Log("prog: ", prog)882 cpu := NewDebugBoard(assemble(prog)).Cpu()883 cpu.instret = instret884 cpu.Step()885 assertRegEq(t, cpu, rd, uint32(instret))886 }887}888func TestRdinstreth(t *testing.T) {889 progTmpl := NewProgTemplate(`rdinstreth x{{.rd}}`)890 for i := 0; i < FUZZ_ITER; i++ {891 rd := randReg()892 instret := rand.Uint64()893 prog := progTmpl.Execute(ProgArgs{"rd": rd})894 t.Log("prog: ", prog)895 cpu := NewDebugBoard(assemble(prog)).Cpu()896 cpu.instret = instret897 cpu.Step()898 assertRegEq(t, cpu, rd, uint32(instret>>32))899 }900}901func TestReadCsr(t *testing.T) {902 progTmpl := NewProgTemplate(`csrrw x{{.rd}}, mscratch, x0`)903 for i := 0; i < FUZZ_ITER; i++ {904 rd := randReg()905 v := rand.Uint32()906 prog := progTmpl.Execute(ProgArgs{907 "rd": rd,908 })909 t.Log("prog: ", prog)910 cpu := NewDebugBoard(assemble(prog)).Cpu()911 cpu.SetCsr(CsrScratch|CsrM, v)912 cpu.Step()913 assertRegEq(t, cpu, rd, v)914 assertCsrEq(t, cpu, CsrScratch|CsrM, v)915 }916}917func TestWriteCsr(t *testing.T) {918 progTmpl := NewProgTemplate(`csrrw x0, mscratch, x{{.rd}}`)919 for i := 0; i < FUZZ_ITER; i++ {920 rd := randReg()921 v := rand.Uint32()922 prog := progTmpl.Execute(ProgArgs{923 "rd": rd,924 })925 t.Log("prog: ", prog)926 cpu := NewDebugBoard(assemble(prog)).Cpu()927 cpu.SetReg(rd, v)928 cpu.Step()929 assertCsrEq(t, cpu, CsrScratch|CsrM, v)930 }931}932func TestCsrrw(t *testing.T) {933 progTmpl := NewProgTemplate(`csrrw x{{.rd}}, mscratch, x{{.rd}}`)934 for i := 0; i < FUZZ_ITER; i++ {935 rd := (randReg() % 30) + 1936 csrv := rand.Uint32()937 rdv := rand.Uint32()938 prog := progTmpl.Execute(ProgArgs{939 "rd": rd,940 })941 t.Log("prog: ", prog)942 cpu := NewDebugBoard(assemble(prog)).Cpu()943 cpu.SetReg(rd, rdv)944 cpu.SetCsr(CsrScratch|CsrM, csrv)945 cpu.Step()946 assertCsrEq(t, cpu, CsrScratch|CsrM, rdv)947 assertRegEq(t, cpu, rd, csrv)948 }949}950func TestCsrrs(t *testing.T) {951 progTmpl := NewProgTemplate(`csrrs x{{.rd}}, mscratch, x{{.rd}}`)952 for i := 0; i < FUZZ_ITER; i++ {953 rd := (randReg() % 30) + 1954 csrv := rand.Uint32()955 rdv := rand.Uint32()956 prog := progTmpl.Execute(ProgArgs{957 "rd": rd,958 })959 t.Log("prog: ", prog)960 cpu := NewDebugBoard(assemble(prog)).Cpu()961 cpu.SetReg(rd, rdv)962 cpu.SetCsr(CsrScratch|CsrM, csrv)963 cpu.Step()964 assertCsrEq(t, cpu, CsrScratch|CsrM, csrv&rdv)965 assertRegEq(t, cpu, rd, csrv)966 }967}968func TestCsrrc(t *testing.T) {969 progTmpl := NewProgTemplate(`csrrc x{{.rd}}, mscratch, x{{.rd}}`)970 for i := 0; i < FUZZ_ITER; i++ {971 rd := (randReg() % 30) + 1972 csrv := rand.Uint32()973 rdv := rand.Uint32()974 prog := progTmpl.Execute(ProgArgs{975 "rd": rd,976 })977 t.Log("prog: ", prog)978 cpu := NewDebugBoard(assemble(prog)).Cpu()979 cpu.SetReg(rd, rdv)980 cpu.SetCsr(CsrScratch|CsrM, csrv)981 cpu.Step()982 assertCsrEq(t, cpu, CsrScratch|CsrM, csrv&(^rdv))983 assertRegEq(t, cpu, rd, csrv)984 }985}986func TestEbreak(t *testing.T) {987 progTmpl := NewProgTemplate(`ebreak`)988 for i := 0; i < FUZZ_ITER; i++ {989 prog := progTmpl.Execute(nil)990 t.Log("prog: ", prog)991 cpu := NewDebugBoard(assemble(prog)).Cpu()992 cpu.Step()993 assertCsrEq(t, cpu, CsrEpc|CsrM, cpu.initialAddr)994 assertCsrEq(t, cpu, CsrTval|CsrM, cpu.initialAddr)995 assertCsrEq(t, cpu, CsrCause|CsrM, ExceptionBreakpoint)996 }997}998func TestProgs(t *testing.T) {999 files, err := ioutil.ReadDir("./testprogs")1000 if err != nil {1001 panic(err)1002 }1003 for _, f := range files {1004 if !strings.HasSuffix(f.Name(), ".c") {1005 continue1006 }1007 t.Log("Running test prog:", f.Name())1008 prog, err := ioutil.ReadFile("./testprogs/" + f.Name())1009 if err != nil {1010 panic(err)1011 }1012 board := NewDebugBoard(compile(string(prog)))1013 cpu := board.Cpu()1014 cpu.Execute()1015 output := strings.TrimSpace(board.output.String())1016 if output != "" {1017 t.Log("output: ", output)1018 }1019 if cpu.GetCsr(CsrHalt) != 0 {1020 t.Errorf("Execution failed")1021 }1022 outfile := "./testprogs/" + f.Name() + ".out"1023 if _, err := os.Stat(outfile); err == nil {1024 expected, err := ioutil.ReadFile(outfile)1025 expectedOutput := strings.TrimSpace(string(expected))1026 if err != nil {1027 t.Error("Failed to read output file:", err)1028 }1029 if strings.Compare(expectedOutput, output) != 0 {1030 t.Errorf("Expected output:\n%s\nGot output:\n%s\n", expectedOutput, output)1031 }1032 }1033 }1034}...

Full Screen

Full Screen

init_vusb.go

Source:init_vusb.go Github

copy

Full Screen

...53 if g.Target().ArgContainsAny(arg) {54 return55 }56 id := randUsbDeviceID(g)57 bcdDevice := id.BcdDeviceLo + uint16(g.Rand().Intn(int(id.BcdDeviceHi-id.BcdDeviceLo)+1))58 devArg := arg.(*prog.GroupArg).Inner[0]59 patchGroupArg(devArg, 7, "idVendor", uint64(id.IDVendor))60 patchGroupArg(devArg, 8, "idProduct", uint64(id.IDProduct))61 patchGroupArg(devArg, 9, "bcdDevice", uint64(bcdDevice))62 patchGroupArg(devArg, 3, "bDeviceClass", uint64(id.BDeviceClass))63 patchGroupArg(devArg, 4, "bDeviceSubClass", uint64(id.BDeviceSubClass))64 patchGroupArg(devArg, 5, "bDeviceProtocol", uint64(id.BDeviceProtocol))65 configArg := devArg.(*prog.GroupArg).Inner[14].(*prog.GroupArg).Inner[0].(*prog.GroupArg).Inner[0]66 interfacesArg := configArg.(*prog.GroupArg).Inner[8]67 for i, interfaceArg := range interfacesArg.(*prog.GroupArg).Inner {68 interfaceArg = interfaceArg.(*prog.GroupArg).Inner[0]69 if i > 0 {70 // Generate new IDs for every interface after the first one.71 id = randUsbDeviceID(g)72 }73 patchGroupArg(interfaceArg, 5, "bInterfaceClass", uint64(id.BInterfaceClass))74 patchGroupArg(interfaceArg, 6, "bInterfaceSubClass", uint64(id.BInterfaceSubClass))75 patchGroupArg(interfaceArg, 7, "bInterfaceProtocol", uint64(id.BInterfaceProtocol))76 patchGroupArg(interfaceArg, 2, "bInterfaceNumber", uint64(id.BInterfaceNumber))77 }78 return79}80func randUsbDeviceID(g *prog.Gen) UsbDeviceID {81 totalIds := len(usbIds) / BytesPerUsbID82 idNum := g.Rand().Intn(totalIds)83 base := usbIds[idNum*BytesPerUsbID : (idNum+1)*BytesPerUsbID]84 p := strings.NewReader(base)85 var id UsbDeviceID86 if binary.Read(p, binary.LittleEndian, &id) != nil {87 panic("not enough data to read")88 }89 if (id.MatchFlags & USB_DEVICE_ID_MATCH_VENDOR) == 0 {90 id.IDVendor = uint16(g.Rand().Intn(0xffff + 1))91 }92 if (id.MatchFlags & USB_DEVICE_ID_MATCH_PRODUCT) == 0 {93 id.IDProduct = uint16(g.Rand().Intn(0xffff + 1))94 }95 if (id.MatchFlags & USB_DEVICE_ID_MATCH_DEV_LO) == 0 {96 id.BcdDeviceLo = 0x097 }98 if (id.MatchFlags & USB_DEVICE_ID_MATCH_DEV_HI) == 0 {99 id.BcdDeviceHi = 0xffff100 }101 if (id.MatchFlags & USB_DEVICE_ID_MATCH_DEV_CLASS) == 0 {102 id.BDeviceClass = uint8(g.Rand().Intn(0xff + 1))103 }104 if (id.MatchFlags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) == 0 {105 id.BDeviceSubClass = uint8(g.Rand().Intn(0xff + 1))106 }107 if (id.MatchFlags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) == 0 {108 id.BDeviceProtocol = uint8(g.Rand().Intn(0xff + 1))109 }110 if (id.MatchFlags & USB_DEVICE_ID_MATCH_INT_CLASS) == 0 {111 id.BInterfaceClass = uint8(g.Rand().Intn(0xff + 1))112 }113 if (id.MatchFlags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) == 0 {114 id.BInterfaceSubClass = uint8(g.Rand().Intn(0xff + 1))115 }116 if (id.MatchFlags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) == 0 {117 id.BInterfaceProtocol = uint8(g.Rand().Intn(0xff + 1))118 }119 if (id.MatchFlags & USB_DEVICE_ID_MATCH_INT_NUMBER) == 0 {120 id.BInterfaceNumber = uint8(g.Rand().Intn(0xff + 1))121 }122 return id123}124func (arch *arch) generateUsbHidDeviceDescriptor(g *prog.Gen, typ0 prog.Type, dir prog.Dir, old prog.Arg) (125 arg prog.Arg, calls []*prog.Call) {126 if old == nil {127 arg = g.GenerateSpecialArg(typ0, dir, &calls)128 } else {129 arg = old130 calls = g.MutateArg(arg)131 }132 if g.Target().ArgContainsAny(arg) {133 return134 }135 totalIds := len(hidIds) / BytesPerHidID136 idNum := g.Rand().Intn(totalIds)137 base := hidIds[idNum*BytesPerHidID : (idNum+1)*BytesPerHidID]138 p := strings.NewReader(base)139 var id HidDeviceID140 if binary.Read(p, binary.LittleEndian, &id) != nil {141 panic("not enough data to read")142 }143 devArg := arg.(*prog.GroupArg).Inner[0]144 patchGroupArg(devArg, 7, "idVendor", uint64(id.Vendor))145 patchGroupArg(devArg, 8, "idProduct", uint64(id.Product))146 return147}148func patchGroupArg(arg prog.Arg, index int, field string, value uint64) {149 a := arg.(*prog.GroupArg)150 typ := a.Type().(*prog.StructType)...

Full Screen

Full Screen

Rand

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "math/rand"3func main() {4 fmt.Println(rand.Intn(100))5}6import "fmt"7import "math"8func main() {9 fmt.Println(math.Sqrt(7))10}11import "fmt"12import "math"13func main() {14 fmt.Println(math.Pi)15}16import "fmt"17import "math"18func main() {19 fmt.Println(math.Pow(2, 3))20}21import "fmt"22import "math"23func main() {24 fmt.Println(math.Abs(-1))25}26import "fmt"27import "math"28func main() {29 fmt.Println(math.Ceil(1.2))30}31import "fmt"32import "math"33func main() {34 fmt.Println(math.Floor(1.2))35}36import "fmt"37import "math"38func main() {39 fmt.Println(math.Round(1.2))40}41import "fmt"42import "math"43func main() {44 fmt.Println(math.Exp(1))45}46import "fmt"47import "math"48func main() {49 fmt.Println(math.Log(1))50}51import "fmt"52import "math"53func main() {54 fmt.Println(math.Log10(1))55}56import "fmt"57import "math"58func main() {59 fmt.Println(math.Log2(1))60}

Full Screen

Full Screen

Rand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("A random number is", rand.Intn(10))4}5import (6func main() {7 fmt.Println("A random number is", rand.Intn(10))8}9import (10func main() {11 fmt.Println("A random number is", rand.Intn(10))12}13import (14func main() {15 fmt.Println("A random number is", rand.Intn(10))16}17import (18func main() {19 fmt.Println("A random number is", rand.Intn(10))20}21import (22func main() {23 fmt.Println("A random number is", rand.Intn(10))24}25import (26func main() {27 fmt.Println("A random number is", rand.Intn(10))28}29import (30func main() {31 fmt.Println("A random number is", rand.Intn(10))32}33import (34func main() {35 fmt.Println("A random number is", rand.Intn(10))36}37import (38func main() {39 fmt.Println("A random number is", rand.Int

Full Screen

Full Screen

Rand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 x = rand.Intn(100)4 fmt.Println(x)5}6import (7func main() {8 x = rand.Intn(100)9 fmt.Println(x)10}11import (12func main() {13 x = rand.Intn(100)14 fmt.Println(x)15}16import (17func main() {18 x = rand.Intn(100)19 fmt.Println(x)20}21import (22func main() {23 x = rand.Intn(100)24 fmt.Println(x)25}26import (27func main() {28 x = rand.Intn(100)29 fmt.Println(x)30}31import (32func main() {33 x = rand.Intn(100)34 fmt.Println(x)35}36import (37func main() {38 x = rand.Intn(100)39 fmt.Println(x)40}

Full Screen

Full Screen

Rand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 fmt.Println(rand.Intn(100))5}6import (7func main() {

Full Screen

Full Screen

Rand

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "Math/rand"3func main() {4 fmt.Println("My favorite number is", rand.Intn(10))5}6import "fmt"7import "Math/rand"8import "time"9func main() {10 fmt.Println("My favorite number is", rand.Intn(10))11}12import "fmt"13import "Math/rand"14import "time"15func main() {16 s1 := rand.NewSource(time.Now().UnixNano())17 r1 := rand.New(s1)18 fmt.Println("My favorite number is", r1.Intn(10))19}20import "fmt"21import "Math/rand"22func main() {23 fmt.Print(rand.Intn(100), ",")24 fmt.Print(rand.Intn(100))25 fmt.Println()26 fmt.Println(rand.Float64())27 fmt.Print((rand.Float64()*5)+5, ",")28 fmt.Print((rand.Float64() * 5) + 5)29 fmt.Println()30}31import "fmt"32import "Math/rand"33func main() {34 fmt.Println(rand.Intn(100))35 fmt.Println(rand.Intn(100))36 fmt.Println()37 fmt.Println(rand.Float64())38 fmt.Println((rand.Float64() * 5) + 5)39 fmt.Println()40 s1 := rand.NewSource(42)41 r1 := rand.New(s1)42 fmt.Println(r1.Intn(100

Full Screen

Full Screen

Rand

Using AI Code Generation

copy

Full Screen

1import prog.*2public class 2 {3 public static void main(String[] args) {4 System.out.println(prog.Rand.randInt(0, 10));5 }6}

Full Screen

Full Screen

Rand

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "math/rand"3func main() {4 fmt.Println(rand.Intn(100))5}6import "fmt"7import "math/rand"8func main() {9 fmt.Println(rand.Intn(100))10 fmt.Println(rand.Intn(100))11 fmt.Println(rand.Float64())12 fmt.Println((rand.Float64() * 5) + 5)13 fmt.Println(rand.Intn(100))14 fmt.Println(rand.Intn(100))15 fmt.Println(rand.Float64())16 fmt.Println((rand.Float64() * 5) + 5)17}18import "fmt"19import "math/rand"20func main() {21 rand.Seed(42)22 fmt.Println(rand.Intn(100))23 fmt.Println(rand.Intn(100))24 fmt.Println(rand.Float64())25 fmt.Println((rand.Float64() * 5) + 5)26 fmt.Println(rand.Intn(100))27 fmt.Println(rand.Intn(100))28 fmt.Println(rand.Float64())29 fmt.Println((rand.Float64() * 5) + 5)30}31import "fmt"32import "math/rand"33func main() {34 rand.Seed(42)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful