emmansun / gmsm

ShangMi (SM) cipher suites for golang (Go语言商用密码软件)

Home Page:https://emmansun.github.io/gmsm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sm4: SSE 代码在某些CPU架构下报SIGSEGV

zhangyongding opened this issue · comments

version:v0.21.3

unexpected fault address 0x0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x824e8f]

goroutine 1 [running]:
runtime.throw({0x116a456?, 0x0?})
runtime/panic.go:1077 +0x5c fp=0xc000574398 sp=0xc000574368 pc=0x44481c
runtime.sigpanic()
runtime/signal_unix.go:875 +0x285 fp=0xc0005743f8 sp=0xc000574398 pc=0x45ac25
github.com/emmansun/gmsm/sm4.gcmSm4Dec(0xc000092598, {0xc0003ae00d, 0x198, 0x6f3}, {0xc0003ae00d, 0x198, 0x6f3}, 0xc000574528, 0xc000574518, {0xc0000e2280, ...})
github.com/emmansun/gmsm@v0.21.3/sm4/gcm_amd64.s:1686 +0x486f fp=0xc000574488 sp=0xc0005743f8 pc=0x824e8f
github.com/emmansun/gmsm/sm4.(*gcmAsm).Open(0xc000092480, {0xc0003ae00d, 0x0, 0x6f3}, {0xc0004b3660?, 0xba437c?, 0xc0004b3660?}, {0xc0003ae00d, 0x1a8, 0x6f3}, ...)
github.com/emmansun/gmsm@v0.21.3/sm4/sm4_gcm_asm.go:148 +0x3cb fp=0xc000574568 sp=0xc000574488 pc=0x7a5deb
gitee.com/Trisia/gotlcp/tlcp.(*prefixNonceAEAD).Open(0xc0004b3660?, {0xc0003ae00d?, 0x0?, 0x6f3?}, {0xc0003ae005?, 0xc000574680?, 0xba923b?}, {0xc0003ae00d, 0x1a8, 0x6f3}, ...)
gitee.com/Trisia/gotlcp@v1.3.13/tlcp/cipher_suites.go:242 +0xf0 fp=0xc0005745e0 sp=0xc000574568 pc=0xba4610
gitee.com/Trisia/gotlcp/tlcp.(*halfConn).decrypt(0xc000446fe8, {0xc0003ae000, 0x1b5, 0x700})
gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:333 +0x563 fp=0xc000574728 sp=0xc0005745e0 pc=0xba5d03
gitee.com/Trisia/gotlcp/tlcp.(*Conn).readRecordOrCCS(0xc000446f00, 0x0)
gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:597 +0xaa6 fp=0xc000574a68 sp=0xc000574728 pc=0xba7806
gitee.com/Trisia/gotlcp/tlcp.(*Conn).readRecord(...)
gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:503
gitee.com/Trisia/gotlcp/tlcp.(*Conn).Read(0xc000446f00, {0xc0003a9000, 0x1000, 0x9dedf8?})
gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:1091 +0x15a fp=0xc000574ad8 sp=0xc000574a68 pc=0xbab87a

@zhangyongding , 你有重现代码、步骤吗?

这个要想一下办法,因为机器在别的地方

是每次都报错,还是在特定情况下报错?

一定报错

uname -a
Linux union-00 5.15.0-86-generic #96~20.04.1-Ubuntu SMP Thu Sep 21 13:23:37 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

cat /etc/os-release

NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

cat /proc/cpuinfo

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 2367.603
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 0
cpu cores       : 6
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 1
cpu cores       : 6
apicid          : 2
initial apicid  : 2
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 2
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 2399.975
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 2
cpu cores       : 6
apicid          : 4
initial apicid  : 4
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 3
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 8
cpu cores       : 6
apicid          : 16
initial apicid  : 16
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 4
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 2371.873
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 9
cpu cores       : 6
apicid          : 18
initial apicid  : 18
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 5
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 10
cpu cores       : 6
apicid          : 20
initial apicid  : 20
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 6
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 0
cpu cores       : 6
apicid          : 1
initial apicid  : 1
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 7
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 1
cpu cores       : 6
apicid          : 3
initial apicid  : 3
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 8
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 2
cpu cores       : 6
apicid          : 5
initial apicid  : 5
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 9
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 8
cpu cores       : 6
apicid          : 17
initial apicid  : 17
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 10
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 1596.000
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 9
cpu cores       : 6
apicid          : 19
initial apicid  : 19
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 11
vendor_id       : GenuineIntel
cpu family      : 6
model           : 44
model name      : Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
stepping        : 2
microcode       : 0x1f
cpu MHz         : 2400.010
cache size      : 12288 KB
physical id     : 0
siblings        : 12
core id         : 10
cpu cores       : 6
apicid          : 21
initial apicid  : 21
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid dtherm arat flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only ept_1gb flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest ple
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit mmio_unknown
bogomips        : 4799.95
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

  1. 可以在上面跑单元测试吗?如果能跑的话,可能是协程安全并发问题。
  2. 以前曾有哪个版本可以跑吗?

sm4的单元测试没有问题

但是sm9的单元测试有问题

gmsm/sm9# go test -v
=== RUN TestSignMasterPrivateKeyMarshalASN1
SIGILL: illegal instruction
PC=0x58b519 m=0 sigcode=2
instruction bytes: 0xc5 0xfe 0x6f 0x6 0xc5 0xfe 0x6f 0x4e 0x20 0xc5 0xfe 0x7f 0x7 0xc5 0xfe 0x7f

goroutine 6 [running]:
github.com/emmansun/gmsm/sm9/bn256.gfp2Copy(0xc000105b38, 0x752020)
/usr/local/src/gmsm/sm9/bn256/select_amd64.s:49 +0x39 fp=0xc000105ac0 sp=0xc000105ab8 pc=0x58b519
github.com/emmansun/gmsm/sm9/bn256.(*gfP2).Set(...)
/usr/local/src/gmsm/sm9/bn256/gfp2.go:29
github.com/emmansun/gmsm/sm9/bn256.(*twistPoint).Set(0xc000105b58?, 0x441231?)
/usr/local/src/gmsm/sm9/bn256/twist.go:46 +0x66 fp=0xc000105b10 sp=0xc000105ac0 pc=0x5824c6
github.com/emmansun/gmsm/sm9/bn256.NewTwistGenerator(...)
/usr/local/src/gmsm/sm9/bn256/twist.go:60
github.com/emmansun/gmsm/sm9/bn256.(*G2).generatorTable.func1()
/usr/local/src/gmsm/sm9/bn256/g2.go:25 +0x85 fp=0xc000105c48 sp=0xc000105b10 pc=0x583625
sync.(*Once).doSlow(0x5d6120?, 0xc000105c01?)
/usr/local/go/src/sync/once.go:74 +0xbf fp=0xc000105ca8 sp=0xc000105c48 pc=0x4701df
sync.(*Once).Do(...)
/usr/local/go/src/sync/once.go:65
github.com/emmansun/gmsm/sm9/bn256.(*G2).generatorTable(0xc000105d68?)
/usr/local/src/gmsm/sm9/bn256/g2.go:23 +0x2c fp=0xc000105cc8 sp=0xc000105ca8 pc=0x574dcc
github.com/emmansun/gmsm/sm9/bn256.(*G2).ScalarBaseMult(0xc00004c058, {0xc00001ec00, 0xf?, 0x6500ea?})
/usr/local/src/gmsm/sm9/bn256/g2.go:99 +0x86 fp=0xc000105e18 sp=0xc000105cc8 pc=0x574e86
github.com/emmansun/gmsm/sm9.GenerateSignMasterKey({0x626d20?, 0xc000068020?})
/usr/local/src/gmsm/sm9/sm9_key.go:66 +0xdb fp=0xc000105e88 sp=0xc000105e18 pc=0x597b7b
github.com/emmansun/gmsm/sm9.TestSignMasterPrivateKeyMarshalASN1(0xc000007a00)
/usr/local/src/gmsm/sm9/sm9_key_test.go:14 +0x38 fp=0xc000105f70 sp=0xc000105e88 pc=0x59b658
testing.tRunner(0xc000007a00, 0x5f7bd0)
/usr/local/go/src/testing/testing.go:1595 +0xff fp=0xc000105fc0 sp=0xc000105f70 pc=0x4bfd7f
testing.(*T).Run.func1()
/usr/local/go/src/testing/testing.go:1648 +0x25 fp=0xc000105fe0 sp=0xc000105fc0 pc=0x4c0d05
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000105fe8 sp=0xc000105fe0 pc=0x46a101
created by testing.(*T).Run in goroutine 1
/usr/local/go/src/testing/testing.go:1648 +0x3ad

