From c7b3036cefcd0dc412cb400455324d7ca8cd518e Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 13 May 2018 09:55:13 +0800 Subject: Standalone MIRI - memrchr and better null checking --- tools/standalone_miri/main.cpp | 22 ++++++++++++++++++++++ tools/standalone_miri/value.cpp | 3 +++ 2 files changed, 25 insertions(+) (limited to 'tools') diff --git a/tools/standalone_miri/main.cpp b/tools/standalone_miri/main.cpp index 16cfd972..4ee503d3 100644 --- a/tools/standalone_miri/main.cpp +++ b/tools/standalone_miri/main.cpp @@ -1857,6 +1857,28 @@ Value MIRI_Invoke_Extern(ModuleTree& modtree, ThreadState& thread, const ::std:: } return rv; } + else if( link_name == "memrchr" ) + { + auto ptr_alloc = args.at(0).get_relocation(0); + auto c = args.at(1).read_i32(0); + auto n = args.at(2).read_usize(0); + const void* ptr = args.at(0).read_pointer_const(0, n); + + const void* ret = memrchr(ptr, c, n); + + auto rv = Value(::HIR::TypeRef(RawType::USize)); + rv.create_allocation(); + if( ret ) + { + rv.write_usize(0, args.at(0).read_usize(0) + ( static_cast(ret) - static_cast(ptr) )); + rv.allocation.alloc().relocations.push_back({ 0, ptr_alloc }); + } + else + { + rv.write_usize(0, 0); + } + return rv; + } // Allocators! else { diff --git a/tools/standalone_miri/value.cpp b/tools/standalone_miri/value.cpp index 468425e9..db352019 100644 --- a/tools/standalone_miri/value.cpp +++ b/tools/standalone_miri/value.cpp @@ -169,6 +169,9 @@ void* ValueCommon::read_pointer_unsafe(size_t rd_ofs, size_t req_valid, size_t& if( ofs != 0 ) { LOG_FATAL("Read a non-zero offset with no relocation"); } + if( req_valid > 0 ) { + LOG_ERROR("Attempting to read a null pointer"); + } out_is_mut = false; out_size = 0; return nullptr; -- cgit v1.2.3