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

Wrong local string varialbe allocation #819

Open
cpunion opened this issue Oct 8, 2024 · 0 comments
Open

Wrong local string varialbe allocation #819

cpunion opened this issue Oct 8, 2024 · 0 comments

Comments

@cpunion
Copy link
Contributor

cpunion commented Oct 8, 2024

package main

func Foo(s string) int {
	return len(s)
}

func Test() {
	for i := 0; i < 1000000; i++ {
		_ = Foo("hello")
	}
}

func main() {
	Test()
}

Run with:

$ llgo build -o string . && ./string
# github.com/goplus/llgo/_demo/string
[1]    69802 segmentation fault  ./string

Generated code by llgen:

define void @main.Test() {
_llgo_0:
  br label %_llgo_3

_llgo_1:                                          ; preds = %_llgo_3
  %0 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
  %1 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 0
  store ptr @0, ptr %1, align 8
  %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %0, i32 0, i32 1
  store i64 5, ptr %2, align 4
  %3 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %0, align 8
  %4 = call i64 @main.Foo(%"github.com/goplus/llgo/internal/runtime.String" %3)
  %5 = add i64 %6, 1
  br label %_llgo_3

_llgo_2:                                          ; preds = %_llgo_3
  ret void

_llgo_3:                                          ; preds = %_llgo_1, %_llgo_0
  %6 = phi i64 [ 0, %_llgo_0 ], [ %5, %_llgo_1 ]
  %7 = icmp slt i64 %6, 1000000
  br i1 %7, label %_llgo_1, label %_llgo_2
}

Allocating memory by alloca on the stack in each iteration will exhaust the stack.

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

No branches or pull requests

1 participant