diff --git a/InOsEmuPkg/Include/Protocol/EmuThunk.h b/InOsEmuPkg/Include/Protocol/EmuThunk.h index ed4df3a942..6c1190c141 100644 --- a/InOsEmuPkg/Include/Protocol/EmuThunk.h +++ b/InOsEmuPkg/Include/Protocol/EmuThunk.h @@ -78,6 +78,18 @@ BOOLEAN ); +typedef +VOID * +(EFIAPI *EMU_OS_MALLOC) ( + IN UINTN Size + ); + +typedef +VOID +(EFIAPI *EMU_OS_FREE) ( + IN VOID *Ptr + ); + typedef EFI_STATUS @@ -205,6 +217,13 @@ struct _EMU_THUNK_PROTOCOL { EMU_READ_STD_IN ReadStdIn; EMU_POLL_STD_IN PollStdIn; + // + // Map OS malloc/free so we can use OS based guard malloc + // + EMU_OS_MALLOC Malloc; + EMU_OS_FREE Free; + + /// /// PE/COFF loader hooks to get symbols loaded /// diff --git a/InOsEmuPkg/Unix/Sec/EmuThunk.c b/InOsEmuPkg/Unix/Sec/EmuThunk.c index 97c339d241..50afbb5cd6 100644 --- a/InOsEmuPkg/Unix/Sec/EmuThunk.c +++ b/InOsEmuPkg/Unix/Sec/EmuThunk.c @@ -118,6 +118,23 @@ SecPollStdIn ( } +VOID * +SecMalloc ( + IN UINTN Size + ) +{ + return malloc ((size_t)Size); +} + +VOID +SecFree ( + IN VOID *Ptr + ) +{ + free (Ptr); + return; +} + void settimer_handler (int sig) @@ -370,6 +387,8 @@ EMU_THUNK_PROTOCOL gEmuThunkProtocol = { GasketSecWriteStdOut, GasketSecReadStdIn, GasketSecPollStdIn, + GasketSecMalloc, + GasketSecFree, GasketSecPeCoffGetEntryPoint, GasketSecPeCoffRelocateImageExtraAction, GasketSecPeCoffUnloadImageExtraAction, diff --git a/InOsEmuPkg/Unix/Sec/Gasket.h b/InOsEmuPkg/Unix/Sec/Gasket.h index 725e250eb3..90c2aa8ee7 100644 --- a/InOsEmuPkg/Unix/Sec/Gasket.h +++ b/InOsEmuPkg/Unix/Sec/Gasket.h @@ -53,6 +53,16 @@ GasketSecPollStdIn ( VOID ); +VOID * +EFIAPI +GasketSecMalloc ( + IN UINTN Size + ); + +VOID +GasketSecFree ( + IN VOID *Ptr + ); RETURN_STATUS diff --git a/InOsEmuPkg/Unix/Sec/Ia32/Gasket.S b/InOsEmuPkg/Unix/Sec/Ia32/Gasket.S index f1b974e18a..b8d9c0470f 100644 --- a/InOsEmuPkg/Unix/Sec/Ia32/Gasket.S +++ b/InOsEmuPkg/Unix/Sec/Ia32/Gasket.S @@ -107,6 +107,34 @@ ASM_PFX(GasketSecPollStdIn): leave ret +ASM_GLOBAL ASM_PFX(GasketSecMalloc) +ASM_PFX(GasketSecMalloc): + pushl %ebp + movl %esp, %ebp + subl $24, %esp // sub extra 16 from the stack for alignment + and $-16, %esp // stack needs to end in 0xFFFFFFF0 before call + movl 8(%ebp), %eax + movl %eax, (%esp) + + call ASM_PFX(SecMalloc) + + leave + ret + +ASM_GLOBAL ASM_PFX(GasketSecFree) +ASM_PFX(GasketSecFree): + pushl %ebp + movl %esp, %ebp + subl $24, %esp // sub extra 16 from the stack for alignment + and $-16, %esp // stack needs to end in 0xFFFFFFF0 before call + movl 8(%ebp), %eax + movl %eax, (%esp) + + call ASM_PFX(SecFree) + + leave + ret + ASM_GLOBAL ASM_PFX(GasketSecSetTimer) ASM_PFX(GasketSecSetTimer): diff --git a/InOsEmuPkg/Unix/Sec/X64/Gasket.S b/InOsEmuPkg/Unix/Sec/X64/Gasket.S index d30aed617b..6b5e782413 100644 --- a/InOsEmuPkg/Unix/Sec/X64/Gasket.S +++ b/InOsEmuPkg/Unix/Sec/X64/Gasket.S @@ -126,6 +126,34 @@ ASM_PFX(GasketSecPollStdIn): popq %rbp ret +ASM_GLOBAL ASM_PFX(GasketSecMalloc) +ASM_PFX(GasketSecMalloc): + pushq %rbp // stack frame is for the debugger + movq %rsp, %rbp + + pushq %rsi // %rsi & %rdi are volatile in Unix and callee-save in EFI ABI + + call ASM_PFX(SecMalloc) + + popq %rdi // restore state + popq %rsi + popq %rbp + ret + +ASM_GLOBAL ASM_PFX(GasketSecFree) +ASM_PFX(GasketSecFree): + pushq %rbp // stack frame is for the debugger + movq %rsp, %rbp + + pushq %rsi // %rsi & %rdi are volatile in Unix and callee-save in EFI ABI + + call ASM_PFX(SecFree) + + popq %rdi // restore state + popq %rsi + popq %rbp + ret + ASM_GLOBAL ASM_PFX(GasketSecSetTimer) ASM_PFX(GasketSecSetTimer):