This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/22360] New: Wl not wrapping all functions call


https://sourceware.org/bugzilla/show_bug.cgi?id=22360

            Bug ID: 22360
           Summary: Wl not wrapping all functions call
           Product: glibc
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: federico.kircheis at gmail dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

Hello, I hope I'm reporting the bug to the right group.
I've opened the bug at gcc, but it seems to be an "issue" of the linker or
glibc (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82736).


I'm trying to mocking (wrapping) function calls for getting the current time.
I would like to do it mainly for unit testing.

I was able to use successfully "-Wl,wrap=time", "-Wl,wrap=clock_gettime" and so
one as parameters for the compiler (which are passed to the linker) for mocking
calls to time, gettime, and so on.
Unfortunately when using chrono, the wrapped function does not get called, here
is an example:

----------------------------------------
#include <chrono>
#include <cassert>
#include <time.h>

extern "C" {
#if USE_WRAP
int __wrap_clock_gettime(clockid_t, struct timespec *tp) {
#else
int clock_gettime(clockid_t, struct timespec *tp) {
#endif
    *tp = {};
    tp->tv_sec = 123;
    return 0;
}
}

int main(){
    clockid_t clk_id{};
    struct timespec tp;
    auto res = clock_gettime(clk_id, &tp);

    assert(res == 0);
    assert(tp.tv_sec == 123);

    auto now = std::chrono::system_clock::now();
    auto now_t = std::chrono::system_clock::to_time_t(now);
    assert(now_t == 123);
}
-------------------------------------------

When executing "g++ -std=c++11 main.cpp && ./a.out" the program runs and
returns 0, while when executing "g++ -std=c++11 -DUSE_WRAP
"-Wl,-wrap=clock_gettime" main.cpp && ./a.out" the program crashes with "int
main(): Assertion `now_c == 123' failed.".


AFAIK the version compiled without the macro USE_WRAP is UB , even if it works.
Therefore I would prefer to use the "-Wl" compiler option.

The statement "assert(tp.tv_sec == 123);" assures that the "mocked" version of
the function is called. This happens in both cases.


I've also tried to implement wrapping functions for time() and gettimeofday()
(with and without the wrap option), but it did not made any difference.

I've also tried to compile those functions in a shared library, but it did not
made a difference either.


I would like to find a robust way for wrapping all OS functions call for
querying what time it is and eventually call the original function through
dlsym(RTLD_NEXT, func_name)).


When linking statically (with "-static" or "-static-libstdc++"), the wrapped
function gets called. It would be of course great if it would be possible to
get the same behavior without linking statically.



If it may be somehow relevant, clang++ behaved exactly the same way in all
scenarios (with static linking, with and without the -Wl compiler option, when
compiling the functions as shared library and so on).

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]