The 'parser' in lavaanC was written in two parts:
* a part in  pure C/C++ without the R interface things like SEXP, etc. ; taking 
as input a string (the model) and a bool (produce debuginfo yes/no) , and 
returning a structure with all parsed information and debuginfo (if requested) 
or an error code (and position in the model) if an error was detected in the 
model; (lav_SyntaxParser.cpp + lav_SyntaxParser.h + lav_Util.cpp + lav_Util.h + 
lav_SmallStringList.h)
* an interface function with R which is callable from R with .Call taking SEXP 
model and SEXP debug as input and returning a SEXP with the parsed information 
in about the same format as the lavaan parser written in R returns. This 
interface function also evaluates the parts of the model syntax that are 
written as an R expression (and that can be evaluated in the Global 
environment). (lav_parser_interface.cpp)

The first part of parser was written using Visual Studio (the IDE I know well 
(for C#) because I wrote many programs in C# in this environment), and I also 
wrote a main function as 'replacement' for the interface function in R to be 
able to test this first part in Visual Studio.

When searching for the reason Rstudio aborts the R session when executing 
tests, I found - thanks to the help of Ivan - that there were some memory 
allocation or usage problems in my code. Because I work most of the time in 
Windows and the Address Sanitizer in R is only available on Linux/MacOS (cfr 
Writing R Extensions 4.3.3) I felt stuck.

Because C/C++ is relatively new to me (about 3 months) I didn't know very well 
the different tools for C/C++ available in Visual Studio, but looking at the 
documentation of Dr Memory I realized that there should also be some tools in 
Visual Studio to check the memory allocation and usage in C++ programs.  So I 
found that in Visual Studio Project options -> C/C++  -> General there is an 
option "Enable Address Sanitizer". When I enabled it and ran the tests I found 
the problematic code where I allocated one byte less then needed to copy a 
string.

I have looked at the documentation of Dr. Memory but not yet installed it.


Luc

________________________________________
Van: Ivan Krylov <ikry...@disroot.org>
Verzonden: donderdag 14 november 2024 21:32
Aan: Luc De Wilde <luc.dewi...@ugent.be>
CC: r-package-devel@r-project.org <r-package-devel@r-project.org>; Yves Rosseel 
<yves.ross...@ugent.be>
Onderwerp: Re: [R-pkg-devel] New package with C++ code causes R abort in 
RStudio, not in R console.

В Thu, 14 Nov 2024 13:08:13 +0000
Luc De Wilde <luc.dewi...@ugent.be> пишет:

> At last, with checking of the program with address sanitizer in
> Visual Studio, I found an "off by 1 error" in my code. Now all tests
> pass without problems in Rstudio.

Congratulations on being able to solve the problem yourself!

It took me too long to figure out that the syntax errors I was getting
from lavaanC::lav_parse_model_string_c() were due to the invisible
U+00A0 (non-breakable space) characters from the e-mail. Once I ran
lav_parse_model_string_c(gsub('\ua0', ' ', model)) in a sanitized build
of R, I too saw the buffer overflow and a number of memory leaks.

Would you mind sharing with the list how you used address sanitizer
with R on Windows? Did you have to use the clang compiler, or have you
been able to use MSVC? Does Dr. Memory <https://drmemory.org/> find any
additional problems?

--
Best regards,
Ivan

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to