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

CI: cross-compile: riscv: enable more tests on extensions #24403

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

ZenithalHourlyRate
Copy link
Contributor

@ZenithalHourlyRate ZenithalHourlyRate commented May 15, 2024

Github Actions recently supported Ubuntu 24.04, which ships with QEMU 8.2.2 and all currently OpenSSL-supported extensions are there.

This PR picks up an old attempt to add CI on Zb/Zk and will try to add other CIs covering other extensions like #20149, #24069 (enabled by OPENSSL_riscvcap) and #16710 (enabled by CFLAGS)

Cc @cmuellner

Though fips: no might be needed for those CI as they failed to pass the test on installing fips module on my machine. I do not fully understand the relationship between fips support and RISC-V extension support, please point out if fips support is a requirement when some extension is enabled and these should be addressed in other PRs

Checklist
  • tests are added or updated

@@ -150,7 +167,7 @@ jobs:
tests: none
}
]
runs-on: ${{ github.server_url == 'https://github.com' && 'ubuntu-latest' || 'ubuntu-22.04-self-hosted' }}
runs-on: ${{ github.server_url == 'https://github.com' && 'ubuntu-24.04' || 'ubuntu-22.04-self-hosted' }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ubuntu-latest not pointing to ubuntu-24.04?

Also, the fallback to Ubuntu 22.04 is pointless because it will always trigger a failing build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ubuntu-latest not pointing to ubuntu-24.04?

The README of https://github.com/actions/runner-images shows ubuntu-latest points to ubuntu-22.04 and ubuntu-24.04 is marked as "beta"

Also, the fallback to Ubuntu 22.04 is pointless because it will always trigger a failing build.

Do not know the details of these self hosted CI. Do not know if it is safe to remove them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. it would be bad to remove them. But you should skip the build for these two newly added targets if running on these self-hosted runners.

@cmuellner
Copy link
Contributor

Github Actions recently supported Ubuntu 22.04

You probably mean Ubuntu 24.04.

In general, I think it would be great if we could finally have proper CI pipelines for RISC-V.

What I am missing is covering all relevant combinations. For example, we have different Zvkg-optimizations with (gcm_init_rv64i_zvkg_zvkb) and without Zvkb (gcm_init_rv64i_zvkg) enabled. We don't need to run all possible combinations of extensions, but we could iterate over all combinations that trigger different code paths. A list could be part of the cross-compiles.yml.

@t8m
Copy link
Member

t8m commented May 15, 2024

Could the fips issue be another instance of #23979 ? We really need to solve it.

@t8m t8m added branch: master Merge to master branch triaged: feature The issue/pr requests/adds a feature tests: present The PR has suitable tests present labels May 15, 2024
@ZenithalHourlyRate ZenithalHourlyRate changed the title CI: cross-compile: riscv: enable tests on extensions due to CI: cross-compile: riscv: enable more tests on extensions May 15, 2024
@ZenithalHourlyRate
Copy link
Contributor Author

https://github.com/openssl/openssl/actions/runs/9097624311?pr=24403

Interesting results...

First #23973 does not fix fips install, check https://github.com/openssl/openssl/actions/runs/9097624311/job/25005869697?pr=24403

Second some cross-compile would not dump info -cpusettings, might be binfmt issues. Would try to find a way to correctly dump cpusettings from command line, removed commit ZenithalHourlyRate@ea3e431 for now.

Third hwprobe interface for RISC-V exists and qemu-riscv64 implements this syscall, the default case is no longer RV64GC and riscvcap should be set to empty now. Check https://github.com/openssl/openssl/actions/runs/9097624311/job/25005868354?pr=24403

Run QEMU_LD_PREFIX=/usr/riscv64-linux-gnu util/wrap.pl apps/openssl info -cpusettings
  QEMU_LD_PREFIX=/usr/riscv64-linux-gnu util/wrap.pl apps/openssl info -cpusettings
  shell: /usr/bin/bash -e {0}
OPENSSL_riscvcap=ZBA_ZBB_ZBS_V

@ZenithalHourlyRate
Copy link
Contributor Author

Second some cross-compile would not dump info -cpusettings, might be binfmt issues. Would try to find a way to correctly dump cpusettings from command line, removed commit ZenithalHourlyRate@ea3e431 for now.

Turned out those are tests: no cases and qemu-user is not installed. Added back the dump

https://github.com/openssl/openssl/actions/runs/9098142158/job/25007670871?pr=24403

Now for RV64GC case, fips install would fail as OPENSSL_riscvcap is now manually set; set fips: no for other cases would work.

@ZenithalHourlyRate
Copy link
Contributor Author

ZenithalHourlyRate commented May 15, 2024

image

Reproduced

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000003ff76bbf70 in BIO_snprintf (buf=0x3fffffe8a8 "", n=256, format=0x3ff77c7b98 "_%s") at providers/fips/fipsprov.c:938
#2  0x0000003ff7758e7c in parse_env (envstr=0x3fffffff0b "rv64gc") at crypto/riscvcap.c:71
#3  0x0000003ff7758f9c in OPENSSL_cpuid_setup () at crypto/riscvcap.c:130
#4  0x0000003ff7fdfe8e in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#5  0x0000003ff7fdff52 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#6  0x0000003ff7fdd16e in _dl_catch_exception () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#7  0x0000003ff7fe4b40 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#8  0x0000003ff7fdd118 in _dl_catch_exception () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#9  0x0000003ff7fe4e3c in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#10 0x0000003ff7885d52 in ?? () from /usr/lib/libc.so.6
#11 0x0000003ff7fdd118 in _dl_catch_exception () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#12 0x0000003ff7fdd1e2 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#13 0x0000003ff78859c0 in ?? () from /usr/lib/libc.so.6
#14 0x0000003ff7885dde in dlopen () from /usr/lib/libc.so.6
#15 0x0000003ff7b0e4f0 in dlfcn_load (dso=0x144f80) at crypto/dso/dso_dlfcn.c:116
#16 0x0000003ff7b0f32e in DSO_load (dso=0x144f80, filename=0x147120 "./fips.so", meth=0x0, flags=0) at crypto/dso/dso_lib.c:146
#17 0x0000003ff7bc412c in provider_init (prov=0x146b60) at crypto/provider_core.c:937
#18 0x0000003ff7bc475e in provider_activate (prov=0x146b60, lock=0, upcalls=1) at crypto/provider_core.c:1176
#19 0x0000003ff7bc4b0c in ossl_provider_activate (prov=0x146b60, upcalls=1, aschild=0) at crypto/provider_core.c:1313
#20 0x0000003ff7bc1e76 in provider_conf_activate (libctx=0x0, name=0x145bc0 "fips", value=0x145c10 "fips_sect", path=0x0, soft=0, cnf=0x1454e0) at crypto/provider_conf.c:243
#21 0x0000003ff7bc2326 in provider_conf_load (libctx=0x0, name=0x145bc0 "fips", value=0x145c10 "fips_sect", cnf=0x1454e0) at crypto/provider_conf.c:362
#22 0x0000003ff7bc24de in provider_conf_init (md=0x146720, cnf=0x1454e0) at crypto/provider_conf.c:418
#23 0x0000003ff7ae8e72 in module_init (pmod=0x146910, name=0x145a80 "providers", value=0x145ad0 "provider_section", cnf=0x1454e0) at crypto/conf/conf_mod.c:453
#24 0x0000003ff7ae8950 in module_run (cnf=0x1454e0, name=0x145a80 "providers", value=0x145ad0 "provider_section", flags=0) at crypto/conf/conf_mod.c:284
#25 0x0000003ff7ae867e in CONF_modules_load (cnf=0x1454e0, appname=0x0, flags=0) at crypto/conf/conf_mod.c:176
#26 0x000000000005b9ee in generate_config_and_load (prov_name=0x3ffffffce7 "fips", section=0x3ffffffcfa "fips_sect", 
    module_mac=0x3ffffff638 "1\247\032\221\033\357\215\001\375k\362X\364\355\314Q\316A\216G\270\003\237\306\360JMn͜\035m\300\367\022", module_mac_len=32, 
    opts=0x10b4b8 <fips_opts>) at apps/fipsinstall.c:270
