From bc28454b43b717dee47cf8c1a1abfb7e8ad09a1e Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sat, 13 Jul 2024 17:46:24 -0400 Subject: [PATCH 1/3] zig.h: replace `_InterlockedExchangeAdd` with a plain volatile load This was causing zig2.exe to crash during bootstrap, because there was an atomic load of read-only memory, and the attempt to write to it as part of the (idempotent) atomic exchange was invalid. Aligned reads (of u32 / u64) are atomic on x86 / x64, so this is replaced with an optimization-proof load (`__iso_volatile_load8*`) and a reordering barrier. --- lib/zig.h | 24 +++++++++++++----------- stage1/zig.h | 32 +++++++++++++++++--------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/zig.h b/lib/zig.h index f3b3897186..0c7ca7352b 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -3670,7 +3670,7 @@ typedef int zig_memory_order; /* TODO: zig_msvc_atomic_load should load 32 bit without interlocked on x86, and load 64 bit without interlocked on x64 */ -#define zig_msvc_atomics(ZigType, Type, SigType, suffix) \ +#define zig_msvc_atomics(ZigType, Type, SigType, suffix, iso_suffix) \ static inline bool zig_msvc_cmpxchg_##ZigType(Type volatile* obj, Type* expected, Type desired) { \ Type comparand = *expected; \ Type initial = _InterlockedCompareExchange##suffix((SigType volatile*)obj, (SigType)desired, (SigType)comparand); \ @@ -3741,21 +3741,23 @@ typedef int zig_memory_order; } \ static inline void zig_msvc_atomic_store_##ZigType(Type volatile* obj, Type value) { \ (void)_InterlockedExchange##suffix((SigType volatile*)obj, (SigType)value); \ - } \ + } \ static inline Type zig_msvc_atomic_load_##ZigType(Type volatile* obj) { \ - return _InterlockedExchangeAdd##suffix((SigType volatile*)obj, (SigType)0); \ + Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + return val; \ } -zig_msvc_atomics( u8, uint8_t, char, 8) -zig_msvc_atomics( i8, int8_t, char, 8) -zig_msvc_atomics(u16, uint16_t, short, 16) -zig_msvc_atomics(i16, int16_t, short, 16) -zig_msvc_atomics(u32, uint32_t, long, ) -zig_msvc_atomics(i32, int32_t, long, ) +zig_msvc_atomics( u8, uint8_t, char, 8, 8) +zig_msvc_atomics( i8, int8_t, char, 8, 8) +zig_msvc_atomics(u16, uint16_t, short, 16, 16) +zig_msvc_atomics(i16, int16_t, short, 16, 16) +zig_msvc_atomics(u32, uint32_t, long, , 32) +zig_msvc_atomics(i32, int32_t, long, , 32) #if _M_X64 -zig_msvc_atomics(u64, uint64_t, __int64, 64) -zig_msvc_atomics(i64, int64_t, __int64, 64) +zig_msvc_atomics(u64, uint64_t, __int64, 64, 64) +zig_msvc_atomics(i64, int64_t, __int64, 64, 64) #endif #define zig_msvc_flt_atomics(Type, SigType, suffix) \ diff --git a/stage1/zig.h b/stage1/zig.h index 1171c7efac..0c7ca7352b 100644 --- a/stage1/zig.h +++ b/stage1/zig.h @@ -207,16 +207,16 @@ typedef char bool; __asm(zig_mangle_c(name) " = " zig_mangle_c(symbol)) #endif +#define zig_mangled_tentative zig_mangled +#define zig_mangled_final zig_mangled #if _MSC_VER -#define zig_mangled_tentative(mangled, unmangled) -#define zig_mangled_final(mangled, unmangled) ; \ +#define zig_mangled(mangled, unmangled) ; \ zig_export(#mangled, unmangled) #define zig_mangled_export(mangled, unmangled, symbol) \ zig_export(unmangled, #mangled) \ zig_export(symbol, unmangled) #else /* _MSC_VER */ -#define zig_mangled_tentative(mangled, unmangled) __asm(zig_mangle_c(unmangled)) -#define zig_mangled_final(mangled, unmangled) zig_mangled_tentative(mangled, unmangled) +#define zig_mangled(mangled, unmangled) __asm(zig_mangle_c(unmangled)) #define zig_mangled_export(mangled, unmangled, symbol) \ zig_mangled_final(mangled, unmangled) \ zig_export(symbol, unmangled) @@ -3670,7 +3670,7 @@ typedef int zig_memory_order; /* TODO: zig_msvc_atomic_load should load 32 bit without interlocked on x86, and load 64 bit without interlocked on x64 */ -#define zig_msvc_atomics(ZigType, Type, SigType, suffix) \ +#define zig_msvc_atomics(ZigType, Type, SigType, suffix, iso_suffix) \ static inline bool zig_msvc_cmpxchg_##ZigType(Type volatile* obj, Type* expected, Type desired) { \ Type comparand = *expected; \ Type initial = _InterlockedCompareExchange##suffix((SigType volatile*)obj, (SigType)desired, (SigType)comparand); \ @@ -3741,21 +3741,23 @@ typedef int zig_memory_order; } \ static inline void zig_msvc_atomic_store_##ZigType(Type volatile* obj, Type value) { \ (void)_InterlockedExchange##suffix((SigType volatile*)obj, (SigType)value); \ - } \ + } \ static inline Type zig_msvc_atomic_load_##ZigType(Type volatile* obj) { \ - return _InterlockedExchangeAdd##suffix((SigType volatile*)obj, (SigType)0); \ + Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + return val; \ } -zig_msvc_atomics( u8, uint8_t, char, 8) -zig_msvc_atomics( i8, int8_t, char, 8) -zig_msvc_atomics(u16, uint16_t, short, 16) -zig_msvc_atomics(i16, int16_t, short, 16) -zig_msvc_atomics(u32, uint32_t, long, ) -zig_msvc_atomics(i32, int32_t, long, ) +zig_msvc_atomics( u8, uint8_t, char, 8, 8) +zig_msvc_atomics( i8, int8_t, char, 8, 8) +zig_msvc_atomics(u16, uint16_t, short, 16, 16) +zig_msvc_atomics(i16, int16_t, short, 16, 16) +zig_msvc_atomics(u32, uint32_t, long, , 32) +zig_msvc_atomics(i32, int32_t, long, , 32) #if _M_X64 -zig_msvc_atomics(u64, uint64_t, __int64, 64) -zig_msvc_atomics(i64, int64_t, __int64, 64) +zig_msvc_atomics(u64, uint64_t, __int64, 64, 64) +zig_msvc_atomics(i64, int64_t, __int64, 64, 64) #endif #define zig_msvc_flt_atomics(Type, SigType, suffix) \ From 373e53d7c5940c875a3423a6797f7b5d4a7f36c8 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sat, 13 Jul 2024 19:04:55 -0400 Subject: [PATCH 2/3] Compile: Pass the default --zig-lib-dir along to child processes main: print the self_exe_path when `findZigLibDirFromSelfExe` fails in all cases --- lib/std/Build/Step/Compile.zig | 11 +++++++++-- src/main.zig | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 504025e3d4..89862e7501 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -1635,9 +1635,16 @@ fn getZigArgs(compile: *Compile) ![][]const u8 { }); } - if (compile.zig_lib_dir) |dir| { + const opt_zig_lib_dir = if (compile.zig_lib_dir) |dir| + dir.getPath2(b, step) + else if (b.graph.zig_lib_directory.path) |_| + b.fmt("{}", .{b.graph.zig_lib_directory}) + else + null; + + if (opt_zig_lib_dir) |zig_lib_dir| { try zig_args.append("--zig-lib-dir"); - try zig_args.append(dir.getPath2(b, step)); + try zig_args.append(zig_lib_dir); } try addFlag(&zig_args, "PIE", compile.pie); diff --git a/src/main.zig b/src/main.zig index 39f07e982b..6ccf4f50be 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2710,7 +2710,7 @@ fn buildOutputType( break :d getWasiPreopen("/lib"); } else if (self_exe_path) |p| { break :d introspect.findZigLibDirFromSelfExe(arena, p) catch |err| { - fatal("unable to find zig installation directory: {s}", .{@errorName(err)}); + fatal("unable to find zig installation directory '{s}': {s}", .{ p, @errorName(err) }); }; } else { unreachable; @@ -7403,7 +7403,7 @@ fn findTemplates(gpa: Allocator, arena: Allocator) Templates { fatal("unable to find self exe path: {s}", .{@errorName(err)}); }; var zig_lib_directory = introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| { - fatal("unable to find zig installation directory: {s}", .{@errorName(err)}); + fatal("unable to find zig installation directory '{s}': {s}", .{ self_exe_path, @errorName(err) }); }; const s = fs.path.sep_str; From c31871065394c78d7d316b2a1709077437d29ccf Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sat, 13 Jul 2024 19:45:45 -0400 Subject: [PATCH 3/3] zig.h: expand zig_msvc_atomic_load_ into version for relaxed, acquire, and seq_cst --- lib/zig.h | 37 ++++++++++++++++++++++++++++++------- stage1/zig.h | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 14 deletions(-) diff --git a/lib/zig.h b/lib/zig.h index 0c7ca7352b..2b238ef1fa 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -3636,7 +3636,7 @@ typedef int zig_memory_order; #define zig_atomicrmw_min(res, obj, arg, order, Type, ReprType) res = zig_msvc_atomicrmw_min_ ##Type(obj, arg) #define zig_atomicrmw_max(res, obj, arg, order, Type, ReprType) res = zig_msvc_atomicrmw_max_ ##Type(obj, arg) #define zig_atomic_store( obj, arg, order, Type, ReprType) zig_msvc_atomic_store_ ##Type(obj, arg) -#define zig_atomic_load(res, obj, order, Type, ReprType) res = zig_msvc_atomic_load_ ##Type(obj) +#define zig_atomic_load(res, obj, order, Type, ReprType) res = zig_msvc_atomic_load_ ##order##_##Type(obj) #if _M_X64 #define zig_fence(order) __faststorefence() #else @@ -3742,7 +3742,15 @@ typedef int zig_memory_order; static inline void zig_msvc_atomic_store_##ZigType(Type volatile* obj, Type value) { \ (void)_InterlockedExchange##suffix((SigType volatile*)obj, (SigType)value); \ } \ - static inline Type zig_msvc_atomic_load_##ZigType(Type volatile* obj) { \ + static inline Type zig_msvc_atomic_load_zig_memory_order_relaxed_##ZigType(Type volatile* obj) { \ + return __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + } \ + static inline Type zig_msvc_atomic_load_zig_memory_order_acquire_##ZigType(Type volatile* obj) { \ + Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + return val; \ + } \ + static inline Type zig_msvc_atomic_load_zig_memory_order_seq_cst_##ZigType(Type volatile* obj) { \ Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ _ReadWriteBarrier(); \ return val; \ @@ -3760,7 +3768,7 @@ zig_msvc_atomics(u64, uint64_t, __int64, 64, 64) zig_msvc_atomics(i64, int64_t, __int64, 64, 64) #endif -#define zig_msvc_flt_atomics(Type, SigType, suffix) \ +#define zig_msvc_flt_atomics(Type, SigType, suffix, iso_suffix) \ static inline bool zig_msvc_cmpxchg_##Type(zig_##Type volatile* obj, zig_##Type* expected, zig_##Type desired) { \ SigType exchange; \ SigType comparand; \ @@ -3778,15 +3786,30 @@ zig_msvc_atomics(i64, int64_t, __int64, 64, 64) memcpy(&value, &arg, sizeof(value)); \ (void)_InterlockedExchange##suffix((SigType volatile*)obj, value); \ } \ - static inline zig_##Type zig_msvc_atomic_load_##Type(zig_##Type volatile* obj) { \ + static inline zig_##Type zig_msvc_atomic_load_zig_memory_order_relaxed_##Type(zig_##Type volatile* obj) { \ zig_##Type result; \ - SigType initial = _InterlockedExchangeAdd##suffix((SigType volatile*)obj, (SigType)0); \ + SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ memcpy(&result, &initial, sizeof(result)); \ return result; \ + } \ + static inline zig_##Type zig_msvc_atomic_load_zig_memory_order_acquire_##Type(zig_##Type volatile* obj) { \ + zig_##Type result; \ + SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + memcpy(&result, &initial, sizeof(result)); \ + return result; \ + } \ + static inline zig_##Type zig_msvc_atomic_load_zig_memory_order_seq_cst_##Type(zig_##Type volatile* obj) { \ + zig_##Type result; \ + SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + memcpy(&result, &initial, sizeof(result)); \ + return result; \ } -zig_msvc_flt_atomics(f32, long, ) + +zig_msvc_flt_atomics(f32, long, , 32) #if _M_X64 -zig_msvc_flt_atomics(f64, int64_t, 64) +zig_msvc_flt_atomics(f64, int64_t, 64, 64) #endif #if _M_IX86 diff --git a/stage1/zig.h b/stage1/zig.h index 0c7ca7352b..2b238ef1fa 100644 --- a/stage1/zig.h +++ b/stage1/zig.h @@ -3636,7 +3636,7 @@ typedef int zig_memory_order; #define zig_atomicrmw_min(res, obj, arg, order, Type, ReprType) res = zig_msvc_atomicrmw_min_ ##Type(obj, arg) #define zig_atomicrmw_max(res, obj, arg, order, Type, ReprType) res = zig_msvc_atomicrmw_max_ ##Type(obj, arg) #define zig_atomic_store( obj, arg, order, Type, ReprType) zig_msvc_atomic_store_ ##Type(obj, arg) -#define zig_atomic_load(res, obj, order, Type, ReprType) res = zig_msvc_atomic_load_ ##Type(obj) +#define zig_atomic_load(res, obj, order, Type, ReprType) res = zig_msvc_atomic_load_ ##order##_##Type(obj) #if _M_X64 #define zig_fence(order) __faststorefence() #else @@ -3742,7 +3742,15 @@ typedef int zig_memory_order; static inline void zig_msvc_atomic_store_##ZigType(Type volatile* obj, Type value) { \ (void)_InterlockedExchange##suffix((SigType volatile*)obj, (SigType)value); \ } \ - static inline Type zig_msvc_atomic_load_##ZigType(Type volatile* obj) { \ + static inline Type zig_msvc_atomic_load_zig_memory_order_relaxed_##ZigType(Type volatile* obj) { \ + return __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + } \ + static inline Type zig_msvc_atomic_load_zig_memory_order_acquire_##ZigType(Type volatile* obj) { \ + Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + return val; \ + } \ + static inline Type zig_msvc_atomic_load_zig_memory_order_seq_cst_##ZigType(Type volatile* obj) { \ Type val = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ _ReadWriteBarrier(); \ return val; \ @@ -3760,7 +3768,7 @@ zig_msvc_atomics(u64, uint64_t, __int64, 64, 64) zig_msvc_atomics(i64, int64_t, __int64, 64, 64) #endif -#define zig_msvc_flt_atomics(Type, SigType, suffix) \ +#define zig_msvc_flt_atomics(Type, SigType, suffix, iso_suffix) \ static inline bool zig_msvc_cmpxchg_##Type(zig_##Type volatile* obj, zig_##Type* expected, zig_##Type desired) { \ SigType exchange; \ SigType comparand; \ @@ -3778,15 +3786,30 @@ zig_msvc_atomics(i64, int64_t, __int64, 64, 64) memcpy(&value, &arg, sizeof(value)); \ (void)_InterlockedExchange##suffix((SigType volatile*)obj, value); \ } \ - static inline zig_##Type zig_msvc_atomic_load_##Type(zig_##Type volatile* obj) { \ + static inline zig_##Type zig_msvc_atomic_load_zig_memory_order_relaxed_##Type(zig_##Type volatile* obj) { \ zig_##Type result; \ - SigType initial = _InterlockedExchangeAdd##suffix((SigType volatile*)obj, (SigType)0); \ + SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ memcpy(&result, &initial, sizeof(result)); \ return result; \ + } \ + static inline zig_##Type zig_msvc_atomic_load_zig_memory_order_acquire_##Type(zig_##Type volatile* obj) { \ + zig_##Type result; \ + SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + memcpy(&result, &initial, sizeof(result)); \ + return result; \ + } \ + static inline zig_##Type zig_msvc_atomic_load_zig_memory_order_seq_cst_##Type(zig_##Type volatile* obj) { \ + zig_##Type result; \ + SigType initial = __iso_volatile_load##iso_suffix((SigType volatile*)obj); \ + _ReadWriteBarrier(); \ + memcpy(&result, &initial, sizeof(result)); \ + return result; \ } -zig_msvc_flt_atomics(f32, long, ) + +zig_msvc_flt_atomics(f32, long, , 32) #if _M_X64 -zig_msvc_flt_atomics(f64, int64_t, 64) +zig_msvc_flt_atomics(f64, int64_t, 64, 64) #endif #if _M_IX86