goroutine 1 [chan receive]:
runtime.gopark(0xc0001049e8?, 0x40cd05?, 0x50?, 0x1?, 0x18?)
/usr/local/go/src/runtime/proc.go:398 +0xce fp=0xc000104980 sp=0xc000104960 pc=0x43956e
runtime.chanrecv(0xc000022150, 0xc000104a67, 0x1)
/usr/local/go/src/runtime/chan.go:583 +0x3cd fp=0xc0001049f8 sp=0xc000104980 pc=0x40662d
runtime.chanrecv1(0x722980?, 0x5ba0e0?)
/usr/local/go/src/runtime/chan.go:442 +0x12 fp=0xc000104a20 sp=0xc0001049f8 pc=0x406252
testing.(*T).Run(0xc000007860, {0x5f0906?, 0x4bfabc?}, 0x5f7bd0)
/usr/local/go/src/testing/testing.go:1649 +0x3c8 fp=0xc000104ae0 sp=0xc000104a20 pc=0x4c0ba8
testing.runTests.func1(0x7233e0?)
/usr/local/go/src/testing/testing.go:2054 +0x3e fp=0xc000104b30 sp=0xc000104ae0 pc=0x4c2c5e
testing.tRunner(0xc000007860, 0xc000104c48)
/usr/local/go/src/testing/testing.go:1595 +0xff fp=0xc000104b80 sp=0xc000104b30 pc=0x4bfd7f
testing.runTests(0xc00010a0a0?, {0x71ec20, 0x24, 0x24}, {0x41277f?, 0xc000104d08?, 0x722bc0?})
/usr/local/go/src/testing/testing.go:2052 +0x445 fp=0xc000104c78 sp=0xc000104b80 pc=0x4c2b45
testing.(*M).Run(0xc00010a0a0)
/usr/local/go/src/testing/testing.go:1925 +0x636 fp=0xc000104ec0 sp=0xc000104c78 pc=0x4c1536
main.main()
_testmain.go:141 +0x19c fp=0xc000104f40 sp=0xc000104ec0 pc=0x5aafbc
runtime.main()
/usr/local/go/src/runtime/proc.go:267 +0x2bb fp=0xc000104fe0 sp=0xc000104f40 pc=0x4390fb
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000104fe8 sp=0xc000104fe0 pc=0x46a101

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:398 +0xce fp=0xc000048fa8 sp=0xc000048f88 pc=0x43956e
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:404
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:322 +0xb3 fp=0xc000048fe0 sp=0xc000048fa8 pc=0x4393d3
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000048fe8 sp=0xc000048fe0 pc=0x46a101
created by runtime.init.6 in goroutine 1
/usr/local/go/src/runtime/proc.go:310 +0x1a

goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:398 +0xce fp=0xc000049778 sp=0xc000049758 pc=0x43956e
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:404
runtime.bgsweep(0x0?)
/usr/local/go/src/runtime/mgcsweep.go:280 +0x94 fp=0xc0000497c8 sp=0xc000049778 pc=0x423f94
runtime.gcenable.func1()
/usr/local/go/src/runtime/mgc.go:200 +0x25 fp=0xc0000497e0 sp=0xc0000497c8 pc=0x419125
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc0000497e8 sp=0xc0000497e0 pc=0x46a101
created by runtime.gcenable in goroutine 1
/usr/local/go/src/runtime/mgc.go:200 +0x66

goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc000022070?, 0x624858?, 0x1?, 0x0?, 0xc000006d00?)
/usr/local/go/src/runtime/proc.go:398 +0xce fp=0xc000049f70 sp=0xc000049f50 pc=0x43956e
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:404
runtime.(*scavengerState).park(0x722c40)
/usr/local/go/src/runtime/mgcscavenge.go:425 +0x49 fp=0xc000049fa0 sp=0xc000049f70 pc=0x421829
runtime.bgscavenge(0x0?)
/usr/local/go/src/runtime/mgcscavenge.go:653 +0x3c fp=0xc000049fc8 sp=0xc000049fa0 pc=0x421dbc
runtime.gcenable.func2()
/usr/local/go/src/runtime/mgc.go:201 +0x25 fp=0xc000049fe0 sp=0xc000049fc8 pc=0x4190c5
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000049fe8 sp=0xc000049fe0 pc=0x46a101
created by runtime.gcenable in goroutine 1
/usr/local/go/src/runtime/mgc.go:201 +0xa5

goroutine 5 [runnable]:
runtime.runfinq()
/usr/local/go/src/runtime/mfinal.go:176 fp=0xc0000487e0 sp=0xc0000487d8 pc=0x4180a0
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc0000487e8 sp=0xc0000487e0 pc=0x46a101
created by runtime.createfing in goroutine 1
/usr/local/go/src/runtime/mfinal.go:163 +0x3d

