Skip to content

Commit

Permalink
fix(core): remove shutdown()
Browse files Browse the repository at this point in the history
In a very weird situation, our declaration of `shutdown()` shadows a
function `shutdown(int, int)` from sys/socket, which _just happens_ to
be called by libxcb when closing the sdl window. This calls
`main_clean_exit` which calls into micropython and causes at best an
uncaught NLR and at worst an outright segfault because by that time the
micropython environment doesn't exist anymore.

I didn't think this sort of thing would be possible but here we are??

Fixed by removing `__shutdown()` and replacing `shutdown` with
`trezor_shutdown`
  • Loading branch information
matejcik committed Mar 24, 2023
1 parent 987e848 commit 8609f48
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 16 deletions.
4 changes: 2 additions & 2 deletions core/embed/rust/src/trezorhal/fatal_error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod ffi {
extern "C" {
// trezorhal/common.c
pub fn shutdown() -> !;
pub fn trezor_shutdown() -> !;
}
}

Expand All @@ -13,7 +13,7 @@ use heapless::String;
use crate::ui::util::u32_to_str;

fn shutdown() -> ! {
unsafe { ffi::shutdown() }
unsafe { ffi::trezor_shutdown() }
}

#[cfg(feature = "bootloader")]
Expand Down
6 changes: 3 additions & 3 deletions core/embed/trezorhal/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
// from util.s
extern void shutdown_privileged(void);

void __attribute__((noreturn)) shutdown(void) {
void __attribute__((noreturn)) trezor_shutdown(void) {
#ifdef USE_SVC_SHUTDOWN
svc_shutdown();
#else
Expand Down Expand Up @@ -88,7 +88,7 @@ __fatal_error(const char *expr, const char *msg, const char *file, int line,
#endif
display_printf("\nPlease contact Trezor support.\n");
#endif
shutdown();
trezor_shutdown();
}

void __attribute__((noreturn))
Expand All @@ -108,7 +108,7 @@ error_shutdown(const char *label, const char *msg) {
display_printf("\nPlease unplug the device.\n");
#endif
display_backlight(255);
shutdown();
trezor_shutdown();
}

#ifndef NDEBUG
Expand Down
2 changes: 1 addition & 1 deletion core/embed/trezorhal/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

#define STAY_IN_BOOTLOADER_FLAG 0x0FC35A96

void __attribute__((noreturn)) shutdown(void);
void __attribute__((noreturn)) trezor_shutdown(void);

void __attribute__((noreturn))
__fatal_error(const char *expr, const char *msg, const char *file, int line,
Expand Down
10 changes: 4 additions & 6 deletions core/embed/unix/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@
extern void main_clean_exit();
extern float DISPLAY_GAMMA;

void __attribute__((noreturn)) __shutdown(void) {
void __attribute__((noreturn)) trezor_shutdown(void) {
printf("SHUTDOWN\n");
main_clean_exit(3);
for (;;)
;
}

void __attribute__((noreturn)) shutdown(void) { __shutdown(); }

#ifdef RGB16
#define COLOR_FATAL_ERROR RGB16(0x7F, 0x00, 0x00)
#else
Expand Down Expand Up @@ -92,7 +90,7 @@ __fatal_error(const char *expr, const char *msg, const char *file, int line,
printf("Hint:\nIsn't the emulator already running?\n");
#endif
hal_delay(3000);
shutdown();
trezor_shutdown();
}

void __attribute__((noreturn))
Expand Down Expand Up @@ -135,15 +133,15 @@ uint32_t hal_ticks_ms() {
static int SDLCALL emulator_event_filter(void *userdata, SDL_Event *event) {
switch (event->type) {
case SDL_QUIT:
__shutdown();
trezor_shutdown();
return 0;
case SDL_KEYUP:
if (event->key.repeat) {
return 0;
}
switch (event->key.keysym.sym) {
case SDLK_ESCAPE:
__shutdown();
trezor_shutdown();
return 0;
case SDLK_p:
display_save("emu");
Expand Down
2 changes: 1 addition & 1 deletion core/embed/unix/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
})
#endif

void __attribute__((noreturn)) shutdown(void);
void __attribute__((noreturn)) trezor_shutdown(void);

void __attribute__((noreturn))
__fatal_error(const char *expr, const char *msg, const char *file, int line,
Expand Down
3 changes: 0 additions & 3 deletions core/embed/unix/touch/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include <stdbool.h>
#include <stdint.h>

extern void __shutdown(void);
extern const char *display_save(const char *prefix);

#if defined TREZOR_MODEL_T

#include "touch.h"
Expand Down

0 comments on commit 8609f48

Please sign in to comment.