Skip to content
Merged
Show file tree
Hide file tree
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
131 changes: 123 additions & 8 deletions runtime/onert/backend/trix/ops/test/mock_syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,128 @@
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <functional>
#include <memory>
#include <cstdarg>

int open(const char *, int, ...) { return 0; }
void *mmap(void *, size_t, int, int, int, off_t) { return (void *)0x1; }
int munmap(void *, size_t) { return 0; }
int close(int) { return 0; }
int ioctl(int, unsigned long, ...) { return 0; }
size_t fread(void *, size_t, size_t, FILE *) { return 1; }
int fseek(FILE *, long, int) { return 0; }
namespace onert
{
namespace backend
{
namespace trix
{
namespace ops
{
namespace test
{
Comment on lines +28 to +37
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update on next PR

Suggested change
namespace onert
{
namespace backend
{
namespace trix
{
namespace ops
{
namespace test
{
namespace onert::backend::trix::ops::test
{


#endif
class MockSyscallsManager
{
public:
// Function type definitions for each syscall
// Note: std::function doesn't work well with variadic functions, so we use specific signatures
using OpenHook = std::function<int(const char *, int)>;
using OpenCreatHook = std::function<int(const char *, int, mode_t)>;
using MmapHook = std::function<void *(void *, size_t, int, int, int, off_t)>;
using MunmapHook = std::function<int(void *, size_t)>;
using CloseHook = std::function<int(int)>;
using IoctlHook = std::function<int(int, unsigned long, void *)>;
using FopenHook = std::function<FILE *(const char *, const char *)>;
using FcloseHook = std::function<int(FILE *)>;
using FreadHook = std::function<size_t(void *, size_t, size_t, FILE *)>;
using FseekHook = std::function<int(FILE *, long, int)>;

static MockSyscallsManager &getInstance()
{
static MockSyscallsManager instance;
return instance;
}

// Hook registration functions
void setOpenHook(OpenHook hook) { _openHook = hook; }
void setOpenCreatHook(OpenCreatHook hook) { _openCreatHook = hook; }
void setMmapHook(MmapHook hook) { _mmapHook = hook; }
void setMunmapHook(MunmapHook hook) { _munmapHook = hook; }
void setCloseHook(CloseHook hook) { _closeHook = hook; }
void setIoctlHook(IoctlHook hook) { _ioctlHook = hook; }
void setFopenHook(FopenHook hook) { _fopenHook = hook; }
void setFcloseHook(FcloseHook hook) { _fcloseHook = hook; }
void setFreadHook(FreadHook hook) { _freadHook = hook; }
void setFseekHook(FseekHook hook) { _fseekHook = hook; }

// Hook retrieval functions
OpenHook getOpenHook() const { return _openHook; }
OpenCreatHook getOpenCreatHook() const { return _openCreatHook; }
MmapHook getMmapHook() const { return _mmapHook; }
MunmapHook getMunmapHook() const { return _munmapHook; }
CloseHook getCloseHook() const { return _closeHook; }
IoctlHook getIoctlHook() const { return _ioctlHook; }
FopenHook getFopenHook() const { return _fopenHook; }
FcloseHook getFcloseHook() const { return _fcloseHook; }
FreadHook getFreadHook() const { return _freadHook; }
FseekHook getFseekHook() const { return _fseekHook; }

// Hook clearing functions
void clearOpenHook() { _openHook = nullptr; }
void clearOpenCreatHook() { _openCreatHook = nullptr; }
void clearMmapHook() { _mmapHook = nullptr; }
void clearMunmapHook() { _munmapHook = nullptr; }
void clearCloseHook() { _closeHook = nullptr; }
void clearIoctlHook() { _ioctlHook = nullptr; }
void clearFopenHook() { _fopenHook = nullptr; }
void clearFcloseHook() { _fcloseHook = nullptr; }
void clearFreadHook() { _freadHook = nullptr; }
void clearFseekHook() { _fseekHook = nullptr; }

// Reset all hooks
void resetAll()
{
clearOpenHook();
clearOpenCreatHook();
clearMmapHook();
clearMunmapHook();
clearCloseHook();
clearIoctlHook();
clearFopenHook();
clearFcloseHook();
clearFreadHook();
clearFseekHook();
}

private:
MockSyscallsManager() = default;
~MockSyscallsManager() = default;
MockSyscallsManager(const MockSyscallsManager &) = delete;
MockSyscallsManager &operator=(const MockSyscallsManager &) = delete;

// Hook function pointers
OpenHook _openHook;
OpenCreatHook _openCreatHook;
MmapHook _mmapHook;
MunmapHook _munmapHook;
CloseHook _closeHook;
IoctlHook _ioctlHook;
FopenHook _fopenHook;
FcloseHook _fcloseHook;
FreadHook _freadHook;
FseekHook _fseekHook;
};

} // namespace test
} // namespace ops
} // namespace trix
} // namespace backend
} // namespace onert

// Mock syscall implementations
int open(const char *pathname, int flags, ...);
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
int munmap(void *addr, size_t length);
int close(int fd);
int ioctl(int fd, unsigned long request, ...);
FILE *fopen(const char *path, const char *mode);
int fclose(FILE *stream);
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
int fseek(FILE *stream, long offset, int whence);

#endif // _MOCK_SYSCALLS_H_
127 changes: 127 additions & 0 deletions runtime/onert/backend/trix/ops/test/mock_syscalls.test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "mock_syscalls.h"

int open(const char *pathname, int flags, ...)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous form of open() was,

int open(const char *, int, ...) { return 0; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();

// Handle variable arguments for open()
if (flags & O_CREAT)
{
if (auto creatHook = manager.getOpenCreatHook())
{
va_list args;
va_start(args, flags);
mode_t mode = va_arg(args, mode_t);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter 'mode' is only required when a file is created (O_CREAT).

va_end(args);
return creatHook(pathname, flags, mode);
}
}
else
{
if (auto hook = manager.getOpenHook())
{
return hook(pathname, flags);
}
}
return 0; // Default mock return value
}

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous definition of mmap() was,

void *mmap(void *, size_t, int, int, int, off_t) { return (void *)0x1; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getMmapHook())
{
return hook(addr, length, prot, flags, fd, offset);
}
return (void *)0x1; // Default mock return value
}

int munmap(void *addr, size_t length)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous definition of munmap() was,

int munmap(void *, size_t) { return 0; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getMunmapHook())
{
return hook(addr, length);
}
return 0; // Default mock return value
}

int close(int fd)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous definition of close(),

int close(int) { return 0; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getCloseHook())
{
return hook(fd);
}
return 0; // Default mock return value
}

int ioctl(int fd, unsigned long request, ...)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous definition of ioctl(),

int ioctl(int, unsigned long, ...) { return 0; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getIoctlHook())
{
va_list args;
va_start(args, request);
void *arg = va_arg(args, void *);
va_end(args);
return hook(fd, request, arg);
}
return 0; // Default mock return value
}

FILE *fopen(const char *path, const char *mode)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous definition of fopen()

size_t fread(void *, size_t, size_t, FILE *) { return 1; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getFopenHook())
{
return hook(path, mode);
}
return (FILE *)0x1; // Default mock return value
}

int fclose(FILE *stream)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fclose() is newly added.

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getFcloseHook())
{
return hook(stream);
}
return 0;
}

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous definition of fread()

size_t fread(void *, size_t, size_t, FILE *) { return 1; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getFreadHook())
{
return hook(ptr, size, nmemb, stream);
}
return 1; // Default mock return value
}

int fseek(FILE *stream, long offset, int whence)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous definition of fseek()

int fseek(FILE *, long, int) { return 0; }

{
auto &manager = onert::backend::trix::ops::test::MockSyscallsManager::getInstance();
if (auto hook = manager.getFseekHook())
{
return hook(stream, offset, whence);
}
return 0; // Default mock return value
}