summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/standalone_miri/main.cpp46
-rw-r--r--tools/standalone_miri/miri.cpp23
-rw-r--r--tools/standalone_miri/value.cpp13
-rw-r--r--tools/standalone_miri/value.hpp5
4 files changed, 79 insertions, 8 deletions
diff --git a/tools/standalone_miri/main.cpp b/tools/standalone_miri/main.cpp
index 2011edfa..1755a9d7 100644
--- a/tools/standalone_miri/main.cpp
+++ b/tools/standalone_miri/main.cpp
@@ -14,10 +14,15 @@ struct ProgramOptions
{
::std::string infile;
//TODO: Architecture file
+ //::std::string archname;
//TODO: Loadable FFI descriptions
+ //::std::vector<const char*> ffi_api_files;
//TODO: Logfile
+ //::std::string logfile;
+ ::std::vector<const char*> args;
int parse(int argc, const char* argv[]);
+ void show_help(const char* prog) const;
};
int main(int argc, const char* argv[])
@@ -38,8 +43,19 @@ int main(int argc, const char* argv[])
argv_ty.wrappers.push_back(TypeWrapper { TypeWrapper::Ty::Pointer, 0 });
argv_ty.wrappers.push_back(TypeWrapper { TypeWrapper::Ty::Pointer, 0 });
auto val_argv = Value(argv_ty);
- val_argc.write_isize(0, 0);
+
+ // Create argc/argv based on input arguments
+ val_argc.write_isize(0, 1 + opts.args.size());
+ auto argv_alloc = Allocation::new_alloc((1 + opts.args.size()) * POINTER_SIZE);
+ argv_alloc->write_usize(0 * POINTER_SIZE, 0);
+ argv_alloc->relocations.push_back({ 0 * POINTER_SIZE, RelocationPtr::new_ffi(FFIPointer { "", (void*)(opts.infile.c_str()), opts.infile.size() + 1 }) });
+ for(size_t i = 0; i < opts.args.size(); i ++)
+ {
+ argv_alloc->write_usize((1 + i) * POINTER_SIZE, 0);
+ argv_alloc->relocations.push_back({ (1 + i) * POINTER_SIZE, RelocationPtr::new_ffi({ "", (void*)(opts.args[0]), ::std::strlen(opts.args[0]) + 1 }) });
+ }
val_argv.write_usize(0, 0);
+ val_argv.allocation->relocations.push_back({ 0 * POINTER_SIZE, RelocationPtr::new_alloc(argv_alloc) });
try
{
@@ -85,16 +101,37 @@ int ProgramOptions::parse(int argc, const char* argv[])
}
else
{
- // TODO: Too many free arguments
+ this->args.push_back(arg);
}
}
else if( arg[1] != '-' )
{
// Short
+ if( arg[2] != '\0' ) {
+ // Error?
+ }
+ switch(arg[1])
+ {
+ case 'h':
+ this->show_help(argv[0]);
+ exit(0);
+ default:
+ // TODO: Error
+ break;
+ }
}
else if( arg[2] != '\0' )
{
// Long
+ if( ::std::strcmp(arg, "--help") == 0 ) {
+ this->show_help(argv[0]);
+ exit(0);
+ }
+ //else if( ::std::strcmp(arg, "--api") == 0 ) {
+ //}
+ else {
+ // TODO: Error
+ }
}
else
{
@@ -103,3 +140,8 @@ int ProgramOptions::parse(int argc, const char* argv[])
}
return 0;
}
+
+void ProgramOptions::show_help(const char* prog) const
+{
+ ::std::cout << "USAGE: " << prog << " <infile> <... args>" << ::std::endl;
+}
diff --git a/tools/standalone_miri/miri.cpp b/tools/standalone_miri/miri.cpp
index f7e47a6b..eccc54c9 100644
--- a/tools/standalone_miri/miri.cpp
+++ b/tools/standalone_miri/miri.cpp
@@ -1856,6 +1856,24 @@ bool InterpreterThread::call_extern(Value& rv, const ::std::string& link_name, c
rv.write_usize(0, 0);
}
}
+ else if( link_name == "strlen" )
+ {
+ // strlen - custom implementation to ensure validity
+ bool _is_mut;
+ size_t size;
+ const char* ptr = reinterpret_cast<const char*>( args.at(0).read_pointer_unsafe(0, 1, size, _is_mut) );
+ size_t len = 0;
+ while(size -- && *ptr)
+ {
+ ptr ++;
+ len ++;
+ }
+ args.at(0).read_pointer_const(0, len + 1);
+
+ //rv = Value::new_usize(len);
+ rv = Value(::HIR::TypeRef(RawType::USize));
+ rv.write_usize(0, len);
+ }
// Allocators!
else
{
@@ -2222,7 +2240,10 @@ bool InterpreterThread::call_intrinsic(Value& rv, const ::std::string& name, con
LOG_FATAL("Attempt to copy* a function");
break;
case RelocationPtr::Ty::FfiPointer:
- LOG_BUG("Trying to copy from a FFI pointer");
+ LOG_ASSERT(src_ofs <= src_alloc.ffi().size, "");
+ LOG_ASSERT(byte_count <= src_alloc.ffi().size, "");
+ LOG_ASSERT(src_ofs + byte_count <= src_alloc.ffi().size, "");
+ dst_alloc.alloc().write_bytes(dst_ofs, src_alloc.ffi().ptr_value + src_ofs, byte_count);
break;
}
}
diff --git a/tools/standalone_miri/value.cpp b/tools/standalone_miri/value.cpp
index d8eeee01..e9376ce6 100644
--- a/tools/standalone_miri/value.cpp
+++ b/tools/standalone_miri/value.cpp
@@ -227,13 +227,16 @@ void* ValueCommonRead::read_pointer_unsafe(size_t rd_ofs, size_t req_valid, size
}
case RelocationPtr::Ty::Function:
LOG_FATAL("read_pointer w/ function");
- case RelocationPtr::Ty::FfiPointer:
- if( req_valid )
- LOG_FATAL("Can't request valid data from a FFI pointer");
+ case RelocationPtr::Ty::FfiPointer: {
+ const auto& f = reloc.ffi();
+ // TODO: Validity?
+ //if( req_valid )
+ // LOG_FATAL("Can't request valid data from a FFI pointer");
// TODO: Have an idea of mutability and available size from FFI
- out_size = 0;
+ out_size = f.size - ofs;
out_is_mut = false;
- return reloc.ffi().ptr_value /* + ofs */;
+ return reloc.ffi().ptr_value + ofs;
+ }
}
throw "";
}
diff --git a/tools/standalone_miri/value.hpp b/tools/standalone_miri/value.hpp
index 7219f1f7..4528b98f 100644
--- a/tools/standalone_miri/value.hpp
+++ b/tools/standalone_miri/value.hpp
@@ -315,6 +315,11 @@ struct ValueRef:
assert(size <= m_alloc.str().size());
assert(ofs+size <= m_alloc.str().size());
break;
+ case RelocationPtr::Ty::FfiPointer:
+ assert(ofs < m_alloc.ffi().size);
+ assert(size <= m_alloc.ffi().size);
+ assert(ofs+size <= m_alloc.ffi().size);
+ break;
default:
throw "TODO";
}