[Bug c++/82736] New: -Wl not wrapping all functions call
federico.kircheis at gmail dot com
gcc-bugzilla@gcc.gnu.org
Thu Oct 26 15:52:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82736
Bug ID: 82736
Summary: -Wl not wrapping all functions call
Product: gcc
Version: 7.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: federico.kircheis at gmail dot com
Target Milestone: ---
AFAIK, std::chrono::system_clock::now(); uses clock_gettime internally.
For various reasons (unit testing), I would like to "mock" time calls, so I
tried to use the -Wl option, but it does not seem to work as expected.
Given the following program:
----------------------------------------
#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, funcname)).
Simply overwriting them seems to work, but is AFAIK UB.
Using "-Wl" works too, but only if using the C functions, not when using
std::chrono.
If it may be somehow relevant, clang++ (version 3.8.1-24) behaves exactly the
same way.
More information about the Gcc-bugs
mailing list