emmansun / gmsm

ShangMi (SM) cipher suites for golang (Go语言商用密码软件)
https://emmansun.github.io/gmsm
MIT License
338 stars 62 forks source link

[plugin] When dynamic linking, R15 is clobbered by a global variable access and is used here: 03578 #154

Closed fzzf678 closed 1 year ago

fzzf678 commented 1 year ago

build plugin 时候遇到这个问题,请教下是什么原因

go version:

root@b158c8f1ffd7:/home/code/tidb# go version
go version go1.20.5 linux/amd64

msg:

# github.com/emmansun/gmsm/sm3

asm: sm3block_amd64.s:1073: when dynamic linking, R15 is clobbered by a global variable access and is used here: 03578 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm3/sm3block_amd64.s:1073)  RORXL   $-7, R14, R15

asm: assembly failed

# github.com/emmansun/gmsm/internal/sm2ec

asm: p256_asm_amd64.s:410: when dynamic linking, R15 is clobbered by a global variable access and is used here: 00271 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/internal/sm2ec/p256_asm_amd64.s:410) MULXQ   16(SI), AX, R15

asm: p256_asm_amd64.s:708: when dynamic linking, R15 is clobbered by a global variable access and is used here: 00590 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/internal/sm2ec/p256_asm_amd64.s:708) MULXQ   (SI), AX, R15

asm: p256_asm_amd64.s:1465: when dynamic linking, R15 is clobbered by a global variable access and is used here: 01205 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/internal/sm2ec/p256_asm_amd64.s:1465)   MULXQ   p256ord<>(SB), AX, R15

asm: p256_asm_amd64.s:1882: when dynamic linking, R15 is clobbered by a global variable access and is used here: 01553 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/internal/sm2ec/p256_asm_amd64.s:1882)   MULXQ   16(SI), AX, R15

asm: p256_asm_amd64.s:2114: when dynamic linking, R15 is clobbered by a global variable access and is used here: 01719 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/internal/sm2ec/p256_asm_amd64.s:2114)   MULQ    R15

asm: p256_asm_amd64.s:2646: when dynamic linking, R15 is clobbered by a global variable access and is used here: 02146 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/internal/sm2ec/p256_asm_amd64.s:2646)   MULXQ   R13, AX, R15

asm: p256_asm_amd64.s:2912: when dynamic linking, R15 is clobbered by a global variable access and is used here: 02270 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/internal/sm2ec/p256_asm_amd64.s:2912)   MOVL    R15, 488(SP)

asm: assembly failed

# github.com/emmansun/gmsm/sm9/bn256

asm: gfp2_g1_amd64.s:218: when dynamic linking, R15 is clobbered by a global variable access and is used here: 00141 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:218)    ADCXQ   R15, R9

asm: gfpMulInternal: invalid instruction: 00256 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:139) MULXQ   (R15), AX

asm: gfpMulInternal: invalid instruction: 00263 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:141) MULXQ   (R15), R14

asm: gfpMulInternal: invalid instruction: 00276 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:144) MULXQ   8(R15), BP

asm: gfpMulInternal: invalid instruction: 00295 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:148) MULXQ   16(R15), R14

asm: gfpMulInternal: invalid instruction: 00314 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:152) MULXQ   24(R15), BX

asm: gfpMulInternal: invalid instruction: 00348 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:160) MULXQ   (R15), AX

asm: gfpMulInternal: invalid instruction: 00355 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:162) MULXQ   (R15), R14

asm: gfpMulInternal: invalid instruction: 00368 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:165) MULXQ   8(R15), BP

asm: gfpMulInternal: invalid instruction: 00387 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:169) MULXQ   16(R15), R14

asm: gfpMulInternal: invalid instruction: 00406 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:173) MULXQ   24(R15), CX

asm: gfpMulInternal: invalid instruction: 00440 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:181) MULXQ   (R15), AX

asm: gfpMulInternal: invalid instruction: 00447 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:183) MULXQ   (R15), R14

asm: gfpMulInternal: invalid instruction: 00460 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:186) MULXQ   8(R15), BP

asm: gfpMulInternal: invalid instruction: 00479 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:190) MULXQ   16(R15), R14

asm: gfpMulInternal: invalid instruction: 00498 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:194) MULXQ   24(R15), R8

asm: gfpMulInternal: invalid instruction: 00532 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:202) MULXQ   (R15), AX

asm: gfpMulInternal: invalid instruction: 00539 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:204) MULXQ   (R15), R14

asm: gfpMulInternal: invalid instruction: 00552 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:207) MULXQ   8(R15), BP

asm: gfpMulInternal: invalid instruction: 00571 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:211) MULXQ   16(R15), R14

asm: gfpMulInternal: invalid instruction: 00590 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:215) MULXQ   24(R15), R9

asm: gfp2_g1_amd64.s:939: when dynamic linking, R15 is clobbered by a global variable access and is used here: 00713 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:939)    CMOVQCS R15, R11

asm: gfpSqrInternal: invalid instruction: 00252 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:599) MULXQ   (R15), AX

asm: gfpSqrInternal: invalid instruction: 00259 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:601) MULXQ   (R15), R10

