Skip to content

feat(libSceLibreSsl3): implement BN_dup - #15627

Merged
oneandonlydean merged 10 commits into
boykopovar:mainfrom
alvaroelpob:feat/libscelibressl3-bn-dup
Nov 10, 2026
Merged

oneandonlydean merged 10 commits into
boykopovar:mainfrom
alvaroelpob:feat/libscelibressl3-bn-dup

Conversation

@alvaroelpob

@alvaroelpob alvaroelpob commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements BN_dup in libSceLibreSsl3, following LibreSSL 4.0.0 bn_lib.c: a null source returns null; otherwise a new libSceLibreSsl BIGNUM gets a guest-heap copy of the words, the sign and the constant-time flag. A failed allocation or a source over INT_MAX / 256 words returns null and queues ERR_R_MALLOC_FAILURE or BN_R_BIGNUM_TOO_LONG on the shared libSceLibreSsl error queue.

Tested

Linux x86-64 (WSL2), Release: the full build passes and ctest --test-dir build -j16 passes 3829 of 3829 tests, including the new guest_libressl3_bn_dup. python3 tools/check_conventions.py --base upstream/main passes. Not run on a console or in a title.

Checklist

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Progress report (53bb992 → f15d5a9)

📈 System libraries: 35.8% (+0.0026%, +1 implemented functions)

✅ 1 implemented
Library Name
libSceLibreSsl3 BN_dup_nid_postfix

Changed libraries (implemented exports)

Library Before After Change
libSceLibreSsl3 517/680 (76.03%) 518/680 (76.18%) +0.15%

@XairRrR XairRrR left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — approved

BN_dup matches LibreSSL 4.0.0 BN_new + bn_wexpand + BN_copy, including the queued error codes and the BN_FLG_CONSTTIME copy. It builds, the new test passes, and the test fails both when the export is put back to a throwing stub and when the constant-time copy is removed.

What I ran

Head a87c18213efb31970a888f7dbf02bd7997429226, main 1c8c0efc8c41314402e6966da5bef6ea3d639d10, merged tree d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0 (--no-ff), on Debian 13, GCC 14.2.0, Release, Ninja.

git diff --check 1c8c0efc8c41314402e6966da5bef6ea3d639d10...a87c18213efb31970a888f7dbf02bd7997429226
python3 tools/check_conventions.py --base 1c8c0efc8c41314402e6966da5bef6ea3d639d10
ctest --test-dir build --output-on-failure -R "^guest_libressl3_"
ctest --test-dir build --output-on-failure -R "public_headers"
# negative control 1: src/BignumDup.cpp replaced by main's throwing stub (NotImplemented_nid_no_patch) with the new signature
ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests
ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$"
# negative control 2: merged src/BignumDup.cpp without the `copy->flags |= source->flags & FlagConstantTime;` line
ninja -C build -j32 -l32 guest_libressl3_bn_dup_tests
ctest --test-dir build --output-on-failure -R "^guest_libressl3_bn_dup$"
# restore merged file, rebuild, rerun, confirm clean tree
git show d481db5aaf5d72a9b6dee0b1ea8604ba7864bce0:core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp > core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp
git status --porcelain
# reference check against the LibreSSL commit cited in the PR (present in the 3rdparty/libressl-openbsd object store)
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn_lib.c
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/bn/bn.h
git show ce488b78e0bd994780569df718596b9776ff681a:src/lib/libcrypto/err/err.h

Results

Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Build (script, merged tree) OK
Selected tests (script): 835 ran 831 passed, 3 skipped (all expected), 0 new failures, 0 timeouts. One known failure (unwrap_self_..._inherits_the_destination_directory_group) also fails on main and is unrelated
ctest -R ^guest_libressl3_ (11 tests) 11/11 passed; guest_libressl3_bn_dup takes 0.01 s
ctest -R public_headers (65 tests) 65/65 passed
Negative control 1 (export back to a stub) fails: BN_dup_nid_postfix not implemented
Negative control 2 (no constant-time flag copy) fails at test line 90
Worktree after restore git status --porcelain empty; test passes again
Vulkan validation not applicable (no AGC/shader/VideoOut files)
Negative control 1 output
1/1 Test #2874: guest_libressl3_bn_dup ...........Subprocess aborted***Exception:   0.01 sec
[libc] unhandled exception 'St13runtime_error' thrown from 0x7f01a74cbb79
terminate called after throwing an instance of 'std::runtime_error'
  what():  BN_dup_nid_postfix not implemented
