From 9a528ba3c73145a42b1f2d689d4fba8941cecee3 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Tue, 20 Oct 2026 19:32:56 +0100 Subject: [PATCH] Fixed MZ executable memory allocation during EXEC --- interrupts.c | 10 +++++++--- loader.c | 14 ++++++++++++-- loader.h | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/interrupts.c b/interrupts.c index 26f0aaa..7ee3b05 100644 --- a/interrupts.c +++ b/interrupts.c @@ -7578,7 +7578,7 @@ static void int_21 (CPU8086 *cpu) { char name[260], path[520], args[128]; - uint16_t paragraphs, child_psp, source_env, env_size; + uint16_t paragraphs, max_paragraphs, child_psp, source_env, env_size; uint16_t command_off, command_seg; uint16_t tail_length; @@ -7602,7 +7602,7 @@ static void int_21 (CPU8086 *cpu) { dos_read_asciiz (cpu, cpu->ds, cpu->regs[REG_DX], name, sizeof (name)); dos_resolve_path (name, path, sizeof (path)); - if (!dos_executable_memory (path, ¶graphs, &is_mz)) { + if (!dos_executable_memory (path, ¶graphs, &max_paragraphs, &is_mz)) { dos_set_error (cpu, 0x0002); break; @@ -7622,7 +7622,7 @@ static void int_21 (CPU8086 *cpu) { } - if (!is_mz) { + if (!is_mz || max_paragraphs > paragraphs) { uint16_t mcb_seg = 0x004F; uint16_t largest = 0; @@ -7651,6 +7651,10 @@ static void int_21 (CPU8086 *cpu) { } paragraphs = (uint16_t) (largest - env_size - 1); + + if (is_mz && paragraphs > max_paragraphs) { + paragraphs = max_paragraphs; + } } diff --git a/loader.c b/loader.c index b9d75d9..cb2c8b6 100644 --- a/loader.c +++ b/loader.c @@ -81,7 +81,7 @@ typedef struct __attribute__ ((packed)) { } MZHeader; -int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) { +int dos_executable_memory (const char *path, uint16_t *paragraphs, uint16_t *max_paragraphs, int *is_mz) { FILE *fp; MZHeader hdr; @@ -92,9 +92,11 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) { uint32_t total_size; uint32_t image_start; uint32_t image_size; + uint32_t required; + uint32_t maximum; - if (path == NULL || paragraphs == NULL || is_mz == NULL) { + if (path == NULL || paragraphs == NULL || max_paragraphs == NULL || is_mz == NULL) { return 0; } @@ -128,7 +130,13 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) { } image_size = total_size - image_start; + required = 0x10 + (image_size + 15) / 16 + hdr.min_alloc; + maximum = 0x10 + (image_size + 15) / 16 + hdr.max_alloc; + + if (maximum < required) { + maximum = required; + } } else { @@ -138,6 +146,7 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) { /* A COM process needs a PSP and the conventional 64 KiB segment. */ required = 0x1000; + maximum = 0xFFFF; } @@ -145,6 +154,7 @@ int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz) { return 0; } + *max_paragraphs = (uint16_t) (maximum > 0xFFFF ? 0xFFFF : maximum); *paragraphs = (uint16_t) required; *is_mz = mz; diff --git a/loader.h b/loader.h index d7e9e9b..e23ba4c 100644 --- a/loader.h +++ b/loader.h @@ -9,7 +9,7 @@ #include "cpu.h" -int dos_executable_memory (const char *path, uint16_t *paragraphs, int *is_mz); +int dos_executable_memory (const char *path, uint16_t *paragraphs, uint16_t *max_paragraphs, int *is_mz); int load_com_file (const char *path, CPU8086 *cpu, uint8_t *mem, size_t mem_size, uint16_t load_addr, size_t *out_size); int load_mz_file (const char *path, CPU8086 *cpu, uint16_t base_seg, uint16_t available_paragraphs); -- 2.34.1