Skip to content

Commit 53f74d6

Browse files
committed
move libssp into libcompiler_rt
closes #7265
1 parent 2a81a0f commit 53f74d6

File tree

6 files changed

+60
-148
lines changed

6 files changed

+60
-148
lines changed

lib/compiler_rt.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,6 @@ comptime {
233233
_ = @import("compiler_rt/memmove.zig");
234234
_ = @import("compiler_rt/memcmp.zig");
235235
_ = @import("compiler_rt/bcmp.zig");
236+
_ = @import("compiler_rt/ssp.zig");
236237
}
237238
}

lib/ssp.zig renamed to lib/compiler_rt/ssp.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,44 @@
1313
//! - __vsprintf_chk
1414

1515
const std = @import("std");
16+
const common = @import("./common.zig");
17+
const builtin = @import("builtin");
1618

1719
extern fn strncpy(dest: [*:0]u8, src: [*:0]const u8, n: usize) callconv(.C) [*:0]u8;
1820
extern fn memset(dest: ?[*]u8, c: u8, n: usize) callconv(.C) ?[*]u8;
1921
extern fn memcpy(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
2022
extern fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) callconv(.C) ?[*]u8;
2123

22-
// Avoid dragging in the runtime safety mechanisms into this .o file.
23-
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?usize) noreturn {
24-
_ = msg;
25-
_ = error_return_trace;
26-
@setCold(true);
27-
std.os.abort();
24+
comptime {
25+
@export(__stack_chk_fail, .{ .name = "__stack_chk_fail", .linkage = common.linkage, .visibility = common.visibility });
26+
@export(__chk_fail, .{ .name = "__chk_fail", .linkage = common.linkage, .visibility = common.visibility });
27+
@export(__stack_chk_guard, .{ .name = "__stack_chk_guard", .linkage = common.linkage, .visibility = common.visibility });
28+
@export(__strcpy_chk, .{ .name = "__strcpy_chk", .linkage = common.linkage, .visibility = common.visibility });
29+
@export(__strncpy_chk, .{ .name = "__strncpy_chk", .linkage = common.linkage, .visibility = common.visibility });
30+
@export(__strcat_chk, .{ .name = "__strcat_chk", .linkage = common.linkage, .visibility = common.visibility });
31+
@export(__strncat_chk, .{ .name = "__strncat_chk", .linkage = common.linkage, .visibility = common.visibility });
32+
@export(__memcpy_chk, .{ .name = "__memcpy_chk", .linkage = common.linkage, .visibility = common.visibility });
33+
@export(__memmove_chk, .{ .name = "__memmove_chk", .linkage = common.linkage, .visibility = common.visibility });
34+
@export(__memset_chk, .{ .name = "__memset_chk", .linkage = common.linkage, .visibility = common.visibility });
2835
}
2936

30-
export fn __stack_chk_fail() callconv(.C) noreturn {
37+
fn __stack_chk_fail() callconv(.C) noreturn {
3138
@panic("stack smashing detected");
3239
}
3340

34-
export fn __chk_fail() callconv(.C) noreturn {
41+
fn __chk_fail() callconv(.C) noreturn {
3542
@panic("buffer overflow detected");
3643
}
3744

38-
// Emitted when targeting some architectures (eg. x86)
39-
// XXX: This symbol should be hidden
40-
export fn __stack_chk_fail_local() callconv(.C) noreturn {
41-
__stack_chk_fail();
42-
}
43-
44-
// XXX: Initialize the canary with random data
45-
export var __stack_chk_guard: usize = blk: {
45+
// TODO: Initialize the canary with random data
46+
var __stack_chk_guard: usize = blk: {
4647
var buf = [1]u8{0} ** @sizeOf(usize);
4748
buf[@sizeOf(usize) - 1] = 255;
4849
buf[@sizeOf(usize) - 2] = '\n';
4950
break :blk @as(usize, @bitCast(buf));
5051
};
5152

52-
export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
53+
fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
5354
@setRuntimeSafety(false);
5455

5556
var i: usize = 0;
@@ -64,12 +65,12 @@ export fn __strcpy_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
6465
return dest;
6566
}
6667

67-
export fn __strncpy_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
68+
fn __strncpy_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
6869
if (dest_n < n) __chk_fail();
6970
return strncpy(dest, src, n);
7071
}
7172

72-
export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
73+
fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callconv(.C) [*:0]u8 {
7374
@setRuntimeSafety(false);
7475

7576
var avail = dest_n;
@@ -94,7 +95,7 @@ export fn __strcat_chk(dest: [*:0]u8, src: [*:0]const u8, dest_n: usize) callcon
9495
return dest;
9596
}
9697

97-
export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
98+
fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usize) callconv(.C) [*:0]u8 {
9899
@setRuntimeSafety(false);
99100

100101
var avail = dest_n;
@@ -119,17 +120,17 @@ export fn __strncat_chk(dest: [*:0]u8, src: [*:0]const u8, n: usize, dest_n: usi
119120
return dest;
120121
}
121122

122-
export fn __memcpy_chk(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123+
fn __memcpy_chk(noalias dest: ?[*]u8, noalias src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
123124
if (dest_n < n) __chk_fail();
124125
return memcpy(dest, src, n);
125126
}
126127

127-
export fn __memmove_chk(dest: ?[*]u8, src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128+
fn __memmove_chk(dest: ?[*]u8, src: ?[*]const u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
128129
if (dest_n < n) __chk_fail();
129130
return memmove(dest, src, n);
130131
}
131132

132-
export fn __memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133+
fn __memset_chk(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
133134
if (dest_n < n) __chk_fail();
134135
return memset(dest, c, n);
135136
}

src/Compilation.zig

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ libunwind_static_lib: ?CRTFile = null,
152152
/// Populated when we build the TSAN static library. A Job to build this is placed in the queue
153153
/// and resolved before calling linker.flush().
154154
tsan_static_lib: ?CRTFile = null,
155-
/// Populated when we build the libssp static library. A Job to build this is placed in the queue
156-
/// and resolved before calling linker.flush().
157-
libssp_static_lib: ?CRTFile = null,
158155
/// Populated when we build the libc static library. A Job to build this is placed in the queue
159156
/// and resolved before calling linker.flush().
160157
libc_static_lib: ?CRTFile = null,
@@ -286,7 +283,6 @@ const Job = union(enum) {
286283
libcxx: void,
287284
libcxxabi: void,
288285
libtsan: void,
289-
libssp: void,
290286
/// needed when not linking libc and using LLVM for code generation because it generates
291287
/// calls to, for example, memcpy and memset.
292288
zig_libc: void,
@@ -683,7 +679,6 @@ pub const MiscTask = enum {
683679
libtsan,
684680
wasi_libc_crt_file,
685681
compiler_rt,
686-
libssp,
687682
zig_libc,
688683
analyze_mod,
689684

@@ -1353,10 +1348,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13531348
if (stack_check and !target_util.supportsStackProbing(options.target))
13541349
return error.StackCheckUnsupportedByTarget;
13551350

1356-
const capable_of_building_ssp = canBuildLibSsp(options.target, use_llvm);
1357-
1358-
const stack_protector: u32 = options.want_stack_protector orelse b: {
1359-
if (!target_util.supportsStackProtector(options.target)) break :b @as(u32, 0);
1351+
const stack_protector: u32 = sp: {
1352+
const zig_backend = zigBackend(options.target, use_llvm);
1353+
if (!target_util.supportsStackProtector(options.target, zig_backend)) {
1354+
if (options.want_stack_protector) |x| {
1355+
if (x > 0) return error.StackProtectorUnsupportedByTarget;
1356+
}
1357+
break :sp 0;
1358+
}
13601359

13611360
// This logic is checking for linking libc because otherwise our start code
13621361
// which is trying to set up TLS (i.e. the fs/gs registers) but the stack
@@ -1365,19 +1364,17 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13651364
// as being exempt from stack protection checks, we could change this logic
13661365
// to supporting stack protection even when not linking libc.
13671366
// TODO file issue about this
1368-
if (!link_libc) break :b 0;
1369-
if (!capable_of_building_ssp) break :b 0;
1370-
if (is_safe_mode) break :b default_stack_protector_buffer_size;
1371-
break :b 0;
1367+
if (!link_libc) {
1368+
if (options.want_stack_protector) |x| {
1369+
if (x > 0) return error.StackProtectorUnavailableWithoutLibC;
1370+
}
1371+
break :sp 0;
1372+
}
1373+
1374+
if (options.want_stack_protector) |x| break :sp x;
1375+
if (is_safe_mode) break :sp default_stack_protector_buffer_size;
1376+
break :sp 0;
13721377
};
1373-
if (stack_protector != 0) {
1374-
if (!target_util.supportsStackProtector(options.target))
1375-
return error.StackProtectorUnsupportedByTarget;
1376-
if (!capable_of_building_ssp)
1377-
return error.StackProtectorUnsupportedByBackend;
1378-
if (!link_libc)
1379-
return error.StackProtectorUnavailableWithoutLibC;
1380-
}
13811378

13821379
const include_compiler_rt = options.want_compiler_rt orelse
13831380
(!options.skip_linker_dependencies and is_exe_or_dyn_lib);
@@ -2195,24 +2192,11 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
21952192
comp.job_queued_compiler_rt_obj = true;
21962193
}
21972194
}
2198-
if (needsCSymbols(
2199-
options.skip_linker_dependencies,
2200-
options.output_mode,
2201-
options.link_mode,
2202-
options.target,
2203-
comp.bin_file.options.use_llvm,
2204-
)) {
2205-
// Related: https://github.com/ziglang/zig/issues/7265.
2206-
if (comp.bin_file.options.stack_protector != 0 and
2207-
(!comp.bin_file.options.link_libc or
2208-
!target_util.libcProvidesStackProtector(target)))
2209-
{
2210-
try comp.work_queue.writeItem(.{ .libssp = {} });
2211-
}
22122195

2213-
if (!comp.bin_file.options.link_libc and capable_of_building_zig_libc) {
2214-
try comp.work_queue.writeItem(.{ .zig_libc = {} });
2215-
}
2196+
if (!comp.bin_file.options.skip_linker_dependencies and is_exe_or_dyn_lib and
2197+
!comp.bin_file.options.link_libc and capable_of_building_zig_libc)
2198+
{
2199+
try comp.work_queue.writeItem(.{ .zig_libc = {} });
22162200
}
22172201
}
22182202

@@ -2258,9 +2242,6 @@ pub fn destroy(self: *Compilation) void {
22582242
if (self.compiler_rt_obj) |*crt_file| {
22592243
crt_file.deinit(gpa);
22602244
}
2261-
if (self.libssp_static_lib) |*crt_file| {
2262-
crt_file.deinit(gpa);
2263-
}
22642245
if (self.libc_static_lib) |*crt_file| {
22652246
crt_file.deinit(gpa);
22662247
}
@@ -4027,26 +4008,6 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: *std.Progress.Node) !v
40274008
);
40284009
};
40294010
},
4030-
.libssp => {
4031-
const named_frame = tracy.namedFrame("libssp");
4032-
defer named_frame.end();
4033-
4034-
comp.buildOutputFromZig(
4035-
"ssp.zig",
4036-
.Lib,
4037-
&comp.libssp_static_lib,
4038-
.libssp,
4039-
prog_node,
4040-
) catch |err| switch (err) {
4041-
error.OutOfMemory => return error.OutOfMemory,
4042-
error.SubCompilationFailed => return, // error reported already
4043-
else => comp.lockAndSetMiscFailure(
4044-
.libssp,
4045-
"unable to build libssp: {s}",
4046-
.{@errorName(err)},
4047-
),
4048-
};
4049-
},
40504011
.zig_libc => {
40514012
const named_frame = tracy.namedFrame("zig_libc");
40524013
defer named_frame.end();
@@ -6531,21 +6492,6 @@ fn canBuildLibCompilerRt(target: std.Target, use_llvm: bool) bool {
65316492
};
65326493
}
65336494

6534-
fn canBuildLibSsp(target: std.Target, use_llvm: bool) bool {
6535-
switch (target.os.tag) {
6536-
.plan9 => return false,
6537-
else => {},
6538-
}
6539-
switch (target.cpu.arch) {
6540-
.spirv32, .spirv64 => return false,
6541-
else => {},
6542-
}
6543-
return switch (zigBackend(target, use_llvm)) {
6544-
.stage2_llvm => true,
6545-
else => build_options.have_llvm,
6546-
};
6547-
}
6548-
65496495
/// Not to be confused with canBuildLibC, which builds musl, glibc, and similar.
65506496
/// This one builds lib/c.zig.
65516497
fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool {
@@ -6585,29 +6531,6 @@ fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
65856531
};
65866532
}
65876533

6588-
fn needsCSymbols(
6589-
skip_linker_dependencies: bool,
6590-
output_mode: std.builtin.OutputMode,
6591-
link_mode: ?std.builtin.LinkMode,
6592-
target: std.Target,
6593-
use_llvm: bool,
6594-
) bool {
6595-
if (skip_linker_dependencies)
6596-
return false;
6597-
6598-
switch (output_mode) {
6599-
.Obj => return false,
6600-
.Lib => if (link_mode != .Dynamic) return false,
6601-
.Exe => {},
6602-
}
6603-
6604-
// LLVM might generate calls to libc symbols.
6605-
if (zigBackend(target, use_llvm) == .stage2_llvm)
6606-
return true;
6607-
6608-
return false;
6609-
}
6610-
66116534
pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 {
66126535
const tracy_trace = trace(@src());
66136536
defer tracy_trace.end();

src/link/Coff/lld.zig

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
483483
try argv.append(lib.full_object_path);
484484
}
485485
}
486-
// MinGW doesn't provide libssp symbols
487-
if (target.abi.isGnu()) {
488-
if (comp.libssp_static_lib) |lib| {
489-
try argv.append(lib.full_object_path);
490-
}
491-
}
492486
// MSVC compiler_rt is missing some stuff, so we build it unconditionally but
493487
// and rely on weak linkage to allow MSVC compiler_rt functions to override ours.
494488
if (comp.compiler_rt_obj) |obj| try argv.append(obj.full_object_path);

