2016-12-15

Issue in Eclipse with Generated Sources

So I had a maven project in eclipse, which has a few grammars, generated by ANTLR. Once in a while, eclipse goes nuts, it fails to recognize the classes in the target/generated-sources directory.

What happened to me is that it somehow reinstated a filter in the Java Build Path. Something "reverts" the .classpath file in the project root...
  1. Right click on your project,
  2. click Properties,
  3. click on Java Build Path,
  4. find your source folder with the generated sources, and you will see Excluded: ** [means exclude all]
  5. click on it,
  6. click Edit,
  7. click on the Exclusion pattern stating **,
  8. click Remove,
  9. click Finish,
  10. now you should see Excluded: (None),
  11. click Apply,
  12. click OK,
  13. rebuild :)

2016-10-26

Mysql: MySQL Workbench 6.3 and the Options File editor

Yeah, today it wrote the query_cache_wlock_invalidate (boolean) parameter to the my.cnf file and forgot to give it some value.

You won't believe what happened next... MySQL did not woke up after the restart... FML

2016-10-25

Java: About invoke bytecode instructions

You can find the Java bytecode instruction here.
  1. invokestatic: This instruction is used to call static methods on a class. It is determined at compile time, which method will be called.
  2. invokevirtual: This calls public and protected instance methods. Of course, it needs the instance reference on the stack, and also does the runtime binding (which adds overhead compared to invokestatic, but this "overhead" is not something you want to micro-optimize by design) for method dispatching. In fact, invokevirtual is not a true instruction, the actual steps taken are determined at runtime (cf. invokestatic) by the JVM, and can even involve Reflection, maybe even prohibit JIT in-place optimizations. Note, that the method handles are determined compile time, just the call path and strategy is determined at runtime (see: polymorphism).
  3. invokeinterface: Similar to invokevirtual, just the method dispatch is based on an interface, not a class.
  4. invokespecial: Calls the constructors (as they have special names that can't be referenced from plain Java code directly [on purpose]), also private and super (class parent's) methods.
  5. invokedynamic: This was introduced to address the "performance issues" of invokevirtual in a way, that the first call is executed as an invokevirtual, and the selected call path is stored for that invocation for subsequent reuse. In fact, this dynamic method resolution works with method handles obtained runtime (see: java.lang.invoke.* methods), not at compile time.
How does this affect you? Make your methods static if possible, but don't be overzealous! That's never a good approach.

2016-10-14

Java 8: Glossary of a session

OCP
Open/Closed Principle
Open for extension, Closed for modification Principle
SAM / Function Object
Only one method in a class. See: @FunctionalInterface; eg. Callable or Runnable.
Covariant Overriding
During inheritance, an overridden method's return type can be changed to one of it's subtypes. Eg. when overriding clone().
Erasure
Type parameters are removed from the types in the class bytecode.

2016-10-13

Java: PMD AvoidStringBufferField resolution

Inside an inherited code, I got a warning from PMD, stating
StringBuffers can grow quite a lot, and so may become a source of memory leak (if the owning class has a long life time).

StringBuffers/StringBuilders can grow considerably, and so may become a source of memory leaks if held within objects with long lifetimes.
So I went ahead and replaced the StringBuilder with a StringWriter. It even has the same append method signature for strings :D

2016-09-29

Java 8: Find First Match

Oh, how I love chainable methods, especially over a stream...

// Return first match in this List, or null.
return contents.stream().filter(c -> c.name.equals(fieldName)).findFirst().orElse(null);

<3 lambda <3

2016-09-27

Java 8: Not So Poor Man's Timing

import java.time.Duration;
import java.time.Instant;

// ...

Instant start = Instant.now();
// Timed operations come here
Instant stop = Instant.now();
System.out.println("Task ran for: " + Duration.between(start, stop));

I love it.

2016-08-02

C: Linux sysinfo loads interpretation

There's this handy syscall in Linux, the sysinfo (kernel source), which returns some metrics about the current process.

What immediately stands out, is the load averages array. They are long values, how to make them to be our beloved fractional numbers?

These three values are scaled up by 65536, or more precisely by SI_LOAD_SHIFT. So we just divide them:

#include <sys/sysinfo.h>

// ...

struct sysinfo memInfo;
sysinfo(&memInfo);
printf("sysinfo: load1 = %2.2f\n", (float)memInfo.loads[0] / (float)(1 << SI_LOAD_SHIFT) );

