Skip to content

Add runtime page_size #17382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
6 changes: 3 additions & 3 deletions lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ const PosixThreadImpl = struct {
// Use the same set of parameters used by the libc-less impl.
const stack_size = @max(config.stack_size, 16 * 1024);
assert(c.pthread_attr_setstacksize(&attr, stack_size) == .SUCCESS);
assert(c.pthread_attr_setguardsize(&attr, std.mem.page_size) == .SUCCESS);
assert(c.pthread_attr_setguardsize(&attr, std.heap.pageSize()) == .SUCCESS);

var handle: c.pthread_t = undefined;
switch (c.pthread_create(
Expand Down Expand Up @@ -1066,7 +1066,7 @@ const LinuxThreadImpl = struct {
completion: Completion = Completion.init(.running),
child_tid: std.atomic.Value(i32) = std.atomic.Value(i32).init(1),
parent_tid: i32 = undefined,
mapped: []align(std.mem.page_size) u8,
mapped: []u8,

/// Calls `munmap(mapped.ptr, mapped.len)` then `exit(1)` without touching the stack (which lives in `mapped.ptr`).
/// Ported over from musl libc's pthread detached implementation:
Expand Down Expand Up @@ -1210,7 +1210,7 @@ const LinuxThreadImpl = struct {
};

fn spawn(config: SpawnConfig, comptime f: anytype, args: anytype) !Impl {
const page_size = std.mem.page_size;
const page_size = std.heap.pageSize();
const Args = @TypeOf(args);
const Instance = struct {
fn_args: Args,
Expand Down
13 changes: 7 additions & 6 deletions lib/std/c.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const std = @import("std");
const builtin = @import("builtin");
const c = @This();
const page_size = std.mem.page_size;
const iovec = std.posix.iovec;
const iovec_const = std.posix.iovec_const;
const wasi = @import("c/wasi.zig");
Expand Down Expand Up @@ -1507,6 +1506,8 @@ pub extern "c" fn sigwait(set: ?*c.sigset_t, sig: ?*c_int) c_int;

pub extern "c" fn alarm(seconds: c_uint) c_uint;

pub extern "c" fn sysconf(sc: c_int) c_long;

pub const clock_getres = switch (native_os) {
.netbsd => private.__clock_getres50,
else => private.clock_getres,
Expand Down Expand Up @@ -1632,9 +1633,9 @@ pub extern "c" fn writev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint) i
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: c.off_t) isize;
pub extern "c" fn write(fd: c.fd_t, buf: [*]const u8, nbyte: usize) isize;
pub extern "c" fn pwrite(fd: c.fd_t, buf: [*]const u8, nbyte: usize, offset: c.off_t) isize;
pub extern "c" fn mmap(addr: ?*align(page_size) anyopaque, len: usize, prot: c_uint, flags: MAP, fd: c.fd_t, offset: c.off_t) *anyopaque;
pub extern "c" fn munmap(addr: *align(page_size) const anyopaque, len: usize) c_int;
pub extern "c" fn mprotect(addr: *align(page_size) anyopaque, len: usize, prot: c_uint) c_int;
pub extern "c" fn mmap(addr: ?*anyopaque, len: usize, prot: c_uint, flags: MAP, fd: c.fd_t, offset: c.off_t) *anyopaque;
pub extern "c" fn munmap(addr: *const anyopaque, len: usize) c_int;
pub extern "c" fn mprotect(addr: *anyopaque, len: usize, prot: c_uint) c_int;
pub extern "c" fn link(oldpath: [*:0]const u8, newpath: [*:0]const u8, flags: c_int) c_int;
pub extern "c" fn linkat(oldfd: c.fd_t, oldpath: [*:0]const u8, newfd: c.fd_t, newpath: [*:0]const u8, flags: c_int) c_int;
pub extern "c" fn unlink(path: [*:0]const u8) c_int;
Expand Down Expand Up @@ -1922,7 +1923,7 @@ const private = struct {
extern "c" fn getdirentries(fd: c.fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
extern "c" fn gettimeofday(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;
extern "c" fn msync(addr: *const anyopaque, len: usize, flags: c_int) c_int;
extern "c" fn nanosleep(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
extern "c" fn readdir(dir: *c.DIR) ?*c.dirent;
extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;
Expand Down Expand Up @@ -1952,7 +1953,7 @@ const private = struct {
extern "c" fn __getrusage50(who: c_int, usage: *c.rusage) c_int;
extern "c" fn __gettimeofday50(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
extern "c" fn __libc_thr_yield() c_int;
extern "c" fn __msync13(addr: *align(std.mem.page_size) const anyopaque, len: usize, flags: c_int) c_int;
extern "c" fn __msync13(addr: *const anyopaque, len: usize, flags: c_int) c_int;
extern "c" fn __nanosleep50(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
extern "c" fn __sigaction14(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
extern "c" fn __sigfillset14(set: ?*c.sigset_t) void;
Expand Down
4 changes: 3 additions & 1 deletion lib/std/c/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3184,7 +3184,7 @@ pub const MachTask = extern struct {
return left;
}

fn getPageSize(task: MachTask) MachError!usize {
pub fn getPageSize(task: MachTask) MachError!usize {
if (task.isValid()) {
var info_count = TASK_VM_INFO_COUNT;
var vm_info: task_vm_info_data_t = undefined;
Expand Down Expand Up @@ -3337,3 +3337,5 @@ pub extern "c" fn os_signpost_interval_begin(log: os_log_t, signpos: os_signpost
pub extern "c" fn os_signpost_interval_end(log: os_log_t, signpos: os_signpost_id_t, func: [*]const u8, ...) void;
pub extern "c" fn os_signpost_id_make_with_pointer(log: os_log_t, ptr: ?*anyopaque) os_signpost_id_t;
pub extern "c" fn os_signpost_enabled(log: os_log_t) bool;

pub extern "c" fn getpagesize() c_int;
6 changes: 3 additions & 3 deletions lib/std/c/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub extern "c" fn fstatat64(dirfd: fd_t, noalias path: [*:0]const u8, noalias st
pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int;
pub extern "c" fn getrlimit64(resource: rlimit_resource, rlim: *rlimit) c_int;
pub extern "c" fn lseek64(fd: fd_t, offset: i64, whence: c_int) i64;
pub extern "c" fn mmap64(addr: ?*align(std.mem.page_size) anyopaque, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: i64) *anyopaque;
pub extern "c" fn mmap64(addr: ?*anyopaque, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: i64) *anyopaque;
pub extern "c" fn open64(path: [*:0]const u8, oflag: linux.O, ...) c_int;
pub extern "c" fn openat64(fd: c_int, path: [*:0]const u8, oflag: linux.O, ...) c_int;
pub extern "c" fn pread64(fd: fd_t, buf: [*]u8, nbyte: usize, offset: i64) isize;
Expand Down Expand Up @@ -295,13 +295,13 @@ pub extern "c" fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: u
pub extern "c" fn malloc_usable_size(?*const anyopaque) usize;

pub extern "c" fn mincore(
addr: *align(std.mem.page_size) anyopaque,
addr: *anyopaque,
length: usize,
vec: [*]u8,
) c_int;

pub extern "c" fn madvise(
addr: *align(std.mem.page_size) anyopaque,
addr: *anyopaque,
length: usize,
advice: c_uint,
) c_int;
Expand Down
10 changes: 5 additions & 5 deletions lib/std/crypto/tlcsprng.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var install_atfork_handler = std.once(struct {
}
}.do);

threadlocal var wipe_mem: []align(mem.page_size) u8 = &[_]u8{};
threadlocal var wipe_mem: []u8 = &[_]u8{};

fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void {
if (builtin.link_libc and @hasDecl(std.c, "arc4random_buf")) {
Expand Down Expand Up @@ -96,15 +96,15 @@ fn tlsCsprngFill(_: *anyopaque, buffer: []u8) void {
} else {
// Use a static thread-local buffer.
const S = struct {
threadlocal var buf: Context align(mem.page_size) = .{
threadlocal var buf: Context = .{
.init_state = .uninitialized,
.rng = undefined,
};
};
wipe_mem = mem.asBytes(&S.buf);
}
}
const ctx = @as(*Context, @ptrCast(wipe_mem.ptr));
const ctx = @as(*Context, @ptrCast(@alignCast(wipe_mem.ptr)));

switch (ctx.init_state) {
.uninitialized => {
Expand Down Expand Up @@ -160,7 +160,7 @@ fn childAtForkHandler() callconv(.C) void {
}

fn fillWithCsprng(buffer: []u8) void {
const ctx = @as(*Context, @ptrCast(wipe_mem.ptr));
const ctx = @as(*Context, @ptrCast(@alignCast(wipe_mem.ptr)));
return ctx.rng.fill(buffer);
}

Expand All @@ -176,7 +176,7 @@ fn initAndFill(buffer: []u8) void {
// the `std.options.cryptoRandomSeed` function is provided.
std.options.cryptoRandomSeed(&seed);

const ctx = @as(*Context, @ptrCast(wipe_mem.ptr));
const ctx = @as(*Context, @ptrCast(@alignCast(wipe_mem.ptr)));
ctx.rng = Rng.init(seed);
std.crypto.utils.secureZero(u8, &seed);

Expand Down
48 changes: 32 additions & 16 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,9 @@ pub const StackIterator = struct {
// We are unable to determine validity of memory for freestanding targets
if (native_os == .freestanding) return true;

const aligned_address = address & ~@as(usize, @intCast((mem.page_size - 1)));
const aligned_address = address & ~@as(usize, @intCast((std.heap.pageSize() - 1)));
if (aligned_address == 0) return false;
const aligned_memory = @as([*]align(mem.page_size) u8, @ptrFromInt(aligned_address))[0..mem.page_size];
const aligned_memory = @as([*]u8, @ptrFromInt(aligned_address))[0..std.heap.pageSize()];

if (native_os == .windows) {
var memory_info: windows.MEMORY_BASIC_INFORMATION = undefined;
Expand Down Expand Up @@ -1139,7 +1139,7 @@ pub fn readElfDebugInfo(
build_id: ?[]const u8,
expected_crc: ?u32,
parent_sections: *DW.DwarfInfo.SectionArray,
parent_mapped_mem: ?[]align(mem.page_size) const u8,
parent_mapped_mem: ?[]const u8,
) !ModuleDebugInfo {
nosuspend {
const elf_file = (if (elf_filename) |filename| blk: {
Expand All @@ -1152,7 +1152,7 @@ pub fn readElfDebugInfo(
const mapped_mem = try mapWholeFile(elf_file);
if (expected_crc) |crc| if (crc != std.hash.crc.Crc32SmallWithPoly(.IEEE).hash(mapped_mem)) return error.InvalidDebugInfo;

const hdr: *const elf.Ehdr = @ptrCast(&mapped_mem[0]);
const hdr: *const elf.Ehdr = @ptrCast(@alignCast(&mapped_mem[0]));
if (!mem.eql(u8, hdr.e_ident[0..4], elf.MAGIC)) return error.InvalidElfMagic;
if (hdr.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;

Expand Down Expand Up @@ -1457,7 +1457,7 @@ fn printLineFromFileAnyOs(out_stream: anytype, line_info: LineInfo) !void {
defer f.close();
// TODO fstat and make sure that the file has the correct size

var buf: [mem.page_size]u8 = undefined;
var buf: [1024]u8 = undefined;
var amt_read = try f.read(buf[0..]);
const line_start = seek: {
var current_line_start: usize = 0;
Expand Down Expand Up @@ -1557,7 +1557,7 @@ test "printLineFromFileAnyOs" {

const overlap = 10;
var writer = file.writer();
try writer.writeByteNTimes('a', mem.page_size - overlap);
try writer.writeByteNTimes('a', std.heap.pageSize() - overlap);
try writer.writeByte('\n');
try writer.writeByteNTimes('a', overlap);

Expand All @@ -1572,10 +1572,16 @@ test "printLineFromFileAnyOs" {
defer allocator.free(path);

var writer = file.writer();
try writer.writeByteNTimes('a', mem.page_size);
try writer.writeByteNTimes('a', std.heap.pageSize());

const expected = try allocator.alloc(u8, std.heap.pageSize() + 1);
defer allocator.free(expected);

@memset(expected, 'a');
expected[expected.len - 1] = '\n';

try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
try expectEqualStrings(("a" ** mem.page_size) ++ "\n", output.items);
try expectEqualStrings(expected, output.items);
output.clearRetainingCapacity();
}
{
Expand All @@ -1585,18 +1591,28 @@ test "printLineFromFileAnyOs" {
defer allocator.free(path);

var writer = file.writer();
try writer.writeByteNTimes('a', 3 * mem.page_size);
try writer.writeByteNTimes('a', 3 * std.heap.pageSize());

try expectError(error.EndOfFile, printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 }));

var expected = try allocator.alloc(u8, (3 * std.heap.pageSize()) + 1);
defer allocator.free(expected);

@memset(expected, 'a');
expected[expected.len - 1] = '\n';

try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "\n", output.items);
try expectEqualStrings(expected, output.items);
output.clearRetainingCapacity();

try writer.writeAll("a\na");

expected = try allocator.realloc(expected, (3 * std.heap.pageSize()) + 2);
@memset(expected, 'a');
expected[expected.len - 1] = '\n';

try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 1, .column = 0 });
try expectEqualStrings(("a" ** (3 * mem.page_size)) ++ "a\n", output.items);
try expectEqualStrings(expected, output.items);
output.clearRetainingCapacity();

try printLineFromFileAnyOs(output_stream, .{ .file_name = path, .line = 2, .column = 0 });
Expand All @@ -1610,7 +1626,7 @@ test "printLineFromFileAnyOs" {
defer allocator.free(path);

var writer = file.writer();
const real_file_start = 3 * mem.page_size;
const real_file_start = 3 * std.heap.pageSize();
try writer.writeByteNTimes('\n', real_file_start);
try writer.writeAll("abc\ndef");

Expand Down Expand Up @@ -1643,7 +1659,7 @@ const MachoSymbol = struct {

/// Takes ownership of file, even on error.
/// TODO it's weird to take ownership even on error, rework this code.
fn mapWholeFile(file: File) ![]align(mem.page_size) const u8 {
fn mapWholeFile(file: File) ![]const u8 {
nosuspend {
defer file.close();

Expand Down Expand Up @@ -2132,7 +2148,7 @@ pub const ModuleDebugInfo = switch (native_os) {
.macos, .ios, .watchos, .tvos => struct {
base_address: usize,
vmaddr_slide: usize,
mapped_memory: []align(mem.page_size) const u8,
mapped_memory: []const u8,
symbols: []const MachoSymbol,
strings: [:0]const u8,
ofiles: OFileTable,
Expand Down Expand Up @@ -2425,8 +2441,8 @@ pub const ModuleDebugInfo = switch (native_os) {
.linux, .netbsd, .freebsd, .dragonfly, .openbsd, .haiku, .solaris, .illumos => struct {
base_address: usize,
dwarf: DW.DwarfInfo,
mapped_memory: []align(mem.page_size) const u8,
external_mapped_memory: ?[]align(mem.page_size) const u8,
mapped_memory: []const u8,
external_mapped_memory: ?[]const u8,

pub fn deinit(self: *@This(), allocator: mem.Allocator) void {
self.dwarf.deinit(allocator);
Expand Down
12 changes: 6 additions & 6 deletions lib/std/dynamic_library.zig
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub const ElfDynLib = struct {
hashtab: [*]posix.Elf_Symndx,
versym: ?[*]u16,
verdef: ?*elf.Verdef,
memory: []align(mem.page_size) u8,
memory: []u8,

pub const Error = error{
FileTooBig,
Expand All @@ -152,15 +152,15 @@ pub const ElfDynLib = struct {
// corresponding to the actual LOAD sections.
const file_bytes = try posix.mmap(
null,
mem.alignForward(usize, size, mem.page_size),
mem.alignForward(usize, size, std.heap.pageSize()),
posix.PROT.READ,
.{ .TYPE = .PRIVATE },
fd,
0,
);
defer posix.munmap(file_bytes);

const eh = @as(*elf.Ehdr, @ptrCast(file_bytes.ptr));
const eh = @as(*elf.Ehdr, @ptrCast(@alignCast(file_bytes.ptr)));
if (!mem.eql(u8, eh.e_ident[0..4], elf.MAGIC)) return error.NotElfFile;
if (eh.e_type != elf.ET.DYN) return error.NotDynamicLibrary;

Expand Down Expand Up @@ -213,10 +213,10 @@ pub const ElfDynLib = struct {
elf.PT_LOAD => {
// The VirtAddr may not be page-aligned; in such case there will be
// extra nonsense mapped before/after the VirtAddr,MemSiz
const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, mem.page_size) - 1);
const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, std.heap.pageSize()) - 1);
const extra_bytes = (base + ph.p_vaddr) - aligned_addr;
const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, mem.page_size);
const ptr = @as([*]align(mem.page_size) u8, @ptrFromInt(aligned_addr));
const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, std.heap.pageSize());
const ptr = @as([*]u8, @ptrFromInt(aligned_addr));
const prot = elfToMmapProt(ph.p_flags);
if ((ph.p_flags & elf.PF_W) == 0) {
// If it does not need write access, it can be mapped from the fd.
Expand Down
2 changes: 1 addition & 1 deletion lib/std/fifo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn LinearFifo(
mem.copyForwards(T, self.buf[0..self.count], self.buf[self.head..][0..self.count]);
self.head = 0;
} else {
var tmp: [mem.page_size / 2 / @sizeOf(T)]T = undefined;
var tmp: [4096 / 2 / @sizeOf(T)]T = undefined;

while (self.head != 0) {
const n = @min(self.head, tmp.len);
Expand Down
Loading