]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/errors.cpp
Security-58286.1.32.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / errors.cpp
1 /*
2 * Copyright (c) 2000-2004,2011-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // Error hierarchy
27 //
28 #include <security_utilities/errors.h>
29 #include <security_utilities/debugging.h>
30 #include <security_utilities/utility_config.h>
31 #include <security_utilities/debugging_internal.h>
32 #include <typeinfo>
33 #include <stdio.h>
34 #include <Security/SecBase.h>
35 #include <execinfo.h>
36 #include <cxxabi.h>
37
38 //@@@
39 // From cssmapple.h - layering break
40 // Where should this go?
41 //@@@
42 #define errSecErrnoBase 100000
43 #define errSecErrnoLimit 100255
44
45 //
46 // The base of the exception hierarchy.
47 //
48 CommonError::CommonError() : whatBuffer("CommonError")
49 {
50 }
51
52
53 //
54 // We strongly encourage catching all exceptions by const reference, so the copy
55 // constructor of our exceptions should never be called.
56 //
57 CommonError::CommonError(const CommonError &source)
58 {
59 strlcpy(whatBuffer, source.whatBuffer, whatBufferSize);
60 }
61
62 CommonError::~CommonError() throw ()
63 {
64 }
65
66 void CommonError::LogBacktrace() {
67 // Only do this work if we're actually going to log things
68 if(secinfoenabled("security_exception")) {
69 const size_t maxsize = 32;
70 void* callstack[maxsize];
71
72 int size = backtrace(callstack, maxsize);
73 char** names = backtrace_symbols(callstack, size);
74
75 // C++ symbolicate the callstack
76
77 const char* delim = " ";
78 string build;
79 char * token = NULL;
80 char * line = NULL;
81
82 for(int i = 0; i < size; i++) {
83 build = "";
84
85 line = names[i];
86
87 while((token = strsep(&line, delim))) {
88 if(*token == '\0') {
89 build += " ";
90 } else {
91 int status = 0;
92 char * demangled = abi::__cxa_demangle(token, NULL, NULL, &status);
93 if(status == 0) {
94 build += demangled;
95 } else {
96 build += token;
97 }
98 build += " ";
99
100 if(demangled) {
101 free(demangled);
102 }
103 }
104 }
105
106 secinfo("security_exception", "%s", build.c_str());
107 }
108 free(names);
109 }
110 }
111
112
113
114 //
115 // UnixError exceptions
116 //
117 UnixError::UnixError() : error(errno)
118 {
119 SECURITY_EXCEPTION_THROW_UNIX(this, errno);
120
121 snprintf(whatBuffer, whatBufferSize, "UNIX errno exception: %d", this->error);
122 secnotice("security_exception", "%s", what());
123 LogBacktrace();
124 }
125
126 UnixError::UnixError(int err, bool suppresslogging) : error(err)
127 {
128 SECURITY_EXCEPTION_THROW_UNIX(this, err);
129
130 if(!suppresslogging || secinfoenabled("security_exception")) {
131 snprintf(whatBuffer, whatBufferSize, "UNIX error exception: %d", this->error);
132 secnotice("security_exception", "%s", what());
133 LogBacktrace();
134 }
135 }
136
137 const char *UnixError::what() const throw ()
138 {
139 return whatBuffer;
140 }
141
142
143 OSStatus UnixError::osStatus() const
144 {
145 return error + errSecErrnoBase;
146 }
147
148 int UnixError::unixError() const
149 { return error; }
150
151 void UnixError::throwMe(int err) { throw UnixError(err, false); }
152 void UnixError::throwMeNoLogging(int err) { throw UnixError(err, true); }
153
154
155 // @@@ This is a hack for the Network protocol state machine
156 UnixError UnixError::make(int err) { return UnixError(err, false); }
157
158
159 //
160 // MacOSError exceptions
161 //
162 MacOSError::MacOSError(int err) : error(err)
163 {
164 SECURITY_EXCEPTION_THROW_OSSTATUS(this, err);
165
166 snprintf(whatBuffer, whatBufferSize, "MacOS error: %d", this->error);
167 secnotice("security_exception", "%s", what());
168 LogBacktrace();
169 }
170
171 const char *MacOSError::what() const throw ()
172 {
173 return whatBuffer;
174 }
175
176 OSStatus MacOSError::osStatus() const
177 { return error; }
178
179 int MacOSError::unixError() const
180 {
181 // embedded UNIX errno values are returned verbatim
182 if (error >= errSecErrnoBase && error <= errSecErrnoLimit)
183 return error - errSecErrnoBase;
184
185 switch (error) {
186 default:
187 // cannot map this to errno space
188 return -1;
189 }
190 }
191
192 void MacOSError::throwMe(int error)
193 { throw MacOSError(error); }
194
195 MacOSError MacOSError::make(int error)
196 { return MacOSError(error); }
197
198
199 //
200 // CFError exceptions
201 //
202 CFError::CFError()
203 {
204 SECURITY_EXCEPTION_THROW_CF(this);
205 secnotice("security_exception", "CFError");
206 LogBacktrace();
207 }
208
209 const char *CFError::what() const throw ()
210 { return "CoreFoundation error"; }
211
212 OSStatus CFError::osStatus() const
213 { return errSecCoreFoundationUnknown; }
214
215 int CFError::unixError() const
216 {
217 return EFAULT; // nothing really matches
218 }
219
220 void CFError::throwMe()
221 { throw CFError(); }
222
223
224
225
226 void ModuleNexusError::throwMe()
227 {
228 throw ModuleNexusError();
229 }
230
231
232
233 OSStatus ModuleNexusError::osStatus() const
234 {
235 return errSecParam;
236 }
237
238
239
240 int ModuleNexusError::unixError() const
241 {
242 return EINVAL;
243 }
244