Skip to content

Commit 550668f

Browse files
memory safety stuff
1 parent 1d446e8 commit 550668f

14 files changed

Lines changed: 197 additions & 71 deletions

File tree

include/e1000.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ void e1000_get_mac_addr(uint8_t* src_mac_addr);
157157

158158
bool e1000_send_packet(void * p_data, uint16_t p_len);
159159

160-
bool init_e1000();
160+
void init_e1000();

include/kmalloc.h

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,120 @@
1212

1313
#include <stddef.h>
1414

15-
/* Initialize the heap system */
16-
void init_heap();
17-
void print_heapinfo();
15+
/**
16+
* @brief Initialise the kernel heap.
17+
*
18+
* Sets up internal data structures required for dynamic memory allocation.
19+
* Should be called early during kernel initialisation.
20+
*/
21+
void init_heap(void);
22+
23+
/**
24+
* @brief Print diagnostic heap information to the terminal.
25+
*
26+
* Outputs allocator statistics such as used, free, and total heap space.
27+
* Primarily for debugging and diagnostics.
28+
*/
29+
void print_heapinfo(void);
1830

19-
/* Core alloc/free */
31+
/**
32+
* @brief Allocate memory from the kernel heap.
33+
*
34+
* @param size Number of bytes to allocate.
35+
* @return Pointer to allocated memory, or NULL if allocation fails.
36+
*/
2037
void* kmalloc(uint64_t size);
38+
39+
/**
40+
* @brief Free previously allocated memory.
41+
*
42+
* @param addr Pointer previously returned by kmalloc/kcalloc/krealloc.
43+
*/
2144
void kfree(const void* addr);
2245

23-
/* calloc/realloc helpers */
46+
/**
47+
* @brief Allocate zero-initialised memory.
48+
*
49+
* @param num Number of elements.
50+
* @param size Size of each element in bytes.
51+
* @return Pointer to allocated memory, or NULL if allocation fails.
52+
*/
2453
void* kcalloc(size_t num, size_t size);
54+
55+
/**
56+
* @brief Resize previously allocated memory block.
57+
*
58+
* Preserves contents up to the lesser of old and new sizes.
59+
*
60+
* @param ptr Pointer to memory allocated by kmalloc/kcalloc/krealloc.
61+
* @param new_size New size in bytes.
62+
* @return Pointer to reallocated memory, or NULL if resizing fails.
63+
*/
2564
void* krealloc(void* ptr, size_t new_size);
2665

27-
/* Low-memory special allocator: for <4GB identity mapped area */
66+
/**
67+
* @brief Allocate memory from low (identity-mapped) memory under 4GB.
68+
*
69+
* Intended for DMA buffers or page tables that require low physical addresses.
70+
*
71+
* @param size Number of bytes to allocate.
72+
* @return Physical address of the allocated block (below 4GB).
73+
*/
2874
uint32_t kmalloc_low(uint32_t size);
29-
void kfree_low(uint32_t addr); /* New: explicitly free low memory if needed */
3075

31-
/* Debug/statistics */
32-
uint64_t get_free_memory();
33-
uint64_t get_used_memory();
34-
uint64_t get_total_memory();
76+
/**
77+
* @brief Free a low-memory allocation made with kmalloc_low().
78+
*
79+
* Only needed if low memory is explicitly managed and reused.
80+
*
81+
* @param addr Physical address of the block to free.
82+
*/
83+
void kfree_low(uint32_t addr);
84+
85+
/**
86+
* @brief Get total amount of free memory.
87+
*
88+
* @return Free heap space in bytes.
89+
*/
90+
uint64_t get_free_memory(void);
91+
92+
/**
93+
* @brief Get total amount of used memory.
94+
*
95+
* @return Used heap space in bytes.
96+
*/
97+
uint64_t get_used_memory(void);
98+
99+
/**
100+
* @brief Get total available memory managed by the kernel heap.
101+
*
102+
* @return Total heap size in bytes.
103+
*/
104+
uint64_t get_total_memory(void);
105+
106+
/**
107+
* @brief Halt the system with a preboot failure message.
108+
*
109+
* Used when a critical failure occurs before full initialisation.
110+
*
111+
* @param msg Null-terminated string describing the failure.
112+
*/
35113
void preboot_fail(const char* msg);
36114

115+
/**
116+
* @brief Free memory and set the pointer to NULL.
117+
*
118+
* This macro safely frees a dynamically allocated pointer and sets it to NULL
119+
* to avoid dangling references. It must be passed the address of the pointer
120+
* (i.e., a pointer to the pointer).
121+
*
122+
* Example usage:
123+
* @code
124+
* void* ptr = kmalloc(128);
125+
* kfree_null(&ptr); // ptr is now NULL
126+
* @endcode
127+
*
128+
* @param p A pointer to the pointer to be freed. Must not be NULL.
129+
*/
130+
#define kfree_null(p) do { kfree(*(p)); *(p) = NULL; } while (0)
131+

include/rtl8139.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ void rtl8139_handler(uint8_t isr, uint64_t error, uint64_t irq, void* opaque);
184184
* @brief Initialise RTL8139
185185
*
186186
* @return true card detected and initialised
187-
* @return false card not detected, or would not reset
188187
*/
189-
bool init_rtl8139();
188+
void init_rtl8139();
190189

191190
/**
192191
* @brief Get the MAC address of the card in display format

include/string.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
#include <stdint.h>
88
#include <stdbool.h>
99

10-
struct gc_str
10+
typedef struct gc_str
1111
{
1212
const char* ptr;
13-
struct gc_str* next;
14-
};
13+
struct gc_str_t* next;
14+
} gc_str_t;
1515

1616
unsigned int strlen(const char* str);
1717

src/arp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void arp_send_packet(uint8_t* dst_hardware_addr, uint8_t* dst_protocol_addr) {
101101

102102
ethernet_send_packet(broadcast_mac_address, (uint8_t*)arp_packet, sizeof(arp_packet_t), ETHERNET_TYPE_ARP);
103103

104-
kfree(arp_packet);
104+
kfree_null(&arp_packet);
105105
}
106106

107107
void arp_prediscover(uint8_t* protocol_addr) {

src/dhcp.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ void dhcp_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, v
77
if (*type == DHCPOFFER) {
88
uint32_t* server_ip = get_dhcp_options(packet, OPT_SERVER_IP);
99
if (!server_ip) {
10-
kfree(type);
11-
kfree(server_ip);
10+
kfree_null(&type);
11+
kfree_null(&server_ip);
1212
return;
1313
}
1414
dhcp_request((uint8_t*)&packet->your_ip, packet->xid, *server_ip);
15-
kfree(server_ip);
15+
kfree_null(&server_ip);
1616
} else if (*type == DHCPACK) {
1717
sethostaddr((const unsigned char*)&packet->your_ip);
1818
uint32_t* dns = get_dhcp_options(packet, OPT_DNS);
@@ -22,25 +22,25 @@ void dhcp_handle_packet(uint32_t src_ip, uint16_t src_port, uint16_t dst_port, v
2222
if (subnet) {
2323
setnetmask(*subnet);
2424
get_ip_str(ip, (uint8_t*)subnet);
25-
kfree(subnet);
25+
kfree_null(&subnet);
2626
dprintf("DHCP: Received subnet mask: %s\n", ip);
2727
}
2828
if (dns) {
2929
setdnsaddr(*dns);
3030
get_ip_str(ip, (uint8_t*)dns);
31-
kfree(dns);
31+
kfree_null(&dns);
3232
dprintf("DHCP: Received DNS address: %s\n", ip);
3333
}
3434
if (gateway) {
3535
setgatewayaddr(*gateway);
3636
get_ip_str(ip, (uint8_t*)gateway);
37-
kfree(gateway);
37+
kfree_null(&gateway);
3838
dprintf("DHCP: Received gateway address: %s\n", ip);
3939
}
4040
} else if (*type == DHCPNAK) {
4141
/* Negative ack, to be implemented */
4242
}
43-
kfree(type);
43+
kfree_null(&type);
4444
}
4545
}
4646

@@ -53,7 +53,7 @@ void dhcp_discover() {
5353
memset(packet, 0, sizeof(dhcp_packet_t));
5454
uint16_t optsize = make_dhcp_packet(packet, DHCPDISCOVER, request_ip, DHCP_TRANSACTION_IDENTIFIER, 0);
5555
udp_send_packet(dst_ip, DHCP_DST_PORT, DHCP_SRC_PORT, packet, optsize);
56-
kfree(packet);
56+
kfree_null(&packet);
5757
}
5858

5959
void dhcp_request(uint8_t* request_ip, uint32_t xid, uint32_t server_ip) {
@@ -62,7 +62,7 @@ void dhcp_request(uint8_t* request_ip, uint32_t xid, uint32_t server_ip) {
6262
memset(packet, 0, sizeof(dhcp_packet_t));
6363
uint16_t optsize = make_dhcp_packet(packet, DHCPREQUEST, request_ip, xid, server_ip);
6464
udp_send_packet(dst_ip, DHCP_DST_PORT, DHCP_SRC_PORT, packet, optsize);
65-
kfree(packet);
65+
kfree_null(&packet);
6666
}
6767

6868
void* get_dhcp_options(dhcp_packet_t* packet, uint8_t type) {
@@ -73,7 +73,9 @@ void* get_dhcp_options(dhcp_packet_t* packet, uint8_t type) {
7373
uint8_t len = *(options + 1);
7474
if (curr_type == type) {
7575
void* ret = kmalloc(len);
76-
memcpy(ret, options + 2, len);
76+
if (ret) {
77+
memcpy(ret, options + 2, len);
78+
}
7779
return ret;
7880
}
7981
options += (2 + len);

src/e1000.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ bool e1000_start(pci_dev_t *pci_device) {
372372
return true;
373373
}
374374

375-
bool init_e1000() {
375+
void init_e1000() {
376376
pci_dev_t pci_device;
377377
bool found = false;
378378

@@ -386,7 +386,7 @@ bool init_e1000() {
386386
}
387387
}
388388
if (!found) {
389-
return false;
389+
return;
390390
}
391391
uint32_t ret = pci_read(pci_device, PCI_BAR0);
392392
bar_type = pci_bar_type(ret);
@@ -398,5 +398,5 @@ bool init_e1000() {
398398
pci_bus_master(pci_device);
399399
eerprom_exists = false;
400400

401-
return e1000_start(&pci_device);
401+
e1000_start(&pci_device);
402402
}

src/ethernet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int ethernet_send_packet(uint8_t* dst_mac_addr, uint8_t* data, int len, uint16_t
1919
frame->type = htons(protocol);
2020
dprintf("ethernet_send_packet frame=%08x\n", frame);
2121
dev->send_packet(frame, sizeof(ethernet_frame_t) + len);
22-
kfree(frame);
22+
kfree_null(&frame);
2323
return len;
2424
}
2525

src/init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ init_func_t init_funcs[] = {
77
init_cores, init_idt, init_pci, init_realtime_clock,
88
init_devicenames, init_keyboard, init_ide, init_ahci,
99
init_filesystem, init_iso9660, init_devfs, init_fat32,
10+
init_rtl8139, init_e1000,
1011
NULL,
1112
};
1213

@@ -15,6 +16,7 @@ char* init_funcs_names[] = {
1516
"idt", "pci", "clock",
1617
"devicenames", "keyboard", "ide", "ahci",
1718
"filesystem", "iso9660", "devfs", "fat32",
19+
"rtl8139", "e1000",
1820
NULL,
1921
};
2022

0 commit comments

Comments
 (0)