Skip to content
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

enumerate all kinds of undefined behavior, even that which cannot be safety-checked #1966

Open
andrewrk opened this issue Feb 15, 2019 · 3 comments
Labels
Milestone

Comments

@andrewrk
Copy link
Member

The language reference should have a comprehensive list of every of possible undefined behavior that is possible, with a description of either how safety checks work for it, and under what circumstances the safety check will catch the problem or not, or the docs should explain why there cannot be safety checking for this particular kind of undefined behavior.

With this it will be obvious how close (or distant) zig is from a safe language.

@Mouvedia
Copy link

Most undefined behavior that cannot be detected at compile-time can be detected at runtime.

So you are saying that we need to add a no-safety sub section in https://ziglang.org/documentation/master/#Undefined-Behavior that will be continuously populated?
Do you have examples of such cases? e.g. from existing github issues

@hiroakitakada
Copy link

hiroakitakada commented Aug 22, 2020

So you are saying that we need to add a no-safety sub section in https://ziglang.org/documentation/master/#Undefined-Behavior that will be continuously populated?

I believe that the subsection listing all no-safety behavior should be added.

Do you have examples of such cases? e.g. from existing github issues

#3180 is already mentioned above. Other examples are unsafe language constructs and builtin functions including (but, not limited to) the followings.

  • undefined
  • [*]T
  • [*c]T
  • @bitCast
  • @intToPtr
  • @ptrCast
  • @memcpy
  • @memset
  • @fieldParentPtr

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 9, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Jun 4, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 24, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@perillo
Copy link
Contributor

perillo commented Feb 4, 2023

Here is an undefined behavior caused by incorrectly using @qualCast. The UB is currently unchecked (zig version 0.11.0-dev.6306+693b12f8e, stage3 built following the wiki):

comptime

const std = @import("std");
const s = "hello";

pub fn main() void {
    comptime {
        var s2 = @qualCast(*[5:0]u8, s);
        s2[0] = 'H';
    }
}
$ zig run comptime_qual-cast-ub.zig
Segmentation fault at address 0x2086bf
/tmp/zigtmp/comptime_qual-cast-ub.zig:5:5: 0x20b544 in main (comptime_qual-cast-ub)
    comptime {
    ^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:606:22: 0x20aa70 in posixCallMainAndExit (comptime_qual-cast-ub)
            root.main();
                     ^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:376:5: 0x20a521 in _start (comptime_qual-cast-ub)
    @call(.never_inline, posixCallMainAndExit, .{});
    ^
zsh: IOT instruction (core dumped)  ~/src/contrib/zig/github.com/perillo/zig/build/stage3/bin/zig run

runtime

const std = @import("std");
const debug = std.debug;
const s = "hello";

pub fn main() void {
    var s2 = @qualCast(*[5:0]u8, s);
    s2[0] = 'H';

    std.debug.print("s[0]: {c}\n", .{s[0]});
}
$ zig run runtime_qual-cast-ub.zig
Segmentation fault at address 0x2086bf
/tmp/zigtmp/comptime_qual-cast-ub.zig:5:5: 0x20b544 in main (comptime_qual-cast-ub)
    comptime {
    ^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:606:22: 0x20aa70 in posixCallMainAndExit (comptime_qual-cast-ub)
            root.main();
                     ^
/home/manlio/src/contrib/zig/github.com/perillo/zig/lib/std/start.zig:376:5: 0x20a521 in _start (comptime_qual-cast-ub)
    @call(.never_inline, posixCallMainAndExit, .{});
    ^
zsh: IOT instruction (core dumped)  ~/src/contrib/zig/github.com/perillo/zig/build/stage3/bin/zig run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: To do
Development

No branches or pull requests

4 participants