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

@ -10,6 +10,8 @@
# This makefile compiles and runs each test twice. The first with
# GCC-built chibicc, and a second time with chibicc-built chibicc
ifneq ($(MODE), dbg)
ifneq ($(MODE), asan)
ifeq ($(ARCH), x86_64)
CHIBICC = o/$(MODE)/third_party/chibicc/chibicc.com
@ -113,6 +115,8 @@ THIRD_PARTY_CHIBICC_CHECKS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x)
THIRD_PARTY_CHIBICC_OBJS = $(foreach x,$(THIRD_PARTY_CHIBICC_ARTIFACTS),$($(x)_OBJS))
$(THIRD_PARTY_CHIBICC_OBJS): $(BUILD_FILES) third_party/chibicc/chibicc.mk
endif
endif
endif
.PHONY: o/$(MODE)/third_party/chibicc

View file

@ -10,6 +10,8 @@
# This makefile compiles and runs each test twice. The first with
# GCC-built chibicc, and a second time with chibicc-built chibicc
ifneq ($(MODE), dbg)
ifneq ($(MODE), asan)
ifeq ($(ARCH), x86_64)
PKGS += THIRD_PARTY_CHIBICC_TEST
@ -77,6 +79,8 @@ o/$(MODE)/third_party/chibicc/test/%.o: \
o/$(MODE)/third_party/chibicc/test/int128_test.o: private QUOTA = -M1024m
endif
endif
endif
.PHONY: o/$(MODE)/third_party/chibicc/test

View file

@ -185,6 +185,7 @@ o/$(MODE)/third_party/ggml/companionai.txt.zip.o: private \
-B
o/$(MODE)/third_party/ggml/ggml.o: private QUOTA = -C64
o/$(MODE)/third_party/ggml/llama.o: private QUOTA = -C64
################################################################################

View file

@ -2660,13 +2660,14 @@ size_t llama_copy_state_data(struct llama_context * ctx, uint8_t * dst) {
rng_ss << ctx->rng;
const size_t rng_size = rng_ss.str().size();
char rng_buf[LLAMA_MAX_RNG_STATE];
llama_buffer rng_buf;
rng_buf.resize(LLAMA_MAX_RNG_STATE);
memset(&rng_buf[0], 0, LLAMA_MAX_RNG_STATE);
memcpy(&rng_buf[0], rng_ss.str().data(), rng_ss.str().size());
memset(&rng_buf.addr[0], 0, LLAMA_MAX_RNG_STATE);
memcpy(&rng_buf.addr[0], rng_ss.str().data(), rng_ss.str().size());
memcpy(out, &rng_size, sizeof(rng_size)); out += sizeof(rng_size);
memcpy(out, &rng_buf[0], LLAMA_MAX_RNG_STATE); out += LLAMA_MAX_RNG_STATE;
memcpy(out, &rng_buf.addr[0], LLAMA_MAX_RNG_STATE); out += LLAMA_MAX_RNG_STATE;
}
// copy logits
@ -2759,13 +2760,14 @@ size_t llama_set_state_data(struct llama_context * ctx, const uint8_t * src) {
// set rng
{
size_t rng_size;
char rng_buf[LLAMA_MAX_RNG_STATE];
llama_buffer rng_buf;
rng_buf.resize(LLAMA_MAX_RNG_STATE);
memcpy(&rng_size, in, sizeof(rng_size)); in += sizeof(rng_size);
memcpy(&rng_buf[0], in, LLAMA_MAX_RNG_STATE); in += LLAMA_MAX_RNG_STATE;
memcpy(&rng_size, in, sizeof(rng_size)); in += sizeof(rng_size);
memcpy(&rng_buf.addr[0], in, LLAMA_MAX_RNG_STATE); in += LLAMA_MAX_RNG_STATE;
std::stringstream rng_ss;
rng_ss.str(std::string(&rng_buf[0], rng_size));
rng_ss.str(std::string((char *)&rng_buf.addr[0], rng_size));
rng_ss >> ctx->rng;
LLAMA_ASSERT(rng_ss.fail() == false);

View file

@ -190,11 +190,11 @@ o/$(MODE)/third_party/sqlite3/parse.o: private \
CFLAGS += \
-fpie
o/$(MODE)/third_party/sqlite3/shell.o: private QUOTA = -M512m -C16 -L180
o/$(MODE)/third_party/sqlite3/shell.o: private QUOTA = -M512m -C32 -L180
o/$(MODE)/third_party/sqlite3/vdbe.o: private QUOTA = -M1024m
o/$(MODE)/third_party/sqlite3/vdbe.shell.o: private QUOTA = -M1024m
o/$(MODE)/third_party/sqlite3/fts5.o: private QUOTA = -M512m -C16
o/$(MODE)/third_party/sqlite3/fts5.shell.o: private QUOTA = -M512m -C16 -L180
o/$(MODE)/third_party/sqlite3/fts5.o: private QUOTA = -M512m -C32
o/$(MODE)/third_party/sqlite3/fts5.shell.o: private QUOTA = -M512m -C32 -L180
o/$(MODE)/third_party/sqlite3/rtree.o: \
third_party/sqlite3/rtree.c \