From 7c158731b0cf4c973ac2afc632c6fa606c53b8c4 Mon Sep 17 00:00:00 2001 From: DarkSharpness <2040703891@qq.com> Date: Tue, 16 Dec 2025 02:28:36 +0800 Subject: [PATCH] fix: fix libc io ret value Co-authored-by: wzy --- src/libc/inout.cpp | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/libc/inout.cpp b/src/libc/inout.cpp index 048a6c9..a636697 100644 --- a/src/libc/inout.cpp +++ b/src/libc/inout.cpp @@ -125,9 +125,7 @@ auto puts(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { auto ptr = rf[Register::a0]; auto str = checked_get_string<_Index::puts>(mem, ptr); dev.out << str << '\n'; - dev.counter.libcIO += kLibcOverhead + io(str.size() + 1); - return return_to_user(rf, mem, 0); } @@ -135,7 +133,7 @@ auto putchar(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { auto c = rf[Register::a0]; dev.out.put(static_cast(c)); dev.counter.libcIO += kLibcOverhead + io(1); - return return_to_user(rf, mem, 0); + return return_to_user(rf, mem, c); } auto printf(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { @@ -143,31 +141,24 @@ auto printf(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { auto fmt = checked_get_string<_Index::printf>(mem, ptr); auto os = std::stringstream{}; checked_printf_impl<_Index::printf>(rf, mem, os, fmt, Register::a1); - auto str = std::move(os).str(); dev.out << str; - dev.counter.libcIO += kLibcOverhead + io(str.size()) + op(fmt.size()); - - return return_to_user(rf, mem, 0); + return return_to_user(rf, mem, str.size()); } auto sprintf(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { auto ptr0 = rf[Register::a0]; auto ptr1 = rf[Register::a1]; auto fmt = checked_get_string<_Index::sprintf>(mem, ptr1); - - auto os = std::stringstream{}; + auto os = std::stringstream{}; checked_printf_impl<_Index::sprintf>(rf, mem, os, fmt, Register::a2); - auto str = std::move(os).str(); auto raw = checked_get_area<_Index::sprintf>(mem, ptr0, str.size() + 1); std::memcpy(raw, str.data(), str.size() + 1); - // Format time + IO time dev.counter.libcOp += kLibcOverhead + io(str.size()) + op(fmt.size()); - - return return_to_user(rf, mem, ptr0); + return return_to_user(rf, mem, str.size()); } auto getchar(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { @@ -181,24 +172,19 @@ auto scanf(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { auto fmt = checked_get_string<_Index::scanf>(mem, ptr); auto &is = dev.in; auto result = checked_scanf_impl<_Index::scanf>(rf, mem, is, fmt, Register::a1); - dev.counter.libcIO += kLibcOverhead + io(result.second) + op(fmt.size()); - return return_to_user(rf, mem, result.first); } auto sscanf(Executable &, RegisterFile &rf, Memory &mem, Device &dev) -> Hint { - auto ptr0 = rf[Register::a0]; - auto ptr1 = rf[Register::a1]; - auto str = checked_get_string<_Index::sscanf>(mem, ptr0); - auto fmt = checked_get_string<_Index::sscanf>(mem, ptr1); - + auto ptr0 = rf[Register::a0]; + auto ptr1 = rf[Register::a1]; + auto str = checked_get_string<_Index::sscanf>(mem, ptr0); + auto fmt = checked_get_string<_Index::sscanf>(mem, ptr1); auto is = std::stringstream{std::string(str)}; auto result = checked_scanf_impl<_Index::sscanf>(rf, mem, is, fmt, Register::a2); - // Format time + IO time dev.counter.libcOp += kLibcOverhead + op(result.second) + op(fmt.size()); - return return_to_user(rf, mem, result.first); }