diff options
author | John Hodge <tpg@ucc.asn.au> | 2019-08-04 09:53:23 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2019-08-04 09:53:23 +0800 |
commit | 1cae9e657932e999d525c45ccaef325705319975 (patch) | |
tree | b39ac4cf553a7d779b10c8ee31d2bdd602270375 /tools | |
parent | 8ae530e20301e32fe7ef9b539e502cc529805910 (diff) | |
download | mrust-1cae9e657932e999d525c45ccaef325705319975.tar.gz |
Standalone MIRI - `read` FFI
Diffstat (limited to 'tools')
-rw-r--r-- | tools/standalone_miri/miri.cpp | 27 | ||||
-rw-r--r-- | tools/standalone_miri/value.cpp | 17 | ||||
-rw-r--r-- | tools/standalone_miri/value.hpp | 6 |
3 files changed, 47 insertions, 3 deletions
diff --git a/tools/standalone_miri/miri.cpp b/tools/standalone_miri/miri.cpp index f6ea1125..8d9f31ed 100644 --- a/tools/standalone_miri/miri.cpp +++ b/tools/standalone_miri/miri.cpp @@ -20,6 +20,7 @@ #endif #include <sys/stat.h> #include <fcntl.h> +#include <unistd.h> #undef DEBUG unsigned ThreadState::s_next_tls_key = 1; @@ -1915,6 +1916,30 @@ bool InterpreterThread::call_extern(Value& rv, const ::std::string& link_name, c rv = Value::new_isize(val); } + else if( link_name == "read" ) + { + auto fd = args.at(0).read_i32(0); + auto count = args.at(2).read_isize(0); + auto buf_vr = args.at(1).read_pointer_valref_mut(0, count); + + LOG_DEBUG("read(" << fd << ", " << buf_vr.data_ptr_mut() << ", " << count << ")"); + ssize_t val = read(fd, buf_vr.data_ptr_mut(), count); + LOG_DEBUG("= " << val); + + if( val > 0 ) + { + buf_vr.mark_bytes_valid(0, val); + } + + rv = Value::new_isize(val); + } + else if( link_name == "close" ) + { + auto fd = args.at(0).read_i32(0); + LOG_DEBUG("close(" << fd << ")"); + // TODO: Ensure that this FD is from the set known by the FFI layer + close(fd); + } else if( link_name == "sysconf" ) { auto name = args.at(0).read_i32(0); @@ -2224,7 +2249,7 @@ bool InterpreterThread::call_intrinsic(Value& rv, const RcString& name, const :: { rv = Value(); } - else if( name == "atomic_store" ) + else if( name == "atomic_store" || name == "atomic_store_relaxed" ) { auto& ptr_val = args.at(0); auto& data_val = args.at(1); diff --git a/tools/standalone_miri/value.cpp b/tools/standalone_miri/value.cpp index 363d980b..01f0fbbc 100644 --- a/tools/standalone_miri/value.cpp +++ b/tools/standalone_miri/value.cpp @@ -962,6 +962,23 @@ extern ::std::ostream& operator<<(::std::ostream& os, const ValueRef& v) return os; } +void ValueRef::mark_bytes_valid(size_t ofs, size_t size) +{ + if( m_alloc ) { + switch(m_alloc.get_ty()) + { + case RelocationPtr::Ty::Allocation: + m_alloc.alloc().mark_bytes_valid(m_offset + ofs, size); + break; + default: + LOG_TODO("mark_valid in " << m_alloc); + } + } + else { + m_value->mark_bytes_valid(m_offset + ofs, size); + } +} + Value ValueRef::read_value(size_t ofs, size_t size) const { if( size == 0 ) diff --git a/tools/standalone_miri/value.hpp b/tools/standalone_miri/value.hpp index 5e5ccc4f..ebae2699 100644 --- a/tools/standalone_miri/value.hpp +++ b/tools/standalone_miri/value.hpp @@ -365,7 +365,7 @@ struct ValueRef: { struct H { static bool in_bounds(size_t ofs, size_t size, size_t max_size) { - if( !(ofs < max_size) ) + if( ofs > 0 && !(ofs < max_size) ) return false; if( !(size <= max_size) ) return false; @@ -448,6 +448,8 @@ struct ValueRef: return nullptr; } } + + // TODO: Remove these two (move to a helper?) uint8_t* data_ptr_mut() { if( m_alloc ) { switch(m_alloc.get_ty()) @@ -466,7 +468,7 @@ struct ValueRef: return nullptr; } } - void mark_valid(size_t ofs, size_t size); + void mark_bytes_valid(size_t ofs, size_t size); void read_bytes(size_t ofs, void* dst, size_t size) const { if( size == 0 ) |