2019-11-23 17:35:57 +01:00
|
|
|
module main
|
2019-12-07 20:25:19 +01:00
|
|
|
import forkedtest
|
2019-11-23 17:35:57 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
sample_text_file1 = ""
|
|
|
|
)
|
|
|
|
|
2019-12-04 09:46:09 +01:00
|
|
|
fn check_fork_minimal () {
|
2019-11-26 04:12:37 +01:00
|
|
|
child := sys_fork()
|
2019-12-04 09:46:09 +01:00
|
|
|
ec := 100
|
2019-11-26 04:12:37 +01:00
|
|
|
if child == 0 {
|
2019-12-04 09:46:09 +01:00
|
|
|
println("child")
|
|
|
|
sys_exit(ec)
|
2019-11-26 04:12:37 +01:00
|
|
|
}
|
2019-12-04 09:46:09 +01:00
|
|
|
siginfo := [
|
2019-12-07 20:25:19 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
2019-12-04 09:46:09 +01:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
|
|
|
|
e := sys_waitid(.p_pid, child, intptr(siginfo.data) , .wexited, 0)
|
|
|
|
|
|
|
|
assert e == .enoerror
|
2020-04-23 05:16:10 +02:00
|
|
|
//println(i64_tos(buffer0,80,siginfo[Sig_index.si_code],16))
|
|
|
|
assert siginfo[Sig_index.si_code] == int(Wi_si_code.cld_exited)
|
|
|
|
assert siginfo[Sig_index.si_pid] == child
|
|
|
|
assert siginfo[Sig_index.si_status] == ec
|
|
|
|
assert siginfo[Sig_index.si_signo] == int(Signo.sigchld)
|
|
|
|
assert siginfo[Sig_index.si_uid] == sys_getuid()
|
2019-11-26 04:12:37 +01:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:35:57 +01:00
|
|
|
fn check_read_write_pipe() {
|
2019-11-26 04:12:37 +01:00
|
|
|
// Checks the following system calls:
|
|
|
|
// sys_pipe
|
|
|
|
// sys_write
|
|
|
|
// sys_read
|
|
|
|
// sys_close
|
|
|
|
//
|
2019-12-08 11:44:52 +01:00
|
|
|
buffer0 := [byte(0)].repeat(128)
|
|
|
|
buffer := byteptr(buffer0.data)
|
2019-12-07 20:25:19 +01:00
|
|
|
|
|
|
|
fd := [-1, -1]
|
2019-11-23 17:35:57 +01:00
|
|
|
|
|
|
|
assert fd[0] == -1
|
|
|
|
assert fd[1] == -1
|
|
|
|
|
2019-12-08 11:44:52 +01:00
|
|
|
a := sys_pipe(intptr(&fd[0]))
|
2019-11-23 17:35:57 +01:00
|
|
|
|
2019-11-29 08:14:17 +01:00
|
|
|
assert a == .enoerror
|
2019-11-23 17:35:57 +01:00
|
|
|
|
|
|
|
assert fd[0] != -1
|
|
|
|
assert fd[1] != -1
|
|
|
|
|
|
|
|
test_data := "test_data"
|
|
|
|
b := test_data.len + 1
|
2019-11-29 08:14:17 +01:00
|
|
|
c1, e1 := sys_write (fd[1], test_data.str, u64(b))
|
2019-11-23 17:35:57 +01:00
|
|
|
|
2019-11-29 08:14:17 +01:00
|
|
|
assert e1 == .enoerror
|
|
|
|
assert c1 == b
|
2019-11-23 17:35:57 +01:00
|
|
|
|
2019-12-08 11:44:52 +01:00
|
|
|
c2, e2 := sys_read(fd[0], buffer, u64(b))
|
2019-11-23 17:35:57 +01:00
|
|
|
|
2019-11-29 08:14:17 +01:00
|
|
|
assert e2 == .enoerror
|
|
|
|
assert c2 == b
|
2019-11-23 17:35:57 +01:00
|
|
|
|
|
|
|
assert buffer[b-1] == 0
|
|
|
|
|
|
|
|
for i in 0..b {
|
|
|
|
assert test_data[i] == buffer[i]
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:14:17 +01:00
|
|
|
assert sys_close(fd[0]) == .enoerror
|
|
|
|
assert sys_close(fd[1]) == .enoerror
|
2019-11-23 17:35:57 +01:00
|
|
|
|
2019-11-29 08:14:17 +01:00
|
|
|
assert sys_close(-1) == .ebadf
|
2019-11-23 17:35:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_read_file() {
|
|
|
|
/*
|
|
|
|
Checks the following system calls:
|
|
|
|
sys_read
|
|
|
|
sys_write
|
|
|
|
sys_close
|
2019-11-26 04:12:37 +01:00
|
|
|
sys_open
|
2019-11-23 17:35:57 +01:00
|
|
|
*/
|
2019-12-08 11:44:52 +01:00
|
|
|
buffer0 := [byte(0)].repeat(128)
|
|
|
|
buffer := byteptr(buffer0.data)
|
2019-12-07 20:25:19 +01:00
|
|
|
|
2019-11-23 17:35:57 +01:00
|
|
|
test_file := "sample_text1.txt"
|
|
|
|
sample_text := "Do not change this text.\n"
|
2019-11-26 04:12:37 +01:00
|
|
|
fd, ec := sys_open(test_file.str, .o_rdonly, 0)
|
2019-11-23 17:35:57 +01:00
|
|
|
assert fd > 0
|
2019-11-26 04:12:37 +01:00
|
|
|
assert ec == .enoerror
|
2019-11-23 17:35:57 +01:00
|
|
|
n := sample_text.len
|
2019-11-29 08:14:17 +01:00
|
|
|
c, e := sys_read(fd, buffer, u64(n*2))
|
|
|
|
assert e == .enoerror
|
2019-11-23 17:35:57 +01:00
|
|
|
assert c == n
|
|
|
|
for i in 0..n {
|
|
|
|
assert sample_text[i] == buffer[i]
|
|
|
|
}
|
2019-11-29 08:14:17 +01:00
|
|
|
assert sys_close(fd) == .enoerror
|
2019-11-23 17:35:57 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 04:12:37 +01:00
|
|
|
fn check_open_file_fail() {
|
|
|
|
fd1, ec1 := sys_open("./nofilehere".str, .o_rdonly, 0)
|
|
|
|
assert fd1 == -1
|
|
|
|
assert ec1 == .enoent
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
fn check_print() {
|
|
|
|
println ("checking print and println")
|
|
|
|
|
|
|
|
a := sys_pipe(intptr(fd))
|
|
|
|
assert a != -1
|
|
|
|
assert fd[0] != -1
|
|
|
|
assert fd[1] != -1
|
|
|
|
|
|
|
|
//sys_dup2
|
|
|
|
println ("print and println passed")
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-11-29 08:14:17 +01:00
|
|
|
fn check_munmap_fail() {
|
|
|
|
ec := sys_munmap(-16384,8192)
|
|
|
|
assert ec == .einval
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_mmap_one_page() {
|
2020-04-23 05:16:10 +02:00
|
|
|
mp := int(Mm_prot.prot_read) | int(Mm_prot.prot_write)
|
|
|
|
mf := int(Map_flags.map_private) | int(Map_flags.map_anonymous)
|
|
|
|
mut a, e := sys_mmap(0, u64(Linux_mem.page_size), Mm_prot(mp), Map_flags(mf), -1, 0)
|
2019-11-29 08:14:17 +01:00
|
|
|
|
|
|
|
assert e == .enoerror
|
|
|
|
assert a != byteptr(-1)
|
|
|
|
|
2020-04-23 05:16:10 +02:00
|
|
|
for i in 0..int(Linux_mem.page_size) {
|
2019-11-29 08:14:17 +01:00
|
|
|
b := i & 0xFF
|
|
|
|
a[i] = b
|
|
|
|
assert a[i] == b
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:16:10 +02:00
|
|
|
ec := sys_munmap(a, u64(Linux_mem.page_size))
|
2019-11-29 08:14:17 +01:00
|
|
|
assert ec == .enoerror
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_mm_pages() {
|
2020-04-23 05:16:10 +02:00
|
|
|
for i in 0 .. int(Linux_mem.page_size)-4 {
|
2019-11-29 08:14:17 +01:00
|
|
|
assert u32(1) == mm_pages(u64(i))
|
|
|
|
}
|
2020-04-23 05:16:10 +02:00
|
|
|
for i in int(Linux_mem.page_size)-3 .. (int(Linux_mem.page_size)*2)-4 {
|
2019-11-29 08:14:17 +01:00
|
|
|
assert u32(2) == mm_pages(u64(i))
|
|
|
|
}
|
2020-04-23 05:16:10 +02:00
|
|
|
for i in (int(Linux_mem.page_size)*2)-3 .. (int(Linux_mem.page_size)*3)-4 {
|
2019-11-29 08:14:17 +01:00
|
|
|
assert u32(3) == mm_pages(u64(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:16:10 +02:00
|
|
|
//pub fn mm_alloc(size u64) (voidptr, Errno)
|
2019-11-29 08:14:17 +01:00
|
|
|
|
|
|
|
fn check_mm_alloc() {
|
|
|
|
for i in 1 .. 2000 {
|
|
|
|
size := u64(i*1000)
|
|
|
|
pages := mm_pages(size)
|
|
|
|
mut a, e := mm_alloc(size)
|
|
|
|
|
|
|
|
assert e == .enoerror
|
|
|
|
ap := intptr(a-4)
|
|
|
|
assert *ap == int(pages)
|
|
|
|
assert e == .enoerror
|
|
|
|
assert !isnil(a)
|
|
|
|
|
|
|
|
if (i%111) == 0 {
|
|
|
|
for j in 0 .. int(size) {
|
|
|
|
b := j & 0xFF
|
|
|
|
a[j] = b
|
|
|
|
assert b == int(a[j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mfa := mm_free(a)
|
|
|
|
|
|
|
|
assert mfa == .enoerror
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 09:27:36 +01:00
|
|
|
fn check_int_array_ro() {
|
|
|
|
a := [100,110,120,130]
|
|
|
|
assert a.len == 4
|
|
|
|
assert a[0] == 100
|
|
|
|
assert a[1] == 110
|
|
|
|
assert a[2] == 120
|
|
|
|
assert a[3] == 130
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_int_array_rw() {
|
|
|
|
mut a := [-10,-11,-12,-13]
|
|
|
|
assert a.len == 4
|
|
|
|
assert a[0] == -10
|
|
|
|
assert a[1] == -11
|
|
|
|
assert a[2] == -12
|
|
|
|
assert a[3] == -13
|
|
|
|
for i in 0..a.len {
|
|
|
|
b := -a[i] * 10
|
|
|
|
a[i] = b
|
|
|
|
assert a[i] == b
|
|
|
|
}
|
|
|
|
assert a[3] == 130
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_int64_array_ro() {
|
|
|
|
a := [i64(1000),1100,1200,1300,1400]
|
|
|
|
assert a.len == 5
|
|
|
|
assert a[0] == 1000
|
|
|
|
assert a[1] == 1100
|
|
|
|
assert a[2] == 1200
|
|
|
|
assert a[3] == 1300
|
|
|
|
assert a[4] == 1400
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_voidptr_array_ro() {
|
|
|
|
a := [
|
|
|
|
voidptr(10000),
|
|
|
|
voidptr(11000),
|
|
|
|
voidptr(12000),
|
|
|
|
voidptr(13000),
|
|
|
|
voidptr(14000),
|
|
|
|
voidptr(15000)
|
|
|
|
]
|
|
|
|
assert a.len == 6
|
|
|
|
assert a[0] == voidptr(10000)
|
|
|
|
assert a[1] == voidptr(11000)
|
|
|
|
assert a[2] == voidptr(12000)
|
|
|
|
assert a[3] == voidptr(13000)
|
|
|
|
assert a[4] == voidptr(14000)
|
|
|
|
assert a[5] == voidptr(15000)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_voidptr_array_rw() {
|
|
|
|
mut a := [
|
|
|
|
voidptr(-1),
|
|
|
|
voidptr(-1),
|
|
|
|
voidptr(-1),
|
|
|
|
voidptr(-1),
|
|
|
|
voidptr(-1),
|
|
|
|
voidptr(-1)
|
|
|
|
]
|
|
|
|
assert a.len == 6
|
|
|
|
|
|
|
|
assert a[0] == voidptr(-1)
|
|
|
|
assert a[1] == voidptr(-1)
|
|
|
|
assert a[2] == voidptr(-1)
|
|
|
|
assert a[3] == voidptr(-1)
|
|
|
|
assert a[4] == voidptr(-1)
|
|
|
|
assert a[5] == voidptr(-1)
|
|
|
|
|
|
|
|
a[0] = voidptr(100000)
|
|
|
|
assert a[0] == voidptr(100000)
|
|
|
|
|
|
|
|
a[1] = voidptr(110000)
|
|
|
|
assert a[1] == voidptr(110000)
|
|
|
|
|
|
|
|
a[2] = voidptr(120000)
|
|
|
|
assert a[2] == voidptr(120000)
|
|
|
|
|
|
|
|
a[3] = voidptr(130000)
|
|
|
|
assert a[3] == voidptr(130000)
|
|
|
|
|
|
|
|
a[4] = voidptr(140000)
|
|
|
|
assert a[4] == voidptr(140000)
|
|
|
|
|
|
|
|
a[5] = voidptr(150000)
|
|
|
|
assert a[5] == voidptr(150000)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-23 17:35:57 +01:00
|
|
|
fn main() {
|
2019-12-07 20:25:19 +01:00
|
|
|
mut fails := 0
|
|
|
|
fails += forkedtest.normal_run(check_fork_minimal, "check_fork_minimal")
|
|
|
|
fails += forkedtest.normal_run(check_munmap_fail, "check_munmap_fail")
|
|
|
|
fails += forkedtest.normal_run(check_mmap_one_page, "check_mmap_one_page")
|
|
|
|
fails += forkedtest.normal_run(check_mm_pages, "check_mm_pages")
|
|
|
|
fails += forkedtest.normal_run(check_mm_alloc, "check_mm_alloc")
|
|
|
|
fails += forkedtest.normal_run(check_read_write_pipe, "check_read_write_pipe")
|
|
|
|
fails += forkedtest.normal_run(check_read_file, "check_read_file")
|
2019-11-26 04:12:37 +01:00
|
|
|
// check_print()
|
2019-12-07 20:25:19 +01:00
|
|
|
fails += forkedtest.normal_run(check_open_file_fail, "check_open_file_fail")
|
|
|
|
fails += forkedtest.normal_run(check_int_array_ro, "check_int_array_ro")
|
|
|
|
fails += forkedtest.normal_run(check_int_array_rw, "check_int_array_rw")
|
|
|
|
fails += forkedtest.normal_run(check_int64_array_ro, "check_int64_array_ro")
|
|
|
|
fails += forkedtest.normal_run(check_voidptr_array_ro, "check_voidptr_array_ro")
|
|
|
|
fails += forkedtest.normal_run(check_voidptr_array_rw, "check_voidptr_array_rw")
|
|
|
|
|
|
|
|
assert fails == 0
|
2019-11-23 17:35:57 +01:00
|
|
|
sys_exit(0)
|
|
|
|
}
|