Determine all global variables of a module - llvm

How to determine all global variables of a LLVM module?
I want to modify them using a module pass.

llvm::Module class has getGlobalList() method:
/// Get the Module's list of global variables.
GlobalListType &getGlobalList() { return GlobalList; }
So you can do something like:
for (auto &Global : M->getModule()->getGlobalList()) ...

Related

jest.createMockFromModule vs jest.mock

I'm learning unit testing with jest. I can not understand the difference between the jest.createMockFromModule and jest.mock. Seems that they do the same? Can someone please explain me the difference?
jest.mock - does module auto-mocking and implicitly replaces imported module in the test file.
If you provide fabric function by the second argument it will define the module implementation like:
jest.mock('./moduleName', () => ({
default: 12345 // default constant definition
MyClass: { // Named export object or class definition
get: () => { /* get method class definition */ }
}
/* etc... */
}))
You also can override only part of imported module.
jest.createMockFromModule - generates auto-mocked module and returns it as a value. It useful in the manual mocking. You can override required module values:
// __mocks__/MyModule.js
const moduleName = jest.createMockFromModule('./moduleName');
moduleName.someNamedImport = () => 12345; // override method implementation
expect(moduleName.default.mocked).toBeTruthy(); // to check default import auto-mocking
expect(moduleName.someNamedImport()).toBe(12345)

Mocking functions with cmocka

