Using malloc() with driver code ?!

Discussion in 'Windows Vista Drivers' started by Walter Oney, Aug 2, 2004.

  1. Walter Oney

    Walter Oney Guest

    Short answer: no, use ExAllocatePoolWithTag instead.

    Longer answer: not unless you write your own implementation, since you
    must not link with the standard C runtime library that defines this
    function.
     
    Walter Oney, Aug 2, 2004
    #1
    1. Advertisements

  2. Walter Oney

    Ray Trent Guest

    There is a limited set of string/memory manipulation routines available
    in kernel mode. It is recommended that you do not use these routines,
    because they can be security holes and may have other problems, but
    instead use the NT safe string routines (e.g. RtlCopyString). In the
    case of memcpy, RtlCopyMemory is one recommended routine (unless you
    need overlapping moves, in which case RtlMoveMemory will serve).

     
    Ray Trent, Aug 2, 2004
    #2
    1. Advertisements

  3. Walter Oney

    Phil Barila Guest

    Which devolve to macros which invoke memcpy/memmove, which are inlined
    anyway.

    malloc() can't be inlined, memcpy() can.

    Phil
    --
    Philip D. Barila Windows DDK MVP
    Seagate Technology LLC
    (720) 684-1842
    As if I need to say it: Not speaking for Seagate.
    E-mail address is pointed at a domain squatter. Use reply-to instead.
     
    Phil Barila, Aug 3, 2004
    #3
  4. Hi,

    Is it is possible to use malloc() in the driver code?

    With Regards,
    A.Ilamparithi.
     
    A.Ilamparithi, Aug 3, 2004
    #4
  5. Thanks for u r answer. I am using memcpy inside my driver. But malloc i am
    not able to. is memcpy is not in C runtime library ? Can you tell me the
    difference between malloc() and memcpy () ?

    With Thankz,
    A. Ilamparithi.
     
    A.Ilamparithi, Aug 3, 2004
    #5
  6. Walter Oney

    Ray Trent Guest

    They are *now*. We don't know what they will end up doing in the future.
    While in the case of these particular functions it's pretty reasonable
    to assume that this will always be the case, that's not necessarily so
    for the other run-time library functions. Again: better to use the
    supported routines.
     
    Ray Trent, Aug 3, 2004
    #6
  7. Walter Oney

    Phil Barila Guest

    Although it could have been interpreted that way, I was NOT advocating using
    anything but the documented kernel APIs for all of this. With that said,
    the DDK says (reading between the lines a bit) that any standard lib routine
    that can be an intrinsic is safe to use. That's a pretty small list, and is
    subject to change, so it's safer to just use the documented APIs and forget
    about it. Anything the compiler/linker can do to optimize for the
    particular platform, it will.

    If you're trying to write portable code for multiple OS types, your best bet
    is to abstract the OS into an API you can implement for each OS, then write
    your portable code to that API, and only that API.

    Phil
    --
    Philip D. Barila Windows DDK MVP
    Seagate Technology LLC
    (720) 684-1842
    As if I need to say it: Not speaking for Seagate.
    E-mail address is pointed at a domain squatter. Use reply-to instead.
     
    Phil Barila, Aug 3, 2004
    #7
  8. Walter Oney

    vipin Guest

    you can definiely use malloc provided you have it built with kernel mode
    options for use in kernel mode driver. You can write your own.The project I
    have worked on for a kernel mode driver had the c runtime libray written
    from scratch. I also noticed an open source kernel mode c runtime library in
    Mark Roddy's website.You may want to use it.

    thanks
    vipin
     
    vipin, Aug 5, 2004
    #8
  9. That's a foolish response. So what do you do? Use ExAllocatePoolWithTag to
    get a super block and then write a silly little malloc so you can allocate
    memory the way some antiquated book on C says you should allocate memory? Or
    you manage to cobble together a build and link that brings in the C library
    for which you have such a fetish. Where does malloc get it's memory when
    malloc is called? Back to that ExAllocatePoolWithTag. Paged or nonpaged?
    Foolishly pathetic programming practices promote piss poor performance.
    Early on when Pascal was running around, and C was just getting its legs
    under itself, I remember folks bragging about writing macros to change {
    and } to BEGIN and END. Dumb is as dumb does.

    Someone paid someone to write a C library from scratch for the kernel? What
    a gigantic waste of time! The string functions in the C library are all
    deprecated and unsafe to use, with recommendations to go to RtlStringXxxx
    functions for safe string manipulations.

    What in the bloody hell is WRONG with ExAllocatePool or
    ExAllocatePoolWithTag?
     
    Gary G. Little, Aug 5, 2004
    #9
  10. Walter Oney

    Ray Trent Guest

    Frankly, several things.

    ExAllocatePool fragments non paged pool if you allocate and free a lot
    of small blocks (which is often the most modular and maintainable way to
    write your code). Also, it fails. It is often possible to determine the
    total amount of memory your driver will ever need, preallocate that, and
    then have to worry less about failed memory allocations during runtime
    (or perhaps not at all if you're clever enough), or at least deal with
    them in one place rather than all over.

    Your own memory suballocator (without the limitations of lookaside
    lists) is often the smartest way to go. It's not like they're hard to
    write (dare I say that anyone that can't write a safe and efficient
    memory allocator shouldn't be writing kernel code? :).

    A lot of people are trying to write portable drivers, ExAllocatePool is,
    among all the generic OS services a driver needs, one of the most likely
    to need to be wrapped by those folks anyway.

    I personally wouldn't name my allocator "malloc" because it would be too
    easy to confuse the details of its operation with that of the standard
    Unix function, but hey... to each his own.
     
    Ray Trent, Aug 5, 2004
    #10
  11. There are some subtle differences between, for example, free() and
    ExFreePool. ExFreePool crashes on a NULL pointer.
     
    Alexander Grigoriev, Aug 5, 2004
    #11
  12. Walter Oney

    vipin Guest

    Hi Gary,
    sorry, if it hurts your sentiments.Actually the runtime c
    library I mentioned was in context of printer drivers. To maintain
    compatibility of the common piece of code for usermode and kernel mode
    drivers, such a runtime encapsulation was written.On NT4, driver is
    kernel-mode and 2000/XP, its usermode. Really the driver functionality for
    those two operating systems remain the same but with the only difference
    that one has to be built with user mode switches and the other with kernel
    mode switches.

    Beg the pardon,but don't hate me

    thanks
    vipin
     
    vipin, Aug 6, 2004
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.