0% tests passed, 1 tests failed out of 1
Negative control 2 output
LibreSSL3 BN_dup check failed at line 90
0% tests passed, 1 tests failed out of 1

Reference check

I checked the implementation line by line against LibreSSL at the commit the PR cites (ce488b78e0bd):

  • BN_new uses calloc and sets BN_FLG_MALLOCED (0x01). On failure it queues ERR_R_MALLOC_FAILURE (1|ERR_R_FATAL = 65).
  • bn_wexpand returns 0 for a negative word count without queuing anything. That matches the silent null for a negative top.
  • bn_expand_internal queues BN_R_BIGNUM_TOO_LONG (114) above INT_MAX / (4 * BN_BITS2), which is INT_MAX / 256 with 64-bit words. It allocates a zeroed array of exactly words, sets dmax = words, and queues ERR_R_MALLOC_FAILURE on failure.
  • BN_copy copies the words, top, neg, and b->flags & BN_FLG_CONSTTIME (0x04).
  • BNerror uses function code 0xfff and ERR_LIB_BN = 3. With ERR_PACK, the test's expected codes are 0x03fff041 and 0x03fff072.
  • The 24-byte bignum_st layout matches the static_asserts in libSceLibreSsl/src/Bignum.cpp.

The copy's d comes from calloc_nid_postfix, and the struct has FlagMalloced without FlagStaticData. So the existing BN_free_nid_postfix frees both, and the test's liveBlocks == 0 check confirms it. A source with top > 0 and null d throws where LibreSSL would dereference null, as the project rules require.

Negative control

The PR adds src/BignumDup.cpp. Moving it aside would break the CMake source list, so I replaced it with main's production behaviour: a NotImplemented_nid_no_patch stub, with the new signature so the test still compiles. The test aborted as expected. As a second check, I removed only the BN_FLG_CONSTTIME copy, and the test failed at line 90. I then restored the merged file, rebuilt and reran the test (it passed), and confirmed the worktree is back to the merged tree.

Findings

Blocking

None.

Non-blocking

  • docs/dev/debt/libSceLibreSsl3.md:28 cites bn_lib.c#L71-L276, which runs from BN_new to BN_dup. The word, sign and BN_FLG_CONSTTIME copy that core/libs/prx/libSceLibreSsl3/src/BignumDup.cpp:48 reproduces is in BN_copy, at L289-L307, outside the cited range. Extending the range to L307 (in the doc and the PR description) would make the source of the flag behaviour explicit.

Not verified

  • Behaviour on a console (the debt entry says so).
  • Windows/MinGW build and the new ENVIRONMENT_MODIFICATION block. I only built on Linux with GCC.
  • The description's claim of 3829/3829 tests passing. I ran only the selected tests, not the full suite.

@alvaroelpob

Copy link
Copy Markdown
Contributor Author

@XairRrR thanks. The debt entry now cites bn_lib.c#L71-L307, so the range includes BN_copy, and the description does too.

@TechLevelZero

Copy link
Copy Markdown
Contributor

✅ AMD GPU test: passed

PR head 47a3a24 merged onto main 5b1d381 · tested 2026-11-04 12:26 UTC · 17 min

