5437 Commits

Author SHA1 Message Date
Veikka Tuominen
ae7b32eb62 Sema: validate deref operator type and value 2022-07-01 10:22:25 +03:00
Andrew Kelley
77a334451f
Merge pull request #11967 from ziglang/runtime-float-negation
stage2: lower float negation explicitly + modify hash/eql logic for floats
2022-06-30 17:34:05 -04:00
Mikko Kaihlavirta
be18459c81
std.event: use .{} to initiate Lock to unlocked state
Co-authored-by: Mikko Kaihlavirta <kaihlavirta@gmail.com>
2022-07-01 00:09:36 +03:00
Andrew Kelley
6bc6e47b15 stage2: lower float negation explicitly
Rather than lowering float negation as `0.0 - x`.

 * Add AIR instruction for float negation.
 * Add compiler-rt functions for f128, f80 negation

closes #11853
2022-06-30 00:02:00 -07:00
Andrew Kelley
c248af3bdc LLVM: fix lowering of untagged union types
The LLVM backend was calculating the amount of padding solely based
on the payload size. However, in the case where there is no union
tag, this fails to take into account alignment.

Closes #11857
2022-06-30 02:43:05 -04:00
Andrew Kelley
54454fd010 std.math.big.int: breaking API changes to prevent UAF
Many of the Managed methods accepted by-val parameters which could
reference Limb slices that became invalid memory after any
ensureCapacity calls. Now, Managed methods accept `*const Managed`
parameters so that if the function allows aliasing and the
ensure-capacity call resizes the Limb slice, it also affects the
aliased parameters, avoiding use-after-free bugs.

This is a breaking change that reduces the requirement for callsites to
manually make the ensure-capacity changes prior to calling many of the
Managed methods.

Closes #11897
2022-06-29 22:06:27 -04:00
Andrew Kelley
98681b2da0
Merge pull request #11958 from ziglang/store-to-inferred-ptr
stage2: fix miscompilations for peer expressions any time they needed coercions to runtime types
2022-06-29 14:26:23 -04:00
Frank Denis
234ccb4a50
std.crypto.ecc: add support for the secp256k1 curve (#11880)
std.crypto.ecc: add support for the secp256k1 curve

Usage of the secp256k1 elliptic curve recently grew exponentially,
since this is the curve used by Bitcoin and other popular blockchains
such as Ethereum.

With this, Zig has support for all the widely deployed elliptic curves
today.
2022-06-29 15:11:33 +02:00
May B
ea13437ac5
std.json: Support disabling indent (#11823)
Newline Delimited JSON (ndjson) expect compact json without newline inside its content
Add None to StringfyOptions.indent and move newline writeByte inside StringfyOptions.outputIndent
2022-06-29 11:53:01 +02:00
Motiejus Jakštys
4a6b70fbd1
mem: add splitBackwards (#11908)
* mem: refactor tests of split()

- add a few cases for .rest()
- use expectEqualSlices()

* mem: add splitBackwards

Over the last couple of weeks weeks I needed to iterate over a
collection backwards at least twice. Do we want to have this in stdlib?
If yes, click "Merge" and start using today! Free shipping and returns
(before 1.0).

Why is this useful?
-------------------

I need this for building an error wrapper: errors are added in the
wrapper from "lowest" level to "highest" level, and then printed in
reverse order. Imagine `UpdateUsers` call, which needs to return
`error.InvalidInput` and a wrappable error context. In Go we would add a
context to the error when returning it:

    // if update_user fails, add context on which user we are operating
    if err := update_user(user); err != nil {
        return fmt.Errorf("user id=%d: %w", user.id, err)
    }

Since Zig cannot pass anything else than u16 with an error (#2647), I
will pass a `err_ctx: *Err`, to the callers, where they can, besides
returning an error, augment it with auxiliary data. `Err` is a
preallocated array that can add zero-byte-separated strings. For a
concrete example, imagine such a call graph:

    update_user(User, *Err) error{InvalidInput}!<...>
      validate_user([]const u8, *Err) error{InvalidInput}!<...>

Where `validate_user` would like, besides only the error, signal the
invalid field. And `update_user`, besides the error, would signal the
offending user id.

We also don't want the low-level functions to know in which context they
are operating to construct a meaningful error message: if validation
fails, they append their "context" to the buffer. To translate/augment
the Go example above:

    pub fn validate_user(err_ctx: *Err, user: User) error{InvalidInput}!void {
        const name = user.name;
        if (!ascii.isAlpha(name)) {
            err_ctx.print("name '{s}' must be ascii-letters only", .{name});
            return error.InvalidInput;
        }
        <...>
    }

    // update_user validates each user and does something with it.
    pub fn update_user(err_ctx: *Err, user: User) error{InvalidInput}!void {
        // validate the user before updating it
        validate_user(user) catch {
            err_ctx.print("user id={d}", .{user.id});
            return error.InvalidInput;
        };
        <...>
    }

Then the top-level function (in my case, CLI) will read the buffer
backwards (splitting on `"\x00"`) and print:

    user id=123: name 'Žvangalas' must be ascii-letters only

To read that buffer backwards, dear readers of this commit message, I
need `mem.splitBackwards`.
2022-06-29 08:23:09 +02:00
Frank Denis
41533fa6a1
std/crypto/{25519,pcurves}: make the scalar field order public (#11955)
For 25519, it's very likely that applications would ever need the
serialized representation. Expose the value as an integer as in
other curves. Rename the internal representation from `field_size`
to `field_order` for consistency.

Also fix a common typo in `scalar.sub()`.
2022-06-29 07:44:43 +02:00
Frank Denis
b2e4dda001
std.crypto.{p256,p384}: process the top nibble in mulDoubleBasePublic (#11956)
Unlike curve25519 where the scalar size is not large enough to fill
the top nibble, this can definitely be the case for p256 and p384.
2022-06-29 07:43:49 +02:00
Andrew Kelley
c3ae909e93 Revert "AstGen: preserve inferred ptr result loc for breaks"
This reverts commit 8bf3e1f8d0902abd4133e2729b3625c25011c3ff, which
introduced miscompilations for peer expressions any time they needed
coercions to runtime types.

I opened #11957 as a proposal to accomplish the goal of the reverted
commit.

Closes #11898
2022-06-28 18:38:25 -07:00
Ben Fiedler
76546b3f8e std.mem: add peek() to TokenIterator(T) 2022-06-28 21:45:02 +02:00
Philipp Lühmann
bb2929ba08
zig fmt: fix idempotency with newlines surrounding doc comment
Fixes: https://github.com/ziglang/zig/issues/11802
2022-06-28 21:38:28 +02:00
Andrew Kelley
8974cee5a1
Merge pull request #11919 from squeek502/failing-allocator-stacktrace
Add stack trace capturing to `FailingAllocator` and use it to improve `checkAllAllocationFailures`
2022-06-28 14:44:00 -04:00
Jakub Konka
9e8298b864
Merge pull request #11950 from ziglang/macho-weak-libs-frameworks
macho: fully implement `-weak-lx` and `-weak_framework x` flags
2022-06-28 20:29:20 +02:00
Jakub Konka
5834a608fc link-tests: do not save global extracted var unless a match
Improve testing MachO binaries by verbose printing of the symtab
which includes segment,section names for defined symbols, and
import (dylib) name for imports.
2022-06-28 10:23:25 +02:00
Jakub Konka
075f5bc5ff link-tests: test -weak-lx and -weak_framework x 2022-06-28 09:22:04 +02:00
Jakub Konka
20bd722464 build: handle weakly imported libs and frameworks 2022-06-28 09:19:01 +02:00
Jakub Konka
f353b59efa macho: discriminate between normal and weak dylibs
Parse `-weak-lx` and `-weak_framework x` in the CLI.
2022-06-28 09:18:54 +02:00
Andrew Kelley
975a660807
Merge pull request #11945 from ziglang/stage2-std
stage2 fixes towards standard library tests passing
2022-06-28 01:34:46 -04:00
Andrew Kelley
a71d00a4d5 std.crypto.25519.field: avoid excessive inlining
This valid zig code produces reasonable LLVM IR, however, on the
wasm32-wasi target, when using the wasmtime runtime, the number of
locals of the `isSquare` function exceeds 50000, causing wasmtime
to refuse to execute the binary.

The `inline` keyword in Zig is intended to be used only where it is
semantically necessary; not as an optimization hint. Otherwise, this may
produce unwanted binary bloat for the -OReleaseSmall use case.

In the future, it is possible that we may end up with both `inline`
keyword, which operates as it does in status quo, and additionally
`callconv(.inline_hint)` which has no semantic impact, but may be
observed by optimization passes.

In this commit, I also cleaned up `isSquare` by eliminating an
unnecessary mutable variable, replacing it with several local constants.

Closes #11947.
2022-06-27 19:11:55 -07:00
Andrew Kelley
0b8bd9b2b4 std.os.linux.clone: upgrade to stage2 fn ptr semantics 2022-06-27 18:27:06 -07:00
Andrew Kelley
df1f401cf0 std.x.os.net: make error set consistent across targets 2022-06-27 18:26:50 -07:00
Andrew Kelley
3c1daf951c LLVM: fix invalid IR on @returnAddress of wasm/bpf
see #11946
2022-06-27 17:12:45 -07:00
Jakub Konka
0dd28920da macho: implement and handle -needed-* and -needed_* family of flags
MachO linker now handles `-needed-l<name>`, `-needed_library=<name>`
and `-needed_framework=<name>`. While on macOS `-l` is equivalent
to `-needed-l`, and `-framework` to `-needed_framework`, it can be
used to the same effect as on Linux if combined with `-dead_strip_dylibs`.

This commit also adds handling for `-needed_library` which is macOS
specific flag only (in addition to `-needed-l`).

Finally, in order to leverage new linker testing harness, this commit
added ability to specify lowering to those flags via `build.zig`:
`linkSystemLibraryNeeded` (and related), and `linkFrameworkNeeded`.
2022-06-27 19:53:38 +02:00
Jakub Konka
efc5c97bff macho: implement -dead_strip_dylibs linker flag 2022-06-27 19:53:38 +02:00
Ryan Liptak
a76775b50a Fix stack traces with non-null first_address on Windows
Before this commit, the passed in length would always be given to the RtlCaptureStackBackTrace call. Now we always give the length of the actual buffer we're using (the addr_buf_stack size of 32 or the passed in length if it's larger than 32; this matches what the doc comment says the function was meant to be doing as well).

This was causing empty stack traces for things like the GeneralPurposeAllocator leak checking.

Fixes #6687
2022-06-27 20:00:39 +03:00
Ryan Liptak
22720981ea Move sys_can_stack_trace from GPA to std.debug so that it can be re-used as needed 2022-06-25 21:27:56 -07:00
Jakub Konka
589bf67635 macho: implement -headerpad_max_install_names 2022-06-25 18:04:40 +02:00
Jakub Konka
8c1feef4cd macho: implement -headerpad_size option
Includes both traditiona and incremental codepaths with one caveat that
in incremental case, the requested size cannot be smaller than the
default padding size due to prealloc required due to incremental nature
of linking.

Also parse `-headerpad_max_install_names`, however, not actionable just yet -
missing implementation.
2022-06-25 17:59:08 +02:00
Jakub Konka
f91503e577 link-tests: defer parsing of the RPN program until running the action 2022-06-25 17:56:03 +02:00
Jakub Konka
0df7ed79d3 macho: implement -search_dylibs_first linker option 2022-06-24 20:25:16 +02:00
Ryan Liptak
19d7f4dd82 FailingAllocator: Only capture the stack trace of the first induced allocation failure
This is a precaution to avoid confusing stack traces on the off chance that FailingAllocator continues to try to allocate after the first failure.
2022-06-23 17:27:55 -07:00
Ryan Liptak
c321b2f2a0 checkAllAllocationFailures: add possibility of SwallowedOutOfMemoryError (split from NondeterministicMemoryUsage)
Inducing failure but not getting OutOfMemory back is not as much of a problem as never inducing failure when it was expected to be induced, so treating them differently and allowing them to be handled differently by the caller is useful.

For example, the current implementation of `std.HashMapUnmanaged.getOrPutContextAdapted` always tries to grow and then recovers from OutOfMemory by attempting a lookup of an existing key. If this function is used (i.e. from `std.BufMap.putMove`) with `checkAllAllocationFailures`, then we'd have previously triggered `error.NondeterministicMemoryUsage`, but the real cause is that `OutOfMemory` is being recovered from and so the error is being swallowed. The new error allows us to both understand what's happening easier and to catch it and ignore it if we're okay with the code we're testing handling `error.OutOfMemory` without always bubbling it up.
2022-06-23 17:20:24 -07:00
Ryan Liptak
def304a9a5 Integrate FailingAllocator stack trace with testing.checkAllAllocationFailures 2022-06-23 17:02:29 -07:00
Ryan Liptak
819e0e83d3 Add stack trace capturing to FailingAllocator 2022-06-23 17:01:56 -07:00
Jakub Konka
291c08f7b0
Merge pull request #11910 from ziglang/linker-tests 2022-06-24 00:02:12 +02:00
FlandreScarlet
87d8cb19e4
std.debug: fix ConfigurableTrace.dump OOB
The for-loop in dump() would index out of bounds if `t.index` is greater
than size, because `end` is the maximum of `t.index` and `size` rather than the
minimum.
2022-06-23 15:07:08 +03:00
Jakub Konka
03ddb42b8b link-tests: rename check() to checkStart()
Do not hardcode the symtab label; instead allow each parser to define
its own.

Check for missing extractor value in the matcher when matching `{}`.
2022-06-23 13:16:03 +02:00
Jakub Konka
6e04c2faab link-tests: fix parsing symtab for macho 2022-06-23 13:16:03 +02:00
Jakub Konka
e6c012c743 link-tests: add better docs 2022-06-22 22:40:05 +02:00
Jakub Konka
51f2442fc4 link-tests: clean up error messages in case of failure 2022-06-22 22:24:52 +02:00
Jakub Konka
b35e434cae link-tests: clean up linker testing harness 2022-06-22 18:34:39 +02:00
Jakub Konka
211de9b63b link-tests: fix dumping of LOAD_DYLIB: name instead of path field 2022-06-22 10:40:10 +02:00
Jakub Konka
23a63f4ce4 link-tests: rename CheckMachOStep to CheckObjectStep and accept obj format 2022-06-22 10:27:51 +02:00
Jakub Konka
b5601a2da6 link-tests: extract values into variables
We can then collect multiple variables (currently assumed always
in global scope) and run a comparison with some very basic
arithmetic on the values.
2022-06-22 00:49:22 +02:00
Jakub Konka
3bb4d65b2f link-tests: move macho tests to subfolder
Handle `-e` option in MachO linker allowing the user to set a custom
entrypoint address.
2022-06-21 23:01:09 +02:00
Jakub Konka
937464f398 link-tests: dump metadata to string and grep results
This approach is more inline with what LLVM/LLD does for testing
of their output, and seems to be more generic and easier to extend
than implementing a lot of repetitive and nontrivial comparison
logic when working directly on structures.
2022-06-21 22:19:57 +02:00