|
| 1 | +/** This file contains functionality related to handling mutexes. */ |
| 2 | + |
| 3 | +#include "MiriInterface.hpp" |
| 4 | + |
| 5 | +// CXX.rs generated headers: |
| 6 | +#include "genmc-sys/src/lib.rs.h" |
| 7 | + |
| 8 | +auto MiriGenmcShim::handle_mutex_lock(ThreadId thread_id, uint64_t address, uint64_t size) |
| 9 | + -> MutexLockResult { |
| 10 | + // FIXME(genmc,multithreading): ensure this annotation id is unique even if Miri runs GenMC mode |
| 11 | + // multithreaded. We cannot use the address directly, since it is 64 bits, and `ID` is an |
| 12 | + // `unsigned int`. |
| 13 | + ModuleID::ID annot_id; |
| 14 | + auto [it, inserted] = annotation_id.try_emplace(address, annotation_id_counter); |
| 15 | + if (inserted) { |
| 16 | + annot_id = annotation_id_counter++; |
| 17 | + } else { |
| 18 | + annot_id = it->second; |
| 19 | + } |
| 20 | + const auto annot = std::move(Annotation( |
| 21 | + AssumeType::Spinloop, |
| 22 | + Annotation::ExprVP(NeExpr<AnnotID>::create( |
| 23 | + RegisterExpr<AnnotID>::create(ASize(size).getBits(), annot_id), |
| 24 | + ConcreteExpr<AnnotID>::create(ASize(size).getBits(), SVal(1)) |
| 25 | + ) |
| 26 | + .release()) |
| 27 | + )); |
| 28 | + |
| 29 | + // Mutex starts out unlocked, so we always say the previous value is "unlocked". |
| 30 | + const auto old_val = SVal(0); |
| 31 | + const auto load_ret = handle_load_reset_if_none<EventLabel::EventLabelKind::LockCasRead>( |
| 32 | + thread_id, |
| 33 | + old_val, |
| 34 | + address, |
| 35 | + size, |
| 36 | + annot, |
| 37 | + EventDeps() |
| 38 | + ); |
| 39 | + if (const auto* err = std::get_if<VerificationError>(&load_ret)) |
| 40 | + return MutexLockResultExt::from_error(format_error(*err)); |
| 41 | + // If we get a `Reset`, GenMC decided that this lock operation should not yet run, since it |
| 42 | + // would not acquire the mutex. Like the handling of the case further down where we read a `1` |
| 43 | + // ("Mutex already locked"), Miri should call the handle function again once the current thread |
| 44 | + // is scheduled by GenMC the next time. |
| 45 | + if (std::holds_alternative<Reset>(load_ret)) |
| 46 | + return MutexLockResultExt::ok(false); |
| 47 | + |
| 48 | + const auto* ret_val = std::get_if<SVal>(&load_ret); |
| 49 | + ERROR_ON(!ret_val, "Unimplemented: mutex lock returned unexpected result."); |
| 50 | + ERROR_ON(*ret_val != SVal(0) && *ret_val != SVal(1), "Mutex read value was neither 0 nor 1"); |
| 51 | + const bool is_lock_acquired = *ret_val == SVal(0); |
| 52 | + if (is_lock_acquired) { |
| 53 | + const auto store_ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::LockCasWrite>( |
| 54 | + inc_pos(thread_id), |
| 55 | + old_val, |
| 56 | + address, |
| 57 | + size, |
| 58 | + EventDeps() |
| 59 | + ); |
| 60 | + if (const auto* err = std::get_if<VerificationError>(&store_ret)) |
| 61 | + return MutexLockResultExt::from_error(format_error(*err)); |
| 62 | + // We don't update Miri's memory for this operation so we don't need to know if the store |
| 63 | + // was the co-maximal store, but we still check that we at least get a boolean as the result |
| 64 | + // of the store. |
| 65 | + const bool* is_coherence_order_maximal_write = std::get_if<bool>(&store_ret); |
| 66 | + ERROR_ON( |
| 67 | + nullptr == is_coherence_order_maximal_write, |
| 68 | + "Unimplemented: store part of mutex try_lock returned unexpected result." |
| 69 | + ); |
| 70 | + } else { |
| 71 | + // We did not acquire the mutex, so we tell GenMC to block the thread until we can acquire |
| 72 | + // it. |
| 73 | + GenMCDriver::handleAssume(inc_pos(thread_id), AssumeType::Spinloop); |
| 74 | + } |
| 75 | + return MutexLockResultExt::ok(is_lock_acquired); |
| 76 | +} |
| 77 | + |
| 78 | +auto MiriGenmcShim::handle_mutex_try_lock(ThreadId thread_id, uint64_t address, uint64_t size) |
| 79 | + -> MutexLockResult { |
| 80 | + auto& currPos = threads_action_[thread_id].event; |
| 81 | + // Mutex starts out unlocked, so we always say the previous value is "unlocked". |
| 82 | + const auto old_val = SVal(0); |
| 83 | + const auto load_ret = GenMCDriver::handleLoad<EventLabel::EventLabelKind::TrylockCasRead>( |
| 84 | + ++currPos, |
| 85 | + old_val, |
| 86 | + SAddr(address), |
| 87 | + ASize(size) |
| 88 | + ); |
| 89 | + if (const auto* err = std::get_if<VerificationError>(&load_ret)) |
| 90 | + return MutexLockResultExt::from_error(format_error(*err)); |
| 91 | + const auto* ret_val = std::get_if<SVal>(&load_ret); |
| 92 | + if (nullptr == ret_val) { |
| 93 | + ERROR("Unimplemented: mutex trylock load returned unexpected result."); |
| 94 | + } |
| 95 | + |
| 96 | + ERROR_ON(*ret_val != SVal(0) && *ret_val != SVal(1), "Mutex read value was neither 0 nor 1"); |
| 97 | + const bool is_lock_acquired = *ret_val == SVal(0); |
| 98 | + if (!is_lock_acquired) { |
| 99 | + return MutexLockResultExt::ok(false); /* Lock already held. */ |
| 100 | + } |
| 101 | + |
| 102 | + const auto store_ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::TrylockCasWrite>( |
| 103 | + ++currPos, |
| 104 | + old_val, |
| 105 | + SAddr(address), |
| 106 | + ASize(size) |
| 107 | + ); |
| 108 | + if (const auto* err = std::get_if<VerificationError>(&store_ret)) |
| 109 | + return MutexLockResultExt::from_error(format_error(*err)); |
| 110 | + // We don't update Miri's memory for this operation so we don't need to know if the store was |
| 111 | + // co-maximal, but we still check that we get a boolean result. |
| 112 | + const bool* is_coherence_order_maximal_write = std::get_if<bool>(&store_ret); |
| 113 | + ERROR_ON( |
| 114 | + nullptr == is_coherence_order_maximal_write, |
| 115 | + "Unimplemented: store part of mutex try_lock returned unexpected result." |
| 116 | + ); |
| 117 | + return MutexLockResultExt::ok(true); |
| 118 | +} |
| 119 | + |
| 120 | +auto MiriGenmcShim::handle_mutex_unlock(ThreadId thread_id, uint64_t address, uint64_t size) |
| 121 | + -> StoreResult { |
| 122 | + const auto pos = inc_pos(thread_id); |
| 123 | + const auto ret = GenMCDriver::handleStore<EventLabel::EventLabelKind::UnlockWrite>( |
| 124 | + pos, |
| 125 | + // Mutex starts out unlocked, so we always say the previous value is "unlocked". |
| 126 | + /* old_val */ SVal(0), |
| 127 | + MemOrdering::Release, |
| 128 | + SAddr(address), |
| 129 | + ASize(size), |
| 130 | + AType::Signed, |
| 131 | + SVal(0), |
| 132 | + EventDeps() |
| 133 | + ); |
| 134 | + if (const auto* err = std::get_if<VerificationError>(&ret)) |
| 135 | + return StoreResultExt::from_error(format_error(*err)); |
| 136 | + const bool* is_coherence_order_maximal_write = std::get_if<bool>(&ret); |
| 137 | + ERROR_ON( |
| 138 | + nullptr == is_coherence_order_maximal_write, |
| 139 | + "Unimplemented: store part of mutex unlock returned unexpected result." |
| 140 | + ); |
| 141 | + return StoreResultExt::ok(*is_coherence_order_maximal_write); |
| 142 | +} |
0 commit comments