Files
zig/src/libs/libtsan.zig
mlugg 37a9a4e0f1 compiler: refactor Zcu.File and path representation
This commit makes some big changes to how we track state for Zig source
files. In particular, it changes:

* How `File` tracks its path on-disk
* How AstGen discovers files
* How file-level errors are tracked
* How `builtin.zig` files and modules are created

The original motivation here was to address incremental compilation bugs
with the handling of files, such as #22696. To fix this, a few changes
are necessary.

Just like declarations may become unreferenced on an incremental update,
meaning we suppress analysis errors associated with them, it is also
possible for all imports of a file to be removed on an incremental
update, in which case file-level errors for that file should be
suppressed. As such, after AstGen, the compiler must traverse files
(starting from analysis roots) and discover the set of "live files" for
this update.

Additionally, the compiler's previous handling of retryable file errors
was not very good; the source location the error was reported as was
based only on the first discovered import of that file. This source
location also disappeared on future incremental updates. So, as a part
of the file traversal above, we also need to figure out the source
locations of imports which errors should be reported against.

Another observation I made is that the "file exists in multiple modules"
error was not implemented in a particularly good way (I get to say that
because I wrote it!). It was subject to races, where the order in which
different imports of a file were discovered affects both how errors are
printed, and which module the file is arbitrarily assigned, with the
latter in turn affecting which other files are considered for import.
The thing I realised here is that while the AstGen worker pool is
running, we cannot know for sure which module(s) a file is in; we could
always discover an import later which changes the answer.

So, here's how the AstGen workers have changed. We initially ensure that
`zcu.import_table` contains the root files for all modules in this Zcu,
even if we don't know any imports for them yet. Then, the AstGen
workers do not need to be aware of modules. Instead, they simply ignore
module imports, and only spin off more workers when they see a by-path
import.

During AstGen, we can't use module-root-relative paths, since we don't
know which modules files are in; but we don't want to unnecessarily use
absolute files either, because those are non-portable and can make
`error.NameTooLong` more likely. As such, I have introduced a new
abstraction, `Compilation.Path`. This type is a way of representing a
filesystem path which has a *canonical form*. The path is represented
relative to one of a few special directories: the lib directory, the
global cache directory, or the local cache directory. As a fallback, we
use absolute (or cwd-relative on WASI) paths. This is kind of similar to
`std.Build.Cache.Path` with a pre-defined list of possible
`std.Build.Cache.Directory`, but has stricter canonicalization rules
based on path resolution to make sure deduplicating files works
properly. A `Compilation.Path` can be trivially converted to a
`std.Build.Cache.Path` from a `Compilation`, but is smaller, has a
canonical form, and has a digest which will be consistent across
different compiler processes with the same lib and cache directories
(important when we serialize incremental compilation state in the
future). `Zcu.File` and `Zcu.EmbedFile` both contain a
`Compilation.Path`, which is used to access the file on-disk;
module-relative sub paths are used quite rarely (`EmbedFile` doesn't
even have one now for simplicity).

After the AstGen workers all complete, we know that any file which might
be imported is definitely in `import_table` and up-to-date. So, we
perform a single-threaded graph traversal; similar to what
`resolveReferences` plays for `AnalUnit`s, but for files instead. We
figure out which files are alive, and which module each file is in. If a
file turns out to be in multiple modules, we set a field on `Zcu` to
indicate this error. If a file is in a different module to a prior
update, we set a flag instructing `updateZirRefs` to invalidate all
dependencies on the file. This traversal also discovers "import errors";
these are errors associated with a specific `@import`. With Zig's
current design, there is only one possible error here: "import outside
of module root". This must be identified during this traversal instead
of during AstGen, because it depends on which module the file is in. I
tried also representing "module not found" errors in this same way, but
it turns out to be much more useful to report those in Sema, because of
use cases like optional dependencies where a module import is behind a
comptime-known build option.

