Allow clients of mapMemory to recover.
Before, memory could only be nullptr if a mapper instance returned
nullptr. However, sometimes this method would abort. This is
problematic, for instance, when unknown code sends an instance
of hidl_memory to another process. You are forced to manually
write the contents of this mapMemory method with the proper
error handling or to risk your process being aborted. Since this
method already returns nullptr sometimes, and the default usecase
is to pass things into this method which are from another process,
allowing users of this method to handle errors manually will
close a whole class of errors.
Test: (sanity) hidl_test, internal device boots
Fixes: 38377981
Change-Id: Ida6e73b224da34175746e86a08f545ef6db92293
diff --git a/libhidlmemory/include/hidlmemory/mapping.h b/libhidlmemory/include/hidlmemory/mapping.h
index 8ed0d54..5e1dab3 100644
--- a/libhidlmemory/include/hidlmemory/mapping.h
+++ b/libhidlmemory/include/hidlmemory/mapping.h
@@ -19,7 +19,11 @@
namespace android {
namespace hardware {
+/**
+ * Returns the IMemory instance corresponding to a hidl_memory object.
+ * If the shared memory cannot be fetched, this returns nullptr.
+ */
sp<android::hidl::memory::V1_0::IMemory> mapMemory(const hidl_memory &memory);
} // namespace hardware
-} // namespace android
\ No newline at end of file
+} // namespace android
diff --git a/libhidlmemory/mapping.cpp b/libhidlmemory/mapping.cpp
index 3761f99..f4bb21e 100644
--- a/libhidlmemory/mapping.cpp
+++ b/libhidlmemory/mapping.cpp
@@ -33,17 +33,20 @@
sp<IMapper> mapper = IMapper::getService(memory.name(), true /* getStub */);
if (mapper == nullptr) {
- LOG(FATAL) << "Could not fetch mapper for " << memory.name() << " shared memory";
+ LOG(ERROR) << "Could not fetch mapper for " << memory.name() << " shared memory";
+ return nullptr;
}
if (mapper->isRemote()) {
- LOG(FATAL) << "IMapper must be a passthrough service.";
+ LOG(ERROR) << "IMapper must be a passthrough service.";
+ return nullptr;
}
Return<sp<IMemory>> ret = mapper->mapMemory(memory);
if (!ret.isOk()) {
- LOG(FATAL) << "hidl_memory map returned transport error.";
+ LOG(ERROR) << "hidl_memory map returned transport error.";
+ return nullptr;
}
return ret;