asm: gfpSqrInternal: invalid instruction: 00272 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:604) MULXQ   8(R15), BP

asm: gfpSqrInternal: invalid instruction: 00291 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:608) MULXQ   16(R15), R10

asm: gfpSqrInternal: invalid instruction: 00310 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:612) MULXQ   24(R15), BX

asm: gfpSqrInternal: invalid instruction: 00344 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:620) MULXQ   (R15), AX

asm: gfpSqrInternal: invalid instruction: 00351 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:622) MULXQ   (R15), R10

asm: gfpSqrInternal: invalid instruction: 00364 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:625) MULXQ   8(R15), BP

asm: gfpSqrInternal: invalid instruction: 00383 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:629) MULXQ   16(R15), R10

asm: gfpSqrInternal: invalid instruction: 00402 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:633) MULXQ   24(R15), CX

asm: gfpSqrInternal: invalid instruction: 00436 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:641) MULXQ   (R15), AX

asm: gfpSqrInternal: invalid instruction: 00443 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:643) MULXQ   (R15), R10

asm: gfpSqrInternal: invalid instruction: 00456 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:646) MULXQ   8(R15), BP

asm: gfpSqrInternal: invalid instruction: 00475 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:650) MULXQ   16(R15), R10

asm: gfpSqrInternal: invalid instruction: 00494 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:654) MULXQ   24(R15), R8

asm: gfpSqrInternal: invalid instruction: 00528 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:662) MULXQ   (R15), AX

asm: gfpSqrInternal: invalid instruction: 00535 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:664) MULXQ   (R15), R10

asm: gfpSqrInternal: invalid instruction: 00548 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:667) MULXQ   8(R15), BP

asm: gfpSqrInternal: invalid instruction: 00567 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:671) MULXQ   16(R15), R10

asm: gfpSqrInternal: invalid instruction: 00586 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:675) MULXQ   24(R15), R9

asm: gfp2_g1_amd64.s:1077: when dynamic linking, R15 is clobbered by a global variable access and is used here: 00819 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:1077)  CMOVQCS R11, R15

asm: gfp2_g1_amd64.s:1162: when dynamic linking, R15 is clobbered by a global variable access and is used here: 00991 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:1162)  CMOVQCS R11, R15

asm: gfp2_g1_amd64.s:1213: when dynamic linking, R15 is clobbered by a global variable access and is used here: 01046 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:1213)  CMOVQCS R11, R15

asm: gfp2_g1_amd64.s:1282: when dynamic linking, R15 is clobbered by a global variable access and is used here: 01202 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:1282)  CMOVQCS R11, R15

asm: gfp2_g1_amd64.s:1344: when dynamic linking, R15 is clobbered by a global variable access and is used here: 01372 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:1344)  CMOVQCS R11, R15

asm: gfp2_g1_amd64.s:1471: when dynamic linking, R15 is clobbered by a global variable access and is used here: 01744 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:1471)  CMOVQCS R11, R15

asm: gfp2_g1_amd64.s:1720: when dynamic linking, R15 is clobbered by a global variable access and is used here: 03280 (/share/.go/pkg/mod/github.com/emmansun/gmsm@v0.20.0/sm9/bn256/gfp2_g1_amd64.s:1720)  CMOVQCS R11, R15

asm: assembly failed

2023/09/05 10:33:26 compile plugin source code failure, exit status 1
emmansun commented 1 year ago

看来插件模式不能用寄存器R15,这个比较麻烦,我先看看。 https://groups.google.com/g/golang-codereviews/c/124cVR_LDTo https://github.com/golang/go/issues/43661

emmansun commented 1 year ago

Hi @fzzf678 , 目前 sm2/sm3/sm9/zuc 的amd64汇编实现,都使用了R15寄存器,由于AMD64寄存器本来就比较少:

x86 has just 8 general-purpose registers available (eax, ebx, ecx, edx, ebp, esp, esi, edi). x64 extended them to 64 bits (prefix "r" instead of "e") and added another 8 (r8, r9, r10, r11, r12, r13, r14, r15). Since some of x86's registers have special implicit meanings and aren't really used as general-purpose (most notably ebp and esp), the effective increase is even larger than it seems.

所以,真要使用插件模式,目前唯有使用purego编译选项(-tags=purego),而且目前只支持整个项目的purego编译选项,不支持分sm2/sm3/sm4/sm9/zuc等的编译选项(这个可以改进)。

fzzf678 commented 1 year ago

@emmansun thks,使用 purego 选项可以编译通过。目前看来是只有这一种解决方法了吧

emmansun commented 1 year ago

@emmansun thks,使用 purego 选项可以编译通过。目前看来是只有这一种解决方法了吧

是的,只是性能就也是purego的性能了,目前可以改进的是,增加一些算法级别的编译选项(sm2purego/sm3purego/sm9purego/zucpurego,或者定义一个plugin编译选项),这样插件模式也能使用amd64汇编实现的sm4等。如果不能接受这种编译选项下的性能,建议使用非插件模式。

fzzf678 commented 1 year ago

@emmansun 了解了

emmansun commented 1 year ago

为amd64 plugin编译提供汇编支持

emmansun commented 1 year ago

v0.20.2