Coverage Report

Created: 2024-09-06 07:53

/src/libvpx/vpx_mem/vpx_mem.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "vpx_mem.h"
12
#include <limits.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
#include "include/vpx_mem_intrnl.h"
17
#include "vpx/vpx_integer.h"
18
19
#if !defined(VPX_MAX_ALLOCABLE_MEMORY)
20
#if SIZE_MAX > (1ULL << 40)
21
#define VPX_MAX_ALLOCABLE_MEMORY (1ULL << 40)
22
#else
23
// For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
24
#define VPX_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
25
#endif
26
#endif
27
28
// Returns 0 in case of overflow of nmemb * size.
29
41.6M
static int check_size_argument_overflow(uint64_t nmemb, uint64_t size) {
30
41.6M
  const uint64_t total_size = nmemb * size;
31
41.6M
  if (nmemb == 0) return 1;
32
41.6M
  if (size > VPX_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
33
41.6M
  if (total_size != (size_t)total_size) return 0;
34
35
41.6M
  return 1;
36
41.6M
}
37
38
80.5M
static size_t *get_malloc_address_location(void *const mem) {
39
80.5M
  return ((size_t *)mem) - 1;
40
80.5M
}
41
42
40.2M
static uint64_t get_aligned_malloc_size(size_t size, size_t align) {
43
40.2M
  return (uint64_t)size + align - 1 + ADDRESS_STORAGE_SIZE;
44
40.2M
}
45
46
static void set_actual_malloc_address(void *const mem,
47
40.2M
                                      const void *const malloc_addr) {
48
40.2M
  size_t *const malloc_addr_location = get_malloc_address_location(mem);
49
40.2M
  *malloc_addr_location = (size_t)malloc_addr;
50
40.2M
}
51
52
40.2M
static void *get_actual_malloc_address(void *const mem) {
53
40.2M
  size_t *const malloc_addr_location = get_malloc_address_location(mem);
54
40.2M
  return (void *)(*malloc_addr_location);
55
40.2M
}
56
57
40.2M
void *vpx_memalign(size_t align, size_t size) {
58
40.2M
  void *x = NULL, *addr;
59
40.2M
  const uint64_t aligned_size = get_aligned_malloc_size(size, align);
60
40.2M
  if (!check_size_argument_overflow(1, aligned_size)) return NULL;
61
62
40.2M
  addr = malloc((size_t)aligned_size);
63
40.2M
  if (addr) {
64
40.2M
    x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
65
40.2M
    set_actual_malloc_address(x, addr);
66
40.2M
  }
67
40.2M
  return x;
68
40.2M
}
69
70
1.35M
void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
71
72
1.34M
void *vpx_calloc(size_t num, size_t size) {
73
1.34M
  void *x;
74
1.34M
  if (!check_size_argument_overflow(num, size)) return NULL;
75
76
1.34M
  x = vpx_malloc(num * size);
77
1.34M
  if (x) memset(x, 0, num * size);
78
1.34M
  return x;
79
1.34M
}
80
81
54.9M
void vpx_free(void *memblk) {
82
54.9M
  if (memblk) {
83
40.2M
    void *addr = get_actual_malloc_address(memblk);
84
40.2M
    free(addr);
85
40.2M
  }
86
54.9M
}