Skip to content

Commit 69c8e8e

Browse files
committed
frontend: fix stack protector option logic
Commit 97e2389 regressed this behavior because it made target_util.supportsStackProtector *correctly* notice which zig backend is being used to generate code, while the logic calling that function *incorrectly assumed* that .zig code is being compiled, when in reality it might be only C code being compiled. This commit adjusts the option resolution logic for stack protector so that it takes into account the zig backend only if there is a zig compilation unit. A separate piece of logic checks whether clang supports stack protector for a given target. closes #18009 closes #18114 closes #18254
1 parent 484c0a2 commit 69c8e8e

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

src/Compilation/Config.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ shared_memory: bool,
3131
is_test: bool,
3232
test_evented_io: bool,
3333
entry: ?[]const u8,
34+
any_c_source_files: bool,
3435

3536
pub const CFrontend = enum { clang, aro };
3637

@@ -48,7 +49,7 @@ pub const Options = struct {
4849
any_sanitize_thread: bool = false,
4950
any_unwind_tables: bool = false,
5051
any_dyn_libs: bool = false,
51-
c_source_files_len: usize = 0,
52+
any_c_source_files: bool = false,
5253
emit_llvm_ir: bool = false,
5354
emit_llvm_bc: bool = false,
5455
link_libc: ?bool = null,
@@ -231,7 +232,7 @@ pub fn resolve(options: Options) !Config {
231232
}
232233

233234
if (options.lto) |x| break :b x;
234-
if (options.c_source_files_len == 0) break :b false;
235+
if (!options.any_c_source_files) break :b false;
235236

236237
if (target.cpu.arch.isRISCV()) {
237238
// Clang and LLVM currently don't support RISC-V target-abi for LTO.
@@ -384,6 +385,7 @@ pub fn resolve(options: Options) !Config {
384385
.use_lld = use_lld,
385386
.entry = entry,
386387
.wasi_exec_model = wasi_exec_model,
388+
.any_c_source_files = options.any_c_source_files,
387389
};
388390
}
389391

src/Package/Module.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,18 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
229229
};
230230

231231
const stack_protector: u32 = sp: {
232-
if (!target_util.supportsStackProtector(target, zig_backend)) {
232+
const use_zig_backend = options.global.have_zcu or
233+
(options.global.any_c_source_files and options.global.c_frontend == .aro);
234+
if (use_zig_backend and !target_util.supportsStackProtector(target, zig_backend)) {
235+
if (options.inherited.stack_protector) |x| {
236+
if (x > 0) return error.StackProtectorUnsupportedByTarget;
237+
}
238+
break :sp 0;
239+
}
240+
241+
if (options.global.any_c_source_files and options.global.c_frontend == .clang and
242+
!target_util.clangSupportsStackProtector(target))
243+
{
233244
if (options.inherited.stack_protector) |x| {
234245
if (x > 0) return error.StackProtectorUnsupportedByTarget;
235246
}

src/main.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ fn buildOutputType(
949949
// Populated just before the call to `createModule`.
950950
.emit_bin = undefined,
951951
// Populated just before the call to `createModule`.
952-
.c_source_files_len = undefined,
952+
.any_c_source_files = undefined,
953953
},
954954
// Populated in the call to `createModule` for the root module.
955955
.resolved_options = undefined,
@@ -2635,7 +2635,7 @@ fn buildOutputType(
26352635
create_module.opts.emit_llvm_ir = emit_llvm_ir != .no;
26362636
create_module.opts.emit_llvm_bc = emit_llvm_bc != .no;
26372637
create_module.opts.emit_bin = emit_bin != .no;
2638-
create_module.opts.c_source_files_len = create_module.c_source_files.items.len;
2638+
create_module.opts.any_c_source_files = create_module.c_source_files.items.len != 0;
26392639

26402640
const main_mod = try createModule(gpa, arena, &create_module, 0, null, zig_lib_directory);
26412641
for (create_module.modules.keys(), create_module.modules.values()) |key, cli_mod| {

src/target.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ pub fn supportsStackProtector(target: std.Target, backend: std.builtin.CompilerB
360360
};
361361
}
362362

363+
pub fn clangSupportsStackProtector(target: std.Target) bool {
364+
return switch (target.cpu.arch) {
365+
.spirv32, .spirv64 => return false,
366+
else => true,
367+
};
368+
}
369+
363370
pub fn libcProvidesStackProtector(target: std.Target) bool {
364371
return !target.isMinGW() and target.os.tag != .wasi and !target.isSpirV();
365372
}

0 commit comments

Comments
 (0)