Explorar o código

update: luf的成果先保存, 障碍已经搞清楚了,待解决

Wendal Chen %!s(int64=4) %!d(string=hai) anos
pai
achega
746bd7a344
Modificáronse 3 ficheiros con 86 adicións e 15 borrados
  1. 3 0
      components/luf/README.md
  2. 43 7
      components/luf/luat_luf_dump.c
  3. 40 8
      components/luf/luat_luf_undump.c

+ 3 - 0
components/luf/README.md

@@ -0,0 +1,3 @@
+# Luf 格式加载器
+
+TODO 这个库还没完成, 仅做临时保存

+ 43 - 7
components/luf/luat_luf_dump.c

@@ -29,8 +29,14 @@
 #define white2gray(x)	resetbits(x->marked, WHITEBITS)
 #define black2gray(x)	resetbit(x->marked, BLACKBIT)
 
-#define fslen(s) (sizeof(TString) + tsslen(s) + 1)
-
+// #define fslen(s) (sizeof(TString) + tsslen(s) + 1)
+static size_t fslen(TString *ts) {
+  size_t t = sizeof(TString) + tsslen(ts) + 1;
+  if (t % 0x04 != 0) {
+    t += (4 - (t % 0x04));
+  }
+  return t;
+}
 
 #define LUF_SIGNATURE "\x1cLUF"
 
@@ -94,9 +100,12 @@ static TString* spool_add(TString* ts) {
         tmp->ts[i] = ts;
         tmp->ptr[i] = (void*)(str_offset);
         str_offset += fslen(ts);
+
+        LLOGD("spool_add new %s %p", getstr(ts), tmp->ptr[i]);
         return tmp->ptr[i];
       }
       if (!strcmp(getstr(ts), getstr(tmp->ts[i]))) {
+        LLOGD("spool_add match %s %p", getstr(ts), tmp->ptr[i]);
         return tmp->ptr[i];
       }
     }
@@ -110,6 +119,7 @@ static TString* spool_add(TString* ts) {
   tmp->ts[0] = ts;
   tmp->ptr[0] = (void*)(str_offset);
   str_offset += fslen(ts);
+  LLOGD("spool_add new %s %p", getstr(ts), tmp->ptr[0]);
   return tmp->ptr[0];
 }
 
@@ -209,21 +219,31 @@ static void DumpString (const TString *s, DumpState *D) {
   ts.next = NULL;
   if (ts.tt == LUA_TSHRSTR) {
     ts.u.hnext = NULL;
-    ts.u.lnglen = ts.shrlen;
-    ts.hash = (getstr(s), ts.shrlen, 0);
+    // ts.u.lnglen = ts.shrlen;
+    // ts.hash = (getstr(s), ts.shrlen, 0);
     ts.extra = 1;
-    ts.tt = LUA_TLNGSTR;
+    // ts.tt = LUA_TLNGSTR;
   }
   white2gray((&ts));
   //LLOGD("B>DumpString %d %d", fslen(s), fd_offset);
   DumpBlock(&ts, sizeof(TString), D);
   DumpBlock(getstr(s), tsslen(s)+1, D);
+  if ((tsslen(s) + 1) % 0x04 != 0) {
+    for (size_t i = 0; i < (4-((tsslen(s) + 1) % 0x04)); i++)
+    {
+      DumpByte(0, D);
+    }
+  }
   //LLOGD("A>DumpString %d %d", fslen(s), fd_offset);
 }
 
 static void DumpCode (const Proto *f, DumpState *D) {
   //DumpInt(f->sizecode, D);
   DumpVector(f->code, f->sizecode, D);
+  for (size_t i = 0; i < f->sizecode; i++)
+  {
+    LLOGD("Code %02X -> %08X", i, f->code[i]);
+  }
 }
 
 
