Skip to content

Replace banned APIs in native code - #7912

Open
svick wants to merge 2 commits into
dotnet:mainfrom
svick:fix/native-banned-apis
Open

svick wants to merge 2 commits into
dotnet:mainfrom
svick:fix/native-banned-apis

Conversation

@svick

@svick svick commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Summary

  • replace banned formatted-output APIs in first-party native code with typed C++ streams and string construction
  • replace first-party memcpy calls with std::copy_n and propagate destination capacities so copies are validated before execution
  • use getenv_s for the diagnostic environment flag on Windows (the old _dupenv_s was not banned, but it allocated unnecessarily)
  • retain a narrowly scoped getenv presence check on Unix, where no better portable replacement is available; the returned pointer is neither dereferenced nor retained
  • note that the Windows and Linux implementation behave differently when the variable is set to empty string; I didn't change this to maintain compatibility
  • leave vendored third-party code unchanged

Justification

The replacements remove avoidable banned APIs while preserving existing output and training behavior. Explicit capacity propagation strengthens the copy replacements rather than merely changing the function name. The Unix environment lookup controls only optional diagnostic output and uses getenv solely to test whether the variable exists.

Note

This PR was generated with GitHub Copilot.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI 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.

🟡 Changes recommended

Concurrent LDA worker logging can interleave timing records and alter another worker’s floating-point formatting, requiring local record formatting and serialized emission without modifying std::cout’s formatting state.

1 open issue
What changed in this PR

This PR replaces banned APIs in ML.NET’s first-party native code while leaving vendored code unchanged.

Changes:

  • Replaces formatted-output calls with C++ streams and string construction.
  • Replaces memcpy calls with std::copy_n, propagating buffer capacities for validation.
  • Uses getenv_s for Windows diagnostics and retains a presence-only Unix environment check.
File Description
src/​Native/​SymSgdNative/​SymSgdNative.h Validates model size before copying weights.
src/​Native/​SymSgdNative/​SymSgdNative.cpp Passes model size to reset calls.
src/​Native/​OneDalNative/​OneDalAlgorithms.cpp Replaces diagnostic output and environment lookup APIs.
src/​Native/​LdaNative/​timer.h Uses string messages and stream-based timing output.
src/​Native/​LdaNative/​model_block.h Propagates rehash and alias buffer capacities.
src/​Native/​LdaNative/​model_block.cpp Updates map construction with buffer size.
src/​Native/​LdaNative/​light_doc_sampler.cpp Supplies rehash buffer capacity.
src/​Native/​LdaNative/​lda_engine.cpp Converts diagnostics to streams and string messages.
src/​Native/​LdaNative/​hybrid_map.h Validates rehash capacity before copying.
src/​Native/​LdaNative/​hybrid_map.cpp Preserves buffer capacity across construction and copying.
src/​Native/​LdaNative/​hybrid_alias_map.h Adds alias memory capacity tracking.
src/​Native/​LdaNative/​hybrid_alias_map.cpp Propagates capacity to alias-table generation.
src/​Native/​LdaNative/​alias_multinomial_rng_int.hpp Validates alias workspace and destination before copying.

Review effort: Balanced

Comment thread src/Native/LdaNative/timer.h Resolved
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI 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.

🟢 Approval recommended

No active review issues are blocking the native API replacements and buffer-capacity updates.

1 open issue

Review effort: Balanced

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.86%. Comparing base (886c962) to head (bd9f3b0).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7912   +/-   ##
=======================================
  Coverage   70.85%   70.86%           
=======================================
  Files        1420     1420           
  Lines      279991   279991           
  Branches    27594    27594           
=======================================
+ Hits       198381   198408   +27     
+ Misses      73462    73446   -16     
+ Partials     8148     8137   -11     
Flag Coverage Δ
Debug 70.86% <ø> (+<0.01%) ⬆️
production 64.31% <ø> (+0.01%) ⬆️
test 90.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 12 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@svick
svick marked this pull request as ready for review November 13, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants