Fix stack abuse in llama.cc

This change also incorporates improvements for MODE=asan. It's been
confirmed that o/asan/third_party/ggml/llama.com will work.

Fixes #829
This commit is contained in:
Justine Tunney 2023-06-08 06:44:54 -07:00
parent 32682f0ce7
commit 4d629fd424
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
12 changed files with 73 additions and 76 deletions

View file

@ -16,30 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/str/str.h"
#ifndef __aarch64__
// TODO(jart): ASAN support here is important.
typedef char xmm_u __attribute__((__vector_size__(16), __aligned__(1)));
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
#ifdef __x86_64__
static inline noasan size_t stpcpy_sse2(char *d, const char *s, size_t i) {
xmm_t v, z = {0};
for (;;) {
v = *(xmm_t *)(s + i);
if (!__builtin_ia32_pmovmskb128(v == z)) {
*(xmm_u *)(d + i) = v;
i += 16;
} else {
break;
}
}
return i;
}
#endif
/**
* Copies bytes from 𝑠 to 𝑑 until a NUL is encountered.
*
@ -49,15 +33,27 @@ static inline noasan size_t stpcpy_sse2(char *d, const char *s, size_t i) {
* @return pointer to nul byte
* @asyncsignalsafe
*/
char *stpcpy(char *d, const char *s) {
noasan char *stpcpy(char *d, const char *s) {
size_t i = 0;
if (IsAsan()) {
__asan_verify(d, strlen(s) + 1);
}
#ifdef __x86_64__
for (; (uintptr_t)(s + i) & 15; ++i) {
if (!(d[i] = s[i])) {
return d + i;
}
}
i = stpcpy_sse2(d, s, i);
for (;;) {
xmm_t z = {0};
xmm_t v = *(xmm_t *)(s + i);
if (!__builtin_ia32_pmovmskb128(v == z)) {
*(xmm_u *)(d + i) = v;
i += 16;
} else {
break;
}
}
#endif
for (;;) {
if (!(d[i] = s[i])) {