Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,6 @@ endif
endif
endif
endif
ifdef MAC
ifeq ($(filter -cc,$(VFLAGS)),)
ifeq ($(findstring -cc=,$(VFLAGS)),)
ifeq ($(shell expr $(shell uname -r | cut -d. -f1) \>= 27), 1)
# Darwin 27's dyld fills the zero-fill pages of TCC-linked executables with file
# bytes, corrupting their globals (see pref.host_rejects_tcc_executables), so keep
# both `v1 -> v2` and `v2 -> v` on the system compiler.
BOOTSTRAP_CCOMPILER_VFLAG := -cc "$(CC)"
BOOTSTRAP_VC_CCOMPILER_VFLAG := $(BOOTSTRAP_CCOMPILER_VFLAG)
endif
endif
endif
endif
ifeq ($(LINUX),1)
ifneq ($(BOOTSTRAP_TCC_REQUESTED),)
ifneq ($(CC),tcc)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ working `tcc` from `PATH`, before using the platform compiler. The system fallba
when required V runtime artifacts are available, including the bundled `libgc.a` used by default
glibc and Windows Boehm builds.

On macOS 27 (Darwin 27) and later, V never selects TCC by itself, and `make` and `v self` build
V with the system compiler: depending on their layout, TCC-linked executables can start there
with corrupted global variables ([#29744](https://github.com/vlang/v/issues/29744)). An explicit
`-cc tcc` is still honored.
On macOS, regular builds and `v self` use TCC by default, including on Darwin 27 and later.
Use a current TCC bundle built with the Mach-O zero-fill fix for
[#29744](https://github.com/vlang/v/issues/29744); older bundles can corrupt global variables.
An explicit `-cc` overrides the default, and `CC` also overrides the compiler used by `v self`.

`-prod` builds never default to TCC, which cannot do their optimizations. On x64 Windows,
without `-cc`, a `-prod` build uses MSVC when an x64 Visual Studio Developer environment is
Expand Down
7 changes: 4 additions & 3 deletions cmd/tools/install_v1_fallback.sh
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,11 @@ download_release() {
return 1
fi
mkdir -p "$release_tree" || return 1
if command -v unzip >/dev/null 2>&1; then
unzip -q "$archive" -d "$release_tree" || return 1
elif command -v bsdtar >/dev/null 2>&1; then
# macOS unzip rejects the release archive's absolute libc.dylib symlink.
if command -v bsdtar >/dev/null 2>&1; then
bsdtar -xf "$archive" -C "$release_tree" || return 1
elif command -v unzip >/dev/null 2>&1; then
unzip -q "$archive" -d "$release_tree" || return 1
elif command -v tar >/dev/null 2>&1; then
tar -xf "$archive" -C "$release_tree" || return 1
else
Expand Down
60 changes: 60 additions & 0 deletions cmd/tools/install_v1_fallback_macos_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import crypto.sha256
import os
import v.cmdexec

fn test_v1_release_installer_preserves_absolute_library_symlinks() {
zip := os.find_abs_path_of_executable('zip') or { return }
os.find_abs_path_of_executable('bsdtar') or { return }
os.find_abs_path_of_executable('curl') or { return }
dir := os.join_path(os.vtmp_dir(), 'v1_installer_library_link_${os.getpid()}')
os.mkdir_all(dir)!
os.chmod(dir, 0o700)!
defer {
os.rmdir_all(dir) or {}
}
release := os.join_path(dir, 'v')
for path in ['thirdparty/tcc/lib', 'vlib/crypto/internal/subtle', 'vlib/x/json2'] {
os.mkdir_all(os.join_path(release, path))!
}
os.write_file(os.join_path(release, 'v'), '#!/bin/sh\necho "V 0.5.2 test-release"\n')!
os.chmod(os.join_path(release, 'v'), 0o755)!
for path in ['vlib/crypto/internal/subtle/aliasing.v', 'vlib/crypto/internal/subtle/comparison.v',
'vlib/x/json2/json2.v'] {
os.write_file(os.join_path(release, path), '// release module\n')!
}
// This mirrors the absolute library link in the official macOS archive.
library := os.join_path(dir, 'system-library.dylib')
os.write_file(library, 'library fixture')!
os.symlink(library, os.join_path(release, 'thirdparty/tcc/lib/libc.dylib'))!
archive := os.join_path(dir, 'release.zip')
packed := cmdexec.run_in(zip, ['-q', '-y', '-r', archive, 'v'], dir)
assert packed.exit_code == 0, packed.output
bootstrap := os.join_path(dir, 'unexpected-bootstrap')
os.write_file(bootstrap, '#!/bin/sh\necho "unexpected source fallback" >&2\nexit 1\n')!
os.chmod(bootstrap, 0o755)!
cache := os.join_path(dir, 'cache')
output := os.join_path(dir, 'installed', 'v1_fallback')
installer := os.join_path(@VEXEROOT, 'cmd', 'tools', 'install_v1_fallback.sh')
installed := cmdexec.run_in_merged_with_env('/bin/sh', [installer, bootstrap, output], @VEXEROOT,
{
'TMPDIR': dir
'V1_FALLBACK_CACHE_DIR': cache
'V1_FALLBACK_RELEASE_URL': 'file://${archive}'
'V1_FALLBACK_RELEASE_SHA256': sha256.sum(os.read_bytes(archive)!).hex()
'V1_FALLBACK_BOOTSTRAP': bootstrap
'V1_FALLBACK_OUTPUT': output
})
assert installed.exit_code == 0, installed.output
assert installed.output.contains('Installed the V 0.5.2 release fallback'), installed.output
assert !installed.output.contains('Building the V 0.5.2 fallback'), installed.output
assert os.read_file(output + '.vroot')!.trim_space() == os.join_path(cache, '0.5.2')
link := os.join_path(cache, '0.5.2', 'thirdparty/tcc/lib/libc.dylib')
assert os.is_link(link)
assert os.real_path(link) == os.real_path(library)
assert os.read_file(link)! == 'library fixture'
assert os.is_file(os.join_path(cache, '0.5.2', 'vlib/crypto/subtle/aliasing.v'))
assert os.is_file(os.join_path(cache, '0.5.2', 'vlib/json2/.v1-fallback-complete'))
run := cmdexec.run(output, ['version'])
assert run.exit_code == 0, run.output
assert run.output.trim_space() == 'V 0.5.2 test-release', run.output
}
13 changes: 3 additions & 10 deletions cmd/tools/vself.v
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,10 @@ fn main() {
// compiling by default, i.e. `v self`:
unam := os.uname()
if host_os == 'macos' {
// Apple Silicon's bundled TCC is much faster for compiler rebuilds. The
// The bundled TCC is much faster for compiler rebuilds. The
// generated compiler uses pthread-backed allocator state because native
// TinyCC TLS is not reliable on macOS. Newer macOS releases cannot load what
// TCC links at all (see pref.host_rejects_tcc_executables).
default_cc := if unam.machine in ['arm64', 'aarch64']
&& !pref.host_rejects_tcc_executables() {
'tcc'
} else {
'cc'
}
args << ['-cc', os.getenv_opt('CC') or { default_cc }]
// TinyCC TLS is not reliable on macOS.
args << ['-cc', os.getenv_opt('CC') or { 'tcc' }]
} else if host_os == 'linux'
&& (unam.machine in ['arm64', 'aarch64'] || pref.host_arch() == 'arm32') {
// Bundled TCC can hang while bootstrapping V on Linux ARM64, so
Expand Down
9 changes: 1 addition & 8 deletions cmd/tools/vself_test.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import v.pref

const vexe = @VEXE
const vroot = os.dir(vexe)
Expand Down Expand Up @@ -152,14 +151,8 @@ fn test_macos_default_self_build_compiler_selection() {
os.exec(['env', '-u', 'CC', 'VFLAGS=', 'VEXE=' + '${noop}', '${tool}', 'self', '-o',
'/tmp/vself_macos_prealloc_test'])
assert default_result.exit_code == 0, default_result.output
default_cc := if os.uname().machine in ['arm64', 'aarch64']
&& !pref.host_rejects_tcc_executables() {
'tcc'
} else {
'cc'
}
assert !default_result.output.contains('-b fastc'), default_result.output
assert default_result.output.contains('-cc ${default_cc}'), default_result.output
assert default_result.output.contains('-cc tcc'), default_result.output
assert default_result.output.contains('-prealloc'), default_result.output
assert_vself_preserves_full_cli(default_result.output)
override_result :=
Expand Down
8 changes: 1 addition & 7 deletions vlib/sync/stdatomic/tcc_macos_atomic_fence_test.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import v.cmdexec
import v.pref

fn test_macos_tcc_links_atomic_thread_fences_without_fallback() ! {
$if !macos {
Expand Down Expand Up @@ -30,12 +29,7 @@ fn main() {
}
')!
exe := os.join_path(root, 'main')
// Without `-cc`, V selects TCC only on hosts that can load its executables.
compiler_variants := if pref.host_rejects_tcc_executables() {
[['-cc', 'tcc']]
} else {
[([]string{}), ['-cc', 'tcc']]
}
compiler_variants := [([]string{}), ['-cc', 'tcc']]
for compiler_args in compiler_variants {
for gc_mode in ['none', 'boehm'] {
mut args := ['-new-compiler', '-nocache', '-no-retry-compilation', '-gc', gc_mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() {
}
'
os.write_file(src, source) or { panic(err) }
compile := os.exec([lmsr_vexe, '-nocache', '-no-memory-limit', '${src}', '-b', 'c', '-o', '${out}'])
compile := os.exec([lmsr_vexe, '-nocache', '-no-memory-limit', '-b', 'c', '-o', out, src])
assert compile.exit_code == 0, compile.output
assert !compile.output.contains('C compilation failed'), compile.output
run := os.exec([out])
Expand Down
38 changes: 38 additions & 0 deletions vlib/v/compiler_tests/tcc_globals_macos_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import strings
import v.cmdexec

fn test_tcc_macos_preserves_zero_initialized_globals() {
tcc_dir := os.join_path(@VEXEROOT, 'thirdparty', 'tcc')
tcc := os.join_path(tcc_dir, 'tcc.exe')
if !os.is_file(tcc) {
return
}
dir := os.join_path(os.vtmp_dir(), 'tcc_zero_globals_${os.getpid()}')
os.mkdir_all(dir)!
defer {
os.rmdir_all(dir) or {}
}
mut source := strings.new_builder(400000)
source.write_string('#include <stdio.h>\nchar data_bytes[100] = {1};\nchar *data_ptr = data_bytes;\nstatic char zero_bytes[32768];\n')
// A large symbol table keeps the file long enough for dyld page-in linking.
// Chained fixups must exclude the zero-fill pages even when they fit in the file.
for i in 0 .. 6000 {
source.writeln('int padding_function_with_a_long_name_${i}(void) { return ${i}; }')
}
source.write_string('int main(void) {
long bad = 0;
for (long i = 0; i < (long)sizeof(zero_bytes); i++) bad += zero_bytes[i] != 0;
printf("non-zero __bss bytes: %ld\\n", bad);
return bad != 0 || data_ptr != data_bytes || data_ptr[0] != 1;
}
')
cfile := os.join_path(dir, 'globals.c')
executable := os.join_path(dir, 'globals')
os.write_file(cfile, source.str())!
compile := cmdexec.run(tcc, ['-B', tcc_dir, '-o', executable, cfile])
assert compile.exit_code == 0, compile.output
run := cmdexec.run(executable, [])
assert run.exit_code == 0, run.output
assert run.output.trim_space() == 'non-zero __bss bytes: 0', run.output
}
67 changes: 42 additions & 25 deletions vlib/v/driver/c_compiler_flags_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2128,31 +2128,48 @@ fn test_tcc_compiler_identity_tracks_same_size_rebuilds() {
assert first != tcc_compiler_identity(compiler)
}

// A host whose loader corrupts TCC-linked executables never selects TCC
// implicitly, neither the bundled one nor a system one, but still honors an
// explicit `-cc tcc`.
fn test_v3_host_rejecting_tcc_executables_never_selects_tcc_implicitly() {
host := pref.host_target()
options := V3BundledTccProbeOptions{
backend: 'c'
c_compiler: 'cc'
host_os: host.os
host_target: host
target: host
bundled_tcc: os.join_path(@VEXEROOT, 'thirdparty', 'tcc', 'tcc.exe')
host_rejects_tcc: true
}
assert !v3_should_probe_bundled_tcc(options)
selection := v3_select_c_compiler(@VEXEROOT, options)
assert !selection.bundled_tcc_available
assert selection.implicit_tcc == ''
assert !selection.c_compiler.contains('tcc')
assert selection.effective_c_compiler != 'tinyc'
assert v3_should_probe_bundled_tcc(V3BundledTccProbeOptions{
...options
c_compiler: 'tcc'
c_compiler_explicit: true
})
// Native macOS builds use the bundled TCC by default on both supported architectures.
fn test_v3_native_macos_selects_bundled_tcc_by_default() {
$if windows {
// The fake compiler is a shell script.
return
}
root := os.join_path(os.vtmp_dir(), 'v3_macos_default_tcc_${os.getpid()}')
os.mkdir_all(root)!
defer {
os.rmdir_all(root) or {}
}
bundled_tcc := write_v3_test_tcc(os.join_path(root, 'thirdparty', 'tcc', 'tcc.exe'), 0)
for arch in ['arm64', 'amd64'] {
target := pref.Target{
os: 'macos'
arch: arch
}
options := V3BundledTccProbeOptions{
backend: 'c'
c_compiler: 'cc'
host_os: 'macos'
host_target: target
target: target
bundled_tcc: bundled_tcc
}
assert v3_should_probe_bundled_tcc(options), arch
selection := v3_select_c_compiler(root, options)
assert selection.bundled_tcc_available, arch
assert selection.implicit_tcc == bundled_tcc, arch
assert selection.c_compiler == bundled_tcc, arch
assert selection.use_implicit_tcc_semantics, arch
assert selection.effective_c_compiler == 'tinyc', arch
// An explicit compiler still overrides the platform default.
explicit := v3_select_c_compiler(root, V3BundledTccProbeOptions{
...options
c_compiler: 'clang'
c_compiler_explicit: true
})
assert explicit.c_compiler == 'clang', arch
assert explicit.implicit_tcc == '', arch
assert !explicit.use_implicit_tcc_semantics, arch
}
}

fn test_v3_cstrict_flag_plan_has_one_conversion_warning_override() {
Expand Down
6 changes: 2 additions & 4 deletions vlib/v/driver/driver.v
Original file line number Diff line number Diff line change
Expand Up @@ -8842,7 +8842,6 @@ struct V3BundledTccProbeOptions {
host_target pref.Target
target pref.Target
bundled_tcc string
host_rejects_tcc bool // see pref.host_rejects_tcc_executables; only an explicit `-cc tcc` selects TCC
prefer_platform_cc bool // Source-built precise Boehm needs the platform compiler by default.
}

Expand All @@ -8860,7 +8859,7 @@ fn v3_should_probe_bundled_tcc(options V3BundledTccProbeOptions) bool {
}
return os.real_path(compiler_path) == os.real_path(options.bundled_tcc)
}
if options.host_rejects_tcc || options.prefer_platform_cc {
if options.prefer_platform_cc {
return false
}
if options.is_shared && !options.is_liveshared && options.target.os == 'linux' {
Expand Down Expand Up @@ -9026,7 +9025,7 @@ fn v3_select_c_compiler(vroot string, requested V3BundledTccProbeOptions) V3CCom
bundled_tcc_available := v3_bundled_tcc_available(options)
allow_system_tcc := options.backend == 'c' && !options.c_only && !options.is_prod
&& !options.is_c_debug && !options.race && !options.c_compiler_explicit
&& !options.host_rejects_tcc && !options.prefer_platform_cc
&& !options.prefer_platform_cc
&& !(options.is_shared && !options.is_liveshared && options.target.os == 'linux')
&& (!options.parallel_cc || options.target.os == 'windows')
&& options.target.os == options.host_target.os
Expand Down Expand Up @@ -12832,7 +12831,6 @@ pub fn run(args []string) {
host_target: host_target
target: target
bundled_tcc: bundled_tcc
host_rejects_tcc: pref.host_rejects_tcc_executables()
prefer_platform_cc: v3_gc_prefers_platform_cc(user_defines) || bare_needs_platform_cc
})
bundled_tcc_available := selection.bundled_tcc_available
Expand Down
4 changes: 0 additions & 4 deletions vlib/v/driver/implicit_tcc_fallback_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ fn test_forced_implicit_tcc_failure_reports_the_fallback() {
eprintln('skipping: no bundled or system tcc available for implicit tcc selection')
return
}
if pref.host_rejects_tcc_executables() {
eprintln('skipping: this host never selects tcc implicitly')
return
}
fallback := v3_platform_c_compiler_command(os.user_os())
os.find_abs_path_of_executable(fallback) or {
eprintln('skipping: no ${fallback} to fall back to')
Expand Down
1 change: 0 additions & 1 deletion vlib/v/driver/vtest_build_environment.v
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ pub fn vtest_build_environment(vroot string, args []string) TestBuildEnvironment
host_target: host_target
target: target
bundled_tcc: os.join_path(vroot, 'thirdparty', 'tcc', 'tcc.exe')
host_rejects_tcc: pref.host_rejects_tcc_executables()
})
v3_apply_libc_define(mut user_defines, mut compile_values, libc_mode, selection.c_compiler,
v3_should_infer_host_libc(false, false, '', cross_output, target, host_target))
Expand Down
7 changes: 4 additions & 3 deletions vlib/v/pref/os_targets.v
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ pub fn host_os_name() string {
// https://github.com/vlang/v/issues/29744 .
const first_tcc_unloadable_darwin_release = 27

// host_rejects_tcc_executables reports whether executables that TCC links for this
// machine can start with corrupted global variables, so V must not pick TCC by
// itself. An explicit `-cc tcc` is still honored.
// host_rejects_tcc_executables reports whether this Darwin release was affected
// by the historical TinyCC Mach-O zero-fill bug. Retained for compatibility;
// compiler selection no longer consults this version check. Fixed TCC bundles
// can run on these releases.
pub fn host_rejects_tcc_executables() bool {
$if macos {
return darwin_release_rejects_tcc_executables(os.uname().release)
Expand Down
Loading