samee / obliv-c

Other
176 stars 57 forks source link

Can obliv-c complie c++ #89

Open siaaron045 opened 5 years ago

siaaron045 commented 5 years ago

I am sorry to trouble you. I have a question but do not know who can help me. I want to compile c++ project in obliv-c . Can obliv-c complie c++? the error is

/usr/include/string.h[43:0-19] : syntax error Parsing errorFatal error: exception Frontc.ParseError("Parse error")

Thank you very much!

weikengchen commented 5 years ago

Nope. Obliv-C relies on CIL, which can basically support many features in C, but no guarantee in C++. https://people.eecs.berkeley.edu/~necula/cil/

siaaron045 commented 5 years ago

Thank you for your reply. And do you know which garble project can compile c++ programs? Thanks a million.

samee commented 5 years ago

Thanks for your interest in this. The first step is figuring out how to mix C and C++ in general, without Obliv-C being in the mix. Here is a good place to get started: https://isocpp.org/wiki/faq/mixing-c-and-cpp, but you can probably find others online.

After that, you should be able to link Obliv-C functions and C++ together. You'll still have to keep the obliv parts away from C++, but things should otherwise work. Let us know if you bump into problems.

siaaron045 commented 5 years ago

I am trying to make it .But I do not know how oblivcc (in"oblivc/bin")works. It is a executable file. A: When i trying to create a .o file use the command(oblivcc)and then create a static lib .a file,it tell me that the .o file which i create in the last step is unrecognized file format. What oblivcc is ? Is it a compiler like gcc and g++? B: On the other hand,when i use g++ to create a .o file directly,the warning is header file was not find. I do not find them too, where are u put them?:) It may be that u were create a liboblivc.a file use all the .c file.Could you tell how to create a .a file use my own project together with your liboblivc.a . I have already created a c++ lib use my c++ project which has a independent compiler too but use oblivcc or g++ can not link it with oblivc file .So i plan to lib the oblivc file too. The main problem is whatever i use g++ or oblivcc can not create static lib. Thanks a million.

samee commented 5 years ago

oblivcc is just a Bash shell script. You can open it up in your editor or just look here to learn what it does: https://github.com/samee/obliv-c/blob/obliv-c/bin/oblivcc

Can you list the exact commands you are trying? And the path where your files are? It's hard to figure out what's wrong from the description.

siaaron045 commented 5 years ago

I have created three files "gazelle.c" “gazelle.h” and “gazelle.oc”in oblivc/test/oblivc/garble.They can run accurately. Then i want to pack them to a static lib (already transformed the main() into garble() ). Commands: ../../../bin/oblivcc -c gazelle.c gazelle.oc #create a gazelle.o ar cr libgable.a gazelle.o #create a static lib libgable.a ../../../bin/oblivcc main.c -L. -lobliv -lgable #link the main.c together with libobliv.a and libgable.a the error is : /tmp/cil-Rj29F_ej.o:in function ‘main’: cil-InHj3WZ2.cil.c:(.text+0xa):‘garble’ undefined reference I am trying to change the gcc in oblivcc to g++,but the error still exists. I have already defined the function garble() in main.c and main.h. what libobliv.a is? Is it a static lib include all the files you created? Thank you!

samee commented 5 years ago

Yes, libobliv.a gets created in _build/ once you run make in the Obliv-C source folder. Try to simplify the second oblivcc to just directly calling gcc with -L../../../_build. If that doesn't work attach the source files here and we can take a look.

Btw, you shouldn't need to create a .a archive. It should be possible to directly link in a .o object file. My wild guess is that C++ name mangling was biting you, but your example doesn't use C++, so I am a bit confused right now.

siaaron045 commented 5 years ago

I have tried it again and again,but failed. garble.zip I would appreciate it if you could spare some time to help me.

samee commented 5 years ago

Ok, I made a few changes to make this work:

$ diff -ru garble garble-modified/
diff -ru garble/gazelle.c garble-modified/gazelle.c
--- garble/gazelle.c    2019-04-27 13:23:22.000000000 -0700
+++ garble-modified/gazelle.c   2019-04-27 11:36:51.730552256 -0700
@@ -4,7 +4,7 @@
 #include<pthread.h>
 //#include<gazelle.oc>
 #include"gazelle.h"
-void sereveprog(void *args)
+void* sereveprog(void *args)
 {
   ProtocolDesc pd;
   Args *ref=args;
@@ -15,8 +15,9 @@
   setCurrentParty(&pd,1);
   execYaoProtocol(&pd,gazelle,&(*ref).io);
   cleanupProtocol(&pd);
+  return NULL;
 }
-void clientprog(void *args)
+void* clientprog(void *args)
 {
   ProtocolDesc pd;
   Args *ref=args;
@@ -27,9 +28,10 @@
   setCurrentParty(&pd,2);
   execYaoProtocol(&pd,gazelle,&(*ref).io);
   cleanupProtocol(&pd);
+  return NULL;
 }
-extern "C"
-{
 int garble()
 {
  // helib();
@@ -121,4 +123,4 @@
   printf("\n");
   return 0;
 }
-}
Only in garble-modified/: gazelle.c.o
diff -ru garble/gazelle.h garble-modified/gazelle.h
--- garble/gazelle.h    2019-04-25 15:15:44.000000000 -0700
+++ garble-modified/gazelle.h   2019-04-27 11:37:09.127684745 -0700
@@ -29,5 +29,5 @@
   char* port;
 }Args;
 void gazelle(void* args);
-void sereveprog(void *args);
-void clientprog(void *args);
+void* sereveprog(void *args);
+void* clientprog(void *args);
Only in garble-modified/: gazelle.oc.o
diff -ru garble/main.c garble-modified/main.c
--- garble/main.c   2019-04-27 13:28:50.000000000 -0700
+++ garble-modified/main.c  2019-04-27 11:39:09.472600331 -0700
@@ -1,19 +1,9 @@
-#include "main.h"
 #include <stdio.h>

-//include "hr-dmethod1.h"
-
-#ifdef __cplusplus
-extern "C"{
-#endif
 int garble();
-#ifdef _cplusplus
-}
-#endif
-//int helib();
+
 int main()
 {
-  // helib();
   garble();
   return 0;
 }

And then compilation (I didn't try running it, just compiled):

$ alias oblivcc=$HOME/Projects/obliv-c/bin/oblivcc
$ oblivcc -c gazelle.oc -o gazelle.oc.o
$ gcc -c main.c -o main.c.o
$ gcc -c gazelle.c -o gazelle.c.o -I $HOME/Projects/obliv-c/src/ext/oblivc
$ oblivcc gazelle.c.o gazelle.oc.o main.c.o -pthread

This creates an a.out. Hope this helps.

Now, a few points about C and C++ linking (these are independent of Obliv-C):

The other thing I noticed is that your client and server are in the same process, different threads. I don't know why you are doing that, but garbled circuits is meant for 2PC, which usually means two parties in two different machines. Let me know if you run into other issues.