char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
5788b510a
char/aliceai-t5-moe
1import struct 2import numpy as np 3import pytest 4 5from gguf .gguf_reader import GGUFReader 6 7 8def _write_gguf (path ,n_dims_field ,dims ): 9buf = b'GGUF' + struct .pack ('<IQQ' ,3 ,1 ,0 )# version 3, 1 tensor, 0 kv 10name = b'bad_tensor' 11buf += struct .pack ('<Q' ,len (name ))+ name 12buf += struct .pack ('<I' ,n_dims_field ) 13for d in dims : 14buf += struct .pack ('<Q' ,d ) 15buf += struct .pack ('<I' ,0 )# dtype F32 16buf += struct .pack ('<Q' ,0 )# tensor offset 17buf += b'\x00' * 64 18path .write_bytes (buf ) 19 20 21def test_n_dims_upper_bound (tmp_path ): 22# crafted file claims 1_000_000 dims; must be rejected, not read past EOF 23p = tmp_path / 'evil_ndims.gguf' 24_write_gguf (p ,1_000_000 , [1 ]* 8 ) 25with pytest .raises (ValueError ,match = 'exceeds GGML_MAX_DIMS' ): 26GGUFReader (p ) 27 28 29def test_dims_product_no_uint64_wraparound (tmp_path ): 30# dims whose true product overflows uint64; np.prod would wrap to 4 and 31# silently pass an undersized read. The reader must not accept it. 32dims = [4194305 ,4194305 ,211106198978564 ] 33assert int (np .prod (np .array (dims ,dtype = np .uint64 )))== 4 # the wrap bug 34p = tmp_path / 'evil_overflow.gguf' 35_write_gguf (p ,len (dims ),dims ) 36with pytest .raises (ValueError ): 37GGUFReader (p )