authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-24 21:28:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-24 21:28:54-04:00
log02713e8d8aa9641616bd85e77dda784009c96113
tree675d61a60cbe732d2c61290b33db560552b16e05
parentadefd1a52b812813dd3e3590d398f927ffc5b9af

fix race conditions in self-hosted compiler; add test

* fix race condition in std.event.Channel deinit * add support to zig build for --no-rosegment * add passing self-hosted compare-output test for calling a function * put a global lock on LLD linking because it's not thread safe

6 files changed, 67 insertions(+), 32 deletions(-)

build.zig+4
......@@ -45,6 +45,7 @@ pub fn build(b: *Builder) !void {
4545 .c_header_files = nextValue(&index, build_info),
4646 .dia_guids_lib = nextValue(&index, build_info),
4747 .llvm = undefined,
48 .no_rosegment = b.option(bool, "no-rosegment", "Workaround to enable valgrind builds") orelse false,
4849 };
4950 ctx.llvm = try findLLVM(b, ctx.llvm_config_exe);
5051
......@@ -228,6 +229,8 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {
228229 // TODO turn this into -Dextra-lib-path=/lib option
229230 exe.addLibPath("/lib");
230231
232 exe.setNoRoSegment(ctx.no_rosegment);
233
231234 exe.addIncludeDir("src");
232235 exe.addIncludeDir(ctx.cmake_binary_dir);
233236 addCppLib(b, exe, ctx.cmake_binary_dir, "zig_cpp");
......@@ -286,4 +289,5 @@ const Context = struct {
286289 c_header_files: []const u8,
287290 dia_guids_lib: []const u8,
288291 llvm: LibraryDep,
292 no_rosegment: bool,
289293};
src-self-hosted/compilation.zig+3
......@@ -35,6 +35,7 @@ const CInt = @import("c_int.zig").CInt;
3535pub const EventLoopLocal = struct {
3636 loop: *event.Loop,
3737 llvm_handle_pool: std.atomic.Stack(llvm.ContextRef),
38 lld_lock: event.Lock,
3839
3940 /// TODO pool these so that it doesn't have to lock
4041 prng: event.Locked(std.rand.DefaultPrng),
......@@ -55,6 +56,7 @@ pub const EventLoopLocal = struct {
5556
5657 return EventLoopLocal{
5758 .loop = loop,
59 .lld_lock = event.Lock.init(loop),
5860 .llvm_handle_pool = std.atomic.Stack(llvm.ContextRef).init(),
5961 .prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),
6062 .native_libc = event.Future(LibCInstallation).init(loop),
......@@ -63,6 +65,7 @@ pub const EventLoopLocal = struct {
6365
6466 /// Must be called only after EventLoop.run completes.
6567 fn deinit(self: *EventLoopLocal) void {
68 self.lld_lock.deinit();
6669 while (self.llvm_handle_pool.pop()) |node| {
6770 c.LLVMContextDispose(node.data);
6871 self.loop.allocator.destroy(node);
src-self-hosted/link.zig+23-10
......@@ -80,15 +80,22 @@ pub async fn link(comp: *Compilation) !void {
8080
8181 const extern_ofmt = toExternObjectFormatType(comp.target.getObjectFormat());
8282 const args_slice = ctx.args.toSlice();
83 // Not evented I/O. LLD does its own multithreading internally.
84 if (!ZigLLDLink(extern_ofmt, args_slice.ptr, args_slice.len, linkDiagCallback, @ptrCast(*c_void, &ctx))) {
85 if (!ctx.link_msg.isNull()) {
86 // TODO capture these messages and pass them through the system, reporting them through the
87 // event system instead of printing them directly here.
88 // perhaps try to parse and understand them.
89 std.debug.warn("{}\n", ctx.link_msg.toSliceConst());
83
84 {
85 // LLD is not thread-safe, so we grab a global lock.
86 const held = await (async comp.event_loop_local.lld_lock.acquire() catch unreachable);
87 defer held.release();
88
89 // Not evented I/O. LLD does its own multithreading internally.
90 if (!ZigLLDLink(extern_ofmt, args_slice.ptr, args_slice.len, linkDiagCallback, @ptrCast(*c_void, &ctx))) {
91 if (!ctx.link_msg.isNull()) {
92 // TODO capture these messages and pass them through the system, reporting them through the
93 // event system instead of printing them directly here.
94 // perhaps try to parse and understand them.
95 std.debug.warn("{}\n", ctx.link_msg.toSliceConst());
96 }
97 return error.LinkFailed;
9098 }
91 return error.LinkFailed;
9299 }
93100}
94101
......@@ -672,7 +679,13 @@ const DarwinPlatform = struct {
672679 };
673680
674681 var had_extra: bool = undefined;
675 try darwinGetReleaseVersion(ver_str, &result.major, &result.minor, &result.micro, &had_extra,);
682 try darwinGetReleaseVersion(
683 ver_str,
684 &result.major,
685 &result.minor,
686 &result.micro,
687 &had_extra,
688 );
676689 if (had_extra or result.major != 10 or result.minor >= 100 or result.micro >= 100) {
677690 return error.InvalidDarwinVersionString;
678691 }
......@@ -713,7 +726,7 @@ fn darwinGetReleaseVersion(str: []const u8, major: *u32, minor: *u32, micro: *u3
713726 return error.InvalidDarwinVersionString;
714727
715728 var start_pos: usize = 0;
716 for ([]*u32{major, minor, micro}) |v| {
729 for ([]*u32{ major, minor, micro }) |v| {
717730 const dot_pos = mem.indexOfScalarPos(u8, str, start_pos, '.');
718731 const end_pos = dot_pos orelse str.len;
719732 v.* = std.fmt.parseUnsigned(u32, str[start_pos..end_pos], 10) catch return error.InvalidDarwinVersionString;
std/build.zig+21
......@@ -807,6 +807,7 @@ pub const LibExeObjStep = struct {
807807 disable_libc: bool,
808808 frameworks: BufSet,
809809 verbose_link: bool,
810 no_rosegment: bool,
810811
811812 // zig only stuff
812813 root_src: ?[]const u8,
......@@ -874,6 +875,7 @@ pub const LibExeObjStep = struct {
874875
875876 fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: *const Version) LibExeObjStep {
876877 var self = LibExeObjStep{
878 .no_rosegment = false,
877879 .strip = false,
878880 .builder = builder,
879881 .verbose_link = false,
......@@ -914,6 +916,7 @@ pub const LibExeObjStep = struct {
914916
915917 fn initC(builder: *Builder, name: []const u8, kind: Kind, version: *const Version, static: bool) LibExeObjStep {
916918 var self = LibExeObjStep{
919 .no_rosegment = false,
917920 .builder = builder,
918921 .name = name,
919922 .kind = kind,
......@@ -953,6 +956,10 @@ pub const LibExeObjStep = struct {
953956 return self;
954957 }
955958
959 pub fn setNoRoSegment(self: *LibExeObjStep, value: bool) void {
960 self.no_rosegment = value;
961 }
962
956963 fn computeOutFileNames(self: *LibExeObjStep) void {
957964 switch (self.kind) {
958965 Kind.Obj => {
......@@ -1306,6 +1313,10 @@ pub const LibExeObjStep = struct {
13061313 }
13071314 }
13081315
1316 if (self.no_rosegment) {
1317 try zig_args.append("--no-rosegment");
1318 }
1319
13091320 try builder.spawnChild(zig_args.toSliceConst());
13101321
13111322 if (self.kind == Kind.Lib and !self.static and self.target.wantSharedLibSymLinks()) {
......@@ -1598,6 +1609,7 @@ pub const TestStep = struct {
15981609 include_dirs: ArrayList([]const u8),
15991610 lib_paths: ArrayList([]const u8),
16001611 object_files: ArrayList([]const u8),
1612 no_rosegment: bool,
16011613
16021614 pub fn init(builder: *Builder, root_src: []const u8) TestStep {
16031615 const step_name = builder.fmt("test {}", root_src);
......@@ -1615,9 +1627,14 @@ pub const TestStep = struct {
16151627 .include_dirs = ArrayList([]const u8).init(builder.allocator),
16161628 .lib_paths = ArrayList([]const u8).init(builder.allocator),
16171629 .object_files = ArrayList([]const u8).init(builder.allocator),
1630 .no_rosegment = false,
16181631 };
16191632 }
16201633
1634 pub fn setNoRoSegment(self: *TestStep, value: bool) void {
1635 self.no_rosegment = value;
1636 }
1637
16211638 pub fn addLibPath(self: *TestStep, path: []const u8) void {
16221639 self.lib_paths.append(path) catch unreachable;
16231640 }
......@@ -1761,6 +1778,10 @@ pub const TestStep = struct {
17611778 try zig_args.append(lib_path);
17621779 }
17631780
1781 if (self.no_rosegment) {
1782 try zig_args.append("--no-rosegment");
1783 }
1784
17641785 try builder.spawnChild(zig_args.toSliceConst());
17651786 }
17661787};
std/event/channel.zig+3-22
......@@ -71,11 +71,6 @@ pub fn Channel(comptime T: type) type {
7171 /// puts a data item in the channel. The promise completes when the value has been added to the
7272 /// buffer, or in the case of a zero size buffer, when the item has been retrieved by a getter.
7373 pub async fn put(self: *SelfChannel, data: T) void {
74 // TODO should be able to group memory allocation failure before first suspend point
75 // so that the async invocation catches it
76 var dispatch_tick_node_ptr: *Loop.NextTickNode = undefined;
77 _ = async self.dispatch(&dispatch_tick_node_ptr) catch unreachable;
78
7974 suspend |handle| {
8075 var my_tick_node = Loop.NextTickNode{
8176 .next = undefined,
......@@ -91,18 +86,13 @@ pub fn Channel(comptime T: type) type {
9186 self.putters.put(&queue_node);
9287 _ = @atomicRmw(usize, &self.put_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
9388
94 self.loop.onNextTick(dispatch_tick_node_ptr);
89 self.dispatch();
9590 }
9691 }
9792
9893 /// await this function to get an item from the channel. If the buffer is empty, the promise will
9994 /// complete when the next item is put in the channel.
10095 pub async fn get(self: *SelfChannel) T {
101 // TODO should be able to group memory allocation failure before first suspend point
102 // so that the async invocation catches it
103 var dispatch_tick_node_ptr: *Loop.NextTickNode = undefined;
104 _ = async self.dispatch(&dispatch_tick_node_ptr) catch unreachable;
105
10696 // TODO integrate this function with named return values
10797 // so we can get rid of this extra result copy
10898 var result: T = undefined;
......@@ -121,21 +111,12 @@ pub fn Channel(comptime T: type) type {
121111 self.getters.put(&queue_node);
122112 _ = @atomicRmw(usize, &self.get_count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst);
123113
124 self.loop.onNextTick(dispatch_tick_node_ptr);
114 self.dispatch();
125115 }
126116 return result;
127117 }
128118
129 async fn dispatch(self: *SelfChannel, tick_node_ptr: **Loop.NextTickNode) void {
130 // resumed by onNextTick
131 suspend |handle| {
132 var tick_node = Loop.NextTickNode{
133 .data = handle,
134 .next = undefined,
135 };
136 tick_node_ptr.* = &tick_node;
137 }
138
119 fn dispatch(self: *SelfChannel) void {
139120 // set the "need dispatch" flag
140121 _ = @atomicRmw(u8, &self.need_dispatch, AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst);
141122
test/stage2/compare_output.zig+13
......@@ -2,6 +2,7 @@ const std = @import("std");
22const TestContext = @import("../../src-self-hosted/test.zig").TestContext;
33
44pub fn addCases(ctx: *TestContext) !void {
5 // hello world
56 try ctx.testCompareOutputLibC(
67 \\extern fn puts([*]const u8) void;
78 \\export fn main() c_int {
......@@ -9,4 +10,16 @@ pub fn addCases(ctx: *TestContext) !void {
910 \\ return 0;
1011 \\}
1112 , "Hello, world!" ++ std.cstr.line_sep);
13
14 // function calling another function
15 try ctx.testCompareOutputLibC(
16 \\extern fn puts(s: [*]const u8) void;
17 \\export fn main() c_int {
18 \\ return foo(c"OK");
19 \\}
20 \\fn foo(s: [*]const u8) c_int {
21 \\ puts(s);
22 \\ return 0;
23 \\}
24 , "OK" ++ std.cstr.line_sep);
1225}