Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions CppCoreGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,7 @@ Interface rule summary:
* [I.26: If you want a cross-compiler ABI, use a C-style subset](#ri-abi)
* [I.27: For stable library ABI, consider the Pimpl idiom](#ri-pimpl)
* [I.30: Encapsulate rule violations](#ri-encapsulate)
* [I.31: Ensure that dynamically loaded symbols respect their type](#ri-dlsyms)

**See also**:

Expand Down Expand Up @@ -2328,6 +2329,40 @@ Presumably, a bit of checking for potential errors would be added in real code.
* Hard, it is hard to decide what rule-breaking code is essential
* Flag rule suppression that enable rule-violations to cross interfaces

### <a name="ri-dlsyms></a>:I.31: Ensure that dynamically loaded symbols respect their type

##### Reason

To ensure safety across boundaries.

While there are lots of useful IPC techniques, many features are not feasible within a program without the use of dynamically loaded modules.
It is not a trivial task to confirm that the type of a symbol from a foreign module coincides with the signature loaded within the program source code, so it is better to implement dynamic loading sparingly.
Failure to do so results in either the program or the module being at compromise.

##### Example

We have a file that exposes a function that describes the quota of a device

extern "C" int query_quota() {
int q = 0;
// ...
return q;
}

To declare that a dynamic symbol has the same signature as the function `gsl::foreign` is used

void *handle = dlopen("device.so", RTLD_LAZY);
// ...
std::function<int()> query_quota(gsl::foreign<int (*)()>(dlsym(handle, "query_quota")));
Quota quota(query_quota());
// and when the module is not in use
dlclose(handle);

##### Enforcement

* Only dynamically loaded symbols are admitted when declaring via `gsl::foreign`
* APIs should declare typedefs for function signatures

# <a name="s-functions"></a>F: Functions

A function specifies an action or a computation that takes the system from one consistent state to the next. It is the fundamental building block of programs.
Expand Down
Loading