I'm trying to mock some functions using cmocka:
void test_led_driver_NeedToImplement(void **state)
{
state_t current = CLEAR;
will_return(led_status_toggel,SET);
TEST_ASSERT_EQUAL(SET, led_status_toggel(current));
}
But, I get an error: led_status_toggel() has remaining non-returned values.
Do I have to create a mock file for my functions or what's the source of this error?
Ps: I'm using unity.h as an assertions library.
According to your test, it seams that function you are testing is led_status_toggel. If that is the case, you should not mock it. You should just remove will_return(led_status_toggel,SET);, since your led_status_toggel is probably something like this (You dind't share it so I don't know exactly):
state_t led_status_toggel(state_t state)
{
if (state == CLEAR)
{
return SET;
}
return CLEAR;
}
If your function under test is not led_status_toggel, but some other (that you didn't mentioned) which calls this led_status_toggel, then you can mock this function like this
state_t __wrap_led_status_toggel(state_t state)
{
return (state_t)mock();
}
and use -Wl,--wrap=led_status_toggel in your build command. With --wrap linker flag, when you execute your test, the mock function __wrap_led_status_toggel will be called instead of the original led_status_toggel.

call back (reinstantiation) of a module through another module at run time with change of parameter

I am using this module hierarchy :
Node: {udpApp[0]<->udp<->networkLayer->wlan[0]} and wlan[0]: {CNPCBeacon<->mac<->radio}
With some ini parameter for udpApp as:
I have given some initial parameter in the ini file for udpApp as :
**.host*.numUdpApps = 2
**.host*.udpApp[0].typename = "UDPBasicApp"
**.host*.udpApp[0].destAddresses = "gw1"
**.host*.udpApp[0].startTime = 1.32s
**.host*.udpApp[0].stopTime = 1.48s
But at run time I want to change the startTime and stopTime for udpAPP[0] through CNPCBeacon module.
Hence I changed CNPCBeacon.cc as:-
cModule* parentmod = getParentModule();
cModule* grantParentmod = parentmod->getParentModule();
cModule* udpmod = grantParentmod->getSubmodule("udpApp",0);
double varHoldingStartTime = udpmod->par("startTime").doubleValue();
double varGoldingStopTime = udpmod->par("stopTime").doubleValue();
varHoldingStartTime = SIMTIME_DBL(4.2);
varGoldingStopTime = SIMTIME_DBL(4.5);
udpmod->par("startTime").setDoubleValue(varHoldingStartTime);
udpmod->par("stopTime").setDoubleValue(varGoldingStopTime);
EV<<"New start and stop time is "<<udpmod->par("startTime").str()<<"\t"<<udpmod->par("stopTime").str()<<endl;`
Which successfully change the parameters. However it doesn't initiate the udpApp[0] module again. So I try to use dynamic casting of this module as:
UDPBasicApp* udpBasicMod = dynamic_cast<UDPBasicApp*>(udpmod);
sendTimer = new cMessage("sendTimer");
scheduleAt(iniSchduleTime, sendTimer);
and it resulted in following error:-
error in module (CNPCBeacon) BSoneNode.gw1.wlan[0].CNPCBeacon (id=23) at event #1496, t=4: scheduleAt() of module (UDPBasicApp)BSoneNode.gw1.udpApp[0] called in the context of module (CNPCBeacon)BSoneNode.gw1.wlan[0].CNPCBeacon: method called from the latter module lacks Enter_Method() or Enter_Method_Silent()?.
Is there also any other way to instantiate a module through other sub module.
Thanks for this help.
The solution for reinitializing a module (target module) through another module (requesting module) is creating handleParamterChange() function in target module. handleParameterChange() is used to reread the changed parameter at tun time. However it won't start scheduleAt() event to reinitialize the event for the target module. So I just added the scheduleAt() event in this function as :
void UDPBasicApp:: handleParameterChange(const char* parname)
{
if(parname)
{
if((strcmp(parname, "startTime")==0) &&
(startTime != par("startTime").doubleValue())
startTime = par("startTime").doubleValue();
if(strcmp(parname,"stopTime")==0)&&
(stopTime != par("stopTime").doubleValue())
{
stopTime = par("stopTime").doubleValue();
selfMsg->setKind(START);
scheduleAt((simtime_t)(startTime), selfMsg);
}
}
Note here that selfMsg is defined in the initialize function of UdpBasciApp.cc in INET framework.
I am a bit lost in the hierarchy and relation between your modules and sub-modules, however I think if your want to create (or re-create) a module dynamically you could use the built-in approach suggested by OMNeT++: https://omnetpp.org/doc/omnetpp/manual/usman.html#sec186
Maybe you could use the one-liner directly once you have (re-)defined the parameter values:
cModuleType *moduleType = cModuleType::get("foo.nodes.WirelessNode");
cModule *mod = moduleType->createScheduleInit("node", this);
On the other hand you error message complains about: Enter_Method() and/or Enter_Method_Silent()
These macros should be used in case that you try to call a function of a module (example X::get()) from within another module:
Y::doSmthWithXsGet()
{
x->get();
}
For this to work Enter_Method() (or Enter_Method_Silent()) has to be written in the beginning of X::get()
X::get()
{
Enter_Method();
/* rest of the code */
}
You can read Direct Method Calls section of the OMNeT++ userman to see what that means.
The weird thing is that you are getting this error for the scheduleAt() method, which is a method which belongs to the fundamental OMNeT++ class cSimpleModule. That means, in order to use this method in your class you will have to inherit from cSimpleModule in your class definition.
Maybe simply doing something like:
class MyCurrentClass: public cSimpleModule
{
/* further class definition */
};
... could solve your prob.

Accessing module's local var

For testing reasons, is there a way to access a module's local variable?
module m {
var i = 0;
export function go() {
++i;
}
}
m.go();
expect(m["i"]).toBe(1); // m["i"] is undefined
[That is - is it possible to access a javascript function's local var?]
Ofcourse I can export it, or make it static and wrap it in a class, but I'm looking for something cleaner.
No, looking at the generated JavaScript helps in these situations:
var m;
(function (m) {
var i = 0;
function go() {
++i;
}
m.go = go;
})(m || (m = {}));
As you can see, var i is scoped within the function and therefore inaccessible outside that function.
Just follow your suggestion:
Of course I can export it, or make it static and wrap it in a class

node.js: any way to export ALL functions in a file en masse (e.g., to enable unit testing), vs. one by one

in node.js, is there any shortcut to export ALL functions in a given file? i want to do this for unit testing purposes, as my unit tests are in a separate file from my production code.
I know i can go through and export each function manually, as in:
exports.myFunction = myFunction;
But i'm wondering if there is a simpler/slicker way to do this.
(and yes, i realize for modularity reasons it isn't always a good idea to export all functions, but for unit testing purposes you do want to see all the little functions so you can test them piece by piece.)
Thanks!
You could do something like this:
// save this into a variable, so it can be used reliably in other contexts
var self = this;
// the scope of the file is the `exports` object, so `this === self === exports`
self.fnName = function () { ... }
// call it the same way
self.fnName();
Or this:
// You can declare your exported functions here
var file = module.exports = {
fn1: function () {
// do stuff...
},
fn2: function () {
// do stuff...
}
}
// and use them like this in the file as well
file.fn1();
Or this:
// each function is declared like this. Have to watch for typeos, as we're typing fnName twice
fnName = exports.fnName = function () { ... }
// now you can use them as file-scoped functions, rather than as properties of an object
fnName();
Mixin objects is the answer.
This lib can help you: https://github.com/shimondoodkin/nodejs-clone-extend
//file1.js
var _ = require('cloneextend');
_.extend(this, require('file2.js'));
file1.js has now all exports from file2.js
Here's a simple way to do it. Parse the AST and look for top level function definitions, and export those.
const esprima = require('esprima')
const program = fs.readFileSync(__filename,'utf8')
const parsed = esprima.parseScript(program)
for (let fn of parsed.body) {
if (fn.type.endsWith('FunctionDeclaration')) {
module.exports[fn.id.name] = eval(fn.id.name)
}
}