Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 299 additions & 0 deletions rules/stdio/ir_unsafe.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,305 @@
"is_unsafe_pointer": true
}
},
"f11": {
"body": [
{
"text": "match "
},
{
"method_call": {
"receiver": [
{
"text": "(*"
},
{
"placeholder": {
"arg": 1,
"access": "write"
}
},
{
"text": ")"
}
],
"body": [
{
"text": ".write_all(&["
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": " as u8])"
}
]
}
},
{
"text": " {\n Ok(()) => "
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": " & 0xff,\n Err(_) => -1,\n }"
}
],
"params": {
"a0": {
"type": "i32"
},
"a1": {
"type": "*mut ::std::fs::File",
"is_unsafe_pointer": true
}
},
"return_type": {
"type": "i32"
}
},
"f12": {
"body": [
{
"text": "let bytes = "
},
{
"method_call": {
"receiver": [
{
"text": "std::ffi::CStr::from_ptr("
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": " as *const i8)"
}
],
"body": [
{
"text": ".to_bytes()"
}
]
}
},
{
"text": ";\n match "
},
{
"method_call": {
"receiver": [
{
"text": "(*"
},
{
"placeholder": {
"arg": 1,
"access": "write"
}
},
{
"text": ")"
}
],
"body": [
{
"text": ".write_all(bytes)"
}
]
}
},
{
"text": " {\n Ok(()) => 0,\n Err(_) => -1,\n }"
}
],
"multi_statement": true,
"params": {
"a0": {
"type": "*const u8",
"is_unsafe_pointer": true
},
"a1": {
"type": "*mut ::std::fs::File",
"is_unsafe_pointer": true
}
},
"return_type": {
"type": "i32"
}
},
"f13": {
"body": [
{
"text": "let bytes = "
},
{
"method_call": {
"receiver": [
{
"text": "std::ffi::CStr::from_ptr("
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": " as *const i8)"
}
],
"body": [
{
"text": ".to_bytes()"
}
]
}
},
{
"text": ";\n let stdout = libcc2rs::cout_unsafe();\n let r1 = "
},
{
"method_call": {
"receiver": [
{
"text": "(*stdout)"
}
],
"body": [
{
"text": ".write_all(bytes)"
}
]
}
},
{
"text": ";\n let r2 = "
},
{
"method_call": {
"receiver": [
{
"text": "(*stdout)"
}
],
"body": [
{
"text": ".write_all(b\"\\n\")"
}
]
}
},
{
"text": ";\n if "
},
{
"method_call": {
"receiver": [
{
"text": "r1"
}
],
"body": [
{
"text": ".is_ok()"
}
]
}
},
{
"text": " && "
},
{
"method_call": {
"receiver": [
{
"text": "r2"
}
],
"body": [
{
"text": ".is_ok()"
}
]
}
},
{
"text": " { 0 } else { -1 }"
}
],
"multi_statement": true,
"params": {
"a0": {
"type": "*const u8",
"is_unsafe_pointer": true
}
},
"return_type": {
"type": "i32"
}
},
"f14": {
"body": [
{
"text": "if "
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": " == libcc2rs::cin_unsafe() {\n 0\n } else if "
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": " == libcc2rs::cout_unsafe() {\n 1\n } else if "
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": " == libcc2rs::cerr_unsafe() {\n 2\n } else {\n ::std::os::fd::AsRawFd::as_raw_fd(&*"
},
{
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": ")\n }"
}
],
"params": {
"a0": {
"type": "*mut ::std::fs::File",
"is_unsafe_pointer": true
}
},
"return_type": {
"type": "i32"
}
},
"f2": {
"body": [
{
Expand Down
8 changes: 8 additions & 0 deletions rules/stdio/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ FILE *f8() { return stdout; }
FILE *f9() { return stderr; }

FILE *f10() { return stdin; }

int f11(int c, FILE *stream) { return fputc(c, stream); }

int f12(const char *s, FILE *stream) { return fputs(s, stream); }

int f13(const char *s) { return puts(s); }

int f14(FILE *stream) { return fileno(stream); }
35 changes: 35 additions & 0 deletions rules/stdio/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,38 @@ unsafe fn f9() -> *mut ::std::fs::File {
unsafe fn f10() -> *mut ::std::fs::File {
libcc2rs::cin_unsafe()
}

unsafe fn f11(a0: i32, a1: *mut ::std::fs::File) -> i32 {
match (*a1).write_all(&[a0 as u8]) {
Ok(()) => a0 & 0xff,
Err(_) => -1,
}
}

unsafe fn f12(a0: *const u8, a1: *mut ::std::fs::File) -> i32 {
let bytes = std::ffi::CStr::from_ptr(a0 as *const i8).to_bytes();
match (*a1).write_all(bytes) {
Ok(()) => 0,
Err(_) => -1,
}
}

unsafe fn f13(a0: *const u8) -> i32 {
let bytes = std::ffi::CStr::from_ptr(a0 as *const i8).to_bytes();
let stdout = libcc2rs::cout_unsafe();
let r1 = (*stdout).write_all(bytes);
let r2 = (*stdout).write_all(b"\n");
if r1.is_ok() && r2.is_ok() { 0 } else { -1 }
}

unsafe fn f14(a0: *mut ::std::fs::File) -> i32 {
if a0 == libcc2rs::cin_unsafe() {
0
} else if a0 == libcc2rs::cout_unsafe() {
1
} else if a0 == libcc2rs::cerr_unsafe() {
2
} else {
::std::os::fd::AsRawFd::as_raw_fd(&*a0)
}
}
Loading
Loading