Guest Race Detection

Building on top of context-awareness, TEMU can detect data races in guest software (similar to Valgrind’s Helgrind for POSIX-like userspace applications or LLVM’s (and GCC’s) ThreadSanitizer compiler extensions/runtimes). The underlying algorithms for race detection are the same. TEMU is unique here in that it can do this for unmodified guest software running directly in TEMU.

Currently only the SPARC target is supported with TEMU only providing configuration files for RTEMS, but guest OS specific configurations can all be done in external custom files/overloads.

The TEMU object system class used to interact with the race detector is ThreadSanitizer. Given the DWARF debug information also used for TEMU’s context-awareness, a race is reported with the variable, the function making the access, both threads involved, and whether each side read or wrote.

Races between interrupt handlers and threads can be detected as well (though with restrictions).

Requirements

  • A SPARC (e.g. LEON4) processor running RTEMS 5 (single-core or SMP) or a custom guest-OS specific YAML configuration for TSAN describing OS primitives like locks, mutexes, and semaphores.

  • An OS-awareness software context for the software, see OS Awareness. The race detector learns from it which thread runs on which processor.

  • An image with DWARF .debug_info. The other debug sections can be stripped.

Synchronization is recognized by the calls the software makes into the guest OS’s API, for example rtems_semaphore_obtain or pthread_mutex_lock. Atomic instructions (casa, swap, ldstub) are seen directly, so a lock built from them is recognized when both taking and releasing it are atomic. Other locks the software implements itself (or that are inlined) are not seen, and the accesses they protect can be reported as false positives. The configurations (see Project Configuration) can extend a parent, and can be used to add additional synchronization primitives to avoid false positives.

Quick Start

A minimal OS-awareness configuration:

app.yaml
version: 1
plugin: sparc-rtems
main:
  debug-info: app.elf

The race detector has to be set up before running the first CPU:

import Component
import Debugging

Component.new name=board0 file=gr765.yaml
load obj=board0-mem file=app.elf
cpu-set-reg cpu=board0-cpu0 reg="%sp" value=0x46000000
cpu-set-reg cpu=board0-cpu0 reg="%fp" value=0x46000000
board0-cpu0.pc = 0x40000000

ContextManager.new name=cm0
cm0.attach board=board0
cm0.add-software-context board=board0 name=app config=app.yaml

# Races are reported through the context manager.
# By default they will be printed as log messages,
# but can also be serialized to JSON (one line per record):
cm0.report-log file=races.json categories=race

ThreadSanitizer.new name=tsan0
tsan0.attach board=board0 context=app
tsan0.arm

# Also works with the MLS etc.
object-run obj=board0-cpu0 time=3.0

tsan0.status

When arming, TEMU picks the configuration matching the software, rtems5-sparc-single-core or rtems5-sparc-smp. To use a different one, name it before arming:

tsan0.config file=rtems5-sparc-single-core-isr.yaml

tsan0.providers lists the configurations TEMU can find. The tsan/paths configuration entry can be used to add custom ones.

While armed, every guest memory access is analyzed, so the emulation runs considerably slower than usual. DBT while using TSAN works (tsan0.set-mode mode=dbt), and will speed things up, but there still is a slowdown.

The race detector needs no real parallelism to find a race, and a single thread makes every run the same run, so a report can be reproduced:

SingleLevelScheduler.new name=sched
sched.set-frequency frequency=board0-cpu0.freq
sched.set-quanta quanta=1000
sched.set-threads threads=1
sched.bind-to-group group=0 cpu=board0-cpu0
sched.bind-to-group group=0 cpu=board0-cpu1
sched.bind-to-group group=0 cpu=board0-cpu2
sched.bind-to-group group=0 cpu=board0-cpu3

Reading the Results

tsan0.status prints a summary. The line to look at is races:

ThreadSanitizer:
	armed: yes
	context: app
	provider: rtems5-sparc-single-core
	...
	race detection: on
	races: 19

If status also lists counters such as unmatched-exits or dropped-pending, some synchronization was missed, and reports may be false.

One individual JSON entry logged by cm0.report-log file=races.json categories=race looks something like this:

{
  "seq": 2,
  "category": "race",
  "level": "warning",
  "cpu": 0,
  "pc": 1073747108,
  "thread": "CNT0",
  "write": false,
  "previous_thread": "CNT1",
  "previous_write": true,
  "symbol": "SuiteCounter",
  "symbol_offset": 0,
  "function": "counterWorker",
  "source_file": "tsan-suite.c",
  "object": "tsan-suite.c",
  "va": 1073915896,
  "pa": 1073915896,
  "size": 4,
  "tid": 1,
  "previous_tid": 2,
  "previous_since_pc": 1073747064,
  "previous_until_pc": 1073747128
}

Here, the RTEMS task CNT0 read SuiteCounter in counterWorker, racing with an earlier write by task CNT1.

thread, write

The thread making the access, and whether it wrote.

previous_thread, previous_write

The thread of the earlier, conflicting access, and whether it wrote.

symbol, symbol_offset

The variable accessed, and the offset into it.

function, source_file, object

Where the access was made from.

pc, cpu

The virtual address of the instruction making the access, and the processor running it.

va, pa, size

The accessed virtual and physical address, and the number of bytes accessed.

tid, previous_tid

Numeric ids of the two threads. Ids of exited threads are reused, so they are only unique among the threads alive at the same time.

previous_since_pc, previous_until_pc

The earlier access was made between these two PCs of its thread, 0 if no longer known or the thread has not moved on yet.

The use of a synchronization primitive by the guest SW as detected for TSAN and context switches can also be reported:

cm0.report-log file=trace.json level=debug categories="race,sync,context"

tsan0.summary lists each synchronization object seen during the run, with a count per operation. It shows whether the locks the software relies on were recognized.

Narrowing the Reports

Ignoring Variables

Races on a variable can be ignored by name:

tsan0.ignore symbol=SharedStatistics

ignore and watch symbol= need attach first, and the symbol must have a size.

Project Configuration

A project configuration builds on a shipped one with extends, and adds what is specific to the software:

my-project.yaml
version: 1
name: my-project
extends: rtems5-sparc-single-core.yaml

# The project's own lock functions. The first argument identifies the lock.
hooks:
  - symbol: app_lock
    op: mutex-acquire-enter
  - symbol: app_unlock
    op: mutex-release

# Variables not to report races on. A trailing '*' matches a prefix.
ignore:
  - SharedStatistics
  - debug_trace_*
tsan0.config file=my-project.yaml
tsan0.arm

Each hook names a function and what calling it means:

mutex-acquire-enter

Acquires a lock. The lock is held once the function returns.

mutex-try-acquire

Tries to acquire a lock (only successful if it returns 0).

mutex-release

Releases a lock.

sem-wait-enter, sem-post

Waits on and posts a semaphore.

sem-try-wait

Tries a semaphore (only successful if it returns 0).

barrier-wait

Waits at a barrier.

event-send, event-receive

Sends an event to the task whose id is the argument (with kind: handle), and receives the calling task’s events, used e.g. for blocking event queues.

By default the first argument identifies the lock by its address. arg selects another argument (counting from 0), and kind: handle for an argument that is an OS object id instead of a pointer. TEMU warns if a hooked function is missing from the image, unless the hook has optional: true. without: [symbol] drops a hook or an ignore inherited through extends.

Suppressions

Other than ignore: in the YAML configuration, suppressions text files in LLVM’s format can be used as well.

known-races.supp
# Known race, tracked as issue XYZ.
race:^SuiteCounter$

# Races involving the logging task.
thread:^LOG0$

# Third-party code.
called_from_lib:^third_party_
tsan0.load-suppressions file=known-races.supp

Restricting Analysis

By default all RAM/ROM guest memory is analyzed. TEMU currently does not support detecting races for accesses to memory mapped I/O models. watch restricts the analysis to selected ranges, given either as a symbol or as a physical address and size. This must be done before calling arm and only works at guest page size granularity.

tsan0.watch obj=board0-mem symbol=SharedTable
tsan0.watch obj=board0-mem addr=0x40100000 size=0x1000

Interrupt Handlers

By default, an interrupt handler’s accesses count as accesses by the task it interrupted, so a race between a task and a handler is not reported.

The rtems5-sparc-single-core-isr and rtems5-sparc-smp-isr configurations run interrupt handlers as one thread per processor, named ISR, and treat disabling interrupts as a lock against the handler on that processor.

RTEMS disables interrupts often, and since that acts as a lock, it also orders tasks against each other. Some races between tasks are then missed.

Timing-Dependent Races

Just like Valgrind’s Helgrind or LLVM’s (and GCC’s) ThreadSanitizer, a race actually needs to happen for TEMU to detect it. Whether a race happens or not can be timing-dependent. Changing the processors' cycles per instruction (cpi, default 1) changes the simulated time each instruction takes, see Instruction Cycle Adjustments. Running the same image at a different cpi can expose such races:

board0-cpu0.cpi = 4
board0-cpu1.cpi = 4
board0-cpu2.cpi = 4
board0-cpu3.cpi = 4

Limitations

  • Only SPARC targets are supported, and configurations are only shipped for RTEMS 5.

  • Only accesses made by processors are analyzed. Accesses to memory mapped I/O, and DMA by device models, are not.

  • Only threads known to the OS awareness are analyzed. Accesses before the first context switch, such as during boot, are not analyzed.

  • Synchronization is only seen through hooked functions and atomic instructions. Inlined or hand-written locks are not, see Project Configuration.

  • At most 255 threads are analyzed at the same time, counting each processor’s ISR thread. Exited threads' ids are reused.

  • Shadow memory tracks up to 4 accesses (per thread and access type) per cell. A further access replaces one of them, which can miss a race (status lists evictions).

  • The shadow memory takes 4 times the size of the analyzed guest RAM/ROM.