Changeset: 1b9f39d82528 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1b9f39d82528
Modified Files:
        sql/backends/monet5/UDF/capi/Tests/capi03.stable.err
        sql/backends/monet5/UDF/capi/Tests/capi03.stable.out
        sql/backends/monet5/UDF/capi/Tests/capi04.sql
        sql/backends/monet5/UDF/capi/capi.c
Branch: jitudf
Log Message:

Create temporary files in the LEFTOVERS directory of the BBP farm.


diffs (151 lines):

diff --git a/sql/backends/monet5/UDF/capi/Tests/capi03.stable.err 
b/sql/backends/monet5/UDF/capi/Tests/capi03.stable.err
--- a/sql/backends/monet5/UDF/capi/Tests/capi03.stable.err
+++ b/sql/backends/monet5/UDF/capi/Tests/capi03.stable.err
@@ -36,6 +36,12 @@ ERROR = !No data returned.
 MAPI  = (monetdb) /var/tmp/mtest-2836/.s.monetdb.35915
 QUERY = SELECT capi03(i) FROM integers;
 ERROR = !Something went wrong!
+MAPI  = (monetdb) /var/tmp/mtest-25067/.s.monetdb.39212
+QUERY = SELECT capi03(i) FROM integers;
+ERROR = !Attempting to write to the input or triggered a segfault/bus error
+MAPI  = (monetdb) /var/tmp/mtest-25067/.s.monetdb.39212
+QUERY = SELECT capi03(i) FROM integers;
+ERROR = !Attempting to write to the input or triggered a segfault/bus error
 
 # 17:24:55 >  
 # 17:24:55 >  "Done."
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi03.stable.out 
b/sql/backends/monet5/UDF/capi/Tests/capi03.stable.out
--- a/sql/backends/monet5/UDF/capi/Tests/capi03.stable.out
+++ b/sql/backends/monet5/UDF/capi/Tests/capi03.stable.out
@@ -87,6 +87,26 @@ Ready.
 #INSERT INTO integers VALUES (1), (2), (3), (4), (5);
 [ 5    ]
 #ROLLBACK;
+#START TRANSACTION;
+#CREATE FUNCTION capi03(inp INTEGER) RETURNS INTEGER LANGUAGE C {
+#      inp.data[0] = 10;
+#      result->initialize(result, inp.count);
+#      for(size_t i = 0; i < inp.count; i++) {
+#              result->data[i] = inp.data[i] * 2;
+#      }
+#};
+#CREATE TABLE integers(i INTEGER);
+#INSERT INTO integers VALUES (1), (2), (3), (4), (5);
+[ 5    ]
+#ROLLBACK;
+#START TRANSACTION;
+#CREATE FUNCTION capi03(inp INTEGER) RETURNS INTEGER LANGUAGE C {
+#      int x = *((int*)NULL);
+#};
+#CREATE TABLE integers(i INTEGER);
+#INSERT INTO integers VALUES (1), (2), (3), (4), (5);
+[ 5    ]
+#ROLLBACK;
 
 # 17:24:55 >  
 # 17:24:55 >  "Done."
diff --git a/sql/backends/monet5/UDF/capi/Tests/capi04.sql 
b/sql/backends/monet5/UDF/capi/Tests/capi04.sql
--- a/sql/backends/monet5/UDF/capi/Tests/capi04.sql
+++ b/sql/backends/monet5/UDF/capi/Tests/capi04.sql
@@ -15,7 +15,6 @@ CREATE FUNCTION capi04(inp STRING) RETUR
                        result->data[i] = malloc(strlen(inp.data[i]) + 2);
                        strcpy(result->data[i] + 1, inp.data[i]);
                        result->data[i][0] = 'H';
-                       printf("%s\n", result->data[i]);
                }
        }
 };
