<feed xmlns='http://www.w3.org/2005/Atom'>
<title>src/lib/libc/amd64/string, branch main</title>
<subtitle>FreeBSD source tree</subtitle>
<id>https://cgit-dev.freebsd.org/src/atom?h=main</id>
<link rel='self' href='https://cgit-dev.freebsd.org/src/atom?h=main'/>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/'/>
<updated>2026-03-26T13:01:57Z</updated>
<entry>
<title>libc/amd64/strrchr.S: fix rebase error</title>
<updated>2026-03-26T13:01:57Z</updated>
<author>
<name>Robert Clausecker</name>
<email>fuz@FreeBSD.org</email>
</author>
<published>2026-03-26T13:00:21Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=23d6516773916d8f324bea51867b0713c476f379'/>
<id>urn:sha1:23d6516773916d8f324bea51867b0713c476f379</id>
<content type='text'>
I accidentally dropped a part of the patch on squash rebase.
Should be fine now.

Fixes:		253f15c016ca699906f78b8e522a3f7ed675929b
PR:		293915
MFC after:	1 week
</content>
</entry>
<entry>
<title>libc/amd64/strrchr.S: rewrite and fix scalar implementation</title>
<updated>2026-03-26T11:41:49Z</updated>
<author>
<name>Robert Clausecker</name>
<email>fuz@FreeBSD.org</email>
</author>
<published>2026-03-22T12:37:06Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=253f15c016ca699906f78b8e522a3f7ed675929b'/>
<id>urn:sha1:253f15c016ca699906f78b8e522a3f7ed675929b</id>
<content type='text'>
The original scalar implementation of strrchr() had incorrect
logic that failed if the character searched for was the NUL
character.  It was also possibly affected by the issue fixed
in 3d8ef251a for strchrnul().

Rewrite the function with logic that actually works.  We defer
checking for the character until after we have checked for NUL.
When we encounter the final NUL byte, we mask out the characters
beyond the tail before checking for a match.

This bug only affects users running on amd64 with ARCHLEVEL=scalar
(cf. simd(7)).  The default configuration is not affected.

The bug was unfortunately not caught by the unit test inherited
from NetBSD.  An extended unit test catching the issue is proposed
in D56037.

PR:		293915
Reported by:	safonov.paul@gmail.com
Tested by:	safonov.paul@gmail.com
Fixes:		2ed514a220edbac6ca5ec9f40a3e0b3f2804796d
See also:	https://reviews.freebsd.org/D56037
MFC after:	1 week
</content>
</entry>
<entry>
<title>libc/amd64: fix stpncpy.S again</title>
<updated>2026-01-04T13:21:41Z</updated>
<author>
<name>Robert Clausecker</name>
<email>fuz@FreeBSD.org</email>
</author>
<published>2025-12-18T23:37:33Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=2f83319214d9adb8ab7a77e35d1014658b3f9cae'/>
<id>urn:sha1:2f83319214d9adb8ab7a77e35d1014658b3f9cae</id>
<content type='text'>
The previous fix introduced a regression on machines without the BMI1
instruction set extension.  The TZCNT instruction used in this function
behaves different on old machines when the source operand is zero, but
the code was originally designed to never trigger this case.  The bug
fix caused this case to be possible, leading to a regression on
sufficiently old hardware.

Fix the code by messing with things such that the source operand is
never zero.

PR:		291720
Fixes:		66eb78377bf109af1d9e25626bf254b4369436ec
Tested by:	cy
Approved by:	markj (mentor)
Differential Revision:	https://reviews.freebsd.org/D54303
</content>
</entry>
<entry>
<title>libc/amd64: Disable baseline version of stpncpy()</title>
<updated>2025-12-16T19:14:44Z</updated>
<author>
<name>Dag-Erling Smørgrav</name>
<email>des@FreeBSD.org</email>
</author>
<published>2025-12-16T19:04:51Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=ce9557d4ee174267504a63b2c8f6009be27368c9'/>
<id>urn:sha1:ce9557d4ee174267504a63b2c8f6009be27368c9</id>
<content type='text'>
This implementation appears to be broken on some CPUs.  Disable it
until the issue can be investigated and fixed.

