@@ -46,6 +46,29 @@ using namespace reflection;
46
46
#include < objc/objc.h>
47
47
#endif
48
48
49
+ #if __has_include(<mach-o/dyld_priv.h>)
50
+ #include < mach-o/dyld_priv.h>
51
+ #define SWIFT_HAS_DYLD_IS_MEMORY_IMMUTABLE
52
+ #endif
53
+
54
+ // / If the target platform has an API for asking whether an address is mapped
55
+ // / from immutable pages of an executable image, this returns true if the
56
+ // / given address is *not* from an executable image. Otherwise, this always
57
+ // / returns false. The intent is to check that this returns false as a defense
58
+ // / for APIs that expect to operate on immutable memory to prevent them from
59
+ // / being fed untrusted data by an attacker, when the platform makes that
60
+ // / possible.
61
+ static bool isKnownToBeInMutableMemory (const void *base, size_t size) {
62
+ #if defined(SWIFT_HAS_DYLD_IS_MEMORY_IMMUTABLE)
63
+ if (__builtin_available (macOS 10.12 , iOS 10.0 , tvOS 10.0 , watchOS 3.0 , *))
64
+ return !_dyld_is_memory_immutable (base, size);
65
+ else
66
+ return false ;
67
+ #else
68
+ return false ;
69
+ #endif
70
+ }
71
+
49
72
// / Produce a Demangler value suitable for resolving runtime type metadata
50
73
// / strings.
51
74
static Demangler getDemanglerForRuntimeTypeResolution () {
@@ -55,8 +78,14 @@ static Demangler getDemanglerForRuntimeTypeResolution() {
55
78
// mangled name we can immediately find the associated metadata.
56
79
dem.setSymbolicReferenceResolver ([&](int32_t offset,
57
80
const void *base) -> NodePointer {
58
- auto absolute_addr = (uintptr_t )detail::applyRelativeOffset (base, offset);
59
- auto reference = dem.createNode (Node::Kind::SymbolicReference, absolute_addr);
81
+ // Only read symbolic references out of constant memory.
82
+ if (isKnownToBeInMutableMemory (base, sizeof (int )))
83
+ return nullptr ;
84
+
85
+ auto absolute_addr = detail::applyRelativeOffset (base, offset);
86
+
87
+ auto reference = dem.createNode (Node::Kind::SymbolicReference,
88
+ (uintptr_t )absolute_addr);
60
89
auto type = dem.createNode (Node::Kind::Type);
61
90
type->addChild (reference, dem);
62
91
return type;
0 commit comments