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

Draft: start to implement emscripten_return_address for UBSan support #420

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mrexodia
Copy link

@mrexodia mrexodia commented Jan 26, 2023

This isn't really a pull request in the traditional sense, but I figure it will be easier to ask my question this way.

My goal is to compile a WASM executable with emscripten+UBSan (https://emscripten.org/docs/debugging/Sanitizers.html) and then run it in wasm3, showing a backtrace if anything is triggered.

I took the null-assign.c from the page:

int main(void) {
    int *a = 0;
    *a = 0;
}

Built it with the minimal UBSan runtime:

emcc null-assign.c -fsanitize=undefined -fsanitize-minimal-runtime -o null-assign.wasm -g

And I get null-assign.wasm.

I figured out that I need to set some preprocessor definitions to enable the call stack and trace output: -Dd_m3EnableStrace=2 -Dd_m3RecordBacktraces. Additionally I added my own -Dd_m3HasUBSan and implemented a stub emscripten_return_address (the function that the UBSan runtime needs).

Everything looks 'fine' from the trace (except obviously the 1234 value for the return address which triggers another check):

build\Debug>wasm3 null-assign.wasm
_start () {
  __wasm_call_ctors () {
    emscripten_stack_init () {
    }
  }
  __original_main () {
    __ubsan_handle_type_mismatch_minimal () {
      env!emscripten_return_address(0) { <native> } = 1234
      report_this_error(void*) (i32: 1234) {
      } = 1
      strlen (i32: 65560) {
      } = 21
      write (i32: 2, i32: 65560, i32: 21) {
ubsan: type-mismatch
        wasi_snapshot_preview1!fd_write(2, 65512, 1, 65508) { <native> } = 0
        __wasi_syscall_ret (i32: 0) {
        } = 0
      } = 21
    }
  } = 0
  exit (i32: 0) {
    dummy () {
    }
    libc_exit_fini () {
      dummy () {
      }
    }
    dummy () {
    }
    _Exit (i32: 0) {
      wasi_snapshot_preview1!proc_exit(0) { <native> } -> [trap] program called exit
Assertion failed at D:\CodeBlocks\wasm3\source\m3_core.c:556 : pcFound

The issue is at the end, it looks like it is not able to match the PC register back to an offset in the file. Any ideas why? I tried messing with the emscripten_return_address function signature but it doesn't appear to make a difference...

@mrexodia mrexodia marked this pull request as draft January 26, 2023 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant