llvm passing loaded value to an external function - llvm

I am trying create a pass that will pass loaded value to an external function.
This is what i am doing
cpProfFunc = M.getOrInsertFunction("_Z6cpProfiiiiii",Type::getVoidTy(*Context),Type::getInt8tInt8PtrTy(*Context),NULL);
if(isa<LoadInst>(&(*BI)) ) //next in block iterator
std::vector<Value*> a1(1);
LoadInst *CI = dyn_cast<LoadInst>(BI);
a1[0]=dyn_cast<ConstantExpr>(CI->getPointerOperand());
CallInst* newInst = CallInst::Create(cpProf,a1,"");
BB->getInstList().insert((Instruction*)CI, newInst);
This is giving me error.I dont know where i am wrong.Somebody,Please correct me.

You write that you receive the error:
Stack dump:
0. Program arguments: opt -load /home/anshul/llvm/llvm-3.2/Release+Asserts/lib/Ass1Pri.so -anshul_insert
1. Running pass 'test function exist' on module '<stdin>'.
./myscript: line 6: 10726 Segmentation fault (core dumped)
opt -load /home/anshul/llvm/llvm-3.2/Release+Asserts/lib/Ass1Pri.so -anshul_insert < a.bc > b1.bc
'main' function not found in module.
So it seems that a pass called "test function exist" is trying to actually run some bitcode file (probably invoking lli on it), but a main function is missing from that file.

Related

netcdf fortran how to retrieve the total number of global attributes

I am trying to figure out how to discover how many global attributes a netcdf file has. By doing so, I am using the call:
status = nf90_Inquire_Variable(ncid, NF90_GLOBAL, nAtts=natts)
In order to test it I created a simple example. First, it creates a file with global attributes and then it tries to read it:
...
! create the file
call check( nf90_create("test.nc", NF90_NETCDF4, ncid) )
call check( nf90_put_att(ncid, NF90_GLOBAL, "date", "01/01/2021") )
call check( nf90_put_att(ncid, NF90_GLOBAL, "time", "00:00:00") )
call check( nf90_put_att(ncid, NF90_GLOBAL, "seconds", 1000) )
call check( nf90_close(ncid) )
! Read the file.
status = nf90_open("test.nc", NF90_NOWRITE, ncid)
call check(status)
! how many global attributes?
status = nf90_Inquire_Variable(ncid, NF90_GLOBAL, nAtts=natts)
call check(status)
! bye
status = nf90_close(ncid)
call check(status)
The netcdf file is properly created
netcdf simpletest {
// global attributes:
:date = "01/01/2021" ;
:time = "00:00:00" ;
:seconds = 1000 ;
}
But the following error shows up:
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7f34c7156d01 in ???
#1 0x7f34c7155ed5 in ???
#2 0x7f34c6e2020f in ???
#3 0x7f34c749304e in nf_inq_var_
at /to/some/path/netcdf-fortran-4.5.1/fortran/nf_genvar.f90:181
#4 0x7f34c74ebedf in __netcdf_MOD_nf90_inquire_variable
at /to/some/path/netcdf-fortran-4.5.1/fortran/netcdf4_variables.f90:293
Violació de segment (s'ha bolcat la memòria)
GDB stops at:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f5e04e in nf_inq_var (ncid=1, varid=0, name=..., xtype=1077936128, ndims=1073741824, dimids=..., natts=3, _name=256)
at nf_genvar.f90:181
warning: Source file is more recent than executable.
181 dimids(1:ndims) = cdimids(ndims:1:-1)+1
So it seems to me I am doing something wrong. Am I using the original call in a way it is meant to?
If not, how could it be done? By reading the netcdf fortran documentation I am not able to find it.
I am using the following versions of:
gfortran 9.3
zlib 1.2.11
hdf5 1.10.5
netcdf-c 4.7.1
netcdf-fortran 4.5.1
Edit:
As suggested in one of the comments below, I did a couple of minimum reproducible examples with different choices as I am a bit confused regarding the options.
I also created a new fortran bindings to the nc_inq_natts call but renamed to nf90_inq_natts.
a fortran file which creates and read a netcdf file
a c file which creates a netcdf file with 1 global attribute
a fortran file which only read the file from point 2
All files are compiled in debug mode when running.
The following outputs came up:
Create a new file (C):
*** SUCCESS writing example file simple_xy.nc!
Only read netcdf file (fortran):
nf90_inquire_variable ...
NetCDF: Variable not found
nf90_inq_natts ...
NetCDF: Not a valid ID
Global attributes (inquire_variable(GLOBAL)): 1788528357 <-- not working
Global attributes (inq_natts): 260315136 <-- not working
Create and read file (fortran):
nf90_inquire_variable ...
nf90_inq_natts ...
NetCDF: Not a valid ID
Global attributes (inquire_variable(GLOBAL)): 3 <-- it works
Global attributes (inq_natts): 1256308480 <-- not working
The results:
I cannot reproduce the error I first mentioned. :-/
nc_inq_natts does not seems to work when mixed with Fortran (NetCDF: Not a valid ID)
nf90_inquire_variables seems to work when the netcdf file is created in the same file (?). Regarding documention it is not supposed to happen.
C example taken from github works as expected
From a thread on the netcdfgroup mail list found by googling:
A variable ID of NF90_GLOBAL is explicitly allowed with
nf90_inquire_attribute, but not with nf90_inquire_variable. The intended
functions for discovering global attributes are nf90_inquire and
nf90_inquire_attribute. With a bit of study, the F90 documentation on this
topic seems rather clear to me.
Attempting to use nf90_inquire_variable for global attributes is a
mis-application of that function. Furthermore, that function is probably
returning the appropriate error code in this case, as documented: "The
variable ID is invalid for the specified netCDF dataset."
i.e., change the function you call to nf90_inquire_attribute.

gdb "Couldn't find method ..." (Method from a library)

I compiled my code only with -g flag. I have this exact expression in my code:
auto b = some_func(row[0].as<MyType>());
But when I want to inspect part of the expression:
(gdb) print row[0].as<MyType>()
Couldn't find method pqxx::field::as<MyType>
I get this error. Even though I run it in the debugger when in the same block of code the whole expression is.
(the library in which the method resides is a C++ PostgreSQL libpqxx.so)
(gdb 8.3)
That's not the only thing that doesn't work. When I do:
(gdb) print my_unordered_map.find(MyType(1))
A syntax error in expression, near `1))'.
Or:
(gdb) print my_unordered_map.find(my_lambda(row[0]))
Invalid data type for function to be called.
(also for just my_lambda(row[0])) Even though exactly this was compiled.

funny Code behaviour

I am trying to connect to a mysql database with the following code in my main() function.
MYSQL *connect = mysql_init(NULL);
which works fine and I get a pointer value returned.
I have created another c++ file with a function in it as below
int Newl_connection(FileHandler& pProcLog)
{
MYSQL *connect = mysql_init(NULL);
return 0;
}
compiling and Linking fine, I have made several calls to the same line of code in the main function (as a test) and all ok but when I run the code it fails at the line in the Newl_connection() function.
I have stripped the Newl_connection to as basic as possible, and included all the headers from the file containing Main() into the file containing Newl_connection() function.
THe compiler error I recieve is
Program received signal SIGABRT, Aborted.
0x00007ffff6bc6428 in __GI_raise (sig=sig#entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
Attempt to use a type name as an expression.
Attempt to use a type name as an expression
Attempt to use a type name as an expression. appears for all instance calls, but the call in the Newl_connection() function is terminal.
I have tried removing all other instances in main() and still get the same problem.
Why should code work in one place but behave differently in another?
Help would be appreciated I am at my witts end on this one.
According to your function header:
int Newl_connection(FileHandler& pProcLog)
you should call it with a valid object of type FileHandler.
Setting the variable name as pProcLog makes me think you want to use it as a pointer to an object. Then the declaration should be:
int Newl_connection(FileHandler* pProcLog)
This way you could call the function safe with a null value as argument.

How can I call a protobuf object's function in GDB?

The protobuf definition is:
message AttackUserInfo
{
required string name = 1;
repeated uint32 skill_ids = 2;
}
Now, there a protobuf object attackUserInfo.
I want to clear the skill_ids.What should I do in gdb?
I have called the function Clear_skill_ids().But It isn't effect.
gdb> call attackUserInfo.Clear_skill_ids() //It cored.
What is "It" in "It cored"? Did the program under GDB get SIGSEGV, or did GDB itself dump core?
If the latter, it's a bug in GDB. You should upgrade it to the latest version, and if that doesn't help, file a bug against GDB with repro instructions.

QSharedPointer Invalid Address specified to RtlFreeHeap

I have a program that makes heavy use of QSharedPointer. When I execute my program it runs fine, but when I debug it with GDB it starts throwing errors. "Invalid Address specified to RtlFreeHeap" is thrown in the following code:
QSharedPointer<PersistentList> p =
PersistentList::createEx(wrap("abc")).dynamicCast<PersistentList>();
QSharedPointer<IPersistentCollection> c = p->empty(); // Error thrown after this line
QSharedPointer<IPersistentCollection> ASeq::empty()
{
return QSharedPointer<EmptyList>(new EmptyList());
}
If I disable the p->empty() line the program runs just fine. Any ideas?
The problem is likely elsewhere in your code. Run it under Valgrind and see if you're touching any memory that's not yours, or using uninitialized data, etc.