]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/errors.cpp
Security-57740.51.3.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 <typeinfo>
31 #include <stdio.h>
32 #include <Security/SecBase.h>
33 #include <execinfo.h>
34 #include <cxxabi.h>
35
36 //@@@
37 // From cssmapple.h - layering break
38 // Where should this go?
39 //@@@
40 #define errSecErrnoBase 100000
41 #define errSecErrnoLimit 100255
42
43 //
44 // The base of the exception hierarchy.
45 //
46 CommonError::CommonError() : whatBuffer("CommonError")
47 {
48 }
49
50
51 //
52 // We strongly encourage catching all exceptions by const reference, so the copy
53 // constructor of our exceptions should never be called.
54 //
55 CommonError::CommonError(const CommonError &source)
56 {
57 strlcpy(whatBuffer, source.whatBuffer, whatBufferSize);
58 }
59
60 CommonError::~CommonError() throw ()
61 {
62 }
63
64 void CommonError::LogBacktrace() {
65 // Only do this work if we're actually going to log things
66 if(secinfoenabled("security_exception")) {
67 const size_t maxsize = 32;
68 void* callstack[maxsize];
69
70 int size = backtrace(callstack, maxsize);
71 char** names = backtrace_symbols(callstack, size);
72
73 // C++ symbolicate the callstack
74
75 const char* delim = " ";
76 string build;
77 char * token = NULL;
78 char * line = NULL;
79
80 for(int i = 0; i < size; i++) {
81 build = "";
82
83 line = names[i];
84
85 while((token = strsep(&line, delim))) {
86 if(*token == '\0') {
87 build += " ";
88 } else {
89 int status = 0;
90 char * demangled = abi::__cxa_demangle(token, NULL, NULL, &status);
91 if(status == 0) {
92 build += demangled;
93 } else {
94 build += token;
95 }
96 build += " ";
97
98 if(demangled) {
99 free(demangled);
100 }
101 }
102 }
103
104 secinfo("security_exception", "%s", build.c_str());
105 }
106 free(names);
107 }
108 }
109
110
111
112 //
113 // UnixError exceptions
114 //
115 UnixError::UnixError() : error(errno)
116 {
117 SECURITY_EXCEPTION_THROW_UNIX(this, errno);
118
119 snprintf(whatBuffer, whatBufferSize, "UNIX errno exception: %d", this->error);
120 secnotice("security_exception", "%s", what());
121 LogBacktrace();
122 }
123
124 UnixError::UnixError(int err) : error(err)
125 {
126 SECURITY_EXCEPTION_THROW_UNIX(this, err);
127
128 snprintf(whatBuffer, whatBufferSize, "UNIX error exception: %d", this->error);
129 secnotice("security_exception", "%s", what());
130 LogBacktrace();
131 }
132
133 const char *UnixError::what() const throw ()
134 {
135 return whatBuffer;
136 }
137
138
139 OSStatus UnixError::osStatus() const
140 {
141 return error + errSecErrnoBase;
142 }
143
144 int UnixError::unixError() const
145 { return error; }
146
147 void UnixError::throwMe(int err) { throw UnixError(err); }
148
149 // @@@ This is a hack for the Network protocol state machine
150 UnixError UnixError::make(int err) { return UnixError(err); }
151
152
153 //
154 // MacOSError exceptions
155 //
156 MacOSError::MacOSError(int err) : error(err)
157 {
158 SECURITY_EXCEPTION_THROW_OSSTATUS(this, err);
159
160 snprintf(whatBuffer, whatBufferSize, "MacOS error: %d", this->error);
161 secnotice("security_exception", "%s", what());
162 LogBacktrace();
163 }
164
165 const char *MacOSError::what() const throw ()
166 {
167 return whatBuffer;
168 }
169
170 OSStatus MacOSError::osStatus() const
171 { return error; }
172
173 int MacOSError::unixError() const
174 {
175 // embedded UNIX errno values are returned verbatim
176 if (error >= errSecErrnoBase && error <= errSecErrnoLimit)
177 return error - errSecErrnoBase;
178
179 switch (error) {
180 default:
181 // cannot map this to errno space
182 return -1;
183 }
184 }
185
186 void MacOSError::throwMe(int error)
187 { throw MacOSError(error); }
188
189 MacOSError MacOSError::make(int error)
190 { return MacOSError(error); }
191
192
193 //
194 // CFError exceptions
195 //
196 CFError::CFError()
197 {
198 SECURITY_EXCEPTION_THROW_CF(this);
199 secnotice("security_exception", "CFError");
200 LogBacktrace();
201 }
202
203 const char *CFError::what() const throw ()
204 { return "CoreFoundation error"; }
205
206 OSStatus CFError::osStatus() const
207 { return errSecCoreFoundationUnknown; }
208
209 int CFError::unixError() const
210 {
211 return EFAULT; // nothing really matches
212 }
213
214 void CFError::throwMe()
215 { throw CFError(); }
216
217
218
219
220 void ModuleNexusError::throwMe()
221 {
222 throw ModuleNexusError();
223 }
224
225
226
227 OSStatus ModuleNexusError::osStatus() const
228 {
229 return errSecParam;
230 }
231
232
233
234 int ModuleNexusError::unixError() const
235 {
236 return EINVAL;
237 }
238