1 @include package-locks-basic.texinfo
3 None of the following sections apply to SBCL built
6 The interface described here is experimental: incompatible changes
7 in future SBCL releases are possible, even expected.
10 * Package Locking Overview::
11 * Implementation Packages::
12 * Package Locked Errors::
13 * Package Locks in Compiled Code::
14 * Package Lock Violations::
15 * Package Lock Dictionary::
18 @node Package Locking Overview
19 @section Package Locking Overview
20 Package locks protect against unintentional modifications of a
21 package: they provide similar protection to user packages as is
22 mandated to @code{common-lisp} package by the ANSI specification. They
23 are not, and should not be used as a security measure.
25 Newly created packages are by default unlocked (see the @code{:lock}
26 option to @code{defpackage}).
28 The package @code{common-lisp} and SBCL internal implementation
29 packages are locked by default, including @code{sb-ext}.
31 It may be beneficial to lock @code{common-lisp-user} as well, to
32 ensure that various libraries don't pollute it without asking,
33 but this is not currently done by default.
35 @node Implementation Packages
36 @section Implementation Packages
38 Each package has a list of associated implementation packages. A
39 locked package, and the symbols whose home package it is, can be
40 modified without violating package locks only when @code{*package*} is
41 bound to one of the implementation packages of the locked package.
43 Unless explicitly altered by @code{defpackage},
44 @code{sb-ext:add-implementation-package}, or
45 @code{sb-ext:remove-implementation-package} each package is its own
46 (only) implementation package.
48 @node Package Locked Errors
49 @section Package Locked Errors
51 If an operation violates a package lock, a continuable error that is
52 of a subtype of @code{sb-ext:package-lock-violation} (subtype of
53 @code{package-error}) is signalled when the operation is attempted.
55 Additional restarts may be established for continuable package lock
56 violations for interactive use.
58 The actual type of the error depends on circumstances that caused the
59 violation: operations on packages signal errors of type
60 @code{sb-ext:package-locked-error}, and operations on symbols signal
61 errors of type @code{sb-ext:symbol-package-locked-error}.
63 @node Package Locks in Compiled Code
64 @section Package Locks in Compiled Code
66 @subsection Lexical bindings and declarations
68 Compiling lexical binding constructs or lexical declarations that
69 violate package locks package cause a compile-time package-lock
70 violation. A complete listing of operators affect by this is:
71 @code{let}, @code{let*}, @code{flet}, @code{labels}, @code{macrolet},
72 and @code{symbol-macrolet}, @code{declare}.
74 Package locks affecting both lexical bindings and declarations can be
75 disabled at compile-time with @code{sb-ext:disable-package-locks}
76 declaration, and re-enabled with @code{sb-ext:enable-package-locks}
77 declaration. Constructs compiled with package locks thusly disabled
78 are guaranteed not to signal package lock violation errors at runtime.
87 (defmacro with-foo (&body body)
88 `(locally (declare (disable-package-locks locked:foo))
90 (declare (enable-package-locks locked:foo)) ; re-enable for body
94 @subsection Interned symbols
96 If compiled code contains interned symbols, then loading that code
97 into an image without the said symbols will not cause a package lock
98 violation even if the packages in question are locked.
100 @subsection Other limitations on compiled code
102 With the exception of the aforementioned contructs, and interned
103 symbols, behaviour is unspecified if package locks affecting compiled
104 code are not the same during loading of the code or execution.
106 Specifically, code compiled with packages unlocked may or may not fail
107 to signal package-lock-violations even if the packages are locked at
108 runtime, and code compiled with packages locked may or may not signal
109 spurious package-lock-violations at runtime even if the packages are
112 In practise all this means that package-locks have a neglible
113 performance penalty in compiled code as long as they are not violated.
115 @node Package Lock Violations
116 @section Package Lock Violations
118 @heading Operations on Packages
120 Following actions cause a package lock violation if the package
121 operated on is locked, and @code{*package*} is not an implementation
122 package of that package, and the action would cause a change in the
123 state of the package (eg. exporting already external symbols is
124 allowed). Package lock violations caused by these operations signal
125 errors of type @code{sb-ext:package-locked-error}.
129 Shadowing a symbol in a package.
132 Importing a symbol to a package.
135 Uninterning a symbol from a package.
138 Exporting a symbol from a package.
141 Unexporting a symbol from a package.
144 Changing the packages used by a package.
154 @heading Operations on Symbols
156 Following actions cause a package lock violation if the home package
157 of the symbol operated on is locked, and @code{*package*} is not an
158 implementation package of that package. Package lock violations caused
159 by these action signal errors of type
160 @code{sb-ext:symbol-package-locked-error}.
162 These actions cause only one package lock violation per lexically
163 apparent violated package.
168 ;; Packages FOO and BAR are locked.
170 ;; Two lexically apparent violated packages: exactly two
171 ;; package-locked-errors will be signalled.
173 (defclass foo:point ()
175 (y :accessor bar:y)))
180 Binding or altering its value lexically or dynamically, or
181 establishing it as a symbol-macro.
187 If the symbol is not defined as a constant, global symbol-macro or a
188 global dynamic variable, it may be lexically bound or established as a
192 If the symbol is defined as a global dynamic variable, it may be
198 Defining, undefining, or binding it, or its setf name as a function.
204 If the symbol is not defined as a function, macro, or special operator
205 it and its setf name may be lexically bound as a function.
210 Defining, undefining, or binding it as a macro or compiler macro.
216 If the symbol is not defined as a function, macro, or special operator
217 it may be lexically bound as a macro.
222 Defining it as a type specifier or structure.
225 Defining it as a declaration with a declaration proclamation.
228 Declaring or proclaiming it special.
231 Declaring or proclaiming its type or ftype.
237 If the symbol may be lexically bound, the type of that binding may be
241 If the symbol may be lexically bound as a function, the ftype of that
242 binding may be declared.
247 Defining a setf expander for it.
250 Defining it as a method combination type.
253 Using it as the class-name argument to setf of find-class.
257 @node Package Lock Dictionary
258 @section Package Lock Dictionary
260 @deftp {Declaration} sb-ext:disable-package-locks
262 Syntax: @code{(sb-ext:disable-package-locks symbol*)}
264 Disables package locks affecting the named symbols during compilation
265 in the lexical scope of the declaration. Disabling locks on symbols
266 whose home package is unlocked, or disabling an already disabled lock,
270 @deftp {Declaration} sb-ext:enable-package-locks
272 Syntax: @code{(sb-ext:enable-package-locks symbol*)}
274 Re-enables package locks affecting the named symbols during
275 compilation in the lexical scope of the declaration. Enabling locks
276 that were not first disabled with @code{sb-ext:disable-package-locks}
277 declararion, or enabling locks that are already enabled has no effect.
280 @include condition-sb-ext-package-lock-violation.texinfo
281 @include condition-sb-ext-package-locked-error.texinfo
282 @include condition-sb-ext-symbol-package-locked-error.texinfo
284 @defun sb-ext:package-locked-error-symbol @var{symbol-package-locked-error}
286 Returns the symbol that caused the @code{symbol-package-locked-error}
290 @include fun-sb-ext-package-locked-p.texinfo
291 @include fun-sb-ext-lock-package.texinfo
292 @include fun-sb-ext-unlock-package.texinfo
293 @include fun-sb-ext-package-implemented-by-list.texinfo
294 @include fun-sb-ext-package-implements-list.texinfo
295 @include fun-sb-ext-add-implementation-package.texinfo
296 @include fun-sb-ext-remove-implementation-package.texinfo
297 @include macro-sb-ext-without-package-locks.texinfo
298 @include macro-sb-ext-with-unlocked-packages.texinfo
300 @defmac defpackage name [[@var{option}]]* @result{} package
302 Options are extended to include the following:
306 @code{:lock} @var{boolean}
308 If the argument to @code{:lock} is @code{t}, the package is initially
309 locked. If @code{:lock} is not provided it defaults to @code{nil}.
312 @code{:implement} @var{package-designator}*
314 The package is added as an implementation package to the packages
315 named. If @code{:implement} is not provided, it defaults to the
322 (defpackage "FOO" (:export "BAR") (:lock t) (:implement))
323 (defpackage "FOO-INT" (:use "FOO") (:implement "FOO" "FOO-INT"))
327 (defpackage "FOO") (:export "BAR"))
329 (remove-implementation-package "FOO" "FOO")
330 (defpackage "FOO-INT" (:use "BAR"))
331 (add-implementation-package "FOO-INT" "FOO")