golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.87k stars 17.52k forks source link

runtime: crosscompiled to AIX/ppc64 not working in IBMi PASE (AIX7.2) due to "not in usable address space" #45017

Open rfx77 opened 3 years ago

rfx77 commented 3 years ago

I want to crosscompile a simple Hello-World example to IBM-i PASE environment. This is a AIX 7.2 subsystem on the IBMi (AS400). Normally AIX compiled code should work in this environment.

But when i want to run the test application i get the following error:

runtime: memory allocated by OS [0x700000028000000, 0x70000002c000000) not in usable address space: base outside usable address space
fatal error: memory reservation exceeds address space limit

runtime stack:
runtime.throw(0x1000d9f06, 0x2e)
        /usr/lib/go-1.14/src/runtime/panic.go:1116 +0x68 fp=0xffffffffffff590 sp=0xffffffffffff550 pc=0x100032e08
runtime.(*mheap).sysAlloc(0x180037440, 0x400000, 0x100062a18, 0x9001000a01a1450)
        /usr/lib/go-1.14/src/runtime/malloc.go:706 +0x838 fp=0xffffffffffff660 sp=0xffffffffffff590 pc=0x10000a9a8
runtime.(*mheap).grow(0x180037440, 0x1, 0x0)
        /usr/lib/go-1.14/src/runtime/mheap.go:1286 +0x18c fp=0xffffffffffff6f0 sp=0xffffffffffff660 pc=0x10002586c
runtime.(*mheap).allocSpan(0x180037440, 0x1, 0x2a000100062a18, 0x18004d8e8, 0x900000000332200)
        /usr/lib/go-1.14/src/runtime/mheap.go:1124 +0x748 fp=0xffffffffffff780 sp=0xffffffffffff6f0 pc=0x100025578
runtime.(*mheap).alloc.func1()
        /usr/lib/go-1.14/src/runtime/mheap.go:871 +0x7c fp=0xffffffffffff7e8 sp=0xffffffffffff780 pc=0x10005e26c
runtime.systemstack(0xffffffffffff840)
        /usr/lib/go-1.14/src/runtime/asm_ppc64x.s:295 +0xd0 fp=0xffffffffffff808 sp=0xffffffffffff7e8 pc=0x1000604c0

...

Is this a problem which can be solved easily? How can i fix this?

This is the go code i compiled

package main

import "fmt"

func main() {
   fmt.Println("hello world22e")

}

i tried the crosscompile from windows and linux with the same result.

greetings, Franz

onlysumitg commented 1 year ago

@rfx77 is your statement true for cross-compiled executables also?

mknyszek commented 1 year ago

@rfx77 I went back and read through this issue, and it seems like we should maybe just support the 0x7000... base address scheme (including in lfstack)? That's a small enough patch that I don't feel uncomfortable with landing it, I think we'd just need to add some kind of compile-time switch. A GOEXPERIMENT, maybe? (I don't think we can afford to make arenaBaseOffset non-constant; that would surely slow down one of the hot paths in the runtime.) I'll think about this.

What's really unfortunate about all this is that ideally we wouldn't have an arenaBaseOffset for AIX at all. We could just set heapAddrBits to 60. This is how it used to be, but the runtime has a data structure that maps virtual memory proportional to the address space size. With a value of 60, that mapping would be about 2 TiB (PROT_NONE, so no actual physical memory used). Unfortunately, I found that aix/ppc64 code runs really slowly when you map that amount of virtual address space, hence the arenaBaseOffset workaround.

I also dont see that without IBM Support there will be a usefull solution because of the async filesys problems with the IFS and the 64002005 Errors.

This doesn't seem solvable from our side, but at least we could get past the problems on our side.

greenscreens-io commented 1 year ago

Just to give a hint if this might help... https://ibmi-oss-docs.readthedocs.io/en/latest/porting/GOTCHAS.html

onlysumitg commented 1 year ago

Thanks. I was able to cross compile the golang app without cgo for AIX/ppc64. But still not luck with cgo and ODBC uses cgo.

rfx77 commented 1 year ago

Thanks. I was able to cross compile the golang app without cgo for AIX/ppc64. But still not luck with cgo and ODBC uses cgo.

You need to install gcc10. then you can enable cgo.

https://github.com/golang/go/issues/45017#issuecomment-818973695

onlysumitg commented 1 year ago

Thanks. I was able to cross compile the golang app without cgo for AIX/ppc64. But still not luck with cgo and ODBC uses cgo.

You need to install gcc10. then you can enable cgo.

#45017 (comment)

Thanks. I have gcc 11.3.0.

It seems I have to compile the code on IBM I(AIX). Cross compile for IBM I(AIX) from Linux is not going to work.

When I try to cross compile, gcc complains about undefined -maix64 flag. As per gcc documentation, this flag is hardware specific.

onlysumitg commented 1 year ago

I have create a version that that works on AIX (on IBM I). Based on this discussion and notes from other people. Its not perfect but its works (somehow) if anyone wants to play around.

Its based on golang version 1.20.5 and its able to compile CGO apps as well.

https://github.com/onlysumitg/ibmigo

Neo4jvv commented 7 months ago

Hello, to cross-compile the aix executable on windows, I use go version 1.16 and 1.19, Compiling command GOOS=aix GOARCH=ppc64 CGO_ENABLED=0 go build The compiled file cannot be executed on aix with oslevel=7.2.0.0. Illegal instruction(coredump) is returned. Is there a solution

randall77 commented 7 months ago

@Neo4jvv Have you tried a more recent release, like 1.21 or 1.22rc1?

Neo4jvv commented 7 months ago

Yes, I followed up with 1.13, 1.15, 1.21.6,but nothing worked @randall77

onlysumitg commented 7 months ago

Cross compile from windows wont work for AIX on power system. You have to compile on Power system.

I added a link to github repo in this thread, check the readme there. That might help you.

randall77 commented 7 months ago

@onlysumitg : I think @neo4jvv 's issue is different, as they are trying to compile with CGO_ENABLED=0. That should generate the same target binary regardless of the host build system.

@neo4jvv Does cross-compiling work for you from another OS, like linux?

I can get the same binary compiling from amd64/linux and arm64/darwin:

hello.go:

package main

import "fmt"

func main() {
    fmt.Printf("hello world\n")
}

arm64/darwin:

% GOARCH=ppc64 GOOS=linux CGO_ENABLED=0 ~/go1.21.6/bin/go build -trimpath hello.go
% md5 hello
MD5 (hello) = 5b9b78ab0f9b40ed15514e0645726feb

amd64/linux:

$ GOARCH=ppc64 GOOS=linux CGO_ENABLED=0 ~/go1.21.6/bin/go build -trimpath hello.go 
$ md5sum hello
5b9b78ab0f9b40ed15514e0645726feb  hello
onlysumitg commented 7 months ago

Sorry, Missed the cgo_enabled flag. Yes it should work. But you need to make some changes in go runtime/code as recommended by @rfx77

Neo4jvv commented 7 months ago

@randall77 I can compile linux/amd64, linux/arm64 files from windows and execute normally, but aix/ppc64 compiled files will fail to execute

randall77 commented 7 months ago

That is strange. Do your cross-compiled and native-compiled binaries have the same md5? If not, can you tell what is different about them? (objdump them, strings them, ...)

onlysumitg commented 7 months ago

Download the go lang code from repo i mentioned on windows and cross compile using that. It will work.

Neo4jvv commented 7 months ago

@onlysumitg Where do I get the link you uploaded, this is the first time I asked on github, I don't know how to operate, can you post the link here

onlysumitg commented 7 months ago

Try this one https://github.com/onlysumitg/ibmigo.

I have to find the link that explain how to compile go from the source code.

Neo4jvv commented 7 months ago

@onlysumitg Is your approach to compile go code on an aix host? But my development environment is windows, if I do not have an aix host is not able to implement this method

onlysumitg commented 7 months ago

@Neo4jvv dont worry about the readme. I wrote that considering the need to cgo.

I belive you can use this golang code. Compile it on windows and use that version to cross compile for aix/ppc.

I can try to write down exact step for windows in few days.

Neo4jvv commented 7 months ago

@onlysumitg Okay, I'll try it now. Thank you

Neo4jvv commented 7 months ago

@onlysumitg Please try to write the operation steps of windows,Please ,Please ,Please ,thank u, thank u,thank u

Neo4jvv commented 7 months ago

@onlysumitg GOROOT=D:\ibmigo-main\go cannot be set directly after cloning this DIRECTORY locally, and the go file in the bin directory is not an exe file

onlysumitg commented 7 months ago

@Neo4jvv the ibmigo is for AIX PPC64 not for windows.

for windows please give a try to following:

https://github.com/onlysumitg/win_ibmigo