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

Add back Console::drop #17

Merged
merged 4 commits into from
Jan 12, 2022
Merged

Add back Console::drop #17

merged 4 commits into from
Jan 12, 2022

Conversation

AzureMarker
Copy link
Member

@AzureMarker AzureMarker commented Jan 11, 2022

This is needed for soundness. Otherwise libctru will have a dangling pointer and will write to it when printing.

Closes #15 (I think, maybe we also need some panic changes before closing?)

This is needed for soundness. Otherwise libctru will have a dangling
pointer, and will write to it when printing.
@Meziu
Copy link
Member

Meziu commented Jan 11, 2022

Testing your branch against a simple "make console, drop it and then print" binary, made it crash on drawChar:

image

Does it work to you?

@Meziu
Copy link
Member

Meziu commented Jan 11, 2022

Just to be on the same page, my main is:

    ctru::init();
    let gfx = Gfx::default();
    let hid = Hid::init().expect("Couldn't obtain HID controller");
    let apt = Apt::init().expect("Couldn't obtain APT controller");
    let _console = Console::default();
    drop(_console);

    println!("Works");

If the default console isn't initialized then it will segfault.
@AzureMarker
Copy link
Member Author

I guess I had the misconception that the initial selected console value is the default console. It's actually a zeroed-out console:
https://github.com/devkitPro/libctru/blob/5f13628dac75206f0c97d29a7427ce8284d910f1/libctru/source/console.c#L65-L67

(I was wondering if it would use uninitialized memory or zeroed memory, and found this: https://stackoverflow.com/questions/4080673/are-the-members-of-a-global-structure-initialized-to-zero-by-default-in-c)

So I'll change it to set the console to some zeroed out console on drop, if it was the selected console.

@AzureMarker
Copy link
Member Author

Unfortunately right now std::mem::zeroed is not const:
rust-lang/rust#62061

So I used the recommended workaround:
https://crates.io/crates/const-zero

@AzureMarker AzureMarker changed the title Add back slightly simpler Console::drop Add back Console::drop Jan 12, 2022
@AzureMarker
Copy link
Member Author

AzureMarker commented Jan 12, 2022

It's no longer simpler, but at least it's safe.

@Meziu
Copy link
Member

Meziu commented Jan 12, 2022

Nice, not printing once the Console is dropped is both expected behaviour and easy to use.

@Meziu Meziu merged commit 6ffcc98 into rust3ds:master Jan 12, 2022
@AzureMarker AzureMarker deleted the fix/console-drop branch January 13, 2022 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Console printing and lifetimes
2 participants