PR:		291720
Fixes:		66eb78377bf1 ("libc/amd64: fix overread conditions in stpncpy()")
Fixes:		90253d49db09 ("lib/libc/amd64/string: add stpncpy scalar, baseline implementation")
</content>
</entry>
<entry>
<title>libc/amd64: fix overread conditions in stpncpy()</title>
<updated>2025-12-14T16:06:05Z</updated>
<author>
<name>Robert Clausecker</name>
<email>fuz@FreeBSD.org</email>
</author>
<published>2025-12-10T20:45:18Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=66eb78377bf109af1d9e25626bf254b4369436ec'/>
<id>urn:sha1:66eb78377bf109af1d9e25626bf254b4369436ec</id>
<content type='text'>
Due to incorrect unit test design, two overread conditions went
undetected in the amd64 baseline stpncpy() implementation.
For buffers of 1--16 and 32 bytes that do not contain nul bytes
and end exactly at a page boundary, the code would incorrectly
read 16 bytes from the next page, possibly crossing into an
unmapped page and crashing the program.  If the next page was
mapped, the code would then proceed with the expected behaviour
of the stpncpy() function.

Three changes were made to fix the bug:

 - an off-by-one error is fixed in the code deciding whether to
   enter the runt case or not, entering it for 0&lt;n&lt;=32 bytes
   instead of 0&lt;n&lt;32 bytes as it was before.
 - in the runt case, the logic to skip reading a second 16-byte
   chunk if the buffer ends in the first chunk was fixed to
   account for buffers that end at a 16-byte boundary but do not
   hold a nul byte.
 - in the runt case, the logic to transform the location of the
   end of the input buffer into a bit mask was fixed to allow
   the case of n==32, which was previously impossible due to the
   incorrect logic for entering said case.

The performance impact should be minimal.

PR:		291359
See also:	D54169
Reported by:	Collin Funk &lt;collin.funk1@gmail.com&gt;
Reviewed by:	getz
Approved by:	markj (mentor)
MFC after:	1 week
Fixes:		90253d49db09a9b1490c448d05314f3e4bbfa468 (D42519)
Differential Revision:	https://reviews.freebsd.org/D54170
</content>
</entry>
<entry>
<title>libc/amd64: rewrite memrchr() scalar impl. to read the string from the back</title>
<updated>2025-08-09T20:13:27Z</updated>
<author>
<name>Robert Clausecker</name>
<email>fuz@FreeBSD.org</email>
</author>
<published>2025-07-29T20:12:11Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=30acc84270266e41f66cf572f67c3290d923da2f'/>
<id>urn:sha1:30acc84270266e41f66cf572f67c3290d923da2f</id>
<content type='text'>
A very simple implementation as I don't have the patience right now
to write a full SWAR kernel.  Should still do the trick if you wish
to opt out of SSE for some reason.

Reported by:	Mikael Simonsson &lt;m@mikaelsimonsson.com&gt;
Reviewed by:	strajabot
PR:		288321
MFC after:	1 month
</content>
</entry>
<entry>
<title>libc/amd64: rewrite memrchr() baseline impl. to read the string from the back</title>
<updated>2025-08-09T20:13:27Z</updated>
<author>
<name>Robert Clausecker</name>
<email>fuz@FreeBSD.org</email>
</author>
<published>2025-07-29T20:12:32Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=4b15965daa99044daf184221b7c283bf7f2d7e66'/>
<id>urn:sha1:4b15965daa99044daf184221b7c283bf7f2d7e66</id>
<content type='text'>
This ensures O(1) behaviour if the character is a constant offset
from the end of the string, regardless of how long the string is.

Reported by:	Mikael Simonsson &lt;m@mikaelsimonsson.com&gt;
Reviewed by:	benni
PR:		288321
MFC after:	1 month
</content>
</entry>
<entry>
<title>lib/libc/amd64/string: fix overread condition in memccpy</title>
<updated>2024-07-29T19:36:10Z</updated>
<author>
<name>Robert Clausecker</name>
<email>fuz@FreeBSD.org</email>
</author>
<published>2024-07-20T19:53:04Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=9082398090bcf0ac333397d47e594b105ea3aefd'/>
<id>urn:sha1:9082398090bcf0ac333397d47e594b105ea3aefd</id>
<content type='text'>
An overread condition in memccpy(dst, src, c, len) would occur if
src does not cross a 16 byte boundary and there is no instance of
c between *src and the next 16 byte boundary.  This could cause a
read fault if src is just before the end of a page and the next page
is unmapped or unreadable.

