mirror of
				https://github.com/GrapheneOS/hardened_malloc.git
				synced 2025-11-03 01:06:33 +01:00 
			
		
		
		
	added tests for malloc_usable_size
This commit is contained in:
		
							parent
							
								
									c75dcb9d9c
								
							
						
					
					
						commit
						7652785c10
					
				
					 6 changed files with 63 additions and 1 deletions
				
			
		
							
								
								
									
										3
									
								
								test/simple-memory-corruption/.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								test/simple-memory-corruption/.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
				
			
			@ -23,4 +23,7 @@ write_after_free_large_reuse
 | 
			
		|||
write_after_free_small
 | 
			
		||||
write_after_free_small_reuse
 | 
			
		||||
write_zero_size
 | 
			
		||||
unaligned_malloc_usable_size_small
 | 
			
		||||
invalid_malloc_usable_size_small
 | 
			
		||||
invalid_malloc_usable_size_small_quarantine
 | 
			
		||||
__pycache__/
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,7 +23,10 @@ EXECUTABLES := \
 | 
			
		|||
    eight_byte_overflow_small \
 | 
			
		||||
    eight_byte_overflow_large \
 | 
			
		||||
    string_overflow \
 | 
			
		||||
    delete_type_size_mismatch
 | 
			
		||||
    delete_type_size_mismatch \
 | 
			
		||||
	unaligned_malloc_usable_size_small \
 | 
			
		||||
	invalid_malloc_usable_size_small \
 | 
			
		||||
	invalid_malloc_usable_size_small_quarantine
 | 
			
		||||
 | 
			
		||||
all: $(EXECUTABLES)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
#include <malloc.h>
 | 
			
		||||
 | 
			
		||||
__attribute__((optimize(0)))
 | 
			
		||||
int main(void) {
 | 
			
		||||
    char *p = malloc(16);
 | 
			
		||||
    if (!p) {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    char *q = p + 4096 * 4;
 | 
			
		||||
    malloc_usable_size(q);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
#include <malloc.h>
 | 
			
		||||
 | 
			
		||||
__attribute__((optimize(0)))
 | 
			
		||||
int main(void) {
 | 
			
		||||
    void *p = malloc(16);
 | 
			
		||||
    if (!p) {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    free(p);
 | 
			
		||||
    malloc_usable_size(p);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -86,6 +86,20 @@ class TestSimpleMemoryCorruption(unittest.TestCase):
 | 
			
		|||
        self.assertEqual(stderr.decode("utf-8"),
 | 
			
		||||
                         "fatal allocator error: invalid free\n")
 | 
			
		||||
 | 
			
		||||
    def test_invalid_malloc_usable_size_small_quarantene(self):
 | 
			
		||||
        _stdout, stderr, returncode = self.run_test(
 | 
			
		||||
            "invalid_malloc_usable_size_small_quarantine")
 | 
			
		||||
        self.assertEqual(returncode, -6)
 | 
			
		||||
        self.assertEqual(stderr.decode(
 | 
			
		||||
            "utf-8"), "fatal allocator error: invalid malloc_usable_size (quarantine)\n")
 | 
			
		||||
 | 
			
		||||
    def test_invalid_malloc_usable_size_small(self):
 | 
			
		||||
        _stdout, stderr, returncode = self.run_test(
 | 
			
		||||
            "invalid_malloc_usable_size_small")
 | 
			
		||||
        self.assertEqual(returncode, -6)
 | 
			
		||||
        self.assertEqual(stderr.decode(
 | 
			
		||||
            "utf-8"), "fatal allocator error: invalid malloc_usable_size\n")
 | 
			
		||||
 | 
			
		||||
    def test_read_after_free_large(self):
 | 
			
		||||
        _stdout, _stderr, returncode = self.run_test("read_after_free_large")
 | 
			
		||||
        self.assertEqual(returncode, -11)
 | 
			
		||||
| 
						 | 
				
			
			@ -117,6 +131,13 @@ class TestSimpleMemoryCorruption(unittest.TestCase):
 | 
			
		|||
        self.assertEqual(stderr.decode("utf-8"),
 | 
			
		||||
                         "fatal allocator error: invalid unaligned free\n")
 | 
			
		||||
 | 
			
		||||
    def test_unaligned_malloc_usable_size_small(self):
 | 
			
		||||
        _stdout, stderr, returncode = self.run_test(
 | 
			
		||||
            "unaligned_malloc_usable_size_small")
 | 
			
		||||
        self.assertEqual(returncode, -6)
 | 
			
		||||
        self.assertEqual(stderr.decode("utf-8"),
 | 
			
		||||
                         "fatal allocator error: invalid unaligned malloc_usable_size\n")
 | 
			
		||||
 | 
			
		||||
    def test_uninitialized_free(self):
 | 
			
		||||
        _stdout, stderr, returncode = self.run_test("uninitialized_free")
 | 
			
		||||
        self.assertEqual(returncode, -6)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
#include <malloc.h>
 | 
			
		||||
 | 
			
		||||
__attribute__((optimize(0)))
 | 
			
		||||
int main(void) {
 | 
			
		||||
    char *p = malloc(16);
 | 
			
		||||
    if (!p) {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    malloc_usable_size(p + 1);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		
		Reference in a new issue