1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A crazy linker test to test callbacks for delayed execution.
8 #include <crazy_linker.h>
10 #include "test_util.h"
12 typedef void (*FunctionPtr
)();
16 bool PostCallback(crazy_callback_t
* callback
, void* poster_opaque
) {
17 printf("Post callback, poster_opaque %p, handler %p, opaque %p\n",
22 // Copy callback to the address given in poster_opaque.
23 crazy_callback_t
* request
=
24 reinterpret_cast<crazy_callback_t
*>(poster_opaque
);
30 void CheckAndRunCallback(crazy_callback_t
* callback
) {
31 printf("Run callback, handler %p, opaque %p\n",
35 if (!callback
->handler
) {
36 Panic("Post for delayed execution not invoked\n");
39 // Run the callback, then clear it for re-use.
40 crazy_callback_run(callback
);
41 memset(callback
, 0, sizeof(*callback
));
47 crazy_context_t
* context
= crazy_context_create();
48 crazy_library_t
* library
;
51 crazy_context_set_load_address(context
, 0x20000000);
53 // Create a new callback, initialized to NULLs.
54 crazy_callback_t callback
= {NULL
, NULL
};
56 // Set a callback poster that copies its callback to &callback.
57 crazy_context_set_callback_poster(context
, &PostCallback
, &callback
);
59 crazy_callback_poster_t poster
;
62 // Check that the API returns the values we set.
63 crazy_context_get_callback_poster(context
, &poster
, &poster_opaque
);
64 if (poster
!= &PostCallback
|| poster_opaque
!= &callback
) {
65 Panic("Get callback poster error\n");
69 if (!crazy_library_open(&library
, "libfoo.so", context
)) {
70 Panic("Could not open library: %s\n", crazy_context_get_error(context
));
72 CheckAndRunCallback(&callback
);
74 // Find the "Foo" symbol.
76 if (!crazy_library_find_symbol(
77 library
, "Foo", reinterpret_cast<void**>(&foo_func
))) {
78 Panic("Could not find 'Foo' in libfoo.so\n");
85 crazy_library_close_with_context(library
, context
);
86 CheckAndRunCallback(&callback
);
88 crazy_context_destroy(context
);