libguile-foreign-objects.texi 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2010, 2011, 2013, 2014, 2018
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Defining New Foreign Object Types
  7. @section Defining New Foreign Object Types
  8. The @dfn{foreign object type} facility is Guile's mechanism for
  9. importing object and types from C or other languages into Guile's
  10. system. If you have a C @code{struct foo} type, for example, you can
  11. define a corresponding Guile foreign object type that allows Scheme code
  12. to handle @code{struct foo *} objects.
  13. To define a new foreign object type, the programmer provides Guile with
  14. some essential information about the type --- what its name is, how many
  15. fields it has, and its finalizer (if any) --- and Guile allocates a
  16. fresh type for it. Foreign objects can be accessed from Scheme or from
  17. C.
  18. @menu
  19. * Defining Foreign Object Types::
  20. * Creating Foreign Objects::
  21. * Type Checking of Foreign Objects::
  22. * Foreign Object Memory Management::
  23. * Foreign Objects and Scheme::
  24. @end menu
  25. @node Defining Foreign Object Types
  26. @subsection Defining Foreign Object Types
  27. To create a new foreign object type from C, call
  28. @code{scm_make_foreign_object_type}. It returns a value of type
  29. @code{SCM} which identifies the new type.
  30. Here is how one might declare a new type representing eight-bit
  31. gray-scale images:
  32. @example
  33. #include <libguile.h>
  34. struct image @{
  35. int width, height;
  36. char *pixels;
  37. /* The name of this image */
  38. SCM name;
  39. /* A function to call when this image is
  40. modified, e.g., to update the screen,
  41. or SCM_BOOL_F if no action necessary */
  42. SCM update_func;
  43. @};
  44. static SCM image_type;
  45. void
  46. init_image_type (void)
  47. @{
  48. SCM name, slots;
  49. scm_t_struct_finalize finalizer;
  50. name = scm_from_utf8_symbol ("image");
  51. slots = scm_list_1 (scm_from_utf8_symbol ("data"));
  52. finalizer = NULL;
  53. image_type =
  54. scm_make_foreign_object_type (name, slots, finalizer);
  55. @}
  56. @end example
  57. The result is an initialized @code{image_type} value that identifies the
  58. new foreign object type. The next section describes how to create
  59. foreign objects and how to access their slots.
  60. @node Creating Foreign Objects
  61. @subsection Creating Foreign Objects
  62. Foreign objects contain zero or more ``slots'' of data. A slot can hold
  63. a pointer, an integer that fits into a @code{size_t} or @code{ssize_t},
  64. or a @code{SCM} value.
  65. All objects of a given foreign type have the same number of slots. In
  66. the example from the previous section, the @code{image} type has one
  67. slot, because the slots list passed to
  68. @code{scm_make_foreign_object_type} is of length one. (The actual names
  69. given to slots are unimportant for most users of the C interface, but
  70. can be used on the Scheme side to introspect on the foreign object.)
  71. To construct a foreign object and initialize its first slot, call
  72. @code{scm_make_foreign_object_1 (@var{type}, @var{first_slot_value})}.
  73. There are similarly named constructors for initializing 0, 1, 2, or 3
  74. slots, or initializing @var{n} slots via an array. @xref{Foreign
  75. Objects}, for full details. Any fields that are not explicitly
  76. initialized are set to 0.
  77. To get or set the value of a slot by index, you can use the
  78. @code{scm_foreign_object_ref} and @code{scm_foreign_object_set_x}
  79. functions. These functions take and return values as @code{void *}
  80. pointers; there are corresponding convenience procedures like
  81. @code{_signed_ref}, @code{_unsigned_set_x} and so on for dealing with
  82. slots as signed or unsigned integers.
  83. Foreign objects fields that are pointers can be tricky to manage. If
  84. possible, it is best that all memory that is referenced by a foreign
  85. object be managed by the garbage collector. That way, the GC can
  86. automatically ensure that memory is accessible when it is needed, and
  87. freed when it becomes inaccessible. If this is not the case for your
  88. program -- for example, if you are exposing an object to Scheme that was
  89. allocated by some other, Guile-unaware part of your program -- then you
  90. will probably need to implement a finalizer. @xref{Foreign Object
  91. Memory Management}, for more.
  92. Continuing the example from the previous section, if the global variable
  93. @code{image_type} contains the type returned by
  94. @code{scm_make_foreign_object_type}, here is how we could construct a
  95. foreign object whose ``data'' field contains a pointer to a freshly
  96. allocated @code{struct image}:
  97. @example
  98. SCM
  99. make_image (SCM name, SCM s_width, SCM s_height)
  100. @{
  101. struct image *image;
  102. int width = scm_to_int (s_width);
  103. int height = scm_to_int (s_height);
  104. /* Allocate the `struct image'. Because we
  105. use scm_gc_malloc, this memory block will
  106. be automatically reclaimed when it becomes
  107. inaccessible, and its members will be traced
  108. by the garbage collector. */
  109. image = (struct image *)
  110. scm_gc_malloc (sizeof (struct image), "image");
  111. image->width = width;
  112. image->height = height;
  113. /* Allocating the pixels with
  114. scm_gc_malloc_pointerless means that the
  115. pixels data is collectable by GC, but
  116. that GC shouldn't spend time tracing its
  117. contents for nested pointers because there
  118. aren't any. */
  119. image->pixels =
  120. scm_gc_malloc_pointerless (width * height, "image pixels");
  121. image->name = name;
  122. image->update_func = SCM_BOOL_F;
  123. /* Now wrap the struct image* in a new foreign
  124. object, and return that object. */
  125. return scm_make_foreign_object_1 (image_type, image);
  126. @}
  127. @end example
  128. We use @code{scm_gc_malloc_pointerless} for the pixel buffer to tell the
  129. garbage collector not to scan it for pointers. Calls to
  130. @code{scm_gc_malloc}, @code{scm_make_foreign_object_1}, and
  131. @code{scm_gc_malloc_pointerless} raise an exception in out-of-memory
  132. conditions; the garbage collector is able to reclaim previously
  133. allocated memory if that happens.
  134. @node Type Checking of Foreign Objects
  135. @subsection Type Checking of Foreign Objects
  136. Functions that operate on foreign objects should check that the passed
  137. @code{SCM} value indeed is of the correct type before accessing its
  138. data. They can do this with @code{scm_assert_foreign_object_type}.
  139. For example, here is a simple function that operates on an image object,
  140. and checks the type of its argument.
  141. @example
  142. SCM
  143. clear_image (SCM image_obj)
  144. @{
  145. int area;
  146. struct image *image;
  147. scm_assert_foreign_object_type (image_type, image_obj);
  148. image = scm_foreign_object_ref (image_obj, 0);
  149. area = image->width * image->height;
  150. memset (image->pixels, 0, area);
  151. /* Invoke the image's update function. */
  152. if (scm_is_true (image->update_func))
  153. scm_call_0 (image->update_func);
  154. return SCM_UNSPECIFIED;
  155. @}
  156. @end example
  157. @node Foreign Object Memory Management
  158. @subsection Foreign Object Memory Management
  159. Once a foreign object has been released to the tender mercies of the
  160. Scheme system, it must be prepared to survive garbage collection. In
  161. the example above, all the memory associated with the foreign object is
  162. managed by the garbage collector because we used the @code{scm_gc_}
  163. allocation functions. Thus, no special care must be taken: the garbage
  164. collector automatically scans them and reclaims any unused memory.
  165. However, when data associated with a foreign object is managed in some
  166. other way---e.g., @code{malloc}'d memory or file descriptors---it is
  167. possible to specify a @dfn{finalizer} function to release those
  168. resources when the foreign object is reclaimed.
  169. As discussed in @pxref{Garbage Collection}, Guile's garbage collector
  170. will reclaim inaccessible memory as needed. This reclamation process
  171. runs concurrently with the main program. When Guile analyzes the heap
  172. and determines that an object's memory can be reclaimed, that memory is
  173. put on a ``free list'' of objects that can be reclaimed. Usually that's
  174. the end of it---the object is available for immediate re-use. However
  175. some objects can have ``finalizers'' associated with them---functions
  176. that are called on reclaimable objects to effect any external cleanup
  177. actions.
  178. Finalizers are tricky business and it is best to avoid them. They can
  179. be invoked at unexpected times, or not at all---for example, they are
  180. not invoked on process exit. They don't help the garbage collector do
  181. its job; in fact, they are a hindrance. Furthermore, they perturb the
  182. garbage collector's internal accounting. The GC decides to scan the
  183. heap when it thinks that it is necessary, after some amount of
  184. allocation. Finalizable objects almost always represent an amount of
  185. allocation that is invisible to the garbage collector. The effect can
  186. be that the actual resource usage of a system with finalizable objects
  187. is higher than what the GC thinks it should be.
  188. All those caveats aside, some foreign object types will need finalizers.
  189. For example, if we had a foreign object type that wrapped file
  190. descriptors---and we aren't suggesting this, as Guile already has ports
  191. ---then you might define the type like this:
  192. @example
  193. static SCM file_type;
  194. static void
  195. finalize_file (SCM file)
  196. @{
  197. int fd = scm_foreign_object_signed_ref (file, 0);
  198. if (fd >= 0)
  199. @{
  200. scm_foreign_object_signed_set_x (file, 0, -1);
  201. close (fd);
  202. @}
  203. @}
  204. static void
  205. init_file_type (void)
  206. @{
  207. SCM name, slots;
  208. scm_t_struct_finalize finalizer;
  209. name = scm_from_utf8_symbol ("file");
  210. slots = scm_list_1 (scm_from_utf8_symbol ("fd"));
  211. finalizer = finalize_file;
  212. image_type =
  213. scm_make_foreign_object_type (name, slots, finalizer);
  214. @}
  215. static SCM
  216. make_file (int fd)
  217. @{
  218. return scm_make_foreign_object_1 (file_type, (void *) fd);
  219. @}
  220. @end example
  221. @cindex finalizer
  222. @cindex finalization
  223. Note that the finalizer may be invoked in ways and at times you might
  224. not expect. In a Guile built without threading support, finalizers are
  225. invoked via ``asyncs'', which interleaves them with running Scheme code;
  226. @pxref{Asyncs}. If the user's Guile is built with support for threads,
  227. the finalizer will probably be called by a dedicated finalization
  228. thread, unless the user invokes @code{scm_run_finalizers ()} explicitly.
  229. In either case, finalizers run concurrently with the main program, and
  230. so they need to be async-safe and thread-safe. If for some reason this
  231. is impossible, perhaps because you are embedding Guile in some
  232. application that is not itself thread-safe, you have a few options. One
  233. is to use guardians instead of finalizers, and arrange to pump the
  234. guardians for finalizable objects. @xref{Guardians}, for more
  235. information. The other option is to disable automatic finalization
  236. entirely, and arrange to call @code{scm_run_finalizers ()} at
  237. appropriate points. @xref{Foreign Objects}, for more on these
  238. interfaces.
  239. Finalizers are allowed to allocate memory, access GC-managed memory, and
  240. in general can do anything any Guile user code can do. This was not the
  241. case in Guile 1.8, where finalizers were much more restricted. In
  242. particular, in Guile 2.0, finalizers can resuscitate objects. We do not
  243. recommend that users avail themselves of this possibility, however, as a
  244. resuscitated object can re-expose other finalizable objects that have
  245. been already finalized back to Scheme. These objects will not be
  246. finalized again, but they could cause use-after-free problems to code
  247. that handles objects of that particular foreign object type. To guard
  248. against this possibility, robust finalization routines should clear
  249. state from the foreign object, as in the above @code{free_file} example.
  250. One final caveat. Foreign object finalizers are associated with the
  251. lifetime of a foreign object, not of its fields. If you access a field
  252. of a finalizable foreign object, and do not arrange to keep a reference
  253. on the foreign object itself, it could be that the outer foreign object
  254. gets finalized while you are working with its field.
  255. For example, consider a procedure to read some data from a file, from
  256. our example above.
  257. @example
  258. SCM
  259. read_bytes (SCM file, SCM n)
  260. @{
  261. int fd;
  262. SCM buf;
  263. size_t len, pos;
  264. scm_assert_foreign_object_type (file_type, file);
  265. fd = scm_foreign_object_signed_ref (file, 0);
  266. if (fd < 0)
  267. scm_wrong_type_arg_msg ("read-bytes", SCM_ARG1,
  268. file, "open file");
  269. len = scm_to_size_t (n);
  270. SCM buf = scm_c_make_bytevector (scm_to_size_t (n));
  271. pos = 0;
  272. while (pos < len)
  273. @{
  274. char *bytes = SCM_BYTEVECTOR_CONTENTS (buf);
  275. ssize_t count = read (fd, bytes + pos, len - pos);
  276. if (count < 0)
  277. scm_syserror ("read-bytes");
  278. if (count == 0)
  279. break;
  280. pos += count;
  281. @}
  282. scm_remember_upto_here_1 (file);
  283. return scm_values (scm_list_2 (buf, scm_from_size_t (pos)));
  284. @}
  285. @end example
  286. After the prelude, only the @code{fd} value is used and the C compiler
  287. has no reason to keep the @code{file} object around. If
  288. @code{scm_c_make_bytevector} results in a garbage collection,
  289. @code{file} might not be on the stack or anywhere else and could be
  290. finalized, leaving @code{read} to read a closed (or, in a multi-threaded
  291. program, possibly re-used) file descriptor. The use of
  292. @code{scm_remember_upto_here_1} prevents this, by creating a reference
  293. to @code{file} after all data accesses. @xref{Garbage Collection
  294. Functions}.
  295. @code{scm_remember_upto_here_1} is only needed on finalizable objects,
  296. because garbage collection of other values is invisible to the program
  297. -- it happens when needed, and is not observable. But if you can, save
  298. yourself the headache and build your program in such a way that it
  299. doesn't need finalization.
  300. @node Foreign Objects and Scheme
  301. @subsection Foreign Objects and Scheme
  302. It is also possible to create foreign objects and object types from
  303. Scheme, and to access fields of foreign objects from Scheme. For
  304. example, the file example from the last section could be equivalently
  305. expressed as:
  306. @example
  307. (define-module (my-file)
  308. #:use-module (system foreign-object)
  309. #:use-module ((oop goops) #:select (make))
  310. #:export (make-file))
  311. (define (finalize-file file)
  312. (let ((fd (struct-ref file 0)))
  313. (unless (< fd 0)
  314. (struct-set! file 0 -1)
  315. (close-fdes fd))))
  316. (define <file>
  317. (make-foreign-object-type '<file> '(fd)
  318. #:finalizer finalize-file))
  319. (define (make-file fd)
  320. (make <file> #:fd fd))
  321. @end example
  322. Here we see that the result of @code{make-foreign-object-type}, which is
  323. the equivalent of @code{scm_make_foreign_object_type}, is a struct
  324. vtable. @xref{Vtables}, for more information. To instantiate the
  325. foreign object, which is really a Guile struct, we use @code{make}. (We
  326. could have used @code{make-struct/no-tail}, but as an implementation
  327. detail, finalizers are attached in the @code{initialize} method called
  328. by @code{make}). To access the fields, we use @code{struct-ref} and
  329. @code{struct-set!}. @xref{Structure Basics}.
  330. There is a convenience syntax, @code{define-foreign-object-type}, that
  331. defines a type along with a constructor, and getters for the fields. An
  332. appropriate invocation of @code{define-foreign-object-type} for the
  333. file object type could look like this:
  334. @example
  335. (use-modules (system foreign-object))
  336. (define-foreign-object-type <file>
  337. make-file
  338. (fd)
  339. #:finalizer finalize-file)
  340. @end example
  341. This defines the @code{<file>} type with one field, a @code{make-file}
  342. constructor, and a getter for the @code{fd} field, bound to @code{fd}.
  343. Foreign object types are not only vtables but are actually GOOPS
  344. classes, as hinted at above. @xref{GOOPS}, for more on Guile's
  345. object-oriented programming system. Thus one can define print and
  346. equality methods using GOOPS:
  347. @example
  348. (use-modules (oop goops))
  349. (define-method (write (file <file>) port)
  350. ;; Assuming existence of the `fd' getter
  351. (format port "#<<file> ~a>" (fd file)))
  352. (define-method (equal? (a <file>) (b <file>))
  353. (eqv? (fd a) (fd b)))
  354. @end example
  355. One can even sub-class foreign types.
  356. @example
  357. (define-class <named-file> (<file>)
  358. (name #:init-keyword #:name #:init-value #f #:accessor name))
  359. @end example
  360. The question arises of how to construct these values, given that
  361. @code{make-file} returns a plain old @code{<file>} object. It turns out
  362. that you can use the GOOPS construction interface, where every field of
  363. the foreign object has an associated initialization keyword argument.
  364. @example
  365. (define* (my-open-file name #:optional (flags O_RDONLY))
  366. (make <named-file> #:fd (open-fdes name flags) #:name name))
  367. (define-method (write (file <named-file>) port)
  368. (format port "#<<file> ~s ~a>" (name file) (fd file)))
  369. @end example
  370. @xref{Foreign Objects}, for full documentation on the Scheme interface
  371. to foreign objects. @xref{GOOPS}, for more on GOOPS.
  372. As a final note, you might wonder how this system supports encapsulation
  373. of sensitive values. First, we have to recognize that some facilities
  374. are essentially unsafe and have global scope. For example, in C, the
  375. integrity and confidentiality of a part of a program is at the mercy of
  376. every other part of that program -- because any part of the program can
  377. read and write anything in its address space. At the same time,
  378. principled access to structured data is organized in C on lexical
  379. boundaries; if you don't expose accessors for your object, you trust
  380. other parts of the program not to work around that barrier.
  381. The situation is not dissimilar in Scheme. Although Scheme's unsafe
  382. constructs are fewer in number than in C, they do exist. The
  383. @code{(system foreign)} module can be used to violate confidentiality
  384. and integrity, and shouldn't be exposed to untrusted code. Although
  385. @code{struct-ref} and @code{struct-set!} are less unsafe, they still
  386. have a cross-cutting capability of drilling through abstractions.
  387. Performing a @code{struct-set!} on a foreign object slot could cause
  388. unsafe foreign code to crash. Ultimately, structures in Scheme are
  389. capabilities for abstraction, and not abstractions themselves.
  390. That leaves us with the lexical capabilities, like constructors and
  391. accessors. Here is where encapsulation lies: the practical degree to
  392. which the innards of your foreign objects are exposed is the degree to
  393. which their accessors are lexically available in user code. If you want
  394. to allow users to reference fields of your foreign object, provide them
  395. with a getter. Otherwise you should assume that the only access to your
  396. object may come from your code, which has the relevant authority, or via
  397. code with access to cross-cutting @code{struct-ref} and such, which also
  398. has the cross-cutting authority.