GPU AMD Radeon RX 6300 (RADV NAVI24), driver Mesa 25.2.8-0ubuntu0.24.04.4, Vulkan 1.4.318
Host Ubuntu 26.04.1 LTS, kernel 7.0.0-42-generic, container ubuntu:24.04
Logs build and test logs for this run
Build ✅ passed (Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON)
Tests ✅ 1,128 passed · 4 failed (0 new, 3 also fail on main, 1 unstable on main) · 7 skipped (ANYPS5_REQUIRE_VULKAN=1)
Test setup CPU-only tests without a GPU; GPU tests (and any CPU-only test that failed or skipped without one) once on the GPU with VK_LAYER_KHRONOS_validation and ANYPS5_NO_SHADER_CACHE=1; main ran the same way
Test selection 1,139 of 3,883 tests run: the ones this change can reach, chosen by tools/select_tests.py from main's build graph
Vulkan validation ✅ no new validation IDs vs main (550 tests run on the GPU)
Slow tests (>30 s, GPU tests timed with validation layers) ⚠️ agc_driver_pipeline_journal 120.2 s, ⚠️ public_headers 80.8 s, ⚠️ agc_driver_image_sample_unnormalized 32.6 s, ⚠️ agc_driver_lazy_resolved_abi 31.3 s
Unstable on main (1, these also failed in recent main runs on this machine; not counted against this PR)
agc_driver_pipeline_journal: failed in 8 of the last 10 main runs
Failing on main too (3, not counted against this PR)
agc_driver_image_table
agc_driver_native_depth_bda_clear
agc_driver_native_multisample_depth
Validation IDs already present on main (8 IDs in 467 tests)
VUID-VkDeviceCreateInfo-pNext-pNext (467 tests): agc_driver_address_passes, agc_driver_alias_copy_label, agc_driver_alpha_to_coverage, agc_driver_array_layer_target, agc_driver_bc_block_copy, agc_driver_bda_anchor_reads, agc_driver_bda_span_reads, agc_driver_bindless_images, and 459 more
VUID-vkCmdCopyImage-srcImage-01548 (4 tests): agc_driver_depth_clear_hle, agc_driver_depth_storage_dispatch, agc_driver_depth_surface_from_storage, agc_driver_dispatch_move_depth
VUID-vkCmdCopyImage-srcImage-01551 (4 tests): agc_driver_depth_clear_hle, agc_driver_depth_storage_dispatch, agc_driver_depth_surface_from_storage, agc_driver_dispatch_move_depth
VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605 (2 tests): agc_driver_prepared_exports, agc_driver_srgb8_color_target
VUID-VkSamplerCreateInfo-addressModeU-01079 (1 test): agc_driver_image_gather_volume
VUID-VkSamplerCreateInfo-addressModeU-parameter (1 test): agc_driver_image_gather_volume
VUID-VkSamplerCreateInfo-addressModeV-parameter (1 test): agc_driver_image_gather_volume
VUID-VkSamplerCreateInfo-addressModeW-parameter (1 test): agc_driver_image_gather_volume
Skipped (7)
agc_driver_copied_writer_label_order: skipped, the device reports a minStorageBufferOffsetAlignment of 4, so the dispatch binds its record at +0x1004 in place and nothing is written back; the test needs an alignment above 4
agc_driver_float16_unused_nan_payloads: skipped, the device has no native f16 conversions with RTE rounding and kept f16 denormals
agc_driver_gcn_display_present: skipped, no display: Vulkan support is either not configured in SDL or not available in current SDL video driver (offscreen) or platform
agc_driver_linear_target_direct_write_completion: skipped, the device does not keep host imports write-watched, so no completion stamp over the target is tracked
agc_driver_ngg_vertex_wave_reduction: skipped, s3 describes a 32-lane wave only on 32-lane subgroups, and the device has 64 lanes
agc_driver_scattered_runs: skipped, host imports are unavailable: /dev/udmabuf is not accessible
agc_driver_sparse_dma_buf_import: skipped, host imports are unavailable: /dev/udmabuf is not accessible
Reproduce
git fetch origin 47a3a2412c629a709a247b013e03faab377ea16a 5b1d381d5ad5d9f923326b18479c3bd889cdd588
git checkout 5b1d381d5ad5d9f923326b18479c3bd889cdd588 && git merge --no-ff 47a3a2412c629a709a247b013e03faab377ea16a
git submodule update --init --recursive
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON
cmake --build build && cmake --build build --target libs
VK_DRIVER_FILES=/nonexistent/icd.json ctest --test-dir build -E '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 12 --output-on-failure
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation \
  ctest --test-dir build -R '^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpu' --timeout 120 --parallel 16 --output-on-failure