rax 0xc000105b38
rbx 0x752020
rcx 0xc000105b78
rdx 0x752060
rdi 0xc000105b38
rsi 0x752020
rbp 0xc000105b00
rsp 0xc000105ab8
r8 0xc000105bf8
r9 0x7520e0
r10 0x137
r11 0x0
r12 0x1
r13 0x0
r14 0xc000007ba0
r15 0x1
rip 0x58b519
rflags 0x10297
cs 0x33
fs 0x0
gs 0x0
exit status 2
FAIL github.com/emmansun/gmsm/sm9 0.008s

sm9的问题,我会fix;如果能通过cipher/gcm_sm4_test.go的测试,应该没问题。

你的意思是说,上层代码调用的问题?

你可以使用go run -race 或者 go build -race 检测一下

使用race检测了一下我的程序,没看出有什么异常的地方,我这是一个命令行程序,理论上没有并发,一样的程序在别的机器上没有问题,就这个机器有问题

用CBC协议有问题吗?

  • 这台机器的go是什么版本?
  • 你用-race编译后运行,或直接go run -race运行,出问题后没有多一点信息?

SIGSEGV: segmentation violation指程序、代码访问非法内存地址,如果只在特定机器、环境有问题,况且cipher/gcm_sm4_test.go的测试也没有问题,这个不好定位问题。

go 版本是1.21.1

如果我在gcm_amd64.s Line 1686 插入一行XORQ ctx, ctx,运行测试可以获得如下错误:

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0xd301b7]

goroutine 6 [running]:
testing.tRunner.func1.2({0xe0c200, 0xf36b90})
	C:/Program Files/Go/src/testing/testing.go:1545 +0x48a
testing.tRunner.func1()
	C:/Program Files/Go/src/testing/testing.go:1548 +0x63f
panic({0xe0c200?, 0xf36b90?})
	C:/Program Files/Go/src/runtime/panic.go:920 +0x290
github.com/emmansun/gmsm/sm4.gcmSm4Dec(0xc000126118, {0xc000128000, 0x198, 0x1a8}, {0xc000128000, 0x198, 0x1a8}, 0xc000125a20, 0xc000125a10, {0xc00001e200, ...})
	d:/git/gsbn/go/src/gmsm/sm4/gcm_amd64.s:1687 +0x4877
github.com/emmansun/gmsm/sm4.(*gcmAsm).Open(0xc000126000, {0xc000128000, 0x0, 0x1a8}, {0xc00000a300, 0xc, 0xc}, {0xc000128000, 0x198, 0x1a8}, ...)
	d:/git/gsbn/go/src/gmsm/sm4/sm4_gcm_asm.go:148 +0x7e5
github.com/emmansun/gmsm/cipher_test.TestSM4GCMRandom(0xc000037520)
	d:/git/gsbn/go/src/gmsm/cipher/gcm_sm4_test.go:413 +0x639
testing.tRunner(0xc000037520, 0xe45f98)
	C:/Program Files/Go/src/testing/testing.go:1595 +0x1c2
created by testing.(*T).Run in goroutine 1
	C:/Program Files/Go/src/testing/testing.go:1648 +0x7e5

虽然不完全一样,不是SIGSEGV,但应该可以证明就是密文切片当时变成非法访问了。
gcm_amd64.s Line 1686 代码是:

PXOR (16*0)(ctx), B0

我尝试写一个最简单的程序来触发这个情况,有没有可能是tlcp库有问题?因为是通过tlcp来调用gmsm的

我尝试写一个最简单的程序来触发这个情况,有没有可能是tlcp库有问题?因为是通过tlcp来调用gmsm的

看代码和跑https://github.com/Trisia/gotlcp/blob/main/example/client/mutual_auth_spec/ecdhe/main.go
https://github.com/Trisia/gotlcp/blob/main/example/server/mutual_auth_spec/main.go 没有问题,当然不是在目标机器。最好能写个重现代码。

version: v0.21.4

作为tlcp的服务端时,0.21.4版本会报错,0.21.3正常,报错和前面的类似

unexpected fault address 0x0
fatal error: fault
unexpected fault address 0x0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x5a998b]

goroutine 100 [running]:
runtime.throw({0x122ec6a?, 0xc000468540?})
        runtime/panic.go:1077 +0x5c fp=0xc000d1b600 sp=0xc000d1b5d0 pc=0x44627c
runtime.sigpanic()
        runtime/signal_unix.go:875 +0x285 fp=0xc000d1b660 sp=0xc000d1b600 pc=0x45d245
