secretflow / psi

The repo of Private Set Intersection(PSI) and Private Information Retrieval(PIR) from SecretFlow.
https://www.secretflow.org.cn/docs/psi
Apache License 2.0
21 stars 16 forks source link

[Bug]: 在linux机器下,将bucket_psi打包成.so库时出错 #138

Closed WandQ closed 1 month ago

WandQ commented 1 month ago

Describe the bug

我希望将bucket_psi打包成动态链接库给go使用,原始的bazel打包参数写为:

psi_cc_library(
    name = "bucket_psi",
    srcs = ["bucket_psi.cc"],
    hdrs = [
        "bucket_psi.h",
    ],
    deps = [
        ":bucket_ub_psi",
        ":memory_psi",
        "//psi:prelude",
        "//psi/proto:psi_cc_proto",
        "//psi/utils:batch_provider",
        "//psi/utils:csv_checker",
        "//psi/utils:csv_header_analyzer",
        "//psi/utils:ec_point_store",
        "@boost//:uuid",
    ],
)

,通过这个打包出来的lib貌似东西不全,打出来的libbucket_psi.so才600kb。于是我将其修改为:

psi_cc_binary(
    name = "bucket_psi",
    srcs = ["bucket_psi.cc","bucket_psi.h"],
    deps = [
        ":bucket_ub_psi",
        ":memory_psi",
        "//psi:prelude",
        "//psi/proto:psi_cc_proto",
        "//psi/utils:batch_provider",
        "//psi/utils:csv_checker",
        "//psi/utils:csv_header_analyzer",
        "//psi/utils:ec_point_store",
        "@boost//:uuid",
    ],linkshared = True,
)

我先在mac上执行bazel build -c opt //psi/legacy:bucket_psi完成了打包,打包出来的libbucket_psi.dylib有45MB,证实可用。 但是我在ubuntu的机器下执行bazel build -c opt //psi/legacy:bucket_psi,打包时报错:

(base) root@f25287b5fd26:/home/admin/dev# bazel build -c opt //psi/legacy:bucket_psi
WARNING: Download from https://golang.org/dl/?mode=json&include=all failed: class java.io.IOException connect timed out
INFO: Analyzed target //psi/legacy:bucket_psi (1 packages loaded, 33 targets configured).
INFO: Found 1 target...
ERROR: /home/admin/dev/psi/legacy/BUILD.bazel:186:14: Linking psi/legacy/libbucket_psi.so failed: (Exit 1): gcc failed: error executing command (from target //psi/legacy:bucket_psi) /usr/bin/gcc @bazel-out/k8-opt/bin/psi/legacy/libbucket_psi.so-2.params

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'scale19'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x12)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'two4x'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x1A)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha22'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x461)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha107'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x47C)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha192'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x497)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha43'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x4B3)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha128'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x4CE)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha213'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x4EA)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha64'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x505)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha149'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x520)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha234'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x53C)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha85'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x558)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha170'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x573)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha255'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x58F)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha22'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x5AD)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha107'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x5C5)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha192'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_square.pic.o:(.text+0x5DD)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'scale19'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_mul.pic.o:(.text+0x12)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha22'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_mul.pic.o:(.text+0x71E)

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'alpha107'; recompile with -fPIC
>>> defined in bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x.pic.o
>>> referenced by bazel-out/k8-opt/bin/external/simplest_ot/_objs/simplest_ot_x86_asm/gfe4x_mul.pic.o:(.text+0x739)
d.lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
collect2: error: ld returned 1 exit status
Target //psi/legacy:bucket_psi failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 30.277s, Critical Path: 29.46s
INFO: 4 processes: 3 internal, 1 processwrapper-sandbox.
FAILED: Build did NOT complete successfully

Steps To Reproduce

psi_cc_library打出来不全,修改为psi_cc_binary后打包报错

Expected behavior

期望linux打包的lib文件和mac打包出来的一样可以使用

Version

0.4.0

Operating system

ubuntu20.04 x86

Hardware Resources

8c32g

lq0404510 commented 1 month ago

hi,关于打包报错,请问ubuntu20.04 x86的gcc版本号是多少,这边建议使用11.4

WandQ commented 1 month ago

hi,关于打包报错,请问ubuntu20.04 x86的gcc版本号是多少,这边建议使用11.4

我使用的gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04),使用官方推荐的镜像secretflow/ubuntu-base-ci:latest同样出现了上述问题。

6fj commented 1 month ago

hi @WandQ

你前面提到编译出来的.so不可用是什么意思,有啥报错呢?

WandQ commented 1 month ago

hi @WandQ

你前面提到编译出来的.so不可用是什么意思,有啥报错呢?

我有一个go项目,配置CGO使用了PSI的.so库进行开发,但是我换一台机器没有编译过psi项目,将其.so拷贝过去,会出现找不到依赖的问题,应该是打出来.so引用了其他.so,也即其中的内容不全。我查看了.so的大小才500K,并且执行grep "KkrtPsiRecv" libbucket_psi.so,验证了其中确实没有KkrtPsiRecv方法。

其实我要解决的问题就是,怎么打出一个完整的.so,让我拷贝到其他机器上也能用。网上提到将cc_library修改cc_binary可以打出完整的库,我在mac测试确实可以,但linux上出现了上面的问题。

WandQ commented 1 month ago

我注意到bazel-out/k8-opt/bin 中有很多打出来的.so。 (base) root@f25287b5fd26:/home/admin/dev/bazel-out/k8-opt/bin/psi/legacy# ls -l | grep ".so$" -r-xr-xr-x. 1 root root 448624 Jun 5 02:52 libbase_operator.so -r-xr-xr-x. 1 root root 421968 Jun 5 02:52 libbc22_2party_psi.so -r-xr-xr-x. 1 root root 646032 Jun 5 03:03 libbucket_psi.so -r-xr-xr-x. 1 root root 634936 Jun 5 02:52 libbucket_ub_psi.so -r-xr-xr-x. 1 root root 456552 Jun 5 02:52 libdp_2party_psi.so -r-xr-xr-x. 1 root root 483880 Jun 5 02:52 libecdh_3party_psi.so -r-xr-xr-x. 1 root root 428744 Jun 5 02:52 libkkrt_2party_psi.so -r-xr-xr-x. 1 root root 514216 Jun 5 02:52 libmemory_psi.so -r-xr-xr-x. 1 root root 490936 Jun 5 02:52 libnparty_psi.so -r-xr-xr-x. 1 root root 476240 Jun 5 02:52 librr22_2party_psi.so

但是都不大,应该是一级一级引用的。单独拿出来某一个根本用不了

WandQ commented 1 month ago

同样的修改后的代码,我在mac能顺利打出来: -r-xr-xr-x 1 admin wheel 45669728 Jun 5 11:33 libbucket_psi.dylib

admin@xx legacy % grep "KkrtPsiRecv" libbucket_psi.dylib Binary file libbucket_psi.dylib matches

我用未修改的原始代码打出来是这样: -r-xr-xr-x 1 admin wheel 603136 May 29 17:59 libbucket_psi.a 不完整。

anakinxc commented 1 month ago

Hi @WandQ

比较建议用 cc_shared_library rule. ld 的问题,试一下全局开 fPIC.

WandQ commented 1 month ago

Hi @WandQ

比较建议用 cc_shared_library rule. ld 的问题,试一下全局开 fPIC.

我将代码恢复并新加了cc_shared_library,修改为:

psi_cc_library(
    name = "bucket_psi",
    srcs = ["bucket_psi.cc"],
    hdrs = [
        "bucket_psi.h",
    ],
    deps = [
        ":bucket_ub_psi",
        ":memory_psi",
        "//psi:prelude",
        "//psi/proto:psi_cc_proto",
        "//psi/utils:batch_provider",
        "//psi/utils:csv_checker",
        "//psi/utils:csv_header_analyzer",
        "//psi/utils:ec_point_store",
        "@boost//:uuid",
    ],
)

cc_shared_library(
    name = "bucket_psi_shared",
    shared_lib_name = "bucket_psi.so",
    deps = [":bucket_psi"],
)

编译命令改为bazel build -c opt --copt '-fPIC' //psi/legacy:bucket_psi_shared 但是上述报错仍然存在,关于-fPIC在代码根目录.bazelrc文件中貌似已经全局给出build --copt=-fPIC。

anakinxc commented 1 month ago

hmmmm....仔细看了一下 log,突然意识到是 simplest-ot 这个东西

这个库的 x64 assembly 的版本并不是 pic 的,所以你如果尝试 export simplest-ot assembly 版本的 symbols 就会出问题。

一个比较简单解决办法就是用 map 来只 export 你需要的 symbol,具体做法可以参考一下(这里)[https://github.com/secretflow/spu/blob/11e7249da927b3952a75b14cc03aa73edd80d12c/spu/BUILD.bazel#L34]

还有就是如果你真要给 go 用,可能需要一个 c interface

WandQ commented 1 month ago

hmmmm....仔细看了一下 log,突然意识到是 simplest-ot 这个东西

这个库的 x64 assembly 的版本并不是 pic 的,所以你如果尝试 export simplest-ot assembly 版本的 symbols 就会出问题。

一个比较简单解决办法就是用 map 来只 export 你需要的 symbol,具体做法可以参考一下(这里)[https://github.com/secretflow/spu/blob/11e7249da927b3952a75b14cc03aa73edd80d12c/spu/BUILD.bazel#L34]

还有就是如果你真要给 go 用,可能需要一个 c interface

我尝试一下,给 go 用的话我会用swig包一层,主要是编译不通过t.t

anakinxc commented 1 month ago

hmmmm....仔细看了一下 log,突然意识到是 simplest-ot 这个东西 这个库的 x64 assembly 的版本并不是 pic 的,所以你如果尝试 export simplest-ot assembly 版本的 symbols 就会出问题。 一个比较简单解决办法就是用 map 来只 export 你需要的 symbol,具体做法可以参考一下(这里)[https://github.com/secretflow/spu/blob/11e7249da927b3952a75b14cc03aa73edd80d12c/spu/BUILD.bazel#L34] 还有就是如果你真要给 go 用,可能需要一个 c interface

我尝试一下,给 go 用的话我会用swig包一层,主要是编译不通过t.t

主要是 c++ 的 export map 不是很好写,或者你可以更直接点尝试把基于汇编的 simplest-ot 给去掉,在这儿

WandQ commented 1 month ago

hmmmm....仔细看了一下 log,突然意识到是 simplest-ot 这个东西 这个库的 x64 assembly 的版本并不是 pic 的,所以你如果尝试 export simplest-ot assembly 版本的 symbols 就会出问题。 一个比较简单解决办法就是用 map 来只 export 你需要的 symbol,具体做法可以参考一下(这里)[https://github.com/secretflow/spu/blob/11e7249da927b3952a75b14cc03aa73edd80d12c/spu/BUILD.bazel#L34] 还有就是如果你真要给 go 用,可能需要一个 c interface

我尝试一下,给 go 用的话我会用swig包一层,主要是编译不通过t.t

主要是 c++ 的 export map 不是很好写,或者你可以更直接点尝试把基于汇编的 simplest-ot 给去掉,在这儿

@anakinxc 谢谢提醒,确实不太好写,去掉比较快.我做了以下尝试,将这儿,修改为:

yacl_cc_library(
    name = "base_ot",
    srcs = ["base_ot.cc"],
    hdrs = ["base_ot.h"],
    deps = [
        "//yacl/base:exception",
        "//yacl/crypto/primitives/ot:ot_store",
        "//yacl/crypto/utils:secparam",
        "//yacl/link",
        "@com_google_absl//absl/types:span",
        ":portable_ot_interface",
    ],
)

并且删除了yacl中的一些判断。 simplest-ot的问题解决了,但是apsi中好像也有类似的,出现了以下报错。

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'ONEx8'; recompile with -fPIC
>>> defined in bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a(consts.c.o)
>>> referenced by fp2_1271_AVX2.S.o:(.text+0x2EA) in archive bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'TWOx8'; recompile with -fPIC
>>> defined in bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a(consts.c.o)
>>> referenced by fp2_1271_AVX2.S.o:(.text+0x2F2) in archive bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'PRIME1271'; recompile with -fPIC
>>> defined in bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a(consts.c.o)
>>> referenced by fp2_1271_AVX2.S.o:(.text+0x5BE) in archive bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a
collect2: error: ld returned 1 exit statu

这个要去掉哪些代码吗?

anakinxc commented 1 month ago

hmmmm....仔细看了一下 log,突然意识到是 simplest-ot 这个东西 这个库的 x64 assembly 的版本并不是 pic 的,所以你如果尝试 export simplest-ot assembly 版本的 symbols 就会出问题。 一个比较简单解决办法就是用 map 来只 export 你需要的 symbol,具体做法可以参考一下(这里)[https://github.com/secretflow/spu/blob/11e7249da927b3952a75b14cc03aa73edd80d12c/spu/BUILD.bazel#L34] 还有就是如果你真要给 go 用,可能需要一个 c interface

我尝试一下,给 go 用的话我会用swig包一层,主要是编译不通过t.t

主要是 c++ 的 export map 不是很好写,或者你可以更直接点尝试把基于汇编的 simplest-ot 给去掉,在这儿

@anakinxc 谢谢提醒,确实不太好写,去掉比较快.我做了以下尝试,将这儿,修改为:

yacl_cc_library(
    name = "base_ot",
    srcs = ["base_ot.cc"],
    hdrs = ["base_ot.h"],
    deps = [
        "//yacl/base:exception",
        "//yacl/crypto/primitives/ot:ot_store",
        "//yacl/crypto/utils:secparam",
        "//yacl/link",
        "@com_google_absl//absl/types:span",
        ":portable_ot_interface",
    ],
)

并且删除了yacl中的一些判断。 simplest-ot的问题解决了,但是apsi中好像也有类似的,出现了以下报错。

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'ONEx8'; recompile with -fPIC
>>> defined in bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a(consts.c.o)
>>> referenced by fp2_1271_AVX2.S.o:(.text+0x2EA) in archive bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'TWOx8'; recompile with -fPIC
>>> defined in bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a(consts.c.o)
>>> referenced by fp2_1271_AVX2.S.o:(.text+0x2F2) in archive bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol 'PRIME1271'; recompile with -fPIC
>>> defined in bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a(consts.c.o)
>>> referenced by fp2_1271_AVX2.S.o:(.text+0x5BE) in archive bazel-out/k8-fastbuild/bin/external/com_github_microsoft_apsi/apsi/lib/libapsi-0.11.a
collect2: error: ld returned 1 exit statu

这个要去掉哪些代码吗?

这里 加一个 "APSI_USE_ASM": "OFF" 看看

WandQ commented 1 month ago

打出来了,大佬牛皮,膜拜了! @anakinxc