mirror of
https://codeberg.org/ziglings/exercises.git
synced 2024-12-27 18:06:28 +00:00
added ub exploration to ex090
This commit is contained in:
parent
34af14ca7b
commit
be349dae5d
2 changed files with 65 additions and 1 deletions
20
091_async8.zig
Normal file
20
091_async8.zig
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
//
|
||||||
|
// Perhaps you have been wondering why we have always called 'suspend'
|
||||||
|
// with an expression in the form of an empty block:
|
||||||
|
//
|
||||||
|
// suspend {}
|
||||||
|
//
|
||||||
|
// well,
|
||||||
|
//
|
||||||
|
const print = @import("std").debug.print;
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
|
||||||
|
var my_beef = getBeef(0);
|
||||||
|
|
||||||
|
print("beef? {X}!\n", .{my_beef});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getBeef(input: u32) u32 {
|
||||||
|
suspend {}
|
||||||
|
}
|
|
@ -35,9 +35,53 @@ pub fn main() void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getBeef(input: u32) u32 {
|
fn getBeef(input: u32) u32 {
|
||||||
if (input > 0xDEAD) {
|
if (input == 0xDEAD) {
|
||||||
suspend {}
|
suspend {}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0xBEEF;
|
return 0xBEEF;
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// Going Deeper Into...
|
||||||
|
// ...uNdeFiNEd beHAVi0r!
|
||||||
|
//
|
||||||
|
// We haven't discussed it yet, but runtime "safety" features
|
||||||
|
// require some extra instructions in your compiled program.
|
||||||
|
// Most of the time, you're going to want to keep these in.
|
||||||
|
//
|
||||||
|
// But in some programs, when data integrity is less important
|
||||||
|
// than raw speed (some games, for example), you can compile
|
||||||
|
// without these safety features.
|
||||||
|
//
|
||||||
|
// Instead of a safe panic when something goes wrong, your
|
||||||
|
// program will now exhibit Undefined Behavior (UB), which simply
|
||||||
|
// means that the Zig language does not (cannot) define what will
|
||||||
|
// happen. The best case is that it will crash, but in the worst
|
||||||
|
// case, it will continue to run with the wrong results and
|
||||||
|
// corrupt your data or expose you to security risks.
|
||||||
|
//
|
||||||
|
// This program is a great way to explore UB. Once you get it
|
||||||
|
// working, try calling the getBeef() function with the value
|
||||||
|
// 0xDEAD so that it will invoke the 'suspend' keyword:
|
||||||
|
//
|
||||||
|
// getBeef(0xDEAD)
|
||||||
|
//
|
||||||
|
// Now when you run the program, it will panic and give you a
|
||||||
|
// nice stack trace to help debug the problem.
|
||||||
|
//
|
||||||
|
// zig run exercises/090_async7.zig
|
||||||
|
// thread 328 panic: async function called...
|
||||||
|
// ...
|
||||||
|
//
|
||||||
|
// But see what happens when you turn off safety checks by using
|
||||||
|
// ReleaseFast mode:
|
||||||
|
//
|
||||||
|
// zig run -O ReleaseFast exercises/090_async7.zig
|
||||||
|
// beef? 0!
|
||||||
|
//
|
||||||
|
// This is the wrong result. On your computer, you may get a
|
||||||
|
// different answer or it might crash! What exactly will happen
|
||||||
|
// is UNDEFINED. Your computer is now like a wild animal,
|
||||||
|
// reacting to bits and bytes of raw memory with the base
|
||||||
|
// instincts of the CPU. It is both terrifying and exhilarating.
|
||||||
|
//
|
||||||
|
|
Loading…
Reference in a new issue