diff --git a/sql/backends/monet5/UDF/capi/capi.c 
b/sql/backends/monet5/UDF/capi/capi.c
--- a/sql/backends/monet5/UDF/capi/capi.c
+++ b/sql/backends/monet5/UDF/capi/capi.c
@@ -223,6 +223,8 @@ static daytime time_from_data(cudf_data_
 static void data_from_timestamp(timestamp d, cudf_data_timestamp* ptr);
 static timestamp timestamp_from_data(cudf_data_timestamp* ptr);
 
+static char valid_path_characters[] = "abcdefghijklmnopqrstuvwxyz";
+
 static str
 CUDFeval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci, bit grouped) 
{
        sql_func * sqlfun = NULL;
@@ -232,6 +234,7 @@ CUDFeval(Client cntxt, MalBlkPtr mb, Mal
        char argbuf[64];
        char buf[BUFSIZ];
        char fname[BUFSIZ];
+       char oname[BUFSIZ];
        char libname[BUFSIZ];
        char error_buf[BUFSIZ];
        char total_error_buf[8192];
@@ -358,9 +361,52 @@ CUDFeval(Client cntxt, MalBlkPtr mb, Mal
                }
        }
 
-       // name of the source file and lib file that will be generated
-       snprintf(fname, BUFSIZ, "%s.c", funcname);
-       snprintf(libname, BUFSIZ, "lib%s%s", funcname, SO_EXT);
+       {
+               const int RANDOM_NAME_SIZE = 32;
+               char* path = NULL;
+               const char* prefix = "LEFTOVERS"DIR_SEP_STR;
+               size_t prefix_size = strlen(prefix);
+
+               memcpy(buf, prefix, sizeof(char) * strlen(prefix));
+               // generate a random 32-character name for the temporary files
+               for(i = prefix_size; i < prefix_size + RANDOM_NAME_SIZE; i++) {
+                       buf[i] = valid_path_characters[rand() % 
(sizeof(valid_path_characters) - 1)];
+               }
+               buf[i] = '\0';
+               path = GDKfilepath(0, BATDIR, buf, "c");
+               if (!path) {
+                       msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
+                       goto wrapup;
+               }
+               strcpy(fname, path);
+               strcpy(oname, fname);
+               oname[strlen(oname) - 1] = 'o';
+               GDKfree(path);
+
+               memmove(buf + strlen(SO_PREFIX) + prefix_size, buf + 
prefix_size, i + 1 - prefix_size);
+               memcpy(buf + prefix_size, SO_PREFIX, sizeof(char) * 
strlen(SO_PREFIX));
+               path = GDKfilepath(0, BATDIR, buf, SO_EXT[0] == '.' ? SO_EXT + 
1 : SO_EXT);
+               if (!path) {
+                       msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
+                       goto wrapup;
+               }
+               strcpy(libname, path);
+               GDKfree(path);
+       }
+
+       // if leftovers directory does not exist, create it
+       {
+               char* leftdirpath = GDKfilepath(0, NULL, LEFTDIR, NULL);
+               if (!leftdirpath) {
+                       msg = createException(MAL, "cudf.eval", 
MAL_MALLOC_FAIL);
+                       goto wrapup;
+               }
+               if (mkdir(leftdirpath, 0755) < 0 && errno != EEXIST) {
+                       msg = createException(MAL, "cudf.eval", "cannot create 
directory %s\n", leftdirpath);
+                       goto wrapup;
+               }
+               GDKfree(leftdirpath);
+       }
 
        // first generate the source file
        f = fopen(fname, "w+");
@@ -468,7 +514,7 @@ CUDFeval(Client cntxt, MalBlkPtr mb, Mal
 
        // now it's time to try to compile the code
        // we use popen to capture any error output
-       snprintf(buf, sizeof(buf), "%s -c -fPIC %s %s.c -o %s.o 2>&1 
>/dev/null", c_compiler, compilation_flags, funcname, funcname);
+       snprintf(buf, sizeof(buf), "%s -c -fPIC %s %s -o %s 2>&1 >/dev/null", 
c_compiler, compilation_flags, fname, oname);
        compiler = popen(buf, "r");
        if (!compiler) {
                msg = createException(MAL, "cudf.eval", "Failed popen");
@@ -490,7 +536,7 @@ CUDFeval(Client cntxt, MalBlkPtr mb, Mal
                msg = createException(MAL, "cudf.eval", "Failed to compile C 
UDF:\n%s", total_error_buf);
                goto wrapup;
        }
-       snprintf(buf, sizeof(buf), "%s %s.o -shared -o %s", c_compiler, 
funcname, libname);
+       snprintf(buf, sizeof(buf), "%s %s -shared -o %s", c_compiler, oname, 
libname);
        compiler = popen(buf, "r");
        if (!compiler) {
                msg = createException(MAL, "cudf.eval", "Failed popen");
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to