Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
98 changes: 98 additions & 0 deletions libcc2rs/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,104 @@ pub fn fread_refcount(a0: AnyPtr, a1: u64, a2: u64, a3: Ptr<::std::fs::File>) ->
(read_bytes / a1 as usize) as u64
}

pub fn fwrite_refcount(a0: AnyPtr, a1: u64, a2: u64, a3: Ptr<::std::fs::File>) -> u64 {
let total = a1.saturating_mul(a2) as usize;
let mut src = a0
.cast::<u8>()
.expect("fwrite: only supporting u8 pointers")
.clone();

let f = (*a3.upgrade().deref())
.try_clone()
.expect("try_clone failed");
let mut writer = std::io::BufWriter::with_capacity(64 * 1024, f);

let mut written_bytes: usize = 0;
let mut buffer: [u8; 8192] = [0; 8192];

while written_bytes < total {
let remaining = total - written_bytes;
let to_fill = std::cmp::min(buffer.len(), remaining);

for b in buffer.iter_mut().take(to_fill) {
*b = src.read();
src = src.offset(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src += 1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

let mut off = 0;
while off < to_fill {
match std::io::Write::write(&mut writer, &buffer[off..to_fill]) {
Ok(0) => break,
Ok(n) => off += n,
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) => panic!("Unhandled error in fwrite: {e}"),
}
}

if off == 0 {
break;
}

written_bytes += off;
}

std::io::Write::flush(&mut writer).expect("flush failed");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think fwrite flushes the stream

@lucic71 lucic71 May 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted the flush


(written_bytes / a1 as usize) as u64
}

/// # Safety
///
/// `a0` must point to a readable buffer of at least `a1 * a2` bytes, and `a3`
/// must point to a valid, open `std::fs::File`.
pub unsafe fn fwrite_unsafe(
a0: *const ::std::ffi::c_void,
a1: u64,
a2: u64,
a3: *mut ::std::fs::File,
) -> u64 {
let total = a1.saturating_mul(a2) as usize;
let mut src = a0 as *const u8;

let f = unsafe { (*a3).try_clone().expect("try_clone failed") };
let mut writer = std::io::BufWriter::with_capacity(64 * 1024, f);

let mut written_bytes: usize = 0;
let mut buffer: [u8; 8192] = [0; 8192];

while written_bytes < total {
let remaining = total - written_bytes;
let to_fill = std::cmp::min(buffer.len(), remaining);

for b in buffer.iter_mut().take(to_fill) {
unsafe {
*b = *src;
src = src.offset(1);
}
}

let mut off = 0;
while off < to_fill {
match std::io::Write::write(&mut writer, &buffer[off..to_fill]) {
Ok(0) => break,
Ok(n) => off += n,
Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) => panic!("Unhandled error in fwrite: {e}"),
}
}

if off == 0 {
break;
}

written_bytes += off;
}

std::io::Write::flush(&mut writer).expect("flush failed");

(written_bytes / a1 as usize) as u64
}

/// # Safety
///
/// `a0` must point to a writable buffer of at least `a1 * a2` bytes, and `a3`
Expand Down
216 changes: 19 additions & 197 deletions rules/stdio/ir_refcount.json
Original file line number Diff line number Diff line change
Expand Up @@ -503,234 +503,56 @@
"f6": {
"body": [
{
"text": "let total = "
"text": "libcc2rs::fwrite_refcount("
},
{
"method_call": {
"receiver": [
{
"placeholder": {
"arg": 1,
"access": "read"
}
}
],
"body": [
{
"text": ".saturating_mul("
},
{
"placeholder": {
"arg": 2,
"access": "read"
}
},
{
"text": ")"
}
]
}
},
{
"text": " as usize;\n let mut src = "
},
{
"method_call": {
"receiver": [
{
"method_call": {
"receiver": [
{
"method_call": {
"receiver": [
{
"placeholder": {
"arg": 0,
"access": "read"
}
}
],
"body": [
{
"text": ".cast::<u8>()"
}
]
}
}
],
"body": [
{
"text": ".expect(\"fwrite: only supporting u8 pointers\")"
}
]
}
}
],
"body": [
{
"text": ".clone()"
}
]
}
},
{
"text": ";\n\n let mut f = "
},
{
"method_call": {
"receiver": [
{
"method_call": {
"receiver": [
{
"text": "(*"
},
{
"method_call": {
"receiver": [
{
"method_call": {
"receiver": [
{
"placeholder": {
"arg": 3,
"access": "read"
}
}
],
"body": [
{
"text": ".upgrade()"
}
]
}
}
],
"body": [
{
"text": ".deref()"
}
]
}
},
{
"text": ")"
}
],
"body": [
{
"text": ".try_clone()"
}
]
}
}
],
"body": [
{
"text": ".expect(\"try_clone failed\")"
}
]
}
},
{
"text": ";\n let mut writer = std::io::BufWriter::with_capacity(64 * 1024, f);\n\n let mut written_bytes: usize = 0;\n let mut buffer: [u8; 8192] = [0; 8192];\n\n while written_bytes < total {\n let remaining = total - written_bytes;\n let to_fill = std::cmp::min("
},
{
"method_call": {
"receiver": [
{
"text": "buffer"
}
],
"body": [
{
"text": ".len()"
}
]
}
},
{
"text": ", remaining);\n\n for i in 0..to_fill {\n buffer[i] = "
},
{
"method_call": {
"receiver": [
{
"text": "src"
}
],
"body": [
{
"text": ".read()"
}
]
"placeholder": {
"arg": 0,
"access": "read"
}
},
{
"text": ";\n src = "
"text": ", "
},
{
"method_call": {
"receiver": [
{
"text": "src"
}
],
"body": [
{
"text": ".offset(1)"
}
]
"placeholder": {
"arg": 1,
"access": "read"
}
},
{
"text": ";\n }\n\n let mut off = 0;\n while off < to_fill {\n match std::io::Write::write(&mut writer, &buffer[off..to_fill]) {\n Ok(0) => break,\n Ok(n) => off += n,\n Err(ref e) if "
"text": ", "
},
{
"method_call": {
"receiver": [
{
"text": "e"
}
],
"body": [
{
"text": ".kind()"
}
]
"placeholder": {
"arg": 2,
"access": "read"
}
},
{
"text": " == std::io::ErrorKind::Interrupted => continue,\n Err(e) => panic!(\"Unhandled error in fwrite: {e}\"),\n }\n }\n\n if off == 0 {\n break;\n }\n\n written_bytes += off;\n }\n\n "
"text": ", "
},
{
"method_call": {
"receiver": [
{
"text": "std::io::Write::flush(&mut writer)"
"placeholder": {
"arg": 3,
"access": "read"
}
}
],
"body": [
{
"text": ".expect(\"flush failed\")"
"text": ".clone()"
}
]
}
},
{
"text": ";\n\n (written_bytes / "
},
{
"placeholder": {
"arg": 1,
"access": "read"
}
},
{
"text": " as usize) as u64"
"text": ")"
}
],
"multi_statement": true,
"params": {
"a0": {
"type": "AnyPtr"
Expand Down
Loading
Loading