Skip to content

Commit cf57708

Browse files
authored
Merge pull request #1 from ShadiBahaa/codex/conduct-code-review-for-repository
Use TOPDIR for recipe paths and harden HMM allocator (bounds, overflow, flags)
2 parents 7ec9ffc + 5e90eef commit cf57708

7 files changed

Lines changed: 58 additions & 43 deletions

File tree

meta-dynamic/recipes-dynamic/build-dynamic/build-dynamic_0.1.bb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
DESCRIPTION = "Building the HMM library dynamically"
22
PR = "r1"
33
do_build () {
4-
script_dir="$(dirname "$(realpath "$0")")"
5-
6-
local dir="$script_dir/../../../../../project-files"
4+
local dir="${TOPDIR}/../project-files"
75

86
if [ ! -d "$dir" ]; then
97
echo "Directory '$dir' does not exist."

meta-static/recipes-static/build-static/build-static_0.1.bb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
DESCRIPTION = "Building the HMM library statically"
22
PR = "r1"
33
do_build () {
4-
log_dir="$(dirname "$(realpath "$0")")"
5-
6-
local dir="$log_dir/../../../../../project-files"
4+
local dir="${TOPDIR}/../project-files"
75

86
if [ ! -d "$dir" ]; then
97
echo "Directory '$dir' does not exist."

meta-test/recipes-test/test-dynamic/test-dynamic_1.0.bb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ PR = "r1"
33

44
do_build() {
55
# Set the paths to the files
6-
log_dir="$(dirname "$(realpath "$0")")"
7-
local test_c_path="$log_dir/../../../../../project-files/test.c"
8-
local lib_dir_path="$log_dir/../../../../../project-files"
9-
local run_script_path="$log_dir/../../../../../project-files/run_executable.sh"
10-
local output_executable="$log_dir/../../../../../project-files/test.exe"
6+
local project_dir="${TOPDIR}/../project-files"
7+
local test_c_path="$project_dir/test.c"
8+
local lib_dir_path="$project_dir"
9+
local run_script_path="$project_dir/run_executable.sh"
10+
local output_executable="$project_dir/test.exe"
1111

1212
# Compile the test.c with libhmm.a into test.exe statically
1313
gcc -o "$output_executable" "$test_c_path" -L"$lib_dir_path" -lhmm
@@ -27,4 +27,3 @@ do_build() {
2727
return 1
2828
fi
2929
}
30-

meta-test/recipes-test/test-static/test-static_1.0.bb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ PR = "r1"
33

44
do_build() {
55
# Set the paths to the files
6-
log_dir="$(dirname "$(realpath "$0")")"
7-
local test_c_path="$log_dir/../../../../../project-files/test.c"
8-
local libhmm_path="$log_dir/../../../../../project-files/libhmm.a"
9-
local run_script_path="$log_dir/../../../../../project-files/run_executable.sh"
10-
local output_executable="$log_dir/../../../../../project-files/test.exe"
6+
local project_dir="${TOPDIR}/../project-files"
7+
local test_c_path="$project_dir/test.c"
8+
local libhmm_path="$project_dir/libhmm.a"
9+
local run_script_path="$project_dir/run_executable.sh"
10+
local output_executable="$project_dir/test.exe"
1111

1212
# Compile the test.c with libhmm.a into test.exe statically
1313
gcc "$test_c_path" "$libhmm_path" -o "$output_executable" --static
@@ -24,4 +24,3 @@ do_build() {
2424
return 1
2525
fi
2626
}
27-

project-files/HMM.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ static mem_chunk_t *tail;
5858

5959
static size_t current_free_size;
6060

61+
/**< Start and end addresses of memory acquired from sbrk. */
62+
static void *heap_start;
63+
static void *heap_end;
64+
6165
/**
6266
* @brief Removes a free memory block from the list of free blocks in hash table.
6367
*
@@ -73,6 +77,8 @@ static void HMMremove_free_block(mem_chunk_t *block)
7377
return;
7478
}
7579
size_t cur_size = block->size;
80+
if (cur_size < ALIGNMENT)
81+
return;
7682
size_t idx = (cur_size / ALIGNMENT) - 1;
7783
// If the index is within bounds, traverse the free list to remove the block.
7884

@@ -102,16 +108,6 @@ static void HMMremove_free_block(mem_chunk_t *block)
102108
}
103109
}
104110
}
105-
/**
106-
* @brief Checks if a memory block is found in the list of free blocks in hash table.
107-
*
108-
* @param block Pointer to the memory block to be checked.
109-
* @return 1 if the block is found, 0 otherwise.
110-
*/
111-
static unsigned char HMMis_block_found(mem_chunk_t *block)
112-
{
113-
return block->is_added;
114-
}
115111
/**
116112
* @brief Adds a free memory block to the list of free blocks in hash table.
117113
*
@@ -123,6 +119,8 @@ static void HMMadd_free_block(mem_chunk_t *block)
123119
return;
124120
// Calculate the index for the block based on its size.
125121
size_t cur_size = block->size;
122+
if (cur_size < ALIGNMENT)
123+
return;
126124
size_t idx = (cur_size / ALIGNMENT) - 1;
127125
// If the index is within bounds and the block is not already added, add it to the free list.
128126

@@ -152,6 +150,8 @@ static void HMMadd_free_block(mem_chunk_t *block)
152150
*/
153151
static mem_chunk_t *HMMget_free_block(size_t size)
154152
{
153+
if (size < ALIGNMENT)
154+
return NULL;
155155
size_t idx = (size / ALIGNMENT) - 1;
156156
if (idx < MULTIPLES_MAX)
157157
{
@@ -247,6 +247,7 @@ static mem_chunk_t *HMMget_free_chunk(size_t size)
247247
splitted->prev = current;
248248
splitted->next = current_next;
249249
splitted->is_free = 1;
250+
splitted->is_added = 0;
250251
if (current_next)
251252
current_next->prev = splitted;
252253
current->next = splitted;
@@ -270,7 +271,12 @@ static mem_chunk_t *HMMget_free_chunk(size_t size)
270271
// If no free block is large enough, allocate new memory from the system.
271272

272273
size_t allocation_size = ALLOCATED_BYTES;
273-
size_t num_allocated_bytes = ((size + sizeof(mem_chunk_t) + allocation_size) / allocation_size) * allocation_size;
274+
if (size > (SIZE_MAX - sizeof(mem_chunk_t) - allocation_size))
275+
{
276+
return NULL;
277+
}
278+
size_t total_requested = size + sizeof(mem_chunk_t) + allocation_size;
279+
size_t num_allocated_bytes = (total_requested / allocation_size) * allocation_size;
274280
void *new_free_space = sbrk(num_allocated_bytes);
275281
if (new_free_space == (void *)-1)
276282
{
@@ -289,6 +295,10 @@ static mem_chunk_t *HMMget_free_chunk(size_t size)
289295
// Initialize a new memory chunk and add it to the list.
290296

291297
mem_chunk_t *new_chunk = (mem_chunk_t *)new_free_space;
298+
if (heap_start == NULL)
299+
heap_start = new_free_space;
300+
heap_end = (unsigned char *)new_free_space + num_allocated_bytes;
301+
new_chunk->is_added = 0;
292302
new_chunk->is_free = 1;
293303
new_chunk->size = num_allocated_bytes - sizeof(mem_chunk_t);
294304
new_chunk->prev = tail;
@@ -315,6 +325,15 @@ static void HMMfree(void *ptr)
315325
}
316326
// Get the memory chunk from the given pointer.
317327

328+
if (heap_start == NULL || heap_end == NULL)
329+
{
330+
return;
331+
}
332+
if ((unsigned char *)ptr < ((unsigned char *)heap_start + sizeof(mem_chunk_t)) ||
333+
(unsigned char *)ptr >= (unsigned char *)heap_end)
334+
{
335+
return;
336+
}
318337
mem_chunk_t *alloacted_member = (mem_chunk_t *)((unsigned char *)ptr - sizeof(mem_chunk_t));
319338
// Check if the memory is already free, if not, free it.
320339

@@ -378,6 +397,7 @@ static void HMMfree(void *ptr)
378397
{
379398
return;
380399
}
400+
heap_end = new_break;
381401
}
382402
}
383403
}
@@ -536,8 +556,8 @@ void HMMtraverse(void)
536556
size_t cnt = 1;
537557
while (cur)
538558
{
539-
printf("Node number: %d, Address: %10p, free: %lu, size: %lu\r\n", cnt, cur, cur->is_free, cur->size);
559+
printf("Node number: %zu, Address: %10p, free: %u, size: %zu\r\n", cnt, (void *)cur, (unsigned int)cur->is_free, cur->size);
540560
cnt++;
541561
cur = cur->next;
542562
}
543-
}
563+
}

project-files/free_and_sbrk.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <time.h>
4-
#include <string.h>
5-
#include <math.h>
6-
#define _BSD_SOURCE
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
#include <string.h>
5+
#include <math.h>
6+
#include <unistd.h>
77

88
int min(int a, int b){
99
if (a < b)return a;
1010
return b;
1111
}
1212

1313
#define MAX_ALLOCS 10000
14-
int
15-
main(int argc, char *argv[])
16-
{
17-
srand(time(NULL));
14+
int
15+
main(int argc, char *argv[])
16+
{
17+
(void)argc;
18+
(void)argv;
19+
srand(time(NULL));
1820
char *ptr[MAX_ALLOCS];
1921
int freeStep, freeMin, freeMax, blockSize, numAllocs, j;
2022

@@ -50,4 +52,4 @@ main(int argc, char *argv[])
5052
printf("After free(), program break is: %10p\n", sbrk(0));
5153

5254
exit(EXIT_SUCCESS);
53-
}
55+
}

project-files/test.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ void perform_random_operations()
3737
case CALLOC:
3838
{
3939
size_t num = rand() % MAX_SIZE + 1;
40-
size_t size = rand() % MAX_SIZE + 1;
4140
allocated_blocks[i] = calloc(num, MAX_SIZE/num);
4241
//printf("Allocated block %p with %zu elements of size %zu\n", allocated_blocks[i], num, size);
4342
break;

0 commit comments

Comments
 (0)