#! @file races.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the race checker, which a run with its memory checked has once the #! program starts a thread: it stops the program where two threads reach the #! same memory, one of them writing it, and nothing orders the two, as #! ThreadSanitizer does. #! #! What orders them is what happened before: each thread has a clock of its own, #! and knows of each other thread's clock as far as what it saw of that thread, #! a vector of clocks. Starting a thread gives it what its starter knew; an #! atomic operation releasing gives what its thread knows to the address it #! writes, and one acquiring takes what the address was given, so a thread #! joining another, which waits for the other's `finished` with `acquire`, knows #! what the other did. A fence releases and acquires through one address of its #! own. A relaxed operation orders nothing. #! #! Of each word of memory read or written through a reference or pointer, the #! checker keeps its last write and the last two reads of different threads, #! each with the thread, its clock then, the bytes of the word, and where it #! was. A read races the last write of another thread the reader knows nothing #! of yet, and a write races that too and either read. Memory the program #! allocates anew forgets what was kept of it, as it holds nothing of before. #! What a thread's frame holds of its own is not watched, so only what threads #! reach of each other's frames through references is. import std::containers; import sakura::compiler; module sakura::machine; #! A read or a write of a word, as the race checker keeps it. struct race_access { var thread: u32; #! One more than the thread's number, or 0 for none. var clock: u32; #! The thread's own clock then. var bytes: u8; #! The bytes of the word, a bit each, the lowest first. var function: u32; #! The function reaching it. var pc: u32; #! Where in its code. } #! What the race checker keeps of a word: its last write, and the last reads of #! two threads. struct race_word { var wrote: race_access; var first: race_access; var second: race_access; } #! What the race checker keeps of the program and of the thread running. struct race_checker : owning { var on: bool; #! Whether the program started a thread, running with memory checked. var threads: u32; #! How many threads were numbered. var thread: u32; #! The number of the thread running. var clock: vector; #! What the thread running knows of each thread's clock, its own among them. var words: dict; #! What was kept of each word, by its address. var syncs: dict; #! The clocks each address an atomic operation released to was given, by where they are in `held`. var held: vector>; #! Those clocks. var text: string; #! What the program stopping says, naming where the other access was. } #! The address a fence releases and acquires through, which no memory has. const fence_address: u64 = 0; #! Makes a checker, which checks nothing until the program starts a thread. #! @returns The checker. def race_checker::new(): race_checker { return race_checker { clock = vector::::new(), words = dict::::new(), syncs = dict::::new(), held = vector::>::new(), text = string::new(nothing {}) }; } def race_checker::close(&mut this): void { this.clock.close(); this.words.close(); this.syncs.close(); for i in 0..this.held.length { this.held[i].close(); } this.held.close(); this.text.close(); } #! What the thread running knows of a thread's clock. #! @param this The checker. #! @param thread The thread's number. #! @returns Its clock, or 0 where it knows nothing of it. def race_checker::knows(&this, thread: u32): u32 { return thread < this.clock.length as u32 ? this.clock[thread as u64] : 0; } #! Takes what a clock knows into the thread running's: the later of each. #! @param this The checker. #! @param from The clock. def race_checker::join(&mut this, from: &vector): void { if this.clock.length < from.length { this.clock.resize(from.length, 0); } for i in 0..from.length { if from[i] > this.clock[i] { this.clock[i] = from[i]; } } } #! Whether an access was made before what the thread running knows of, so it #! is ordered before the thread's own: it is the thread's own, or none. #! @param this The checker. #! @param made The access. #! @returns Whether it is. def race_checker::before(&this, made: &race_access): bool { return made.thread == 0 || made.thread - 1 == this.thread || made.clock <= this.knows(made.thread - 1); } #! Begins checking as a thread starts another: the one running is numbered 0 #! where it is the first to, and the one started is given what its starter #! knows, and a clock of its own. The starter's own clock moves on, so what it #! does after is not known to the one started. #! @param this The checker. #! @param into The started thread's strand. pub def interpreter::race_start(&mut this, into: *mut strand): void { return if !this.options.checks; val races := &mut this.races; if !races.on { races.on = true; races.threads = 1; races.thread = 0; races.clock.clear(); races.clock.append(1); } val number := races.threads; races.threads++; into[0].racer = number; into[0].clock = vector::::new(); into[0].clock.extend(races.clock.span); into[0].clock.resize(number as u64 + 1, 0); into[0].clock[number as u64] = 1; races.clock[races.thread as u64]++; } #! What an atomic operation orders: where it acquires, the thread running takes #! what its address was given; where it releases, the address is given what the #! thread knows, and the thread's own clock moves on. #! @param this The machine. #! @param address The address, or anything for a fence. #! @param operation The operation, as `atomic_load` or `atomic_fence`. #! @param order Its order, as `core::order` numbers them. pub def interpreter::race_atomic(&mut this, address: u64, operation: u32, order: u64): void { return if !this.races.on; val at := operation == atomic_fence ? fence_address : address; val acquires := (order == order_acquire || order == order_acq_rel || order == order_seq_cst) && operation != atomic_store; val releases := (order == order_release || order == order_acq_rel || order == order_seq_cst) && operation != atomic_load; val races := &mut this.races; if acquires { if races.syncs.get(&at) is some(index) { races.join(&races.held[index as u64]); } } if releases { var index: u32 = 0; if races.syncs.get(&at) is some(found) { index = found; } else { index = races.held.length as u32; races.held.append(vector::::new()); races.syncs.set(at, index); } val given := &mut races.held[index as u64]; if given.length < races.clock.length { given.resize(races.clock.length, 0); } for i in 0..races.clock.length { if races.clock[i] > given[i] { given[i] = races.clock[i]; } } races.clock[races.thread as u64]++; } } #! Checks a read or a write through a reference or pointer against what other #! threads did of the words it reaches, then keeps it. #! @param this The machine. #! @param address The first byte. #! @param size How many bytes. #! @param writes Whether it writes. #! @param function The function reaching it. #! @param pc Where in its code. #! @returns `race` where it races another thread's, or `none`. pub def interpreter::race_access(&mut this, address: u64, size: u64, writes: bool, function: u32, pc: u64): halt { return halt::none if !this.races.on || size == 0; var word := address & ~7u; val end := address + size; while word < end { val from := address > word ? address - word : 0; val to := end < word + 8 ? end - word : 8; val bytes := (((1u << (to - from)) - 1) << from) as u8; val made := race_access { thread = this.races.thread + 1, clock = this.races.knows(this.races.thread), bytes = bytes, function = function, pc = pc as u32 }; val why := this.race_word(word, &made, writes); return why if why != halt::none; word += 8; } return halt::none; } #! Checks an access of one word against what was kept of it, then keeps it. #! @param this The machine. #! @param word The word's address. #! @param made The access. #! @param writes Whether it writes. #! @returns `race` where it races another thread's, or `none`. def interpreter::race_word(&mut this, word: u64, made: &race_access, writes: bool): halt { var held := this.races.words.get_or(&word, race_word {}); if (held.wrote.bytes & made.bytes) != 0 && !this.races.before(&held.wrote) { return this.raced(&held.wrote); } if writes { if (held.first.bytes & made.bytes) != 0 && !this.races.before(&held.first) { return this.raced(&held.first); } if (held.second.bytes & made.bytes) != 0 && !this.races.before(&held.second) { return this.raced(&held.second); } # What was kept is all ordered before it, so it alone is kept. held = race_word { wrote = made }; } else if held.first.thread == made.thread || held.first.thread == 0 || this.races.before(&held.first) { held.first = made; } else if held.second.thread == made.thread || held.second.thread == 0 || this.races.before(&held.second) { held.second = made; } else { held.second = held.first; held.first = made; } this.races.words.set(word, held); return halt::none; } #! Stops the program as it races another thread's access, saying where that #! access was. #! @param this The machine. #! @param other The other access. #! @returns `race`. def interpreter::raced(&mut this, other: &race_access): halt { val frame := this.site_frame(other.function, other.pc as u64); this.races.text = $"Threads raced: memory one wrote, another read or wrote, with nothing ordering the two. The other was in {frame.function} ({frame.path}:{frame.line}:{frame.column})."; return halt::race; } #! Forgets what was kept of memory the program is given anew, as a block it #! allocates, which holds nothing of what was there before. #! @param this The machine. #! @param address The first byte. #! @param size How many. pub def interpreter::race_forget(&mut this, address: u64, size: u64): void { return if !this.races.on || this.races.words.length == 0; val first := address & ~7u; val end := address + size; # A large block is passed over by what was kept, rather than word by word. if size / 8 > this.races.words.length { var gone := vector::::new(); for &entry in this.races.words { if entry.key >= first && entry.key < end { gone.append(entry.key); } } for &each in gone { this.races.words.remove(&each); } gone.close(); return; } var word := first; while word < end { this.races.words.remove(&word); word += 8; } }