| 1 |
#ifndef RBIMPL_INTERN_MARSHAL_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_MARSHAL_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs related to rb_mMarshal. |
| 22 |
*/ |
| 23 |
#include "ruby/internal/dllexport.h" |
| 24 |
#include "ruby/internal/value.h" |
| 25 |
|
| 26 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 27 |
|
| 28 |
/* marshal.c */ |
| 29 |
|
| 30 |
/** |
| 31 |
* Serialises the given object and all its referring objects, to write them |
| 32 |
* down to the passed port. |
| 33 |
* |
| 34 |
* @param[in] obj Target object to dump. |
| 35 |
* @param[out] port IO-like destination buffer. |
| 36 |
* @exception rb_eTypeError `obj` cannot be dumped for some reason. |
| 37 |
* @exception rb_eRuntimeError `obj` was tampered during dumping. |
| 38 |
* @exception rb_eArgError Traversal too deep. |
| 39 |
* @return The passed `port` as-is. |
| 40 |
* @post Serialised representation of `obj` is written to `port`. |
| 41 |
* @note `port` is basically an IO but StringIO is also possible. |
| 42 |
*/ |
| 43 |
VALUE rb_marshal_dump(VALUE obj, VALUE port); |
| 44 |
|
| 45 |
/** |
| 46 |
* Deserialises a previous output of rb_marshal_dump() into a network of |
| 47 |
* objects. |
| 48 |
* |
| 49 |
* @param[in,out] port Either IO or String. |
| 50 |
* @exception rb_eTypeError `port` is in unexpected type. |
| 51 |
* @exception rb_eArgError Contents of `port` is broken. |
| 52 |
* @return Object(s) rebuilt using the info from `port`. |
| 53 |
* |
| 54 |
* SECURITY CONSIDERATIONS |
| 55 |
* ======================== |
| 56 |
* |
| 57 |
* @warning By design, rb_marshal_load() can deserialise almost any |
| 58 |
* class loaded into the Ruby process. In many cases this can |
| 59 |
* lead to remote code execution if the Marshal data is loaded |
| 60 |
* from an untrusted source. |
| 61 |
* @warning As a result, rb_marshal_load() is not suitable as a general |
| 62 |
* purpose serialisation format and you should never unmarshal |
| 63 |
* user supplied input or other untrusted data. |
| 64 |
* @warning If you need to deserialise untrusted data, use JSON or |
| 65 |
* another serialisation format that is only able to load |
| 66 |
* simple, 'primitive' types such as String, Array, Hash, etc. |
| 67 |
* Never allow user input to specify arbitrary types to |
| 68 |
* deserialise into. |
| 69 |
*/ |
| 70 |
VALUE rb_marshal_load(VALUE port); |
| 71 |
|
| 72 |
/** |
| 73 |
* Marshal format compatibility layer. Over time, classes evolve, so that |
| 74 |
* their internal data structure change drastically. For instance an instance |
| 75 |
* of ::rb_cRange was made of ::RUBY_T_OBJECT in 1.x., but in 3.x it is a |
| 76 |
* ::RUBY_T_STRUCT now. In order to keep binary compatibility, we "fake" the |
| 77 |
* marshalled representation to stick to old types. This is the API to enable |
| 78 |
* that manoeuvre. Here is how: |
| 79 |
* |
| 80 |
* First, because you are going to keep backwards compatibility, you need to |
| 81 |
* retain the old implementation of your class. Rename it, and keep the class |
| 82 |
* somewhere (for instance rb_register_global_address() could help). Next |
| 83 |
* create your new class. Do whatever you want. |
| 84 |
* |
| 85 |
* Then, this is the key point. Create two new "bridge" functions that convert |
| 86 |
* the structs back and forth: |
| 87 |
* |
| 88 |
* - the "dumper" function that takes an instance of the new class, and |
| 89 |
* returns an instance of the old one. This is called from |
| 90 |
* rb_marshal_dump(), to keep it possible for old programs to read your new |
| 91 |
* data. |
| 92 |
* |
| 93 |
* - the "loader" function that takes two arguments, new one and old one, in |
| 94 |
* that order. rb_marshal_load() calls this function when it finds a |
| 95 |
* representation of the retained old class. The old one passed to this |
| 96 |
* function is the reconstructed instance of the old class. |
| 97 |
* Reverse-engineer that to modify the new one, to have the identical |
| 98 |
* contents. |
| 99 |
* |
| 100 |
* Finally, connect all of them using this function. |
| 101 |
* |
| 102 |
* @param[in] newclass The class that needs conversion. |
| 103 |
* @param[in] oldclass Old implementation of `newclass`. |
| 104 |
* @param[in] dumper Function that converts `newclass` to `oldclass`. |
| 105 |
* @param[in] loader Function that converts `oldclass` to `newclass`. |
| 106 |
* @exception rb_eTypeError `newclass` has no allocator. |
| 107 |
*/ |
| 108 |
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE (*dumper)(VALUE), VALUE (*loader)(VALUE, VALUE)); |
| 109 |
|
| 110 |
RBIMPL_SYMBOL_EXPORT_END() |
| 111 |
|
| 112 |
#endif /* RBIMPL_INTERN_MARSHAL_H */ |