2016-08-01

C: How to Detect LD_PRELOAD

As I'm writing a lib to be preloaded under something heavy, I tried to detect if LD_PRELOAD is set or not. For fun.

The easiest way to check is...get the environment variable! It is so straightforward, that I feel the urge to write it down :-)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 char *env_val = getenv("LD_PRELOAD");
 if (env_val[0] != 0) // May be set but value is empty.
  printf("LD_PRELOAD active: \"%s\"\n", env_val);
 return 0;
}

Note, this only checks the existence of the variable, not that if it is actually loaded...

2016-07-27

C: Sorted Walk Through Directory Contents

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

void walk_dir(const char *path)
{
 struct dirent **namelist;
 int i, n;

 n = scandir(path, &namelist, NULL/*filter*/, alphasort);
 if (n < 0)
 {
  perror("scandir");
 } else {
  for (i = 0; i < n; i++) {
   printf("Entry: %s\n", namelist[i]->d_name);
   free(namelist[i]);
  }
  free(namelist);
 }
}

int main(void)
{
 walk_dir("/tmp");
 return EXIT_SUCCESS;
}

C: Simple Walk Through Directory Contents

You can easily walk through the entries in a directory. Note, that it's unordered.

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

void walk_dir(const char *path)
{
 struct dirent *entry;
 DIR *dir = opendir(path);

 while ((entry = readdir(dir)) != NULL) {
  printf("Entry: %s\n", entry->d_name);
 }
}

int main(void)
{
 walk_dir("/tmp");
 return EXIT_SUCCESS;
}

2016-07-26

C: Prettyprinted Value aka Controlled Rounding

void fancy(long l, long nearest)
{
 char suffix[2] = "";
 long mod, val = l;
 float f;
 
 if(val < 1500) /* Too small to pretty print. */
 {
  printf("%li B", val);
 } else {
  mod = val % nearest;
  /* Where are we in the range? */
  if(mod >= (nearest - mod))
  { // hi - we are at or above the 50% of the [0-nearest[ range.
   // add the difference to reach upper nearest.
   val += (nearest - mod);
  } else { // lo
   // substract the difference to reach lower nearest.
   val -= mod;
  }
  
  if(val < 1100000L)
  {
   f = (float)val / 1000;
   suffix[0] = 'K';
   
  } else if(val < 1100000000L)
  {
   f = (float)val / 1000000;
   suffix[0] = 'M';
  } else {
   f = (float)val / 1000000000;
   suffix[0] = 'G';
  }
  
  printf("%.2f %sB", f, suffix); // Here you can play with the format.
 }
}

Usage:

 long size = 3141592535L;
 fancy(size, 1000);

2016-07-25

Some Hungarian laws #3

http://net.jogtar.hu/jr/gen/hjegy_doc.cgi?docid=A1200001.TV
2012. évi I. törvény
a munka törvénykönyvéről

7. § (1) Tilos a joggal való visszaélés. E törvény alkalmazásában joggal való visszaélés különösen, ha az mások jogos érdekeinek csorbítására, érdekérvényesítési lehetőségeinek korlátozására, zaklatására, véleménynyilvánításának elfojtására irányul vagy ehhez vezet.

Érdekesség, hogy az említett "mások jogos érdekei"-ben a "mások" vonatkozik a munkaerőpiac többi tagjára (pl. jelen és leendő munkavállalók, tehát en bloc a Piac), illetve a fizetés eltitkolása nem jogszerű érdeke a munkáltatónak, hisz az piackorlátozó hatású.

Azaz például (leendő) munkáltató nem korlátozhatja munkaszerződés-ajánlat nyilvánosságra hozását, ajánlott bér összegének továbbközlését; munkáltatóról (vagy bármi másról) vélemény formálását, mely nem tetszik a munkáltatónak, pl. felvételi folyamat kritizálása, vagy sima leírása (lásd: Glassdoor, fórumok), hiszen az nem a munkája során kerül tudomására.

A probléma az, hogy a jogalkalmazói gyakorlatban viszont a PTK üzleti titok definícióját alkalmazzák, ami szerint a munkabér kifejezetten üzleti titok.

Ám mivel a munkabérrel, kompenzációval kapcsolatban az alkalmazó és az alkalmazott is titokgazda, az alkalmazottak a saját bérezésükkel kapcsolatban nem jogosulatlan személyek, így az üzleti titoksértés nem állhat fent (nincs illetéktelenség).

