1 /* Copyright (c) 1999,2003-2004,2008 Apple Inc.
3 * attachLeak.c - analyze memory leaks from CSSM_Init/ModuleAttach.
10 #include <Security/cssm.h>
14 static CSSM_API_MEMORY_FUNCS memFuncs
= {
22 static CSSM_VERSION vers
= {2, 0};
24 static void usage(char **argv
)
26 printf("usage: %s [options]\n", argv
[0]);
27 printf(" Options: \n");
28 printf(" l (do ModuleLoad on each loop)\n");
29 printf(" u (do Module{Load,Unload} on each loop)\n");
30 printf(" d (CSP/DL; default = bare CSP)\n");
36 static int doPause(const char *state
)
41 printf("%s\n", state
);
42 printf("q to abort, anything else to continue: ");
47 int main(int argc
, char **argv
)
51 CSSM_HANDLE modHand
= 0;
53 CSSM_SERVICE_TYPE svcType
= CSSM_SERVICE_CSP
;
54 const CSSM_GUID
*guid
= &gGuidAppleCSP
;
55 const char *modName
= "AppleCSP";
57 CSSM_BOOL doLoad
= CSSM_FALSE
;
58 CSSM_BOOL doUnload
= CSSM_FALSE
;
60 /* force link against malloc */
62 for(arg
=1; arg
<argc
; arg
++) {
69 doLoad
= doUnload
= CSSM_TRUE
;
72 guid
= &gGuidAppleCSPDL
;
73 modName
= "AppleCSPDL";
76 guid
= &gGuidAppleX509CL
;
77 svcType
= CSSM_SERVICE_CL
;
78 modName
= "AppleX509CL";
81 guid
= &gGuidAppleX509TP
;
82 svcType
= CSSM_SERVICE_TP
;
83 modName
= "AppleX509TP";
91 if(doPause("Top of test")) {
95 /* CSSM init, just once */
97 printf("Oops, error starting up CSSM\n");
100 if(doPause("CSSM initialized")) {
106 /* load, just once */
107 crtn
= CSSM_ModuleLoad(guid
,
108 CSSM_KEY_HIERARCHY_NONE
,
109 NULL
, // eventHandler
110 NULL
); // AppNotifyCallbackCtx
112 printf("Error loading %s\n", modName
);
113 printError("CSSM_ModuleLoad", crtn
);
116 if(doPause("CSSM_ModuleLoad() complete")) {
122 /* load, each time */
123 crtn
= CSSM_ModuleLoad(guid
,
124 CSSM_KEY_HIERARCHY_NONE
,
125 NULL
, // eventHandler
126 NULL
); // AppNotifyCallbackCtx
128 printf("Error loading %s\n", modName
);
129 printError("CSSM_ModuleLoad", crtn
);
132 if(doPause("CSSM_ModuleLoad() complete")) {
136 crtn
= CSSM_ModuleAttach (guid
,
138 &memFuncs
, // memFuncs
142 CSSM_KEY_HIERARCHY_NONE
,
143 NULL
, // FunctionTable
148 printf("Error attaching to %s\n", modName
);
149 printError("CSSM_ModuleAttach", crtn
);
152 if(doPause("ModuleAttach() complete")) {
155 CSSM_ModuleDetach(modHand
);
157 if(doPause("ModuleDetach() complete")) {
161 /* unload, each time */
162 crtn
= CSSM_ModuleUnload(guid
, NULL
, NULL
);
164 printf("Error unloading %s\n", modName
);
165 printError("CSSM_ModuleUnload", crtn
);
168 if(doPause("ModuleUnload() complete")) {
177 CSSM_ModuleDetach(modHand
);
178 printf("Final detach complete; cr to exit: ");
181 printf("Test complete; cr to exit: ");