203 Commits

Author SHA1 Message Date
mlugg
eae9aa800e
std: avoid references that trigger compile errors
Note that the `_ = Address` statements in tests previously were a nop,
and now actually check that the type is valid. However, on WASI, the
type is *not* valid.
2024-07-04 21:01:42 +01:00
Michael Bradshaw
02b3d5b58a Rename isASCII to isAscii 2024-07-02 16:31:15 +02:00
Nameless
aecd9cc6d1 std.posix.iovec: use .base and .len instead of .iov_base and .iov_len 2024-04-28 00:20:30 -07:00
mlugg
03ad862197
compiler: un-implement #19634
This commit reverts the handling of partially-undefined values in
bitcasting to transform these bits into an arbitrary numeric value,
like happens on `master` today.

As @andrewrk rightly points out, #19634 has unfortunate consequences
for the standard library, and likely requires more thought. To avoid
a major breaking change, it has been decided to revert this design
decision for now, and make a more informed decision further down the
line.
2024-04-17 13:41:25 +01:00
mlugg
d0e74ffe52
compiler: rework comptime pointer representation and access
We've got a big one here! This commit reworks how we represent pointers
in the InternPool, and rewrites the logic for loading and storing from
them at comptime.

Firstly, the pointer representation. Previously, pointers were
represented in a highly structured manner: pointers to fields, array
elements, etc, were explicitly represented. This works well for simple
cases, but is quite difficult to handle in the cases of unusual
reinterpretations, pointer casts, offsets, etc. Therefore, pointers are
now represented in a more "flat" manner. For types without well-defined
layouts -- such as comptime-only types, automatic-layout aggregates, and
so on -- we still use this "hierarchical" structure. However, for types
with well-defined layouts, we use a byte offset associated with the
pointer. This allows the comptime pointer access logic to deal with
reinterpreted pointers far more gracefully, because the "base address"
of a pointer -- for instance a `field` -- is a single value which
pointer accesses cannot exceed since the parent has undefined layout.
This strategy is also more useful to most backends -- see the updated
logic in `codegen.zig` and `codegen/llvm.zig`. For backends which do
prefer a chain of field and elements accesses for lowering pointer
values, such as SPIR-V, there is a helpful function in `Value` which
creates a strategy to derive a pointer value using ideally only field
and element accesses. This is actually more correct than the previous
logic, since it correctly handles pointer casts which, after the dust
has settled, end up referring exactly to an aggregate field or array
element.

In terms of the pointer access code, it has been rewritten from the
ground up. The old logic had become rather a mess of special cases being
added whenever bugs were hit, and was still riddled with bugs. The new
logic was written to handle the "difficult" cases correctly, the most
notable of which is restructuring of a comptime-only array (for
instance, converting a `[3][2]comptime_int` to a `[2][3]comptime_int`.
Currently, the logic for loading and storing work somewhat differently,
but a future change will likely improve the loading logic to bring it
more in line with the store strategy. As far as I can tell, the rewrite
has fixed all bugs exposed by #19414.

As a part of this, the comptime bitcast logic has also been rewritten.
Previously, bitcasts simply worked by serializing the entire value into
an in-memory buffer, then deserializing it. This strategy has two key
weaknesses: pointers, and undefined values. Representations of these
values at comptime cannot be easily serialized/deserialized whilst
preserving data, which means many bitcasts would become runtime-known if
pointers were involved, or would turn `undefined` values into `0xAA`.
The new logic works by "flattening" the datastructure to be cast into a
sequence of bit-packed atomic values, and then "unflattening" it; using
serialization when necessary, but with special handling for `undefined`
values and for pointers which align in virtual memory. The resulting
code is definitely slower -- more on this later -- but it is correct.

The pointer access and bitcast logic required some helper functions and
types which are not generally useful elsewhere, so I opted to split them
into separate files `Sema/comptime_ptr_access.zig` and
`Sema/bitcast.zig`, with simple re-exports in `Sema.zig` for their small
public APIs.

Whilst working on this branch, I caught various unrelated bugs with
transitive Sema errors, and with the handling of `undefined` values.
These bugs have been fixed, and corresponding behavior test added.

In terms of performance, I do anticipate that this commit will regress
performance somewhat, because the new pointer access and bitcast logic
is necessarily more complex. I have not yet taken performance
measurements, but will do shortly, and post the results in this PR. If
the performance regression is severe, I will do work to to optimize the
new logic before merge.

Resolves: #19452
Resolves: #19460
2024-04-17 13:41:25 +01:00
Andrew Kelley
cd62005f19 extract std.posix from std.os
closes #5019
2024-03-19 11:45:09 -07:00
Andrew Kelley
aa852f737b improve documentation in std
A lot of these "shorthand" doc comments were redundant, low quality
filler content. Better to let the actual modules speak for themselves
with top level doc comments rather than trying to document their
aliases.
2024-03-10 18:13:30 -07:00
Andrew Kelley
a8958c99a9 std.net: fix std lib test regression. fixup 2024-02-23 02:37:11 -07:00
Andrew Kelley
12a9e0f415 std.net.listen: fix Windows API use
In a previous commit I removed a load-bearing use of `@hasDecl` to
detect whether the SO.REUSEPORT option should be set. `@hasDecl` should
not be used for OS feature detection because it can hide bugs.

The new logic checks for the operating system specifically and then does
the thing that is supposed to be done on that operating system directly.
2024-02-23 02:37:11 -07:00
Andrew Kelley
6129ecd4fe std.net, std.http: simplify 2024-02-23 02:37:11 -07:00
Andrew Kelley
63acc856c1 std.http.Client: remove invalid use of refAllDecls 2024-02-23 02:37:11 -07:00
Veikka Tuominen
a4f27e8987 remove std.io.Mode 2024-02-01 15:22:36 +02:00
Christiano Haesbaert
bf7ebfa67a Handle all errors on std.net.Ipv4address.resolveIP
The following test fails since NonCanonical is not handled

test "foo" {
    std.net.Ip4Address.resolveIp("1.1.1.1", 0) catch unreachable;
}

/usr/lib/zig/std/net.zig:240:60: error: switch must handle all possibilities
        if (parse(name, port)) |ip4| return ip4 else |err| switch (err) {
                                                           ^~~~~~
/usr/lib/zig/std/net.zig:240:60: note: unhandled error value: 'error.NonCanonical'
referenced by:
    test.foo: src/dhcp.zig:383:23
2024-01-21 11:20:03 +02:00
Artem Kolichenkov
90a19f7411
std.net: add explicit error sets for IP parsing
Inferred errors in switch statements prevented IP address parsing at comptime.
Adding explicit error sets fixes it.

Closes #18276
2023-12-16 16:15:51 +00:00
Carter Snook
a98d4a66e9 std.net: replace @ptrCast with slice syntax 2023-11-27 23:14:07 -05:00
Jan Philipp Hafer
27b34a5b77 std.net: enable forcing non-blocking mode for accept
Justification: It is common for non-CPU bound short routines to do
non-blocking accept to eliminate unnecessary delays before subscribing
to data, for example in hardware integration tests.
2023-11-21 18:04:33 +02:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
Andrew Kelley
3fc6fc6812 std.builtin.Endian: make the tags lower case
Let's take this breaking change opportunity to fix the style of this
enum.
2023-10-31 21:37:35 -04:00
Jacob Young
d890e81761 mem: fix ub in writeInt
Use inline to vastly simplify the exposed API.  This allows a
comptime-known endian parameter to be propogated, making extra functions
for a specific endianness completely unnecessary.
2023-10-31 21:37:35 -04:00
Luis Cáceres
8976ad7ecb std.net: Fix IPv6 address parsing for single digit
This fixes the case where IPv6 address parsing incorrectly succeeded on
input such as `1`, which now returns error.Incomplete.
2023-09-06 11:14:24 +03:00
Jacob Young
f571438fc0 Unrevert "Sema: preserve extern struct field alignment"
This unreverts commit 1a2468abfcd8b539193d943c1eefb71319cc7b88.
2023-06-30 23:24:52 -04:00
Jacob Young
1a2468abfc Revert "Sema: preserve extern struct field alignment"
This reverts commit 4620972d086ebb3b7686a79914876488c6dfd171.
2023-06-29 00:23:19 -04:00
Alex Kladov
4620972d08 Sema: preserve extern struct field alignment
In

    extern struct { x: u32, y: u16 }

we actually know that y's alignment is `@alignOf(u32)`, and not just
`@alignOf(u16)`.

closes: #16134
2023-06-28 16:36:32 +02:00
Eric Joldasov
0a868dacdd std.cstr: deprecate namespace
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-25 14:51:03 -07:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
r00ster91
2593156068 migration: std.math.{min, min3, max, max3} -> @min & @max 2023-06-16 13:44:09 -07:00
Andrew Kelley
629f0d23b5
Merge pull request #15579 from squeek502/mem-delimiters
Split `std.mem.split` and `tokenize` into `sequence`, `any`, and `scalar` versions
2023-06-03 13:51:02 -07:00
Ali Chraghi
3db3cf7790 std.sort: add pdqsort and heapsort 2023-05-23 17:55:59 -07:00
Ryan Liptak
2129f28953 Update all std.mem.split calls to their appropriate function
Everywhere that can now use `splitScalar` should get a nice little performance boost.
2023-05-13 13:45:05 -07:00
Ryan Liptak
815e53b147 Update all std.mem.tokenize calls to their appropriate function
Everywhere that can now use `tokenizeScalar` should get a nice little performance boost.
2023-05-13 13:45:04 -07:00
dweiller
bd3360e03d convert s[start..start+len] to s[start..][0..len] 2023-05-07 15:55:21 +10:00
Andrew Kelley
125221cce9 std: update to use @memcpy directly 2023-04-28 13:24:43 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Andrew Kelley
a5c910adb6 change semantics of @memcpy and @memset
Now they use slices or array pointers with any element type instead of
requiring byte pointers.

This is a breaking enhancement to the language.

The safety check for overlapping pointers will be implemented in a
future commit.

closes #14040
2023-04-25 11:23:40 -07:00
Jon
529064856a
std.net.StreamServer.Options: add reuse_port 2023-04-24 12:23:22 +03:00
hequn
f56f3c5824
Enable IPv4 mapped address conversion in linux version getAddressList (#14916)
It seems like the original code of setsockopt is not effective because
i catch the EINVAL branch when uncomment this code, it should call
setsockopt before the bind call.

This should fix issue #14900.

Co-authored-by: Qun He <hawkbee@qq.com>
2023-03-17 15:58:02 -04:00
Nameless
0a4130f364
std.http: handle relative redirects 2023-03-09 14:55:13 -06:00
Nameless
8d86194b6e
add error sets to tcpConnect* and tls.Client.init 2023-03-09 14:54:26 -06:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Isaac Freund
186e805838
std.net.Address: clarify unix socket getOsSockLen 2023-01-23 14:49:06 +01:00
Isaac Freund
faf2fd18d3
std: eliminate pointless meta.assumeSentinel() usage
This fixes a bug in std.net caused during the introduction of
meta.assumeSentinel due to the unfortunate semantics of mem.span()

This leaves only 3 remaining uses of meta.assumeSentinel() in the
standard library, each of which could be a simple @ptrCast([*:0]T, foo)
instead. I think this function should likely be removed.
2023-01-23 12:19:53 +01:00
Jarred Sumner
a9eb463202 Make res nullable in getaddrinfo 2023-01-19 23:08:15 +02:00
Andrew Kelley
86308ba1e1 std.net.getAddressList: call WSAStartup on Windows 2023-01-17 00:08:42 -07:00
praschke
3ab43988c1 std.net: check for localhost names before asking DNS
the old implementation asks DNS before checking if it shouldn't.
additionally, it did not catch absolute 'localhost.' names.
2023-01-10 18:09:07 -05:00
Andrew Kelley
e4a9b19a14 std.crypto.tls.Client: rework the read function
Here's what I landed on for the TLS client. It's 16896 bytes
(max_ciphertext_record_len is 16640). I believe this is the theoretical
minimum size, give or take a few bytes.

These constraints are satisfied:
 * a call to the readvAdvanced() function makes at most one call to the
   underlying readv function
 * iovecs are provided by the API, and used by the implementation for
   underlying readv() calls to the socket
 * the theoretical minimum number of memcpy() calls are issued in all
   circumstances
 * decryption is only performed once for any given TLS record
 * large read buffers are fully exploited

This is accomplished by using the partial read buffer to storing both
cleartext and ciphertext.
2023-01-02 16:57:16 -07:00
Andrew Kelley
940d368e7e std.crypto.tls.Client: fix the read function
The read function has been renamed to readAdvanced since it has slightly
different semantics than typical read functions, specifically regarding
the end-of-file. A higher level read function is implemented on top.

Now, API users may pass small buffers to the read function and
everything will work fine. This is done by re-decrypting the same
ciphertext record with each call to read() until the record is finished
being transmitted.

If the buffer supplied to read() is large enough, then any given
ciphertext record will only be decrypted once, since it decrypts
directly to the read() buffer and therefore does not need any memcpy. On
the other hand, if the buffer supplied to read() is small, then the
ciphertext is decrypted into a stack buffer, a subset is copied to the
read() buffer, and then the entire ciphertext record is saved for the
next call to read().
2023-01-02 16:57:16 -07:00
Andrew Kelley
920e5bc4ff std.crypto.Tls: discard ChangeCipherSpec messages
The next step here is to decrypt encrypted records
2023-01-02 16:57:15 -07:00
Andrew Kelley
d2f5d0b199 std.crypto.Tls: parse the ServerHello handshake 2023-01-02 16:57:15 -07:00
Andrew Kelley
ba44513c2f std.http reorg; introduce std.crypto.Tls
TLS is capable of sending a Client Hello
2023-01-02 16:57:15 -07:00