Skip to content

Bad load offset emitted for i64.load on Emscripten 3.1.66+ #22794

@Twinklebear

Description

@Twinklebear

Summary

I started seeing a crash in our application on Emscripten 3.1.66+ when loading a GLTF file with Magnum and traced it down to what appears to be a bad compile output by Emscripten, where on 3.1.66+ an i64.load instruction is emitted with offset=4294967280 causing the app to crash w/ an out of bounds memory access.

I was able to make an isolated reproducer case repo which you can find here: https://github.com/Twinklebear/repro-gltf-compile-bug , which includes minimal code to reproduce the issue, instructions on building it, and where the bad output assembly can be found. In short, part of the lambda here: https://github.com/mosra/magnum-plugins/blob/master/src/MagnumPlugins/GltfImporter/GltfImporter.cpp#L3349 on Emscripten 3.1.65 compiles to:

local.get $p2
i32.load offset=12
local.get $p2
i32.const 4
i32.sub
i32.load
i32.eq
if $I26
  local.get $l4
  local.get $p2
  i64.load align=4
  local.tee $l8
  i64.store offset=504
  local.get $l4
  local.get $p2
  i32.const 16
  i32.sub
  i64.load align=4
  local.tee $l18
  i64.store offset=880
  local.get $l4
  local.get $l8
  i64.store offset=448
  local.get $l4
  local.get $l18
  i64.store offset=440
  i32.const 0
  local.set $l6
  local.get $l4
  i32.const 448
  i32.add
  local.get $l4
  i32.const 440
  i32.add
  call $Corrade::Containers::operator==_Corrade::Containers::BasicStringView<char_const>__Corrade::Containers::BasicStringView<char_const>_
  br_if $B24
end

However, starting with 3.1.66, the second i64.load here starts being emitted
with an offset of 4294967280 max, causing a memory out of bounds error to occur.

local.get $p2
i32.load offset=12
local.get $p2
i32.const 4
i32.sub
i32.load
i32.eq
if $I26
  local.get $l4
  local.get $p2
  i64.load align=4
  local.tee $l8
  i64.store offset=504
  local.get $l4
  local.get $p2
  i64.load offset=4294967280 align=4
  local.tee $l18
  i64.store offset=880
  local.get $l4
  local.get $l8
  i64.store offset=448
  local.get $l4
  local.get $l18
  i64.store offset=440
  i32.const 0
  local.set $l6
  local.get $l4
  i32.const 448
  i32.add
  local.get $l4
  i32.const 440
  i32.add
  call $Corrade::Containers::operator==_Corrade::Containers::BasicStringView<char_const>__Corrade::Containers::BasicStringView<char_const>_
  br_if $B24
end

The repro case repo should have everything needed to reproduce the good/bad outputs: https://github.com/Twinklebear/repro-gltf-compile-bug but please let me know if you run into issues trying to repro the issue or other questions.

Basic Info

Version of emscripten/emsdk:
Emscripten 3.1.66 and later

Failing command line in full:
See repro repo for build config: https://github.com/Twinklebear/repro-gltf-compile-bug/blob/main/src/CMakeLists.txt , but not sure the flags are relevant as this is a bad ASM output.

Full link command and output with -v appended:
See repro repo for build config: https://github.com/Twinklebear/repro-gltf-compile-bug/blob/main/src/CMakeLists.txt , but not sure the flags are relevant as this is a bad ASM output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions