Subject very strange behavior with UDF ...
Author Vander Clock Stephane
hello,

when we install Firebird 2.5 (super classic, win64) we start to notice
that the server was crashing too often. this happen only when the server
is higly loaded (lot of concurrent users doing some update). so we
decide to look in the debug error log and i m very surprised by what i see :

(f80.ccc): Access violation - code c0000005 (first chance)
FirstChance_av_AccessViolation

Call stack below ---
# Child-SP RetAddr : Args to
Child : Call Site
00 00000000`63dadc60 00000000`774fce82 : 00000000`00000000
00000000`160e3b78 00000000`63daf7b0 00000000`3262ecf0 :
ntdll!CsrAllocateMessagePointer+0x1f4
*** WARNING: Unable to verify timestamp for C:\Program
Files\Firebird\UDF\aludf.DLL
*** ERROR: Symbol file could not be found. Defaulted to export symbols
for C:\Program Files\Firebird\UDF\aludf.DLL -
01 00000000`63dadd10 00000000`3261d196 : 00000000`63dadfe0
00000000`522f8010 00000000`63dae5d0 00000000`001f7b68 :
ntdll!CsrAllocateMessagePointer+0x122
02 00000000`63dadd40 00000000`63dadfe0 : 00000000`522f8010
00000000`63dae5d0 00000000`001f7b68 00000000`001f7f10 :
aludf!aludf_stringreplace+0xaf56
03 00000000`63dadd48 00000000`522f8010 : 00000000`63dae5d0
00000000`001f7b68 00000000`001f7f10 00000000`002d0e48 : 0x63dadfe0
04 00000000`63dadd50 00000000`63dae5d0 : 00000000`001f7b68
00000000`001f7f10 00000000`002d0e48 00000000`522fb918 : 0x522f8010
05 00000000`63dadd58 00000000`001f7b68 : 00000000`001f7f10
00000000`002d0e48 00000000`522fb918 00000000`160e3c00 : 0x63dae5d0
06 00000000`63dadd60 00000000`001f7f10 : 00000000`002d0e48
00000000`522fb918 00000000`160e3c00 00000000`3262ecf0 : 0x1f7b68
07 00000000`63dadd68 00000000`002d0e48 : 00000000`522fb918
00000000`160e3c00 00000000`3262ecf0 00000000`00000000 : 0x1f7f10
08 00000000`63dadd70 00000000`522fb918 : 00000000`160e3c00
00000000`3262ecf0 00000000`00000000 00000000`775c7288 : 0x2d0e48
09 00000000`63dadd78 00000000`160e3c00 : 00000000`3262ecf0
00000000`00000000 00000000`775c7288 00000000`00000000 : 0x522fb918
0a 00000000`63dadd80 00000000`3262ecf0 : 00000000`00000000
00000000`775c7288 00000000`00000000 00000000`00000030 : 0x160e3c00
0b 00000000`63dadd88 00000000`00000000 : 00000000`775c7288
00000000`00000000 00000000`00000030 00000000`001f0000 :
aludf!aludf_stringreplace+0x1cab0

(f80.ccc): Access violation - code c0000005 (!!! second chance !!!)
SecondChance_av_AccessViolation

Call stack below ---
# Child-SP RetAddr : Args to
Child : Call Site
00 00000000`63dadc60 00000000`774fce82 : 00000000`00000000
00000000`160e3b78 00000000`63daf7b0 00000000`3262ecf0 :
ntdll!CsrAllocateMessagePointer+0x1f4
01 00000000`63dadd10 00000000`3261d196 : 00000000`63dadfe0
00000000`522f8010 00000000`63dae5d0 00000000`001f7b68 :
ntdll!CsrAllocateMessagePointer+0x122
02 00000000`63dadd40 00000000`63dadfe0 : 00000000`522f8010
00000000`63dae5d0 00000000`001f7b68 00000000`001f7f10 :
aludf!aludf_stringreplace+0xaf56
03 00000000`63dadd48 00000000`522f8010 : 00000000`63dae5d0
00000000`001f7b68 00000000`001f7f10 00000000`002d0e48 : 0x63dadfe0
04 00000000`63dadd50 00000000`63dae5d0 : 00000000`001f7b68
00000000`001f7f10 00000000`002d0e48 00000000`522fb918 : 0x522f8010
05 00000000`63dadd58 00000000`001f7b68 : 00000000`001f7f10
00000000`002d0e48 00000000`522fb918 00000000`160e3c00 : 0x63dae5d0
06 00000000`63dadd60 00000000`001f7f10 : 00000000`002d0e48
00000000`522fb918 00000000`160e3c00 00000000`3262ecf0 : 0x1f7b68
07 00000000`63dadd68 00000000`002d0e48 : 00000000`522fb918
00000000`160e3c00 00000000`3262ecf0 00000000`00000000 : 0x1f7f10
08 00000000`63dadd70 00000000`522fb918 : 00000000`160e3c00
00000000`3262ecf0 00000000`00000000 00000000`775c7288 : 0x2d0e48
09 00000000`63dadd78 00000000`160e3c00 : 00000000`3262ecf0
00000000`00000000 00000000`775c7288 00000000`00000000 : 0x522fb918
0a 00000000`63dadd80 00000000`3262ecf0 : 00000000`00000000
00000000`775c7288 00000000`00000000 00000000`00000030 : 0x160e3c00
0b 00000000`63dadd88 00000000`00000000 : 00000000`775c7288
00000000`00000000 00000000`00000030 00000000`001f0000 :
aludf!aludf_stringreplace+0x1cab0

Creating
c:\CrashDumps\\20101118_134658_Crash_Mode\FULLDUMP_SecondChance_av_AccessViolation_fb_inet_server.exe__084c_2010-11-19_05-15-28-475_0f80.dmp
- mini user dump
Dump successfully written

(f80.900): Access violation - code c0000005 (first chance)
FirstChance_av_AccessViolation

Call stack below ---
# Child-SP RetAddr : Args to
Child : Call Site
00 00000000`7b4ddd20 00000000`774fce82 : 00000000`00000000
00000000`3ded2568 00000000`7b4df7b0 00000000`3262ecf0 :
ntdll!CsrAllocateMessagePointer+0x1f4
01 00000000`7b4dddd0 00000000`3261d196 : 00000000`7b4de0a0
00000000`4fef0040 00000000`7b4de5a0 00000000`5219c7d0 :
ntdll!CsrAllocateMessagePointer+0x122
02 00000000`7b4dde00 00000000`7b4de0a0 : 00000000`4fef0040
00000000`7b4de5a0 00000000`5219c7d0 00000000`7b4df7b0 :
aludf!aludf_stringreplace+0xaf56
03 00000000`7b4dde08 00000000`4fef0040 : 00000000`7b4de5a0
00000000`5219c7d0 00000000`7b4df7b0 00000000`005831ed : 0x7b4de0a0
04 00000000`7b4dde10 00000000`7b4de5a0 : 00000000`5219c7d0
00000000`7b4df7b0 00000000`005831ed ffffffff`fffffffe : 0x4fef0040
05 00000000`7b4dde18 00000000`5219c7d0 : 00000000`7b4df7b0
00000000`005831ed ffffffff`fffffffe 00000000`48807210 : 0x7b4de5a0
06 00000000`7b4dde20 00000000`7b4df7b0 : 00000000`005831ed
ffffffff`fffffffe 00000000`48807210 00000000`3262ecf0 : 0x5219c7d0
07 00000000`7b4dde28 00000000`005831ed : ffffffff`fffffffe
00000000`48807210 00000000`3262ecf0 00000000`7b4de380 : 0x7b4df7b0
08 00000000`7b4dde30 00000000`32616596 : 00000000`563ea008
00000000`68fd3638 00000000`7b4df7b0 ffffffff`fffffffe :
fb_inet_server!Jrd::TextType::compare+0x62d
09 00000000`7b4de170 00000000`563ea008 : 00000000`68fd3638
00000000`7b4df7b0 ffffffff`fffffffe 00000000`68fd2010 :
aludf!aludf_stringreplace+0x4356
0a 00000000`7b4de178 00000000`68fd3638 : 00000000`7b4df7b0
ffffffff`fffffffe 00000000`68fd2010 00000000`0046e4d0 : 0x563ea008
0b 00000000`7b4de180 00000000`7b4df7b0 : ffffffff`fffffffe
00000000`68fd2010 00000000`0046e4d0 ffffffff`fffffffe : 0x68fd3638
0c 00000000`7b4de188 ffffffff`fffffffe : 00000000`68fd2010
00000000`0046e4d0 ffffffff`fffffffe 00000000`00492a9c : 0x7b4df7b0
0d 00000000`7b4de190 00000000`68fd2010 : 00000000`0046e4d0
ffffffff`fffffffe 00000000`00492a9c 00000015`68fd0015 : 0xffffffff`fffffffe
0e 00000000`7b4de198 00000000`0046e4cf : ffffffff`fffffffe
00000000`00492a9c 00000015`68fd0015 00000000`00000000 : 0x68fd2010
0f 00000000`7b4de1a0 ffffffff`fffffffe : 00000000`00492a9c
00000015`68fd0015 00000000`00000000 00000000`59ef02f0 :
fb_inet_server!ERR_post_nothrow+0x3f
10 00000000`7b4de1a8 00000000`00492a9c : 00000015`68fd0015
00000000`00000000 00000000`59ef02f0 00000000`7b4de250 : 0xffffffff`fffffffe
11 00000000`7b4de1b0 00000000`006d9184 : 00000000`690a0040
00000000`7b4de4c8 00000000`00000000 00000000`00000000 :
fb_inet_server!CharSetContainer::lookupCollation+0x72c
12 00000000`7b4de360 00000000`0048bab5 : 00000000`690a0040
00000000`3ded2568 00000000`4fef0040 00000000`3ded2608 :
fb_inet_server!CVT_move_common+0x574
13 00000000`7b4de450 00000000`005569b4 : 00000000`4fef3270
00000000`7b4df7b0 00000000`521efc60 ffffffff`fffffffe :
fb_inet_server!FUN_evaluate+0x9c5
14 00000000`7b4de9a0 00000000`004d0af2 : 00000000`7b4df7b0
00000000`4880de68 00000000`4fef0040 00000000`521efad8 :
fb_inet_server!EVL_expr+0x3f4
15 00000000`7b4dea20 00000000`004d7ffb : 00000000`7b4df7b0
00000000`7b4df7b0 00000000`4fedfd30 00000000`521efad8 :
fb_inet_server!EXE_assignment+0x62
16 00000000`7b4dea60 00000000`004d0521 : 00000000`7b4df7b0
00000000`4fef0040 00000000`521e0908 00000000`4fedfd30 :
fb_inet_server!EXE_looper+0x2ab
17 00000000`7b4debb0 00000000`004d4a8b : 00000000`00000002
00000000`4fedfd30 00000000`00000001 00000000`7b4df7b0 :
fb_inet_server!execute_looper+0x81
18 00000000`7b4debe0 00000000`004d7b0f : 00000000`7b4df7b0
00000000`4fef0040 00000000`5219a328 00000000`4fed0002 :
fb_inet_server!EXE_receive+0x11b
19 00000000`7b4dec70 00000000`004d81ef : 00000000`7b4df7b0
00000000`4fed0002 00000000`7b4df7b0 00000000`4fedfd30 :
fb_inet_server!execute_procedure+0x26f
1a 00000000`7b4ded60 00000000`004da010 : 00000000`7b4df7b0
00000000`4fefc010 00000000`521f5038 00000000`4fefc010 :
fb_inet_server!EXE_looper+0x49f
1b 00000000`7b4deeb0 00000000`004da2ff : 00000000`4fefc010
00000000`4fedfd30 00000000`4fefc010 00000000`00000000 :
fb_inet_server!looper_seh+0x10
1c 00000000`7b4deee0 00000000`004da6bf : 00000000`4fefc010
00000000`7b4df7b0 00000000`00000000 00000000`00000000 :
fb_inet_server!EXE_start+0x2bf
1d 00000000`7b4def40 00000000`004d74c6 : 00000000`7b4df7b0
00000000`226b3198 00000000`68fd2620 00000000`68fd26c0 :
fb_inet_server!execute_triggers+0x30f
1e 00000000`7b4df020 00000000`004d91d2 : 00000000`68fd3bc0
00000000`563e000d 00000000`4fed0000 00000000`563e9ec0 :
fb_inet_server!modify+0x2b6
1f 00000000`7b4df0d0 00000000`004da010 : 00000000`7b4df7b0
00000000`68fd20b8 00000000`563e8de0 00000000`68fd20b8 :
fb_inet_server!EXE_looper+0x1482
20 00000000`7b4df220 00000000`004da2ff : 00000000`4ce6080d
00000000`4fedfd30 00000000`68fd20b8 00000000`7b4df7b0 :
fb_inet_server!looper_seh+0x10
21 00000000`7b4df250 00000000`004467ae : 00000000`68fd20b8
00000000`20af1bf0 00000000`4fedfd30 00000000`7b4df7b0 :
fb_inet_server!EXE_start+0x2bf
22 00000000`7b4df2b0 00000000`00636704 : 00000000`00000000
00000000`278f8030 00000000`7b4df7b0 00000000`00000000 :
fb_inet_server!JRD_start+0x4e
23 00000000`7b4df2e0 00000000`00639e35 : 00000000`18a39ff0
00000000`48800403 00000000`48800403 00000000`278f7ff0 :
fb_inet_server!execute_request+0x294
24 00000000`7b4df4e0 00000000`0063a09b : 00000000`7b4df7b0
00000000`48807210 00000000`7b4df8f0 00000000`4880de6c :
fb_inet_server!execute_immediate+0x2e5
25 00000000`7b4df5d0 00000000`0044fc66 : 00000000`48800020
00000000`20af1bf0 00000000`7b4df8f0 00000000`7b4dfae0 :
fb_inet_server!DSQL_execute_immediate+0x8b
26 00000000`7b4df650 00000000`0042df3b : 00000000`7b4dfae0
00000000`7b4df920 00000000`7b4df900 00000000`22332460 :
fb_inet_server!jrd8_execute_immediate+0x2e6
27 00000000`7b4df870 00000000`0073f445 : 00000000`0008001f
00000000`006bf5f5 00000000`7b4dfac4 00000000`00000078 :
fb_inet_server!isc_dsql_exec_immed3_m+0x33b
28 00000000`7b4dfa40 00000000`00740635 : 00000000`67040000
00000000`00000040 00000000`01d8f7e0 00000000`6704fd98 :
fb_inet_server!rem_port::execute_immediate+0x2e5
29 00000000`7b4dfbd0 00000000`00740e55 : 00000000`6091bbc8
00000000`20af1520 00000000`20af1930 00000000`7b4dfde0 :
fb_inet_server!process_packet+0x4e5
2a 00000000`7b4dfdc0 00000000`0046f331 : 00000000`00000000
00000000`00000000 00000000`00740cc0 00000000`0000003a :
fb_inet_server!loopThread+0x195
2b 00000000`7b4dfec0 00000000`744b37d7 : 00000000`00e04000
00000000`00000000 00000000`00000000 00000000`00000000 :
fb_inet_server!`anonymous namespace'::threadStart+0x51
2c 00000000`7b4dff00 00000000`744b3894 : 00000000`745695c0
00000000`00e04000 00000000`00000000 00000000`00000000 :
MSVCR80!endthreadex+0x47
2d 00000000`7b4dff30 00000000`773af56d : 00000000`00000000
00000000`00000000 00000000`00000000 00000000`00000000 :
MSVCR80!endthreadex+0x104
2e 00000000`7b4dff60 00000000`774e3021 : 00000000`00000000
00000000`00000000 00000000`00000000 00000000`00000000 :
kernel32!BaseThreadInitThunk+0xd
2f 00000000`7b4dff90 00000000`00000000 : 00000000`00000000
00000000`00000000 00000000`00000000 00000000`00000000 :
ntdll!RtlUserThreadStart+0x21

STATUS_STACK_BUFFER_OVERRUN encountered



as you can see, their is the name aludf_stringreplace, that is a
function in our UDF.
but the most strange is here : Their is NO FUNCTION or even NO TRIGGER
or NO SQL that use this functions ! actually noone and nothing use this
function ! it's just declared but not used !! (Drop External function
aludf_stringreplace => dont raise any exception so their is absolutely
no dependency)

Someone have any explanation about this ?

below the declaration of the udf (the UDF is write in FreePascal and
compiled in 64bit) :

library aludf;

uses
windows,
sysutils,

function ib_util_malloc(l: integer): pointer; cdecl; external 'ib_util.dll';

function ALMakeResultString(Source: PChar): PChar; cdecl; export;
var Len: cardinal;
begin
Len := StrLen(Source) + 1;
result := ib_util_malloc(Len);

if Len = 1 then result[0] := #0
else Move(Source^, result^, Len);
end;

function aludf_stringreplace(s, OldPattern, NewPattern: PChar; var
replaceall, ignorecase: integer): Pchar; cdecl; export;
var aFlags: TReplaceFlags;
begin
aFlags := [];
if replaceall = 1 then aFlags := aFlags + [rfreplaceall];
if ignorecase = 1 then aFlags := aFlags + [rfignorecase];
result :=
ALMakeResultString(pchar(stringreplace(String(s),String(OldPattern),String(NewPattern),aFlags)));
end;

DECLARE EXTERNAL FUNCTION aludf_stringreplace
CSTRING(16384),
CSTRING(16384),
CSTRING(16384),
SMALLINT,
SMALLINT
RETURNS CSTRING(16384) FREE_IT
ENTRY_POINT 'aludf_stringreplace' MODULE_NAME 'aludf';

exports
aludf_stringreplace;

begin
IsMultiThread := True;
randomize;
end.


so this function is very (very) simple !


thanks by advance for your help !!

stéphane