#27 0x000000000005c3f2 in fipsinstall_main (argc=10, argv=0x3ffffff980) at apps/fipsinstall.c:580
#28 0x000000000006715c in do_cmd (prog=0x12f520, argc=10, argv=0x3ffffff980) at apps/openssl.c:426
#29 0x0000000000066d44 in main (argc=10, argv=0x3ffffff980) at apps/openssl.c:307
(gdb) 

(gdb) b parse_env
(gdb) bt
#0  parse_env (envstr=0x3fffffff0b "rv64gc") at crypto/riscvcap.c:61
#1  0x0000003ff7bc7482 in OPENSSL_cpuid_setup () at crypto/riscvcap.c:130
#2  0x0000003ff7fdfe8e in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#3  0x0000003ff7fdff52 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#4  0x0000003ff7fec5be in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
(gdb) c
Continuing.

Breakpoint 1.1, parse_env (envstr=0x3fffffff0b "rv64gc") at crypto/riscvcap.c:61
warning: 61     crypto/riscvcap.c: No such file or directory
(gdb) bt
#0  parse_env (envstr=0x3fffffff0b "rv64gc") at crypto/riscvcap.c:61
#1  0x0000003ff7758f9c in OPENSSL_cpuid_setup () at crypto/riscvcap.c:130
#2  0x0000003ff7fdfe8e in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#3  0x0000003ff7fdff52 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#4  0x0000003ff7fdd16e in _dl_catch_exception () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#5  0x0000003ff7fe4b40 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#6  0x0000003ff7fdd118 in _dl_catch_exception () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#7  0x0000003ff7fe4e3c in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#8  0x0000003ff7885d52 in ?? () from /usr/lib/libc.so.6
#9  0x0000003ff7fdd118 in _dl_catch_exception () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#10 0x0000003ff7fdd1e2 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#11 0x0000003ff78859c0 in ?? () from /usr/lib/libc.so.6
#12 0x0000003ff7885dde in dlopen () from /usr/lib/libc.so.6
#13 0x0000003ff7b0e4f0 in dlfcn_load (dso=0x144f80) at crypto/dso/dso_dlfcn.c:116
#14 0x0000003ff7b0f32e in DSO_load (dso=0x144f80, filename=0x147120 "./fips.so", meth=0x0, flags=0) at crypto/dso/dso_lib.c:146
#15 0x0000003ff7bc412c in provider_init (prov=0x146b60) at crypto/provider_core.c:937
#16 0x0000003ff7bc475e in provider_activate (prov=0x146b60, lock=0, upcalls=1) at crypto/provider_core.c:1176
#17 0x0000003ff7bc4b0c in ossl_provider_activate (prov=0x146b60, upcalls=1, aschild=0) at crypto/provider_core.c:1313
#18 0x0000003ff7bc1e76 in provider_conf_activate (libctx=0x0, name=0x145bc0 "fips", value=0x145c10 "fips_sect", path=0x0, soft=0, cnf=0x1454e0) at crypto/provider_conf.c:243
#19 0x0000003ff7bc2326 in provider_conf_load (libctx=0x0, name=0x145bc0 "fips", value=0x145c10 "fips_sect", cnf=0x1454e0) at crypto/provider_conf.c:362
#20 0x0000003ff7bc24de in provider_conf_init (md=0x146720, cnf=0x1454e0) at crypto/provider_conf.c:418
#21 0x0000003ff7ae8e72 in module_init (pmod=0x146910, name=0x145a80 "providers", value=0x145ad0 "provider_section", cnf=0x1454e0) at crypto/conf/conf_mod.c:453
#22 0x0000003ff7ae8950 in module_run (cnf=0x1454e0, name=0x145a80 "providers", value=0x145ad0 "provider_section", flags=0) at crypto/conf/conf_mod.c:284
#23 0x0000003ff7ae867e in CONF_modules_load (cnf=0x1454e0, appname=0x0, flags=0) at crypto/conf/conf_mod.c:176
#24 0x000000000005b9ee in generate_config_and_load (prov_name=0x3ffffffce7 "fips", section=0x3ffffffcfa "fips_sect", 
    module_mac=0x3ffffff638 "1\247\032\221\033\357\215\001\375k\362X\364\355\314Q\316A\216G\270\003\237\306\360JMn͜\035m\300\367\022", module_mac_len=32, 
    opts=0x10b4b8 <fips_opts>) at apps/fipsinstall.c:270
