| | 1549 | |
|---|
| | 1550 | $(V2 |
|---|
| | 1551 | <h2>$(LNAME2 function-safety, Function Safety)</h2> |
|---|
| | 1552 | |
|---|
| | 1553 | $(P $(I Safe functions) are functions that are statically checked |
|---|
| | 1554 | to exhibit no possibility of |
|---|
| | 1555 | $(LINK2 glossary.html#undefined_behavior, $(I undefined behavior)). |
|---|
| | 1556 | Undefined behavior is often used as a vector for malicious |
|---|
| | 1557 | attacks. |
|---|
| | 1558 | ) |
|---|
| | 1559 | |
|---|
| | 1560 | <h3>$(LNAME2 safe-functions, Safe Functions)</h3> |
|---|
| | 1561 | |
|---|
| | 1562 | $(P Safe functions are marked with the $(CODE @safe) attribute.) |
|---|
| | 1563 | |
|---|
| | 1564 | $(P The following operations are not allowed in safe |
|---|
| | 1565 | functions:) |
|---|
| | 1566 | |
|---|
| | 1567 | $(UL |
|---|
| | 1568 | $(LI No casting from a pointer type to any type other than $(CODE void*).) |
|---|
| | 1569 | $(LI No casting from any non-pointer type to a pointer type.) |
|---|
| | 1570 | $(LI No modification of pointer values.) |
|---|
| | 1571 | $(LI Cannot access unions that have pointers or references overlapping |
|---|
| | 1572 | with other types.) |
|---|
| | 1573 | $(LI Calling any unsafe functions.) |
|---|
| | 1574 | $(LI No catching of exceptions that are not derived from $(CODE class Exception).) |
|---|
| | 1575 | $(LI No inline assembler.) |
|---|
| | 1576 | $(LI No explicit casting of mutable objects to immutable.) |
|---|
| | 1577 | $(LI No explicit casting of immutable objects to mutable.) |
|---|
| | 1578 | $(LI No explicit casting of thread local objects to shared.) |
|---|
| | 1579 | $(LI No explicit casting of shared objects to thread local.) |
|---|
| | 1580 | $(LI No taking the address of a local variable or function parameter.) |
|---|
| | 1581 | $(LI Cannot access $(D_KEYWORD __gshared) variables.) |
|---|
| | 1582 | ) |
|---|
| | 1583 | |
|---|
| | 1584 | $(P Functions nested inside safe functions default to being |
|---|
| | 1585 | safe functions. |
|---|
| | 1586 | ) |
|---|
| | 1587 | |
|---|
| | 1588 | $(P Safe functions are covariant with trusted or unsafe functions.) |
|---|
| | 1589 | |
|---|
| | 1590 | $(P $(B Note:) The verifiable safety of functions may be compromised by |
|---|
| | 1591 | bugs in the compiler and specification. Please report all such errors |
|---|
| | 1592 | so they can be corrected. |
|---|
| | 1593 | ) |
|---|
| | 1594 | |
|---|
| | 1595 | <h3>$(LNAME2 trusted-functions, Trusted Functions)</h3> |
|---|
| | 1596 | |
|---|
| | 1597 | $(P Trusted functions are marked with the $(CODE @trusted) attribute.) |
|---|
| | 1598 | |
|---|
| | 1599 | $(P Trusted functions are guaranteed by the programmer to not exhibit |
|---|
| | 1600 | any undefined behavior if called by a safe function. |
|---|
| | 1601 | Generally, trusted functions should be kept small so that they are |
|---|
| | 1602 | easier to manually verify. |
|---|
| | 1603 | ) |
|---|
| | 1604 | |
|---|
| | 1605 | $(P Trusted functions may call safe, trusted, or unsafe functions. |
|---|
| | 1606 | ) |
|---|
| | 1607 | |
|---|
| | 1608 | $(P Trusted functions are covariant with safe or unsafe functions.) |
|---|
| | 1609 | |
|---|
| | 1610 | <h3>$(LNAME2 unsafe-functions, Unsafe Functions)</h3> |
|---|
| | 1611 | |
|---|
| | 1612 | $(P Unsafe functions are functions not marked with $(CODE @safe) or |
|---|
| | 1613 | $(CODE @trusted) |
|---|
| | 1614 | and are not nested inside $(CODE @safe) functions. |
|---|
| | 1615 | A function being unsafe does not mean it actually is unsafe, it just |
|---|
| | 1616 | means that the compiler is unable to verify that it cannot exhibit |
|---|
| | 1617 | undefined behavior. |
|---|
| | 1618 | ) |
|---|
| | 1619 | |
|---|
| | 1620 | $(P Unsafe functions are $(B not) covariant with trusted or safe functions. |
|---|
| | 1621 | ) |
|---|
| | 1622 | ) |
|---|
| | 1623 | |
|---|