Skip to content

Conversation

@batcheu
Copy link
Contributor

@batcheu batcheu commented Dec 3, 2025

It adds new MockSyscallsManager class to provide configurable hook system for mocking system calls in tests.

ONE-DCO-1.0-Signed-off-by: Jonghwa Lee [email protected]


It adds new MockSyscallsManager class to provide configurable hook system
for mocking system calls in tests.

ONE-DCO-1.0-Signed-off-by: Jonghwa Lee <[email protected]>
@batcheu batcheu force-pushed the add_MockSyscallManager branch from 9bacbd5 to 42cb6eb Compare December 3, 2025 07:19
@batcheu
Copy link
Contributor Author

batcheu commented Dec 3, 2025

Sample code is in below.

    MockSyscallsManager::getInstance().resetAll();


    // Add a hook for fread()
    MockSyscallsManager::getInstance().setFreadHook(
      [](void *ptr, size_t size, size_t, FILE *) -> int {
        if (size == NPUBIN_META_SIZE)
        {
          auto meta = reinterpret_cast<npubin_meta *>(ptr);
          meta->program_size = 1024;
          meta->weight_size = 1024;
          meta->size = 4096;
        }
        return 1;
      });


    MockSyscallsManager::getInstance().setIoctlHook(
      [](int, unsigned long request, void *arg) -> int {
        // Get Version
        if (request == _IOR(0x88, 1, unsigned int))
        {
          // Return version 3.2.X.X for trix backend sanity checking
          *static_cast<int *>(arg) = 0x3020000;
        }
        return 0;
      });


#include "mock_syscalls.h"

int open(const char *pathname, int flags, ...)
Copy link
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; }

{
va_list args;
va_start(args, flags);
mode_t mode = va_arg(args, mode_t);
Copy link
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).

return 0; // Default mock return value
}

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
Copy link
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; }

return (void *)0x1; // Default mock return value
}

int munmap(void *addr, size_t length)
Copy link
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; }

return 0; // Default mock return value
}

int close(int fd)
Copy link
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; }

return 0; // Default mock return value
}

int ioctl(int fd, unsigned long request, ...)
Copy link
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; }

return 0; // Default mock return value
}

FILE *fopen(const char *path, const char *mode)
Copy link
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; }

return (FILE *)0x1; // Default mock return value
}

int fclose(FILE *stream)
Copy link
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.

return 0;
}

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Copy link
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; }

return 1; // Default mock return value
}

int fseek(FILE *stream, long offset, int whence)
Copy link
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; }

Comment on lines +28 to +37
namespace onert
{
namespace backend
{
namespace trix
{
namespace ops
{
namespace test
{
Copy link
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
{

Copy link
Contributor

@hseok-oh hseok-oh left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@llFreetimell llFreetimell left a comment

Choose a reason for hiding this comment

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

LGTM

@hseok-oh hseok-oh merged commit e248efe into Samsung:master Dec 8, 2025
10 checks passed
@batcheu batcheu deleted the add_MockSyscallManager branch December 8, 2025 04:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants