Skip to content

128 bit atomic causes data race #14235

Open

Description

@IntegratedQuantum

Zig Version

0.11.0-dev.1201+301a89849 - 0.12.0-dev.2150+63de8a598

Steps to Reproduce and Observed Behavior

Create an atomic and access it from two threads:

const std = @import("std");

const Type = u128;

var atomic: std.atomic.Value(Type) = std.atomic.Value(Type).init(0);

fn run() void {
	var x = atomic.load(.Monotonic);
	_ = &x;
	atomic.store(@intCast(std.Thread.getCurrentId()), .Monotonic);
}

pub fn main() !void {
	var thread = try std.Thread.spawn(.{}, run, .{});
	defer thread.join();
	run();
}

Run it with zig run test.zig -fsanitize-thread and observe a data race:

/usr/bin/addr2line: DWARF error: section .debug_info is larger than its filesize! (0x93ef57 vs 0x530ea0)
==================
WARNING: ThreadSanitizer: data race (pid=6307)
  Atomic write of size 8 at 0x559dc8fea9a0 by thread T1:
    #0 __tsan_atomic128_store ??:? (test+0x1103e0)
    #1 atomic.Atomic.Atomic(u128).store /home/mint/Downloads/zig/lib/std/atomic/Atomic.zig:88 (test+0x6e10a)
    #2 test.run /home/mint/Desktop/test.zig:10 (test+0x6e10a)
    #3 Thread.callFn__anon_6181 /home/mint/Downloads/zig/lib/std/Thread.zig:393 (test+0x97890)
    #4 Thread.PosixThreadImpl.spawn__anon_2776.Instance.entryFn /home/mint/Downloads/zig/lib/std/Thread.zig:661 (test+0x6fa81)

  Previous read of size 8 at 0x559dc8fea9a0 by main thread:
    #0 memcpy ??:? (test+0xd4606)
    #1 __atomic_load ??:? (test+0x192e96)
    #2 test.main /home/mint/Desktop/test.zig:16 (test+0x6dd49)
    #3 start.callMain /home/mint/Downloads/zig/lib/std/start.zig:616 (test+0x6e61d)
    #4 start.initEventLoopAndCallMain /home/mint/Downloads/zig/lib/std/start.zig:550 (test+0x6e61d)
    #5 start.callMainWithArgs /home/mint/Downloads/zig/lib/std/start.zig:500 (test+0x6e61d)
    #6 main /home/mint/Downloads/zig/lib/std/start.zig:515 (test+0x6e61d)

  Location is global '<null>' at 0x000000000000 (test+0x0000001a19a0)

  Thread T1 (tid=6322, running) created by main thread at:
    #0 pthread_create ??:? (test+0xcc0e4)
    #1 Thread.PosixThreadImpl.spawn__anon_2776 /home/mint/Downloads/zig/lib/std/Thread.zig:688 (test+0x6f5bf)
    #2 Thread.spawn__anon_1464 /home/mint/Downloads/zig/lib/std/Thread.zig:326 (test+0x6ddbe)
    #3 test.main /home/mint/Desktop/test.zig:14 (test+0x6dcf5)
    #4 start.callMain /home/mint/Downloads/zig/lib/std/start.zig:616 (test+0x6e61d)
    #5 start.initEventLoopAndCallMain /home/mint/Downloads/zig/lib/std/start.zig:550 (test+0x6e61d)
    #6 start.callMainWithArgs /home/mint/Downloads/zig/lib/std/start.zig:500 (test+0x6e61d)
    #7 main /home/mint/Downloads/zig/lib/std/start.zig:515 (test+0x6e61d)

SUMMARY: ThreadSanitizer: data race ??:? in __tsan_atomic128_store
==================
ThreadSanitizer: reported 1 warnings

Expected Behavior

It should work fine, like in the u64 case (setting Type to u64 in the above code).

Or if atomics were never supposed to support this, it should cause a compiler error.

Activity

  1. added
    bug
    on Jan 7, 2023
  2. added this to the 0.11.0 milestone on Jan 9, 2023
  3. modified the milestones: 0.11.0, 0.12.0 on Jul 20, 2023
  4. modified the milestones: 0.14.0, 0.15.0 on Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions