I'm writing an XS module and Perl seems to be rewriting my C standard library from Visual Studio CRT on win32. malloc() free() and exit() are being redefined to perl specific versions, which can not work inside the thread. Why is Perl rewriting my C standard library functions? I know Perl has its internal C library from
perlclib, so why are my C library's functions being redefined? Perl functions never work outside the Perl thread (no "context"), so the moment I try using a C library function in the other thread, they crash. The free() will always crash. The code sample is not compilable. Its shortened from something much bigger.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
DWORD WINAPI ThreadFunc ( HANDLE * eventPtr) {
DWORD retval;
retval = WaitForSingleObject(*eventPtr, 10000);
if(retval == WAIT_OBJECT_0) {
printf("wait suceeded");
}
else if(retval == WAIT_TIMEOUT){
//crash gracefully
printf("Timeout, now exiting"); exit(1);}
}
//crash gracefully
else {printf("WaitForSingleObject failed"); exit(1);}
if(!CloseHandle(*eventPtr) {
//crash gracefully
printf("CloseHandle failed error is %u\n", GetLastError());
+exit(1);
}
free(eventPtr);
return 1;
}
MODULE = MyMod PACKAGE = MyMod
PROTOTYPES: DISABLE
DWORD
perlfunc ()
PREINIT:
HANDLE * evtHandlePtr;
HANDLE thHandle;
CODE:
evtHandlePtr = malloc(sizeof(HANDLE));
*evtHandlePtr = CreateEvent(NULL, TRUE, FALSE, NULL);
if(*evtHandlePtr == NULL){printf("CreateEvent failed error is %u\n",Ge
+tLastError()); exit(1);}
thHandle = CreateThread(NULL, 1024, ThreadFunc, evtHandlePtr, 0, NULL)
+;
if(thHandle == NULL){printf("CreateThread failed error is %u\n",GetLas
+tError()); exit(1);}
else {
if (!CloseHandle(thHandle))
{printf("CloseHandle on thread handle failed error is %u\n",GetLa
+stError()); exit(1);}
}
//lets time out, so no SetEvent
RETVAL = 1;
OUTPUT:
RETVAL
"exit(1);" becomes "(*(*Perl_IProc_ptr(((PerlInterpreter *)Perl_get_context())))->pExit)((*Perl_IProc_ptr(((PerlInterpreter *)Perl_get_context()))), (1));" everywhere
printf stays the same everywhere
"free(eventPtr);" becomes "(*(*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context())))->pFree)((*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context()))), (eventPtr));"
"evtHandlePtr = malloc(sizeof(HANDLE));" becomes
"evtHandlePtr = (*(*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context())))->pMalloc)((*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context()))), (sizeof(HANDLE)));"
I need to be able to allocate memory inside the XS func, then eventually free it from the thread. The ultimate goal is a timeout feature that creates a thread to cancel a blocking function that is called in the XS func. The full timeout code inside the thread crashes on the free() always. The blocking function sometimes might never return hanging forever due to bugs in the that closed source library. The developer of the library suggests making another thread to end the job if a timeout period elapses without the blocking function returning (and then my XS function telling the timeout thread not to kill the job through a Win32 Event object).
But how can I use my native OS (windows)'s C library when Perl is redefining it?
The exit(1)s are supposed to be basically graceful crashes, but they would crash really hard if they ran inside the thread since perl doesn't exist inside that thread. I'm not sure if Perl's exit replacement will continue to execute destroyers and more perl code or not.
Also this closed source library I'm trying to wrap into XS, has errata that it will overrun the buffer you gave it in rare cases, and it returns an error code that it overran its buffer and you need to exit now since your stack and/or heap are corrupted.
How can I get a hard exit that is guaranteed not to run any more Perl code, or is basically a kill on the process?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.