Skip to content

Commit 4c6971d

Browse files
committed
std.Io.Threaded: add ioBasic which disables networking
1 parent 9264c03 commit 4c6971d

File tree

11 files changed

+375
-118
lines changed

11 files changed

+375
-118
lines changed

lib/std/Io/Threaded.zig

Lines changed: 333 additions & 89 deletions
Large diffs are not rendered by default.

lib/std/Thread.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
320320
const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});
321321

322322
var threaded: std.Io.Threaded = .init_single_threaded;
323-
const io = threaded.io();
323+
const io = threaded.ioBasic();
324324

325325
const file = try std.fs.cwd().openFile(path, .{});
326326
defer file.close();

lib/std/debug.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ pub noinline fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf:
649649
/// See `captureCurrentStackTrace` to capture the trace addresses into a buffer instead of printing.
650650
pub noinline fn writeCurrentStackTrace(options: StackUnwindOptions, writer: *Writer, tty_config: tty.Config) Writer.Error!void {
651651
var threaded: Io.Threaded = .init_single_threaded;
652-
const io = threaded.io();
652+
const io = threaded.ioBasic();
653653

654654
if (!std.options.allow_stack_tracing) {
655655
tty_config.setColor(writer, .dim) catch {};
@@ -780,7 +780,7 @@ pub fn writeStackTrace(st: *const StackTrace, writer: *Writer, tty_config: tty.C
780780
// We use an independent Io implementation here in case there was a problem
781781
// with the application's Io implementation itself.
782782
var threaded: Io.Threaded = .init_single_threaded;
783-
const io = threaded.io();
783+
const io = threaded.ioBasic();
784784

785785
// Fetch `st.index` straight away. Aside from avoiding redundant loads, this prevents issues if
786786
// `st` is `@errorReturnTrace()` and errors are encountered while writing the stack trace.
@@ -1585,7 +1585,7 @@ test "manage resources correctly" {
15851585
};
15861586
const gpa = std.testing.allocator;
15871587
var threaded: Io.Threaded = .init_single_threaded;
1588-
const io = threaded.io();
1588+
const io = threaded.ioBasic();
15891589
var discarding: Io.Writer.Discarding = .init(&.{});
15901590
var di: SelfInfo = .init;
15911591
defer di.deinit(gpa);

lib/std/fs.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ pub const OpenSelfExeError = Io.File.OpenSelfExeError;
340340
pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
341341
if (native_os == .linux or native_os == .serenity or native_os == .windows) {
342342
var threaded: Io.Threaded = .init_single_threaded;
343-
const io = threaded.io();
343+
const io = threaded.ioBasic();
344344
return .adaptFromNewApi(try Io.File.openSelfExe(io, flags));
345345
}
346346
// Use of max_path_bytes here is valid as the resulting path is immediately

lib/std/fs/Dir.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -850,14 +850,14 @@ pub fn close(self: *Dir) void {
850850
/// Deprecated in favor of `Io.Dir.openFile`.
851851
pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
852852
var threaded: Io.Threaded = .init_single_threaded;
853-
const io = threaded.io();
853+
const io = threaded.ioBasic();
854854
return .adaptFromNewApi(try Io.Dir.openFile(self.adaptToNewApi(), io, sub_path, flags));
855855
}
856856

857857
/// Deprecated in favor of `Io.Dir.createFile`.
858858
pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
859859
var threaded: Io.Threaded = .init_single_threaded;
860-
const io = threaded.io();
860+
const io = threaded.ioBasic();
861861
const new_file = try Io.Dir.createFile(self.adaptToNewApi(), io, sub_path, flags);
862862
return .adaptFromNewApi(new_file);
863863
}
@@ -868,7 +868,7 @@ pub const MakeError = Io.Dir.MakeError;
868868
/// Deprecated in favor of `Io.Dir.makeDir`.
869869
pub fn makeDir(self: Dir, sub_path: []const u8) MakeError!void {
870870
var threaded: Io.Threaded = .init_single_threaded;
871-
const io = threaded.io();
871+
const io = threaded.ioBasic();
872872
return Io.Dir.makeDir(.{ .handle = self.fd }, io, sub_path);
873873
}
874874

@@ -895,14 +895,14 @@ pub const MakePathError = Io.Dir.MakePathError;
895895
/// Deprecated in favor of `Io.Dir.makePathStatus`.
896896
pub fn makePathStatus(self: Dir, sub_path: []const u8) MakePathError!MakePathStatus {
897897
var threaded: Io.Threaded = .init_single_threaded;
898-
const io = threaded.io();
898+
const io = threaded.ioBasic();
899899
return Io.Dir.makePathStatus(.{ .handle = self.fd }, io, sub_path);
900900
}
901901

902902
/// Deprecated in favor of `Io.Dir.makeOpenPath`.
903903
pub fn makeOpenPath(dir: Dir, sub_path: []const u8, options: OpenOptions) Io.Dir.MakeOpenPathError!Dir {
904904
var threaded: Io.Threaded = .init_single_threaded;
905-
const io = threaded.io();
905+
const io = threaded.ioBasic();
906906
return .adaptFromNewApi(try Io.Dir.makeOpenPath(dir.adaptToNewApi(), io, sub_path, options));
907907
}
908908

@@ -1071,7 +1071,7 @@ pub const OpenOptions = Io.Dir.OpenOptions;
10711071
/// Deprecated in favor of `Io.Dir.openDir`.
10721072
pub fn openDir(self: Dir, sub_path: []const u8, args: OpenOptions) OpenError!Dir {
10731073
var threaded: Io.Threaded = .init_single_threaded;
1074-
const io = threaded.io();
1074+
const io = threaded.ioBasic();
10751075
return .adaptFromNewApi(try Io.Dir.openDir(.{ .handle = self.fd }, io, sub_path, args));
10761076
}
10771077

@@ -1385,7 +1385,7 @@ pub fn readLinkW(self: Dir, sub_path_w: []const u16, buffer: []u8) ![]u8 {
13851385
/// Deprecated in favor of `Io.Dir.readFile`.
13861386
pub fn readFile(self: Dir, file_path: []const u8, buffer: []u8) ![]u8 {
13871387
var threaded: Io.Threaded = .init_single_threaded;
1388-
const io = threaded.io();
1388+
const io = threaded.ioBasic();
13891389
return Io.Dir.readFile(.{ .handle = self.fd }, io, file_path, buffer);
13901390
}
13911391

@@ -1438,7 +1438,7 @@ pub fn readFileAllocOptions(
14381438
comptime sentinel: ?u8,
14391439
) ReadFileAllocError!(if (sentinel) |s| [:s]align(alignment.toByteUnits()) u8 else []align(alignment.toByteUnits()) u8) {
14401440
var threaded: Io.Threaded = .init_single_threaded;
1441-
const io = threaded.io();
1441+
const io = threaded.ioBasic();
14421442

14431443
var file = try dir.openFile(sub_path, .{});
14441444
defer file.close();
@@ -1893,7 +1893,7 @@ pub const AccessError = Io.Dir.AccessError;
18931893
/// Deprecated in favor of `Io.Dir.access`.
18941894
pub fn access(self: Dir, sub_path: []const u8, options: Io.Dir.AccessOptions) AccessError!void {
18951895
var threaded: Io.Threaded = .init_single_threaded;
1896-
const io = threaded.io();
1896+
const io = threaded.ioBasic();
18971897
return Io.Dir.access(self.adaptToNewApi(), io, sub_path, options);
18981898
}
18991899

@@ -1929,7 +1929,7 @@ pub fn copyFile(
19291929
options: CopyFileOptions,
19301930
) CopyFileError!void {
19311931
var threaded: Io.Threaded = .init_single_threaded;
1932-
const io = threaded.io();
1932+
const io = threaded.ioBasic();
19331933

19341934
const file = try source_dir.openFile(source_path, .{});
19351935
var file_reader: File.Reader = .init(.{ .handle = file.handle }, io, &.{});
@@ -1997,7 +1997,7 @@ pub const StatFileError = File.OpenError || File.StatError || posix.FStatAtError
19971997
/// Deprecated in favor of `Io.Dir.statPath`.
19981998
pub fn statFile(self: Dir, sub_path: []const u8) StatFileError!Stat {
19991999
var threaded: Io.Threaded = .init_single_threaded;
2000-
const io = threaded.io();
2000+
const io = threaded.ioBasic();
20012001
return Io.Dir.statPath(.{ .handle = self.fd }, io, sub_path, .{});
20022002
}
20032003

lib/std/fs/File.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub const StatError = posix.FStatError;
313313
/// Returns `Stat` containing basic information about the `File`.
314314
pub fn stat(self: File) StatError!Stat {
315315
var threaded: Io.Threaded = .init_single_threaded;
316-
const io = threaded.io();
316+
const io = threaded.ioBasic();
317317
return Io.File.stat(.{ .handle = self.handle }, io);
318318
}
319319

lib/std/process/Child.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ fn windowsCreateProcessPathExt(
10891089
// opening function knowing which implementation we are in. Here, we imitate
10901090
// that scenario.
10911091
var threaded: std.Io.Threaded = .init_single_threaded;
1092-
const io = threaded.io();
1092+
const io = threaded.ioBasic();
10931093

10941094
var dir = dir: {
10951095
// needs to be null-terminated

test/standalone/child_process/child.zig

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
const std = @import("std");
2+
const Io = std.Io;
23

34
// 42 is expected by parent; other values result in test failure
45
var exit_code: u8 = 42;
56

67
pub fn main() !void {
78
var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
89
const arena = arena_state.allocator();
9-
try run(arena);
10+
11+
var threaded: std.Io.Threaded = .init(arena);
12+
defer threaded.deinit();
13+
const io = threaded.io();
14+
15+
try run(arena, io);
1016
arena_state.deinit();
1117
std.process.exit(exit_code);
1218
}
1319

14-
fn run(allocator: std.mem.Allocator) !void {
20+
fn run(allocator: std.mem.Allocator, io: Io) !void {
1521
var args = try std.process.argsWithAllocator(allocator);
1622
defer args.deinit();
1723
_ = args.next() orelse unreachable; // skip binary name
@@ -33,7 +39,8 @@ fn run(allocator: std.mem.Allocator) !void {
3339
const hello_stdin = "hello from stdin";
3440
var buf: [hello_stdin.len]u8 = undefined;
3541
const stdin: std.fs.File = .stdin();
36-
const n = try stdin.readAll(&buf);
42+
var reader = stdin.reader(io, &.{});
43+
const n = try reader.interface.readSliceShort(&buf);
3744
if (!std.mem.eql(u8, buf[0..n], hello_stdin)) {
3845
testError("stdin: '{s}'; want '{s}'", .{ buf[0..n], hello_stdin });
3946
}

test/standalone/simple/cat/main.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ pub fn main() !void {
99
defer arena_instance.deinit();
1010
const arena = arena_instance.allocator();
1111

12+
var threaded: std.Io.Threaded = .init(arena);
13+
defer threaded.deinit();
14+
const io = threaded.io();
15+
1216
const args = try std.process.argsAlloc(arena);
1317

1418
const exe = args[0];
1519
var catted_anything = false;
1620
var stdout_buffer: [4096]u8 = undefined;
1721
var stdout_writer = fs.File.stdout().writerStreaming(&stdout_buffer);
1822
const stdout = &stdout_writer.interface;
19-
var stdin_reader = fs.File.stdin().readerStreaming(&.{});
23+
var stdin_reader = fs.File.stdin().readerStreaming(io, &.{});
2024

2125
const cwd = fs.cwd();
2226

@@ -32,7 +36,7 @@ pub fn main() !void {
3236
defer file.close();
3337

3438
catted_anything = true;
35-
var file_reader = file.reader(&.{});
39+
var file_reader = file.reader(io, &.{});
3640
_ = try stdout.sendFileAll(&file_reader, .unlimited);
3741
try stdout.flush();
3842
}

tools/fetch_them_macos_headers.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const Io = std.Io;
23
const fs = std.fs;
34
const mem = std.mem;
45
const process = std.process;
@@ -85,7 +86,7 @@ pub fn main() anyerror!void {
8586
} else try argv.append(arg);
8687
}
8788

88-
var threaded: std.Io.Threaded = .init(gpa);
89+
var threaded: Io.Threaded = .init(gpa);
8990
defer threaded.deinit();
9091
const io = threaded.io();
9192

@@ -118,12 +119,13 @@ pub fn main() anyerror!void {
118119
.arch = arch,
119120
.os_ver = os_ver,
120121
};
121-
try fetchTarget(allocator, argv.items, sysroot_path, target, version, tmp);
122+
try fetchTarget(allocator, io, argv.items, sysroot_path, target, version, tmp);
122123
}
123124
}
124125

125126
fn fetchTarget(
126127
arena: Allocator,
128+
io: Io,
127129
args: []const []const u8,
128130
sysroot: []const u8,
129131
target: Target,
@@ -194,7 +196,7 @@ fn fetchTarget(
194196
var dirs = std.StringHashMap(fs.Dir).init(arena);
195197
try dirs.putNoClobber(".", dest_dir);
196198

197-
var headers_list_file_reader = headers_list_file.reader(&.{});
199+
var headers_list_file_reader = headers_list_file.reader(io, &.{});
198200
const headers_list_str = try headers_list_file_reader.interface.allocRemaining(arena, .unlimited);
199201
const prefix = "/usr/include";
200202

@@ -267,8 +269,8 @@ const Version = struct {
267269

268270
pub fn format(
269271
v: Version,
270-
writer: *std.Io.Writer,
271-
) std.Io.Writer.Error!void {
272+
writer: *Io.Writer,
273+
) Io.Writer.Error!void {
272274
try writer.print("{d}.{d}.{d}", .{ v.major, v.minor, v.patch });
273275
}
274276
};

0 commit comments

Comments
 (0)