Skip to content

Commit

Permalink
standalone_miri - Add hacky printf support
Browse files Browse the repository at this point in the history
  • Loading branch information
thepowersgang committed Oct 1, 2023
1 parent f213e87 commit 0b0c915
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tools/standalone_miri/miri_extern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@ extern "C" {
ssize_t write(int, const void*, size_t);
}
#endif

// A very simple implementation of `printf`-style formatting, with internal checks
::std::string format_string(const char* fmt, const ::std::vector<Value>& args, size_t cur_arg) {
::std::stringstream output;
for(const char* s = fmt; *s; s++) {
if( *s == '%' ) {
s ++;
if( *s == '%' ) {
output << '%';
continue;
}
switch(*s)
{
case 'i':
case 'd':
output << std::dec << args.at(cur_arg).read_i32(0);
cur_arg += 1;
break;
case 'x':
output << std::hex << args.at(cur_arg).read_i32(0);
cur_arg += 1;
break;
case 'p':
output << std::hex << "0x" << args.at(cur_arg).read_usize(0);
cur_arg += 1;
break;
default:
LOG_FATAL("Malformed printf string");
}
}
else {
output << *s;
}
}
return output.str();
}

bool InterpreterThread::call_extern(Value& rv, const ::std::string& link_name, const ::std::string& abi, ::std::vector<Value> args)
{
struct FfiHelpers {
Expand Down Expand Up @@ -892,6 +929,13 @@ bool InterpreterThread::call_extern(Value& rv, const ::std::string& link_name, c
rv = std::move(args.at(1));
}
}
else if( link_name == "printf" )
{
const auto* fmt = FfiHelpers::read_cstr(args.at(0), 0);
auto out = format_string(fmt, args, 1);
::std::cout << out;
rv = Value::new_i32(out.size());
}
// Allocators!
else
{
Expand Down

0 comments on commit 0b0c915

Please sign in to comment.