@@ -239,14 +259,20 @@ static void DumpConstants (const Proto *f, DumpState *D) {
   //size_t init_offset = fd_offset + sizeof(TValue) * n + sizeof(int);
   //size_t i_offset = init_offset;
   TValue tmp;
+  TString* ts;
   for (i = 0; i < n; i++) {
     const TValue *o = &f->k[i];
     switch (ttype(o)) {
       case LUA_TSHRSTR:
       case LUA_TLNGSTR:
       // {
-        memcpy(&tmp, o, sizeof(TValue));
-        tmp.value_.gc = spool_add(tsvalue(o));
+        //memcpy(&tmp, o, sizeof(TValue));
+        ts = spool_add(tsvalue(o));
+        tmp.value_.gc = ts;
+        tmp.tt_ = ttype(o);
+        // tmp.tt_ = LUA_TLNGSTR;
+        DumpBlock(&tmp, sizeof(TValue), D);
+        break;
       //   o = &tmp;
       //  init_offset += fslen(tsvalue(o)) + 1;
       //   //break;
@@ -419,6 +445,16 @@ int luf_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
   DumpHeader(&D); // 25
   DumpByte(f->sizeupvalues, &D); // 1, 26
 
+  size_t fill_offset = fd_offset % 0x04;
+  LLOGD("fix %d 0x00", fill_offset);
+  if (fill_offset != 0) {
+    for (size_t i = 0; i < (4 - fill_offset); i++)
+    {
+      DumpByte(0, &D);
+    }
+  }
+  
+
   LLOGD("after header + sizeupvalues, fd_offset %08X", fd_offset);
 
   size_t tcount = countProtoDumpSize(f);

+ 40 - 8
components/luf/luat_luf_undump.c

@@ -71,7 +71,7 @@ static void LoadBlock (LoadState *S, void *b, size_t size) {
 }
 
 static void* DistBlock (LoadState *S, size_t size) {
-  char b = 0;
+  uint8_t b = 0;
   const char* p = S->Z->p;
   for (size_t i = 0; i < size; i++)
   {
@@ -128,7 +128,16 @@ static void LoadCode (LoadState *S, Proto *f) {
   // int n = LoadInt(S);
   // LLOGD("LoadCode %d %d", n, sizeof(Instruction) * n);
   // f->sizecode = n;
+  // f->code = luat_heap_malloc(sizeof(Instruction) * f->sizecode);
+  // memcpy(f->code, S->Z->p, sizeof(Instruction) * f->sizecode);
   f->code = DistBlock(S, sizeof(Instruction) * f->sizecode);
+  //f->code = ((uint8_t*)f->code) + 2;
+  LLOGD("f->code %p", f->code);
+  for (size_t i = 0; i < f->sizecode; i++)
+  {
+    LLOGD("Code %02X -> %08X", i, f->code[i]);
+  }
+  
 }
 
 
@@ -143,7 +152,20 @@ static void LoadConstants (LoadState *S, Proto *f) {
   // 指向常数数组
   f->k = DistBlock(S, sizeof(TValue) * f->sizek);
   // 跳过字符串段
-
+  for (size_t i = 0; i < f->sizek; i++)
+  {
+    TValue *t = &f->k[i];
+    switch (ttype(t))
+    {
+    case LUA_TSHRSTR:
+    case LUA_TLNGSTR:
+      LLOGD("const string %p %s", tsvalue(t), getstr(tsvalue(t)));
+      break;
+    default:
+      break;
+    }
+  }
+  
 
   
   
@@ -417,6 +439,16 @@ LClosure *luat_luf_undump2(lua_State *L, ZIO *Z, const char *name) {
 
   checkHeader(&S);
   cl = luaF_newLclosure(L, LoadByte(&S));
+  // 有几个对齐用的字节
+  size_t fd_offset = (size_t)S.Z->p;
+  if (fd_offset % 0x04 != 0) {
+    LLOGD("skip %d 0x00", fd_offset % 0x04);
+    for (size_t i = 0; i < (4 - (fd_offset % 0x04)); i++)
+    {
+      LoadByte(&S);
+    }
+  }
+  //
   setclLvalue(L, L->top, cl);
   luaD_inctop(L);
   cl->p = luaF_newproto(L);
@@ -426,12 +458,12 @@ LClosure *luat_luf_undump2(lua_State *L, ZIO *Z, const char *name) {
   lua_assert(cl->nupvalues == cl->p->sizeupvalues);
   luai_verifycode(L, buff, cl->p);
 
-  dumpHex("& upvalues", &cl->p->upvalues[0], 8);
-  dumpHex("& upvalues[0].name", &cl->p->upvalues[0].name, 8);
-  dumpHex("> upvalues[0].name", cl->p->upvalues[0].name, 8);
-  LLOGD("> getstr(upvalues[0].name) %p", getstr(cl->p->upvalues[0].name));
-  LLOGD("> getstr(upvalues[0].name) %s", getstr(cl->p->upvalues[0].name));
-  dumpHex("head",     (char*)0x080E0000, 8);
+  // dumpHex("& upvalues", &cl->p->upvalues[0], 8);
+  // dumpHex("& upvalues[0].name", &cl->p->upvalues[0].name, 8);
+  // dumpHex("> upvalues[0].name", cl->p->upvalues[0].name, 8);
+  // LLOGD("> getstr(upvalues[0].name) %p", getstr(cl->p->upvalues[0].name));
+  // LLOGD("> getstr(upvalues[0].name) %s", getstr(cl->p->upvalues[0].name));
+  // dumpHex("head",     (char*)0x080E0000, 8);
   //dumpHex("lineinfo", cl->p->lineinfo, 8);
   //LLOGD("lineinfo %d %d", cl->p->lineinfo[0], cl->p->lineinfo[1]);