#25 0x000000000005c3f2 in fipsinstall_main (argc=10, argv=0x3ffffff980) at apps/fipsinstall.c:580
#26 0x000000000006715c in do_cmd (prog=0x12f520, argc=10, argv=0x3ffffff980) at apps/openssl.c:426
#27 0x0000000000066d44 in main (argc=10, argv=0x3ffffff980) at apps/openssl.c:307
(gdb) p RISCV_capabilities
$2 = {{name = 0x3ff77c7ad0 "ZBA", index = 0, bit_offset = 0}, {name = 0x3ff77c7ad8 "ZBB", index = 0, bit_offset = 1}, {name = 0x3ff77c7ae0 "ZBC", index = 0, bit_offset = 2}, {
    name = 0x3ff77c7ae8 "ZBS", index = 0, bit_offset = 3}, {name = 0x3ff77c7af0 "ZBKB", index = 0, bit_offset = 4}, {name = 0x3ff77c7af8 "ZBKC", index = 0, bit_offset = 5}, {
    name = 0x3ff77c7b00 "ZBKX", index = 0, bit_offset = 6}, {name = 0x3ff77c7b08 "ZKND", index = 0, bit_offset = 7}, {name = 0x3ff77c7b10 "ZKNE", index = 0, bit_offset = 8}, {
    name = 0x3ff77c7b18 "ZKNH", index = 0, bit_offset = 9}, {name = 0x3ff77c7b20 "ZKSED", index = 0, bit_offset = 10}, {name = 0x3ff77c7b28 "ZKSH", index = 0, bit_offset = 11}, {
    name = 0x3ff77c7b30 "ZKR", index = 0, bit_offset = 12}, {name = 0x3ff77c7b38 "ZKT", index = 0, bit_offset = 13}, {name = 0x3ff77c7b40 "V", index = 0, bit_offset = 14}, {
    name = 0x3ff77c7b48 "ZVBB", index = 0, bit_offset = 15}, {name = 0x3ff77c7b50 "ZVBC", index = 0, bit_offset = 16}, {name = 0x3ff77c7b58 "ZVKB", index = 0, bit_offset = 17}, {
    name = 0x3ff77c7b60 "ZVKG", index = 0, bit_offset = 18}, {name = 0x3ff77c7b68 "ZVKNED", index = 0, bit_offset = 19}, {name = 0x3ff77c7b70 "ZVKNHA", index = 0, 
    bit_offset = 20}, {name = 0x3ff77c7b78 "ZVKNHB", index = 0, bit_offset = 21}, {name = 0x3ff77c7b80 "ZVKSED", index = 0, bit_offset = 22}, {name = 0x3ff77c7b88 "ZVKSH", 
    index = 0, bit_offset = 23}}
(gdb) info symbol RISCV_capabilities
RISCV_capabilities in section .data.rel.ro of ./fips.so

Seems like OPENSSL_cpuid_setup is called twice and the second call from fips caused the problem. There are two definitions of RISCV_capabilities (which is the argument of BIO_snprintf), and THEY ARE DIFFERENT, see the backtrace of the first init

Breakpoint 1, parse_env (envstr=0x3fffffff0b "rv64gc") at crypto/riscvcap.c:61
warning: 61     crypto/riscvcap.c: No such file or directory
(gdb) p RISCV_capabilities
$1 = {{name = 0x3ff7d4df90 "ZBA", index = 0, bit_offset = 0, hwprobe_key = 4, hwprobe_value = 8}, {name = 0x3ff7d4df98 "ZBB", index = 0, bit_offset = 1, hwprobe_key = 4, 
    hwprobe_value = 16}, {name = 0x3ff7d4dfa0 "ZBC", index = 0, bit_offset = 2, hwprobe_key = 4, hwprobe_value = 128}, {name = 0x3ff7d4dfa8 "ZBS", index = 0, bit_offset = 3, 
    hwprobe_key = 4, hwprobe_value = 32}, {name = 0x3ff7d4dfb0 "ZBKB", index = 0, bit_offset = 4, hwprobe_key = 4, hwprobe_value = 256}, {name = 0x3ff7d4dfb8 "ZBKC", index = 0, 
    bit_offset = 5, hwprobe_key = 4, hwprobe_value = 512}, {name = 0x3ff7d4dfc0 "ZBKX", index = 0, bit_offset = 6, hwprobe_key = 4, hwprobe_value = 1024}, {
    name = 0x3ff7d4dfc8 "ZKND", index = 0, bit_offset = 7, hwprobe_key = 4, hwprobe_value = 2048}, {name = 0x3ff7d4dfd0 "ZKNE", index = 0, bit_offset = 8, hwprobe_key = 4, 
    hwprobe_value = 4096}, {name = 0x3ff7d4dfd8 "ZKNH", index = 0, bit_offset = 9, hwprobe_key = 4, hwprobe_value = 8192}, {name = 0x3ff7d4dfe0 "ZKSED", index = 0, 
    bit_offset = 10, hwprobe_key = 4, hwprobe_value = 16384}, {name = 0x3ff7d4dfe8 "ZKSH", index = 0, bit_offset = 11, hwprobe_key = 4, hwprobe_value = 32768}, {
    name = 0x3ff7d4dff0 "ZKR", index = 0, bit_offset = 12, hwprobe_key = -1, hwprobe_value = 0}, {name = 0x3ff7d4dff8 "ZKT", index = 0, bit_offset = 13, hwprobe_key = 4, 
    hwprobe_value = 65536}, {name = 0x3ff7d4e000 "V", index = 0, bit_offset = 14, hwprobe_key = 4, hwprobe_value = 4}, {name = 0x3ff7d4e008 "ZVBB", index = 0, bit_offset = 15, 
    hwprobe_key = 4, hwprobe_value = 131072}, {name = 0x3ff7d4e010 "ZVBC", index = 0, bit_offset = 16, hwprobe_key = 4, hwprobe_value = 262144}, {name = 0x3ff7d4e018 "ZVKB", 
    index = 0, bit_offset = 17, hwprobe_key = 4, hwprobe_value = 524288}, {name = 0x3ff7d4e020 "ZVKG", index = 0, bit_offset = 18, hwprobe_key = 4, hwprobe_value = 1048576}, {
    name = 0x3ff7d4e028 "ZVKNED", index = 0, bit_offset = 19, hwprobe_key = 4, hwprobe_value = 2097152}, {name = 0x3ff7d4e030 "ZVKNHA", index = 0, bit_offset = 20, 
    hwprobe_key = 4, hwprobe_value = 4194304}, {name = 0x3ff7d4e038 "ZVKNHB", index = 0, bit_offset = 21, hwprobe_key = 4, hwprobe_value = 8388608}, {
    name = 0x3ff7d4e040 "ZVKSED", index = 0, bit_offset = 22, hwprobe_key = 4, hwprobe_value = 16777216}, {name = 0x3ff7d4e048 "ZVKSH", index = 0, bit_offset = 23, 
    hwprobe_key = 4, hwprobe_value = 33554432}}
(gdb) info symbol RISCV_capabilities
RISCV_capabilities in section .data.rel.ro of /home/zenithal/libcrypto.so.3
(gdb) bt
#0  parse_env (envstr=0x3fffffff0b "rv64gc") at crypto/riscvcap.c:61
#1  0x0000003ff7bc7482 in OPENSSL_cpuid_setup () at crypto/riscvcap.c:130
#2  0x0000003ff7fdfe8e in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#3  0x0000003ff7fdff52 in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
#4  0x0000003ff7fec5be in ?? () from /usr/lib/ld-linux-riscv64-lp64d.so.1
(gdb) 

# if defined(OPENSSL_SYS_LINUX) && !defined(FIPS_MODULE)
# if __has_include(<asm/hwprobe.h>)
# define OSSL_RISCV_HWPROBE
# endif
# endif

struct RISCV_capability_s {
const char *name;
size_t index;
size_t bit_offset;
# ifdef OSSL_RISCV_HWPROBE
int32_t hwprobe_key;
uint64_t hwprobe_value;
# endif
};

The !FIPS_MODULE is the root case. I (#24172) mimiced this #ifdef from

#if defined(OPENSSL_SYS_LINUX) && !defined(FIPS_MODULE)
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <asm/zcrypt.h>
# include <sys/ioctl.h>
# include <unistd.h>
#endif

And assumed fips module should not make syscall. The different struct definition is a side effect.

For #23979, I assume there are two definition of OPENSSL_ppccap_P (just check readelf of both libcrypto.so and fips.so) and that might be the problem (? but OPENSSL_cpuid_setup is indeed called twice)

@ZenithalHourlyRate
Copy link
Contributor Author

cba7e7d

Now with the same structure it still fails. After debugging I found it is not accessing outside of RISCV_capabilities as changing that line to BIO_snprintf(buf, BUFLEN, "_%s", "a"); still caused segfault. Might the cause be the different implementation of BIO_snprintf in libcrypto/fips.so?

@ZenithalHourlyRate
Copy link
Contributor Author

Might the cause be the different implementation of BIO_snprintf in libcrypto/fips.so?

int BIO_snprintf(char *buf, size_t n, const char *format, ...)
{
va_list args;
int ret;
va_start(args, format);
ret = c_BIO_vsnprintf(buf, n, format, args);
va_end(args);
return ret;
}

c_BIO_vsnprintf is not initialized (points to NULL) as OSSL_provider_init_int for fips provider has not been called when fips.so is loaded

@ZenithalHourlyRate
Copy link
Contributor Author

ZenithalHourlyRate commented May 16, 2024

In riscvcap.c, OPENSSL_cpuid_setup is __attribute__((constructor)) so it is in the .init_array of fips.so (also in libcrypto.so), where everything is not ready. After removing it (as crypto/init.c also calls cpuid_setup), fipsinstall works, but the OPENSSL_riscvcap_P inside fips.so is now uninitialized.

$ gdb --args ../../openssl fipsinstall -pedantic -module ../../fips.so -provider_name fips -section_name fips_sect -out ../../fipsmodule.cnf
(gdb) b parse_env
(gdb) bt
#0  parse_env (envstr=0x3ffffffef5 "rv64gc") at crypto/riscvcap.c:61
#1  0x0000003ff7bc7462 in OPENSSL_cpuid_setup () at crypto/riscvcap.c:127
#2  0x0000003ff7baf3d2 in ossl_init_base () at crypto/init.c:68
#3  0x0000003ff7baf378 in ossl_init_base_ossl_ () at crypto/init.c:55
#4  0x0000003ff788cd7a in ?? () from /usr/lib/libc.so.6
#5  0x0000003ff7bc8c1c in CRYPTO_THREAD_run_once (once=0x3ff7e927a4 <base>, init=0x3ff7baf36c <ossl_init_base_ossl_>) at crypto/threads_pthread.c:802
#6  0x0000003ff7bafa78 in OPENSSL_init_crypto (opts=262144, settings=0x0) at crypto/init.c:520
#7  0x0000003ff7c0105e in ossl_rand_ctx_new (libctx=0x3ff7e91c10 <default_context_int>) at crypto/rand/rand_lib.c:465
#8  0x0000003ff7baa1c2 in context_init (ctx=0x3ff7e91c10 <default_context_int>) at crypto/context.c:115
#9  0x0000003ff7baa708 in default_context_do_init () at crypto/context.c:381
#10 0x0000003ff7baa6cc in default_context_do_init_ossl_ () at crypto/context.c:376
#11 0x0000003ff788cd7a in ?? () from /usr/lib/libc.so.6
#12 0x0000003ff7bc8c1c in CRYPTO_THREAD_run_once (once=0x3ff7e91d58 <default_context_init>, init=0x3ff7baa6c0 <default_context_do_init_ossl_>) at crypto/threads_pthread.c:802
#13 0x0000003ff7baa79a in get_thread_default_context () at crypto/context.c:404
#14 0x0000003ff7baa7d2 in get_default_context () at crypto/context.c:412
#15 0x0000003ff7baaa92 in ossl_lib_ctx_get_concrete (ctx=0x0) at crypto/context.c:528
#16 0x0000003ff7baac9e in ossl_lib_ctx_get_ex_data_global (ctx=0x0) at crypto/context.c:644
#17 0x0000003ff7bae46a in ossl_crypto_new_ex_data_ex (ctx=0x0, class_index=12, obj=0x11b2a0, ad=0x11b310) at crypto/ex_data.c:227
#18 0x0000003ff7bae678 in CRYPTO_new_ex_data (class_index=12, obj=0x11b2a0, ad=0x11b310) at crypto/ex_data.c:266
#19 0x0000003ff7a7e13e in BIO_new_ex (libctx=0x0, method=0x3ff7e71e58 <methods_filep>) at crypto/bio/bio_lib.c:95
#20 0x0000003ff7a7e206 in BIO_new (method=0x3ff7e71e58 <methods_filep>) at crypto/bio/bio_lib.c:116
#21 0x0000003ff7a8d43c in BIO_new_fp (stream=0x3ff796f770 <_IO_2_1_stdin_>, close_flag=16) at crypto/bio/bss_file.c:95
#22 0x00000000000a8c8c in dup_bio_in (format=32769) at apps/lib/apps.c:2993
#23 0x0000000000066a90 in main (argc=11, argv=0x3ffffff948) at apps/openssl.c:250
(gdb) 
$ gdb --args ./openssl fipsinstall -pedantic -module ./fips.so -provider_name fips -section_name fips_sect -out ./fipsmodule.cnf
(gdb) b OPENSSL_cpuid_setup
(gdb) b OSSL_provider_init_int

...After the first cpuid_setup inside libcrypto

(gdb) p OPENSSL_riscvcap_P 
$1 = {16386, 0, 0, 0, 0, 0}
(gdb) info symbol OPENSSL_riscvcap_P 
OPENSSL_riscvcap_P in section .bss of /home/zenithal/libcrypto.so.3

...in fips.so init_int

(gdb) p OPENSSL_riscvcap_P
$2 = {0, 0, 0, 0, 0, 0}
(gdb) info symbol OPENSSL_riscvcap_P 
OPENSSL_riscvcap_P in section .bss of ./fips.so

...And there are no more cpuid_setup called for fips.so

In my opinion, attribute constructor should be removed, and cpuid_setup should be called explicitly inside fips init (as libcrypto has done), instead of the implicit .init_array. Like #23978 (reply in thread) pointed out, this magic is confusing.

@t8m
Copy link
Member

t8m commented May 16, 2024

There has to be a separate OPENSSL_riscvcap_P in the FIPS provider as the FIPS provider is a completely separate module not sharing any data with libcrypto. But I assume this is obvious to you.

And yes, given the providers have an explicit init function, the cpuid setup routines should be called from the FIPS provider's init function after the core callbacks are set but before the self tests are run.

@ZenithalHourlyRate
Copy link
Contributor Author

ZenithalHourlyRate commented May 16, 2024

This PR is now self-contained enough and ready for review. The fips init issue is addressed in another PR #24419 and it is does not block this PR.

https://github.com/openssl/openssl/actions/runs/9110210246/job/25044761009?pr=24403

Interestingly, two alternative code path for crypto/modes/gcm128.c failed, they are

else
ctx->ginit = gcm_init_rv64i_zvkg;

} else if (RISCV_HAS_ZVKB() && RISCV_HAS_ZVBC() && riscv_vlen() >= 128) {
ctx->ginit = gcm_init_rv64i_zvkb_zvbc;
ctx->gmult = gcm_gmult_rv64i_zvkb_zvbc;
ctx->ghash = gcm_ghash_rv64i_zvkb_zvbc;

I recently do not get much spare for debugging into assemblies so might the original authors take care of these paths?

There are several ways to address this (as we want to see CI green on master branch otherwise it is annoying):

  1. Comment out these paths for now to let the CI pass and wait for another fixing PR
  2. Do not enable CI on these paths for now and wait for fix
  3. Wait for fix then add CI

I prefer the first way. If you accept I'll add a commit commenting out them.

I'll also add a tmp commit now for make all tests to find possibly more problems.

--Update1

https://github.com/openssl/openssl/actions/runs/9111809271/job/25049744210

Done. Seems like the gcm problem. The exit code is 132 which is illegal instruction

@paulidale
Copy link
Contributor

paulidale commented May 16, 2024

And yes, given the providers have an explicit init function, the cpuid setup routines should be called from the FIPS provider's init function after the core callbacks are set but before the self tests are run.

Ignore this, it was incorrect.

This isn't right. The self tests run on module load via the DEP, not via the init function libcrypto calls. The cpuid needs to be setup before the self tests are run or they'll test the wrong code. The DEP needs to do this.

@t8m
Copy link
Member

t8m commented May 16, 2024

And yes, given the providers have an explicit init function, the cpuid setup routines should be called from the FIPS provider's init function after the core callbacks are set but before the self tests are run.

This isn't right. The self tests run on module load via the DEP, not via the init function libcrypto calls. The cpuid needs to be setup before the self tests are run or they'll test the wrong code. The DEP needs to do this.

@paulidale no, this is not true. Please look at the current DEP code in the FIPS provider. The DEP just indicates the state - i.e. that the selftest must be run before the module is being operational. But they are actually run from the provider init function.

@ZenithalHourlyRate
Copy link
Contributor Author

Interestingly, two alternative code path for crypto/modes/gcm128.c failed, they are

They are related to a QEMU bug where the zvkb flag does not control itself but ext_zvkg.

Check https://lists.nongnu.org/archive/html/qemu-devel/2024-05/msg02231.html

I'll ping upstream to backport this fix

Now all code path (that I found, that controlled by riscvcap) for riscv64 pass the CI.

The lefting paths are those enabled by -march CFLAGS; would be done in another PR.

__attribute__((constructor)) makes cpuid_setup called
when loading dynamic libraries like libcrypto.so and fips.so,
typically in .init_array. However, this is not wanted as some
required function, like BIO_snprintf is not ready.

Instead, cpuid_setup should be explicitly called inside
the init function of these libraries.
Otherwise riscv hwprobe interface would set cap
Most paths are covered by these two cases
1. bitmanip and scalar crypto extension
2. vector bitmanip/crypto extension
@ZenithalHourlyRate
Copy link
Contributor Author

Ping for review

@t8m t8m added approval: review pending This pull request needs review by a committer approval: otc review pending This pull request needs review by an OTC member labels May 24, 2024
Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is scalable. This adds 9 new CI jobs on every pull request and that is something we probably want to avoid.

At least all the jobs with the same target could be somehow iteratively run on the same build.

@cmuellner
Copy link
Contributor

I am not sure this is scalable. This adds 9 new CI jobs on every pull request and that is something we probably want to avoid.

At least all the jobs with the same target could be somehow iteratively run on the same build.

I fully agree. We don't need to build all relevant combinations (given all optimizations are based on run-time properties, a single build might be enough). But all distinct code paths should be run. So we also don't need to run all tests for all combinations. E.g. all AES-related tests should be run with all AES-relevant extension combinations.

When I was working on the Zvk* tests, I used the following:

        if [ "x$DO_TEST_ALL" = "xyes" ] ; then
                make -j$(nproc) test
        fi

        if [ "x$DO_TEST_GCM" = "xyes" ] ; then
                $QEMU test/aesgcmtest
                make -j$(nproc) TESTS='test_aesgcm' test
        fi

        if [ "x$DO_TEST_AES" = "xyes" ] ; then
                make -j$(nproc) TESTS='test_aesgcm' test
                $QEMU test/evp_test -iter 1 ./test/recipes/30-test_evp_data/evpciph_aes_common.txt
        fi

        if [ "x$DO_TEST_SHA" = "xyes" ] ; then
                $QEMU test/sha_test
                $QEMU test/evp_test -iter 1 ./test/recipes/30-test_evp_data/evpmd_sha.txt
        fi

        if [ "x$DO_TEST_SM3" = "xyes" ] ; then
                $QEMU test/evp_test -iter 1 ./test/recipes/30-test_evp_data/evpmd_sm3.txt
        fi

        if [ "x$DO_TEST_SM4" = "xyes" ] ; then
                $QEMU test/evp_test -iter 1 ./test/recipes/30-test_evp_data/evpciph_sm4.txt
        fi

Maybe this serves as an inspiration to test only relevant tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval: otc review pending This pull request needs review by an OTC member approval: review pending This pull request needs review by a committer branch: master Merge to master branch severity: fips change The pull request changes FIPS provider sources tests: present The PR has suitable tests present triaged: feature The issue/pr requests/adds a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants