Repository navigation
String copies are bounded: memcpy_s on Windows, __memcpy_chk on Linux and Android - #29
Merged
Merged
Conversation
… and Android
lib.ts copied string bytes with memcpy, trusting each count. Every copy now
goes through boundedCopy(dest, destSize, src, count), which ends the process
rather than write past the destination:
- Windows: the CRT's memcpy_s (its invalid parameter handler fails fast,
0xC0000409)
- Linux and Android: glibc's and Bionic's fortified __memcpy_chk ("buffer
overflow detected"), through __builtin___memcpy_chk, which the compiler
turns back into a plain memcpy where it can prove the copy fits
boundedCopy is a C++ wrapper (src/wrappers/memory.cpp, chosen by #ifdef),
not TypeScript in lib.win32.ts / lib.linux.ts. The compiler now emits
memcpy_s / __memcpy_chk for string copies of its own. Under -mm=rc,
lib.win32.ts made such a copy before a TypeScript declaration of memcpy_s
further down was lowered, and the module failed with "redefinition of
symbol named 'memcpy_s'". memory.cpp is added to build_core.bat, build.sh
and build_android.bat.
regex.cpp's two copies out of std::string use memcpy_s on Windows too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lib.tscopied string bytes withmemcpy, trusting each count. Every copy (concat,join,repeat,slice,substring,RegExp.replace,replaceAll) now goes throughboundedCopy(dest, destSize, src, count), which ends the process rather than write past the destination:memcpy_s. Its invalid parameter handler fails fast (0xC0000409).__memcpy_chk("buffer overflow detected"), through__builtin___memcpy_chk. The compiler turns it back into a plainmemcpywhere it can prove the copy fits.boundedCopyis a C++ wrapper (src/wrappers/memory.cpp, platform chosen by#ifdef), declared inlib.native.d.ts. It is not TypeScript inlib.win32.ts/lib.linux.ts: with ASDAlexander77/TypeScriptCompiler#525 the compiler emitsmemcpy_s/__memcpy_chkfor copies of its own. Under-mm=rc,lib.win32.tsmade such a copy before a TypeScriptdeclare function memcpy_sfurther down was lowered, and the module failed with "redefinition of symbol named 'memcpy_s'".memory.cppis added tobuild_core.bat,build.shandbuild_android.bat. The two copies inregex.cppusememcpy_son Windows too.Works with the current compiler, but merge after ASDAlexander77/TypeScriptCompiler#525 (or together) so the compiler's own copies are bounded as well.
Test plan
boundedCopywith a count one byte too large exits0xC0000409; a copy that fits succeedsmemory.cppbuilt with g++ links__memcpy_chk; one byte too many aborts with "buffer overflow detected"build.shand Androidbuild_android.bat: only checked by reading them, not run. This repo has no PR CI; they first run in the release workflow.🤖 Generated with Claude Code