Commit 5876aac9a8f8564c575e0e942516030820b06bd0

Baptiste 2024-07-24T20:15:58

wip make test_http

diff --git a/http/socket_addr.c b/http/socket_addr.c
new file mode 100644
index 0000000..e3612a8
--- /dev/null
+++ b/http/socket_addr.c
@@ -0,0 +1,48 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include <errno.h>
+#include <string.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#include <libkc3/kc3.h>
+#include "socket.h"
+#include "socket_buf.h"
+
+void socket_addr_delete (struct sockaddr *sa)
+{
+  assert(sa);
+  free(sa);
+}
+
+struct sockaddr * socket_addr_new (u32 len)
+{
+    struct sockaddr *sa;
+    assert(len);
+    sa = alloc(len);
+    if (! sa)
+      return NULL;
+    return sa;
+}
+
+struct sockaddr * socket_addr_new_copy (const struct sockaddr *addr,
+                                        u32 len)
+{
+    struct sockaddr *sa;
+    assert(len);
+    sa = alloc(len);
+    if (! sa)
+      return NULL;
+    memcpy(sa, addr, len);
+    return sa;
+}
diff --git a/http/socket_addr.h b/http/socket_addr.h
new file mode 100644
index 0000000..fee9024
--- /dev/null
+++ b/http/socket_addr.h
@@ -0,0 +1,24 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#ifndef SOCKET_ADDR_H
+#define SOCKET_ADDR_H
+
+#include "types.h"
+
+/* Heap-allocation functions. */
+void              socket_addr_delete (struct sockaddr *sa);
+struct sockaddr * socket_addr_new (u32 len);
+struct sockaddr * socket_addr_new_copy (const struct sockaddr *addr,
+                                        u32 len);
+
+#endif /* SOCKET_H */
diff --git a/http/socket_buf.c b/http/socket_buf.c
index 48b09f5..4ba63bc 100644
--- a/http/socket_buf.c
+++ b/http/socket_buf.c
@@ -16,6 +16,7 @@
 #include <netinet/in.h>
 #include <unistd.h>
 #include <libkc3/kc3.h>
+#include "socket_addr.h"
 #include "socket_buf.h"
 
 void socket_buf_clean (s_socket_buf *sb)
@@ -42,8 +43,13 @@ s_socket_buf * socket_buf_init (s_socket_buf *sb, t_socket sockfd,
   tmp.buf_rw.r.user_ptr = sb;
   tmp.buf_rw.w.user_ptr = sb;
   tmp.sockfd = sockfd;
+  tmp.addr = socket_addr_new_copy(addr, addr_len);
+  if (! tmp.addr) {
+    err_puts("socket_buf_init: socket_addr_new_copy");
+    assert(! "socket_buf_init: socket_addr_new_copy");
+    return NULL;
+  }
   tmp.addr_len = addr_len;
-  memcpy(&tmp.addr, addr, addr_len);
   *sb = tmp;
   return sb;
 }
@@ -89,6 +95,7 @@ s_socket_buf * socket_buf_init_connect (s_socket_buf *sb,
       error_reason = "socket_buf_init_connect: connect: ";
       goto next;
     }
+    break;
   next:
     res = res->ai_next;
   }
diff --git a/http/sources.mk b/http/sources.mk
index ec84b73..1d3ab37 100644
--- a/http/sources.mk
+++ b/http/sources.mk
@@ -1,12 +1,14 @@
 # sources.mk generated by update_sources
 HEADERS = \
 	"http.h" \
-	"socket.h" \
+	"socket_addr.h" \
 	"socket_buf.h" \
+	"socket.h" \
 	"types.h" \
 
 SOURCES = \
 	"http.c" \
-	"socket.c" \
+	"socket_addr.c" \
 	"socket_buf.c" \
+	"socket.c" \
 
diff --git a/http/sources.sh b/http/sources.sh
index 3a1b805..7e7e29e 100644
--- a/http/sources.sh
+++ b/http/sources.sh
@@ -1,3 +1,3 @@
 # sources.sh generated by update_sources
-HEADERS='http.h socket.h socket_buf.h types.h '
-SOURCES='http.c socket.c socket_buf.c '
+HEADERS='http.h socket_addr.h socket_buf.h socket.h types.h '
+SOURCES='http.c socket_addr.c socket_buf.c socket.c '
diff --git a/http/types.h b/http/types.h
index 8fea30a..e914894 100644
--- a/http/types.h
+++ b/http/types.h
@@ -25,10 +25,10 @@ typedef t_socket *p_socket;
 
 /* Struct 1 */
 struct socket_buf {
-  s_buf_rw buf_rw;
+  struct sockaddr *addr;
+  u32              addr_len;
   t_socket sockfd;
-  struct sockaddr_storage addr;
-  u32                     addr_len;
+  s_buf_rw buf_rw;
 };
 
 #endif /* HTTP_TYPES_H */
diff --git a/lib/kc3/0.1/ekc3.so b/lib/kc3/0.1/ekc3.so
index 2d95dd8..58dfce3 120000
--- a/lib/kc3/0.1/ekc3.so
+++ b/lib/kc3/0.1/ekc3.so
@@ -1 +1 @@
-../../../ekc3/.libs/libekc3.so.0.0
\ No newline at end of file
+../../../ekc3/.libs/libekc3_asan.so
\ No newline at end of file
diff --git a/lib/kc3/0.1/http.so b/lib/kc3/0.1/http.so
index d0160ff..1c2980b 120000
--- a/lib/kc3/0.1/http.so
+++ b/lib/kc3/0.1/http.so
@@ -1 +1 @@
-../../../http/.libs/libkc3_http.so.0.0
\ No newline at end of file
+../../../http/.libs/libkc3_http_asan.so
\ No newline at end of file
diff --git a/lib/kc3/0.1/ptr_free.facts b/lib/kc3/0.1/ptr_free.facts
index 320da15..b8ee96b 100644
--- a/lib/kc3/0.1/ptr_free.facts
+++ b/lib/kc3/0.1/ptr_free.facts
@@ -2,4 +2,4 @@
   version: 1}
 replace {PtrFree, :is_a, :module}
 replace {PtrFree, :symbol, PtrFree.cast}
-replace {PtrFree.cast, :symbol_value, cfn PtrFree "ptr_free_init_cast" (Result, Tag)}
+replace {PtrFree.cast, :symbol_value, cfn PtrFree "ptr_free_init_cast" (Result, Sym, Tag)}
diff --git a/lib/kc3/0.1/socket/buf.kc3 b/lib/kc3/0.1/socket/buf.kc3
index 78992dc..429fa5d 100644
--- a/lib/kc3/0.1/socket/buf.kc3
+++ b/lib/kc3/0.1/socket/buf.kc3
@@ -1,8 +1,8 @@
 defmodule Socket.Buf do
 
-  defstruct [buf_rw: %BufRW{},
+  defstruct [addr: (PtrFree) 0,
+             addr_len: (U32) 0,
              sockfd: (S32) -1,
-             addr: %Socket.Addr{},
-             addr_len: (U32) 0]
+             buf_rw: %BufRW{}]
 
 end
diff --git a/test/http/01_socket_buf.out.expected b/test/http/01_socket_buf.out.expected
index dc5e795..9ea280b 100644
--- a/test/http/01_socket_buf.out.expected
+++ b/test/http/01_socket_buf.out.expected
@@ -1,4 +1,4 @@
 %Socket.Buf{}
-%Socket.Buf{buf_rw: %BufRW{r: %Buf{column: 0, flush: (Ptr) 0x0, free: false, line: 0, ptr: (Ptr) 0x0, refill: (Ptr) 0x0, rpos: 0, save: (Ptr) 0x0, seek: (Ptr) 0x0, size: 0, user_ptr: (Ptr) 0x0, wpos: 0}, w: %Buf{column: 0, flush: (Ptr) 0x0, free: false, line: 0, ptr: (Ptr) 0x0, refill: (Ptr) 0x0, rpos: 0, save: (Ptr) 0x0, seek: (Ptr) 0x0, size: 0, user_ptr: (Ptr) 0x0, wpos: 0}}, sockfd: -1, addr: %Socket.Addr{data: (U8[256]) {}}, addr_len: 0}
+%Socket.Buf{addr: (PtrFree) 0x0, addr_len: 0, sockfd: -1, buf_rw: %BufRW{r: %Buf{column: 0, flush: (Ptr) 0x0, free: false, line: 0, ptr: (Ptr) 0x0, refill: (Ptr) 0x0, rpos: 0, save: (Ptr) 0x0, seek: (Ptr) 0x0, size: 0, user_ptr: (Ptr) 0x0, wpos: 0}, w: %Buf{column: 0, flush: (Ptr) 0x0, free: false, line: 0, ptr: (Ptr) 0x0, refill: (Ptr) 0x0, rpos: 0, save: (Ptr) 0x0, seek: (Ptr) 0x0, size: 0, user_ptr: (Ptr) 0x0, wpos: 0}}}
 %Socket{}
 %Socket{fd: -1}
diff --git a/test/http/03_server.out.expected b/test/http/03_server.out.expected
index d7cff23..7fabd81 100644
--- a/test/http/03_server.out.expected
+++ b/test/http/03_server.out.expected
@@ -1,12 +1,77 @@
 server = Socket.listen("localhost", "8000")
-%Socket{fd: 4}
+%Socket{fd: 7}
 client = Socket.connect("localhost", "8000")