A Munka Törvénykönyve szerint a munkaszerződés munkaviszonyra vonatkozó szabálytól csak a munkavállaló javára térhet el, így elvileg egy ilyen irányú tiltás alapvetően jogellenes.

Mi hát a megoldás eme szürke zónában Magyarországon? Kinek van jobb ügyvédje, ha a munkáltató pert indít...

2016-05-11

Note to self #4

Next time you screw up your default file extensions as I did (executing a .bat file resulted in an IE's View Downloads window -_- ...), look here for a fix: http://www.winhelponline.com/blog/file-asso-fixes-for-windows-7/

2016-05-09

c: How not to code #1

(gdb) bt
#0  0x00007f8ac115bebe in __lll_lock_wait_private () from /lib64/libc.so.6
#1  0x00007f8ac10f22be in _L_lock_9876 () from /lib64/libc.so.6
#2  0x00007f8ac10f05c1 in free () from /lib64/libc.so.6
#3  0x0000000000402649 in handle_sig (signo=<optimized out>, info=<optimized out>, context=<optimized out>) at lol.c:158
#4  <signal handler called>
#5  0x00007f8ac10edf03 in _int_malloc () from /lib64/libc.so.6
#6  0x00007f8ac10f06b7 in malloc () from /lib64/libc.so.6
#7  0x00000000004015ba in do_the_boogie (fd=3, gp=<optimized out>) at lol.c:715
#8  0x00000000004023cc in main (argc=5, argv=<optimized out>) at lol.c:810

LOL, it is deadlocked. The signal handler (frame #4) was invoked inside a malloc (frame #5), and the signal handler calls free in frame #3. Of course, the heap lock is held in frame #5 and both are in the same memory arena (see: break space), hence we are screwed.

2016-04-22

Note to self #3

Task:
  • Measure network throughput of <your favorite dipshit 3rd party app>
  • Report traffic over granularity periods
  • C99, _GNU_SOURCE
Actions taken:
  • Using libpcap in main thread to capture packets (also filtering, etc)
  • Piping info into another thread (simple pthread usage) that does the math
Problem encountered:
  1. When emitting all the info I read from the pipe to stdout, the average packet count is 2700..3000 packet/s
  2. When only the per GP statistics, the packet capture drops to 3-11 packet/s
Extensive head scratching intensifies.

[few days had passed]

Turned out, if I print all the info, of course it captures more tcp traffic, as I'm over an SSH connection! I'm generating that traffic!!!111one

/me idiot

Don't do Java, kids. It's bad for your brains.

2016-04-06

XML: Can I use it as a database?

A question popped up, that can (and should) we store slightly structured data in XML? For example: results of measurements that do not have any structure except timestamps and named columns.

Well, my opinion matches with this book's ideology:
XML is not a database. It was never meant to be a database. It is never going to be a database. Relational databases are proven technology with more than 20 years of implementation experience. They are solid, stable, useful products. They are not going away. XML is a very useful technology for moving data between different databases or between databases and other programs. However, it is not itself a database. Don't use it like one.

The important point here that you must grasp is, that XML is an encoding. It is a data exchange format, not a data storage format.

2016-03-18

javascript: is Element really really visible?

/**
 * Is the element visible by the Eye?
 * @param {Element | jQuery} el - object to check. Either an Element or a jQuery selection.
 * @returns {object} True if it can be seen.
 */
function isElementVisible(el) {
    "use strict";
    if (!el) // Can't find? Can't see!
        return false;
    var v_element = (el instanceof jQuery) ? el : $(el);
    if (v_element.length === 0) // Still nothing to look for
        return false;
    return v_element[0].isSameNode(document.elementFromPoint((v_element.width() / 2) + v_element.offset().left, (v_element.height() / 2) + v_element.offset().top));
}

What happens here?

We are checking if the element at the given point (our element's middle) is the same element as returned by document.elementFromPoint: then we can see it. If a different one, then that foreign element overlays the one we are looking for.

2016-03-09

linux: fun: Detect debugger and interrupt conditionally

I wanted to place breakpoints to a binary written in C, so I don't have to type in all the breakpoints to gdb, and generally just to learn new tricks :-) Because it has no real life benefits...but it's fun!

For starters, I've found out that I can send a SIGTRAP multiple ways. The easier is to just raise it:
#include <signal.h>
raise(SIGTRAP); // or SIGINT if you also like to live dangerously
The funnier way is to emit it directly in asm:
__asm__("int $3");
// or
__asm__("int3");
To be honest, the latter is a software interrupt, not a signal, but has the same effect. The IA32 Book ("Intel® 64 and IA-32 Architectures Software Developer’s Manual, Chapter 6.4.4") says:
The INT 3 instruction explicitly calls the breakpoint exception (#BP) handler.
So I'm gonna stay with "does the same", as it is a deliberately tricky one.

But if you're not running through a debugger, it will leak to the kernel and that eventually stops your program (like, saying "Trace/BPT trap". BPT --> breakpoint, see?). We need to emit this signal conditionally, which leads us to the next problem: at any given time, are we debugged/traced or not?

Time to look up some anti-debug techniques.

The first idea that got me is to simply try to have the program to debug itself [terms and conditions apply], with PTRACE_TRACEME. The trick is that this call will fail if we are already debugged/traced. Then we can just flip a flag and have our signals protected with a condition.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h> // raise, SIG*
#include <errno.h> // errno
#include <string.h> // strerror

#include <sys/ptrace.h> // ptrace
#include <sys/types.h>

int has_dbg()
{
    long rc = ptrace(PTRACE_TRACEME, 0, 0, 0);
    if (rc < 0) {
        printf("traceme resulted in %ld, errno: %d, %s\n", rc, errno, strerror(errno));
        return 1;
    } else {
        return 0;
    }
}
Oopsie, there is a problem: you can't release TRACEME from the tracee, and while TRACEME is active, you cannot attach from the outside. While this anti debug technique is awesome for blocking out debuggers, we don't want this side effect. We have to release it, but we can't do it from here. So we have to do it somewhere else :-)

I've poked around with fork-and-traceme, but it's cumbersome. But I found an elegant solution on stackoverflow (where else): Just check /proc/self/status if TracerPid is zero or not. Genius.

Caveats: ps also reads /proc/* for status info so it must be pretty standard.

So our check looks like the following:
#include <stdio.h>
#include <stdlib.h> //atoi
#include <signal.h> // raise, SIG*
#include <string.h> // strstr, strerror
#include <fcntl.h> // open
#include <unistd.h> // read, close

int has_dbg()
{
    char buf[2048], *tracer_pid;
    int debugger_present = 0; // We say "no" if not found or error, as we are cowards.
    static const char TracerPid[] = "TracerPid:";
    ssize_t num_read;

    int status_fd = open("/proc/self/status", O_RDONLY);

    if (status_fd == -1)
        return 0;

    num_read = read(status_fd, buf, sizeof(buf));
    close(status_fd);

    if (num_read > 0)
    {
        buf[num_read] = 0;
        tracer_pid = strstr(buf, TracerPid); // Look for "TracerPid"
        if (tracer_pid)
            debugger_present = !!atoi(tracer_pid + sizeof(TracerPid) - 1); // parse an int, and bool-ify it.
    }

    return debugger_present;
}

int main(int argc, char **argv)
{
    if(has_dbg()) {
        printf("can has\n");
    } else {
        printf("no no\n");
    }

    return 0;
}
And the best part is, we can call this function at any time we want.

Now, we can do things like:
#define HALP do{if(has_dbg()){__asm__("int $3");}}while(0)

// ...

printf("foo\n");
if (3.14 >= 3.14)
{
    printf("bar\n");
    HALP;
    printf("baz\n");
}
printf("quux\n");
Isn't it fancy?

Note:

If you love to cut on characters you have to type, I suggest the raise version in the long run:
  • raise variant uses 20 + n*14 chars. (headers, you know)
  • asm variant uses n*17 chars.
Okay, just kidding.

2016-02-25

Note to self #2

When validating an XML against an XSD, make sure to use the correct XSD.
No matching global declaration available for the validation root

2016-02-24

hupquote #2

Igen ez is igaz, de van egy masik oldala is. Az egesz agile tema valahol annak besimerese, hogy a szoftverfejlesztes ido es eroforrasigenye egy bizonyos meret folott nem tervezheto.
-- http://hup.hu/szavazasok/20160222/nem_agile_fejlesztunk_mert?comments_per_page=9999#comment-1962685

2016-02-19

cpp: GLIBCXX suckage

So I'm working on a pretty old system, with a new GCC compiler installed. A boost based project I just received started babbling about /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by ./XYZ).
Wtf?

Turned out, the libstdc++.so.6.0.10 contains symbols up to GLIBCXX_3.4.10, but GCC 4.8.1 only links to newer ones.
How to check: strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX

Solution: I just downgraded to GCC 4.3.4 - so long, suckers!

bash: homemade timeout replacement

So I was young and reckless, and didn't know there is a command called timeout in coreutils. This is how I managed to do it:
#!/bin/sh
{
    ./time_consuming_binary -a param -a notherparam --pleaseblock
} &
CHILDPID=$!
# Kill it after 30 sec
sleep 30
kill -9 $CHILDPID 2>&1 /dev/null
How to check if it had to be killed or not? Measure the wall time of the execution :) I've used it in a Nagios service check, the thing it watched either returned under 5 sec or blocked indefinitely (thanks, NFS), hence the 30 secs.

2016-02-08

boost: Boost.FileSystem linking struggle

Struggling with boost? SSDDBS

If you already linked boost_filesystem and boost_system but still receive errors like boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()', then you should add the following flags to your $(CPP) flags: -DBOOST_NO_CXX11_SCOPED_ENUMS -DBOOST_SYSTEM_NO_DEPRECATED

2016-02-05

Adventure Time Advice


Jake: You see this cup? This is literally my favorite cup.
* Jake throws the cup through the window *
Jake: Now it's gone forever. So it's not real and I don't care about it anymore.

2016-01-31

Some Hungarian laws #2

2012. évi C. törvény a Büntető Törvénykönyvről - II. FEJEZET - Területi és személyi hatály - 3. § (1) c)
3. § (1) A magyar büntető törvényt kell alkalmazni
a) a belföldön elkövetett bűncselekményre,
b) a Magyarország területén kívül tartózkodó magyar felségjelű úszólétesítményen vagy magyar felségjelű légi járművön elkövetett bűncselekményre,
c) a magyar állampolgár által külföldön elkövetett olyan cselekményre, amely a magyar törvény szerint bűncselekmény.

2016-01-20

music: synthwave/ outrun/ retroware playlist

  1. Carpenter Brut - FRA - playlist, bandcamp
  2. Perturbator - FRA - playlist, The Uncanny Valley, bandcamp
  3. Lazerhawk - USA (Austin, TX) - playlist, bandcamp
  4. Miami Nights 1984 - CAN (Victoria, BC) - playlist, bandcamp
  5. Mitch Murder - SWE (Stockholm) - playlistKung Fury OST, bandcamp
  6. Betamaxx - USA (Pittsburgh, PA) - playlist1, playlist2, bandcamp
  7. Power Glove - AUS (Melbourne) - playlist, Blood Dragon OST, bandcamp
  8. Vector Hold - USA (San Jose, CA) - playlist
  9. Timecop1983 - NL (Eindhoven) - playlist1, playlist2
  10. Waveshaper - SWE (Stockholm) - playlist, bandcamp
  11. Garth Knight - UK (Glasgow) - playlist1, playlist2, bandcamp
  12. Dynatron - DEN (Copenhagen) - playlist1, playlist2 
  13. Bourgeoisie - USA (Miami, FL) - playlist, bandcamp
  14. Mega Drive - USA (Dallas, TX) - playlist1, playlist2, playlist3, bandcamp
  15. Zombie Hyperdrive - GER (Halle) - track, bandcamp
...or start here: "Back To The 80's" Mix on YT

Disclaimer: Do not drive under the influence of these.

    2016-01-02

    hupquote #1

    Pedig igaza van. Amíg nem fogják fel a felsőoktatásban, hogy bizony létezik egy olyan szakma, hogy szoftvermérnökség, ami nem csak abból áll, hogy a ciklus leállási feltételét bizonyítjuk be papíron, hanem abból is, hogy hogyan írunk tiszta kódot, hogyan írunk tesztelhető kódot, hogyan és mit dokumentálunk, hogyan keresünk hibát és oldunk meg problémát, addig ne is várd, hogy a cégek elvárják a diplomát az állásokhoz. A cégeknek szoftvermérnökök kellenek. Az egyetemi oktatás nem képest ezt megadni nekik.
    -- http://hup.hu/node/144645?comments_per_page=9999#comment-1941528