src/link/Elf.zig

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,12 +1040,6 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10401040
}
10411041
}
10421042

1043-
// stack-protector.
1044-
// Related: https://github.com/ziglang/zig/issues/7265
1045-
if (comp.libssp_static_lib) |ssp| {
1046-
try positionals.append(.{ .path = ssp.full_object_path });
1047-
}
1048-
10491043
for (positionals.items) |obj| {
10501044
var parse_ctx: ParseErrorCtx = .{ .detected_cpu_arch = undefined };
10511045
self.parsePositional(obj.path, obj.must_link, &parse_ctx) catch |err|
@@ -1689,12 +1683,6 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16891683
}
16901684
}
16911685

1692-
// stack-protector.
1693-
// Related: https://github.com/ziglang/zig/issues/7265
1694-
if (comp.libssp_static_lib) |ssp| {
1695-
try argv.append(ssp.full_object_path);
1696-
}
1697-
16981686
// Shared libraries.
16991687
// Worst-case, we need an --as-needed argument for every lib, as well
17001688
// as one before and one after.
@@ -2729,12 +2717,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
27292717
}
27302718
}
27312719

2732-
// stack-protector.
2733-
// Related: https://github.com/ziglang/zig/issues/7265
2734-
if (comp.libssp_static_lib) |ssp| {
2735-
try argv.append(ssp.full_object_path);
2736-
}
2737-
27382720
// Shared libraries.
27392721
if (is_exe_or_dyn_lib) {
27402722
const system_libs = self.base.options.system_libs.keys();

src/target.zig

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,19 @@ pub fn supportsStackProbing(target: std.Target) bool {
328328
(target.cpu.arch == .x86 or target.cpu.arch == .x86_64);
329329
}
330330

331-
pub fn supportsStackProtector(target: std.Target) bool {
332-
return !target.isSpirV();
331+
pub fn supportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
332+
switch (target.os.tag) {
333+
.plan9 => return false,
334+
else => {},
335+
}
336+
switch (target.cpu.arch) {
337+
.spirv32, .spirv64 => return false,
338+
else => {},
339+
}
340+
return switch (backend) {
341+
.stage2_llvm => true,
342+
else => false,
343+
};
333344
}
334345

335346
pub fn libcProvidesStackProtector(target: std.Target) bool {

0 commit comments

Comments
 (0)