-%Socket{fd: 5}
-server_client = Socket.accept(server)
-%Socket{fd: 6}
-Socket.close(client)
-void
-Socket.close(server_client)
-void
-Socket.close(server)
-void
+=================================================================
+==79724==ERROR: AddressSanitizer: unknown-crash on address 0x6110047704c0 at pc 0x7f0890c0777b bp 0x7fff91b172b0 sp 0x7fff91b172a0
+WRITE of size 216 at 0x6110047704c0 thread T0
+    #0 0x7f0890c0777a in socket_buf_init_connect /home/baptiste/3tek/kmx/kc3/http/socket_buf.c:114
+    #1 0x7f0897436e2d  (/lib/x86_64-linux-gnu/libffi.so.8+0x7e2d)
+    #2 0x7f0897433492  (/lib/x86_64-linux-gnu/libffi.so.8+0x4492)
+    #3 0x7f08940307e5 in cfn_apply /home/baptiste/3tek/kmx/kc3/libkc3/cfn.c:123
+    #4 0x7f089405c3a9 in env_eval_call_cfn /home/baptiste/3tek/kmx/kc3/libkc3/env.c:624
+    #5 0x7f089406c10f in env_eval_call /home/baptiste/3tek/kmx/kc3/libkc3/env.c:562
+    #6 0x7f0894059e7a in env_eval_tag /home/baptiste/3tek/kmx/kc3/libkc3/env.c:1761
+    #7 0x7f089405da80 in env_eval_equal_tag /home/baptiste/3tek/kmx/kc3/libkc3/env.c:1003
+    #8 0x7f08940f5d0d in tag_equal /home/baptiste/3tek/kmx/kc3/libkc3/tag.c:244
+    #9 0x7f0897436e2d  (/lib/x86_64-linux-gnu/libffi.so.8+0x7e2d)
+    #10 0x7f0897433492  (/lib/x86_64-linux-gnu/libffi.so.8+0x4492)
+    #11 0x7f08940307e5 in cfn_apply /home/baptiste/3tek/kmx/kc3/libkc3/cfn.c:123
+    #12 0x7f089405c3a9 in env_eval_call_cfn /home/baptiste/3tek/kmx/kc3/libkc3/env.c:624
+    #13 0x7f089406c10f in env_eval_call /home/baptiste/3tek/kmx/kc3/libkc3/env.c:562
+    #14 0x7f0894059e7a in env_eval_tag /home/baptiste/3tek/kmx/kc3/libkc3/env.c:1761
+    #15 0x7f089407142c in eval_tag /home/baptiste/3tek/kmx/kc3/libkc3/eval.c:19
+    #16 0x55d4c4346e9e in ikc3_run /home/baptiste/3tek/kmx/kc3/ikc3/ikc3.c:59
+    #17 0x55d4c4347636 in main /home/baptiste/3tek/kmx/kc3/ikc3/ikc3.c:143
+    #18 0x7f0893829d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
+    #19 0x7f0893829e3f in __libc_start_main_impl ../csu/libc-start.c:392
+    #20 0x55d4c4346b64 in _start (/home/baptiste/3tek/kmx/kc3/ikc3/.libs/ikc3_asan+0x4b64)
+
+0x611004770594 is located 0 bytes to the right of 212-byte region [0x6110047704c0,0x611004770594)
+allocated by thread T0 here:
+    #0 0x7f0896ab4a57 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154
+    #1 0x7f0893fc07a9 in alloc /home/baptiste/3tek/kmx/kc3/libkc3/alloc.c:25
+    #2 0x7f08940e3aee in struct_allocate /home/baptiste/3tek/kmx/kc3/libkc3/struct.c:58
+    #3 0x7f0894030124 in cfn_tag_init /home/baptiste/3tek/kmx/kc3/libkc3/cfn.c:355
+    #4 0x7f08940310db in cfn_apply /home/baptiste/3tek/kmx/kc3/libkc3/cfn.c:89
+    #5 0x7f089405c3a9 in env_eval_call_cfn /home/baptiste/3tek/kmx/kc3/libkc3/env.c:624
+    #6 0x7f089406c10f in env_eval_call /home/baptiste/3tek/kmx/kc3/libkc3/env.c:562
+    #7 0x7f0894059e7a in env_eval_tag /home/baptiste/3tek/kmx/kc3/libkc3/env.c:1761
+    #8 0x7f089405da80 in env_eval_equal_tag /home/baptiste/3tek/kmx/kc3/libkc3/env.c:1003
+    #9 0x7f08940f5d0d in tag_equal /home/baptiste/3tek/kmx/kc3/libkc3/tag.c:244
+    #10 0x7f0897436e2d  (/lib/x86_64-linux-gnu/libffi.so.8+0x7e2d)
+    #11 0x7fff91b1837f  ([stack]+0x1c37f)
+
+SUMMARY: AddressSanitizer: unknown-crash /home/baptiste/3tek/kmx/kc3/http/socket_buf.c:114 in socket_buf_init_connect
+Shadow bytes around the buggy address:
+  0x0c22808e6040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+  0x0c22808e6050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+  0x0c22808e6060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+  0x0c22808e6070: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+  0x0c22808e6080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+=>0x0c22808e6090: fa fa fa fa fa fa fa fa[00]00 00 00 00 00 00 00
+  0x0c22808e60a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
+  0x0c22808e60b0: 00 00 04 fa fa fa fa fa fa fa fa fa fa fa fa fa
+  0x0c22808e60c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+  0x0c22808e60d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+  0x0c22808e60e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
+Shadow byte legend (one shadow byte represents 8 application bytes):
+  Addressable:           00
+  Partially addressable: 01 02 03 04 05 06 07 
+  Heap left redzone:       fa
+  Freed heap region:       fd
+  Stack left redzone:      f1
+  Stack mid redzone:       f2
+  Stack right redzone:     f3
+  Stack after return:      f5
+  Stack use after scope:   f8
+  Global redzone:          f9
+  Global init order:       f6
+  Poisoned by user:        f7
+  Container overflow:      fc
+  Array cookie:            ac
+  Intra object redzone:    bb
+  ASan internal:           fe
+  Left alloca redzone:     ca
+  Right alloca redzone:    cb
+  Shadow gap:              cc
+==79724==ABORTING