The bug is a consequence of basing memccpy() on the strlcpy() code:
whereas strlcpy() assumes that src is a nul-terminated string and
hence a terminator is always present, c may not be present at all in
the source string.  It was not caught earlier due to insufficient
unit test design.

As a part of the fix, the function is refactored such that the runt
case (buffer length from last alignment boundary between 1 and 32 B)
is handled separately.  This reduces the number of conditional
branches on all code paths and simplifies the handling of early
matches in the non-runt case.  Performance is improved slightly.

os: FreeBSD
arch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
        │ memccpy.unfixed.out │        memccpy.fixed.out           │
        │       sec/op        │   sec/op     vs base               │
Short             66.76µ ± 0%   62.45µ ± 1%  -6.44% (p=0.000 n=20)
Mid               7.938µ ± 0%   7.967µ ± 0%  +0.36% (p=0.001 n=20)
Long              3.577µ ± 0%   3.577µ ± 0%       ~ (p=0.429 n=20)
geomean           12.38µ        12.12µ       -2.08%

        │ memccpy.unfixed.out │         memccpy.fixed.out           │
        │         B/s         │     B/s       vs base               │
Short            1.744Gi ± 0%   1.864Gi ± 1%  +6.89% (p=0.000 n=20)
Mid              14.67Gi ± 0%   14.61Gi ± 0%  -0.36% (p=0.001 n=20)
Long             32.55Gi ± 0%   32.55Gi ± 0%       ~ (p=0.429 n=20)
geomean          9.407Gi        9.606Gi       +2.12%

Reported by:	getz
Reviewed by:	getz
Approved by:	mjg (blanket, via IRC)
See also:	D46051
MFC:		stable/14
Event:		GSoC 2024
Differential Revision:	https://reviews.freebsd.org/D46052
</content>
</entry>
<entry>
<title>include: ssp: round out fortification of current set of headers</title>
<updated>2024-07-13T05:16:24Z</updated>
<author>
<name>Kyle Evans</name>
<email>kevans@FreeBSD.org</email>
</author>
<published>2024-07-13T05:16:10Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=cf8e5289a110954600f135024d1515a77d0ae34d'/>
<id>urn:sha1:cf8e5289a110954600f135024d1515a77d0ae34d</id>
<content type='text'>
ssp/ssp.h needed some improvements:
 - `len` isn't always a size_t, it may need casted
 - In some cases we may want to use a len that isn't specified as a
    parameter (e.g., L_ctermid), so __ssp_redirect() should be more
    flexible.
 - In other cases we may want additional checking, so pull all of the
    declaration bits out of __ssp_redirect_raw() so that some functions
    can implement the body themselves.

strlcat/strlcpy should be the last of the fortified functions that get
their own __*_chk symbols, and these cases are only done to be
consistent with the rest of the str*() set.

Reviewed by:	markj
Sponsored by:	Klara, Inc.
Sponsored by:	Stormshield
Differential Revision:	https://reviews.freebsd.org/D45679
</content>
</entry>
<entry>
<title>Prepare the system for _FORTIFY_SOURCE</title>
<updated>2024-05-13T05:23:50Z</updated>
<author>
<name>Kyle Evans</name>
<email>kevans@FreeBSD.org</email>
</author>
<published>2024-05-13T05:23:50Z</published>
<link rel='alternate' type='text/html' href='https://cgit-dev.freebsd.org/src/commit/?id=e55512504d0178983978d64d67eed1cc85826523'/>
<id>urn:sha1:e55512504d0178983978d64d67eed1cc85826523</id>
<content type='text'>
Notably:
- libc needs to #undef some of the macros from ssp/* for underlying
  implementations
- ssp/* wants a __RENAME() macro (snatched more or less from NetBSD)

There's some extra hinkiness included for read(), since libc spells it
as "_read" while the rest of the world spells it "read."

Reviewed by:	imp, ngie
Sponsored by:	Stormshield
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D32307
</content>
</entry>
</feed>
