Skip to content

it is easy confuse CLI options / command line parameters -D and -O when switching between zig build and zig build-exe #12341

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

Open
sno2 opened this issue Aug 5, 2022 · 4 comments
Labels
use case Describes a real use case that is difficult or impossible, but does not propose a solution.
Milestone

Comments

@sno2
Copy link
Contributor

sno2 commented Aug 5, 2022

Zig Version

0.10.0-dev.3445

Steps to Reproduce

Access the Godbolt playground for the following code or compile on your local and observe using a disassembler:

const std = @import("std");

pub fn main() !void {
    var message = try std.heap.page_allocator.dupe(u8, "Hello, world!");
    std.heap.page_allocator.destroy(message.ptr);
    message.ptr.* = undefined;
}

Compile using -DReleaseFast.

Expected Behavior

Assigning pointers to undefined should be optimized out in ReleaseFast. Assigning pointers to undefined is extremely useful to catch use after free UB in development, especially in more complicated deinit() functions to make access to the self after destruction fail. However, it can be a pointless hit to performance when it is not optimized out in production.

Actual Behavior

Compiled with -DReleaseFast the relevant assembly code is the following:

main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 128
        mov     rsi, rdi
        mov     qword ptr [rbp - 112], rsi
        lea     rdi, [rbp - 80]
        movabs  rdx, offset __unnamed_21
        movabs  rcx, offset __unnamed_22
        call    std.mem.Allocator.dupe
        cmp     word ptr [rbp - 64], 0
        je      .LBB62_2
        mov     rdi, qword ptr [rbp - 112]
        mov     ax, word ptr [rbp - 64]
        mov     word ptr [rbp - 114], ax
        mov     word ptr [rbp - 50], ax
        call    __zig_return_error
        mov     ax, word ptr [rbp - 114]
        add     rsp, 128
        pop     rbp
        ret
.LBB62_2:
        mov     rax, qword ptr [rbp - 80]
        mov     qword ptr [rbp - 96], rax
        mov     rax, qword ptr [rbp - 72]
        mov     qword ptr [rbp - 88], rax

        ; std.heap.page_allocator.destroy(message.ptr);
        mov     rsi, qword ptr [rbp - 96]
        mov     qword ptr [rbp - 104], rsi
        movabs  rdi, offset __unnamed_23
        call    std.mem.Allocator.destroy

        ;  message.ptr.* = undefined;
        mov     rdi, qword ptr [rbp - 96]
        mov     qword ptr [rbp - 128], rdi
        mov     esi, 170
        mov     edx, 1
        call    memset@PLT
        mov     rax, qword ptr [rbp - 128]
        mov     qword ptr [rbp - 48], 1296236545
        mov     qword ptr [rbp - 40], rax
        mov     qword ptr [rbp - 32], 1
        mov     qword ptr [rbp - 24], 0
        mov     qword ptr [rbp - 16], 0
        mov     qword ptr [rbp - 8], 0
        lea     rax, [rbp - 48]
        xor     ecx, ecx
        mov     edx, ecx
        rol     rdi, 3
        rol     rdi, 13
        rol     rdi, 61
        rol     rdi, 51
        xchg    rbx, rbx

        mov     word ptr [rbp - 50], 0
        xor     eax, eax
        add     rsp, 128
        pop     rbp
        ret

Note how it still included the assembly for the message.ptr.* = undefined instruction.

@sno2 sno2 added the bug Observed behavior contradicts documented or intended behavior label Aug 5, 2022
@andrewrk andrewrk added question No questions on the issue tracker, please. and removed bug Observed behavior contradicts documented or intended behavior labels Aug 6, 2022
@andrewrk andrewrk added this to the 0.10.0 milestone Aug 6, 2022
@andrewrk
Copy link
Member

andrewrk commented Aug 6, 2022

Usage:

  -O [mode]                 Choose what to optimize for
    Debug                   (default) Optimizations off, safety on
    ReleaseFast             Optimizations on, safety off
    ReleaseSafe             Optimizations on, safety on
    ReleaseSmall            Optimize for small binary, safety off
...
  -D[macro]=[value]         Define C [macro] to [value] (1 if [value] omitted)

Sorry for the confusion, -D is used when using zig build but means something different when using the CLI.

Leaving the issue open to track this problem.

@andrewrk andrewrk changed the title ReleaseFast does not optimize out undefined assignment to pointers it is easy confuse CLI options -D and -O for command line parameters Aug 6, 2022
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Aug 6, 2022
@andrewrk andrewrk added use case Describes a real use case that is difficult or impossible, but does not propose a solution. and removed question No questions on the issue tracker, please. labels Aug 6, 2022
@andrewrk andrewrk changed the title it is easy confuse CLI options -D and -O for command line parameters it is easy confuse CLI options / command line parameters -D and -O when switching between zig build and zig build-exe Aug 6, 2022
@sno2
Copy link
Contributor Author

sno2 commented Aug 6, 2022

Ah that makes more sense. Thanks for the quick response

@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 May 29, 2023
@mlugg
Copy link
Member

mlugg commented May 29, 2023

The more general issue here is that the meaning of -D is overloaded between zig build and zig build-{exe,obj,lib}. The ideal solution would be to rename zig build's -D option to use some other character - importantly, one which is unused by build-exe etc, so they can emit errors for it.

@xdBronch
Copy link
Contributor

was helping someone earlier and came across this problem (yet again), i think a potential solution would be to only allow the C style -Dmacro=value inside of the CLIs -cflags <flags> -- system. potential problem with that would be that that only applies to C file that are passed after the -cflags, maybe a warning could be issued if a user passes -cflags but no subsequent C files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
use case Describes a real use case that is difficult or impossible, but does not propose a solution.
Projects
None yet
Development

No branches or pull requests

4 participants