github.com/emmansun/gmsm/sm4.encryptBlocksChain(0xc000c7db00, {0xc000d1c015, 0x40, 0x3eb}, {0xc000d1c015, 0x40, 0x3eb}, 0xc000306a30)
        github.com/emmansun/gmsm@v0.21.4/sm4/cbc_amd64.s:36 +0x2b fp=0xc000d1b668 sp=0xc000d1b660 pc=0x5a998b
github.com/emmansun/gmsm/sm4.(*cbc).CryptBlocks(0xc000d1c000?, {0xc000d1c015?, 0x400?, 0xc000306aa0?}, {0xc000d1c015?, 0x10?, 0x0?})
        github.com/emmansun/gmsm@v0.21.4/sm4/cbc_cipher_asm.go:65 +0xd9 fp=0xc000d1b6b8 sp=0xc000d1b668 pc=0x593ef9
gitee.com/Trisia/gotlcp/tlcp.(*halfConn).encrypt(0xc000289678, {0xc000d1c000, 0x5, 0x400}, {0xc000306aa0, 0x10, 0x10}, {0x1901600, 0xc00007ee40})
  [6n   gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:467 +0xab2 fp=0xc000d1b870 sp=0xc000d1b6b8 pc=0xbe3592
gitee.com/Trisia/gotlcp/tlcp.(*Conn).writeRecordLocked(0xc000289500, 0x16, {0xc000306aa0, 0x10, 0x10})
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:904 +0x2f7 fp=0xc000d1b948 sp=0xc000d1b870 pc=0xbe6997
gitee.com/Trisia/gotlcp/tlcp.(*Conn).writeHandshakeRecord(0xc000289500, {0x1909548, 0xc000ca9770}, {0x19018a0, 0xc00022a860})
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:940 +0x150 fp=0xc000d1ba30 sp=0xc000d1b948 pc=0xbe6ef0
gitee.com/Trisia/gotlcp/tlcp.(*serverHandshakeState).sendFinished(0xc00022a820, {0x0, 0x0, 0x11face0?})
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/handshake_server.go:613 +0xca fp=0xc000d1ba80 sp=0xc000d1ba30 pc=0xbf502a
gitee.com/Trisia/gotlcp/tlcp.(*serverHandshakeState).handshake(0xc00022a820)
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/handshake_server.go:109 +0x17c fp=0xc000d1bab8 sp=0xc000d1ba80 pc=0xbf207c
gitee.com/Trisia/gotlcp/tlcp.(*Conn).serverHandshake(0xc000289500, {0x19090e8, 0xc000224370})
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/handshake_server.go:63 +0xdb fp=0xc000d1bae8 sp=0xc000d1bab8 pc=0xbf1ebb
gitee.com/Trisia/gotlcp/tlcp.(*Conn).serverHandshake-fm({0x19090e8?, 0xc000224370?})
        <autogenerated>:1 +0x33 fp=0xc000d1bb10 sp=0xc000d1bae8 pc=0xbfbcb3
gitee.com/Trisia/gotlcp/tlcp.(*Conn).handshakeContext(0xc000289500, {0x1908f60, 0x23546e0})
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:1260 +0x32e fp=0xc000d1bcd0 sp=0xc000d1bb10 pc=0xbe8cae
gitee.com/Trisia/gotlcp/tlcp.(*Conn).HandshakeContext(...)
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:1203
gitee.com/Trisia/gotlcp/tlcp.(*Conn).Handshake(...)
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:1187
gitee.com/Trisia/gotlcp/tlcp.(*Conn).Read(0xc000289500, {0xc0003069c8, 0x8, 0xc000280000?})
        gitee.com/Trisia/gotlcp@v1.3.13/tlcp/conn.go:1078 +0x53 fp=0xc000d1bd40 sp=0xc000d1bcd0 pc=0xbe8193

这两个版本之间就是升级了依赖和fix了bn256的缺失RET bug:

image

sm4/cbc_amd64.s这个文件有变更的,出错就在变更的位置

sm4/cbc_amd64.s这个文件有变更的,出错就在变更的位置

还是那台机器吗?

你用v0.21.5-beta试一下。

不是那台机器

不是那台机器

cpu ?

v0.21.5-beta是可以的,cpu是E52620
前面的问题是不是有可能也是类似这个原因造成的?

v0.21.5-beta是可以的,cpu是E52620 前面的问题是不是有可能也是类似这个原因造成的?

应该是吧

v0.21.5-beta.1

这个版本两台机器测试都正常了