For simplicity, `failed_files` now just maps to `?[]u8`, since the
source location is always the whole file. In fact, this allows removing
`LazySrcLoc.Offset.entire_file` completely, slightly simplifying some
error reporting logic. File-level errors are now directly built in the
`std.zig.ErrorBundle.Wip`. If the payload is not `null`, it is the
message for a retryable error (i.e. an error loading the source file),
and will be reported with a "file imported here" note pointing to the
import site discovered during the single-threaded file traversal.

The last piece of fallout here is how `Builtin` works. Rather than
constructing "builtin" modules when creating `Package.Module`s, they are
now constructed on-the-fly by `Zcu`. The map `Zcu.builtin_modules` maps
from digests to `*Package.Module`s. These digests are abstract hashes of
the `Builtin` value; i.e. all of the options which are placed into
"builtin.zig". During the file traversal, we populate `builtin_modules`
as needed, so that when we see this imports in Sema, we just grab the
relevant entry from this map. This eliminates a bunch of awkward state
tracking during construction of the module graph. It's also now clearer
exactly what options the builtin module has, since previously it
inherited some options arbitrarily from the first-created module with
that "builtin" module!

The user-visible effects of this commit are:
* retryable file errors are now consistently reported against the whole
  file, with a note pointing to a live import of that file
* some theoretical bugs where imports are wrongly considered distinct
  (when the import path moves out of the cwd and then back in) are fixed
* some consistency issues with how file-level errors are reported are
  fixed; these errors will now always be printed in the same order
  regardless of how the AstGen pass assigns file indices
* incremental updates do not print retryable file errors differently
  between updates or depending on file structure/contents
* incremental updates support files changing modules
* incremental updates support files becoming unreferenced

Resolves: #22696
2025-05-18 17:37:02 +01:00

493 lines
16 KiB
Zig

const std = @import("std");
const assert = std.debug.assert;
const Compilation = @import("../Compilation.zig");
const build_options = @import("build_options");
const trace = @import("../tracy.zig").trace;
const Module = @import("../Package/Module.zig");
pub const BuildError = error{
OutOfMemory,
SubCompilationFailed,
ZigCompilerNotBuiltWithLLVMExtensions,
TSANUnsupportedCPUArchitecture,
};
pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!void {
if (!build_options.have_llvm) {
return error.ZigCompilerNotBuiltWithLLVMExtensions;
}
const tracy = trace(@src());
defer tracy.end();
var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
const target = comp.getTarget();
const root_name = switch (target.os.tag) {
// On Apple platforms, we use the same name as LLVM because the
// TSAN library implementation hard-codes a check for these names.
.driverkit, .macos => "clang_rt.tsan_osx_dynamic",
.ios => if (target.abi == .simulator) "clang_rt.tsan_iossim_dynamic" else "clang_rt.tsan_ios_dynamic",
.tvos => if (target.abi == .simulator) "clang_rt.tsan_tvossim_dynamic" else "clang_rt.tsan_tvos_dynamic",
.visionos => if (target.abi == .simulator) "clang_rt.tsan_xrossim_dynamic" else "clang_rt.tsan_xros_dynamic",
.watchos => if (target.abi == .simulator) "clang_rt.tsan_watchossim_dynamic" else "clang_rt.tsan_watchos_dynamic",
else => "tsan",
};
const link_mode: std.builtin.LinkMode = if (target.os.tag.isDarwin()) .dynamic else .static;
const output_mode = .Lib;
const basename = try std.zig.binNameAlloc(arena, .{
.root_name = root_name,
.target = target,
.output_mode = output_mode,
.link_mode = link_mode,
});
const emit_bin = Compilation.EmitLoc{
.directory = null, // Put it in the cache directory.
.basename = basename,
};
const optimize_mode = comp.compilerRtOptMode();
const strip = comp.compilerRtStrip();
const unwind_tables: std.builtin.UnwindTables =
if (target.cpu.arch == .x86 and target.os.tag == .windows) .none else .@"async";
const link_libcpp = target.os.tag.isDarwin();
const config = Compilation.Config.resolve(.{
.output_mode = output_mode,
.link_mode = link_mode,
.resolved_target = comp.root_mod.resolved_target,
.is_test = false,
.have_zcu = false,
.emit_bin = true,
.root_optimize_mode = optimize_mode,
.root_strip = strip,
.link_libc = true,
.link_libcpp = link_libcpp,
.any_unwind_tables = unwind_tables != .none,
// LLVM disables LTO for its libtsan.
.lto = .none,
}) catch |err| {
comp.setMiscFailure(
.libtsan,
"unable to build thread sanitizer runtime: resolving configuration failed: {s}",
.{@errorName(err)},
);
return error.SubCompilationFailed;
};
const common_flags = [_][]const u8{
"-DTSAN_CONTAINS_UBSAN=0",
};
const root_mod = Module.create(arena, .{
.paths = .{
.root = .zig_lib_root,
.root_src_path = "",
},
.fully_qualified_name = "root",
.inherited = .{
.resolved_target = comp.root_mod.resolved_target,
.strip = strip,
.stack_check = false,
.stack_protector = 0,
.sanitize_c = .off,
.sanitize_thread = false,
.red_zone = comp.root_mod.red_zone,
.omit_frame_pointer = optimize_mode != .Debug and !target.os.tag.isDarwin(),
.valgrind = false,
.unwind_tables = unwind_tables,
.optimize_mode = optimize_mode,
.structured_cfg = comp.root_mod.structured_cfg,
.pic = true,
.no_builtin = true,
.code_model = comp.root_mod.code_model,
},
.global = config,
.cc_argv = &common_flags,
.parent = null,
}) catch |err| {
comp.setMiscFailure(
.libtsan,
"unable to build thread sanitizer runtime: creating module failed: {s}",
.{@errorName(err)},
);
return error.SubCompilationFailed;
};
var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena);
try c_source_files.ensureUnusedCapacity(tsan_sources.len);
const tsan_include_path = try comp.dirs.zig_lib.join(arena, &.{"libtsan"});
for (tsan_sources) |tsan_src| {
var cflags = std.ArrayList([]const u8).init(arena);
try cflags.append("-I");
try cflags.append(tsan_include_path);
try addCcArgs(target, &cflags);
c_source_files.appendAssumeCapacity(.{
.src_path = try comp.dirs.zig_lib.join(arena, &.{ "libtsan", tsan_src }),
.extra_flags = cflags.items,
.owner = root_mod,
});
}
const platform_tsan_sources = switch (target.os.tag) {
.ios, .macos, .watchos, .tvos, .visionos => &darwin_tsan_sources,
.windows => &windows_tsan_sources,
else => &unix_tsan_sources,
};
try c_source_files.ensureUnusedCapacity(platform_tsan_sources.len);
for (platform_tsan_sources) |tsan_src| {
var cflags = std.ArrayList([]const u8).init(arena);
try cflags.append("-I");
try cflags.append(tsan_include_path);
try addCcArgs(target, &cflags);
c_source_files.appendAssumeCapacity(.{
.src_path = try comp.dirs.zig_lib.join(arena, &.{ "libtsan", tsan_src }),
.extra_flags = cflags.items,
.owner = root_mod,
});
}
{
const asm_source = switch (target.cpu.arch) {
.aarch64, .aarch64_be => "tsan_rtl_aarch64.S",
.loongarch64 => "tsan_rtl_loongarch64.S",
.mips64, .mips64el => "tsan_rtl_mips64.S",
.powerpc64, .powerpc64le => "tsan_rtl_ppc64.S",
.riscv64 => "tsan_rtl_riscv64.S",
.s390x => "tsan_rtl_s390x.S",
.x86_64 => "tsan_rtl_amd64.S",
else => return error.TSANUnsupportedCPUArchitecture,
};
var cflags = std.ArrayList([]const u8).init(arena);
try cflags.append("-I");
try cflags.append(tsan_include_path);
try cflags.append("-DNDEBUG");
c_source_files.appendAssumeCapacity(.{
.src_path = try comp.dirs.zig_lib.join(arena, &.{ "libtsan", asm_source }),
.extra_flags = cflags.items,
.owner = root_mod,
});
}
try c_source_files.ensureUnusedCapacity(sanitizer_common_sources.len);
const sanitizer_common_include_path = try comp.dirs.zig_lib.join(arena, &.{
"libtsan", "sanitizer_common",
});
for (sanitizer_common_sources) |common_src| {
var cflags = std.ArrayList([]const u8).init(arena);
try cflags.append("-I");
try cflags.append(sanitizer_common_include_path);
try cflags.append("-I");
try cflags.append(tsan_include_path);
try addCcArgs(target, &cflags);
c_source_files.appendAssumeCapacity(.{
.src_path = try comp.dirs.zig_lib.join(arena, &.{
"libtsan", "sanitizer_common", common_src,
}),
.extra_flags = cflags.items,
.owner = root_mod,
});
}
const to_c_or_not_to_c_sources = if (comp.config.link_libc)
&sanitizer_libcdep_sources
else
&sanitizer_nolibc_sources;
try c_source_files.ensureUnusedCapacity(to_c_or_not_to_c_sources.len);
for (to_c_or_not_to_c_sources) |c_src| {
var cflags = std.ArrayList([]const u8).init(arena);
try cflags.append("-I");
try cflags.append(sanitizer_common_include_path);
try cflags.append("-I");
try cflags.append(tsan_include_path);
try addCcArgs(target, &cflags);
c_source_files.appendAssumeCapacity(.{
.src_path = try comp.dirs.zig_lib.join(arena, &.{
"libtsan", "sanitizer_common", c_src,
}),
.extra_flags = cflags.items,
.owner = root_mod,
});
}
try c_source_files.ensureUnusedCapacity(sanitizer_symbolizer_sources.len);
for (sanitizer_symbolizer_sources) |c_src| {
var cflags = std.ArrayList([]const u8).init(arena);
try cflags.append("-I");
try cflags.append(tsan_include_path);
try addCcArgs(target, &cflags);
c_source_files.appendAssumeCapacity(.{
.src_path = try comp.dirs.zig_lib.join(arena, &.{
"libtsan", "sanitizer_common", c_src,
}),
.extra_flags = cflags.items,
.owner = root_mod,
});
}
const interception_include_path = try comp.dirs.zig_lib.join(arena, &.{"interception"});
try c_source_files.ensureUnusedCapacity(interception_sources.len);
for (interception_sources) |c_src| {
var cflags = std.ArrayList([]const u8).init(arena);
try cflags.append("-I");
try cflags.append(interception_include_path);
try cflags.append("-I");
try cflags.append(tsan_include_path);
try addCcArgs(target, &cflags);
c_source_files.appendAssumeCapacity(.{
.src_path = try comp.dirs.zig_lib.join(arena, &.{
"libtsan", "interception", c_src,
}),
.extra_flags = cflags.items,
.owner = root_mod,
});
}
const skip_linker_dependencies = !target.os.tag.isDarwin();
const linker_allow_shlib_undefined = target.os.tag.isDarwin();
const install_name = if (target.os.tag.isDarwin())
try std.fmt.allocPrintZ(arena, "@rpath/{s}", .{basename})
else
null;
// Workaround for https://github.com/llvm/llvm-project/issues/97627
const headerpad_size: ?u32 = if (target.os.tag.isDarwin()) 32 else null;
const sub_compilation = Compilation.create(comp.gpa, arena, .{
.dirs = comp.dirs.withoutLocalCache(),
.thread_pool = comp.thread_pool,
.self_exe_path = comp.self_exe_path,
.cache_mode = .whole,
.config = config,
.root_mod = root_mod,
.root_name = root_name,
.libc_installation = comp.libc_installation,
.emit_bin = emit_bin,
.emit_h = null,
.c_source_files = c_source_files.items,
.verbose_cc = comp.verbose_cc,
.verbose_link = comp.verbose_link,
.verbose_air = comp.verbose_air,
.verbose_llvm_ir = comp.verbose_llvm_ir,
.verbose_llvm_bc = comp.verbose_llvm_bc,
.verbose_cimport = comp.verbose_cimport,
.verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
.clang_passthrough_mode = comp.clang_passthrough_mode,
.skip_linker_dependencies = skip_linker_dependencies,
.linker_allow_shlib_undefined = linker_allow_shlib_undefined,
.install_name = install_name,
.headerpad_size = headerpad_size,
}) catch |err| {
comp.setMiscFailure(
.libtsan,
"unable to build thread sanitizer runtime: create compilation failed: {s}",
.{@errorName(err)},
);
return error.SubCompilationFailed;
};
defer sub_compilation.destroy();
comp.updateSubCompilation(sub_compilation, .libtsan, prog_node) catch |err| switch (err) {
error.SubCompilationFailed => return error.SubCompilationFailed,
else => |e| {
comp.setMiscFailure(
.libtsan,
"unable to build thread sanitizer runtime: compilation failed: {s}",
.{@errorName(e)},
);
return error.SubCompilationFailed;
},
};
const crt_file = try sub_compilation.toCrtFile();
comp.queueLinkTaskMode(crt_file.full_object_path, output_mode);
assert(comp.tsan_lib == null);
comp.tsan_lib = crt_file;
}
fn addCcArgs(target: std.Target, args: *std.ArrayList([]const u8)) error{OutOfMemory}!void {
try args.appendSlice(&[_][]const u8{
"-nostdinc++",
"-fvisibility=hidden",
"-fvisibility-inlines-hidden",
"-std=c++17",
"-fno-rtti",
"-fno-exceptions",
"-w", // Disable all warnings.
});
if (target.abi.isAndroid() and target.os.version_range.linux.android >= 29) {
try args.append("-fno-emulated-tls");
}
if (target.isMinGW()) {
try args.append("-fms-extensions");
}
}
const tsan_sources = [_][]const u8{
"tsan_debugging.cpp",
"tsan_external.cpp",
"tsan_fd.cpp",
"tsan_flags.cpp",
"tsan_ignoreset.cpp",
"tsan_interceptors_memintrinsics.cpp",
"tsan_interceptors_posix.cpp",
"tsan_interface.cpp",
"tsan_interface_ann.cpp",
"tsan_interface_atomic.cpp",
"tsan_interface_java.cpp",
"tsan_malloc_mac.cpp",
"tsan_md5.cpp",
"tsan_mman.cpp",
"tsan_mutexset.cpp",
"tsan_new_delete.cpp",
"tsan_platform_windows.cpp",
"tsan_preinit.cpp",
"tsan_report.cpp",
"tsan_rtl.cpp",
"tsan_rtl_access.cpp",
"tsan_rtl_mutex.cpp",
"tsan_rtl_proc.cpp",
"tsan_rtl_report.cpp",
"tsan_rtl_thread.cpp",
"tsan_stack_trace.cpp",
"tsan_suppressions.cpp",
"tsan_symbolize.cpp",
"tsan_sync.cpp",
"tsan_vector_clock.cpp",
};
const darwin_tsan_sources = [_][]const u8{
"tsan_interceptors_mac.cpp",
"tsan_interceptors_mach_vm.cpp",
"tsan_platform_mac.cpp",
"tsan_platform_posix.cpp",
};
const unix_tsan_sources = [_][]const u8{
"tsan_platform_linux.cpp",
"tsan_platform_posix.cpp",
};
const windows_tsan_sources = [_][]const u8{
"tsan_platform_windows.cpp",
};
const sanitizer_common_sources = [_][]const u8{
"sanitizer_allocator.cpp",
"sanitizer_chained_origin_depot.cpp",
"sanitizer_common.cpp",
"sanitizer_deadlock_detector1.cpp",
"sanitizer_deadlock_detector2.cpp",
"sanitizer_errno.cpp",
"sanitizer_file.cpp",
"sanitizer_flag_parser.cpp",
"sanitizer_flags.cpp",
"sanitizer_fuchsia.cpp",
"sanitizer_libc.cpp",
"sanitizer_libignore.cpp",
"sanitizer_linux.cpp",
"sanitizer_linux_s390.cpp",
"sanitizer_mac.cpp",
"sanitizer_mutex.cpp",
"sanitizer_netbsd.cpp",
"sanitizer_platform_limits_freebsd.cpp",
"sanitizer_platform_limits_linux.cpp",
"sanitizer_platform_limits_netbsd.cpp",
"sanitizer_platform_limits_posix.cpp",
"sanitizer_platform_limits_solaris.cpp",
"sanitizer_posix.cpp",
"sanitizer_printf.cpp",
"sanitizer_procmaps_bsd.cpp",
"sanitizer_procmaps_common.cpp",
"sanitizer_procmaps_fuchsia.cpp",
"sanitizer_procmaps_linux.cpp",
"sanitizer_procmaps_mac.cpp",
"sanitizer_procmaps_solaris.cpp",
"sanitizer_range.cpp",
"sanitizer_solaris.cpp",
"sanitizer_stoptheworld_fuchsia.cpp",
"sanitizer_stoptheworld_mac.cpp",
"sanitizer_stoptheworld_win.cpp",
"sanitizer_suppressions.cpp",
"sanitizer_termination.cpp",
"sanitizer_thread_arg_retval.cpp",
"sanitizer_thread_registry.cpp",
"sanitizer_tls_get_addr.cpp",
"sanitizer_type_traits.cpp",
"sanitizer_win.cpp",
"sanitizer_win_interception.cpp",
};
const sanitizer_nolibc_sources = [_][]const u8{
"sanitizer_common_nolibc.cpp",
};
const sanitizer_libcdep_sources = [_][]const u8{
"sanitizer_common_libcdep.cpp",
"sanitizer_allocator_checks.cpp",
"sanitizer_dl.cpp",
"sanitizer_linux_libcdep.cpp",
"sanitizer_mac_libcdep.cpp",
"sanitizer_posix_libcdep.cpp",
"sanitizer_stoptheworld_linux_libcdep.cpp",
"sanitizer_stoptheworld_netbsd_libcdep.cpp",
};
const sanitizer_symbolizer_sources = [_][]const u8{
"sanitizer_allocator_report.cpp",
"sanitizer_stack_store.cpp",
"sanitizer_stackdepot.cpp",
"sanitizer_stacktrace.cpp",
"sanitizer_stacktrace_libcdep.cpp",
"sanitizer_stacktrace_printer.cpp",
"sanitizer_stacktrace_sparc.cpp",
"sanitizer_symbolizer.cpp",
"sanitizer_symbolizer_libbacktrace.cpp",
"sanitizer_symbolizer_libcdep.cpp",
"sanitizer_symbolizer_mac.cpp",
"sanitizer_symbolizer_markup.cpp",
"sanitizer_symbolizer_markup_fuchsia.cpp",
"sanitizer_symbolizer_posix_libcdep.cpp",
"sanitizer_symbolizer_report.cpp",
"sanitizer_symbolizer_report_fuchsia.cpp",
"sanitizer_symbolizer_win.cpp",
"sanitizer_thread_history.cpp",
"sanitizer_unwind_linux_libcdep.cpp",
"sanitizer_unwind_fuchsia.cpp",
"sanitizer_unwind_win.cpp",
};
const interception_sources = [_][]const u8{
"interception_linux.cpp",
"interception_mac.cpp",
"interception_win.cpp",
"interception_type_test.cpp",
};