Skip to content

Commit

Permalink
Add more checks to backends
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemartinlogan committed Dec 10, 2023
1 parent 725c0f8 commit d16781d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/hermes_shm/memory/backend/memory_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MemoryBackend {
bitfield32_t flags_;

public:
MemoryBackend() = default;
MemoryBackend() : header_(nullptr), data_(nullptr) {}

virtual ~MemoryBackend() = default;

Expand Down
16 changes: 12 additions & 4 deletions include/hermes_shm/memory/backend/memory_backend_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,30 @@ class MemoryBackendFactory {
if constexpr(std::is_same_v<PosixShmMmap, BackendT>) {
// PosixShmMmap
auto backend = std::make_unique<PosixShmMmap>();
backend->shm_init(size, url, std::forward<args>(args)...);
if (!backend->shm_init(size, url, std::forward<args>(args)...)) {
throw MEMORY_BACKEND_CREATE_FAILED.format();
}
return backend;
} else if constexpr(std::is_same_v<PosixMmap, BackendT>) {
// PosixMmap
auto backend = std::make_unique<PosixMmap>();
backend->shm_init(size, std::forward<args>(args)...);
if (!backend->shm_init(size, std::forward<args>(args)...)) {
throw MEMORY_BACKEND_CREATE_FAILED.format();
}
return backend;
} else if constexpr(std::is_same_v<NullBackend, BackendT>) {
// NullBackend
auto backend = std::make_unique<NullBackend>();
backend->shm_init(size, url, std::forward<args>(args)...);
if (!backend->shm_init(size, url, std::forward<args>(args)...)) {
throw MEMORY_BACKEND_CREATE_FAILED.format();
}
return backend;
} else if constexpr(std::is_same_v<ArrayBackend, BackendT>) {
// ArrayBackend
auto backend = std::make_unique<ArrayBackend>();
backend->shm_init(size, url, std::forward<args>(args)...);
if (!backend->shm_init(size, url, std::forward<args>(args)...)) {
throw MEMORY_BACKEND_CREATE_FAILED.format();
}
return backend;
} else {
throw MEMORY_BACKEND_NOT_FOUND.format();
Expand Down
3 changes: 3 additions & 0 deletions include/hermes_shm/memory/backend/posix_shm_mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define HERMES_INCLUDE_MEMORY_BACKEND_POSIX_SHM_MMAP_H

#include "memory_backend.h"
#include "hermes_shm/util/logging.h"
#include <string>

#include <stdio.h>
Expand Down Expand Up @@ -57,6 +58,7 @@ class PosixShmMmap : public MemoryBackend {
shm_unlink(url_.c_str());
fd_ = shm_open(url_.c_str(), O_CREAT | O_RDWR, 0666);
if (fd_ < 0) {
HILOG(kError, "shm_open failed: {}", strerror(errno));
return false;
}
_Reserve(size + HERMES_SYSTEM_INFO->page_size_);
Expand All @@ -74,6 +76,7 @@ class PosixShmMmap : public MemoryBackend {
url_ = std::move(url);
fd_ = shm_open(url_.c_str(), O_RDWR, 0666);
if (fd_ < 0) {
HILOG(kError, "shm_open failed: {}", strerror(errno));
return false;
}
header_ = _Map<MemoryBackendHeader>(HERMES_SYSTEM_INFO->page_size_, 0);
Expand Down
7 changes: 6 additions & 1 deletion include/hermes_shm/memory/memory_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "hermes_shm/memory/allocator/stack_allocator.h"
#include "hermes_shm/memory/backend/posix_mmap.h"
#include "hermes_shm/util/errors.h"
#include "hermes_shm/util/logging.h"

namespace hipc = hshm::ipc;

Expand Down Expand Up @@ -95,7 +96,11 @@ class MemoryRegistry {

/** Registers an allocator. */
HSHM_ALWAYS_INLINE void RegisterAllocator(Allocator *alloc) {
auto idx = alloc->GetId().ToIndex();
uint32_t idx = alloc->GetId().ToIndex();
if (idx > MAX_ALLOCATORS) {
HILOG(kError, "Allocator index out of range: {}", idx)
throw std::runtime_error("Too many allocators");
}
allocators_[idx] = alloc;
}

Expand Down
1 change: 1 addition & 0 deletions include/hermes_shm/util/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace hshm {
const Error SHMEM_CREATE_FAILED("Failed to allocate SHMEM");
const Error SHMEM_RESERVE_FAILED("Failed to reserve SHMEM");
const Error SHMEM_NOT_SUPPORTED("Attempting to deserialize a non-shm backend");
const Error MEMORY_BACKEND_CREATE_FAILED("Failed to load memory backend");
const Error MEMORY_BACKEND_NOT_FOUND("Failed to find the memory backend");
const Error NOT_ENOUGH_CONCURRENT_SPACE("{}: Failed to divide memory slot {} among {} devices");
const Error ALIGNED_ALLOC_NOT_SUPPORTED("Allocator does not support aligned allocations");
Expand Down

0 comments on commit d16781d

Please sign in to comment.