Skip to content

oddness with stack in llvm output + s2wasm #1328

@vvuk

Description

@vvuk

With bitcode that looks like this in .ll (yes, unoptimized):

define dllexport i32 @TestMethod() {
entry:
  %var.0. = alloca i32
  br label %BL.entry
BL.entry:                                         ; preds = %entry
  br label %BL.0000
BL.0000:                                          ; preds = %BL.entry
  store i32 5, i32* %var.0., align 1
  br label %BL.0005
BL.0005:                                          ; preds = %BL.0000
  %0 = load i32, i32* %var.0., align 1
  ret i32 %0
}

This generates the following .s using llc --asm-verbose -o bitcode.s bitcode.bc:

	.globl	TestMethod
	.type	TestMethod,@function
TestMethod:
	.result 	i32
	i32.const	$push2=, 0
	i32.load	$push1=, __stack_pointer($pop2)
	i32.const	$push3=, 16
	i32.sub 	$push4=, $pop1, $pop3
	i32.const	$push0=, 5
	i32.store	12($pop4), $pop0
	i32.const	$push5=, 5
	.endfunc

Are those $push2= and $pop2s magic push/pop operands somehow?

Which when it goes through s2wasm:

 (func $TestMethod (; 2 ;) (result i32)
  (i32.store offset=12
   (i32.sub (i32.load offset=4 (i32.const 0)) (i32.const 16))
   (i32.const 5)
  )
  (i32.const 5)
 )

This ends up doing an invalid memory access:

$ wasm-interp foo.wasm --run-all-exports --trace
>>> running export "TestMethod":
#0.    0: V:0  | i32.const $0
#0.    5: V:1  | i32.load $0:0+$4
#0.   14: V:1  | i32.const $16
#0.   19: V:2  | i32.sub 0, 16
#0.   20: V:1  | i32.const $5
#0.   25: V:2  | i32.store $0:4294967280+$12, 5
TestMethod() => error: out of bounds memory access

It looks like this is expecting to manipulate the stack (the __stack_pointer($pop2) thing), but that's turning into... i32.const 0? Yes, I am very confused. :)

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