# failures are then re-run alone: --timeout 360 --parallel 2; new validation IDs: the test alone, twice, --parallel 1

Automated build and test on a contributor's AMD machine; it does not replace upstream CI. A new comment is posted for each new push. Full logs.

@alvaroelpob

Copy link
Copy Markdown
Contributor Author

Rebased on main 018cfcb to resolve the new conflicts. Head 74f9c46. Fixes from the rebase, as new commits: 74f9c46 (style: drop the blank lines the rebase left behind); Full Linux suite: 4168/4168. check_conventions.py passes.

@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 1384 Allocate
Release
PR 16529 BN_dup
PR 16819 Align
Allocate
PosixAlign
Realign
Reallocate
PR 16971 Allocate
Reallocate
Release
PR 17067 core/libs/prx/libSceLibreSsl3/include/Api.hpp L276 BN_dup
PR 17622 Align
Allocate
AllocateZeroed
PosixAlign
Realign
Reallocate
Release
PR 17626 Align
Allocate
AllocateZeroed
PosixAlign
Realign
Reallocate
Release
PR 17813 Align
Allocate
PosixAlign
Realign
Reallocate
Release

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 12315 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 14212 docs/dev/debt/libSceLibreSsl3.md
PR 14465 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15163 core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 15831 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16529 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16535 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16543 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16586 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16589 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16654 core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16689 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16742 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16819 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16848 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16918 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16977 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16979 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16980 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16981 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 16996 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 17124 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 17147 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 17246 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 17528 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 17621 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 17622 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md
PR 17626 core/libs/prx/libSceLibreSsl3/AdditionalExports.cpp
core/libs/prx/libSceLibreSsl3/CMakeLists.txt
core/libs/prx/libSceLibreSsl3/include/Api.hpp
docs/dev/debt/libSceLibreSsl3.md

@alvaroelpob

Copy link
Copy Markdown
Contributor Author

Rebased on main ef9dc27 to resolve the conflicts. Head 578f0d9. Full Linux suite: 4407/4407. check_conventions.py passes.

@alvaroelpob
alvaroelpob force-pushed the feat/libscelibressl3-bn-dup branch from 74f9c46 to 578f0d9 Compare November 7, 2026 20:28
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @alvaroelpob, main had moved under this PR again (the LibreSsl3 exports, Api.hpp, CMakeLists.txt and the debt doc all changed), so I merged main into your branch as a fast-forward commit on top of 578f0d9e24, without touching your change. Main now implements BIO_set_flags and BIO_clear_flags, so their placeholders are gone from AdditionalExports.cpp and Api.hpp takes main's signatures next to your BN_dup. Your BN_dup entry sits with main's new ones in the debt doc, and its row is out of the inventory table. The additional-exports line now reads 105 still-unimplemented callable exports (96 named, 9 known only by NID), counted from AdditionalExports.cpp; the two older count lines the rebase had left are gone. The LibreSsl3 tests pass on the merged tree (46 of 46). Nothing for you to do; if you push to the branch, pull first.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 10, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Hi @alvaroelpob, main moved again under this PR (it implemented X509_STORE_CTX_get0_param and took its placeholder out of AdditionalExports.cpp), so I merged main into your branch once more as a fast-forward commit on top of the last one, without touching your change. The additional-exports line in the debt doc now reads 104 still-unimplemented callable exports (95 named, 9 known only by NID), recounted from AdditionalExports.cpp. The LibreSsl3 tests pass on the merged tree. Nothing for you to do; if you push to the branch, pull first.

@oneandonlydean
oneandonlydean merged commit 56619b9 into boykopovar:main Nov 10, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants