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

cmd/cgo: SIGSEGV, Segmentation fault, during runtime execution on arm (CortexA9) #48404

Closed
kiruxan opened this issue Sep 15, 2021 · 2 comments
Closed

Comments

@kiruxan
Copy link

kiruxan commented Sep 15, 2021

What version of Go are you using (go version)?

On Host machine:

$ go version
go version go1.16.8 linux/amd64

Build with docker: golang:1.16-buster

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

On Host machine:

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.8"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build34544403=/tmp/go-build -gno-record-gcc-switches"

On Target machine:

cat /proc/cpuinfo Output
# cat /proc/cpuinfo
processor	: 0
model name	: ARMv7 Processor rev 10 (v7l)
BogoMIPS	: 6.45
Features	: half thumb fastmult vfp edsp neon vfpv3 tls vfpd32 
CPU implementer	: 0x41
CPU architecture: 7
CPU variant	: 0x2
CPU part	: 0xc09
CPU revision	: 10

processor : 1
model name : ARMv7 Processor rev 10 (v7l)
BogoMIPS : 6.45
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x2
CPU part : 0xc09
CPU revision : 10

processor : 2
model name : ARMv7 Processor rev 10 (v7l)
BogoMIPS : 6.45
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x2
CPU part : 0xc09
CPU revision : 10

processor : 3
model name : ARMv7 Processor rev 10 (v7l)
BogoMIPS : 6.45
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x2
CPU part : 0xc09
CPU revision : 10

Hardware : Freescale i.MX6 Quad/DualLite (Device Tree)
Revision : 0000
Serial : 0000000000000000

On Target machine:

cat /etc/os-release Output
# cat /etc/os-release
NAME=Buildroot
VERSION=2019.11
ID=buildroot
VERSION_ID=2019.11
PRETTY_NAME="Buildroot 2019.11"

What did you do?

I'm building go application that should be run on CortexA9 CPU and uses 3 different C/C++ shared libraries (I use C wrapper to call C++ code), when calling one of them (Clips rules engine http://www.clipsrules.net/) I accidentally get SIGSEGV, Segmentation fault, the library itself is quite old and well tested, also the same code works well on amd64 arch. The place where error occurs looks quite random, if I rewrite code to avoid the specific code lines, it will occur in the middle of an other function.

How I build lib

I use arm-buildroot-linux-gnueabi toolchain and arm-buildroot-linux-gnueabihf-gcc as C compiler, so is built with this CFLAGS: -Wall -std=c99 -O3 -fno-strict-aliasing -fPIC -march=armv7-a -mcpu=cortex-a9 -mfpu=neon -mfloat-abi=hard --sysroot=${some path}

If I build Clips as standalone binary (with this toolchain) and run some rules on it, it works well.

How I build golang app

I build app on docker golang:1.16-buster, where my toolchanin is placed

go clean && env CGO_CFLAGS="-march=armv7-a -mfpu=neon -mfloat-abi=hard -mtune=cortex-a9 --sysroot={soem path}" CGO_ENABLED=1 GOOS="linux" GOARM=7 GOARCH=arm go build --tags=with_libs,arm -o ./out/$app_name .
CC is set to arm-buildroot-linux-gnueabihf-gcc

To add library to project I have arm.go file:

// +build arm

package clips

// #cgo LDFLAGS: -L../../libs/arm -lclips -lm -Wl,-rpath -Wl,../libs/arm
import "C"

What did you expect to see?

I expect to see no unexpected signals.

What did you see instead?

While app is running, when I perform cgo calls from so library I get this (from gdb):
Thread 1 "myapp-arm" received signal SIGSEGV, Segmentation fault.

gdb: backtrace:

    #0  0x76d6fa6c in PrintTemplateFact (theEnv=0x4c8b90, logicalName=0x76d79184 "FactPPForm", theFact=0x4c6dd8, seperateLines=0, ignoreDefaults=0) at /myapp/clips_source/tmpltutl.c:387
    #1  0x76cbfa70 in PrintFact (theEnv=0x4c8b90, logicalName=0x76d79184 "FactPPForm", factPtr=0x4c6dd8, seperateLines=0, ignoreDefaults=0) at /myapp/clips_source/factmngr.c:445
    #2  0x76cbf704 in PrintFactWithIdentifier (theEnv=0x4c8b90, logicalName=0x76d79184 "FactPPForm", factPtr=0x4c6dd8) at /myapp/clips_source/factmngr.c:328
    #3  0x76cc1950 in EnvGetFactPPForm (theEnv=0x4c8b90, buffer=0x535290 "f--1    (attribute (id ) (comment ) (code ) (path", bufferLength=1023, theFact=0x4c6dd8) at /myapp/clips_source/factmngr.c:1590
    #4  0x002661e8 in _cgo_4cadad88d928_Cfunc_EnvGetFactPPForm ()
    #5  0x000823d8 in runtime.asmcgocall () at /usr/local/go/src/runtime/asm_arm.s:596
    #6  0x00a16050 in ?? ()

On Target machine:

gdb disas If this helps
...
   0x76d6fa3c <+616>:	mov	r2, r1
   0x76d6fa40 <+620>:	ldr	r1, [r11, #-60]	; 0xffffffc4
   0x76d6fa44 <+624>:	ldr	r0, [r11, #-56]	; 0xffffffc8
   0x76d6fa48 <+628>:	bl	0x76c68edc 
   0x76d6fa4c <+632>:	b	0x76d6fad0 
   0x76d6fa50 <+636>:	ldr	r3, [r11, #-8]
   0x76d6fa54 <+640>:	lsl	r3, r3, #3
   0x76d6fa58 <+644>:	ldr	r2, [r11, #-24]	; 0xffffffe8
   0x76d6fa5c <+648>:	add	r3, r2, r3
   0x76d6fa60 <+652>:	ldr	r3, [r3, #4]
   0x76d6fa64 <+656>:	str	r3, [r11, #-28]	; 0xffffffe4
   0x76d6fa68 <+660>:	ldr	r3, [r11, #-28]	; 0xffffffe4
=> 0x76d6fa6c <+664>:	ldr	r3, [r3, #4]
   0x76d6fa70 <+668>:	cmp	r3, #0
   0x76d6fa74 <+672>:	ble	0x76d6fad0 
   0x76d6fa78 <+676>:	ldr	r3, [pc, #232]	; 0x76d6fb68 
   0x76d6fa7c <+680>:	add	r3, pc, r3
   0x76d6fa80 <+684>:	mov	r2, r3
   0x76d6fa84 <+688>:	ldr	r1, [r11, #-60]	; 0xffffffc4
   0x76d6fa88 <+692>:	ldr	r0, [r11, #-56]	; 0xffffffc8
   0x76d6fa8c <+696>:	bl	0x76c68c3c 
   0x76d6fa90 <+700>:	ldr	r3, [r11, #-8]
...
@ianlancetaylor
Copy link
Contributor

If it works on amd64 and fails on ARM, then the most likely cause is that the memory address is misaligned. That would not be a problem with Go.

@kiruxan
Copy link
Author

kiruxan commented Sep 20, 2021

@ianlancetaylor you are right, the problem is not related to go/cgo.

@kiruxan kiruxan closed this as completed Sep 20, 2021
@golang golang locked and limited conversation to collaborators Sep 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants